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

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