blob: 7da432029d83af089817a16e949292a524ca3ca2 [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20#include <assert.h>
21#include <stdio.h>
22#include <string.h>
23
24#include <FL/fl_draw.H>
25
26#include <rfb/CMsgWriter.h>
27#include <rfb/LogWriter.h>
28
Pierre Ossmand014d052011-03-09 13:28:12 +000029// FLTK can pull in the X11 headers on some systems
30#ifndef XK_VoidSymbol
31#define XK_MISCELLANY
32#include <rfb/keysymdef.h>
33#endif
34
Pierre Ossman5156d5e2011-03-09 09:42:34 +000035#include "DesktopWindow.h"
36#include "CConn.h"
37#include "i18n.h"
38#include "parameters.h"
Pierre Ossmand014d052011-03-09 13:28:12 +000039#include "keysym2ucs.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000040
Pierre Ossman89f868a2011-04-11 11:59:31 +000041// FLTK STR #2599 must be fixed for proper dead keys support
42#ifndef HAVE_FLTK_DEAD_KEYS
43#define event_compose_symbol event_text
44#endif
45
Pierre Ossman5156d5e2011-03-09 09:42:34 +000046using namespace rfb;
47
Pierre Ossmanf14bf332011-04-14 12:49:03 +000048#ifdef __APPLE__
49extern "C" const char *osx_event_string(void);
50#endif
51
Pierre Ossman5156d5e2011-03-09 09:42:34 +000052extern void exit_vncviewer();
53
54static rfb::LogWriter vlog("DesktopWindow");
55
56DesktopWindow::DesktopWindow(int w, int h, const char *name,
57 const rfb::PixelFormat& serverPF,
58 CConn* cc_)
Pierre Ossmanc266e5a2011-03-09 10:24:12 +000059 : Fl_Window(w, h), cc(cc_), frameBuffer(NULL), pixelTrans(NULL),
60 lastPointerPos(0, 0), lastButtonMask(0)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000061{
62 callback(handleClose, this);
63
64 setName(name);
65
66 frameBuffer = new ManagedPixelBuffer(getPreferredPF(), w, h);
67 assert(frameBuffer);
68
69 setServerPF(serverPF);
70
71 show();
72}
73
74
75DesktopWindow::~DesktopWindow()
76{
Pierre Ossman3d5d8a02011-03-09 11:53:08 +000077 // Unregister all timeouts in case they get a change tro trigger
78 // again later when this object is already gone.
79 Fl::remove_timeout(handleUpdateTimeout, this);
80 Fl::remove_timeout(handleColourMap, this);
81 Fl::remove_timeout(handlePointerTimeout, this);
82
Pierre Ossman5156d5e2011-03-09 09:42:34 +000083 delete frameBuffer;
84
85 if (pixelTrans)
86 delete pixelTrans;
87}
88
89
90void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
91{
92 if (pixelTrans)
93 delete pixelTrans;
94 pixelTrans = NULL;
95
96 if (pf.equal(getPreferredPF()))
97 return;
98
99 pixelTrans = new PixelTransformer();
100 pixelTrans->init(pf, &colourMap, getPreferredPF());
101}
102
103
104const rfb::PixelFormat &DesktopWindow::getPreferredPF()
105{
106 static PixelFormat prefPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
107
108 return prefPF;
109}
110
111
112// Cursor stuff
113
114void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
115 void* data, void* mask)
116{
117}
118
119
120void DesktopWindow::setName(const char *name)
121{
122 CharArray windowNameStr;
123 windowNameStr.replaceBuf(new char[256]);
124
125 snprintf(windowNameStr.buf, 256, _("TigerVNC: %.240s"), name);
126
127 copy_label(windowNameStr.buf);
128}
129
130// setColourMapEntries() changes some of the entries in the colourmap.
131// Unfortunately these messages are often sent one at a time, so we delay the
132// settings taking effect by 100ms. This is because recalculating the internal
133// translation table can be expensive.
134void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
135 rdr::U16* rgbs)
136{
137 for (int i = 0; i < nColours; i++)
138 colourMap.set(firstColour+i, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
139
140 if (!Fl::has_timeout(handleColourMap, this))
141 Fl::add_timeout(0.100, handleColourMap, this);
142}
143
144
145// Copy the areas of the framebuffer that have been changed (damaged)
146// to the displayed window.
147
148void DesktopWindow::updateWindow()
149{
150 Rect r;
151
152 Fl::remove_timeout(handleUpdateTimeout, this);
153
154 r = damage.get_bounding_rect();
155 Fl_Window::damage(FL_DAMAGE_USER1, r.tl.x, r.tl.y, r.width(), r.height());
156
157 damage.clear();
158}
159
160
161void DesktopWindow::draw()
162{
163 int X, Y, W, H;
164
165 int pixel_bytes, stride_bytes;
166 const uchar *buf_start;
167
168 // Check what actually needs updating
169 fl_clip_box(0, 0, w(), h(), X, Y, W, H);
170 if ((W == 0) || (H == 0))
171 return;
172
173 pixel_bytes = frameBuffer->getPF().bpp/8;
174 stride_bytes = pixel_bytes * frameBuffer->getStride();
175 buf_start = frameBuffer->data +
176 pixel_bytes * X +
177 stride_bytes * Y;
178
179 // FIXME: Check how efficient this thing really is
180 fl_draw_image(buf_start, X, Y, W, H, pixel_bytes, stride_bytes);
181}
182
183
Pierre Ossmanc266e5a2011-03-09 10:24:12 +0000184int DesktopWindow::handle(int event)
185{
186 int buttonMask, wheelMask;
187
188 switch (event) {
189 case FL_PUSH:
190 case FL_RELEASE:
191 case FL_DRAG:
192 case FL_MOVE:
193 case FL_MOUSEWHEEL:
194 buttonMask = 0;
195 if (Fl::event_button1())
196 buttonMask |= 1;
197 if (Fl::event_button2())
198 buttonMask |= 2;
199 if (Fl::event_button3())
200 buttonMask |= 4;
201
202 if (event == FL_MOUSEWHEEL) {
203 if (Fl::event_dy() < 0)
204 wheelMask = 8;
205 else
206 wheelMask = 16;
207
208 // A quick press of the wheel "button", followed by a immediate
209 // release below
210 handlePointerEvent(Point(Fl::event_x(), Fl::event_y()),
211 buttonMask | wheelMask);
212 }
213
214 handlePointerEvent(Point(Fl::event_x(), Fl::event_y()), buttonMask);
215 return 1;
Pierre Ossmand014d052011-03-09 13:28:12 +0000216
217 case FL_FOCUS:
218 // Yes, we would like some focus please!
219 return 1;
220
221 case FL_KEYDOWN:
Pierre Ossman89f868a2011-04-11 11:59:31 +0000222 handleKeyEvent(Fl::event_key(), Fl::event_compose_symbol(), true);
Pierre Ossmand014d052011-03-09 13:28:12 +0000223 return 1;
224
225 case FL_KEYUP:
Pierre Ossman89f868a2011-04-11 11:59:31 +0000226 handleKeyEvent(Fl::event_key(), Fl::event_compose_symbol(), false);
Pierre Ossmand014d052011-03-09 13:28:12 +0000227 return 1;
Pierre Ossmanc266e5a2011-03-09 10:24:12 +0000228 }
229
230 return Fl_Window::handle(event);
231}
232
233
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000234void DesktopWindow::handleUpdateTimeout(void *data)
235{
236 DesktopWindow *self = (DesktopWindow *)data;
237
238 assert(self);
239
240 self->updateWindow();
241}
242
243
244void DesktopWindow::handleColourMap(void *data)
245{
246 DesktopWindow *self = (DesktopWindow *)data;
247
248 assert(self);
249
250 if (self->pixelTrans != NULL)
251 self->pixelTrans->setColourMapEntries(0, 0);
252
253 self->Fl_Window::damage(FL_DAMAGE_ALL);
254}
255
256void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
257{
258 exit_vncviewer();
259}
Pierre Ossmanc266e5a2011-03-09 10:24:12 +0000260
261
262void DesktopWindow::handlePointerEvent(const rfb::Point& pos, int buttonMask)
263{
264 if (!viewOnly) {
265 if (pointerEventInterval == 0 || buttonMask != lastButtonMask) {
266 cc->writer()->pointerEvent(pos, buttonMask);
267 } else {
268 if (!Fl::has_timeout(handlePointerTimeout, this))
269 Fl::add_timeout((double)pointerEventInterval/1000.0,
270 handlePointerTimeout, this);
271 }
272 lastPointerPos = pos;
273 lastButtonMask = buttonMask;
274 }
275}
276
277
278void DesktopWindow::handlePointerTimeout(void *data)
279{
280 DesktopWindow *self = (DesktopWindow *)data;
281
282 assert(self);
283
Pierre Ossmane21ae3d2011-03-09 11:44:24 +0000284 self->cc->writer()->pointerEvent(self->lastPointerPos, self->lastButtonMask);
Pierre Ossmanc266e5a2011-03-09 10:24:12 +0000285}
Pierre Ossmand014d052011-03-09 13:28:12 +0000286
Pierre Ossman98486c12011-03-10 11:39:42 +0000287
288rdr::U32 DesktopWindow::translateKeyEvent(int keyCode, const char *keyText)
289{
290 unsigned ucs;
291
292 // First check for function keys
Pierre Ossman70f32462011-03-10 11:57:03 +0000293 if ((keyCode > FL_F) && (keyCode <= FL_F_Last))
294 return XK_F1 + (keyCode - FL_F - 1);
Pierre Ossman98486c12011-03-10 11:39:42 +0000295
Pierre Ossman381e5462011-03-10 11:56:17 +0000296 // Numpad numbers
297 if ((keyCode >= (FL_KP + '0')) && (keyCode <= (FL_KP + '9')))
298 return XK_KP_0 + (keyCode - (FL_KP + '0'));
299
Pierre Ossman98486c12011-03-10 11:39:42 +0000300 // Then other special keys
301 switch (keyCode) {
302 case FL_BackSpace:
303 return XK_BackSpace;
304 case FL_Tab:
305 return XK_Tab;
306 case FL_Enter:
307 return XK_Return;
308 case FL_Pause:
309 return XK_Pause;
310 case FL_Scroll_Lock:
311 return XK_Scroll_Lock;
312 case FL_Escape:
313 return XK_Escape;
314 case FL_Home:
315 return XK_Home;
316 case FL_Left:
317 return XK_Left;
318 case FL_Up:
319 return XK_Up;
320 case FL_Right:
321 return XK_Right;
322 case FL_Down:
323 return XK_Down;
324 case FL_Page_Up:
325 return XK_Page_Up;
326 case FL_Page_Down:
327 return XK_Page_Down;
328 case FL_End:
329 return XK_End;
330 case FL_Print:
331 return XK_Print;
332 case FL_Insert:
333 return XK_Insert;
334 case FL_Menu:
335 return XK_Menu;
336 case FL_Help:
337 return XK_Help;
338 case FL_Num_Lock:
339 return XK_Num_Lock;
340 case FL_Shift_L:
341 return XK_Shift_L;
342 case FL_Shift_R:
343 return XK_Shift_R;
344 case FL_Control_L:
345 return XK_Control_L;
346 case FL_Control_R:
347 return XK_Control_R;
348 case FL_Caps_Lock:
349 return XK_Caps_Lock;
350 case FL_Meta_L:
351 return XK_Super_L;
352 case FL_Meta_R:
353 return XK_Super_R;
354 case FL_Alt_L:
355 return XK_Alt_L;
356 case FL_Alt_R:
357 return XK_Alt_R;
358 case FL_Delete:
359 return XK_Delete;
Pierre Ossman381e5462011-03-10 11:56:17 +0000360 case FL_KP_Enter:
361 return XK_KP_Enter;
362 case FL_KP + '=':
363 return XK_KP_Equal;
364 case FL_KP + '*':
365 return XK_KP_Multiply;
366 case FL_KP + '+':
367 return XK_KP_Add;
368 case FL_KP + ',':
369 return XK_KP_Separator;
370 case FL_KP + '-':
371 return XK_KP_Subtract;
372 case FL_KP + '.':
373 return XK_KP_Decimal;
374 case FL_KP + '/':
375 return XK_KP_Divide;
Pierre Ossman98486c12011-03-10 11:39:42 +0000376 }
377
Pierre Ossmanf14bf332011-04-14 12:49:03 +0000378 // Ctrl and Cmd tend to fudge input handling, so we need to cheat here
379 if (Fl::event_state() & (FL_COMMAND | FL_CTRL)) {
380#ifdef WIN32
381 BYTE keystate[256];
382 WCHAR wbuf[8];
383 int ret;
384
385 static char buf[32];
386
387 switch (fl_msg.message) {
388 case WM_KEYDOWN:
389 case WM_SYSKEYDOWN:
390 // Most buttons end up here when Ctrl is pressed. Here we can pretend
391 // that Ctrl isn't pressed, and do a character lookup.
392 GetKeyboardState(keystate);
393 keystate[VK_CONTROL] = keystate[VK_LCONTROL] = keystate[VK_RCONTROL] = 0;
394
395 ret = ToUnicode(fl_msg.wParam, 0, keystate, wbuf, sizeof(wbuf)/sizeof(wbuf[0]), 0);
396 if (ret != 0) {
397 // -1 means one dead character
398 ret = abs(ret);
399 wbuf[ret] = 0x0000;
400
401 if (fl_utf8fromwc(buf, sizeof(buf), wbuf, ret) >= sizeof(buf)) {
402 vlog.error(_("Out of buffer space whilst converting key event"));
403 return XK_VoidSymbol;
404 }
405
406 keyText = buf;
407 }
408 break;
409 case WM_CHAR:
410 case WM_SYSCHAR:
411 // Windows doesn't seem to have any sanity when it comes to control
412 // characters. We assume that Ctrl-A through Ctrl-Z range maps to
413 // the VK_A through VK_Z keys, and just let the rest fall through.
414 if ((fl_msg.wParam < 0x01) || (fl_msg.wParam > 0x1a))
415 break;
416
417 // Pretend that Ctrl isn't pressed, and do a character lookup.
418 GetKeyboardState(keystate);
419 keystate[VK_CONTROL] = keystate[VK_LCONTROL] = keystate[VK_RCONTROL] = 0;
420
421 // Ctrl-A is 0x01 and VK_A is 0x41, so add 0x40 for the conversion
422 ret = ToUnicode(fl_msg.wParam + 0x40, 0, keystate, wbuf, sizeof(wbuf)/sizeof(wbuf[0]), 0);
423 if (ret != 0) {
424 // -1 means one dead character
425 ret = abs(ret);
426 wbuf[ret] = 0x0000;
427
428 if (fl_utf8fromwc(buf, sizeof(buf), wbuf, ret) >= sizeof(buf)) {
429 vlog.error(_("Out of buffer space whilst converting key event"));
430 return XK_VoidSymbol;
431 }
432
433 keyText = buf;
434 }
435 break;
436 default:
437 // Not sure how we ended up here. Do nothing...
438 break;
439 }
440#elif defined(__APPLE__)
441 keyText = osx_event_string();
442#else
443 char buf[16];
444 KeySym sym;
445
446 XLookupString((XKeyEvent*)fl_xevent, buf, sizeof(buf), &sym, NULL);
447
448 return sym;
449#endif
450 }
451
Pierre Ossman98486c12011-03-10 11:39:42 +0000452 // Unknown special key?
453 if (keyText[0] == '\0') {
454 vlog.error(_("Unknown FLTK key code %d (0x%04x)"), keyCode, keyCode);
455 return XK_VoidSymbol;
456 }
457
458 // Look up the symbol the key produces and translate that from Unicode
459 // to a X11 keysym.
460 if (fl_utf_nb_char((const unsigned char*)keyText, strlen(keyText)) != 1) {
461 vlog.error(_("Multiple characters given for key code %d (0x%04x): '%s'"),
462 keyCode, keyCode, keyText);
463 return XK_VoidSymbol;
464 }
465
466 ucs = fl_utf8decode(keyText, NULL, NULL);
467 return ucs2keysym(ucs);
468}
469
470
Pierre Ossmand014d052011-03-09 13:28:12 +0000471void DesktopWindow::handleKeyEvent(int keyCode, const char *keyText, bool down)
472{
473 rdr::U32 keySym;
474
475 if (viewOnly)
476 return;
477
478 if (keyCode > 0xFFFF) {
479 vlog.error(_("Too large FLTK key code %d (0x%08x)"), keyCode, keyCode);
480 return;
481 }
482
483 // Because of the way keyboards work, we cannot expect to have the same
484 // symbol on release as when pressed. This breaks the VNC protocol however,
485 // so we need to keep track of what keysym a key _code_ generated on press
486 // and send the same on release.
487 if (!down) {
488 cc->writer()->keyEvent(downKeySym[keyCode], false);
489 return;
490 }
491
Pierre Ossman98486c12011-03-10 11:39:42 +0000492 keySym = translateKeyEvent(keyCode, keyText);
493 if (keySym == XK_VoidSymbol)
494 return;
Pierre Ossmand014d052011-03-09 13:28:12 +0000495
496 downKeySym[keyCode] = keySym;
497 cc->writer()->keyEvent(keySym, down);
498}