summaryrefslogtreecommitdiff
path: root/kvmd
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2018-07-25 23:01:28 +0300
committerDevaev Maxim <[email protected]>2018-07-25 23:01:28 +0300
commit940fe28a9f717236a24f18930d14ff2804155480 (patch)
treea98117ae6fdbc990b4b987de0b17a18c2cd66d31 /kvmd
parente2e8001ba8dec95742e6c121c7252b4fd5634b7d (diff)
simplified hid
Diffstat (limited to 'kvmd')
-rw-r--r--kvmd/web/js/hid.js13
-rw-r--r--kvmd/web/js/keyboard.js36
-rw-r--r--kvmd/web/js/main.js1
-rw-r--r--kvmd/web/js/mouse.js74
4 files changed, 61 insertions, 63 deletions
diff --git a/kvmd/web/js/hid.js b/kvmd/web/js/hid.js
index 3d0ed961..2e5538da 100644
--- a/kvmd/web/js/hid.js
+++ b/kvmd/web/js/hid.js
@@ -1,15 +1,20 @@
var hid = new function() {
var __install_timer = null;
+ this.init = function() {
+ keyboard.init();
+ mouse.init();
+ }
+
this.installCapture = function(ws) {
var http = tools.makeRequest("GET", "/kvmd/hid", function() {
if (http.readyState === 4) {
if (http.status === 200) {
features = JSON.parse(http.responseText).result.features;
if (features.mouse) {
- mouse.installCapture(ws);
+ mouse.setSocket(ws);
}
- keyboard.installCapture(ws);
+ keyboard.setSocket(ws);
} else {
tools.error("Can't resolve HID features:", http.responseText);
__install_timer = setTimeout(() => hid.installCapture(ws), 1000);
@@ -23,7 +28,7 @@ var hid = new function() {
clearTimeout(__install_timer);
__install_timer = null;
}
- mouse.clearCapture();
- keyboard.clearCapture();
+ mouse.setSocket(null);
+ keyboard.setSocket(null);
};
}
diff --git a/kvmd/web/js/keyboard.js b/kvmd/web/js/keyboard.js
index 798b9192..e2d02093 100644
--- a/kvmd/web/js/keyboard.js
+++ b/kvmd/web/js/keyboard.js
@@ -1,23 +1,22 @@
var keyboard = new function() {
- this.installCapture = function(ws) {
- // https://www.codeday.top/2017/05/03/24906.html
- tools.info("Installing keyboard capture ...")
- document.onkeydown = (event) => __keyHandler(ws, event, true);
- document.onkeyup = (event) => __keyHandler(ws, event, false);
- $("hid-keyboard-led").className = "led-on";
+ var __ws = null;
+
+ this.init = function() {
+ document.onkeydown = (event) => __keyHandler(event, true);
+ document.onkeyup = (event) => __keyHandler(event, false);
};
- this.clearCapture = function() {
- tools.info("Removing keyboard capture ...")
- document.onkeydown = null;
- document.onkeyup = null;
- $("hid-keyboard-led").className = "led-off";
+ this.setSocket = function(ws) {
+ __ws = ws;
+ $("hid-keyboard-led").className = (ws ? "led-on" : "led-off");
};
- var __keyHandler = function(ws, event, state) {
+ var __keyHandler = function(event, state) {
// https://github.com/wesbos/keycodes/blob/gh-pages/scripts.js
el_key = $(event.code);
if (el_key) {
+ event.preventDefault();
+
tools.debug("Key", (state ? "pressed:" : "released:"), event);
if (state) {
@@ -28,12 +27,13 @@ var keyboard = new function() {
el_key.removeAttribute("style");
}
- event.preventDefault();
- ws.send(JSON.stringify({
- event_type: "key",
- key: event.code,
- state: state,
- }));
+ if (__ws) {
+ __ws.send(JSON.stringify({
+ event_type: "key",
+ key: event.code,
+ state: state,
+ }));
+ }
}
};
};
diff --git a/kvmd/web/js/main.js b/kvmd/web/js/main.js
index 5a55e966..bf5e8b36 100644
--- a/kvmd/web/js/main.js
+++ b/kvmd/web/js/main.js
@@ -1,5 +1,6 @@
function main () {
ui.init();
+ hid.init();
session.loadKvmdVersion();
session.startPoller();
stream.startPoller();
diff --git a/kvmd/web/js/mouse.js b/kvmd/web/js/mouse.js
index 468c9449..0f2379fe 100644
--- a/kvmd/web/js/mouse.js
+++ b/kvmd/web/js/mouse.js
@@ -1,38 +1,24 @@
var mouse = new function() {
- var __send_move_timer = null;
+ var __ws = null;
var __current_pos = {x: 0, y:0};
var __sent_pos = {x: 0, y:0};
- this.installCapture = function(ws) {
- tools.info("Installing mouse capture ...");
+ this.init = function() {
el_stream_image = $("stream-image");
- el_stream_image.onmousedown = (event) => __buttonHandler(ws, event, true);
- el_stream_image.onmouseup = (event) => __buttonHandler(ws, event, false);
+ el_stream_image.onmousedown = (event) => __buttonHandler(event, true);
+ el_stream_image.onmouseup = (event) => __buttonHandler(event, false);
el_stream_image.oncontextmenu = (event) => event.preventDefault();
el_stream_image.onmousemove = __moveHandler;
- el_stream_image.onwheel = (event) => __wheelHandler(ws, event);
- __send_move_timer = setInterval(() => __sendMove(ws), 100);
- $("hid-mouse-led").className = "led-on";
+ el_stream_image.onwheel = (event) => __wheelHandler(event);
+ setInterval(__sendMove, 100);
};
- this.clearCapture = function() {
- tools.info("Removing mouse capture ...");
- if (__send_move_timer) {
- clearInterval(__send_move_timer);
- __send_move_timer = null;
- }
- __current_pos = {x: 0, y:0};
- __sent_pos = {x: 0, y:0};
- el_stream_image = $("stream-image");
- el_stream_image.onmousedown = null;
- el_stream_image.onmouseup = null;
- el_stream_image.oncontextmenu = null;
- el_stream_image.onmousemove = null;
- el_stream_image.onwheel = null;
- $("hid-mouse-led").className = "led-off";
+ this.setSocket = function(ws) {
+ $("hid-mouse-led").className = (ws ? "led-on" : "led-off");
+ __ws = ws;
};
- var __buttonHandler = function(ws, event, state) {
+ var __buttonHandler = function(event, state) {
// https://www.w3schools.com/jsref/event_button.asp
switch (event.button) {
case 0: var button = "left"; break;
@@ -40,14 +26,16 @@ var mouse = new function() {
default: var button = null; break;
}
if (button) {
- tools.debug("Mouse button", (state ? "pressed:" : "released:"), button);
event.preventDefault();
- __sendMove(ws);
- ws.send(JSON.stringify({
- event_type: "mouse_button",
- button: button,
- state: state,
- }));
+ tools.debug("Mouse button", (state ? "pressed:" : "released:"), button);
+ __sendMove();
+ if (__ws) {
+ __ws.send(JSON.stringify({
+ event_type: "mouse_button",
+ button: button,
+ state: state,
+ }));
+ }
}
};
@@ -59,28 +47,32 @@ var mouse = new function() {
};
};
- var __sendMove = function(ws) {
+ var __sendMove = function() {
var pos = __current_pos;
if (pos.x !== __sent_pos.x || pos.y !== __sent_pos.y) {
tools.debug("Mouse move:", pos);
- ws.send(JSON.stringify({
- event_type: "mouse_move",
- to: pos,
- }));
+ if (__ws) {
+ __ws.send(JSON.stringify({
+ event_type: "mouse_move",
+ to: pos,
+ }));
+ }
__sent_pos = pos;
}
};
- var __wheelHandler = function(ws, event) {
+ var __wheelHandler = function(event) {
// https://learn.javascript.ru/mousewheel
if (event.preventDefault) {
event.preventDefault();
}
delta = {x: event.deltaX, y: event.deltaY};
tools.debug("Mouse wheel:", delta);
- ws.send(JSON.stringify({
- event_type: "mouse_wheel",
- delta: delta,
- }));
+ if (__ws) {
+ __ws.send(JSON.stringify({
+ event_type: "mouse_wheel",
+ delta: delta,
+ }));
+ }
};
};