blob: a621e405fb53dfda71cda8314d4cf1899641a94f [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
41using namespace rfb;
42
43extern void exit_vncviewer();
44
45static rfb::LogWriter vlog("DesktopWindow");
46
47DesktopWindow::DesktopWindow(int w, int h, const char *name,
48 const rfb::PixelFormat& serverPF,
49 CConn* cc_)
Pierre Ossmanc266e5a2011-03-09 10:24:12 +000050 : Fl_Window(w, h), cc(cc_), frameBuffer(NULL), pixelTrans(NULL),
51 lastPointerPos(0, 0), lastButtonMask(0)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000052{
53 callback(handleClose, this);
54
55 setName(name);
56
57 frameBuffer = new ManagedPixelBuffer(getPreferredPF(), w, h);
58 assert(frameBuffer);
59
60 setServerPF(serverPF);
61
62 show();
63}
64
65
66DesktopWindow::~DesktopWindow()
67{
Pierre Ossman3d5d8a02011-03-09 11:53:08 +000068 // Unregister all timeouts in case they get a change tro trigger
69 // again later when this object is already gone.
70 Fl::remove_timeout(handleUpdateTimeout, this);
71 Fl::remove_timeout(handleColourMap, this);
72 Fl::remove_timeout(handlePointerTimeout, this);
73
Pierre Ossman5156d5e2011-03-09 09:42:34 +000074 delete frameBuffer;
75
76 if (pixelTrans)
77 delete pixelTrans;
78}
79
80
81void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
82{
83 if (pixelTrans)
84 delete pixelTrans;
85 pixelTrans = NULL;
86
87 if (pf.equal(getPreferredPF()))
88 return;
89
90 pixelTrans = new PixelTransformer();
91 pixelTrans->init(pf, &colourMap, getPreferredPF());
92}
93
94
95const rfb::PixelFormat &DesktopWindow::getPreferredPF()
96{
97 static PixelFormat prefPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
98
99 return prefPF;
100}
101
102
103// Cursor stuff
104
105void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
106 void* data, void* mask)
107{
108}
109
110
111void DesktopWindow::setName(const char *name)
112{
113 CharArray windowNameStr;
114 windowNameStr.replaceBuf(new char[256]);
115
116 snprintf(windowNameStr.buf, 256, _("TigerVNC: %.240s"), name);
117
118 copy_label(windowNameStr.buf);
119}
120
121// setColourMapEntries() changes some of the entries in the colourmap.
122// Unfortunately these messages are often sent one at a time, so we delay the
123// settings taking effect by 100ms. This is because recalculating the internal
124// translation table can be expensive.
125void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
126 rdr::U16* rgbs)
127{
128 for (int i = 0; i < nColours; i++)
129 colourMap.set(firstColour+i, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
130
131 if (!Fl::has_timeout(handleColourMap, this))
132 Fl::add_timeout(0.100, handleColourMap, this);
133}
134
135
136// Copy the areas of the framebuffer that have been changed (damaged)
137// to the displayed window.
138
139void DesktopWindow::updateWindow()
140{
141 Rect r;
142
143 Fl::remove_timeout(handleUpdateTimeout, this);
144
145 r = damage.get_bounding_rect();
146 Fl_Window::damage(FL_DAMAGE_USER1, r.tl.x, r.tl.y, r.width(), r.height());
147
148 damage.clear();
149}
150
151
152void DesktopWindow::draw()
153{
154 int X, Y, W, H;
155
156 int pixel_bytes, stride_bytes;
157 const uchar *buf_start;
158
159 // Check what actually needs updating
160 fl_clip_box(0, 0, w(), h(), X, Y, W, H);
161 if ((W == 0) || (H == 0))
162 return;
163
164 pixel_bytes = frameBuffer->getPF().bpp/8;
165 stride_bytes = pixel_bytes * frameBuffer->getStride();
166 buf_start = frameBuffer->data +
167 pixel_bytes * X +
168 stride_bytes * Y;
169
170 // FIXME: Check how efficient this thing really is
171 fl_draw_image(buf_start, X, Y, W, H, pixel_bytes, stride_bytes);
172}
173
174
Pierre Ossmanc266e5a2011-03-09 10:24:12 +0000175int DesktopWindow::handle(int event)
176{
177 int buttonMask, wheelMask;
178
179 switch (event) {
180 case FL_PUSH:
181 case FL_RELEASE:
182 case FL_DRAG:
183 case FL_MOVE:
184 case FL_MOUSEWHEEL:
185 buttonMask = 0;
186 if (Fl::event_button1())
187 buttonMask |= 1;
188 if (Fl::event_button2())
189 buttonMask |= 2;
190 if (Fl::event_button3())
191 buttonMask |= 4;
192
193 if (event == FL_MOUSEWHEEL) {
194 if (Fl::event_dy() < 0)
195 wheelMask = 8;
196 else
197 wheelMask = 16;
198
199 // A quick press of the wheel "button", followed by a immediate
200 // release below
201 handlePointerEvent(Point(Fl::event_x(), Fl::event_y()),
202 buttonMask | wheelMask);
203 }
204
205 handlePointerEvent(Point(Fl::event_x(), Fl::event_y()), buttonMask);
206 return 1;
Pierre Ossmand014d052011-03-09 13:28:12 +0000207
208 case FL_FOCUS:
209 // Yes, we would like some focus please!
210 return 1;
211
212 case FL_KEYDOWN:
213 handleKeyEvent(Fl::event_key(), Fl::event_text(), true);
214 return 1;
215
216 case FL_KEYUP:
217 handleKeyEvent(Fl::event_key(), Fl::event_text(), false);
218 return 1;
Pierre Ossmanc266e5a2011-03-09 10:24:12 +0000219 }
220
221 return Fl_Window::handle(event);
222}
223
224
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000225void DesktopWindow::handleUpdateTimeout(void *data)
226{
227 DesktopWindow *self = (DesktopWindow *)data;
228
229 assert(self);
230
231 self->updateWindow();
232}
233
234
235void DesktopWindow::handleColourMap(void *data)
236{
237 DesktopWindow *self = (DesktopWindow *)data;
238
239 assert(self);
240
241 if (self->pixelTrans != NULL)
242 self->pixelTrans->setColourMapEntries(0, 0);
243
244 self->Fl_Window::damage(FL_DAMAGE_ALL);
245}
246
247void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
248{
249 exit_vncviewer();
250}
Pierre Ossmanc266e5a2011-03-09 10:24:12 +0000251
252
253void DesktopWindow::handlePointerEvent(const rfb::Point& pos, int buttonMask)
254{
255 if (!viewOnly) {
256 if (pointerEventInterval == 0 || buttonMask != lastButtonMask) {
257 cc->writer()->pointerEvent(pos, buttonMask);
258 } else {
259 if (!Fl::has_timeout(handlePointerTimeout, this))
260 Fl::add_timeout((double)pointerEventInterval/1000.0,
261 handlePointerTimeout, this);
262 }
263 lastPointerPos = pos;
264 lastButtonMask = buttonMask;
265 }
266}
267
268
269void DesktopWindow::handlePointerTimeout(void *data)
270{
271 DesktopWindow *self = (DesktopWindow *)data;
272
273 assert(self);
274
Pierre Ossmane21ae3d2011-03-09 11:44:24 +0000275 self->cc->writer()->pointerEvent(self->lastPointerPos, self->lastButtonMask);
Pierre Ossmanc266e5a2011-03-09 10:24:12 +0000276}
Pierre Ossmand014d052011-03-09 13:28:12 +0000277
Pierre Ossman98486c12011-03-10 11:39:42 +0000278
279rdr::U32 DesktopWindow::translateKeyEvent(int keyCode, const char *keyText)
280{
281 unsigned ucs;
282
283 // First check for function keys
Pierre Ossman70f32462011-03-10 11:57:03 +0000284 if ((keyCode > FL_F) && (keyCode <= FL_F_Last))
285 return XK_F1 + (keyCode - FL_F - 1);
Pierre Ossman98486c12011-03-10 11:39:42 +0000286
Pierre Ossman381e5462011-03-10 11:56:17 +0000287 // Numpad numbers
288 if ((keyCode >= (FL_KP + '0')) && (keyCode <= (FL_KP + '9')))
289 return XK_KP_0 + (keyCode - (FL_KP + '0'));
290
Pierre Ossman98486c12011-03-10 11:39:42 +0000291 // Then other special keys
292 switch (keyCode) {
293 case FL_BackSpace:
294 return XK_BackSpace;
295 case FL_Tab:
296 return XK_Tab;
297 case FL_Enter:
298 return XK_Return;
299 case FL_Pause:
300 return XK_Pause;
301 case FL_Scroll_Lock:
302 return XK_Scroll_Lock;
303 case FL_Escape:
304 return XK_Escape;
305 case FL_Home:
306 return XK_Home;
307 case FL_Left:
308 return XK_Left;
309 case FL_Up:
310 return XK_Up;
311 case FL_Right:
312 return XK_Right;
313 case FL_Down:
314 return XK_Down;
315 case FL_Page_Up:
316 return XK_Page_Up;
317 case FL_Page_Down:
318 return XK_Page_Down;
319 case FL_End:
320 return XK_End;
321 case FL_Print:
322 return XK_Print;
323 case FL_Insert:
324 return XK_Insert;
325 case FL_Menu:
326 return XK_Menu;
327 case FL_Help:
328 return XK_Help;
329 case FL_Num_Lock:
330 return XK_Num_Lock;
331 case FL_Shift_L:
332 return XK_Shift_L;
333 case FL_Shift_R:
334 return XK_Shift_R;
335 case FL_Control_L:
336 return XK_Control_L;
337 case FL_Control_R:
338 return XK_Control_R;
339 case FL_Caps_Lock:
340 return XK_Caps_Lock;
341 case FL_Meta_L:
342 return XK_Super_L;
343 case FL_Meta_R:
344 return XK_Super_R;
345 case FL_Alt_L:
346 return XK_Alt_L;
347 case FL_Alt_R:
348 return XK_Alt_R;
349 case FL_Delete:
350 return XK_Delete;
Pierre Ossman381e5462011-03-10 11:56:17 +0000351 case FL_KP_Enter:
352 return XK_KP_Enter;
353 case FL_KP + '=':
354 return XK_KP_Equal;
355 case FL_KP + '*':
356 return XK_KP_Multiply;
357 case FL_KP + '+':
358 return XK_KP_Add;
359 case FL_KP + ',':
360 return XK_KP_Separator;
361 case FL_KP + '-':
362 return XK_KP_Subtract;
363 case FL_KP + '.':
364 return XK_KP_Decimal;
365 case FL_KP + '/':
366 return XK_KP_Divide;
Pierre Ossman98486c12011-03-10 11:39:42 +0000367 }
368
369 // Unknown special key?
370 if (keyText[0] == '\0') {
371 vlog.error(_("Unknown FLTK key code %d (0x%04x)"), keyCode, keyCode);
372 return XK_VoidSymbol;
373 }
374
375 // Look up the symbol the key produces and translate that from Unicode
376 // to a X11 keysym.
377 if (fl_utf_nb_char((const unsigned char*)keyText, strlen(keyText)) != 1) {
378 vlog.error(_("Multiple characters given for key code %d (0x%04x): '%s'"),
379 keyCode, keyCode, keyText);
380 return XK_VoidSymbol;
381 }
382
383 ucs = fl_utf8decode(keyText, NULL, NULL);
384 return ucs2keysym(ucs);
385}
386
387
Pierre Ossmand014d052011-03-09 13:28:12 +0000388void DesktopWindow::handleKeyEvent(int keyCode, const char *keyText, bool down)
389{
390 rdr::U32 keySym;
391
392 if (viewOnly)
393 return;
394
395 if (keyCode > 0xFFFF) {
396 vlog.error(_("Too large FLTK key code %d (0x%08x)"), keyCode, keyCode);
397 return;
398 }
399
400 // Because of the way keyboards work, we cannot expect to have the same
401 // symbol on release as when pressed. This breaks the VNC protocol however,
402 // so we need to keep track of what keysym a key _code_ generated on press
403 // and send the same on release.
404 if (!down) {
405 cc->writer()->keyEvent(downKeySym[keyCode], false);
406 return;
407 }
408
Pierre Ossman98486c12011-03-10 11:39:42 +0000409 keySym = translateKeyEvent(keyCode, keyText);
410 if (keySym == XK_VoidSymbol)
411 return;
Pierre Ossmand014d052011-03-09 13:28:12 +0000412
413 downKeySym[keyCode] = keySym;
414 cc->writer()->keyEvent(keySym, down);
415}