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

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