Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Viewport.cxx 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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_EXIT, 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. exit_vncviewer(_("An unexpected error occurred when communicating "
  457. "with the server:\n\n%s"), e.str());
  458. }
  459. strFree(filtered);
  460. return 1;
  461. case FL_ENTER:
  462. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  463. // Yes, we would like some pointer events please!
  464. return 1;
  465. case FL_LEAVE:
  466. window()->cursor(FL_CURSOR_DEFAULT);
  467. // We want a last move event to help trigger edge stuff
  468. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), 0);
  469. return 1;
  470. case FL_PUSH:
  471. case FL_RELEASE:
  472. case FL_DRAG:
  473. case FL_MOVE:
  474. case FL_MOUSEWHEEL:
  475. buttonMask = 0;
  476. if (Fl::event_button1())
  477. buttonMask |= 1;
  478. if (Fl::event_button2())
  479. buttonMask |= 2;
  480. if (Fl::event_button3())
  481. buttonMask |= 4;
  482. if (event == FL_MOUSEWHEEL) {
  483. wheelMask = 0;
  484. if (Fl::event_dy() < 0)
  485. wheelMask |= 8;
  486. if (Fl::event_dy() > 0)
  487. wheelMask |= 16;
  488. if (Fl::event_dx() < 0)
  489. wheelMask |= 32;
  490. if (Fl::event_dx() > 0)
  491. wheelMask |= 64;
  492. // A quick press of the wheel "button", followed by a immediate
  493. // release below
  494. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()),
  495. buttonMask | wheelMask);
  496. }
  497. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), buttonMask);
  498. return 1;
  499. case FL_FOCUS:
  500. Fl::disable_im();
  501. flushPendingClipboard();
  502. // We may have gotten our lock keys out of sync with the server
  503. // whilst we didn't have focus. Try to sort this out.
  504. pushLEDState();
  505. // Resend Ctrl/Alt if needed
  506. if (menuCtrlKey)
  507. handleKeyPress(0x1d, XK_Control_L);
  508. if (menuAltKey)
  509. handleKeyPress(0x38, XK_Alt_L);
  510. // Yes, we would like some focus please!
  511. return 1;
  512. case FL_UNFOCUS:
  513. // Release all keys that were pressed as that generally makes most
  514. // sense (e.g. Alt+Tab where we only see the Alt press)
  515. while (!downKeySym.empty())
  516. handleKeyRelease(downKeySym.begin()->first);
  517. Fl::enable_im();
  518. return 1;
  519. case FL_KEYDOWN:
  520. case FL_KEYUP:
  521. // Just ignore these as keys were handled in the event handler
  522. return 1;
  523. }
  524. return Fl_Widget::handle(event);
  525. }
  526. void Viewport::sendPointerEvent(const rfb::Point& pos, int buttonMask)
  527. {
  528. if (viewOnly)
  529. return;
  530. if ((pointerEventInterval == 0) || (buttonMask != lastButtonMask)) {
  531. try {
  532. cc->writer()->writePointerEvent(pos, buttonMask);
  533. } catch (rdr::Exception& e) {
  534. vlog.error("%s", e.str());
  535. exit_vncviewer(_("An unexpected error occurred when communicating "
  536. "with the server:\n\n%s"), e.str());
  537. }
  538. } else {
  539. if (!Fl::has_timeout(handlePointerTimeout, this))
  540. Fl::add_timeout((double)pointerEventInterval/1000.0,
  541. handlePointerTimeout, this);
  542. }
  543. lastPointerPos = pos;
  544. lastButtonMask = buttonMask;
  545. }
  546. bool Viewport::hasFocus()
  547. {
  548. Fl_Widget* focus;
  549. focus = Fl::grab();
  550. if (!focus)
  551. focus = Fl::focus();
  552. return focus == this;
  553. }
  554. #if ! (defined(WIN32) || defined(__APPLE__))
  555. unsigned int Viewport::getModifierMask(unsigned int keysym)
  556. {
  557. XkbDescPtr xkb;
  558. unsigned int mask, keycode;
  559. XkbAction *act;
  560. mask = 0;
  561. xkb = XkbGetMap(fl_display, XkbAllComponentsMask, XkbUseCoreKbd);
  562. if (xkb == NULL)
  563. return 0;
  564. for (keycode = xkb->min_key_code; keycode <= xkb->max_key_code; keycode++) {
  565. unsigned int state_out;
  566. KeySym ks;
  567. XkbTranslateKeyCode(xkb, keycode, 0, &state_out, &ks);
  568. if (ks == NoSymbol)
  569. continue;
  570. if (ks == keysym)
  571. break;
  572. }
  573. // KeySym not mapped?
  574. if (keycode > xkb->max_key_code)
  575. goto out;
  576. act = XkbKeyAction(xkb, keycode, 0);
  577. if (act == NULL)
  578. goto out;
  579. if (act->type != XkbSA_LockMods)
  580. goto out;
  581. if (act->mods.flags & XkbSA_UseModMapMods)
  582. mask = xkb->map->modmap[keycode];
  583. else
  584. mask = act->mods.mask;
  585. out:
  586. XkbFreeKeyboard(xkb, XkbAllComponentsMask, True);
  587. return mask;
  588. }
  589. #endif
  590. void Viewport::handleClipboardChange(int source, void *data)
  591. {
  592. Viewport *self = (Viewport *)data;
  593. assert(self);
  594. if (!sendClipboard)
  595. return;
  596. #if !defined(WIN32) && !defined(__APPLE__)
  597. if (!sendPrimary && (source == 0))
  598. return;
  599. #endif
  600. self->clipboardSource = source;
  601. self->pendingServerClipboard = false;
  602. if (!self->hasFocus()) {
  603. vlog.debug("Local clipboard changed whilst not focused, will notify server later");
  604. self->pendingClientClipboard = true;
  605. // Clear any older client clipboard from the server
  606. self->cc->announceClipboard(false);
  607. return;
  608. }
  609. vlog.debug("Local clipboard changed, notifying server");
  610. try {
  611. self->cc->announceClipboard(true);
  612. } catch (rdr::Exception& e) {
  613. vlog.error("%s", e.str());
  614. exit_vncviewer(_("An unexpected error occurred when communicating "
  615. "with the server:\n\n%s"), e.str());
  616. }
  617. }
  618. void Viewport::flushPendingClipboard()
  619. {
  620. if (pendingServerClipboard) {
  621. vlog.debug("Focus regained after remote clipboard change, requesting data");
  622. try {
  623. cc->requestClipboard();
  624. } catch (rdr::Exception& e) {
  625. vlog.error("%s", e.str());
  626. exit_vncviewer(_("An unexpected error occurred when communicating "
  627. "with the server:\n\n%s"), e.str());
  628. }
  629. }
  630. if (pendingClientClipboard) {
  631. vlog.debug("Focus regained after local clipboard change, notifying server");
  632. try {
  633. cc->announceClipboard(true);
  634. } catch (rdr::Exception& e) {
  635. vlog.error("%s", e.str());
  636. exit_vncviewer(_("An unexpected error occurred when communicating "
  637. "with the server:\n\n%s"), e.str());
  638. }
  639. }
  640. pendingServerClipboard = false;
  641. pendingClientClipboard = false;
  642. }
  643. void Viewport::handlePointerEvent(const rfb::Point& pos, int buttonMask)
  644. {
  645. filterPointerEvent(pos, buttonMask);
  646. }
  647. void Viewport::handlePointerTimeout(void *data)
  648. {
  649. Viewport *self = (Viewport *)data;
  650. assert(self);
  651. try {
  652. self->cc->writer()->writePointerEvent(self->lastPointerPos,
  653. self->lastButtonMask);
  654. } catch (rdr::Exception& e) {
  655. vlog.error("%s", e.str());
  656. exit_vncviewer(_("An unexpected error occurred when communicating "
  657. "with the server:\n\n%s"), e.str());
  658. }
  659. }
  660. void Viewport::handleKeyPress(int keyCode, rdr::U32 keySym)
  661. {
  662. static bool menuRecursion = false;
  663. // Prevent recursion if the menu wants to send its own
  664. // activation key.
  665. if (menuKeySym && (keySym == menuKeySym) && !menuRecursion) {
  666. menuRecursion = true;
  667. popupContextMenu();
  668. menuRecursion = false;
  669. return;
  670. }
  671. if (viewOnly)
  672. return;
  673. if (keyCode == 0) {
  674. vlog.error(_("No key code specified on key press"));
  675. return;
  676. }
  677. #ifdef __APPLE__
  678. // Alt on OS X behaves more like AltGr on other systems, and to get
  679. // sane behaviour we should translate things in that manner for the
  680. // remote VNC server. However that means we lose the ability to use
  681. // Alt as a shortcut modifier. Do what RealVNC does and hijack the
  682. // left command key as an Alt replacement.
  683. switch (keySym) {
  684. case XK_Super_L:
  685. keySym = XK_Alt_L;
  686. break;
  687. case XK_Super_R:
  688. keySym = XK_Super_L;
  689. break;
  690. case XK_Alt_L:
  691. keySym = XK_Mode_switch;
  692. break;
  693. case XK_Alt_R:
  694. keySym = XK_ISO_Level3_Shift;
  695. break;
  696. }
  697. #endif
  698. // Because of the way keyboards work, we cannot expect to have the same
  699. // symbol on release as when pressed. This breaks the VNC protocol however,
  700. // so we need to keep track of what keysym a key _code_ generated on press
  701. // and send the same on release.
  702. downKeySym[keyCode] = keySym;
  703. #if defined(WIN32) || defined(__APPLE__)
  704. vlog.debug("Key pressed: 0x%04x => 0x%04x", keyCode, keySym);
  705. #else
  706. vlog.debug("Key pressed: 0x%04x => XK_%s (0x%04x)",
  707. keyCode, XKeysymToString(keySym), keySym);
  708. #endif
  709. try {
  710. // Fake keycode?
  711. if (keyCode > 0xff)
  712. cc->writer()->writeKeyEvent(keySym, 0, true);
  713. else
  714. cc->writer()->writeKeyEvent(keySym, keyCode, true);
  715. } catch (rdr::Exception& e) {
  716. vlog.error("%s", e.str());
  717. exit_vncviewer(_("An unexpected error occurred when communicating "
  718. "with the server:\n\n%s"), e.str());
  719. }
  720. }
  721. void Viewport::handleKeyRelease(int keyCode)
  722. {
  723. DownMap::iterator iter;
  724. if (viewOnly)
  725. return;
  726. iter = downKeySym.find(keyCode);
  727. if (iter == downKeySym.end()) {
  728. // These occur somewhat frequently so let's not spam them unless
  729. // logging is turned up.
  730. vlog.debug("Unexpected release of key code %d", keyCode);
  731. return;
  732. }
  733. #if defined(WIN32) || defined(__APPLE__)
  734. vlog.debug("Key released: 0x%04x => 0x%04x", keyCode, iter->second);
  735. #else
  736. vlog.debug("Key released: 0x%04x => XK_%s (0x%04x)",
  737. keyCode, XKeysymToString(iter->second), iter->second);
  738. #endif
  739. try {
  740. if (keyCode > 0xff)
  741. cc->writer()->writeKeyEvent(iter->second, 0, false);
  742. else
  743. cc->writer()->writeKeyEvent(iter->second, keyCode, false);
  744. } catch (rdr::Exception& e) {
  745. vlog.error("%s", e.str());
  746. exit_vncviewer(_("An unexpected error occurred when communicating "
  747. "with the server:\n\n%s"), e.str());
  748. }
  749. downKeySym.erase(iter);
  750. }
  751. int Viewport::handleSystemEvent(void *event, void *data)
  752. {
  753. Viewport *self = (Viewport *)data;
  754. assert(self);
  755. if (!self->hasFocus())
  756. return 0;
  757. assert(event);
  758. #if defined(WIN32)
  759. MSG *msg = (MSG*)event;
  760. if ((msg->message == WM_MOUSEMOVE) ||
  761. (msg->message == WM_LBUTTONDOWN) ||
  762. (msg->message == WM_LBUTTONUP) ||
  763. (msg->message == WM_RBUTTONDOWN) ||
  764. (msg->message == WM_RBUTTONUP) ||
  765. (msg->message == WM_MBUTTONDOWN) ||
  766. (msg->message == WM_MBUTTONUP) ||
  767. (msg->message == WM_MOUSEWHEEL) ||
  768. (msg->message == WM_MOUSEHWHEEL)) {
  769. // We can't get a mouse event in the middle of an AltGr sequence, so
  770. // abort that detection
  771. if (self->altGrArmed)
  772. self->resolveAltGrDetection(false);
  773. return 0; // We didn't really consume the mouse event
  774. } else if ((msg->message == WM_KEYDOWN) || (msg->message == WM_SYSKEYDOWN)) {
  775. UINT vKey;
  776. bool isExtended;
  777. int keyCode;
  778. rdr::U32 keySym;
  779. vKey = msg->wParam;
  780. isExtended = (msg->lParam & (1 << 24)) != 0;
  781. keyCode = ((msg->lParam >> 16) & 0xff);
  782. // Windows' touch keyboard doesn't set a scan code for the Alt
  783. // portion of the AltGr sequence, so we need to help it out
  784. if (!isExtended && (keyCode == 0x00) && (vKey == VK_MENU)) {
  785. isExtended = true;
  786. keyCode = 0x38;
  787. }
  788. // Windows doesn't have a proper AltGr, but handles it using fake
  789. // Ctrl+Alt. However the remote end might not be Windows, so we need
  790. // to merge those in to a single AltGr event. We detect this case
  791. // by seeing the two key events directly after each other with a very
  792. // short time between them (<50ms) and supress the Ctrl event.
  793. if (self->altGrArmed) {
  794. bool altPressed = isExtended &&
  795. (keyCode == 0x38) &&
  796. (vKey == VK_MENU) &&
  797. ((msg->time - self->altGrCtrlTime) < 50);
  798. self->resolveAltGrDetection(altPressed);
  799. }
  800. if (keyCode == SCAN_FAKE) {
  801. vlog.debug("Ignoring fake key press (virtual key 0x%02x)", vKey);
  802. return 1;
  803. }
  804. // Windows sets the scan code to 0x00 for multimedia keys, so we
  805. // have to do a reverse lookup based on the vKey.
  806. if (keyCode == 0x00) {
  807. keyCode = MapVirtualKey(vKey, MAPVK_VK_TO_VSC);
  808. if (keyCode == 0x00) {
  809. if (isExtended)
  810. vlog.error(_("No scan code for extended virtual key 0x%02x"), (int)vKey);
  811. else
  812. vlog.error(_("No scan code for virtual key 0x%02x"), (int)vKey);
  813. return 1;
  814. }
  815. }
  816. if (keyCode & ~0x7f) {
  817. vlog.error(_("Invalid scan code 0x%02x"), (int)keyCode);
  818. return 1;
  819. }
  820. if (isExtended)
  821. keyCode |= 0x80;
  822. // Fortunately RFB and Windows use the same scan code set (mostly),
  823. // so there is no conversion needed
  824. // (as long as we encode the extended keys with the high bit)
  825. // However Pause sends a code that conflicts with NumLock, so use
  826. // the code most RFB implementations use (part of the sequence for
  827. // Ctrl+Pause, i.e. Break)
  828. if (keyCode == 0x45)
  829. keyCode = 0xc6;
  830. // And NumLock incorrectly has the extended bit set
  831. if (keyCode == 0xc5)
  832. keyCode = 0x45;
  833. // And Alt+PrintScreen (i.e. SysRq) sends a different code than
  834. // PrintScreen
  835. if (keyCode == 0xb7)
  836. keyCode = 0x54;
  837. keySym = win32_vkey_to_keysym(vKey, isExtended);
  838. if (keySym == NoSymbol) {
  839. if (isExtended)
  840. vlog.error(_("No symbol for extended virtual key 0x%02x"), (int)vKey);
  841. else
  842. vlog.error(_("No symbol for virtual key 0x%02x"), (int)vKey);
  843. }
  844. // Windows sends the same vKey for both shifts, so we need to look
  845. // at the scan code to tell them apart
  846. if ((keySym == XK_Shift_L) && (keyCode == 0x36))
  847. keySym = XK_Shift_R;
  848. // AltGr handling (see above)
  849. if (win32_has_altgr()) {
  850. if ((keyCode == 0xb8) && (keySym == XK_Alt_R))
  851. keySym = XK_ISO_Level3_Shift;
  852. // Possible start of AltGr sequence?
  853. if ((keyCode == 0x1d) && (keySym == XK_Control_L)) {
  854. self->altGrArmed = true;
  855. self->altGrCtrlTime = msg->time;
  856. Fl::add_timeout(0.1, handleAltGrTimeout, self);
  857. return 1;
  858. }
  859. }
  860. self->handleKeyPress(keyCode, keySym);
  861. // We don't get reliable WM_KEYUP for these
  862. switch (keySym) {
  863. case XK_Zenkaku_Hankaku:
  864. case XK_Eisu_toggle:
  865. case XK_Katakana:
  866. case XK_Hiragana:
  867. case XK_Romaji:
  868. self->handleKeyRelease(keyCode);
  869. }
  870. return 1;
  871. } else if ((msg->message == WM_KEYUP) || (msg->message == WM_SYSKEYUP)) {
  872. UINT vKey;
  873. bool isExtended;
  874. int keyCode;
  875. vKey = msg->wParam;
  876. isExtended = (msg->lParam & (1 << 24)) != 0;
  877. keyCode = ((msg->lParam >> 16) & 0xff);
  878. // Touch keyboard AltGr (see above)
  879. if (!isExtended && (keyCode == 0x00) && (vKey == VK_MENU)) {
  880. isExtended = true;
  881. keyCode = 0x38;
  882. }
  883. // We can't get a release in the middle of an AltGr sequence, so
  884. // abort that detection
  885. if (self->altGrArmed)
  886. self->resolveAltGrDetection(false);
  887. if (keyCode == SCAN_FAKE) {
  888. vlog.debug("Ignoring fake key release (virtual key 0x%02x)", vKey);
  889. return 1;
  890. }
  891. if (keyCode == 0x00)
  892. keyCode = MapVirtualKey(vKey, MAPVK_VK_TO_VSC);
  893. if (isExtended)
  894. keyCode |= 0x80;
  895. if (keyCode == 0x45)
  896. keyCode = 0xc6;
  897. if (keyCode == 0xc5)
  898. keyCode = 0x45;
  899. if (keyCode == 0xb7)
  900. keyCode = 0x54;
  901. self->handleKeyRelease(keyCode);
  902. // Windows has a rather nasty bug where it won't send key release
  903. // events for a Shift button if the other Shift is still pressed
  904. if ((keyCode == 0x2a) || (keyCode == 0x36)) {
  905. if (self->downKeySym.count(0x2a))
  906. self->handleKeyRelease(0x2a);
  907. if (self->downKeySym.count(0x36))
  908. self->handleKeyRelease(0x36);
  909. }
  910. return 1;
  911. }
  912. #elif defined(__APPLE__)
  913. if (cocoa_is_keyboard_event(event)) {
  914. int keyCode;
  915. keyCode = cocoa_event_keycode(event);
  916. if ((unsigned)keyCode >= code_map_osx_to_qnum_len)
  917. keyCode = 0;
  918. else
  919. keyCode = code_map_osx_to_qnum[keyCode];
  920. if (cocoa_is_key_press(event)) {
  921. rdr::U32 keySym;
  922. keySym = cocoa_event_keysym(event);
  923. if (keySym == NoSymbol) {
  924. vlog.error(_("No symbol for key code 0x%02x (in the current state)"),
  925. (int)keyCode);
  926. }
  927. self->handleKeyPress(keyCode, keySym);
  928. // We don't get any release events for CapsLock, so we have to
  929. // send the release right away.
  930. if (keySym == XK_Caps_Lock)
  931. self->handleKeyRelease(keyCode);
  932. } else {
  933. self->handleKeyRelease(keyCode);
  934. }
  935. return 1;
  936. }
  937. #else
  938. XEvent *xevent = (XEvent*)event;
  939. if (xevent->type == KeyPress) {
  940. int keycode;
  941. char str;
  942. KeySym keysym;
  943. keycode = code_map_keycode_to_qnum[xevent->xkey.keycode];
  944. // Generate a fake keycode just for tracking if we can't figure
  945. // out the proper one
  946. if (keycode == 0)
  947. keycode = 0x100 | xevent->xkey.keycode;
  948. XLookupString(&xevent->xkey, &str, 1, &keysym, NULL);
  949. if (keysym == NoSymbol) {
  950. vlog.error(_("No symbol for key code %d (in the current state)"),
  951. (int)xevent->xkey.keycode);
  952. }
  953. switch (keysym) {
  954. // For the first few years, there wasn't a good consensus on what the
  955. // Windows keys should be mapped to for X11. So we need to help out a
  956. // bit and map all variants to the same key...
  957. case XK_Hyper_L:
  958. keysym = XK_Super_L;
  959. break;
  960. case XK_Hyper_R:
  961. keysym = XK_Super_R;
  962. break;
  963. // There has been several variants for Shift-Tab over the years.
  964. // RFB states that we should always send a normal tab.
  965. case XK_ISO_Left_Tab:
  966. keysym = XK_Tab;
  967. break;
  968. }
  969. self->handleKeyPress(keycode, keysym);
  970. return 1;
  971. } else if (xevent->type == KeyRelease) {
  972. int keycode = code_map_keycode_to_qnum[xevent->xkey.keycode];
  973. if (keycode == 0)
  974. keycode = 0x100 | xevent->xkey.keycode;
  975. self->handleKeyRelease(keycode);
  976. return 1;
  977. }
  978. #endif
  979. return 0;
  980. }
  981. #ifdef WIN32
  982. void Viewport::handleAltGrTimeout(void *data)
  983. {
  984. Viewport *self = (Viewport *)data;
  985. assert(self);
  986. self->altGrArmed = false;
  987. self->handleKeyPress(0x1d, XK_Control_L);
  988. }
  989. void Viewport::resolveAltGrDetection(bool isAltGrSequence)
  990. {
  991. altGrArmed = false;
  992. Fl::remove_timeout(handleAltGrTimeout);
  993. // when it's not an AltGr sequence we can't supress the Ctrl anymore
  994. if (!isAltGrSequence)
  995. handleKeyPress(0x1d, XK_Control_L);
  996. }
  997. #endif
  998. void Viewport::initContextMenu()
  999. {
  1000. contextMenu->clear();
  1001. fltk_menu_add(contextMenu, p_("ContextMenu|", "E&xit viewer"),
  1002. 0, NULL, (void*)ID_EXIT, FL_MENU_DIVIDER);
  1003. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Full screen"),
  1004. 0, NULL, (void*)ID_FULLSCREEN,
  1005. FL_MENU_TOGGLE | (window()->fullscreen_active()?FL_MENU_VALUE:0));
  1006. fltk_menu_add(contextMenu, p_("ContextMenu|", "Minimi&ze"),
  1007. 0, NULL, (void*)ID_MINIMIZE, 0);
  1008. fltk_menu_add(contextMenu, p_("ContextMenu|", "Resize &window to session"),
  1009. 0, NULL, (void*)ID_RESIZE,
  1010. (window()->fullscreen_active()?FL_MENU_INACTIVE:0) |
  1011. FL_MENU_DIVIDER);
  1012. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Ctrl"),
  1013. 0, NULL, (void*)ID_CTRL,
  1014. FL_MENU_TOGGLE | (menuCtrlKey?FL_MENU_VALUE:0));
  1015. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Alt"),
  1016. 0, NULL, (void*)ID_ALT,
  1017. FL_MENU_TOGGLE | (menuAltKey?FL_MENU_VALUE:0));
  1018. if (menuKeySym) {
  1019. char sendMenuKey[64];
  1020. snprintf(sendMenuKey, 64, p_("ContextMenu|", "Send %s"), (const char *)menuKey);
  1021. fltk_menu_add(contextMenu, sendMenuKey, 0, NULL, (void*)ID_MENUKEY, 0);
  1022. fltk_menu_add(contextMenu, "Secret shortcut menu key", menuKeyFLTK, NULL,
  1023. (void*)ID_MENUKEY, FL_MENU_INVISIBLE);
  1024. }
  1025. fltk_menu_add(contextMenu, p_("ContextMenu|", "Send Ctrl-Alt-&Del"),
  1026. 0, NULL, (void*)ID_CTRLALTDEL, FL_MENU_DIVIDER);
  1027. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Refresh screen"),
  1028. 0, NULL, (void*)ID_REFRESH, FL_MENU_DIVIDER);
  1029. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Options..."),
  1030. 0, NULL, (void*)ID_OPTIONS, 0);
  1031. fltk_menu_add(contextMenu, p_("ContextMenu|", "Connection &info..."),
  1032. 0, NULL, (void*)ID_INFO, 0);
  1033. fltk_menu_add(contextMenu, p_("ContextMenu|", "About &TigerVNC viewer..."),
  1034. 0, NULL, (void*)ID_ABOUT, 0);
  1035. }
  1036. void Viewport::popupContextMenu()
  1037. {
  1038. const Fl_Menu_Item *m;
  1039. char buffer[1024];
  1040. // Make sure the menu is reset to its initial state between goes or
  1041. // it will start up highlighting the previously selected entry.
  1042. contextMenu->value(-1);
  1043. // initialize context menu before display
  1044. initContextMenu();
  1045. // Unfortunately FLTK doesn't reliably restore the mouse pointer for
  1046. // menus, so we have to help it out.
  1047. if (Fl::belowmouse() == this)
  1048. window()->cursor(FL_CURSOR_DEFAULT);
  1049. // FLTK also doesn't switch focus properly for menus
  1050. handle(FL_UNFOCUS);
  1051. m = contextMenu->popup();
  1052. handle(FL_FOCUS);
  1053. // Back to our proper mouse pointer.
  1054. if (Fl::belowmouse())
  1055. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  1056. if (m == NULL)
  1057. return;
  1058. switch (m->argument()) {
  1059. case ID_EXIT:
  1060. exit_vncviewer();
  1061. break;
  1062. case ID_FULLSCREEN:
  1063. if (window()->fullscreen_active())
  1064. window()->fullscreen_off();
  1065. else
  1066. ((DesktopWindow*)window())->fullscreen_on();
  1067. break;
  1068. case ID_MINIMIZE:
  1069. window()->iconize();
  1070. break;
  1071. case ID_RESIZE:
  1072. if (window()->fullscreen_active())
  1073. break;
  1074. window()->size(w(), h());
  1075. break;
  1076. case ID_CTRL:
  1077. if (m->value())
  1078. handleKeyPress(0x1d, XK_Control_L);
  1079. else
  1080. handleKeyRelease(0x1d);
  1081. menuCtrlKey = !menuCtrlKey;
  1082. break;
  1083. case ID_ALT:
  1084. if (m->value())
  1085. handleKeyPress(0x38, XK_Alt_L);
  1086. else
  1087. handleKeyRelease(0x38);
  1088. menuAltKey = !menuAltKey;
  1089. break;
  1090. case ID_MENUKEY:
  1091. handleKeyPress(menuKeyCode, menuKeySym);
  1092. handleKeyRelease(menuKeyCode);
  1093. break;
  1094. case ID_CTRLALTDEL:
  1095. handleKeyPress(0x1d, XK_Control_L);
  1096. handleKeyPress(0x38, XK_Alt_L);
  1097. handleKeyPress(0xd3, XK_Delete);
  1098. handleKeyRelease(0xd3);
  1099. handleKeyRelease(0x38);
  1100. handleKeyRelease(0x1d);
  1101. break;
  1102. case ID_REFRESH:
  1103. cc->refreshFramebuffer();
  1104. break;
  1105. case ID_OPTIONS:
  1106. OptionsDialog::showDialog();
  1107. break;
  1108. case ID_INFO:
  1109. if (fltk_escape(cc->connectionInfo(), buffer, sizeof(buffer)) < sizeof(buffer)) {
  1110. fl_message_title(_("VNC connection info"));
  1111. fl_message("%s", buffer);
  1112. }
  1113. break;
  1114. case ID_ABOUT:
  1115. about_vncviewer();
  1116. break;
  1117. }
  1118. }
  1119. void Viewport::setMenuKey()
  1120. {
  1121. getMenuKey(&menuKeyFLTK, &menuKeyCode, &menuKeySym);
  1122. }
  1123. void Viewport::handleOptions(void *data)
  1124. {
  1125. Viewport *self = (Viewport*)data;
  1126. self->setMenuKey();
  1127. // FIXME: Need to recheck cursor for dotWhenNoCursor
  1128. }