您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Viewport.cxx 30KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2014 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <rfb/CMsgWriter.h>
  26. #include <rfb/LogWriter.h>
  27. #include <rfb/Exception.h>
  28. #include <rfb/ledStates.h>
  29. // FLTK can pull in the X11 headers on some systems
  30. #ifndef XK_VoidSymbol
  31. #define XK_LATIN1
  32. #define XK_MISCELLANY
  33. #define XK_XKB_KEYS
  34. #include <rfb/keysymdef.h>
  35. #endif
  36. #ifndef XF86XK_ModeLock
  37. #include <rfb/XF86keysym.h>
  38. #endif
  39. #if ! (defined(WIN32) || defined(__APPLE__))
  40. #include <X11/XKBlib.h>
  41. #endif
  42. #ifndef NoSymbol
  43. #define NoSymbol 0
  44. #endif
  45. // Missing in at least some versions of MinGW
  46. #ifndef MAPVK_VK_TO_VSC
  47. #define MAPVK_VK_TO_VSC 0
  48. #endif
  49. #include "Viewport.h"
  50. #include "CConn.h"
  51. #include "OptionsDialog.h"
  52. #include "DesktopWindow.h"
  53. #include "i18n.h"
  54. #include "fltk_layout.h"
  55. #include "parameters.h"
  56. #include "keysym2ucs.h"
  57. #include "menukey.h"
  58. #include "vncviewer.h"
  59. #include "PlatformPixelBuffer.h"
  60. #include <FL/fl_draw.H>
  61. #include <FL/fl_ask.H>
  62. #include <FL/Fl_Menu.H>
  63. #include <FL/Fl_Menu_Button.H>
  64. #if !defined(WIN32) && !defined(__APPLE__)
  65. #include <X11/XKBlib.h>
  66. extern const struct _code_map_xkb_to_qnum {
  67. const char * from;
  68. const unsigned short to;
  69. } code_map_xkb_to_qnum[];
  70. extern const unsigned int code_map_xkb_to_qnum_len;
  71. static int code_map_keycode_to_qnum[256];
  72. #endif
  73. #ifdef __APPLE__
  74. #include "cocoa.h"
  75. extern const unsigned short code_map_osx_to_qnum[];
  76. extern const unsigned int code_map_osx_to_qnum_len;
  77. #endif
  78. #ifdef WIN32
  79. #include "win32.h"
  80. #endif
  81. using namespace rfb;
  82. using namespace rdr;
  83. static rfb::LogWriter vlog("Viewport");
  84. // Menu constants
  85. enum { ID_EXIT, ID_FULLSCREEN, ID_MINIMIZE, ID_RESIZE,
  86. ID_CTRL, ID_ALT, ID_MENUKEY, ID_CTRLALTDEL,
  87. ID_REFRESH, ID_OPTIONS, ID_INFO, ID_ABOUT, ID_DISMISS };
  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. menuCtrlKey(false), menuAltKey(false), cursor(NULL)
  96. {
  97. #if !defined(WIN32) && !defined(__APPLE__)
  98. XkbDescPtr xkb;
  99. Status status;
  100. xkb = XkbGetMap(fl_display, 0, XkbUseCoreKbd);
  101. if (!xkb)
  102. throw rfb::Exception("XkbGetMap");
  103. status = XkbGetNames(fl_display, XkbKeyNamesMask, xkb);
  104. if (status != Success)
  105. throw rfb::Exception("XkbGetNames");
  106. memset(code_map_keycode_to_qnum, 0, sizeof(code_map_keycode_to_qnum));
  107. for (KeyCode keycode = xkb->min_key_code;
  108. keycode < xkb->max_key_code;
  109. keycode++) {
  110. const char *keyname = xkb->names->keys[keycode].name;
  111. unsigned short rfbcode;
  112. if (keyname[0] == '\0')
  113. continue;
  114. rfbcode = 0;
  115. for (unsigned i = 0;i < code_map_xkb_to_qnum_len;i++) {
  116. if (strncmp(code_map_xkb_to_qnum[i].from,
  117. keyname, XkbKeyNameLength) == 0) {
  118. rfbcode = code_map_xkb_to_qnum[i].to;
  119. break;
  120. }
  121. }
  122. if (rfbcode != 0)
  123. code_map_keycode_to_qnum[keycode] = rfbcode;
  124. else
  125. vlog.debug("No key mapping for key %.4s", keyname);
  126. }
  127. XkbFreeKeyboard(xkb, 0, True);
  128. #endif
  129. Fl::add_clipboard_notify(handleClipboardChange, this);
  130. // We need to intercept keyboard events early
  131. Fl::add_system_handler(handleSystemEvent, this);
  132. frameBuffer = new PlatformPixelBuffer(w, h);
  133. assert(frameBuffer);
  134. cc->setFramebuffer(frameBuffer);
  135. contextMenu = new Fl_Menu_Button(0, 0, 0, 0);
  136. // Setting box type to FL_NO_BOX prevents it from trying to draw the
  137. // button component (which we don't want)
  138. contextMenu->box(FL_NO_BOX);
  139. // The (invisible) button associated with this widget can mess with
  140. // things like Fl_Scroll so we need to get rid of any parents.
  141. // Unfortunately that's not possible because of STR #2654, but
  142. // reparenting to the current window works for most cases.
  143. window()->add(contextMenu);
  144. setMenuKey();
  145. OptionsDialog::addCallback(handleOptions, this);
  146. // Send a fake pointer event so that the server will stop rendering
  147. // a server-side cursor. Ideally we'd like to send the actual pointer
  148. // position, but we can't really tell when the window manager is done
  149. // placing us so we don't have a good time for that.
  150. handlePointerEvent(Point(w/2, h/2), 0);
  151. }
  152. Viewport::~Viewport()
  153. {
  154. // Unregister all timeouts in case they get a change tro trigger
  155. // again later when this object is already gone.
  156. Fl::remove_timeout(handlePointerTimeout, this);
  157. Fl::remove_system_handler(handleSystemEvent);
  158. Fl::remove_clipboard_notify(handleClipboardChange);
  159. OptionsDialog::removeCallback(handleOptions);
  160. if (cursor) {
  161. if (!cursor->alloc_array)
  162. delete [] cursor->array;
  163. delete cursor;
  164. }
  165. // FLTK automatically deletes all child widgets, so we shouldn't touch
  166. // them ourselves here
  167. }
  168. const rfb::PixelFormat &Viewport::getPreferredPF()
  169. {
  170. return frameBuffer->getPF();
  171. }
  172. // Copy the areas of the framebuffer that have been changed (damaged)
  173. // to the displayed window.
  174. void Viewport::updateWindow()
  175. {
  176. Rect r;
  177. r = frameBuffer->getDamage();
  178. damage(FL_DAMAGE_USER1, r.tl.x + x(), r.tl.y + y(), r.width(), r.height());
  179. }
  180. static const char * dotcursor_xpm[] = {
  181. "5 5 2 1",
  182. ". c #000000",
  183. " c #FFFFFF",
  184. " ",
  185. " ... ",
  186. " ... ",
  187. " ... ",
  188. " "};
  189. void Viewport::setCursor(int width, int height, const Point& hotspot,
  190. const rdr::U8* data)
  191. {
  192. int i;
  193. if (cursor) {
  194. if (!cursor->alloc_array)
  195. delete [] cursor->array;
  196. delete cursor;
  197. }
  198. for (i = 0; i < width*height; i++)
  199. if (data[i*4 + 3] != 0) break;
  200. if ((i == width*height) && dotWhenNoCursor) {
  201. vlog.debug("cursor is empty - using dot");
  202. Fl_Pixmap pxm(dotcursor_xpm);
  203. cursor = new Fl_RGB_Image(&pxm);
  204. cursorHotspot.x = cursorHotspot.y = 2;
  205. } else {
  206. if ((width == 0) || (height == 0)) {
  207. U8 *buffer = new U8[4];
  208. memset(buffer, 0, 4);
  209. cursor = new Fl_RGB_Image(buffer, 1, 1, 4);
  210. cursorHotspot.x = cursorHotspot.y = 0;
  211. } else {
  212. U8 *buffer = new U8[width * height * 4];
  213. memcpy(buffer, data, width * height * 4);
  214. cursor = new Fl_RGB_Image(buffer, width, height, 4);
  215. cursorHotspot = hotspot;
  216. }
  217. }
  218. if (Fl::belowmouse() == this)
  219. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  220. }
  221. void Viewport::setLEDState(unsigned int state)
  222. {
  223. Fl_Widget *focus;
  224. vlog.debug("Got server LED state: 0x%08x", state);
  225. focus = Fl::grab();
  226. if (!focus)
  227. focus = Fl::focus();
  228. if (!focus)
  229. return;
  230. if (focus != this)
  231. return;
  232. #if defined(WIN32)
  233. INPUT input[6];
  234. UINT count;
  235. UINT ret;
  236. memset(input, 0, sizeof(input));
  237. count = 0;
  238. if (!!(state & ledCapsLock) != !!(GetKeyState(VK_CAPITAL) & 0x1)) {
  239. input[count].type = input[count+1].type = INPUT_KEYBOARD;
  240. input[count].ki.wVk = input[count+1].ki.wVk = VK_CAPITAL;
  241. input[count].ki.wScan = input[count+1].ki.wScan = SCAN_FAKE;
  242. input[count].ki.dwFlags = 0;
  243. input[count+1].ki.dwFlags = KEYEVENTF_KEYUP;
  244. count += 2;
  245. }
  246. if (!!(state & ledNumLock) != !!(GetKeyState(VK_NUMLOCK) & 0x1)) {
  247. input[count].type = input[count+1].type = INPUT_KEYBOARD;
  248. input[count].ki.wVk = input[count+1].ki.wVk = VK_NUMLOCK;
  249. input[count].ki.wScan = input[count+1].ki.wScan = SCAN_FAKE;
  250. input[count].ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
  251. input[count+1].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY;
  252. count += 2;
  253. }
  254. if (!!(state & ledScrollLock) != !!(GetKeyState(VK_SCROLL) & 0x1)) {
  255. input[count].type = input[count+1].type = INPUT_KEYBOARD;
  256. input[count].ki.wVk = input[count+1].ki.wVk = VK_SCROLL;
  257. input[count].ki.wScan = input[count+1].ki.wScan = SCAN_FAKE;
  258. input[count].ki.dwFlags = 0;
  259. input[count+1].ki.dwFlags = KEYEVENTF_KEYUP;
  260. count += 2;
  261. }
  262. if (count == 0)
  263. return;
  264. ret = SendInput(count, input, sizeof(*input));
  265. if (ret < count)
  266. vlog.error(_("Failed to update keyboard LED state: %lu"), GetLastError());
  267. #elif defined(__APPLE__)
  268. int ret;
  269. ret = cocoa_set_caps_lock_state(state & ledCapsLock);
  270. if (ret != 0) {
  271. vlog.error(_("Failed to update keyboard LED state: %d"), ret);
  272. return;
  273. }
  274. ret = cocoa_set_num_lock_state(state & ledNumLock);
  275. if (ret != 0) {
  276. vlog.error(_("Failed to update keyboard LED state: %d"), ret);
  277. return;
  278. }
  279. // No support for Scroll Lock //
  280. #else
  281. unsigned int affect, values;
  282. unsigned int mask;
  283. Bool ret;
  284. affect = values = 0;
  285. affect |= LockMask;
  286. if (state & ledCapsLock)
  287. values |= LockMask;
  288. mask = getModifierMask(XK_Num_Lock);
  289. affect |= mask;
  290. if (state & ledNumLock)
  291. values |= mask;
  292. mask = getModifierMask(XK_Scroll_Lock);
  293. affect |= mask;
  294. if (state & ledScrollLock)
  295. values |= mask;
  296. ret = XkbLockModifiers(fl_display, XkbUseCoreKbd, affect, values);
  297. if (!ret)
  298. vlog.error(_("Failed to update keyboard LED state"));
  299. #endif
  300. }
  301. void Viewport::pushLEDState()
  302. {
  303. unsigned int state;
  304. // Server support?
  305. if (cc->cp.ledState() == ledUnknown)
  306. return;
  307. state = 0;
  308. #if defined(WIN32)
  309. if (GetKeyState(VK_CAPITAL) & 0x1)
  310. state |= ledCapsLock;
  311. if (GetKeyState(VK_NUMLOCK) & 0x1)
  312. state |= ledNumLock;
  313. if (GetKeyState(VK_SCROLL) & 0x1)
  314. state |= ledScrollLock;
  315. #elif defined(__APPLE__)
  316. int ret;
  317. bool on;
  318. ret = cocoa_get_caps_lock_state(&on);
  319. if (ret != 0) {
  320. vlog.error(_("Failed to get keyboard LED state: %d"), ret);
  321. return;
  322. }
  323. if (on)
  324. state |= ledCapsLock;
  325. ret = cocoa_get_num_lock_state(&on);
  326. if (ret != 0) {
  327. vlog.error(_("Failed to get keyboard LED state: %d"), ret);
  328. return;
  329. }
  330. if (on)
  331. state |= ledNumLock;
  332. // No support for Scroll Lock //
  333. state |= (cc->cp.ledState() & ledScrollLock);
  334. #else
  335. unsigned int mask;
  336. Status status;
  337. XkbStateRec xkbState;
  338. status = XkbGetState(fl_display, XkbUseCoreKbd, &xkbState);
  339. if (status != Success) {
  340. vlog.error(_("Failed to get keyboard LED state: %d"), status);
  341. return;
  342. }
  343. if (xkbState.locked_mods & LockMask)
  344. state |= ledCapsLock;
  345. mask = getModifierMask(XK_Num_Lock);
  346. if (xkbState.locked_mods & mask)
  347. state |= ledNumLock;
  348. mask = getModifierMask(XK_Scroll_Lock);
  349. if (xkbState.locked_mods & mask)
  350. state |= ledScrollLock;
  351. #endif
  352. if ((state & ledCapsLock) != (cc->cp.ledState() & ledCapsLock)) {
  353. vlog.debug("Inserting fake CapsLock to get in sync with server");
  354. handleKeyPress(0x3a, XK_Caps_Lock);
  355. handleKeyRelease(0x3a);
  356. }
  357. if ((state & ledNumLock) != (cc->cp.ledState() & ledNumLock)) {
  358. vlog.debug("Inserting fake NumLock to get in sync with server");
  359. handleKeyPress(0x45, XK_Num_Lock);
  360. handleKeyRelease(0x45);
  361. }
  362. if ((state & ledScrollLock) != (cc->cp.ledState() & ledScrollLock)) {
  363. vlog.debug("Inserting fake ScrollLock to get in sync with server");
  364. handleKeyPress(0x46, XK_Scroll_Lock);
  365. handleKeyRelease(0x46);
  366. }
  367. }
  368. void Viewport::draw(Surface* dst)
  369. {
  370. int X, Y, W, H;
  371. // Check what actually needs updating
  372. fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
  373. if ((W == 0) || (H == 0))
  374. return;
  375. frameBuffer->draw(dst, X - x(), Y - y(), X, Y, W, H);
  376. }
  377. void Viewport::draw()
  378. {
  379. int X, Y, W, H;
  380. // Check what actually needs updating
  381. fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
  382. if ((W == 0) || (H == 0))
  383. return;
  384. frameBuffer->draw(X - x(), Y - y(), X, Y, W, H);
  385. }
  386. void Viewport::resize(int x, int y, int w, int h)
  387. {
  388. if ((w != frameBuffer->width()) || (h != frameBuffer->height())) {
  389. vlog.debug("Resizing framebuffer from %dx%d to %dx%d",
  390. frameBuffer->width(), frameBuffer->height(), w, h);
  391. frameBuffer = new PlatformPixelBuffer(w, h);
  392. assert(frameBuffer);
  393. cc->setFramebuffer(frameBuffer);
  394. }
  395. Fl_Widget::resize(x, y, w, h);
  396. }
  397. int Viewport::handle(int event)
  398. {
  399. char *buffer;
  400. int ret;
  401. int buttonMask, wheelMask;
  402. DownMap::const_iterator iter;
  403. switch (event) {
  404. case FL_PASTE:
  405. buffer = new char[Fl::event_length() + 1];
  406. // This is documented as to ASCII, but actually does to 8859-1
  407. ret = fl_utf8toa(Fl::event_text(), Fl::event_length(), buffer,
  408. Fl::event_length() + 1);
  409. assert(ret < (Fl::event_length() + 1));
  410. vlog.debug("Sending clipboard data (%d bytes)", (int)strlen(buffer));
  411. try {
  412. cc->writer()->clientCutText(buffer, ret);
  413. } catch (rdr::Exception& e) {
  414. vlog.error("%s", e.str());
  415. exit_vncviewer(e.str());
  416. }
  417. delete [] buffer;
  418. return 1;
  419. case FL_ENTER:
  420. if (cursor)
  421. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  422. // Yes, we would like some pointer events please!
  423. return 1;
  424. case FL_LEAVE:
  425. window()->cursor(FL_CURSOR_DEFAULT);
  426. // Fall through as we want a last move event to help trigger edge stuff
  427. case FL_PUSH:
  428. case FL_RELEASE:
  429. case FL_DRAG:
  430. case FL_MOVE:
  431. case FL_MOUSEWHEEL:
  432. buttonMask = 0;
  433. if (Fl::event_button1())
  434. buttonMask |= 1;
  435. if (Fl::event_button2())
  436. buttonMask |= 2;
  437. if (Fl::event_button3())
  438. buttonMask |= 4;
  439. if (event == FL_MOUSEWHEEL) {
  440. wheelMask = 0;
  441. if (Fl::event_dy() < 0)
  442. wheelMask |= 8;
  443. if (Fl::event_dy() > 0)
  444. wheelMask |= 16;
  445. if (Fl::event_dx() < 0)
  446. wheelMask |= 32;
  447. if (Fl::event_dx() > 0)
  448. wheelMask |= 64;
  449. // A quick press of the wheel "button", followed by a immediate
  450. // release below
  451. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()),
  452. buttonMask | wheelMask);
  453. }
  454. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), buttonMask);
  455. return 1;
  456. case FL_FOCUS:
  457. Fl::disable_im();
  458. // Yes, we would like some focus please!
  459. return 1;
  460. case FL_UNFOCUS:
  461. // Release all keys that were pressed as that generally makes most
  462. // sense (e.g. Alt+Tab where we only see the Alt press)
  463. while (!downKeySym.empty())
  464. handleKeyRelease(downKeySym.begin()->first);
  465. Fl::enable_im();
  466. return 1;
  467. case FL_KEYDOWN:
  468. case FL_KEYUP:
  469. // Just ignore these as keys were handled in the event handler
  470. return 1;
  471. }
  472. return Fl_Widget::handle(event);
  473. }
  474. #if ! (defined(WIN32) || defined(__APPLE__))
  475. unsigned int Viewport::getModifierMask(unsigned int keysym)
  476. {
  477. XkbDescPtr xkb;
  478. unsigned int mask, keycode;
  479. XkbAction *act;
  480. mask = 0;
  481. xkb = XkbGetMap(fl_display, XkbAllComponentsMask, XkbUseCoreKbd);
  482. if (xkb == NULL)
  483. return 0;
  484. for (keycode = xkb->min_key_code; keycode <= xkb->max_key_code; keycode++) {
  485. unsigned int state_out;
  486. KeySym ks;
  487. XkbTranslateKeyCode(xkb, keycode, 0, &state_out, &ks);
  488. if (ks == NoSymbol)
  489. continue;
  490. if (ks == keysym)
  491. break;
  492. }
  493. // KeySym not mapped?
  494. if (keycode > xkb->max_key_code)
  495. goto out;
  496. act = XkbKeyAction(xkb, keycode, 0);
  497. if (act == NULL)
  498. goto out;
  499. if (act->type != XkbSA_LockMods)
  500. goto out;
  501. if (act->mods.flags & XkbSA_UseModMapMods)
  502. mask = xkb->map->modmap[keycode];
  503. else
  504. mask = act->mods.mask;
  505. out:
  506. XkbFreeKeyboard(xkb, XkbAllComponentsMask, True);
  507. return mask;
  508. }
  509. #endif
  510. void Viewport::handleClipboardChange(int source, void *data)
  511. {
  512. Viewport *self = (Viewport *)data;
  513. assert(self);
  514. if (!sendClipboard)
  515. return;
  516. #if !defined(WIN32) && !defined(__APPLE__)
  517. if (!sendPrimary && (source == 0))
  518. return;
  519. #endif
  520. Fl::paste(*self, source);
  521. }
  522. void Viewport::handlePointerEvent(const rfb::Point& pos, int buttonMask)
  523. {
  524. if (!viewOnly) {
  525. if (pointerEventInterval == 0 || buttonMask != lastButtonMask) {
  526. try {
  527. cc->writer()->pointerEvent(pos, buttonMask);
  528. } catch (rdr::Exception& e) {
  529. vlog.error("%s", e.str());
  530. exit_vncviewer(e.str());
  531. }
  532. } else {
  533. if (!Fl::has_timeout(handlePointerTimeout, this))
  534. Fl::add_timeout((double)pointerEventInterval/1000.0,
  535. handlePointerTimeout, this);
  536. }
  537. lastPointerPos = pos;
  538. lastButtonMask = buttonMask;
  539. }
  540. }
  541. void Viewport::handlePointerTimeout(void *data)
  542. {
  543. Viewport *self = (Viewport *)data;
  544. assert(self);
  545. try {
  546. self->cc->writer()->pointerEvent(self->lastPointerPos, self->lastButtonMask);
  547. } catch (rdr::Exception& e) {
  548. vlog.error("%s", e.str());
  549. exit_vncviewer(e.str());
  550. }
  551. }
  552. void Viewport::handleKeyPress(int keyCode, rdr::U32 keySym)
  553. {
  554. static bool menuRecursion = false;
  555. // Prevent recursion if the menu wants to send its own
  556. // activation key.
  557. if (menuKeySym && (keySym == menuKeySym) && !menuRecursion) {
  558. menuRecursion = true;
  559. popupContextMenu();
  560. menuRecursion = false;
  561. return;
  562. }
  563. if (viewOnly)
  564. return;
  565. if (keyCode == 0) {
  566. vlog.error(_("No key code specified on key press"));
  567. return;
  568. }
  569. #ifdef __APPLE__
  570. // Alt on OS X behaves more like AltGr on other systems, and to get
  571. // sane behaviour we should translate things in that manner for the
  572. // remote VNC server. However that means we lose the ability to use
  573. // Alt as a shortcut modifier. Do what RealVNC does and hijack the
  574. // left command key as an Alt replacement.
  575. switch (keySym) {
  576. case XK_Super_L:
  577. keySym = XK_Alt_L;
  578. break;
  579. case XK_Super_R:
  580. keySym = XK_Super_L;
  581. break;
  582. case XK_Alt_L:
  583. keySym = XK_Mode_switch;
  584. break;
  585. case XK_Alt_R:
  586. keySym = XK_ISO_Level3_Shift;
  587. break;
  588. }
  589. #endif
  590. #ifdef WIN32
  591. // Ugly hack alert!
  592. //
  593. // Windows doesn't have a proper AltGr, but handles it using fake
  594. // Ctrl+Alt. Unfortunately X11 doesn't generally like the combination
  595. // Ctrl+Alt+AltGr, which we usually end up with when Xvnc tries to
  596. // get everything in the correct state. Cheat and temporarily release
  597. // Ctrl and Alt when we send some other symbol.
  598. if (downKeySym.count(0x1d) && downKeySym.count(0xb8)) {
  599. vlog.debug("Faking release of AltGr (Ctrl_L+Alt_R)");
  600. try {
  601. cc->writer()->keyEvent(downKeySym[0x1d], 0x1d, false);
  602. cc->writer()->keyEvent(downKeySym[0xb8], 0xb8, false);
  603. } catch (rdr::Exception& e) {
  604. vlog.error("%s", e.str());
  605. exit_vncviewer(e.str());
  606. }
  607. }
  608. #endif
  609. // Because of the way keyboards work, we cannot expect to have the same
  610. // symbol on release as when pressed. This breaks the VNC protocol however,
  611. // so we need to keep track of what keysym a key _code_ generated on press
  612. // and send the same on release.
  613. downKeySym[keyCode] = keySym;
  614. #if defined(WIN32) || defined(__APPLE__)
  615. vlog.debug("Key pressed: 0x%04x => 0x%04x", keyCode, keySym);
  616. #else
  617. vlog.debug("Key pressed: 0x%04x => XK_%s (0x%04x)",
  618. keyCode, XKeysymToString(keySym), keySym);
  619. #endif
  620. try {
  621. // Fake keycode?
  622. if (keyCode > 0xff)
  623. cc->writer()->keyEvent(keySym, 0, true);
  624. else
  625. cc->writer()->keyEvent(keySym, keyCode, true);
  626. } catch (rdr::Exception& e) {
  627. vlog.error("%s", e.str());
  628. exit_vncviewer(e.str());
  629. }
  630. #ifdef WIN32
  631. // Ugly hack continued...
  632. if (downKeySym.count(0x1d) && downKeySym.count(0xb8)) {
  633. vlog.debug("Restoring AltGr state");
  634. try {
  635. cc->writer()->keyEvent(downKeySym[0x1d], 0x1d, true);
  636. cc->writer()->keyEvent(downKeySym[0xb8], 0xb8, true);
  637. } catch (rdr::Exception& e) {
  638. vlog.error("%s", e.str());
  639. exit_vncviewer(e.str());
  640. }
  641. }
  642. #endif
  643. }
  644. void Viewport::handleKeyRelease(int keyCode)
  645. {
  646. DownMap::iterator iter;
  647. if (viewOnly)
  648. return;
  649. iter = downKeySym.find(keyCode);
  650. if (iter == downKeySym.end()) {
  651. // These occur somewhat frequently so let's not spam them unless
  652. // logging is turned up.
  653. vlog.debug("Unexpected release of key code %d", keyCode);
  654. return;
  655. }
  656. #if defined(WIN32) || defined(__APPLE__)
  657. vlog.debug("Key released: 0x%04x => 0x%04x", keyCode, iter->second);
  658. #else
  659. vlog.debug("Key released: 0x%04x => XK_%s (0x%04x)",
  660. keyCode, XKeysymToString(iter->second), iter->second);
  661. #endif
  662. try {
  663. if (keyCode > 0xff)
  664. cc->writer()->keyEvent(iter->second, 0, false);
  665. else
  666. cc->writer()->keyEvent(iter->second, keyCode, false);
  667. } catch (rdr::Exception& e) {
  668. vlog.error("%s", e.str());
  669. exit_vncviewer(e.str());
  670. }
  671. downKeySym.erase(iter);
  672. }
  673. int Viewport::handleSystemEvent(void *event, void *data)
  674. {
  675. Viewport *self = (Viewport *)data;
  676. Fl_Widget *focus;
  677. assert(self);
  678. focus = Fl::grab();
  679. if (!focus)
  680. focus = Fl::focus();
  681. if (!focus)
  682. return 0;
  683. if (focus != self)
  684. return 0;
  685. assert(event);
  686. #if defined(WIN32)
  687. MSG *msg = (MSG*)event;
  688. if ((msg->message == WM_KEYDOWN) || (msg->message == WM_SYSKEYDOWN)) {
  689. UINT vKey;
  690. bool isExtended;
  691. int keyCode;
  692. rdr::U32 keySym;
  693. vKey = msg->wParam;
  694. isExtended = (msg->lParam & (1 << 24)) != 0;
  695. keyCode = ((msg->lParam >> 16) & 0xff);
  696. if (keyCode == SCAN_FAKE) {
  697. vlog.debug("Ignoring fake key press (virtual key 0x%02x)", vKey);
  698. return 1;
  699. }
  700. // Windows sets the scan code to 0x00 for multimedia keys, so we
  701. // have to do a reverse lookup based on the vKey.
  702. if (keyCode == 0x00) {
  703. keyCode = MapVirtualKey(vKey, MAPVK_VK_TO_VSC);
  704. if (keyCode == 0x00) {
  705. if (isExtended)
  706. vlog.error(_("No scan code for extended virtual key 0x%02x"), (int)vKey);
  707. else
  708. vlog.error(_("No scan code for virtual key 0x%02x"), (int)vKey);
  709. return 1;
  710. }
  711. }
  712. if (keyCode & ~0x7f) {
  713. vlog.error(_("Invalid scan code 0x%02x"), (int)keyCode);
  714. return 1;
  715. }
  716. if (isExtended)
  717. keyCode |= 0x80;
  718. // VK_SNAPSHOT sends different scan codes depending on the state of
  719. // Alt. This means that we can get different scan codes on press and
  720. // release. Force it to be something standard.
  721. if (vKey == VK_SNAPSHOT)
  722. keyCode = 0x54;
  723. keySym = win32_vkey_to_keysym(vKey, isExtended);
  724. if (keySym == NoSymbol) {
  725. if (isExtended)
  726. vlog.error(_("No symbol for extended virtual key 0x%02x"), (int)vKey);
  727. else
  728. vlog.error(_("No symbol for virtual key 0x%02x"), (int)vKey);
  729. }
  730. // Fortunately RFB and Windows use the same scan code set,
  731. // so there is no conversion needed
  732. // (as long as we encode the extended keys with the high bit)
  733. self->handleKeyPress(keyCode, keySym);
  734. return 1;
  735. } else if ((msg->message == WM_KEYUP) || (msg->message == WM_SYSKEYUP)) {
  736. UINT vKey;
  737. bool isExtended;
  738. int keyCode;
  739. vKey = msg->wParam;
  740. isExtended = (msg->lParam & (1 << 24)) != 0;
  741. keyCode = ((msg->lParam >> 16) & 0xff);
  742. if (keyCode == SCAN_FAKE) {
  743. vlog.debug("Ignoring fake key release (virtual key 0x%02x)", vKey);
  744. return 1;
  745. }
  746. if (keyCode == 0x00)
  747. keyCode = MapVirtualKey(vKey, MAPVK_VK_TO_VSC);
  748. if (isExtended)
  749. keyCode |= 0x80;
  750. if (vKey == VK_SNAPSHOT)
  751. keyCode = 0x54;
  752. self->handleKeyRelease(keyCode);
  753. return 1;
  754. }
  755. #elif defined(__APPLE__)
  756. if (cocoa_is_keyboard_event(event)) {
  757. int keyCode;
  758. keyCode = cocoa_event_keycode(event);
  759. if ((unsigned)keyCode >= code_map_osx_to_qnum_len)
  760. keyCode = 0;
  761. else
  762. keyCode = code_map_osx_to_qnum[keyCode];
  763. if (cocoa_is_key_press(event)) {
  764. rdr::U32 keySym;
  765. keySym = cocoa_event_keysym(event);
  766. if (keySym == NoSymbol) {
  767. vlog.error(_("No symbol for key code 0x%02x (in the current state)"),
  768. (int)keyCode);
  769. }
  770. self->handleKeyPress(keyCode, keySym);
  771. // We don't get any release events for CapsLock, so we have to
  772. // send the release right away.
  773. if (keySym == XK_Caps_Lock)
  774. self->handleKeyRelease(keyCode);
  775. } else {
  776. self->handleKeyRelease(keyCode);
  777. }
  778. return 1;
  779. }
  780. #else
  781. XEvent *xevent = (XEvent*)event;
  782. if (xevent->type == KeyPress) {
  783. int keycode;
  784. char str;
  785. KeySym keysym;
  786. keycode = code_map_keycode_to_qnum[xevent->xkey.keycode];
  787. // Generate a fake keycode just for tracking if we can't figure
  788. // out the proper one
  789. if (keycode == 0)
  790. keycode = 0x100 | xevent->xkey.keycode;
  791. XLookupString(&xevent->xkey, &str, 1, &keysym, NULL);
  792. if (keysym == NoSymbol) {
  793. vlog.error(_("No symbol for key code %d (in the current state)"),
  794. (int)xevent->xkey.keycode);
  795. }
  796. switch (keysym) {
  797. // For the first few years, there wasn't a good consensus on what the
  798. // Windows keys should be mapped to for X11. So we need to help out a
  799. // bit and map all variants to the same key...
  800. case XK_Hyper_L:
  801. keysym = XK_Super_L;
  802. break;
  803. case XK_Hyper_R:
  804. keysym = XK_Super_R;
  805. break;
  806. // There has been several variants for Shift-Tab over the years.
  807. // RFB states that we should always send a normal tab.
  808. case XK_ISO_Left_Tab:
  809. keysym = XK_Tab;
  810. break;
  811. }
  812. self->handleKeyPress(keycode, keysym);
  813. return 1;
  814. } else if (xevent->type == KeyRelease) {
  815. int keycode = code_map_keycode_to_qnum[xevent->xkey.keycode];
  816. if (keycode == 0)
  817. keycode = 0x100 | xevent->xkey.keycode;
  818. self->handleKeyRelease(keycode);
  819. return 1;
  820. }
  821. #endif
  822. return 0;
  823. }
  824. void Viewport::initContextMenu()
  825. {
  826. contextMenu->clear();
  827. fltk_menu_add(contextMenu, p_("ContextMenu|", "E&xit viewer"),
  828. 0, NULL, (void*)ID_EXIT, FL_MENU_DIVIDER);
  829. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Full screen"),
  830. 0, NULL, (void*)ID_FULLSCREEN,
  831. FL_MENU_TOGGLE | (window()->fullscreen_active()?FL_MENU_VALUE:0));
  832. fltk_menu_add(contextMenu, p_("ContextMenu|", "Minimi&ze"),
  833. 0, NULL, (void*)ID_MINIMIZE, 0);
  834. fltk_menu_add(contextMenu, p_("ContextMenu|", "Resize &window to session"),
  835. 0, NULL, (void*)ID_RESIZE,
  836. (window()->fullscreen_active()?FL_MENU_INACTIVE:0) |
  837. FL_MENU_DIVIDER);
  838. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Ctrl"),
  839. 0, NULL, (void*)ID_CTRL,
  840. FL_MENU_TOGGLE | (menuCtrlKey?FL_MENU_VALUE:0));
  841. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Alt"),
  842. 0, NULL, (void*)ID_ALT,
  843. FL_MENU_TOGGLE | (menuAltKey?FL_MENU_VALUE:0));
  844. if (menuKeySym) {
  845. char sendMenuKey[64];
  846. snprintf(sendMenuKey, 64, p_("ContextMenu|", "Send %s"), (const char *)menuKey);
  847. fltk_menu_add(contextMenu, sendMenuKey, 0, NULL, (void*)ID_MENUKEY, 0);
  848. fltk_menu_add(contextMenu, "Secret shortcut menu key", menuKeyFLTK, NULL,
  849. (void*)ID_MENUKEY, FL_MENU_INVISIBLE);
  850. }
  851. fltk_menu_add(contextMenu, p_("ContextMenu|", "Send Ctrl-Alt-&Del"),
  852. 0, NULL, (void*)ID_CTRLALTDEL, FL_MENU_DIVIDER);
  853. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Refresh screen"),
  854. 0, NULL, (void*)ID_REFRESH, FL_MENU_DIVIDER);
  855. fltk_menu_add(contextMenu, p_("ContextMenu|", "&Options..."),
  856. 0, NULL, (void*)ID_OPTIONS, 0);
  857. fltk_menu_add(contextMenu, p_("ContextMenu|", "Connection &info..."),
  858. 0, NULL, (void*)ID_INFO, 0);
  859. fltk_menu_add(contextMenu, p_("ContextMenu|", "About &TigerVNC viewer..."),
  860. 0, NULL, (void*)ID_ABOUT, FL_MENU_DIVIDER);
  861. fltk_menu_add(contextMenu, p_("ContextMenu|", "Dismiss &menu"),
  862. 0, NULL, (void*)ID_DISMISS, 0);
  863. }
  864. void Viewport::popupContextMenu()
  865. {
  866. const Fl_Menu_Item *m;
  867. char buffer[1024];
  868. // Make sure the menu is reset to its initial state between goes or
  869. // it will start up highlighting the previously selected entry.
  870. contextMenu->value(-1);
  871. // initialize context menu before display
  872. initContextMenu();
  873. // Unfortunately FLTK doesn't reliably restore the mouse pointer for
  874. // menus, so we have to help it out.
  875. if (Fl::belowmouse() == this)
  876. window()->cursor(FL_CURSOR_DEFAULT);
  877. m = contextMenu->popup();
  878. // Back to our proper mouse pointer.
  879. if ((Fl::belowmouse() == this) && cursor)
  880. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  881. if (m == NULL)
  882. return;
  883. switch (m->argument()) {
  884. case ID_EXIT:
  885. exit_vncviewer();
  886. break;
  887. case ID_FULLSCREEN:
  888. if (window()->fullscreen_active())
  889. window()->fullscreen_off();
  890. else
  891. ((DesktopWindow*)window())->fullscreen_on();
  892. break;
  893. case ID_MINIMIZE:
  894. window()->iconize();
  895. break;
  896. case ID_RESIZE:
  897. if (window()->fullscreen_active())
  898. break;
  899. window()->size(w(), h());
  900. break;
  901. case ID_CTRL:
  902. if (m->value())
  903. handleKeyPress(0x1d, XK_Control_L);
  904. else
  905. handleKeyRelease(0x1d);
  906. menuCtrlKey = !menuCtrlKey;
  907. break;
  908. case ID_ALT:
  909. if (m->value())
  910. handleKeyPress(0x38, XK_Alt_L);
  911. else
  912. handleKeyRelease(0x38);
  913. menuAltKey = !menuAltKey;
  914. break;
  915. case ID_MENUKEY:
  916. handleKeyPress(menuKeyCode, menuKeySym);
  917. handleKeyRelease(menuKeyCode);
  918. break;
  919. case ID_CTRLALTDEL:
  920. handleKeyPress(0x1d, XK_Control_L);
  921. handleKeyPress(0x38, XK_Alt_L);
  922. handleKeyPress(0xd3, XK_Delete);
  923. handleKeyRelease(0xd3);
  924. handleKeyRelease(0x38);
  925. handleKeyRelease(0x1d);
  926. break;
  927. case ID_REFRESH:
  928. cc->refreshFramebuffer();
  929. break;
  930. case ID_OPTIONS:
  931. OptionsDialog::showDialog();
  932. break;
  933. case ID_INFO:
  934. if (fltk_escape(cc->connectionInfo(), buffer, sizeof(buffer)) < sizeof(buffer)) {
  935. fl_message_title(_("VNC connection info"));
  936. fl_message("%s", buffer);
  937. }
  938. break;
  939. case ID_ABOUT:
  940. about_vncviewer();
  941. break;
  942. case ID_DISMISS:
  943. // Don't need to do anything
  944. break;
  945. }
  946. }
  947. void Viewport::setMenuKey()
  948. {
  949. getMenuKey(&menuKeyFLTK, &menuKeyCode, &menuKeySym);
  950. }
  951. void Viewport::handleOptions(void *data)
  952. {
  953. Viewport *self = (Viewport*)data;
  954. self->setMenuKey();
  955. }