summaryrefslogtreecommitdiff
path: root/kvmd/web/js/hid.js
blob: 64ccb0e6f6350db1d321d4653714636312a6e431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var hid = new function() {
	var __install_timer = null;
	var __installed = false;

	var __hidden_attr = null;
	var __visibility_change_attr = null;

	this.init = function() {
		keyboard.init();
		mouse.init();
	}

	this.releaseAll = function() {
		keyboard.releaseAll();
		mouse.releaseAll();
	};

	this.emitShortcut = function(...codes) {
		console.log(codes);
		var delay = 0;
		[[codes, true], [codes.slice().reverse(), false]].forEach(function(op) {
			var [op_codes, state] = op;
			op_codes.forEach(function(code) {
				setTimeout(() => keyboard.fireEvent(code, state), delay);
				delay += 100;
			});
		});
	};

	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.setSocket(ws);
					}
					keyboard.setSocket(ws);
					__installed = true;
				} else {
					tools.error("Can't resolve HID features:", http.responseText);
					__install_timer = setTimeout(() => hid.installCapture(ws), 1000);
				}
			}
		});
	};

	this.clearCapture = function() {
		if (__install_timer) {
			clearTimeout(__install_timer);
			__install_timer = null;
		}
		if (__installed) {
			mouse.setSocket(null);
			keyboard.setSocket(null);
			__installed = false;
		}
	};
}