You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Viewport.cxx 36KB

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