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

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