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 25KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. // FLTK can pull in the X11 headers on some systems
  28. #ifndef XK_VoidSymbol
  29. #define XK_LATIN1
  30. #define XK_MISCELLANY
  31. #define XK_XKB_KEYS
  32. #include <rfb/keysymdef.h>
  33. #endif
  34. #ifndef XF86XK_ModeLock
  35. #include <rfb/XF86keysym.h>
  36. #endif
  37. #ifndef NoSymbol
  38. #define NoSymbol 0
  39. #endif
  40. #include "Viewport.h"
  41. #include "CConn.h"
  42. #include "OptionsDialog.h"
  43. #include "DesktopWindow.h"
  44. #include "i18n.h"
  45. #include "fltk_layout.h"
  46. #include "parameters.h"
  47. #include "keysym2ucs.h"
  48. #include "menukey.h"
  49. #include "vncviewer.h"
  50. #include "PlatformPixelBuffer.h"
  51. #include "FLTKPixelBuffer.h"
  52. #if defined(WIN32)
  53. #include "Win32PixelBuffer.h"
  54. #elif defined(__APPLE__)
  55. #include "OSXPixelBuffer.h"
  56. #else
  57. #include "X11PixelBuffer.h"
  58. #endif
  59. #include <FL/fl_draw.H>
  60. #include <FL/fl_ask.H>
  61. #include <FL/Fl_Menu.H>
  62. #include <FL/Fl_Menu_Button.H>
  63. #ifdef WIN32
  64. #include "win32.h"
  65. #endif
  66. using namespace rfb;
  67. using namespace rdr;
  68. static rfb::LogWriter vlog("Viewport");
  69. // Menu constants
  70. enum { ID_EXIT, ID_FULLSCREEN, ID_RESIZE,
  71. ID_CTRL, ID_ALT, ID_MENUKEY, ID_CTRLALTDEL,
  72. ID_REFRESH, ID_OPTIONS, ID_INFO, ID_ABOUT, ID_DISMISS };
  73. // Fake key presses use this value and above
  74. static const int fakeKeyBase = 0x200;
  75. Viewport::Viewport(int w, int h, const rfb::PixelFormat& serverPF, CConn* cc_)
  76. : Fl_Widget(0, 0, w, h), cc(cc_), frameBuffer(NULL),
  77. lastPointerPos(0, 0), lastButtonMask(0),
  78. cursor(NULL), menuCtrlKey(false), menuAltKey(false)
  79. {
  80. // FLTK STR #2636 gives us the ability to monitor clipboard changes
  81. #ifdef HAVE_FLTK_CLIPBOARD
  82. Fl::add_clipboard_notify(handleClipboardChange, this);
  83. #endif
  84. frameBuffer = createFramebuffer(w, h);
  85. assert(frameBuffer);
  86. contextMenu = new Fl_Menu_Button(0, 0, 0, 0);
  87. // Setting box type to FL_NO_BOX prevents it from trying to draw the
  88. // button component (which we don't want)
  89. contextMenu->box(FL_NO_BOX);
  90. // The (invisible) button associated with this widget can mess with
  91. // things like Fl_Scroll so we need to get rid of any parents.
  92. // Unfortunately that's not possible because of STR #2654, but
  93. // reparenting to the current window works for most cases.
  94. window()->add(contextMenu);
  95. setMenuKey();
  96. OptionsDialog::addCallback(handleOptions, this);
  97. // Send a fake pointer event so that the server will stop rendering
  98. // a server-side cursor. Ideally we'd like to send the actual pointer
  99. // position, but we can't really tell when the window manager is done
  100. // placing us so we don't have a good time for that.
  101. handlePointerEvent(Point(w/2, h/2), 0);
  102. }
  103. Viewport::~Viewport()
  104. {
  105. // Unregister all timeouts in case they get a change tro trigger
  106. // again later when this object is already gone.
  107. Fl::remove_timeout(handlePointerTimeout, this);
  108. #ifdef HAVE_FLTK_CLIPBOARD
  109. Fl::remove_clipboard_notify(handleClipboardChange);
  110. #endif
  111. OptionsDialog::removeCallback(handleOptions);
  112. delete frameBuffer;
  113. if (cursor) {
  114. if (!cursor->alloc_array)
  115. delete [] cursor->array;
  116. delete cursor;
  117. }
  118. // FLTK automatically deletes all child widgets, so we shouldn't touch
  119. // them ourselves here
  120. }
  121. const rfb::PixelFormat &Viewport::getPreferredPF()
  122. {
  123. return frameBuffer->getPF();
  124. }
  125. // Copy the areas of the framebuffer that have been changed (damaged)
  126. // to the displayed window.
  127. // FIXME: Make sure this gets called on slow updates
  128. void Viewport::updateWindow()
  129. {
  130. Rect r;
  131. r = frameBuffer->getDamage();
  132. damage(FL_DAMAGE_USER1, r.tl.x + x(), r.tl.y + y(), r.width(), r.height());
  133. }
  134. rfb::ModifiablePixelBuffer* Viewport::getFramebuffer(void)
  135. {
  136. return frameBuffer;
  137. }
  138. #ifdef HAVE_FLTK_CURSOR
  139. static const char * dotcursor_xpm[] = {
  140. "5 5 2 1",
  141. ". c #000000",
  142. " c #FFFFFF",
  143. " ",
  144. " ... ",
  145. " ... ",
  146. " ... ",
  147. " "};
  148. #endif
  149. void Viewport::setCursor(int width, int height, const Point& hotspot,
  150. void* data, void* mask)
  151. {
  152. #ifdef HAVE_FLTK_CURSOR
  153. if (cursor) {
  154. if (!cursor->alloc_array)
  155. delete [] cursor->array;
  156. delete cursor;
  157. }
  158. int mask_len = ((width+7)/8) * height;
  159. int i;
  160. for (i = 0; i < mask_len; i++)
  161. if (((rdr::U8*)mask)[i]) break;
  162. if ((i == mask_len) && dotWhenNoCursor) {
  163. vlog.debug("cursor is empty - using dot");
  164. Fl_Pixmap pxm(dotcursor_xpm);
  165. cursor = new Fl_RGB_Image(&pxm);
  166. cursorHotspot.x = cursorHotspot.y = 2;
  167. } else {
  168. if ((width == 0) || (height == 0)) {
  169. U8 *buffer = new U8[4];
  170. memset(buffer, 0, 4);
  171. cursor = new Fl_RGB_Image(buffer, 1, 1, 4);
  172. cursorHotspot.x = cursorHotspot.y = 0;
  173. } else {
  174. U8 *buffer = new U8[width*height*4];
  175. U8 *i, *o, *m;
  176. int m_width;
  177. const PixelFormat *pf;
  178. pf = &cc->cp.pf();
  179. i = (U8*)data;
  180. o = buffer;
  181. m = (U8*)mask;
  182. m_width = (width+7)/8;
  183. for (int y = 0;y < height;y++) {
  184. for (int x = 0;x < width;x++) {
  185. pf->rgbFromBuffer(o, i, 1);
  186. if (m[(m_width*y)+(x/8)] & 0x80>>(x%8))
  187. o[3] = 255;
  188. else
  189. o[3] = 0;
  190. o += 4;
  191. i += pf->bpp/8;
  192. }
  193. }
  194. cursor = new Fl_RGB_Image(buffer, width, height, 4);
  195. cursorHotspot = hotspot;
  196. }
  197. }
  198. if (Fl::belowmouse() == this)
  199. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  200. #endif
  201. }
  202. void Viewport::draw()
  203. {
  204. int X, Y, W, H;
  205. // Check what actually needs updating
  206. fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
  207. if ((W == 0) || (H == 0))
  208. return;
  209. frameBuffer->draw(X - x(), Y - y(), X, Y, W, H);
  210. }
  211. void Viewport::resize(int x, int y, int w, int h)
  212. {
  213. PlatformPixelBuffer* newBuffer;
  214. rfb::Rect rect;
  215. const rdr::U8* data;
  216. int stride;
  217. // FIXME: Resize should probably be a feature of the pixel buffer itself
  218. if ((w == frameBuffer->width()) && (h == frameBuffer->height()))
  219. goto end;
  220. vlog.debug("Resizing framebuffer from %dx%d to %dx%d",
  221. frameBuffer->width(), frameBuffer->height(), w, h);
  222. newBuffer = createFramebuffer(w, h);
  223. assert(newBuffer);
  224. rect.setXYWH(0, 0,
  225. __rfbmin(newBuffer->width(), frameBuffer->width()),
  226. __rfbmin(newBuffer->height(), frameBuffer->height()));
  227. data = frameBuffer->getBuffer(frameBuffer->getRect(), &stride);
  228. newBuffer->imageRect(rect, data, stride);
  229. // Black out any new areas
  230. if (newBuffer->width() > frameBuffer->width()) {
  231. rect.setXYWH(frameBuffer->width(), 0,
  232. newBuffer->width() - frameBuffer->width(),
  233. newBuffer->height());
  234. newBuffer->fillRect(rect, 0);
  235. }
  236. if (newBuffer->height() > frameBuffer->height()) {
  237. rect.setXYWH(0, frameBuffer->height(),
  238. newBuffer->width(),
  239. newBuffer->height() - frameBuffer->height());
  240. newBuffer->fillRect(rect, 0);
  241. }
  242. delete frameBuffer;
  243. frameBuffer = newBuffer;
  244. end:
  245. Fl_Widget::resize(x, y, w, h);
  246. }
  247. int Viewport::handle(int event)
  248. {
  249. char *buffer;
  250. int ret;
  251. int buttonMask, wheelMask;
  252. DownMap::const_iterator iter;
  253. switch (event) {
  254. case FL_PASTE:
  255. buffer = new char[Fl::event_length() + 1];
  256. // This is documented as to ASCII, but actually does to 8859-1
  257. ret = fl_utf8toa(Fl::event_text(), Fl::event_length(), buffer,
  258. Fl::event_length() + 1);
  259. assert(ret < (Fl::event_length() + 1));
  260. vlog.debug("Sending clipboard data (%d bytes)", strlen(buffer));
  261. try {
  262. cc->writer()->clientCutText(buffer, ret);
  263. } catch (rdr::Exception& e) {
  264. vlog.error("%s", e.str());
  265. exit_vncviewer(e.str());
  266. }
  267. delete [] buffer;
  268. return 1;
  269. case FL_ENTER:
  270. // Yes, we would like some pointer events please!
  271. #ifdef HAVE_FLTK_CURSOR
  272. if (cursor)
  273. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  274. #endif
  275. return 1;
  276. case FL_LEAVE:
  277. #ifdef HAVE_FLTK_CURSOR
  278. window()->cursor(FL_CURSOR_DEFAULT);
  279. #endif
  280. // Fall through as we want a last move event to help trigger edge stuff
  281. case FL_PUSH:
  282. case FL_RELEASE:
  283. case FL_DRAG:
  284. case FL_MOVE:
  285. case FL_MOUSEWHEEL:
  286. buttonMask = 0;
  287. if (Fl::event_button1())
  288. buttonMask |= 1;
  289. if (Fl::event_button2())
  290. buttonMask |= 2;
  291. if (Fl::event_button3())
  292. buttonMask |= 4;
  293. if (event == FL_MOUSEWHEEL) {
  294. wheelMask = 0;
  295. if (Fl::event_dy() < 0)
  296. wheelMask |= 8;
  297. if (Fl::event_dy() > 0)
  298. wheelMask |= 16;
  299. if (Fl::event_dx() < 0)
  300. wheelMask |= 32;
  301. if (Fl::event_dx() > 0)
  302. wheelMask |= 64;
  303. // A quick press of the wheel "button", followed by a immediate
  304. // release below
  305. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()),
  306. buttonMask | wheelMask);
  307. }
  308. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), buttonMask);
  309. return 1;
  310. case FL_FOCUS:
  311. // Yes, we would like some focus please!
  312. return 1;
  313. case FL_UNFOCUS:
  314. // Release all keys that were pressed as that generally makes most
  315. // sense (e.g. Alt+Tab where we only see the Alt press)
  316. while (!downKeySym.empty())
  317. handleKeyRelease(downKeySym.begin()->first);
  318. return 1;
  319. case FL_KEYDOWN:
  320. handleFLTKKeyPress();
  321. return 1;
  322. case FL_KEYUP:
  323. handleKeyRelease(Fl::event_original_key());
  324. return 1;
  325. }
  326. return Fl_Widget::handle(event);
  327. }
  328. PlatformPixelBuffer* Viewport::createFramebuffer(int w, int h)
  329. {
  330. PlatformPixelBuffer *fb;
  331. try {
  332. #if defined(WIN32)
  333. fb = new Win32PixelBuffer(w, h);
  334. #elif defined(__APPLE__)
  335. fb = new OSXPixelBuffer(w, h);
  336. #else
  337. fb = new X11PixelBuffer(w, h);
  338. #endif
  339. } catch (rdr::Exception& e) {
  340. fb = new FLTKPixelBuffer(w, h);
  341. }
  342. return fb;
  343. }
  344. void Viewport::handleClipboardChange(int source, void *data)
  345. {
  346. Viewport *self = (Viewport *)data;
  347. assert(self);
  348. if (!sendPrimary && (source == 0))
  349. return;
  350. Fl::paste(*self, source);
  351. }
  352. void Viewport::handlePointerEvent(const rfb::Point& pos, int buttonMask)
  353. {
  354. if (!viewOnly) {
  355. if (pointerEventInterval == 0 || buttonMask != lastButtonMask) {
  356. try {
  357. cc->writer()->pointerEvent(pos, buttonMask);
  358. } catch (rdr::Exception& e) {
  359. vlog.error("%s", e.str());
  360. exit_vncviewer(e.str());
  361. }
  362. } else {
  363. if (!Fl::has_timeout(handlePointerTimeout, this))
  364. Fl::add_timeout((double)pointerEventInterval/1000.0,
  365. handlePointerTimeout, this);
  366. }
  367. lastPointerPos = pos;
  368. lastButtonMask = buttonMask;
  369. }
  370. }
  371. void Viewport::handlePointerTimeout(void *data)
  372. {
  373. Viewport *self = (Viewport *)data;
  374. assert(self);
  375. try {
  376. self->cc->writer()->pointerEvent(self->lastPointerPos, self->lastButtonMask);
  377. } catch (rdr::Exception& e) {
  378. vlog.error("%s", e.str());
  379. exit_vncviewer(e.str());
  380. }
  381. }
  382. void Viewport::handleKeyPress(int keyCode, rdr::U32 keySym)
  383. {
  384. static bool menuRecursion = false;
  385. // Prevent recursion if the menu wants to send its own
  386. // activation key.
  387. if (menuKeySym && (keySym == menuKeySym) && !menuRecursion) {
  388. menuRecursion = true;
  389. popupContextMenu();
  390. menuRecursion = false;
  391. return;
  392. }
  393. if (viewOnly)
  394. return;
  395. #ifdef __APPLE__
  396. // Alt on OS X behaves more like AltGr on other systems, and to get
  397. // sane behaviour we should translate things in that manner for the
  398. // remote VNC server. However that means we lose the ability to use
  399. // Alt as a shortcut modifier. Do what RealVNC does and hijack the
  400. // left command key as an Alt replacement.
  401. switch (keySym) {
  402. case XK_Super_L:
  403. keySym = XK_Alt_L;
  404. break;
  405. case XK_Super_R:
  406. keySym = XK_Super_L;
  407. break;
  408. case XK_Alt_L:
  409. case XK_Alt_R:
  410. keySym = XK_ISO_Level3_Shift;
  411. break;
  412. }
  413. #endif
  414. #ifdef WIN32
  415. // Ugly hack alert!
  416. //
  417. // Windows doesn't have a proper AltGr, but handles it using fake
  418. // Ctrl+Alt. Unfortunately X11 doesn't generally like the combination
  419. // Ctrl+Alt+AltGr, which we usually end up with when Xvnc tries to
  420. // get everything in the correct state. Cheat and temporarily release
  421. // Ctrl and Alt when we send some other symbol.
  422. bool ctrlPressed, altPressed;
  423. DownMap::iterator iter;
  424. ctrlPressed = false;
  425. altPressed = false;
  426. for (iter = downKeySym.begin();iter != downKeySym.end();++iter) {
  427. if (iter->second == XK_Control_L)
  428. ctrlPressed = true;
  429. else if (iter->second == XK_Alt_R)
  430. altPressed = true;
  431. }
  432. if (ctrlPressed && altPressed) {
  433. vlog.debug("Faking release of AltGr (Ctrl_L+Alt_R)");
  434. try {
  435. cc->writer()->keyEvent(XK_Control_L, false);
  436. cc->writer()->keyEvent(XK_Alt_R, false);
  437. } catch (rdr::Exception& e) {
  438. vlog.error("%s", e.str());
  439. exit_vncviewer(e.str());
  440. }
  441. }
  442. #endif
  443. // Because of the way keyboards work, we cannot expect to have the same
  444. // symbol on release as when pressed. This breaks the VNC protocol however,
  445. // so we need to keep track of what keysym a key _code_ generated on press
  446. // and send the same on release.
  447. downKeySym[keyCode] = keySym;
  448. #if defined(WIN32) || defined(__APPLE__)
  449. vlog.debug("Key pressed: 0x%04x => 0x%04x", keyCode, keySym);
  450. #else
  451. vlog.debug("Key pressed: 0x%04x => XK_%s (0x%04x)",
  452. keyCode, XKeysymToString(keySym), keySym);
  453. #endif
  454. try {
  455. cc->writer()->keyEvent(keySym, true);
  456. } catch (rdr::Exception& e) {
  457. vlog.error("%s", e.str());
  458. exit_vncviewer(e.str());
  459. }
  460. #ifdef WIN32
  461. // Ugly hack continued...
  462. if (ctrlPressed && altPressed) {
  463. vlog.debug("Restoring AltGr state");
  464. try {
  465. cc->writer()->keyEvent(XK_Control_L, true);
  466. cc->writer()->keyEvent(XK_Alt_R, true);
  467. } catch (rdr::Exception& e) {
  468. vlog.error("%s", e.str());
  469. exit_vncviewer(e.str());
  470. }
  471. }
  472. #endif
  473. }
  474. void Viewport::handleKeyRelease(int keyCode)
  475. {
  476. DownMap::iterator iter;
  477. if (viewOnly)
  478. return;
  479. iter = downKeySym.find(keyCode);
  480. if (iter == downKeySym.end()) {
  481. // These occur somewhat frequently so let's not spam them unless
  482. // logging is turned up.
  483. vlog.debug("Unexpected release of key code %d", keyCode);
  484. return;
  485. }
  486. #if defined(WIN32) || defined(__APPLE__)
  487. vlog.debug("Key released: 0x%04x => 0x%04x", keyCode, iter->second);
  488. #else
  489. vlog.debug("Key released: 0x%04x => XK_%s (0x%04x)",
  490. keyCode, XKeysymToString(iter->second), iter->second);
  491. #endif
  492. try {
  493. cc->writer()->keyEvent(iter->second, false);
  494. } catch (rdr::Exception& e) {
  495. vlog.error("%s", e.str());
  496. exit_vncviewer(e.str());
  497. }
  498. downKeySym.erase(iter);
  499. }
  500. rdr::U32 Viewport::translateKeyEvent(void)
  501. {
  502. unsigned ucs;
  503. int keyCode, origKeyCode;
  504. const char *keyText;
  505. int keyTextLen;
  506. keyCode = Fl::event_key();
  507. origKeyCode = Fl::event_original_key();
  508. keyText = Fl::event_text();
  509. keyTextLen = Fl::event_length();
  510. vlog.debug("FLTK key %d (%d) '%s'[%d]", origKeyCode, keyCode, keyText, keyTextLen);
  511. // First check for function keys
  512. if ((keyCode > FL_F) && (keyCode <= FL_F_Last))
  513. return XK_F1 + (keyCode - FL_F - 1);
  514. // Numpad numbers
  515. if ((keyCode >= (FL_KP + '0')) && (keyCode <= (FL_KP + '9')))
  516. return XK_KP_0 + (keyCode - (FL_KP + '0'));
  517. // FLTK does some special remapping of numpad keys when numlock is off
  518. if ((origKeyCode >= FL_KP) && (origKeyCode <= FL_KP_Last)) {
  519. switch (keyCode) {
  520. case FL_F+1:
  521. return XK_KP_F1;
  522. case FL_F+2:
  523. return XK_KP_F2;
  524. case FL_F+3:
  525. return XK_KP_F3;
  526. case FL_F+4:
  527. return XK_KP_F4;
  528. case FL_Home:
  529. return XK_KP_Home;
  530. case FL_Left:
  531. return XK_KP_Left;
  532. case FL_Up:
  533. return XK_KP_Up;
  534. case FL_Right:
  535. return XK_KP_Right;
  536. case FL_Down:
  537. return XK_KP_Down;
  538. case FL_Page_Up:
  539. return XK_KP_Page_Up;
  540. case FL_Page_Down:
  541. return XK_KP_Page_Down;
  542. case FL_End:
  543. return XK_KP_End;
  544. case FL_Insert:
  545. return XK_KP_Insert;
  546. case FL_Delete:
  547. return XK_KP_Delete;
  548. }
  549. }
  550. #if defined(WIN32) || defined(__APPLE__)
  551. // X11 fairly consistently uses XK_KP_Separator for comma and
  552. // XK_KP_Decimal for period. Windows and OS X are a different matter
  553. // though.
  554. //
  555. // OS X will consistently generate the same key code no matter what
  556. // layout is being used.
  557. //
  558. // Windows is terribly inconcistent, and is not something that's
  559. // likely to change:
  560. // http://blogs.msdn.com/michkap/archive/2006/09/13/752377.aspx
  561. //
  562. // To get X11 behaviour, we instead look at the text generated by
  563. // they key.
  564. if ((keyCode == (FL_KP + ',')) || (keyCode == (FL_KP + '.'))) {
  565. switch (keyText[0]) {
  566. case ',':
  567. return XK_KP_Separator;
  568. case '.':
  569. return XK_KP_Decimal;
  570. default:
  571. vlog.error(_("Unknown decimal separator: '%s'"), keyText);
  572. return XK_KP_Decimal;
  573. }
  574. }
  575. #endif
  576. // Then other special keys
  577. switch (keyCode) {
  578. case FL_BackSpace:
  579. return XK_BackSpace;
  580. case FL_Tab:
  581. return XK_Tab;
  582. case FL_Enter:
  583. return XK_Return;
  584. case FL_Pause:
  585. return XK_Pause;
  586. case FL_Scroll_Lock:
  587. return XK_Scroll_Lock;
  588. case FL_Escape:
  589. return XK_Escape;
  590. case FL_Home:
  591. return XK_Home;
  592. case FL_Left:
  593. return XK_Left;
  594. case FL_Up:
  595. return XK_Up;
  596. case FL_Right:
  597. return XK_Right;
  598. case FL_Down:
  599. return XK_Down;
  600. case FL_Page_Up:
  601. return XK_Page_Up;
  602. case FL_Page_Down:
  603. return XK_Page_Down;
  604. case FL_End:
  605. return XK_End;
  606. case FL_Print:
  607. return XK_Print;
  608. case FL_Insert:
  609. return XK_Insert;
  610. case FL_Menu:
  611. return XK_Menu;
  612. case FL_Help:
  613. return XK_Help;
  614. case FL_Num_Lock:
  615. return XK_Num_Lock;
  616. case FL_Shift_L:
  617. return XK_Shift_L;
  618. case FL_Shift_R:
  619. return XK_Shift_R;
  620. case FL_Control_L:
  621. return XK_Control_L;
  622. case FL_Control_R:
  623. return XK_Control_R;
  624. case FL_Caps_Lock:
  625. return XK_Caps_Lock;
  626. case FL_Meta_L:
  627. return XK_Super_L;
  628. case FL_Meta_R:
  629. return XK_Super_R;
  630. case FL_Alt_L:
  631. return XK_Alt_L;
  632. case FL_Alt_R:
  633. return XK_Alt_R;
  634. case FL_Delete:
  635. return XK_Delete;
  636. case FL_KP_Enter:
  637. return XK_KP_Enter;
  638. case FL_KP + '=':
  639. return XK_KP_Equal;
  640. case FL_KP + '*':
  641. return XK_KP_Multiply;
  642. case FL_KP + '+':
  643. return XK_KP_Add;
  644. case FL_KP + ',':
  645. return XK_KP_Separator;
  646. case FL_KP + '-':
  647. return XK_KP_Subtract;
  648. case FL_KP + '.':
  649. return XK_KP_Decimal;
  650. case FL_KP + '/':
  651. return XK_KP_Divide;
  652. #ifdef HAVE_FLTK_MEDIAKEYS
  653. case FL_Volume_Down:
  654. return XF86XK_AudioLowerVolume;
  655. case FL_Volume_Mute:
  656. return XF86XK_AudioMute;
  657. case FL_Volume_Up:
  658. return XF86XK_AudioRaiseVolume;
  659. case FL_Media_Play:
  660. return XF86XK_AudioPlay;
  661. case FL_Media_Stop:
  662. return XF86XK_AudioStop;
  663. case FL_Media_Prev:
  664. return XF86XK_AudioPrev;
  665. case FL_Media_Next:
  666. return XF86XK_AudioNext;
  667. case FL_Home_Page:
  668. return XF86XK_HomePage;
  669. case FL_Mail:
  670. return XF86XK_Mail;
  671. case FL_Search:
  672. return XF86XK_Search;
  673. case FL_Back:
  674. return XF86XK_Back;
  675. case FL_Forward:
  676. return XF86XK_Forward;
  677. case FL_Stop:
  678. return XF86XK_Stop;
  679. case FL_Refresh:
  680. return XF86XK_Refresh;
  681. case FL_Sleep:
  682. return XF86XK_Sleep;
  683. case FL_Favorites:
  684. return XF86XK_Favorites;
  685. #endif
  686. case XK_ISO_Level3_Shift:
  687. // FLTK tends to let this one leak through on X11...
  688. return XK_ISO_Level3_Shift;
  689. case XK_Multi_key:
  690. // Same for this...
  691. return XK_Multi_key;
  692. }
  693. // Unknown special key?
  694. if (keyTextLen == 0) {
  695. vlog.error(_("Unknown FLTK key code %d (0x%04x)"), keyCode, keyCode);
  696. return NoSymbol;
  697. }
  698. // Control character?
  699. if ((keyTextLen == 1) && ((keyText[0] < 0x20) | (keyText[0] == 0x7f))) {
  700. if (keyText[0] == 0x00)
  701. return XK_2;
  702. else if (keyText[0] < 0x1b) {
  703. if (!!Fl::event_state(FL_SHIFT) != !!Fl::event_state(FL_CAPS_LOCK))
  704. return keyText[0] + XK_A - 0x01;
  705. else
  706. return keyText[0] + XK_a - 0x01;
  707. } else if (keyText[0] < 0x20)
  708. return keyText[0] + XK_3 - 0x1b;
  709. else
  710. return XK_8;
  711. }
  712. // Look up the symbol the key produces and translate that from Unicode
  713. // to a X11 keysym.
  714. if (fl_utf_nb_char((const unsigned char*)keyText, strlen(keyText)) != 1) {
  715. vlog.error(_("Multiple characters given for key code %d (0x%04x): '%s'"),
  716. keyCode, keyCode, keyText);
  717. return NoSymbol;
  718. }
  719. ucs = fl_utf8decode(keyText, NULL, NULL);
  720. return ucs2keysym(ucs);
  721. }
  722. void Viewport::handleFLTKKeyPress(void)
  723. {
  724. rdr::U32 keySym;
  725. keySym = translateKeyEvent();
  726. if (keySym == NoSymbol)
  727. return;
  728. handleKeyPress(Fl::event_original_key(), keySym);
  729. }
  730. void Viewport::initContextMenu()
  731. {
  732. contextMenu->clear();
  733. contextMenu->add(_("Exit viewer"), 0, NULL, (void*)ID_EXIT, FL_MENU_DIVIDER);
  734. #ifdef HAVE_FLTK_FULLSCREEN
  735. contextMenu->add(_("Full screen"), 0, NULL, (void*)ID_FULLSCREEN,
  736. FL_MENU_TOGGLE | (window()->fullscreen_active()?FL_MENU_VALUE:0));
  737. #endif
  738. contextMenu->add(_("Resize window to session"), 0, NULL, (void*)ID_RESIZE,
  739. #ifdef HAVE_FLTK_FULLSCREEN
  740. (window()->fullscreen_active()?FL_MENU_INACTIVE:0) |
  741. #endif
  742. FL_MENU_DIVIDER);
  743. contextMenu->add(_("Ctrl"), 0, NULL, (void*)ID_CTRL,
  744. FL_MENU_TOGGLE | (menuCtrlKey?FL_MENU_VALUE:0));
  745. contextMenu->add(_("Alt"), 0, NULL, (void*)ID_ALT,
  746. FL_MENU_TOGGLE | (menuAltKey?FL_MENU_VALUE:0));
  747. if (menuKeySym) {
  748. char sendMenuKey[64];
  749. snprintf(sendMenuKey, 64, _("Send %s"), (const char *)menuKey);
  750. contextMenu->add(sendMenuKey, 0, NULL, (void*)ID_MENUKEY, 0);
  751. contextMenu->add("Secret shortcut menu key", menuKeyCode, NULL, (void*)ID_MENUKEY, FL_MENU_INVISIBLE);
  752. }
  753. contextMenu->add(_("Send Ctrl-Alt-Del"), 0, NULL, (void*)ID_CTRLALTDEL, FL_MENU_DIVIDER);
  754. contextMenu->add(_("Refresh screen"), 0, NULL, (void*)ID_REFRESH, FL_MENU_DIVIDER);
  755. contextMenu->add(_("Options..."), 0, NULL, (void*)ID_OPTIONS, 0);
  756. contextMenu->add(_("Connection info..."), 0, NULL, (void*)ID_INFO, 0);
  757. contextMenu->add(_("About TigerVNC viewer..."), 0, NULL, (void*)ID_ABOUT, FL_MENU_DIVIDER);
  758. contextMenu->add(_("Dismiss menu"), 0, NULL, (void*)ID_DISMISS, 0);
  759. }
  760. void Viewport::popupContextMenu()
  761. {
  762. const Fl_Menu_Item *m;
  763. char buffer[1024];
  764. // Make sure the menu is reset to its initial state between goes or
  765. // it will start up highlighting the previously selected entry.
  766. contextMenu->value(-1);
  767. // initialize context menu before display
  768. initContextMenu();
  769. // Unfortunately FLTK doesn't reliably restore the mouse pointer for
  770. // menus, so we have to help it out.
  771. #ifdef HAVE_FLTK_CURSOR
  772. if (Fl::belowmouse() == this)
  773. window()->cursor(FL_CURSOR_DEFAULT);
  774. #endif
  775. m = contextMenu->popup();
  776. // Back to our proper mouse pointer.
  777. #ifdef HAVE_FLTK_CURSOR
  778. if ((Fl::belowmouse() == this) && cursor)
  779. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  780. #endif
  781. if (m == NULL)
  782. return;
  783. switch (m->argument()) {
  784. case ID_EXIT:
  785. exit_vncviewer();
  786. break;
  787. #ifdef HAVE_FLTK_FULLSCREEN
  788. case ID_FULLSCREEN:
  789. if (window()->fullscreen_active())
  790. window()->fullscreen_off();
  791. else
  792. ((DesktopWindow*)window())->fullscreen_on();
  793. break;
  794. #endif
  795. case ID_RESIZE:
  796. #ifdef HAVE_FLTK_FULLSCREEN
  797. if (window()->fullscreen_active())
  798. break;
  799. #endif
  800. window()->size(w(), h());
  801. break;
  802. case ID_CTRL:
  803. if (m->value())
  804. handleKeyPress(fakeKeyBase + 0, XK_Control_L);
  805. else
  806. handleKeyRelease(fakeKeyBase + 0);
  807. menuCtrlKey = !menuCtrlKey;
  808. break;
  809. case ID_ALT:
  810. if (m->value())
  811. handleKeyPress(fakeKeyBase + 1, XK_Alt_L);
  812. else
  813. handleKeyRelease(fakeKeyBase + 1);
  814. menuAltKey = !menuAltKey;
  815. break;
  816. case ID_MENUKEY:
  817. handleKeyPress(fakeKeyBase + 2, menuKeySym);
  818. handleKeyRelease(fakeKeyBase + 2);
  819. break;
  820. case ID_CTRLALTDEL:
  821. handleKeyPress(fakeKeyBase + 3, XK_Control_L);
  822. handleKeyPress(fakeKeyBase + 4, XK_Alt_L);
  823. handleKeyPress(fakeKeyBase + 5, XK_Delete);
  824. handleKeyRelease(fakeKeyBase + 5);
  825. handleKeyRelease(fakeKeyBase + 4);
  826. handleKeyRelease(fakeKeyBase + 3);
  827. break;
  828. case ID_REFRESH:
  829. cc->refreshFramebuffer();
  830. break;
  831. case ID_OPTIONS:
  832. OptionsDialog::showDialog();
  833. break;
  834. case ID_INFO:
  835. if (fltk_escape(cc->connectionInfo(), buffer, sizeof(buffer)) < sizeof(buffer)) {
  836. fl_message_title(_("VNC connection info"));
  837. fl_message("%s", buffer);
  838. }
  839. break;
  840. case ID_ABOUT:
  841. about_vncviewer();
  842. break;
  843. case ID_DISMISS:
  844. // Don't need to do anything
  845. break;
  846. }
  847. }
  848. void Viewport::setMenuKey()
  849. {
  850. getMenuKey(&menuKeyCode, &menuKeySym);
  851. }
  852. void Viewport::handleOptions(void *data)
  853. {
  854. Viewport *self = (Viewport*)data;
  855. self->setMenuKey();
  856. }