You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Viewport.cxx 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2019 Pierre Ossman 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. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <rfb/CMsgWriter.h>
  26. #include <rfb/LogWriter.h>
  27. #include <rfb/Exception.h>
  28. #include <rfb/ledStates.h>
  29. // FLTK can pull in the X11 headers on some systems
  30. #ifndef XK_VoidSymbol
  31. #define XK_LATIN1
  32. #define XK_MISCELLANY
  33. #define XK_XKB_KEYS
  34. #include <rfb/keysymdef.h>
  35. #endif
  36. #ifndef XF86XK_ModeLock
  37. #include <rfb/XF86keysym.h>
  38. #endif
  39. #if ! (defined(WIN32) || defined(__APPLE__))
  40. #include <X11/XKBlib.h>
  41. #endif
  42. #ifndef NoSymbol
  43. #define NoSymbol 0
  44. #endif
  45. // Missing in at least some versions of MinGW
  46. #ifndef MAPVK_VK_TO_VSC
  47. #define MAPVK_VK_TO_VSC 0
  48. #endif
  49. #include "fltk/layout.h"
  50. #include "fltk/util.h"
  51. #include "Viewport.h"
  52. #include "CConn.h"
  53. #include "OptionsDialog.h"
  54. #include "DesktopWindow.h"
  55. #include "i18n.h"
  56. #include "parameters.h"
  57. #include "keysym2ucs.h"
  58. #include "menukey.h"
  59. #include "vncviewer.h"
  60. #include "PlatformPixelBuffer.h"
  61. #include <FL/fl_draw.H>
  62. #include <FL/fl_ask.H>
  63. #include <FL/Fl_Menu.H>
  64. #include <FL/Fl_Menu_Button.H>
  65. #include <FL/x.H>
  66. #if !defined(WIN32) && !defined(__APPLE__)
  67. #include <X11/XKBlib.h>
  68. extern const struct _code_map_xkb_to_qnum {
  69. const char * from;
  70. const unsigned short to;
  71. } code_map_xkb_to_qnum[];
  72. extern const unsigned int code_map_xkb_to_qnum_len;
  73. static int code_map_keycode_to_qnum[256];
  74. #endif
  75. #ifdef __APPLE__
  76. #include "cocoa.h"
  77. extern const unsigned short code_map_osx_to_qnum[];
  78. extern const unsigned int code_map_osx_to_qnum_len;
  79. #endif
  80. #ifdef WIN32
  81. #include "win32.h"
  82. #endif
  83. using namespace rfb;
  84. using namespace rdr;
  85. static rfb::LogWriter vlog("Viewport");
  86. // Menu constants
  87. enum { ID_DISCONNECT, ID_FULLSCREEN, ID_MINIMIZE, ID_RESIZE,
  88. ID_CTRL, ID_ALT, ID_MENUKEY, ID_CTRLALTDEL,
  89. ID_REFRESH, ID_OPTIONS, ID_INFO, ID_ABOUT };
  90. // Used to detect fake input (0xaa is not a real key)
  91. #ifdef WIN32
  92. static const WORD SCAN_FAKE = 0xaa;
  93. #endif
  94. Viewport::Viewport(int w, int h, const rfb::PixelFormat& /*serverPF*/, CConn* cc_)
  95. : Fl_Widget(0, 0, w, h), cc(cc_), frameBuffer(NULL),
  96. lastPointerPos(0, 0), lastButtonMask(0),
  97. #ifdef WIN32
  98. altGrArmed(false),
  99. #endif
  100. firstLEDState(true),
  101. pendingServerClipboard(false), pendingClientClipboard(false),
  102. menuCtrlKey(false), menuAltKey(false), cursor(NULL)
  103. {
  104. #if !defined(WIN32) && !defined(__APPLE__)
  105. XkbDescPtr xkb;
  106. Status status;
  107. xkb = XkbGetMap(fl_display, 0, XkbUseCoreKbd);
  108. if (!xkb)
  109. throw rfb::Exception("XkbGetMap");
  110. status = XkbGetNames(fl_display, XkbKeyNamesMask, xkb);
  111. if (status != Success)
  112. throw rfb::Exception("XkbGetNames");
  113. memset(code_map_keycode_to_qnum, 0, sizeof(code_map_keycode_to_qnum));
  114. for (KeyCode keycode = xkb->min_key_code;
  115. keycode < xkb->max_key_code;
  116. keycode++) {
  117. const char *keyname = xkb->names->keys[keycode].name;
  118. unsigned short rfbcode;
  119. if (keyname[0] == '\0')
  120. continue;
  121. rfbcode = 0;
  122. for (unsigned i = 0;i < code_map_xkb_to_qnum_len;i++) {
  123. if (strncmp(code_map_xkb_to_qnum[i].from,
  124. keyname, XkbKeyNameLength) == 0) {
  125. rfbcode = code_map_xkb_to_qnum[i].to;
  126. break;
  127. }
  128. }
  129. if (rfbcode != 0)
  130. code_map_keycode_to_qnum[keycode] = rfbcode;
  131. else
  132. vlog.debug("No key mapping for key %.4s", keyname);
  133. }
  134. XkbFreeKeyboard(xkb, 0, True);
  135. #endif
  136. Fl::add_clipboard_notify(handleClipboardChange, this);
  137. // We need to intercept keyboard events early
  138. Fl::add_system_handler(handleSystemEvent, this);
  139. frameBuffer = new PlatformPixelBuffer(w, h);
  140. assert(frameBuffer);
  141. cc->setFramebuffer(frameBuffer);
  142. contextMenu = new Fl_Menu_Button(0, 0, 0, 0);
  143. // Setting box type to FL_NO_BOX prevents it from trying to draw the
  144. // button component (which we don't want)
  145. contextMenu->box(FL_NO_BOX);
  146. // The (invisible) button associated with this widget can mess with
  147. // things like Fl_Scroll so we need to get rid of any parents.
  148. // Unfortunately that's not possible because of STR #2654, but
  149. // reparenting to the current window works for most cases.
  150. window()->add(contextMenu);
  151. setMenuKey();
  152. OptionsDialog::addCallback(handleOptions, this);
  153. // Make sure we have an initial blank cursor set
  154. setCursor(0, 0, rfb::Point(0, 0), NULL);
  155. }
  156. Viewport::~Viewport()
  157. {
  158. // Unregister all timeouts in case they get a change tro trigger
  159. // again later when this object is already gone.
  160. Fl::remove_timeout(handlePointerTimeout, this);
  161. #ifdef WIN32
  162. Fl::remove_timeout(handleAltGrTimeout, this);
  163. #endif
  164. Fl::remove_system_handler(handleSystemEvent);
  165. Fl::remove_clipboard_notify(handleClipboardChange);
  166. OptionsDialog::removeCallback(handleOptions);
  167. if (cursor) {
  168. if (!cursor->alloc_array)
  169. delete [] cursor->array;
  170. delete cursor;
  171. }
  172. // FLTK automatically deletes all child widgets, so we shouldn't touch
  173. // them ourselves here
  174. }
  175. const rfb::PixelFormat &Viewport::getPreferredPF()
  176. {
  177. return frameBuffer->getPF();
  178. }
  179. // Copy the areas of the framebuffer that have been changed (damaged)
  180. // to the displayed window.
  181. void Viewport::updateWindow()
  182. {
  183. Rect r;
  184. r = frameBuffer->getDamage();
  185. damage(FL_DAMAGE_USER1, r.tl.x + x(), r.tl.y + y(), r.width(), r.height());
  186. }
  187. static const char * dotcursor_xpm[] = {
  188. "5 5 2 1",
  189. ". c #000000",
  190. " c #FFFFFF",
  191. " ",
  192. " ... ",
  193. " ... ",
  194. " ... ",
  195. " "};
  196. void Viewport::setCursor(int width, int height, const Point& hotspot,
  197. const uint8_t* data)
  198. {
  199. int i;
  200. if (cursor) {
  201. if (!cursor->alloc_array)
  202. delete [] cursor->array;
  203. delete cursor;
  204. }
  205. for (i = 0; i < width*height; i++)
  206. if (data[i*4 + 3] != 0) break;
  207. if ((i == width*height) && dotWhenNoCursor) {
  208. vlog.debug("cursor is empty - using dot");
  209. Fl_Pixmap pxm(dotcursor_xpm);
  210. cursor = new Fl_RGB_Image(&pxm);
  211. cursorHotspot.x = cursorHotspot.y = 2;
  212. } else {
  213. if ((width == 0) || (height == 0)) {
  214. uint8_t *buffer = new uint8_t[4];
  215. memset(buffer, 0, 4);
  216. cursor = new Fl_RGB_Image(buffer, 1, 1, 4);
  217. cursorHotspot.x = cursorHotspot.y = 0;
  218. } else {
  219. uint8_t *buffer = new uint8_t[width * height * 4];
  220. memcpy(buffer, data, width * height * 4);
  221. cursor = new Fl_RGB_Image(buffer, width, height, 4);
  222. cursorHotspot = hotspot;
  223. }
  224. }
  225. if (Fl::belowmouse() == this)
  226. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  227. }
  228. void Viewport::handleClipboardRequest()
  229. {
  230. Fl::paste(*this, clipboardSource);
  231. }
  232. void Viewport::handleClipboardAnnounce(bool available)
  233. {
  234. if (!acceptClipboard)
  235. return;
  236. if (!available) {
  237. vlog.debug("Clipboard is no longer available on server");
  238. pendingServerClipboard = false;
  239. return;
  240. }
  241. pendingClientClipboard = false;
  242. if (!hasFocus()) {
  243. vlog.debug("Got notification of new clipboard on server whilst not focused, will request data later");
  244. pendingServerClipboard = true;
  245. return;
  246. }
  247. vlog.debug("Got notification of new clipboard on server, requesting data");
  248. cc->requestClipboard();
  249. }
  250. void Viewport::handleClipboardData(const char* data)
  251. {
  252. size_t len;
  253. if (!hasFocus())
  254. return;
  255. len = strlen(data);
  256. vlog.debug("Got clipboard data (%d bytes)", (int)len);
  257. // RFB doesn't have separate selection and clipboard concepts, so we
  258. // dump the data into both variants.
  259. #if !defined(WIN32) && !defined(__APPLE__)
  260. if (setPrimary)
  261. Fl::copy(data, len, 0);
  262. #endif
  263. Fl::copy(data, len, 1);
  264. }
  265. void Viewport::setLEDState(unsigned int state)
  266. {
  267. vlog.debug("Got server LED state: 0x%08x", state);
  268. // The first message is just considered to be the server announcing
  269. // support for this extension. We will push our state to sync up the
  270. // server when we get focus. If we already have focus we need to push
  271. // it here though.
  272. if (firstLEDState) {
  273. firstLEDState = false;
  274. if (hasFocus())
  275. pushLEDState();
  276. return;
  277. }
  278. if (!hasFocus())
  279. return;
  280. #if defined(WIN32)
  281. INPUT input[6];
  282. UINT count;
  283. UINT ret;
  284. memset(input, 0, sizeof(input));
  285. count = 0;
  286. if (!!(state & ledCapsLock) != !!(GetKeyState(VK_CAPITAL) & 0x1)) {
  287. input[count].type = input[count+1].type = INPUT_KEYBOARD;
  288. input[count].ki.wVk = input[count+1].ki.wVk = VK_CAPITAL;
  289. input[count].ki.wScan = input[count+1].ki.wScan = SCAN_FAKE;
  290. input[count].ki.dwFlags = 0;
  291. input[count+1].ki.dwFlags = KEYEVENTF_KEYUP;
  292. count += 2;
  293. }
  294. if (!!(state & ledNumLock) != !!(GetKeyState(VK_NUMLOCK) & 0x1)) {
  295. input[count].type = input[count+1].type = INPUT_KEYBOARD;
  296. input[count].ki.wVk = input[count+1].ki.wVk = VK_NUMLOCK;
  297. input[count].ki.wScan = input[count+1].ki.wScan = SCAN_FAKE;
  298. input[count].ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
  299. input[count+1].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY;
  300. count += 2;
  301. }
  302. if (!!(state & ledScrollLock) != !!(GetKeyState(VK_SCROLL) & 0x1)) {
  303. input[count].type = input[count+1].type = INPUT_KEYBOARD;
  304. input[count].ki.wVk = input[count+1].ki.wVk = VK_SCROLL;
  305. input[count].ki.wScan = input[count+1].ki.wScan = SCAN_FAKE;
  306. input[count].ki.dwFlags = 0;
  307. input[count+1].ki.dwFlags = KEYEVENTF_KEYUP;
  308. count += 2;
  309. }
  310. if (count == 0)
  311. return;
  312. ret = SendInput(count, input, sizeof(*input));
  313. if (ret < count)
  314. vlog.error(_("Failed to update keyboard LED state: %lu"), GetLastError());
  315. #elif defined(__APPLE__)
  316. int ret;
  317. ret = cocoa_set_caps_lock_state(state & ledCapsLock);
  318. if (ret != 0) {
  319. vlog.error(_("Failed to update keyboard LED state: %d"), ret);
  320. return;
  321. }
  322. ret = cocoa_set_num_lock_state(state & ledNumLock);
  323. if (ret != 0) {
  324. vlog.error(_("Failed to update keyboard LED state: %d"), ret);
  325. return;
  326. }
  327. // No support for Scroll Lock //
  328. #else
  329. unsigned int affect, values;
  330. unsigned int mask;
  331. Bool ret;
  332. affect = values = 0;
  333. affect |= LockMask;
  334. if (state & ledCapsLock)
  335. values |= LockMask;
  336. mask = getModifierMask(XK_Num_Lock);
  337. affect |= mask;
  338. if (state & ledNumLock)
  339. values |= mask;
  340. mask = getModifierMask(XK_Scroll_Lock);
  341. affect |= mask;
  342. if (state & ledScrollLock)
  343. values |= mask;
  344. ret = XkbLockModifiers(fl_display, XkbUseCoreKbd, affect, values);
  345. if (!ret)
  346. vlog.error(_("Failed to update keyboard LED state"));
  347. #endif
  348. }
  349. void Viewport::pushLEDState()
  350. {
  351. unsigned int state;
  352. // Server support?
  353. if (cc->server.ledState() == ledUnknown)
  354. return;
  355. state = 0;
  356. #if defined(WIN32)
  357. if (GetKeyState(VK_CAPITAL) & 0x1)
  358. state |= ledCapsLock;
  359. if (GetKeyState(VK_NUMLOCK) & 0x1)
  360. state |= ledNumLock;
  361. if (GetKeyState(VK_SCROLL) & 0x1)
  362. state |= ledScrollLock;
  363. #elif defined(__APPLE__)
  364. int ret;
  365. bool on;
  366. ret = cocoa_get_caps_lock_state(&on);
  367. if (ret != 0) {
  368. vlog.error(_("Failed to get keyboard LED state: %d"), ret);
  369. return;
  370. }
  371. if (on)
  372. state |= ledCapsLock;
  373. ret = cocoa_get_num_lock_state(&on);
  374. if (ret != 0) {
  375. vlog.error(_("Failed to get keyboard LED state: %d"), ret);
  376. return;
  377. }
  378. if (on)
  379. state |= ledNumLock;
  380. // No support for Scroll Lock //
  381. state |= (cc->server.ledState() & ledScrollLock);
  382. #else
  383. unsigned int mask;
  384. Status status;
  385. XkbStateRec xkbState;
  386. status = XkbGetState(fl_display, XkbUseCoreKbd, &xkbState);
  387. if (status != Success) {
  388. vlog.error(_("Failed to get keyboard LED state: %d"), status);
  389. return;
  390. }
  391. if (xkbState.locked_mods & LockMask)
  392. state |= ledCapsLock;
  393. mask = getModifierMask(XK_Num_Lock);
  394. if (xkbState.locked_mods & mask)
  395. state |= ledNumLock;
  396. mask = getModifierMask(XK_Scroll_Lock);
  397. if (xkbState.locked_mods & mask)
  398. state |= ledScrollLock;
  399. #endif
  400. if ((state & ledCapsLock) != (cc->server.ledState() & ledCapsLock)) {
  401. vlog.debug("Inserting fake CapsLock to get in sync with server");
  402. handleKeyPress(0x3a, XK_Caps_Lock);
  403. handleKeyRelease(0x3a);
  404. }
  405. if ((state & ledNumLock) != (cc->server.ledState() & ledNumLock)) {
  406. vlog.debug("Inserting fake NumLock to get in sync with server");
  407. handleKeyPress(0x45, XK_Num_Lock);
  408. handleKeyRelease(0x45);
  409. }
  410. if ((state & ledScrollLock) != (cc->server.ledState() & ledScrollLock)) {
  411. vlog.debug("Inserting fake ScrollLock to get in sync with server");
  412. handleKeyPress(0x46, XK_Scroll_Lock);
  413. handleKeyRelease(0x46);
  414. }
  415. }
  416. void Viewport::draw(Surface* dst)
  417. {
  418. int X, Y, W, H;
  419. // Check what actually needs updating
  420. fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
  421. if ((W == 0) || (H == 0))
  422. return;
  423. frameBuffer->draw(dst, X - x(), Y - y(), X, Y, W, H);
  424. }
  425. void Viewport::draw()
  426. {
  427. int X, Y, W, H;
  428. // Check what actually needs updating
  429. fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
  430. if ((W == 0) || (H == 0))
  431. return;
  432. frameBuffer->draw(X - x(), Y - y(), X, Y, W, H);
  433. }
  434. void Viewport::resize(int x, int y, int w, int h)
  435. {
  436. if ((w != frameBuffer->width()) || (h != frameBuffer->height())) {
  437. vlog.debug("Resizing framebuffer from %dx%d to %dx%d",
  438. frameBuffer->width(), frameBuffer->height(), w, h);
  439. frameBuffer = new PlatformPixelBuffer(w, h);
  440. assert(frameBuffer);
  441. cc->setFramebuffer(frameBuffer);
  442. }
  443. Fl_Widget::resize(x, y, w, h);
  444. }
  445. int Viewport::handle(int event)
  446. {
  447. std::string filtered;
  448. int buttonMask, wheelMask;
  449. DownMap::const_iterator iter;
  450. switch (event) {
  451. case FL_PASTE:
  452. filtered = convertLF(Fl::event_text(), Fl::event_length());
  453. vlog.debug("Sending clipboard data (%d bytes)", (int)filtered.size());
  454. try {
  455. cc->sendClipboardData(filtered.c_str());
  456. } catch (rdr::Exception& e) {
  457. vlog.error("%s", e.str());
  458. abort_connection_with_unexpected_error(e);
  459. }
  460. return 1;
  461. case FL_ENTER:
  462. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  463. // Yes, we would like some pointer events please!
  464. return 1;
  465. case FL_LEAVE:
  466. window()->cursor(FL_CURSOR_DEFAULT);
  467. // We want a last move event to help trigger edge stuff
  468. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), 0);
  469. return 1;
  470. case FL_PUSH:
  471. case FL_RELEASE:
  472. case FL_DRAG:
  473. case FL_MOVE:
  474. case FL_MOUSEWHEEL:
  475. buttonMask = 0;
  476. if (Fl::event_button1())
  477. buttonMask |= 1;
  478. if (Fl::event_button2())
  479. buttonMask |= 2;
  480. if (Fl::event_button3())
  481. buttonMask |= 4;
  482. if (event == FL_MOUSEWHEEL) {
  483. wheelMask = 0;
  484. if (Fl::event_dy() < 0)
  485. wheelMask |= 8;
  486. if (Fl::event_dy() > 0)
  487. wheelMask |= 16;
  488. if (Fl::event_dx() < 0)
  489. wheelMask |= 32;
  490. if (Fl::event_dx() > 0)
  491. wheelMask |= 64;
  492. // A quick press of the wheel "button", followed by a immediate
  493. // release below
  494. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()),
  495. buttonMask | wheelMask);
  496. }
  497. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), buttonMask);
  498. return 1;
  499. case FL_FOCUS:
  500. Fl::disable_im();
  501. flushPendingClipboard();
  502. // We may have gotten our lock keys out of sync with the server
  503. // whilst we didn't have focus. Try to sort this out.
  504. pushLEDState();
  505. // Resend Ctrl/Alt if needed
  506. if (menuCtrlKey)
  507. handleKeyPress(0x1d, XK_Control_L);
  508. if (menuAltKey)
  509. handleKeyPress(0x38, XK_Alt_L);
  510. // Yes, we would like some focus please!
  511. return 1;
  512. case FL_UNFOCUS:
  513. // We won't get more key events, so reset our knowledge about keys
  514. resetKeyboard();
  515. Fl::enable_im();
  516. return 1;
  517. case FL_KEYDOWN:
  518. case FL_KEYUP:
  519. // Just ignore these as keys were handled in the event handler
  520. return 1;
  521. }
  522. return Fl_Widget::handle(event);
  523. }
  524. void Viewport::sendPointerEvent(const rfb::Point& pos, int buttonMask)
  525. {
  526. if (viewOnly)
  527. return;
  528. if ((pointerEventInterval == 0) || (buttonMask != lastButtonMask)) {
  529. try {
  530. cc->writer()->writePointerEvent(pos, buttonMask);
  531. } catch (rdr::Exception& e) {
  532. vlog.error("%s", e.str());
  533. abort_connection_with_unexpected_error(e);
  534. }
  535. } else {
  536. if (!Fl::has_timeout(handlePointerTimeout, this))
  537. Fl::add_timeout((double)pointerEventInterval/1000.0,
  538. handlePointerTimeout, this);
  539. }
  540. lastPointerPos = pos;
  541. lastButtonMask = buttonMask;
  542. }
  543. bool Viewport::hasFocus()
  544. {
  545. Fl_Widget* focus;
  546. focus = Fl::grab();
  547. if (!focus)
  548. focus = Fl::focus();
  549. return focus == this;
  550. }
  551. #if ! (defined(WIN32) || defined(__APPLE__))
  552. unsigned int Viewport::getModifierMask(unsigned int keysym)
  553. {
  554. XkbDescPtr xkb;
  555. unsigned int mask, keycode;
  556. XkbAction *act;
  557. mask = 0;
  558. xkb = XkbGetMap(fl_display, XkbAllComponentsMask, XkbUseCoreKbd);
  559. if (xkb == NULL)
  560. return 0;
  561. for (keycode = xkb->min_key_code; keycode <= xkb->max_key_code; keycode++) {
  562. unsigned int state_out;
  563. KeySym ks;
  564. XkbTranslateKeyCode(xkb, keycode, 0, &state_out, &ks);
  565. if (ks == NoSymbol)
  566. continue;
  567. if (ks == keysym)
  568. break;
  569. }
  570. // KeySym not mapped?
  571. if (keycode > xkb->max_key_code)
  572. goto out;
  573. act = XkbKeyAction(xkb, keycode, 0);
  574. if (act == NULL)
  575. goto out;
  576. if (act->type != XkbSA_LockMods)
  577. goto out;
  578. if (act->mods.flags & XkbSA_UseModMapMods)
  579. mask = xkb->map->modmap[keycode];
  580. else
  581. mask = act->mods.mask;
  582. out:
  583. XkbFreeKeyboard(xkb, XkbAllComponentsMask, True);
  584. return mask;
  585. }
  586. #endif
  587. void Viewport::handleClipboardChange(int source, void *data)
  588. {
  589. Viewport *self = (Viewport *)data;
  590. assert(self);
  591. if (!sendClipboard)
  592. return;
  593. #if !defined(WIN32) && !defined(__APPLE__)
  594. if (!sendPrimary && (source == 0))
  595. return;
  596. #endif
  597. self->clipboardSource = source;
  598. self->pendingServerClipboard = false;
  599. if (!self->hasFocus()) {
  600. vlog.debug("Local clipboard changed whilst not focused, will notify server later");
  601. self->pendingClientClipboard = true;
  602. // Clear any older client clipboard from the server
  603. self->cc->announceClipboard(false);
  604. return;
  605. }
  606. vlog.debug("Local clipboard changed, notifying server");
  607. try {
  608. self->cc->announceClipboard(true);
  609. } catch (rdr::Exception& e) {
  610. vlog.error("%s", e.str());
  611. abort_connection_with_unexpected_error(e);
  612. }
  613. }
  614. void Viewport::flushPendingClipboard()
  615. {
  616. if (pendingServerClipboard) {
  617. vlog.debug("Focus regained after remote clipboard change, requesting data");
  618. try {
  619. cc->requestClipboard();
  620. } catch (rdr::Exception& e) {
  621. vlog.error("%s", e.str());
  622. abort_connection_with_unexpected_error(e);
  623. }
  624. }
  625. if (pendingClientClipboard) {
  626. vlog.debug("Focus regained after local clipboard change, notifying server");
  627. try {
  628. cc->announceClipboard(true);
  629. } catch (rdr::Exception& e) {
  630. vlog.error("%s", e.str());
  631. abort_connection_with_unexpected_error(e);
  632. }
  633. }
  634. pendingServerClipboard = false;
  635. pendingClientClipboard = false;
  636. }
  637. void Viewport::handlePointerEvent(const rfb::Point& pos, int buttonMask)
  638. {
  639. filterPointerEvent(pos, buttonMask);
  640. }
  641. void Viewport::handlePointerTimeout(void *data)
  642. {
  643. Viewport *self = (Viewport *)data;
  644. assert(self);
  645. try {
  646. self->cc->writer()->writePointerEvent(self->lastPointerPos,
  647. self->lastButtonMask);
  648. } catch (rdr::Exception& e) {
  649. vlog.error("%s", e.str());
  650. abort_connection_with_unexpected_error(e);
  651. }
  652. }
  653. void Viewport::resetKeyboard()
  654. {
  655. while (!downKeySym.empty())
  656. handleKeyRelease(downKeySym.begin()->first);
  657. }
  658. void Viewport::handleKeyPress(int keyCode, uint32_t keySym)
  659. {
  660. static bool menuRecursion = false;
  661. // Prevent recursion if the menu wants to send its own
  662. // activation key.
  663. if (menuKeySym && (keySym == menuKeySym) && !menuRecursion) {
  664. menuRecursion = true;
  665. popupContextMenu();
  666. menuRecursion = false;
  667. return;
  668. }
  669. if (viewOnly)
  670. return;
  671. if (keyCode == 0) {
  672. vlog.error(_("No key code specified on key press"));
  673. return;
  674. }
  675. #ifdef __APPLE__
  676. // Alt on OS X behaves more like AltGr on other systems, and to get
  677. // sane behaviour we should translate things in that manner for the
  678. // remote VNC server. However that means we lose the ability to use
  679. // Alt as a shortcut modifier. Do what RealVNC does and hijack the
  680. // left command key as an Alt replacement.
  681. switch (keySym) {
  682. case XK_Super_L:
  683. keySym = XK_Alt_L;
  684. break;
  685. case XK_Super_R:
  686. keySym = XK_Super_L;
  687. break;
  688. case XK_Alt_L:
  689. keySym = XK_Mode_switch;
  690. break;
  691. case XK_Alt_R:
  692. keySym = XK_ISO_Level3_Shift;
  693. break;
  694. }
  695. #endif
  696. // Because of the way keyboards work, we cannot expect to have the same
  697. // symbol on release as when pressed. This breaks the VNC protocol however,
  698. // so we need to keep track of what keysym a key _code_ generated on press
  699. // and send the same on release.
  700. downKeySym[keyCode] = keySym;
  701. #if defined(WIN32) || defined(__APPLE__)
  702. vlog.debug("Key pressed: 0x%04x => 0x%04x", keyCode, keySym);
  703. #else
  704. vlog.debug("Key pressed: 0x%04x => XK_%s (0x%04x)",
  705. keyCode, XKeysymToString(keySym), keySym);
  706. #endif
  707. try {
  708. // Fake keycode?
  709. if (keyCode > 0xff)
  710. cc->writer()->writeKeyEvent(keySym, 0, true);
  711. else
  712. cc->writer()->writeKeyEvent(keySym, keyCode, true);
  713. } catch (rdr::Exception& e) {
  714. vlog.error("%s", e.str());
  715. abort_connection_with_unexpected_error(e);
  716. }
  717. }
  718. void Viewport::handleKeyRelease(int keyCode)
  719. {
  720. DownMap::iterator iter;
  721. if (viewOnly)
  722. return;
  723. iter = downKeySym.find(keyCode);
  724. if (iter == downKeySym.end()) {
  725. // These occur somewhat frequently so let's not spam them unless
  726. // logging is turned up.
  727. vlog.debug("Unexpected release of key code %d", keyCode);
  728. return;
  729. }
  730. #if defined(WIN32) || defined(__APPLE__)
  731. vlog.debug("Key released: 0x%04x => 0x%04x", keyCode, iter->second);
  732. #else
  733. vlog.debug("Key released: 0x%04x => XK_%s (0x%04x)",
  734. keyCode, XKeysymToString(iter->second), iter->second);
  735. #endif
  736. try {
  737. if (keyCode > 0xff)
  738. cc->writer()->writeKeyEvent(iter->second, 0, false);
  739. else
  740. cc->writer()->writeKeyEvent(iter->second, keyCode, false);
  741. } catch (rdr::Exception& e) {
  742. vlog.error("%s", e.str());
  743. abort_connection_with_unexpected_error(e);
  744. }
  745. downKeySym.erase(iter);
  746. }
  747. int Viewport::handleSystemEvent(void *event, void *data)
  748. {
  749. Viewport *self = (Viewport *)data;
  750. assert(self);
  751. if (!self->hasFocus())
  752. return 0;
  753. assert(event);
  754. #if defined(WIN32)
  755. MSG *msg = (MSG*)event;
  756. if ((msg->message == WM_MOUSEMOVE) ||
  757. (msg->message == WM_LBUTTONDOWN) ||
  758. (msg->message == WM_LBUTTONUP) ||
  759. (msg->message == WM_RBUTTONDOWN) ||
  760. (msg->message == WM_RBUTTONUP) ||
  761. (msg->message == WM_MBUTTONDOWN) ||
  762. (msg->message == WM_MBUTTONUP) ||
  763. (msg->message == WM_MOUSEWHEEL) ||
  764. (msg->message == WM_MOUSEHWHEEL)) {
  765. // We can't get a mouse event in the middle of an AltGr sequence, so
  766. // abort that detection
  767. if (self->altGrArmed)
  768. self->resolveAltGrDetection(false);
  769. return 0; // We didn't really consume the mouse event
  770. } else if ((msg->message == WM_KEYDOWN) || (msg->message == WM_SYSKEYDOWN)) {
  771. UINT vKey;
  772. bool isExtended;
  773. int keyCode;
  774. uint32_t keySym;
  775. vKey = msg->wParam;
  776. isExtended = (msg->lParam & (1 << 24)) != 0;
  777. keyCode = ((msg->lParam >> 16) & 0xff);
  778. // Windows' touch keyboard doesn't set a scan code for the Alt
  779. // portion of the AltGr sequence, so we need to help it out
  780. if (!isExtended && (keyCode == 0x00) && (vKey == VK_MENU)) {
  781. isExtended = true;
  782. keyCode = 0x38;
  783. }
  784. // Windows doesn't have a proper AltGr, but handles it using fake
  785. // Ctrl+Alt. However the remote end might not be Windows, so we need
  786. // to merge those in to a single AltGr event. We detect this case
  787. // by seeing the two key events directly after each other with a very
  788. // short time between them (<50ms) and supress the Ctrl event.
  789. if (self->altGrArmed) {
  790. bool altPressed = isExtended &&
  791. (keyCode == 0x38) &&
  792. (vKey == VK_MENU) &&
  793. ((msg->time - self->altGrCtrlTime) < 50);
  794. self->resolveAltGrDetection(altPressed);
  795. }
  796. if (keyCode == SCAN_FAKE) {
  797. vlog.debug("Ignoring fake key press (virtual key 0x%02x)", vKey);
  798. return 1;
  799. }
  800. // Windows sets the scan code to 0x00 for multimedia keys, so we
  801. // have to do a reverse lookup based on the vKey.
  802. if (keyCode == 0x00) {
  803. keyCode = MapVirtualKey(vKey, MAPVK_VK_TO_VSC);
  804. if (keyCode == 0x00) {
  805. if (isExtended)
  806. vlog.error(_("No scan code for extended virtual key 0x%02x"), (int)vKey);
  807. else
  808. vlog.error(_("No scan code for virtual key 0x%02x"), (int)vKey);
  809. return 1;
  810. }
  811. }
  812. if (keyCode & ~0x7f) {
  813. vlog.error(_("Invalid scan code 0x%02x"), (int)keyCode);
  814. return 1;
  815. }
  816. if (isExtended)
  817. keyCode |= 0x80;
  818. // Fortunately RFB and Windows use the same scan code set (mostly),
  819. // so there is no conversion needed
  820. // (as long as we encode the extended keys with the high bit)
  821. // However Pause sends a code that conflicts with NumLock, so use
  822. // the code most RFB implementations use (part of the sequence for
  823. // Ctrl+Pause, i.e. Break)
  824. if (keyCode == 0x45)
  825. keyCode = 0xc6;
  826. // And NumLock incorrectly has the extended bit set
  827. if (keyCode == 0xc5)
  828. keyCode = 0x45;
  829. // And Alt+PrintScreen (i.e. SysRq) sends a different code than
  830. // PrintScreen
  831. if (keyCode == 0xb7)
  832. keyCode = 0x54;
  833. keySym = win32_vkey_to_keysym(vKey, isExtended);
  834. if (keySym == NoSymbol) {
  835. if (isExtended)
  836. vlog.error(_("No symbol for extended virtual key 0x%02x"), (int)vKey);
  837. else
  838. vlog.error(_("No symbol for virtual key 0x%02x"), (int)vKey);
  839. }
  840. // Windows sends the same vKey for both shifts, so we need to look
  841. // at the scan code to tell them apart
  842. if ((keySym == XK_Shift_L) && (keyCode == 0x36))
  843. keySym = XK_Shift_R;
  844. // AltGr handling (see above)
  845. if (win32_has_altgr()) {
  846. if ((keyCode == 0xb8) && (keySym == XK_Alt_R))
  847. keySym = XK_ISO_Level3_Shift;
  848. // Possible start of AltGr sequence?
  849. if ((keyCode == 0x1d) && (keySym == XK_Control_L)) {
  850. self->altGrArmed = true;
  851. self->altGrCtrlTime = msg->time;
  852. Fl::add_timeout(0.1, handleAltGrTimeout, self);
  853. return 1;
  854. }
  855. }
  856. self->handleKeyPress(keyCode, keySym);
  857. // We don't get reliable WM_KEYUP for these
  858. switch (keySym) {
  859. case XK_Zenkaku_Hankaku:
  860. case XK_Eisu_toggle:
  861. case XK_Katakana:
  862. case XK_Hiragana:
  863. case XK_Romaji:
  864. self->handleKeyRelease(keyCode);
  865. }
  866. return 1;
  867. } else if ((msg->message == WM_KEYUP) || (msg->message == WM_SYSKEYUP)) {
  868. UINT vKey;
  869. bool isExtended;
  870. int keyCode;
  871. vKey = msg->wParam;
  872. isExtended = (msg->lParam & (1 << 24)) != 0;
  873. keyCode = ((msg->lParam >> 16) & 0xff);
  874. // Touch keyboard AltGr (see above)
  875. if (!isExtended && (keyCode == 0x00) && (vKey == VK_MENU)) {
  876. isExtended = true;
  877. keyCode = 0x38;
  878. }
  879. // We can't get a release in the middle of an AltGr sequence, so
  880. // abort that detection
  881. if (self->altGrArmed)
  882. self->resolveAltGrDetection(false);
  883. if (keyCode == SCAN_FAKE) {
  884. vlog.debug("Ignoring fake key release (virtual key 0x%02x)", vKey);
  885. return 1;
  886. }
  887. if (keyCode == 0x00)
  888. keyCode = MapVirtualKey(vKey, MAPVK_VK_TO_VSC);
  889. if (isExtended)
  890. keyCode |= 0x80;
  891. if (keyCode == 0x45)
  892. keyCode = 0xc6;
  893. if (keyCode == 0xc5)
  894. keyCode = 0x45;
  895. if (keyCode == 0xb7)
  896. keyCode = 0x54;
  897. self->handleKeyRelease(keyCode);
  898. // Windows has a rather nasty bug where it won't send key release
  899. // events for a Shift button if the other Shift is still pressed
  900. if ((keyCode == 0x2a) || (keyCode == 0x36)) {
  901. if (self->downKeySym.count(0x2a))
  902. self->handleKeyRelease(0x2a);
  903. if (self->downKeySym.count(0x36))
  904. self->handleKeyRelease(0x36);
  905. }
  906. return 1;
  907. }
  908. #elif defined(__APPLE__)
  909. // Special event that means we temporarily lost some input
  910. if (cocoa_is_keyboard_sync(event)) {
  911. self->resetKeyboard();
  912. return 1;
  913. }
  914. if (cocoa_is_keyboard_event(event)) {
  915. int keyCode;
  916. keyCode = cocoa_event_keycode(event);
  917. if ((unsigned)keyCode >= code_map_osx_to_qnum_len)
  918. keyCode = 0;
  919. else
  920. keyCode = code_map_osx_to_qnum[keyCode];
  921. if (cocoa_is_key_press(event)) {
  922. uint32_t keySym;
  923. keySym = cocoa_event_keysym(event);
  924. if (keySym == NoSymbol) {
  925. vlog.error(_("No symbol for key code 0x%02x (in the current state)"),
  926. (int)keyCode);
  927. }
  928. self->handleKeyPress(keyCode, keySym);
  929. // We don't get any release events for CapsLock, so we have to
  930. // send the release right away.
  931. if (keySym == XK_Caps_Lock)
  932. self->handleKeyRelease(keyCode);
  933. } else {
  934. self->handleKeyRelease(keyCode);
  935. }
  936. return 1;
  937. }
  938. #else
  939. XEvent *xevent = (XEvent*)event;
  940. if (xevent->type == KeyPress) {
  941. int keycode;
  942. char str;
  943. KeySym keysym;
  944. keycode = code_map_keycode_to_qnum[xevent->xkey.keycode];
  945. // Generate a fake keycode just for tracking if we can't figure
  946. // out the proper one
  947. if (keycode == 0)
  948. keycode = 0x100 | xevent->xkey.keycode;
  949. XLookupString(&xevent->xkey, &str, 1, &keysym, NULL);
  950. if (keysym == NoSymbol) {
  951. vlog.error(_("No symbol for key code %d (in the current state)"),
  952. (int)xevent->xkey.keycode);
  953. }
  954. switch (keysym) {
  955. // For the first few years, there wasn't a good consensus on what the
  956. // Windows keys should be mapped to for X11. So we need to help out a
  957. // bit and map all variants to the same key...
  958. case XK_Hyper_L:
  959. keysym = XK_Super_L;
  960. break;
  961. case XK_Hyper_R:
  962. keysym = XK_Super_R;
  963. break;
  964. // There has been several variants for Shift-Tab over the years.
  965. // RFB states that we should always send a normal tab.
  966. case XK_ISO_Left_Tab:
  967. keysym = XK_Tab;
  968. break;
  969. }
  970. self->handleKeyPress(keycode, keysym);
  971. return 1;
  972. } else if (xevent->type == KeyRelease) {
  973. int keycode = code_map_keycode_to_qnum[xevent->xkey.keycode];
  974. if (keycode == 0)
  975. keycode = 0x100 | xevent->xkey.keycode;
  976. self->handleKeyRelease(keycode);
  977. return 1;
  978. }
  979. #endif
  980. return 0;
  981. }
  982. #ifdef WIN32
  983. void Viewport::handleAltGrTimeout(void *data)
  984. {
  985. Viewport *self = (Viewport *)data;
  986. assert(self);
  987. self->altGrArmed = false;
  988. self->handleKeyPress(0x1d, XK_Control_L);
  989. }
  990. void Viewport::resolveAltGrDetection(bool isAltGrSequence)
  991. {
  992. altGrArmed = false;
  993. Fl::remove_timeout(handleAltGrTimeout);
  994. // when it's not an AltGr sequence we can't supress the Ctrl anymore
  995. if (!isAltGrSequence)
  996. handleKeyPress(0x1d, XK_Control_L);
  997. }
  998. #endif
  999. void Viewport::initContextMenu()
  1000. {
  1001. contextMenu->clear();
  1002. fltk_menu_add(contextMenu, p_("ContextMenu|", "Dis&connect"),
  1003. 0, NULL, (void*)ID_DISCONNECT, FL_MENU_DIVIDER);
  1004. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Full screen"),
  1005. 0, NULL, (void*)ID_FULLSCREEN,
  1006. FL_MENU_TOGGLE | (window()->fullscreen_active()?FL_MENU_VALUE:0));
  1007. fltk_menu_add(contextMenu, p_("ContextMenu|", "Minimi&ze"),
  1008. 0, NULL, (void*)ID_MINIMIZE, 0);
  1009. fltk_menu_add(contextMenu, p_("ContextMenu|", "Resize &window to session"),
  1010. 0, NULL, (void*)ID_RESIZE,
  1011. (window()->fullscreen_active()?FL_MENU_INACTIVE:0) |
  1012. FL_MENU_DIVIDER);
  1013. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Ctrl"),
  1014. 0, NULL, (void*)ID_CTRL,
  1015. FL_MENU_TOGGLE | (menuCtrlKey?FL_MENU_VALUE:0));
  1016. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Alt"),
  1017. 0, NULL, (void*)ID_ALT,
  1018. FL_MENU_TOGGLE | (menuAltKey?FL_MENU_VALUE:0));
  1019. if (menuKeySym) {
  1020. char sendMenuKey[64];
  1021. snprintf(sendMenuKey, 64, p_("ContextMenu|", "Send %s"), (const char *)menuKey);
  1022. fltk_menu_add(contextMenu, sendMenuKey, 0, NULL, (void*)ID_MENUKEY, 0);
  1023. fltk_menu_add(contextMenu, "Secret shortcut menu key", menuKeyFLTK, NULL,
  1024. (void*)ID_MENUKEY, FL_MENU_INVISIBLE);
  1025. }
  1026. fltk_menu_add(contextMenu, p_("ContextMenu|", "Send Ctrl-Alt-&Del"),
  1027. 0, NULL, (void*)ID_CTRLALTDEL, FL_MENU_DIVIDER);
  1028. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Refresh screen"),
  1029. 0, NULL, (void*)ID_REFRESH, FL_MENU_DIVIDER);
  1030. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Options..."),
  1031. 0, NULL, (void*)ID_OPTIONS, 0);
  1032. fltk_menu_add(contextMenu, p_("ContextMenu|", "Connection &info..."),
  1033. 0, NULL, (void*)ID_INFO, 0);
  1034. fltk_menu_add(contextMenu, p_("ContextMenu|", "About &TigerVNC viewer..."),
  1035. 0, NULL, (void*)ID_ABOUT, 0);
  1036. }
  1037. void Viewport::popupContextMenu()
  1038. {
  1039. const Fl_Menu_Item *m;
  1040. char buffer[1024];
  1041. // Make sure the menu is reset to its initial state between goes or
  1042. // it will start up highlighting the previously selected entry.
  1043. contextMenu->value(-1);
  1044. // initialize context menu before display
  1045. initContextMenu();
  1046. // Unfortunately FLTK doesn't reliably restore the mouse pointer for
  1047. // menus, so we have to help it out.
  1048. if (Fl::belowmouse() == this)
  1049. window()->cursor(FL_CURSOR_DEFAULT);
  1050. // FLTK also doesn't switch focus properly for menus
  1051. handle(FL_UNFOCUS);
  1052. m = contextMenu->popup();
  1053. handle(FL_FOCUS);
  1054. // Back to our proper mouse pointer.
  1055. if (Fl::belowmouse())
  1056. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  1057. if (m == NULL)
  1058. return;
  1059. switch (m->argument()) {
  1060. case ID_DISCONNECT:
  1061. disconnect();
  1062. break;
  1063. case ID_FULLSCREEN:
  1064. if (window()->fullscreen_active())
  1065. window()->fullscreen_off();
  1066. else
  1067. ((DesktopWindow*)window())->fullscreen_on();
  1068. break;
  1069. case ID_MINIMIZE:
  1070. window()->iconize();
  1071. break;
  1072. case ID_RESIZE:
  1073. if (window()->fullscreen_active())
  1074. break;
  1075. window()->size(w(), h());
  1076. break;
  1077. case ID_CTRL:
  1078. if (m->value())
  1079. handleKeyPress(0x1d, XK_Control_L);
  1080. else
  1081. handleKeyRelease(0x1d);
  1082. menuCtrlKey = !menuCtrlKey;
  1083. break;
  1084. case ID_ALT:
  1085. if (m->value())
  1086. handleKeyPress(0x38, XK_Alt_L);
  1087. else
  1088. handleKeyRelease(0x38);
  1089. menuAltKey = !menuAltKey;
  1090. break;
  1091. case ID_MENUKEY:
  1092. handleKeyPress(menuKeyCode, menuKeySym);
  1093. handleKeyRelease(menuKeyCode);
  1094. break;
  1095. case ID_CTRLALTDEL:
  1096. handleKeyPress(0x1d, XK_Control_L);
  1097. handleKeyPress(0x38, XK_Alt_L);
  1098. handleKeyPress(0xd3, XK_Delete);
  1099. handleKeyRelease(0xd3);
  1100. handleKeyRelease(0x38);
  1101. handleKeyRelease(0x1d);
  1102. break;
  1103. case ID_REFRESH:
  1104. cc->refreshFramebuffer();
  1105. break;
  1106. case ID_OPTIONS:
  1107. OptionsDialog::showDialog();
  1108. break;
  1109. case ID_INFO:
  1110. if (fltk_escape(cc->connectionInfo(), buffer, sizeof(buffer)) < sizeof(buffer)) {
  1111. fl_message_title(_("VNC connection info"));
  1112. fl_message("%s", buffer);
  1113. }
  1114. break;
  1115. case ID_ABOUT:
  1116. about_vncviewer();
  1117. break;
  1118. }
  1119. }
  1120. void Viewport::setMenuKey()
  1121. {
  1122. getMenuKey(&menuKeyFLTK, &menuKeyCode, &menuKeySym);
  1123. }
  1124. void Viewport::handleOptions(void *data)
  1125. {
  1126. Viewport *self = (Viewport*)data;
  1127. self->setMenuKey();
  1128. // FIXME: Need to recheck cursor for dotWhenNoCursor
  1129. }