-
Notifications
You must be signed in to change notification settings - Fork 5
/
viewer.c
562 lines (544 loc) · 14.3 KB
/
viewer.c
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <rfb/keysym.h>
#include <rfb/rfbclient.h>
#include "viewer.h"
/* Return current wall clock time in microseconds. */
static suseconds_t get_time_usec()
{
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec * 1000000 + now.tv_usec;
}
/* Messages to display in a static help menu. */
static char const *viewer_help[] = {
"============ LEFT HAND ============",
"` Toggle input to viewer/VNC ",
"~ Click back-tick in VNC ",
"wasd Pan viewer ",
"q/e Zoom out/in ",
"Space Toggle display mouse pointer ",
"============ RIGHT HAND ===========",
"F10 Quit ",
"ijkl Move mouse cursor ",
"u/o Click L/R mouse button ",
"789 Toggle hold L/M/R mouse button",
"0 Click middle mouse button ",
"p Find mouse pointer ",
"=========== TOGGLE KEYS ===========",
"z/m Toggle hold L/R Control ",
"x/n Toggle hold L/R Shift ",
"c/b Toggle hold L/R Alt ",
"v Toggle hold L Meta ",
NULL
};
/* Return the RFB client the viewer is connected to. */
static struct _rfbClient *rfb(struct viewer *v)
{
return v->vnc->conn;
}
bool viewer_init(struct viewer * v, struct vnc * vnc)
{
/* All bool switches are off by default */
memset(v, 0, sizeof(struct viewer));
/* Initialise visuals */
v->view = caca_create_canvas(0, 0);
if (!v->view) {
fprintf(stderr, "Failed to create caca canvas\n");
return false;
}
v->disp = caca_create_display_with_driver(v->view, "ncurses");
if (!v->disp) {
fprintf(stderr, "Failed to create caca display\n");
return false;
}
v->vnc = vnc;
caca_set_display_title(v->disp, rfb(v)->desktopName);
/* Initialise parameters for geometry calculation */
geo_init(&v->geo, viewer_geo(v));
/* Tell VNC to place mouse pointer to default position */
viewer_vnc_send_pointer(v);
return true;
}
struct geo_facts viewer_geo(struct viewer *v)
{
return geo_facts_of(v->vnc, v->disp, v->view);
}
void viewer_disp_status(struct viewer *v)
{
caca_set_color_ansi(v->view, CACA_WHITE, CACA_BLUE);
char *conn_remark = "";
if (!v->vnc->connected) {
conn_remark = "(Disconnected)";
}
char *who_has_input = "Input to viewer";
if (v->input2vnc) {
who_has_input = "Input to VNC (~ to disengage)";
}
char held_controls[80] = { 0 };
bool disp_held_controls = false;
if (v->mouse_left) {
disp_held_controls = true;
strcat(held_controls, " LMouse");
}
if (v->mouse_middle) {
disp_held_controls = true;
strcat(held_controls, " MMouse");
}
if (v->mouse_right) {
disp_held_controls = true;
strcat(held_controls, " RMouse");
}
if (v->hold_lctrl) {
disp_held_controls = true;
strcat(held_controls, " LCtrl");
}
if (v->hold_lshift) {
disp_held_controls = true;
strcat(held_controls, " LShift");
}
if (v->hold_lalt) {
disp_held_controls = true;
strcat(held_controls, " LAlt");
}
if (v->hold_lsuper) {
disp_held_controls = true;
strcat(held_controls, " LSuper");
}
if (v->hold_ralt) {
disp_held_controls = true;
strcat(held_controls, " RAlt");
}
if (v->hold_rshift) {
disp_held_controls = true;
strcat(held_controls, " RShift");
}
if (v->hold_rctrl) {
disp_held_controls = true;
strcat(held_controls, " RCtrl");
}
char held_controls_msg[100] = { 0 };
if (disp_held_controls) {
strcat(held_controls_msg, "| Holding down:");
strcat(held_controls_msg, held_controls);
}
caca_printf(v->view, 0, 0, "h:Help | %s:%d%s | %s %s",
rfb(v)->serverHost, rfb(v)->serverPort, conn_remark,
who_has_input, held_controls_msg);
}
void viewer_disp_help(struct viewer *v)
{
caca_set_color_ansi(v->view, CACA_WHITE, CACA_BLUE);
int i;
for (i = 0; viewer_help[i] != NULL; i++) {
caca_put_str(v->view, 0, 1 + i, viewer_help[i]);
}
}
void viewer_redraw(struct viewer *v)
{
caca_clear_canvas(v->view);
/*
* Run the latest frame-buffer content through Floyd–Steinberg algorithm -
* it seems to offer higher quality over other algorithm choices.
* The dither parameters are tailored for a connection on 32-bit RGB colours.
*/
if (v->fb_dither != NULL) {
caca_free_dither(v->fb_dither);
}
struct geo_facts facts = viewer_geo(v);
v->fb_dither =
caca_create_dither(32, facts.vnc_width, facts.vnc_height,
facts.vnc_width * 4, 0x000000ff, 0x0000ff00,
0x00ff0000, 0);
caca_set_dither_algorithm(v->fb_dither, "fstein");
caca_set_dither_gamma(v->fb_dither, 1.0);
struct geo_dither_params params =
geo_get_dither_params(&v->geo, viewer_geo(v));
caca_dither_bitmap(v->view, params.x, params.y, params.width,
params.height, v->fb_dither, rfb(v)->frameBuffer);
/*
* Mouse cursors are usually wider than 14 pixels. If it will not take
* more than 5 characters to draw the cusor, then consider it very
* difficult to spot on the VNC canvas, and draw a red block right there.
*/
int mouse_ch_x = geo_dither_ch_px_x(¶ms, v->geo.mouse_x);
int mouse_ch_y = geo_dither_ch_px_y(¶ms, v->geo.mouse_y);
if (geo_dither_numch_x(¶ms, 12) < 5) {
caca_set_color_ansi(v->view, CACA_WHITE, CACA_RED);
caca_fill_box(v->view, mouse_ch_x - 1, mouse_ch_y - 1, 3, 3,
'*');
}
/* Draw local mouse pointer */
if (v->draw_mouse_pointer) {
caca_set_color_ansi(v->view, CACA_WHITE, CACA_RED);
caca_put_char(v->view, mouse_ch_x, mouse_ch_y, '*');
}
viewer_disp_status(v);
if (v->disp_help) {
viewer_disp_help(v);
}
caca_refresh_display(v->disp);
}
void viewer_ev_loop(struct viewer *v)
{
int ev_accept =
CACA_EVENT_KEY_PRESS | CACA_EVENT_RESIZE | CACA_EVENT_QUIT;
while (true) {
/* Listen to the latest event */
caca_event_t ev;
caca_get_event(v->disp, ev_accept, &ev,
VIEWER_FRAME_INTVL_USEC);
/* Certain types of events are caca calling quit */
enum caca_event_type ev_type = caca_get_event_type(&ev);
if (ev_type & CACA_EVENT_QUIT || ev_type & CACA_EVENT_NONE) {
return;
}
/* Handle previously banked escape key (VNC input), send it to VNC. */
if (v->last_vnc_esc != 0
&& get_time_usec() - v->last_vnc_esc >=
VIEWER_FRAME_INTVL_USEC) {
v->last_vnc_esc = 0;
viewer_vnc_click_key(v, cacakey2vnc(CACA_KEY_ESCAPE));
}
/* Redraw at a constant frame rate when there is no key input */
if (!(ev_type & CACA_EVENT_KEY_PRESS)) {
viewer_redraw(v);
continue;
}
int ev_char = caca_get_event_key_ch(&ev);
/* Input never gets directed at VNC if it is disconnected */
if (!v->vnc->connected) {
v->input2vnc = false;
}
/* A key input is directed at either VNC or viewer controls */
if (v->input2vnc && ev_char != '`') {
viewer_input_to_vnc(v, ev_char);
} else if (!viewer_handle_control(v, ev_char)) {
return;
}
}
}
void viewer_vnc_click_key(struct viewer *v, int vnc_key)
{
SendKeyEvent(rfb(v), vnc_key, TRUE);
SendKeyEvent(rfb(v), vnc_key, FALSE);
}
void viewer_vnc_click_ctrl_key_combo(struct viewer *v, int vnc_key)
{
SendKeyEvent(rfb(v), XK_Control_L, TRUE);
SendKeyEvent(rfb(v), vnc_key, TRUE);
SendKeyEvent(rfb(v), vnc_key, FALSE);
SendKeyEvent(rfb(v), XK_Control_L, FALSE);
}
void viewer_vnc_toggle_key(struct viewer *v, int vnc_key, bool key_down)
{
SendKeyEvent(rfb(v), vnc_key, key_down ? TRUE : FALSE);
}
void viewer_vnc_send_pointer(struct viewer *v)
{
int mask = 0;
if (v->mouse_left) {
mask |= rfbButton1Mask;
}
if (v->mouse_middle) {
mask |= rfbButton2Mask;
}
if (v->mouse_right) {
mask |= rfbButton3Mask;
}
SendPointerEvent(rfb(v), v->geo.mouse_x, v->geo.mouse_y, mask);
}
void viewer_input_to_vnc(struct viewer *v, int caca_key)
{
/*
* Escape key could mean two things, either it is a key press by itself, or it
* could be an Alt key combination. An Alt key combination is sent in two key
* events, first event is an escape key, second is the combo key.
* To distinguish between the two meanings, the escape key is banked here and
* dealt with later.
*/
if (caca_key == CACA_KEY_ESCAPE) {
v->last_vnc_esc = get_time_usec();
return;
}
/*
* Control sequences can only be seen by terminal in lower case letters a-z,
* their key code ranges from 1(a) to 26(z). In four cases the control sequence
* also generates an extra key event that has to be discarded.
*/
if (caca_key >= 1 && caca_key <= 26) {
switch (caca_key) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7: /* Ctrl+ABCDEFG */
case 10:
case 11:
case 12: /* (missing H,I) Ctrl+JKL */
case 14:
case 15:
case 16:
case 17:
case 18: /* (missing M) Ctrl+NOPQR */
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26: /* (missing S) Ctrl+TUVWXYZ */
viewer_vnc_click_ctrl_key_combo(v, caca_key - 1 + 'a');
return;
case 8: /* H and an extra backspace */
v->void_backsp = true;
viewer_vnc_click_ctrl_key_combo(v, 'h');
return;
case 9: /* I and an extra tab */
v->void_tab = true;
viewer_vnc_click_ctrl_key_combo(v, 'i');
return;
case 13: /* M and an extra enter */
v->void_ret = true;
viewer_vnc_click_ctrl_key_combo(v, 'm');
return;
case 19: /* S and an extra pause */
v->void_pause = true;
viewer_vnc_click_ctrl_key_combo(v, 's');
return;
}
}
/*
* If the previous key was a control sequence that came with an extra key
* event, discard it and do not send it over VNC.
*/
switch (caca_key) {
case CACA_KEY_BACKSPACE:
if (v->void_backsp) {
v->void_backsp = false;
return;
}
break;
case CACA_KEY_TAB:
if (v->void_tab) {
v->void_tab = false;
return;
}
break;
case CACA_KEY_RETURN:
if (v->void_ret) {
v->void_ret = false;
return;
}
case CACA_KEY_PAUSE:
if (v->void_pause) {
v->void_pause = false;
return;
}
}
/* Some key codes from caca must be translated to VNC key codes before use */
int translated_ch = cacakey2vnc(caca_key);
if (translated_ch == -1) {
rfbClientErr("Unknown key %d is not sent to VNC\n", caca_key);
return;
}
/*
* In case there was a banked escape key, the Alt combination key shall
* arrive pretty soon, and most definitely before the next frame refresh.
* Occasionally it takes even longer to arrive but there is no way to
* work around it.
*/
suseconds_t elapsed = get_time_usec() - v->last_vnc_esc;
if (elapsed < VIEWER_FRAME_INTVL_USEC
&& elapsed < VIEWER_MAX_INPUT_INTVL_USEC) {
viewer_vnc_toggle_key(v, XK_Alt_L, true);
viewer_vnc_click_key(v, translated_ch);
viewer_vnc_toggle_key(v, XK_Alt_L, false);
v->last_vnc_esc = 0;
return;
}
/* Finally the key code can be sent to VNC! */
viewer_vnc_click_key(v, translated_ch);
}
bool viewer_handle_control(struct viewer * v, int caca_key)
{
/*
* When the canvas is too busy panning/zooming, causing very low FPS,
* the terminal occasionally generates repeatitive key input seemingly
* out of nowhere, in very short successions. To work around it, this
* condition caps number of viewe controls to approximately 10 per second.
*/
suseconds_t elapsed = get_time_usec() - v->last_viewer_control;
if (v->last_viewer_control != 0
&& elapsed < VIEWER_MAX_INPUT_INTVL_USEC) {
return true;
}
/*
* In order to avoid redrawing too rapidly, only viewer zoom/pan
* actions redraw immediately.
* VNC input has to wait for main loop to redraw at an interval.
*/
switch (caca_key) {
case 'h':
case 'H':
v->disp_help = !v->disp_help;
break;
case CACA_KEY_F10:
return false;
/* Left hand */
case 'w':
case 'W':
geo_pan(&v->geo, 0, -1);
viewer_redraw(v);
break;
case 'a':
case 'A':
geo_pan(&v->geo, -1, 0);
viewer_redraw(v);
break;
case 's':
case 'S':
geo_pan(&v->geo, 0, 1);
viewer_redraw(v);
break;
case 'd':
case 'D':
geo_pan(&v->geo, 1, 0);
viewer_redraw(v);
break;
case 'q':
case 'Q':
geo_zoom(&v->geo, viewer_geo(v), -1);
viewer_redraw(v);
break;
case 'e':
case 'E':
geo_zoom(&v->geo, viewer_geo(v), 1);
viewer_redraw(v);
break;
case '`':
if (v->vnc->connected) {
v->input2vnc = !v->input2vnc;
}
break;
case '~':
/* Click back-tick (96 in ASCII) in VNC */
viewer_vnc_click_key(v, 96);
break;
case ' ':
v->draw_mouse_pointer = !v->draw_mouse_pointer;
break;
/* Right hand */
case 'i':
case 'I':
geo_move_mouse(&v->geo, viewer_geo(v), 0, -1);
viewer_vnc_send_pointer(v);
break;
case 'j':
case 'J':
geo_move_mouse(&v->geo, viewer_geo(v), -1, 0);
viewer_vnc_send_pointer(v);
break;
case 'k':
case 'K':
geo_move_mouse(&v->geo, viewer_geo(v), 0, 1);
viewer_vnc_send_pointer(v);
break;
case 'l':
case 'L':
geo_move_mouse(&v->geo, viewer_geo(v), 1, 0);
viewer_vnc_send_pointer(v);
break;
case 'u':
case 'U':
v->mouse_left = true;
viewer_vnc_send_pointer(v);
v->mouse_left = false;
viewer_vnc_send_pointer(v);
break;
case 'o':
case 'O':
v->mouse_right = true;
viewer_vnc_send_pointer(v);
v->mouse_right = false;
viewer_vnc_send_pointer(v);
break;
case '7':
v->mouse_left = !v->mouse_left;
viewer_vnc_send_pointer(v);
break;
case '8':
v->mouse_middle = !v->mouse_middle;
viewer_vnc_send_pointer(v);
break;
case '9':
v->mouse_right = !v->mouse_right;
viewer_vnc_send_pointer(v);
break;
case '0':
v->mouse_middle = true;
viewer_vnc_send_pointer(v);
v->mouse_middle = false;
viewer_vnc_send_pointer(v);
break;
case 'p':
case 'P':
geo_zoom_to_cursor(&v->geo, viewer_geo(v));
break;
/* Toggle keys */
case 'z':
case 'Z':
v->hold_lctrl = !v->hold_lctrl;
viewer_vnc_toggle_key(v, XK_Control_L, v->hold_lctrl);
break;
case 'm':
case 'M':
v->hold_rctrl = !v->hold_rctrl;
viewer_vnc_toggle_key(v, XK_Control_R, v->hold_rctrl);
break;
case 'x':
case 'X':
v->hold_lshift = !v->hold_lshift;
viewer_vnc_toggle_key(v, XK_Shift_L, v->hold_lshift);
break;
case 'n':
case 'N':
v->hold_rshift = !v->hold_rshift;
viewer_vnc_toggle_key(v, XK_Shift_R, v->hold_rshift);
break;
case 'c':
case 'C':
v->hold_lalt = !v->hold_lalt;
viewer_vnc_toggle_key(v, XK_Alt_L, v->hold_lalt);
break;
case 'b':
case 'B':
v->hold_ralt = !v->hold_ralt;
viewer_vnc_toggle_key(v, XK_Alt_R, v->hold_ralt);
break;
case 'v':
case 'V':
v->hold_lsuper = !v->hold_lsuper;
viewer_vnc_toggle_key(v, XK_Super_L, v->hold_lsuper);
break;
}
v->last_viewer_control = get_time_usec();
return true;
}
void viewer_terminate(struct viewer *v)
{
if (v->fb_dither != NULL) {
caca_free_dither(v->fb_dither);
}
if (v->disp != NULL) {
caca_free_display(v->disp);
}
if (v->view != NULL) {
caca_free_canvas(v->view);
}
}