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

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