blob: b5c516fa30a86388bce31c8eafc8fc861ef9177c [file] [log] [blame]
Pierre Ossmand50b3d12011-04-15 07:46:56 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +01002 * Copyright 2011-2014 Pierre Ossman for Cendio AB
Pierre Ossmand50b3d12011-04-15 07:46:56 +00003 *
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
Peter Åstrandc359f362011-08-23 12:04:46 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Pierre Ossmand50b3d12011-04-15 07:46:56 +000024#include <assert.h>
25#include <stdio.h>
26#include <string.h>
27
Pierre Ossmand50b3d12011-04-15 07:46:56 +000028#include <rfb/CMsgWriter.h>
29#include <rfb/LogWriter.h>
Pierre Ossmand1a853b2014-10-10 13:37:35 +020030#include <rfb/Exception.h>
Pierre Ossmand50b3d12011-04-15 07:46:56 +000031
32// FLTK can pull in the X11 headers on some systems
33#ifndef XK_VoidSymbol
Pierre Ossman13a809a2014-08-22 15:09:54 +020034#define XK_LATIN1
Pierre Ossmand50b3d12011-04-15 07:46:56 +000035#define XK_MISCELLANY
36#define XK_XKB_KEYS
37#include <rfb/keysymdef.h>
38#endif
39
Pierre Ossmancb0cffe2011-05-20 14:55:10 +000040#ifndef XF86XK_ModeLock
41#include <rfb/XF86keysym.h>
42#endif
43
Pierre Ossman7b8bff62014-07-21 16:17:23 +020044#ifndef NoSymbol
45#define NoSymbol 0
46#endif
47
Pierre Ossman2e9684f2014-07-21 16:46:22 +020048// Missing in at least some versions of MinGW
49#ifndef MAPVK_VK_TO_VSC
50#define MAPVK_VK_TO_VSC 0
51#endif
52
Pierre Ossmand50b3d12011-04-15 07:46:56 +000053#include "Viewport.h"
54#include "CConn.h"
Pierre Ossmand463b572011-05-16 12:04:43 +000055#include "OptionsDialog.h"
Pierre Ossman947b48d2014-01-27 16:52:35 +010056#include "DesktopWindow.h"
Pierre Ossmand50b3d12011-04-15 07:46:56 +000057#include "i18n.h"
Pierre Ossmanc628ba42011-05-23 12:21:21 +000058#include "fltk_layout.h"
Pierre Ossmand50b3d12011-04-15 07:46:56 +000059#include "parameters.h"
60#include "keysym2ucs.h"
Martin Koegler498ef462011-09-04 07:04:43 +000061#include "menukey.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000062#include "vncviewer.h"
Pierre Ossmand50b3d12011-04-15 07:46:56 +000063
Pierre Ossmanac13abe2014-02-07 14:46:26 +010064#include "PlatformPixelBuffer.h"
Pierre Ossman947b48d2014-01-27 16:52:35 +010065
DRCb65bb932011-06-24 03:17:00 +000066#include <FL/fl_draw.H>
67#include <FL/fl_ask.H>
68
Pierre Ossman769963f2014-01-20 14:43:52 +010069#include <FL/Fl_Menu.H>
70#include <FL/Fl_Menu_Button.H>
71
Pierre Ossman6b743d02014-07-21 16:48:43 +020072#ifdef __APPLE__
73#include "cocoa.h"
74#endif
75
DRCb65bb932011-06-24 03:17:00 +000076#ifdef WIN32
77#include "win32.h"
78#endif
79
Pierre Ossman6b743d02014-07-21 16:48:43 +020080
Pierre Ossmand50b3d12011-04-15 07:46:56 +000081using namespace rfb;
Pierre Ossman835b4ef2011-06-08 17:02:36 +000082using namespace rdr;
Pierre Ossmand50b3d12011-04-15 07:46:56 +000083
Pierre Ossmand50b3d12011-04-15 07:46:56 +000084static rfb::LogWriter vlog("Viewport");
85
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +000086// Menu constants
87
Joel Teichroeba7494ac2015-07-13 14:46:22 -070088enum { ID_EXIT, ID_FULLSCREEN, ID_MINIMIZE, ID_RESIZE,
Pierre Ossman8dc8bca2012-07-04 11:37:10 +000089 ID_CTRL, ID_ALT, ID_MENUKEY, ID_CTRLALTDEL,
Pierre Ossman2eb1d112011-05-16 12:18:08 +000090 ID_REFRESH, ID_OPTIONS, ID_INFO, ID_ABOUT, ID_DISMISS };
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +000091
Pierre Ossman25188c42014-07-21 16:30:08 +020092// Fake key presses use this value and above
93static const int fakeKeyBase = 0x200;
94
Pierre Ossmand50b3d12011-04-15 07:46:56 +000095Viewport::Viewport(int w, int h, const rfb::PixelFormat& serverPF, CConn* cc_)
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020096 : Fl_Widget(0, 0, w, h), cc(cc_), frameBuffer(NULL),
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010097 lastPointerPos(0, 0), lastButtonMask(0),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010098 menuCtrlKey(false), menuAltKey(false), cursor(NULL)
Pierre Ossmand50b3d12011-04-15 07:46:56 +000099{
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000100 Fl::add_clipboard_notify(handleClipboardChange, this);
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000101
Pierre Ossman4f3ac692014-08-22 15:10:22 +0200102 // We need to intercept keyboard events early
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200103 Fl::add_system_handler(handleSystemEvent, this);
Pierre Ossman4f3ac692014-08-22 15:10:22 +0200104
Pierre Ossman403ac272017-01-02 17:00:41 +0100105 frameBuffer = new PlatformPixelBuffer(w, h);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000106 assert(frameBuffer);
Pierre Ossman9f273e92015-11-09 16:34:54 +0100107 cc->setFramebuffer(frameBuffer);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000108
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000109 contextMenu = new Fl_Menu_Button(0, 0, 0, 0);
Pierre Ossmanad9d1ae2011-05-26 14:16:02 +0000110 // Setting box type to FL_NO_BOX prevents it from trying to draw the
111 // button component (which we don't want)
112 contextMenu->box(FL_NO_BOX);
113
Pierre Ossmanb043ad12011-06-01 09:26:57 +0000114 // The (invisible) button associated with this widget can mess with
115 // things like Fl_Scroll so we need to get rid of any parents.
116 // Unfortunately that's not possible because of STR #2654, but
117 // reparenting to the current window works for most cases.
118 window()->add(contextMenu);
119
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000120 setMenuKey();
121
122 OptionsDialog::addCallback(handleOptions, this);
Pierre Ossmande673132011-11-10 14:59:24 +0000123
124 // Send a fake pointer event so that the server will stop rendering
125 // a server-side cursor. Ideally we'd like to send the actual pointer
126 // position, but we can't really tell when the window manager is done
127 // placing us so we don't have a good time for that.
128 handlePointerEvent(Point(w/2, h/2), 0);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000129}
130
131
132Viewport::~Viewport()
133{
134 // Unregister all timeouts in case they get a change tro trigger
135 // again later when this object is already gone.
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000136 Fl::remove_timeout(handlePointerTimeout, this);
137
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200138 Fl::remove_system_handler(handleSystemEvent);
Pierre Ossman4f3ac692014-08-22 15:10:22 +0200139
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000140 Fl::remove_clipboard_notify(handleClipboardChange);
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000141
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000142 OptionsDialog::removeCallback(handleOptions);
143
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000144 if (cursor) {
Pierre Ossmanf741d342011-06-09 08:32:49 +0000145 if (!cursor->alloc_array)
146 delete [] cursor->array;
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000147 delete cursor;
148 }
149
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000150 // FLTK automatically deletes all child widgets, so we shouldn't touch
151 // them ourselves here
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000152}
153
154
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000155const rfb::PixelFormat &Viewport::getPreferredPF()
156{
Pierre Ossman132b3d02011-06-13 11:19:32 +0000157 return frameBuffer->getPF();
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000158}
159
160
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000161// Copy the areas of the framebuffer that have been changed (damaged)
162// to the displayed window.
163
164void Viewport::updateWindow()
165{
166 Rect r;
167
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200168 r = frameBuffer->getDamage();
169 damage(FL_DAMAGE_USER1, r.tl.x + x(), r.tl.y + y(), r.width(), r.height());
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000170}
171
Pierre Ossmanf741d342011-06-09 08:32:49 +0000172static const char * dotcursor_xpm[] = {
173 "5 5 2 1",
174 ". c #000000",
175 " c #FFFFFF",
176 " ",
177 " ... ",
178 " ... ",
179 " ... ",
180 " "};
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000181
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000182void Viewport::setCursor(int width, int height, const Point& hotspot,
183 void* data, void* mask)
184{
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000185 if (cursor) {
Pierre Ossmanf741d342011-06-09 08:32:49 +0000186 if (!cursor->alloc_array)
187 delete [] cursor->array;
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000188 delete cursor;
189 }
190
Pierre Ossmanf741d342011-06-09 08:32:49 +0000191 int mask_len = ((width+7)/8) * height;
192 int i;
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000193
Pierre Ossmanf741d342011-06-09 08:32:49 +0000194 for (i = 0; i < mask_len; i++)
195 if (((rdr::U8*)mask)[i]) break;
196
Pierre Ossmana51574a2011-06-09 08:39:35 +0000197 if ((i == mask_len) && dotWhenNoCursor) {
198 vlog.debug("cursor is empty - using dot");
Pierre Ossmanf741d342011-06-09 08:32:49 +0000199
200 Fl_Pixmap pxm(dotcursor_xpm);
201 cursor = new Fl_RGB_Image(&pxm);
202 cursorHotspot.x = cursorHotspot.y = 2;
203 } else {
Pierre Ossman17a48f02011-06-09 08:42:04 +0000204 if ((width == 0) || (height == 0)) {
205 U8 *buffer = new U8[4];
206 memset(buffer, 0, 4);
207 cursor = new Fl_RGB_Image(buffer, 1, 1, 4);
208 cursorHotspot.x = cursorHotspot.y = 0;
209 } else {
210 U8 *buffer = new U8[width*height*4];
211 U8 *i, *o, *m;
212 int m_width;
Pierre Ossmanf741d342011-06-09 08:32:49 +0000213
Pierre Ossmand8f84872011-07-15 08:26:51 +0000214 const PixelFormat *pf;
215
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200216 pf = &cc->cp.pf();
Pierre Ossmanf741d342011-06-09 08:32:49 +0000217
Pierre Ossman17a48f02011-06-09 08:42:04 +0000218 i = (U8*)data;
219 o = buffer;
220 m = (U8*)mask;
221 m_width = (width+7)/8;
222 for (int y = 0;y < height;y++) {
223 for (int x = 0;x < width;x++) {
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100224 pf->rgbFromBuffer(o, i, 1);
Pierre Ossmanf741d342011-06-09 08:32:49 +0000225
Pierre Ossman17a48f02011-06-09 08:42:04 +0000226 if (m[(m_width*y)+(x/8)] & 0x80>>(x%8))
227 o[3] = 255;
228 else
229 o[3] = 0;
Pierre Ossmanf741d342011-06-09 08:32:49 +0000230
Pierre Ossman17a48f02011-06-09 08:42:04 +0000231 o += 4;
Pierre Ossmand8f84872011-07-15 08:26:51 +0000232 i += pf->bpp/8;
Pierre Ossman17a48f02011-06-09 08:42:04 +0000233 }
Pierre Ossmanf741d342011-06-09 08:32:49 +0000234 }
Pierre Ossman17a48f02011-06-09 08:42:04 +0000235
236 cursor = new Fl_RGB_Image(buffer, width, height, 4);
237
238 cursorHotspot = hotspot;
Pierre Ossmanf741d342011-06-09 08:32:49 +0000239 }
Pierre Ossmanf741d342011-06-09 08:32:49 +0000240 }
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000241
242 if (Fl::belowmouse() == this)
243 window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000244}
245
246
Pierre Ossman3d74d882017-01-02 19:49:52 +0100247void Viewport::draw(Surface* dst)
248{
249 int X, Y, W, H;
250
251 // Check what actually needs updating
252 fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
253 if ((W == 0) || (H == 0))
254 return;
255
256 frameBuffer->draw(dst, X - x(), Y - y(), X, Y, W, H);
257}
258
259
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000260void Viewport::draw()
261{
262 int X, Y, W, H;
263
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000264 // Check what actually needs updating
Pierre Ossmana6e20772011-04-15 11:10:52 +0000265 fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000266 if ((W == 0) || (H == 0))
267 return;
268
Pierre Ossman132b3d02011-06-13 11:19:32 +0000269 frameBuffer->draw(X - x(), Y - y(), X, Y, W, H);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000270}
271
272
Pierre Ossmane00f0aa2011-06-01 09:31:53 +0000273void Viewport::resize(int x, int y, int w, int h)
274{
Pierre Ossman9f273e92015-11-09 16:34:54 +0100275 if ((w != frameBuffer->width()) || (h != frameBuffer->height())) {
276 vlog.debug("Resizing framebuffer from %dx%d to %dx%d",
277 frameBuffer->width(), frameBuffer->height(), w, h);
Pierre Ossmane00f0aa2011-06-01 09:31:53 +0000278
Pierre Ossman403ac272017-01-02 17:00:41 +0100279 frameBuffer = new PlatformPixelBuffer(w, h);
Pierre Ossman9f273e92015-11-09 16:34:54 +0100280 assert(frameBuffer);
281 cc->setFramebuffer(frameBuffer);
Pierre Ossmane00f0aa2011-06-01 09:31:53 +0000282 }
283
Pierre Ossmane00f0aa2011-06-01 09:31:53 +0000284 Fl_Widget::resize(x, y, w, h);
285}
286
287
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000288int Viewport::handle(int event)
289{
Pierre Ossman689c4582011-05-26 15:39:41 +0000290 char *buffer;
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000291 int ret;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000292 int buttonMask, wheelMask;
293 DownMap::const_iterator iter;
294
295 switch (event) {
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000296 case FL_PASTE:
Pierre Ossman689c4582011-05-26 15:39:41 +0000297 buffer = new char[Fl::event_length() + 1];
298
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000299 // This is documented as to ASCII, but actually does to 8859-1
Pierre Ossman689c4582011-05-26 15:39:41 +0000300 ret = fl_utf8toa(Fl::event_text(), Fl::event_length(), buffer,
301 Fl::event_length() + 1);
302 assert(ret < (Fl::event_length() + 1));
303
Pierre Ossmanfb450fb2015-03-03 16:34:56 +0100304 vlog.debug("Sending clipboard data (%d bytes)", (int)strlen(buffer));
Pierre Ossman829cdeb2011-07-12 16:58:12 +0000305
306 try {
307 cc->writer()->clientCutText(buffer, ret);
308 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000309 vlog.error("%s", e.str());
Pierre Ossman829cdeb2011-07-12 16:58:12 +0000310 exit_vncviewer(e.str());
311 }
Pierre Ossman689c4582011-05-26 15:39:41 +0000312
313 delete [] buffer;
314
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000315 return 1;
Pierre Ossman689c4582011-05-26 15:39:41 +0000316
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000317 case FL_ENTER:
Pierre Ossman1f1f6fd2011-06-09 08:33:29 +0000318 if (cursor)
319 window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
Pierre Ossman56610fb2015-01-27 16:28:15 +0100320 // Yes, we would like some pointer events please!
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000321 return 1;
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000322
323 case FL_LEAVE:
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000324 window()->cursor(FL_CURSOR_DEFAULT);
Pierre Ossman164865a2011-11-10 13:30:39 +0000325 // Fall through as we want a last move event to help trigger edge stuff
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000326 case FL_PUSH:
327 case FL_RELEASE:
328 case FL_DRAG:
329 case FL_MOVE:
330 case FL_MOUSEWHEEL:
331 buttonMask = 0;
332 if (Fl::event_button1())
333 buttonMask |= 1;
334 if (Fl::event_button2())
335 buttonMask |= 2;
336 if (Fl::event_button3())
337 buttonMask |= 4;
338
339 if (event == FL_MOUSEWHEEL) {
Pierre Ossmandf0ed9f2011-05-24 11:33:43 +0000340 wheelMask = 0;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000341 if (Fl::event_dy() < 0)
Pierre Ossmandf0ed9f2011-05-24 11:33:43 +0000342 wheelMask |= 8;
343 if (Fl::event_dy() > 0)
344 wheelMask |= 16;
345 if (Fl::event_dx() < 0)
346 wheelMask |= 32;
347 if (Fl::event_dx() > 0)
348 wheelMask |= 64;
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000349
350 // A quick press of the wheel "button", followed by a immediate
351 // release below
Pierre Ossman6a464be2011-04-15 12:57:31 +0000352 handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()),
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000353 buttonMask | wheelMask);
354 }
355
Pierre Ossman6a464be2011-04-15 12:57:31 +0000356 handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), buttonMask);
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000357 return 1;
358
359 case FL_FOCUS:
Pierre Ossman48ef54d2014-08-19 14:08:04 +0200360 Fl::disable_im();
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000361 // Yes, we would like some focus please!
362 return 1;
363
364 case FL_UNFOCUS:
365 // Release all keys that were pressed as that generally makes most
366 // sense (e.g. Alt+Tab where we only see the Alt press)
Pierre Ossmanf8450ca2011-07-12 16:10:16 +0000367 while (!downKeySym.empty())
Pierre Ossman25188c42014-07-21 16:30:08 +0200368 handleKeyRelease(downKeySym.begin()->first);
Pierre Ossman48ef54d2014-08-19 14:08:04 +0200369 Fl::enable_im();
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000370 return 1;
371
372 case FL_KEYDOWN:
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000373 case FL_KEYUP:
Pierre Ossman56610fb2015-01-27 16:28:15 +0100374 // Just ignore these as keys were handled in the event handler
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000375 return 1;
376 }
377
378 return Fl_Widget::handle(event);
379}
380
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000381void Viewport::handleClipboardChange(int source, void *data)
382{
383 Viewport *self = (Viewport *)data;
384
385 assert(self);
386
Pierre Ossmana432b582017-01-02 15:16:58 +0100387 if (!sendClipboard)
388 return;
389
Pierre Ossmanf7fef922015-12-10 21:24:08 +0100390#if !defined(WIN32) && !defined(__APPLE__)
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000391 if (!sendPrimary && (source == 0))
392 return;
Pierre Ossmanf7fef922015-12-10 21:24:08 +0100393#endif
Pierre Ossman4a6be4a2011-05-19 14:49:18 +0000394
395 Fl::paste(*self, source);
396}
397
398
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000399void Viewport::handlePointerEvent(const rfb::Point& pos, int buttonMask)
400{
401 if (!viewOnly) {
402 if (pointerEventInterval == 0 || buttonMask != lastButtonMask) {
Pierre Ossman829cdeb2011-07-12 16:58:12 +0000403 try {
404 cc->writer()->pointerEvent(pos, buttonMask);
405 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000406 vlog.error("%s", e.str());
Pierre Ossman829cdeb2011-07-12 16:58:12 +0000407 exit_vncviewer(e.str());
408 }
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000409 } else {
410 if (!Fl::has_timeout(handlePointerTimeout, this))
411 Fl::add_timeout((double)pointerEventInterval/1000.0,
412 handlePointerTimeout, this);
413 }
414 lastPointerPos = pos;
415 lastButtonMask = buttonMask;
416 }
417}
418
419
420void Viewport::handlePointerTimeout(void *data)
421{
422 Viewport *self = (Viewport *)data;
423
424 assert(self);
425
Pierre Ossman829cdeb2011-07-12 16:58:12 +0000426 try {
427 self->cc->writer()->pointerEvent(self->lastPointerPos, self->lastButtonMask);
428 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000429 vlog.error("%s", e.str());
Pierre Ossman829cdeb2011-07-12 16:58:12 +0000430 exit_vncviewer(e.str());
431 }
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000432}
433
434
Pierre Ossman25188c42014-07-21 16:30:08 +0200435void Viewport::handleKeyPress(int keyCode, rdr::U32 keySym)
436{
437 static bool menuRecursion = false;
438
439 // Prevent recursion if the menu wants to send its own
440 // activation key.
441 if (menuKeySym && (keySym == menuKeySym) && !menuRecursion) {
442 menuRecursion = true;
443 popupContextMenu();
444 menuRecursion = false;
445 return;
446 }
447
448 if (viewOnly)
449 return;
450
451#ifdef __APPLE__
452 // Alt on OS X behaves more like AltGr on other systems, and to get
453 // sane behaviour we should translate things in that manner for the
454 // remote VNC server. However that means we lose the ability to use
455 // Alt as a shortcut modifier. Do what RealVNC does and hijack the
456 // left command key as an Alt replacement.
457 switch (keySym) {
458 case XK_Super_L:
459 keySym = XK_Alt_L;
460 break;
461 case XK_Super_R:
462 keySym = XK_Super_L;
463 break;
464 case XK_Alt_L:
Pierre Ossman38fcebb2014-08-21 13:44:28 +0200465 keySym = XK_Mode_switch;
466 break;
Pierre Ossman25188c42014-07-21 16:30:08 +0200467 case XK_Alt_R:
468 keySym = XK_ISO_Level3_Shift;
469 break;
470 }
471#endif
472
473#ifdef WIN32
474 // Ugly hack alert!
475 //
476 // Windows doesn't have a proper AltGr, but handles it using fake
477 // Ctrl+Alt. Unfortunately X11 doesn't generally like the combination
478 // Ctrl+Alt+AltGr, which we usually end up with when Xvnc tries to
479 // get everything in the correct state. Cheat and temporarily release
480 // Ctrl and Alt when we send some other symbol.
481 bool ctrlPressed, altPressed;
482 DownMap::iterator iter;
483
484 ctrlPressed = false;
485 altPressed = false;
486 for (iter = downKeySym.begin();iter != downKeySym.end();++iter) {
487 if (iter->second == XK_Control_L)
488 ctrlPressed = true;
489 else if (iter->second == XK_Alt_R)
490 altPressed = true;
491 }
492
493 if (ctrlPressed && altPressed) {
494 vlog.debug("Faking release of AltGr (Ctrl_L+Alt_R)");
495 try {
496 cc->writer()->keyEvent(XK_Control_L, false);
497 cc->writer()->keyEvent(XK_Alt_R, false);
498 } catch (rdr::Exception& e) {
499 vlog.error("%s", e.str());
500 exit_vncviewer(e.str());
501 }
502 }
503#endif
504
505 // Because of the way keyboards work, we cannot expect to have the same
506 // symbol on release as when pressed. This breaks the VNC protocol however,
507 // so we need to keep track of what keysym a key _code_ generated on press
508 // and send the same on release.
509 downKeySym[keyCode] = keySym;
510
511#if defined(WIN32) || defined(__APPLE__)
512 vlog.debug("Key pressed: 0x%04x => 0x%04x", keyCode, keySym);
513#else
514 vlog.debug("Key pressed: 0x%04x => XK_%s (0x%04x)",
515 keyCode, XKeysymToString(keySym), keySym);
516#endif
517
518 try {
519 cc->writer()->keyEvent(keySym, true);
520 } catch (rdr::Exception& e) {
521 vlog.error("%s", e.str());
522 exit_vncviewer(e.str());
523 }
524
525#ifdef WIN32
526 // Ugly hack continued...
527 if (ctrlPressed && altPressed) {
528 vlog.debug("Restoring AltGr state");
529 try {
530 cc->writer()->keyEvent(XK_Control_L, true);
531 cc->writer()->keyEvent(XK_Alt_R, true);
532 } catch (rdr::Exception& e) {
533 vlog.error("%s", e.str());
534 exit_vncviewer(e.str());
535 }
536 }
537#endif
538}
539
540
541void Viewport::handleKeyRelease(int keyCode)
542{
543 DownMap::iterator iter;
544
545 if (viewOnly)
546 return;
547
548 iter = downKeySym.find(keyCode);
549 if (iter == downKeySym.end()) {
550 // These occur somewhat frequently so let's not spam them unless
551 // logging is turned up.
552 vlog.debug("Unexpected release of key code %d", keyCode);
553 return;
554 }
555
556#if defined(WIN32) || defined(__APPLE__)
557 vlog.debug("Key released: 0x%04x => 0x%04x", keyCode, iter->second);
558#else
559 vlog.debug("Key released: 0x%04x => XK_%s (0x%04x)",
560 keyCode, XKeysymToString(iter->second), iter->second);
561#endif
562
563 try {
564 cc->writer()->keyEvent(iter->second, false);
565 } catch (rdr::Exception& e) {
566 vlog.error("%s", e.str());
567 exit_vncviewer(e.str());
568 }
569
570 downKeySym.erase(iter);
571}
572
573
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200574int Viewport::handleSystemEvent(void *event, void *data)
Pierre Ossman4f3ac692014-08-22 15:10:22 +0200575{
576 Viewport *self = (Viewport *)data;
577 Fl_Widget *focus;
578
579 assert(self);
580
581 focus = Fl::grab();
582 if (!focus)
583 focus = Fl::focus();
584 if (!focus)
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200585 return 0;
Pierre Ossman4f3ac692014-08-22 15:10:22 +0200586
587 if (focus != self)
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200588 return 0;
Pierre Ossman4f3ac692014-08-22 15:10:22 +0200589
590 assert(event);
591
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200592#if defined(WIN32)
593 MSG *msg = (MSG*)event;
594
595 if ((msg->message == WM_KEYDOWN) || (msg->message == WM_SYSKEYDOWN)) {
596 UINT vKey;
597 bool isExtended;
598 int keyCode;
599 rdr::U32 keySym;
600
601 vKey = msg->wParam;
602 isExtended = (msg->lParam & (1 << 24)) != 0;
603
604 keyCode = ((msg->lParam >> 16) & 0xff);
605
606 // Windows sets the scan code to 0x00 for multimedia keys, so we
607 // have to do a reverse lookup based on the vKey.
608 if (keyCode == 0x00) {
609 keyCode = MapVirtualKey(vKey, MAPVK_VK_TO_VSC);
610 if (keyCode == 0x00) {
Pierre Ossmanf05f3552014-09-22 12:19:52 +0200611 if (isExtended)
612 vlog.error(_("No scan code for extended virtual key 0x%02x"), (int)vKey);
613 else
614 vlog.error(_("No scan code for virtual key 0x%02x"), (int)vKey);
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200615 return 1;
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200616 }
617 }
618
619 if (isExtended)
620 keyCode |= 0x100;
621
Pierre Ossmana7d3dc72014-09-30 17:03:28 +0200622 // VK_SNAPSHOT sends different scan codes depending on the state of
623 // Alt. This means that we can get different scan codes on press and
624 // release. Force it to be something standard.
625 if (vKey == VK_SNAPSHOT)
626 keyCode = 0x137;
627
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200628 keySym = win32_vkey_to_keysym(vKey, isExtended);
629 if (keySym == NoSymbol) {
Pierre Ossmanf05f3552014-09-22 12:19:52 +0200630 if (isExtended)
631 vlog.error(_("No symbol for extended virtual key 0x%02x"), (int)vKey);
632 else
633 vlog.error(_("No symbol for virtual key 0x%02x"), (int)vKey);
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200634 return 1;
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200635 }
636
637 self->handleKeyPress(keyCode, keySym);
638
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200639 return 1;
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200640 } else if ((msg->message == WM_KEYUP) || (msg->message == WM_SYSKEYUP)) {
641 UINT vKey;
642 bool isExtended;
643 int keyCode;
644
645 vKey = msg->wParam;
646 isExtended = (msg->lParam & (1 << 24)) != 0;
647
648 keyCode = ((msg->lParam >> 16) & 0xff);
649 if (keyCode == 0x00)
650 keyCode = MapVirtualKey(vKey, MAPVK_VK_TO_VSC);
651 if (isExtended)
652 keyCode |= 0x100;
Pierre Ossmana7d3dc72014-09-30 17:03:28 +0200653 if (vKey == VK_SNAPSHOT)
654 keyCode = 0x137;
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200655
656 self->handleKeyRelease(keyCode);
657
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200658 return 1;
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200659 }
Pierre Ossman6b743d02014-07-21 16:48:43 +0200660#elif defined(__APPLE__)
661 if (cocoa_is_keyboard_event(event)) {
662 int keyCode;
663
664 keyCode = cocoa_event_keycode(event);
665
666 if (cocoa_is_key_press(event)) {
667 rdr::U32 keySym;
668
669 keySym = cocoa_event_keysym(event);
670 if (keySym == NoSymbol) {
671 vlog.error(_("No symbol for key code 0x%02x (in the current state)"),
672 (int)keyCode);
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200673 return 1;
Pierre Ossman6b743d02014-07-21 16:48:43 +0200674 }
675
676 self->handleKeyPress(keyCode, keySym);
677
678 // We don't get any release events for CapsLock, so we have to
679 // send the release right away.
680 if (keySym == XK_Caps_Lock)
681 self->handleKeyRelease(keyCode);
682 } else {
683 self->handleKeyRelease(keyCode);
684 }
685
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200686 return 1;
Pierre Ossman6b743d02014-07-21 16:48:43 +0200687 }
688#else
Pierre Ossman6b9622d2014-07-21 16:42:12 +0200689 XEvent *xevent = (XEvent*)event;
690
691 if (xevent->type == KeyPress) {
692 char str;
693 KeySym keysym;
694
695 XLookupString(&xevent->xkey, &str, 1, &keysym, NULL);
696 if (keysym == NoSymbol) {
697 vlog.error(_("No symbol for key code %d (in the current state)"),
698 (int)xevent->xkey.keycode);
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200699 return 1;
Pierre Ossman6b9622d2014-07-21 16:42:12 +0200700 }
701
702 switch (keysym) {
703 // For the first few years, there wasn't a good consensus on what the
704 // Windows keys should be mapped to for X11. So we need to help out a
705 // bit and map all variants to the same key...
Pierre Ossman6b9622d2014-07-21 16:42:12 +0200706 case XK_Hyper_L:
707 keysym = XK_Super_L;
708 break;
Pierre Ossman6b9622d2014-07-21 16:42:12 +0200709 case XK_Hyper_R:
710 keysym = XK_Super_R;
711 break;
712 // There has been several variants for Shift-Tab over the years.
713 // RFB states that we should always send a normal tab.
714 case XK_ISO_Left_Tab:
715 keysym = XK_Tab;
716 break;
717 }
718
719 self->handleKeyPress(xevent->xkey.keycode, keysym);
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200720 return 1;
Pierre Ossman6b9622d2014-07-21 16:42:12 +0200721 } else if (xevent->type == KeyRelease) {
722 self->handleKeyRelease(xevent->xkey.keycode);
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200723 return 1;
Pierre Ossman6b9622d2014-07-21 16:42:12 +0200724 }
725#endif
726
Pierre Ossman64ff1ca2014-09-11 10:48:29 +0200727 return 0;
Pierre Ossman4f3ac692014-08-22 15:10:22 +0200728}
729
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000730void Viewport::initContextMenu()
731{
Pierre Ossmanb5b0ea52015-02-25 10:35:03 +0100732 contextMenu->clear();
733
Pierre Ossmanff626f82015-09-23 16:39:54 +0200734 fltk_menu_add(contextMenu, p_("ContextMenu|", "E&xit viewer"),
735 0, NULL, (void*)ID_EXIT, FL_MENU_DIVIDER);
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000736
Pierre Ossmanff626f82015-09-23 16:39:54 +0200737 fltk_menu_add(contextMenu, p_("ContextMenu|", "&Full screen"),
738 0, NULL, (void*)ID_FULLSCREEN,
Pierre Ossman245c8022015-02-25 11:27:49 +0100739 FL_MENU_TOGGLE | (window()->fullscreen_active()?FL_MENU_VALUE:0));
Pierre Ossmanff626f82015-09-23 16:39:54 +0200740 fltk_menu_add(contextMenu, p_("ContextMenu|", "Minimi&ze"),
741 0, NULL, (void*)ID_MINIMIZE, 0);
742 fltk_menu_add(contextMenu, p_("ContextMenu|", "Resize &window to session"),
743 0, NULL, (void*)ID_RESIZE,
Pierre Ossman245c8022015-02-25 11:27:49 +0100744 (window()->fullscreen_active()?FL_MENU_INACTIVE:0) |
Pierre Ossman245c8022015-02-25 11:27:49 +0100745 FL_MENU_DIVIDER);
Pierre Ossmanb5b0ea52015-02-25 10:35:03 +0100746
Pierre Ossmanff626f82015-09-23 16:39:54 +0200747 fltk_menu_add(contextMenu, p_("ContextMenu|", "&Ctrl"),
748 0, NULL, (void*)ID_CTRL,
Pierre Ossman245c8022015-02-25 11:27:49 +0100749 FL_MENU_TOGGLE | (menuCtrlKey?FL_MENU_VALUE:0));
Pierre Ossmanff626f82015-09-23 16:39:54 +0200750 fltk_menu_add(contextMenu, p_("ContextMenu|", "&Alt"),
751 0, NULL, (void*)ID_ALT,
Pierre Ossman245c8022015-02-25 11:27:49 +0100752 FL_MENU_TOGGLE | (menuAltKey?FL_MENU_VALUE:0));
Pierre Ossmanb5b0ea52015-02-25 10:35:03 +0100753
754 if (menuKeySym) {
755 char sendMenuKey[64];
Pierre Ossmanff626f82015-09-23 16:39:54 +0200756 snprintf(sendMenuKey, 64, p_("ContextMenu|", "Send %s"), (const char *)menuKey);
Pierre Ossman245c8022015-02-25 11:27:49 +0100757 fltk_menu_add(contextMenu, sendMenuKey, 0, NULL, (void*)ID_MENUKEY, 0);
758 fltk_menu_add(contextMenu, "Secret shortcut menu key", menuKeyCode, NULL,
759 (void*)ID_MENUKEY, FL_MENU_INVISIBLE);
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000760 }
761
Pierre Ossmanff626f82015-09-23 16:39:54 +0200762 fltk_menu_add(contextMenu, p_("ContextMenu|", "Send Ctrl-Alt-&Del"),
763 0, NULL, (void*)ID_CTRLALTDEL, FL_MENU_DIVIDER);
Pierre Ossmanb5b0ea52015-02-25 10:35:03 +0100764
Pierre Ossmanff626f82015-09-23 16:39:54 +0200765 fltk_menu_add(contextMenu, p_("ContextMenu|", "&Refresh screen"),
766 0, NULL, (void*)ID_REFRESH, FL_MENU_DIVIDER);
Pierre Ossmanb5b0ea52015-02-25 10:35:03 +0100767
Pierre Ossmanff626f82015-09-23 16:39:54 +0200768 fltk_menu_add(contextMenu, p_("ContextMenu|", "&Options..."),
769 0, NULL, (void*)ID_OPTIONS, 0);
770 fltk_menu_add(contextMenu, p_("ContextMenu|", "Connection &info..."),
771 0, NULL, (void*)ID_INFO, 0);
772 fltk_menu_add(contextMenu, p_("ContextMenu|", "About &TigerVNC viewer..."),
773 0, NULL, (void*)ID_ABOUT, FL_MENU_DIVIDER);
Pierre Ossmanb5b0ea52015-02-25 10:35:03 +0100774
Pierre Ossmanff626f82015-09-23 16:39:54 +0200775 fltk_menu_add(contextMenu, p_("ContextMenu|", "Dismiss &menu"),
776 0, NULL, (void*)ID_DISMISS, 0);
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000777}
778
779
780void Viewport::popupContextMenu()
781{
782 const Fl_Menu_Item *m;
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000783 char buffer[1024];
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000784
Pierre Ossmanb3ff4372011-06-03 11:45:15 +0000785 // Make sure the menu is reset to its initial state between goes or
786 // it will start up highlighting the previously selected entry.
787 contextMenu->value(-1);
788
Henrik Anderssonabd70ce2011-09-14 06:31:06 +0000789 // initialize context menu before display
790 initContextMenu();
791
Pierre Ossmanfd177f32012-01-05 12:37:04 +0000792 // Unfortunately FLTK doesn't reliably restore the mouse pointer for
793 // menus, so we have to help it out.
Pierre Ossmanbfbdb102012-01-05 12:32:03 +0000794 if (Fl::belowmouse() == this)
795 window()->cursor(FL_CURSOR_DEFAULT);
Pierre Ossmanbfbdb102012-01-05 12:32:03 +0000796
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000797 m = contextMenu->popup();
Pierre Ossmanbfbdb102012-01-05 12:32:03 +0000798
Pierre Ossmanfd177f32012-01-05 12:37:04 +0000799 // Back to our proper mouse pointer.
Pierre Ossmande1a3b92014-03-17 14:29:49 +0100800 if ((Fl::belowmouse() == this) && cursor)
Pierre Ossmanbfbdb102012-01-05 12:32:03 +0000801 window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
Pierre Ossmanbfbdb102012-01-05 12:32:03 +0000802
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000803 if (m == NULL)
804 return;
805
806 switch (m->argument()) {
807 case ID_EXIT:
808 exit_vncviewer();
809 break;
Pierre Ossman4c613d32011-05-26 14:14:06 +0000810 case ID_FULLSCREEN:
811 if (window()->fullscreen_active())
812 window()->fullscreen_off();
Pierre Ossman275284f2012-07-04 11:37:48 +0000813 else
Pierre Ossmanaae38912012-07-13 11:22:55 +0000814 ((DesktopWindow*)window())->fullscreen_on();
Pierre Ossman4c613d32011-05-26 14:14:06 +0000815 break;
Joel Teichroeba7494ac2015-07-13 14:46:22 -0700816 case ID_MINIMIZE:
817 window()->iconize();
Joel Teichroeb1f9e45a2015-07-18 07:09:24 -0700818 break;
Pierre Ossman8dc8bca2012-07-04 11:37:10 +0000819 case ID_RESIZE:
Pierre Ossman8dc8bca2012-07-04 11:37:10 +0000820 if (window()->fullscreen_active())
821 break;
Pierre Ossman8dc8bca2012-07-04 11:37:10 +0000822 window()->size(w(), h());
823 break;
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000824 case ID_CTRL:
Pierre Ossman25188c42014-07-21 16:30:08 +0200825 if (m->value())
826 handleKeyPress(fakeKeyBase + 0, XK_Control_L);
827 else
828 handleKeyRelease(fakeKeyBase + 0);
Henrik Anderssonabd70ce2011-09-14 06:31:06 +0000829 menuCtrlKey = !menuCtrlKey;
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000830 break;
831 case ID_ALT:
Pierre Ossman25188c42014-07-21 16:30:08 +0200832 if (m->value())
833 handleKeyPress(fakeKeyBase + 1, XK_Alt_L);
834 else
835 handleKeyRelease(fakeKeyBase + 1);
Henrik Anderssonabd70ce2011-09-14 06:31:06 +0000836 menuAltKey = !menuAltKey;
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000837 break;
838 case ID_MENUKEY:
Pierre Ossman25188c42014-07-21 16:30:08 +0200839 handleKeyPress(fakeKeyBase + 2, menuKeySym);
840 handleKeyRelease(fakeKeyBase + 2);
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000841 break;
842 case ID_CTRLALTDEL:
Pierre Ossman25188c42014-07-21 16:30:08 +0200843 handleKeyPress(fakeKeyBase + 3, XK_Control_L);
844 handleKeyPress(fakeKeyBase + 4, XK_Alt_L);
845 handleKeyPress(fakeKeyBase + 5, XK_Delete);
Pierre Ossman991b4fe2011-07-12 16:02:30 +0000846
Pierre Ossman25188c42014-07-21 16:30:08 +0200847 handleKeyRelease(fakeKeyBase + 5);
848 handleKeyRelease(fakeKeyBase + 4);
849 handleKeyRelease(fakeKeyBase + 3);
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000850 break;
Pierre Ossmand4c61ce2011-04-29 11:18:12 +0000851 case ID_REFRESH:
852 cc->refreshFramebuffer();
853 break;
Pierre Ossmand463b572011-05-16 12:04:43 +0000854 case ID_OPTIONS:
855 OptionsDialog::showDialog();
856 break;
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000857 case ID_INFO:
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000858 if (fltk_escape(cc->connectionInfo(), buffer, sizeof(buffer)) < sizeof(buffer)) {
859 fl_message_title(_("VNC connection info"));
Pierre Ossmanf52740e2012-04-25 15:43:56 +0000860 fl_message("%s", buffer);
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000861 }
Pierre Ossman2eb1d112011-05-16 12:18:08 +0000862 break;
Pierre Ossmanb8858222011-04-29 11:51:38 +0000863 case ID_ABOUT:
864 about_vncviewer();
865 break;
Pierre Ossmanc7bfaac2011-04-29 11:08:11 +0000866 case ID_DISMISS:
867 // Don't need to do anything
868 break;
869 }
870}
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000871
872
873void Viewport::setMenuKey()
874{
Pierre Ossman25188c42014-07-21 16:30:08 +0200875 getMenuKey(&menuKeyCode, &menuKeySym);
Pierre Ossman4e7271e2011-05-24 12:47:12 +0000876}
877
878
879void Viewport::handleOptions(void *data)
880{
881 Viewport *self = (Viewport*)data;
882
883 self->setMenuKey();
884}