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
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <FL/fl_draw.H>
#include <rfb/CMsgWriter.h>
#include <rfb/LogWriter.h>
// FLTK can pull in the X11 headers on some systems
#ifndef XK_VoidSymbol
#define XK_MISCELLANY
#define XK_XKB_KEYS
#include <rfb/keysymdef.h>
#endif
#include "Viewport.h"
#include "CConn.h"
#include "i18n.h"
#include "parameters.h"
#include "keysym2ucs.h"
// FLTK STR #2599 must be fixed for proper dead keys support
#ifndef HAVE_FLTK_DEAD_KEYS
#define event_compose_symbol event_text
#endif
using namespace rfb;
#ifdef __APPLE__
extern "C" const char *osx_event_string(void);
#endif
extern void exit_vncviewer();
static rfb::LogWriter vlog("Viewport");
Viewport::Viewport(int w, int h, const rfb::PixelFormat& serverPF, CConn* cc_)
: Fl_Widget(0, 0, w, h), cc(cc_), frameBuffer(NULL), pixelTrans(NULL),
lastPointerPos(0, 0), lastButtonMask(0)
{
frameBuffer = new ManagedPixelBuffer(getPreferredPF(), w, h);
assert(frameBuffer);
setServerPF(serverPF);
}
Viewport::~Viewport()
{
// Unregister all timeouts in case they get a change tro trigger
// again later when this object is already gone.
Fl::remove_timeout(handleUpdateTimeout, this);
Fl::remove_timeout(handleColourMap, this);
Fl::remove_timeout(handlePointerTimeout, this);
delete frameBuffer;
if (pixelTrans)
delete pixelTrans;
}
void Viewport::setServerPF(const rfb::PixelFormat& pf)
{
if (pixelTrans)
delete pixelTrans;
pixelTrans = NULL;
if (pf.equal(getPreferredPF()))
return;
pixelTrans = new PixelTransformer();
pixelTrans->init(pf, &colourMap, getPreferredPF());
}
const rfb::PixelFormat &Viewport::getPreferredPF()
{
static PixelFormat prefPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
return prefPF;
}
// setColourMapEntries() changes some of the entries in the colourmap.
// Unfortunately these messages are often sent one at a time, so we delay the
// settings taking effect by 100ms. This is because recalculating the internal
// translation table can be expensive.
void Viewport::setColourMapEntries(int firstColour, int nColours,
rdr::U16* rgbs)
{
for (int i = 0; i < nColours; i++)
colourMap.set(firstColour+i, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
if (!Fl::has_timeout(handleColourMap, this))
Fl::add_timeout(0.100, handleColourMap, this);
}
// Copy the areas of the framebuffer that have been changed (damaged)
// to the displayed window.
void Viewport::updateWindow()
{
Rect r;
Fl::remove_timeout(handleUpdateTimeout, this);
r = damage.get_bounding_rect();
Fl_Widget::damage(FL_DAMAGE_USER1, r.tl.x + x(), r.tl.y + y(), r.width(), r.height());
damage.clear();
}
void Viewport::draw()
{
int X, Y, W, H;
int pixel_bytes, stride_bytes;
const uchar *buf_start;
// Check what actually needs updating
fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
if ((W == 0) || (H == 0))
return;
pixel_bytes = frameBuffer->getPF().bpp/8;
stride_bytes = pixel_bytes * frameBuffer->getStride();
buf_start = frameBuffer->data +
pixel_bytes * (X - x()) +
stride_bytes * (Y - y());
// FIXME: Check how efficient this thing really is
fl_draw_image(buf_start, X, Y, W, H, pixel_bytes, stride_bytes);
}
int Viewport::handle(int event)
{
int buttonMask, wheelMask;
DownMap::const_iterator iter;
switch (event) {
case FL_ENTER:
// Yes, we would like some pointer events please!
return 1;
case FL_PUSH:
case FL_RELEASE:
case FL_DRAG:
case FL_MOVE:
case FL_MOUSEWHEEL:
buttonMask = 0;
if (Fl::event_button1())
buttonMask |= 1;
if (Fl::event_button2())
buttonMask |= 2;
if (Fl::event_button3())
buttonMask |= 4;
if (event == FL_MOUSEWHEEL) {
if (Fl::event_dy() < 0)
wheelMask = 8;
else
wheelMask = 16;
// A quick press of the wheel "button", followed by a immediate
// release below
handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()),
buttonMask | wheelMask);
}
handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), buttonMask);
return 1;
case FL_FOCUS:
// Yes, we would like some focus please!
return 1;
case FL_UNFOCUS:
// Release all keys that were pressed as that generally makes most
// sense (e.g. Alt+Tab where we only see the Alt press)
for (iter = downKeySym.begin();iter != downKeySym.end();++iter)
cc->writer()->keyEvent(iter->second, false);
downKeySym.clear();
return 1;
case FL_KEYDOWN:
handleKeyEvent(Fl::event_key(), Fl::event_compose_symbol(), true);
return 1;
case FL_KEYUP:
handleKeyEvent(Fl::event_key(), Fl::event_compose_symbol(), false);
return 1;
}
return Fl_Widget::handle(event);
}
void Viewport::handleUpdateTimeout(void *data)
{
Viewport *self = (Viewport *)data;
assert(self);
self->updateWindow();
}
void Viewport::handleColourMap(void *data)
{
Viewport *self = (Viewport *)data;
assert(self);
if (self->pixelTrans != NULL)
self->pixelTrans->setColourMapEntries(0, 0);
self->Fl_Widget::damage(FL_DAMAGE_ALL);
}
void Viewport::handlePointerEvent(const rfb::Point& pos, int buttonMask)
{
if (!viewOnly) {
if (pointerEventInterval == 0 || buttonMask != lastButtonMask) {
cc->writer()->pointerEvent(pos, buttonMask);
} else {
if (!Fl::has_timeout(handlePointerTimeout, this))
Fl::add_timeout((double)pointerEventInterval/1000.0,
handlePointerTimeout, this);
}
lastPointerPos = pos;
lastButtonMask = buttonMask;
}
}
void Viewport::handlePointerTimeout(void *data)
{
Viewport *self = (Viewport *)data;
assert(self);
self->cc->writer()->pointerEvent(self->lastPointerPos, self->lastButtonMask);
}
rdr::U32 Viewport::translateKeyEvent(int keyCode, const char *keyText)
{
unsigned ucs;
// First check for function keys
if ((keyCode > FL_F) && (keyCode <= FL_F_Last))
return XK_F1 + (keyCode - FL_F - 1);
// Numpad numbers
if ((keyCode >= (FL_KP + '0')) && (keyCode <= (FL_KP + '9')))
return XK_KP_0 + (keyCode - (FL_KP + '0'));
// Then other special keys
switch (keyCode) {
case FL_BackSpace:
return XK_BackSpace;
case FL_Tab:
return XK_Tab;
case FL_Enter:
return XK_Return;
case FL_Pause:
return XK_Pause;
case FL_Scroll_Lock:
return XK_Scroll_Lock;
case FL_Escape:
return XK_Escape;
case FL_Home:
return XK_Home;
case FL_Left:
return XK_Left;
case FL_Up:
return XK_Up;
case FL_Right:
return XK_Right;
case FL_Down:
return XK_Down;
case FL_Page_Up:
return XK_Page_Up;
case FL_Page_Down:
return XK_Page_Down;
case FL_End:
return XK_End;
case FL_Print:
return XK_Print;
case FL_Insert:
return XK_Insert;
case FL_Menu:
return XK_Menu;
case FL_Help:
return XK_Help;
case FL_Num_Lock:
return XK_Num_Lock;
case FL_Shift_L:
return XK_Shift_L;
case FL_Shift_R:
return XK_Shift_R;
case FL_Control_L:
return XK_Control_L;
case FL_Control_R:
return XK_Control_R;
case FL_Caps_Lock:
return XK_Caps_Lock;
case FL_Meta_L:
return XK_Super_L;
case FL_Meta_R:
return XK_Super_R;
case FL_Alt_L:
return XK_Alt_L;
case FL_Alt_R:
return XK_Alt_R;
case FL_Delete:
return XK_Delete;
case FL_KP_Enter:
return XK_KP_Enter;
case FL_KP + '=':
return XK_KP_Equal;
case FL_KP + '*':
return XK_KP_Multiply;
case FL_KP + '+':
return XK_KP_Add;
case FL_KP + ',':
return XK_KP_Separator;
case FL_KP + '-':
return XK_KP_Subtract;
case FL_KP + '.':
return XK_KP_Decimal;
case FL_KP + '/':
return XK_KP_Divide;
case XK_ISO_Level3_Shift:
// FLTK tends to let this one leak through on X11...
return XK_ISO_Level3_Shift;
}
// Ctrl and Cmd tend to fudge input handling, so we need to cheat here
if (Fl::event_state() & (FL_COMMAND | FL_CTRL)) {
#ifdef WIN32
BYTE keystate[256];
WCHAR wbuf[8];
int ret;
static char buf[32];
switch (fl_msg.message) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
// Most buttons end up here when Ctrl is pressed. Here we can pretend
// that Ctrl isn't pressed, and do a character lookup.
GetKeyboardState(keystate);
keystate[VK_CONTROL] = keystate[VK_LCONTROL] = keystate[VK_RCONTROL] = 0;
ret = ToUnicode(fl_msg.wParam, 0, keystate, wbuf, sizeof(wbuf)/sizeof(wbuf[0]), 0);
if (ret != 0) {
// -1 means one dead character
ret = abs(ret);
wbuf[ret] = 0x0000;
if (fl_utf8fromwc(buf, sizeof(buf), wbuf, ret) >= sizeof(buf)) {
vlog.error(_("Out of buffer space whilst converting key event"));
return XK_VoidSymbol;
}
keyText = buf;
}
break;
case WM_CHAR:
case WM_SYSCHAR:
// Windows doesn't seem to have any sanity when it comes to control
// characters. We assume that Ctrl-A through Ctrl-Z range maps to
// the VK_A through VK_Z keys, and just let the rest fall through.
if ((fl_msg.wParam < 0x01) || (fl_msg.wParam > 0x1a))
break;
// Pretend that Ctrl isn't pressed, and do a character lookup.
GetKeyboardState(keystate);
keystate[VK_CONTROL] = keystate[VK_LCONTROL] = keystate[VK_RCONTROL] = 0;
// Ctrl-A is 0x01 and VK_A is 0x41, so add 0x40 for the conversion
ret = ToUnicode(fl_msg.wParam + 0x40, 0, keystate, wbuf, sizeof(wbuf)/sizeof(wbuf[0]), 0);
if (ret != 0) {
// -1 means one dead character
ret = abs(ret);
wbuf[ret] = 0x0000;
if (fl_utf8fromwc(buf, sizeof(buf), wbuf, ret) >= sizeof(buf)) {
vlog.error(_("Out of buffer space whilst converting key event"));
return XK_VoidSymbol;
}
keyText = buf;
}
break;
default:
// Not sure how we ended up here. Do nothing...
break;
}
#elif defined(__APPLE__)
keyText = osx_event_string();
#else
char buf[16];
KeySym sym;
XLookupString((XKeyEvent*)fl_xevent, buf, sizeof(buf), &sym, NULL);
return sym;
#endif
}
// Unknown special key?
if (keyText[0] == '\0') {
vlog.error(_("Unknown FLTK key code %d (0x%04x)"), keyCode, keyCode);
return XK_VoidSymbol;
}
// Look up the symbol the key produces and translate that from Unicode
// to a X11 keysym.
if (fl_utf_nb_char((const unsigned char*)keyText, strlen(keyText)) != 1) {
vlog.error(_("Multiple characters given for key code %d (0x%04x): '%s'"),
keyCode, keyCode, keyText);
return XK_VoidSymbol;
}
ucs = fl_utf8decode(keyText, NULL, NULL);
return ucs2keysym(ucs);
}
void Viewport::handleKeyEvent(int keyCode, const char *keyText, bool down)
{
rdr::U32 keySym;
if (viewOnly)
return;
// Because of the way keyboards work, we cannot expect to have the same
// symbol on release as when pressed. This breaks the VNC protocol however,
// so we need to keep track of what keysym a key _code_ generated on press
// and send the same on release.
if (!down) {
DownMap::iterator iter;
iter = downKeySym.find(keyCode);
if (iter == downKeySym.end()) {
vlog.error(_("Unexpected release of FLTK key code %d (0x%04x)"), keyCode, keyCode);
return;
}
cc->writer()->keyEvent(iter->second, false);
downKeySym.erase(iter);
return;
}
keySym = translateKeyEvent(keyCode, keyText);
if (keySym == XK_VoidSymbol)
return;
downKeySym[keyCode] = keySym;
cc->writer()->keyEvent(keySym, down);
}
|