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

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