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

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