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 36KB

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