-
Notifications
You must be signed in to change notification settings - Fork 28
/
js.html
275 lines (259 loc) · 10.6 KB
/
js.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>vnc-over-gif (js version)</title>
<script type="text/javascript" src="/jquery.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
var buttonStateBitset = 0
var overGif = false;
var coords = null;
var updateQueue = [];
var sending = false;
// US Keyboard layout mapping (will need to change for non-us layouts)
// [jscode, rfbcode, shift]
var keyMap = [
[8, 0xff08, 0xff08], // bs
[9, 0xff09, 0xff09], // tab
[13, 0xff0d, 0xff0d], // ent
[16, 0xffe1, 0xffe1], // shift (left)
[16, 0xffe2, 0xffe2], // shift (right) warning: jquery returns 16 for both!
[17, 0xffe3, 0xffe3], // ctrl (left)
[17, 0xffe4, 0xffe4], // ctrl (right) warning: jquery returns 17 for both!
[18, 0xffe9, 0xffe9], // alt (left)
[18, 0xffea, 0xffea], // alt (right) warning: jquery returns 18 for both!
[27, 0xff1b, 0xff1b], // esc
[32, 32, 32 ], // spc
[33, 0xff55, 0xff55], // pgup
[34, 0xff56, 0xff56], // pgdn
[35, 0xff57, 0xff57], // end
[36, 0xff50, 0xff50], // home
[37, 0xff51, 0xff51], // left
[38, 0xff52, 0xff52], // up
[39, 0xff53, 0xff53], // right
[40, 0xff54, 0xff54], // down
[45, 0xff63, 0xff63], // ins
[46, 0xffff, 0xffff], // del
[48, 48, 41 ], // 0
[49, 49, 33 ], // 1
[50, 50, 64 ], // 2
[51, 51, 35 ], // 3
[52, 52, 36 ], // 4
[53, 53, 37 ], // 5
[54, 54, 94 ], // 6
[55, 55, 38 ], // 7
[56, 56, 42 ], // 8
[57, 57, 40 ], // 9
[65, 97, 65 ], // A
[66, 98, 66 ], // B
[67, 99, 67 ], // C
[68, 100, 68 ], // D
[69, 101, 69 ], // E
[70, 102, 70 ], // F
[71, 103, 71 ], // G
[72, 104, 72 ], // H
[73, 105, 73 ], // I
[74, 106, 74 ], // J
[75, 107, 75 ], // K
[76, 108, 76 ], // L
[77, 109, 77 ], // M
[78, 110, 78 ], // N
[79, 111, 79 ], // O
[80, 112, 80 ], // P
[81, 113, 81 ], // Q
[82, 114, 82 ], // R
[83, 115, 83 ], // S
[84, 116, 84 ], // T
[85, 117, 85 ], // U
[86, 118, 86 ], // V
[87, 119, 87 ], // W
[88, 120, 88 ], // X
[89, 121, 89 ], // Y
[90, 122, 90 ], // Z
[97, 49, 49 ], // 1 (keypad)
[98, 50, 50 ], // 2 (keypad)
[99, 51, 51 ], // 3 (keypad)
[100, 52, 52 ], // 4 (keypad)
[101, 53, 53 ], // 5 (keypad)
[102, 54, 54 ], // 6 (keypad)
[103, 55, 55 ], // 7 (keypad)
[104, 56, 56 ], // 8 (keypad)
[105, 57, 57 ], // 9 (keypad)
[106, 42, 42 ], // * (keypad)
[107, 61, 61 ], // + (keypad)
[109, 45, 45 ], // - (keypad)
[110, 46, 46 ], // . (keypad)
[111, 47, 47 ], // / (keypad)
[112, 0xffbe, 0xffbe], // f1
[113, 0xffbf, 0xffbf], // f2
[114, 0xffc0, 0xffc0], // f3
[115, 0xffc1, 0xffc1], // f4
[116, 0xffc2, 0xffc2], // f5
[117, 0xffc3, 0xffc3], // f6
[118, 0xffc4, 0xffc4], // f7
[119, 0xffc5, 0xffc5], // f8
[120, 0xffc6, 0xffc6], // f9
[121, 0xffc7, 0xffc7], // f10
[122, 0xffc8, 0xffc8], // f11
[123, 0xffc9, 0xffc9], // f12
[186, 59, 58 ], // ;
[187, 61, 43 ], // =
[188, 44, 60 ], // ,
[189, 45, 95 ], // -
[190, 46, 62 ], // .
[191, 47, 63 ], // /
[192, 96, 126 ], // `
[220, 92, 124 ], // \
[221, 93, 125 ], // ]
[222, 39, 34 ], // '
[219, 91, 123 ] // [
// non-handleable keys:
// 19 - ?????? - break (no corresponding rfb code)
// 44 - ?????? - printscreen (no corresponding rfb code)
// 99 - ?????? - unknown (fn-break on my keyboard)
// 144 - ?????? - numlock (no corresponding rfb code)
// 145 - ?????? - scrolllock (no corresponding rfb code)
// ??? - 0xffe7 - meta-left (jquery does not capture)
// ??? - 0xffe8 - meta-right (jquery does not capture)
];
$.fn.disableSelection = function() {
return this
.attr('unselectable', 'on')
.css('user-select', 'none')
.on('selectstart', false);
};
$.fn.disableImageDrag = function() {
return this.on('mousedown', function() {
return false;
});
};
$.fn.disableContextMenu = function() {
return this.on('contextmenu', function() {
return false;
});
};
var toRfbKeycode = function(code, shift) {
for (var i=0, m=keyMap.length; i<m; i++) {
if (code == keyMap[i][0]) {
return keyMap[i][shift ? 2 : 1];
}
}
return null;
};
var getMouseCoords = function(el, pageX, pageY) {
var offset = $(el).offset();
return { x: pageX - offset.left, y: pageY - offset.top };
};
var createMouseUpdate = function(x, y, buttonState) {
return { event: 'mouse', x: x, y: y, buttonState: buttonState };
};
var createKeyboardUpdate = function(rfbCode, isDown) {
return { event: 'keyboard', keyCode: rfbCode, isDown: isDown };
};
var enqueueUpdate = function(update) {
updateQueue.push(update);
pumpQueue();
};
var pumpQueue = function() {
if (!sending && updateQueue.length > 0) {
sendUpdates();
}
};
var sendUpdates = function(updates) {
var data = updateQueue;
updateQueue = [];
sending = true;
$.ajax({
type: 'POST',
url: '/update',
contentType: 'application/json',
data: JSON.stringify(data),
complete: function() {
sending = false;
pumpQueue();
}
});
};
// KEYBOARD
// Rudimentary keyboard support. Doesn't yet handle the myriad of edge cases.
// See: http://unixpapa.com/js/key.html
// http://www.realvnc.com/docs/rfbproto.pdf (pg 23)
// https://www.berrange.com/posts/2010/07/04/more-than-you-or-i-ever-wanted-to-know-about-virtual-keyboard-handling/
// https://github.com/jakobadam/vnc/blob/master/src/com/tigervnc/keymap-gen.py
// https://github.com/jakobadam/vnc/blob/master/src/com/tigervnc/keymaps.csv
// Notes:
// - not all keys are accurately detectable with jquery (ex: left/right shift, ctrl, alt, numlock, etc)
// - only trap keys if the mouse is over the GIF to allow user to choose where various
// browser-meaningful keystrokes go.
$(document)
.on('keydown', function(e) {
if (overGif) {
var rfbKey = toRfbKeycode(e.keyCode, e.shiftKey);
if (rfbKey) {
enqueueUpdate(createKeyboardUpdate(rfbKey, 1));
} else {
console.log("dropping unrecognised keycode '"+e.keyCode+"'");
}
return false;
}
})
.on('keyup', function(e) {
if (overGif) {
var rfbKey = toRfbKeycode(e.keyCode, e.shiftKey);
if (rfbKey) {
enqueueUpdate(createKeyboardUpdate(rfbKey, 0));
} else {
console.log("dropping unrecognised keycode '"+e.keyCode+"'");
}
return false;
}
})
.on('keypress', function(e) {
if (overGif) {
return false;
}
});
// MOUSE
// Rudimentary mouse support.
// Notes: (applicable in chrome on win, at least)
// - prevents selection events for the image on mouse click+drag
// - prevents image drag/drop on mouse click+drag
// - prevents contextmenu on image rightclick
// - e.which is not reported accurately for mousedown/up (only mousemove) so must manually
// track which mousebuttons are down.
// - a mousemove always follows a mouseup (even when no movement - seems ok to ignore for now)
// - e.offsetX/Y is what we want, but not cross browser, so have to use pageX/Y + calc manually
$('#gif')
.disableSelection()
.disableImageDrag()
.disableContextMenu()
.on('mousedown', function(e) {
coords = getMouseCoords(this, e.pageX, e.pageY);
buttonStateBitset |= 1 << (e.which-1); // set bit corresponding to mouse button
enqueueUpdate(createMouseUpdate(coords.x, coords.y, buttonStateBitset));
})
.on('mouseup', function(e) {
coords = getMouseCoords(this, e.pageX, e.pageY);
buttonStateBitset &= ~(1 << (e.which-1)); // clear bit corresponding to mouse button
enqueueUpdate(createMouseUpdate(coords.x, coords.y, buttonStateBitset));
})
.on('mousemove', function(e) {
coords = getMouseCoords(this, e.pageX, e.pageY);
enqueueUpdate(createMouseUpdate(coords.x, coords.y, buttonStateBitset));
})
.on('mouseenter', function(e) {
overGif = true;
})
.on('mouseleave', function(e) {
overGif = false;
});
});
-->
</script>
</head>
<body>
<img id="gif" src="/screen.gif{{ query }}" />
</body>
</html>