simplified hid

This commit is contained in:
Devaev Maxim 2018-07-25 23:01:28 +03:00
parent e2e8001ba8
commit 940fe28a9f
4 changed files with 61 additions and 63 deletions

View File

@ -1,15 +1,20 @@
var hid = new function() { var hid = new function() {
var __install_timer = null; var __install_timer = null;
this.init = function() {
keyboard.init();
mouse.init();
}
this.installCapture = function(ws) { this.installCapture = function(ws) {
var http = tools.makeRequest("GET", "/kvmd/hid", function() { var http = tools.makeRequest("GET", "/kvmd/hid", function() {
if (http.readyState === 4) { if (http.readyState === 4) {
if (http.status === 200) { if (http.status === 200) {
features = JSON.parse(http.responseText).result.features; features = JSON.parse(http.responseText).result.features;
if (features.mouse) { if (features.mouse) {
mouse.installCapture(ws); mouse.setSocket(ws);
} }
keyboard.installCapture(ws); keyboard.setSocket(ws);
} else { } else {
tools.error("Can't resolve HID features:", http.responseText); tools.error("Can't resolve HID features:", http.responseText);
__install_timer = setTimeout(() => hid.installCapture(ws), 1000); __install_timer = setTimeout(() => hid.installCapture(ws), 1000);
@ -23,7 +28,7 @@ var hid = new function() {
clearTimeout(__install_timer); clearTimeout(__install_timer);
__install_timer = null; __install_timer = null;
} }
mouse.clearCapture(); mouse.setSocket(null);
keyboard.clearCapture(); keyboard.setSocket(null);
}; };
} }

View File

@ -1,23 +1,22 @@
var keyboard = new function() { var keyboard = new function() {
this.installCapture = function(ws) { var __ws = null;
// https://www.codeday.top/2017/05/03/24906.html
tools.info("Installing keyboard capture ...") this.init = function() {
document.onkeydown = (event) => __keyHandler(ws, event, true); document.onkeydown = (event) => __keyHandler(event, true);
document.onkeyup = (event) => __keyHandler(ws, event, false); document.onkeyup = (event) => __keyHandler(event, false);
$("hid-keyboard-led").className = "led-on";
}; };
this.clearCapture = function() { this.setSocket = function(ws) {
tools.info("Removing keyboard capture ...") __ws = ws;
document.onkeydown = null; $("hid-keyboard-led").className = (ws ? "led-on" : "led-off");
document.onkeyup = null;
$("hid-keyboard-led").className = "led-off";
}; };
var __keyHandler = function(ws, event, state) { var __keyHandler = function(event, state) {
// https://github.com/wesbos/keycodes/blob/gh-pages/scripts.js // https://github.com/wesbos/keycodes/blob/gh-pages/scripts.js
el_key = $(event.code); el_key = $(event.code);
if (el_key) { if (el_key) {
event.preventDefault();
tools.debug("Key", (state ? "pressed:" : "released:"), event); tools.debug("Key", (state ? "pressed:" : "released:"), event);
if (state) { if (state) {
@ -28,12 +27,13 @@ var keyboard = new function() {
el_key.removeAttribute("style"); el_key.removeAttribute("style");
} }
event.preventDefault(); if (__ws) {
ws.send(JSON.stringify({ __ws.send(JSON.stringify({
event_type: "key", event_type: "key",
key: event.code, key: event.code,
state: state, state: state,
})); }));
} }
}
}; };
}; };

View File

@ -1,5 +1,6 @@
function main () { function main () {
ui.init(); ui.init();
hid.init();
session.loadKvmdVersion(); session.loadKvmdVersion();
session.startPoller(); session.startPoller();
stream.startPoller(); stream.startPoller();

View File

@ -1,38 +1,24 @@
var mouse = new function() { var mouse = new function() {
var __send_move_timer = null; var __ws = null;
var __current_pos = {x: 0, y:0}; var __current_pos = {x: 0, y:0};
var __sent_pos = {x: 0, y:0}; var __sent_pos = {x: 0, y:0};
this.installCapture = function(ws) { this.init = function() {
tools.info("Installing mouse capture ...");
el_stream_image = $("stream-image"); el_stream_image = $("stream-image");
el_stream_image.onmousedown = (event) => __buttonHandler(ws, event, true); el_stream_image.onmousedown = (event) => __buttonHandler(event, true);
el_stream_image.onmouseup = (event) => __buttonHandler(ws, event, false); el_stream_image.onmouseup = (event) => __buttonHandler(event, false);
el_stream_image.oncontextmenu = (event) => event.preventDefault(); el_stream_image.oncontextmenu = (event) => event.preventDefault();
el_stream_image.onmousemove = __moveHandler; el_stream_image.onmousemove = __moveHandler;
el_stream_image.onwheel = (event) => __wheelHandler(ws, event); el_stream_image.onwheel = (event) => __wheelHandler(event);
__send_move_timer = setInterval(() => __sendMove(ws), 100); setInterval(__sendMove, 100);
$("hid-mouse-led").className = "led-on";
}; };
this.clearCapture = function() { this.setSocket = function(ws) {
tools.info("Removing mouse capture ..."); $("hid-mouse-led").className = (ws ? "led-on" : "led-off");
if (__send_move_timer) { __ws = ws;
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";
}; };
var __buttonHandler = function(ws, event, state) { var __buttonHandler = function(event, state) {
// https://www.w3schools.com/jsref/event_button.asp // https://www.w3schools.com/jsref/event_button.asp
switch (event.button) { switch (event.button) {
case 0: var button = "left"; break; case 0: var button = "left"; break;
@ -40,15 +26,17 @@ var mouse = new function() {
default: var button = null; break; default: var button = null; break;
} }
if (button) { if (button) {
tools.debug("Mouse button", (state ? "pressed:" : "released:"), button);
event.preventDefault(); event.preventDefault();
__sendMove(ws); tools.debug("Mouse button", (state ? "pressed:" : "released:"), button);
ws.send(JSON.stringify({ __sendMove();
if (__ws) {
__ws.send(JSON.stringify({
event_type: "mouse_button", event_type: "mouse_button",
button: button, button: button,
state: state, state: state,
})); }));
} }
}
}; };
var __moveHandler = function(event) { var __moveHandler = function(event) {
@ -59,28 +47,32 @@ var mouse = new function() {
}; };
}; };
var __sendMove = function(ws) { var __sendMove = function() {
var pos = __current_pos; var pos = __current_pos;
if (pos.x !== __sent_pos.x || pos.y !== __sent_pos.y) { if (pos.x !== __sent_pos.x || pos.y !== __sent_pos.y) {
tools.debug("Mouse move:", pos); tools.debug("Mouse move:", pos);
ws.send(JSON.stringify({ if (__ws) {
__ws.send(JSON.stringify({
event_type: "mouse_move", event_type: "mouse_move",
to: pos, to: pos,
})); }));
}
__sent_pos = pos; __sent_pos = pos;
} }
}; };
var __wheelHandler = function(ws, event) { var __wheelHandler = function(event) {
// https://learn.javascript.ru/mousewheel // https://learn.javascript.ru/mousewheel
if (event.preventDefault) { if (event.preventDefault) {
event.preventDefault(); event.preventDefault();
} }
delta = {x: event.deltaX, y: event.deltaY}; delta = {x: event.deltaX, y: event.deltaY};
tools.debug("Mouse wheel:", delta); tools.debug("Mouse wheel:", delta);
ws.send(JSON.stringify({ if (__ws) {
__ws.send(JSON.stringify({
event_type: "mouse_wheel", event_type: "mouse_wheel",
delta: delta, delta: delta,
})); }));
}
}; };
}; };