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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. #include <assert.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <FL/fl_draw.H>
  23. #include <FL/fl_ask.H>
  24. #include <rfb/CMsgWriter.h>
  25. #include <rfb/LogWriter.h>
  26. // FLTK can pull in the X11 headers on some systems
  27. #ifndef XK_VoidSymbol
  28. #define XK_MISCELLANY
  29. #define XK_XKB_KEYS
  30. #include <rfb/keysymdef.h>
  31. #endif
  32. #ifndef XF86XK_ModeLock
  33. #include <rfb/XF86keysym.h>
  34. #endif
  35. #include "Viewport.h"
  36. #include "CConn.h"
  37. #include "OptionsDialog.h"
  38. #include "i18n.h"
  39. #include "fltk_layout.h"
  40. #include "parameters.h"
  41. #include "keysym2ucs.h"
  42. using namespace rfb;
  43. using namespace rdr;
  44. extern void exit_vncviewer();
  45. extern void about_vncviewer();
  46. static rfb::LogWriter vlog("Viewport");
  47. // Menu constants
  48. enum { ID_EXIT, ID_FULLSCREEN, ID_CTRL, ID_ALT, ID_MENUKEY, ID_CTRLALTDEL,
  49. ID_REFRESH, ID_OPTIONS, ID_INFO, ID_ABOUT, ID_DISMISS };
  50. Viewport::Viewport(int w, int h, const rfb::PixelFormat& serverPF, CConn* cc_)
  51. : Fl_Widget(0, 0, w, h), cc(cc_), frameBuffer(NULL), pixelTrans(NULL),
  52. lastPointerPos(0, 0), lastButtonMask(0), cursor(NULL)
  53. {
  54. // FLTK STR #2599 must be fixed for proper dead keys support
  55. #ifdef HAVE_FLTK_DEAD_KEYS
  56. set_simple_keyboard();
  57. #endif
  58. // FLTK STR #2636 gives us the ability to monitor clipboard changes
  59. #ifdef HAVE_FLTK_CLIPBOARD
  60. Fl::add_clipboard_notify(handleClipboardChange, this);
  61. #endif
  62. frameBuffer = new ManagedPixelBuffer(getPreferredPF(), w, h);
  63. assert(frameBuffer);
  64. setServerPF(serverPF);
  65. contextMenu = new Fl_Menu_Button(0, 0, 0, 0);
  66. // Setting box type to FL_NO_BOX prevents it from trying to draw the
  67. // button component (which we don't want)
  68. contextMenu->box(FL_NO_BOX);
  69. // The (invisible) button associated with this widget can mess with
  70. // things like Fl_Scroll so we need to get rid of any parents.
  71. // Unfortunately that's not possible because of STR #2654, but
  72. // reparenting to the current window works for most cases.
  73. window()->add(contextMenu);
  74. initContextMenu();
  75. setMenuKey();
  76. OptionsDialog::addCallback(handleOptions, this);
  77. }
  78. Viewport::~Viewport()
  79. {
  80. // Unregister all timeouts in case they get a change tro trigger
  81. // again later when this object is already gone.
  82. Fl::remove_timeout(handleUpdateTimeout, this);
  83. Fl::remove_timeout(handleColourMap, this);
  84. Fl::remove_timeout(handlePointerTimeout, this);
  85. #ifdef HAVE_FLTK_CLIPBOARD
  86. Fl::remove_clipboard_notify(handleClipboardChange);
  87. #endif
  88. OptionsDialog::removeCallback(handleOptions);
  89. delete frameBuffer;
  90. if (pixelTrans)
  91. delete pixelTrans;
  92. if (cursor) {
  93. if (!cursor->alloc_array)
  94. delete [] cursor->array;
  95. delete cursor;
  96. }
  97. // FLTK automatically deletes all child widgets, so we shouldn't touch
  98. // them ourselves here
  99. }
  100. void Viewport::setServerPF(const rfb::PixelFormat& pf)
  101. {
  102. if (pixelTrans)
  103. delete pixelTrans;
  104. pixelTrans = NULL;
  105. if (pf.equal(getPreferredPF()))
  106. return;
  107. pixelTrans = new PixelTransformer();
  108. pixelTrans->init(pf, &colourMap, getPreferredPF());
  109. }
  110. const rfb::PixelFormat &Viewport::getPreferredPF()
  111. {
  112. static PixelFormat prefPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
  113. return prefPF;
  114. }
  115. // setColourMapEntries() changes some of the entries in the colourmap.
  116. // Unfortunately these messages are often sent one at a time, so we delay the
  117. // settings taking effect by 100ms. This is because recalculating the internal
  118. // translation table can be expensive.
  119. void Viewport::setColourMapEntries(int firstColour, int nColours,
  120. rdr::U16* rgbs)
  121. {
  122. for (int i = 0; i < nColours; i++)
  123. colourMap.set(firstColour+i, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
  124. if (!Fl::has_timeout(handleColourMap, this))
  125. Fl::add_timeout(0.100, handleColourMap, this);
  126. }
  127. // Copy the areas of the framebuffer that have been changed (damaged)
  128. // to the displayed window.
  129. void Viewport::updateWindow()
  130. {
  131. Rect r;
  132. Fl::remove_timeout(handleUpdateTimeout, this);
  133. r = damage.get_bounding_rect();
  134. Fl_Widget::damage(FL_DAMAGE_USER1, r.tl.x + x(), r.tl.y + y(), r.width(), r.height());
  135. damage.clear();
  136. }
  137. #ifdef HAVE_FLTK_CURSOR
  138. static const char * dotcursor_xpm[] = {
  139. "5 5 2 1",
  140. ". c #000000",
  141. " c #FFFFFF",
  142. " ",
  143. " ... ",
  144. " ... ",
  145. " ... ",
  146. " "};
  147. #endif
  148. void Viewport::setCursor(int width, int height, const Point& hotspot,
  149. void* data, void* mask)
  150. {
  151. #ifdef HAVE_FLTK_CURSOR
  152. if (cursor) {
  153. if (!cursor->alloc_array)
  154. delete [] cursor->array;
  155. delete cursor;
  156. }
  157. int mask_len = ((width+7)/8) * height;
  158. int i;
  159. for (i = 0; i < mask_len; i++)
  160. if (((rdr::U8*)mask)[i]) break;
  161. if ((i == mask_len) && dotWhenNoCursor) {
  162. vlog.debug("cursor is empty - using dot");
  163. Fl_Pixmap pxm(dotcursor_xpm);
  164. cursor = new Fl_RGB_Image(&pxm);
  165. cursorHotspot.x = cursorHotspot.y = 2;
  166. } else {
  167. U8 *buffer = new U8[width*height*4];
  168. U8 *i, *o, *m;
  169. int m_width;
  170. const PixelFormat &pf = frameBuffer->getPF();
  171. i = (U8*)data;
  172. o = buffer;
  173. m = (U8*)mask;
  174. m_width = (width+7)/8;
  175. for (int y = 0;y < height;y++) {
  176. for (int x = 0;x < width;x++) {
  177. pf.rgbFromBuffer(o, i, 1, &colourMap);
  178. if (m[(m_width*y)+(x/8)] & 0x80>>(x%8))
  179. o[3] = 255;
  180. else
  181. o[3] = 0;
  182. o += 4;
  183. i += pf.bpp/8;
  184. }
  185. }
  186. cursor = new Fl_RGB_Image(buffer, width, height, 4);
  187. cursorHotspot = hotspot;
  188. }
  189. if (Fl::belowmouse() == this)
  190. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  191. #endif
  192. }
  193. void Viewport::draw()
  194. {
  195. int X, Y, W, H;
  196. int pixel_bytes, stride_bytes;
  197. const uchar *buf_start;
  198. // Check what actually needs updating
  199. fl_clip_box(x(), y(), w(), h(), X, Y, W, H);
  200. if ((W == 0) || (H == 0))
  201. return;
  202. pixel_bytes = frameBuffer->getPF().bpp/8;
  203. stride_bytes = pixel_bytes * frameBuffer->getStride();
  204. buf_start = frameBuffer->data +
  205. pixel_bytes * (X - x()) +
  206. stride_bytes * (Y - y());
  207. // FIXME: Check how efficient this thing really is
  208. fl_draw_image(buf_start, X, Y, W, H, pixel_bytes, stride_bytes);
  209. }
  210. void Viewport::resize(int x, int y, int w, int h)
  211. {
  212. rfb::ManagedPixelBuffer* newBuffer;
  213. rfb::Rect rect;
  214. // FIXME: Resize should probably be a feature of ManagedPixelBuffer
  215. if ((w == frameBuffer->width()) && (h == frameBuffer->height()))
  216. goto end;
  217. newBuffer = new ManagedPixelBuffer(frameBuffer->getPF(), w, h);
  218. assert(newBuffer);
  219. rect.setXYWH(0, 0,
  220. __rfbmin(newBuffer->width(), frameBuffer->width()),
  221. __rfbmin(newBuffer->height(), frameBuffer->height()));
  222. newBuffer->imageRect(rect, frameBuffer->data, frameBuffer->getStride());
  223. // Black out any new areas
  224. if (newBuffer->width() > frameBuffer->width()) {
  225. rect.setXYWH(frameBuffer->width(), 0,
  226. newBuffer->width() - frameBuffer->width(),
  227. newBuffer->height());
  228. newBuffer->fillRect(rect, 0);
  229. }
  230. if (newBuffer->height() > frameBuffer->height()) {
  231. rect.setXYWH(0, frameBuffer->height(),
  232. newBuffer->width(),
  233. newBuffer->height() - frameBuffer->height());
  234. newBuffer->fillRect(rect, 0);
  235. }
  236. delete frameBuffer;
  237. frameBuffer = newBuffer;
  238. end:
  239. Fl_Widget::resize(x, y, w, h);
  240. }
  241. int Viewport::handle(int event)
  242. {
  243. char *buffer;
  244. int ret;
  245. int buttonMask, wheelMask;
  246. DownMap::const_iterator iter;
  247. switch (event) {
  248. case FL_PASTE:
  249. buffer = new char[Fl::event_length() + 1];
  250. // This is documented as to ASCII, but actually does to 8859-1
  251. ret = fl_utf8toa(Fl::event_text(), Fl::event_length(), buffer,
  252. Fl::event_length() + 1);
  253. assert(ret < (Fl::event_length() + 1));
  254. vlog.debug("Sending clipboard data: '%s'", buffer);
  255. cc->writer()->clientCutText(buffer, ret);
  256. delete [] buffer;
  257. return 1;
  258. case FL_ENTER:
  259. // Yes, we would like some pointer events please!
  260. #ifdef HAVE_FLTK_CURSOR
  261. if (cursor)
  262. window()->cursor(cursor, cursorHotspot.x, cursorHotspot.y);
  263. #endif
  264. return 1;
  265. case FL_LEAVE:
  266. #ifdef HAVE_FLTK_CURSOR
  267. window()->cursor(FL_CURSOR_DEFAULT);
  268. #endif
  269. return 1;
  270. case FL_PUSH:
  271. case FL_RELEASE:
  272. case FL_DRAG:
  273. case FL_MOVE:
  274. case FL_MOUSEWHEEL:
  275. buttonMask = 0;
  276. if (Fl::event_button1())
  277. buttonMask |= 1;
  278. if (Fl::event_button2())
  279. buttonMask |= 2;
  280. if (Fl::event_button3())
  281. buttonMask |= 4;
  282. if (event == FL_MOUSEWHEEL) {
  283. wheelMask = 0;
  284. if (Fl::event_dy() < 0)
  285. wheelMask |= 8;
  286. if (Fl::event_dy() > 0)
  287. wheelMask |= 16;
  288. if (Fl::event_dx() < 0)
  289. wheelMask |= 32;
  290. if (Fl::event_dx() > 0)
  291. wheelMask |= 64;
  292. // A quick press of the wheel "button", followed by a immediate
  293. // release below
  294. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()),
  295. buttonMask | wheelMask);
  296. }
  297. handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), buttonMask);
  298. return 1;
  299. case FL_FOCUS:
  300. // Yes, we would like some focus please!
  301. return 1;
  302. case FL_UNFOCUS:
  303. // Release all keys that were pressed as that generally makes most
  304. // sense (e.g. Alt+Tab where we only see the Alt press)
  305. for (iter = downKeySym.begin();iter != downKeySym.end();++iter) {
  306. vlog.debug("Key released: 0x%04x => 0x%04x", iter->first, iter->second);
  307. cc->writer()->keyEvent(iter->second, false);
  308. }
  309. downKeySym.clear();
  310. return 1;
  311. case FL_KEYDOWN:
  312. if (menuKeyCode && (Fl::event_key() == menuKeyCode)) {
  313. popupContextMenu();
  314. return 1;
  315. }
  316. handleKeyEvent(Fl::event_key(), Fl::event_original_key(),
  317. Fl::event_text(), true);
  318. return 1;
  319. case FL_KEYUP:
  320. if (menuKeyCode && (Fl::event_key() == menuKeyCode))
  321. return 1;
  322. handleKeyEvent(Fl::event_key(), Fl::event_original_key(),
  323. Fl::event_text(), false);
  324. return 1;
  325. }
  326. return Fl_Widget::handle(event);
  327. }
  328. void Viewport::handleUpdateTimeout(void *data)
  329. {
  330. Viewport *self = (Viewport *)data;
  331. assert(self);
  332. self->updateWindow();
  333. }
  334. void Viewport::handleColourMap(void *data)
  335. {
  336. Viewport *self = (Viewport *)data;
  337. assert(self);
  338. if (self->pixelTrans != NULL)
  339. self->pixelTrans->setColourMapEntries(0, 0);
  340. self->Fl_Widget::damage(FL_DAMAGE_ALL);
  341. }
  342. void Viewport::handleClipboardChange(int source, void *data)
  343. {
  344. Viewport *self = (Viewport *)data;
  345. assert(self);
  346. if (!sendPrimary && (source == 0))
  347. return;
  348. Fl::paste(*self, source);
  349. }
  350. void Viewport::handlePointerEvent(const rfb::Point& pos, int buttonMask)
  351. {
  352. if (!viewOnly) {
  353. if (pointerEventInterval == 0 || buttonMask != lastButtonMask) {
  354. cc->writer()->pointerEvent(pos, buttonMask);
  355. } else {
  356. if (!Fl::has_timeout(handlePointerTimeout, this))
  357. Fl::add_timeout((double)pointerEventInterval/1000.0,
  358. handlePointerTimeout, this);
  359. }
  360. lastPointerPos = pos;
  361. lastButtonMask = buttonMask;
  362. }
  363. }
  364. void Viewport::handlePointerTimeout(void *data)
  365. {
  366. Viewport *self = (Viewport *)data;
  367. assert(self);
  368. self->cc->writer()->pointerEvent(self->lastPointerPos, self->lastButtonMask);
  369. }
  370. rdr::U32 Viewport::translateKeyEvent(int keyCode, int origKeyCode, const char *keyText)
  371. {
  372. unsigned ucs;
  373. // First check for function keys
  374. if ((keyCode > FL_F) && (keyCode <= FL_F_Last))
  375. return XK_F1 + (keyCode - FL_F - 1);
  376. // Numpad numbers
  377. if ((keyCode >= (FL_KP + '0')) && (keyCode <= (FL_KP + '9')))
  378. return XK_KP_0 + (keyCode - (FL_KP + '0'));
  379. // FLTK does some special remapping of numpad keys when numlock is off
  380. if ((origKeyCode >= FL_KP) && (origKeyCode <= FL_KP_Last)) {
  381. switch (keyCode) {
  382. case FL_F+1:
  383. return XK_KP_F1;
  384. case FL_F+2:
  385. return XK_KP_F2;
  386. case FL_F+3:
  387. return XK_KP_F3;
  388. case FL_F+4:
  389. return XK_KP_F4;
  390. case FL_Home:
  391. return XK_KP_Home;
  392. case FL_Left:
  393. return XK_KP_Left;
  394. case FL_Up:
  395. return XK_KP_Up;
  396. case FL_Right:
  397. return XK_KP_Right;
  398. case FL_Down:
  399. return XK_KP_Down;
  400. case FL_Page_Up:
  401. return XK_KP_Page_Up;
  402. case FL_Page_Down:
  403. return XK_KP_Page_Down;
  404. case FL_End:
  405. return XK_KP_End;
  406. case FL_Insert:
  407. return XK_KP_Insert;
  408. case FL_Delete:
  409. return XK_KP_Delete;
  410. }
  411. }
  412. // Then other special keys
  413. switch (keyCode) {
  414. case FL_BackSpace:
  415. return XK_BackSpace;
  416. case FL_Tab:
  417. return XK_Tab;
  418. case FL_Enter:
  419. return XK_Return;
  420. case FL_Pause:
  421. return XK_Pause;
  422. case FL_Scroll_Lock:
  423. return XK_Scroll_Lock;
  424. case FL_Escape:
  425. return XK_Escape;
  426. case FL_Home:
  427. return XK_Home;
  428. case FL_Left:
  429. return XK_Left;
  430. case FL_Up:
  431. return XK_Up;
  432. case FL_Right:
  433. return XK_Right;
  434. case FL_Down:
  435. return XK_Down;
  436. case FL_Page_Up:
  437. return XK_Page_Up;
  438. case FL_Page_Down:
  439. return XK_Page_Down;
  440. case FL_End:
  441. return XK_End;
  442. case FL_Print:
  443. return XK_Print;
  444. case FL_Insert:
  445. return XK_Insert;
  446. case FL_Menu:
  447. return XK_Menu;
  448. case FL_Help:
  449. return XK_Help;
  450. case FL_Num_Lock:
  451. return XK_Num_Lock;
  452. case FL_Shift_L:
  453. return XK_Shift_L;
  454. case FL_Shift_R:
  455. return XK_Shift_R;
  456. case FL_Control_L:
  457. return XK_Control_L;
  458. case FL_Control_R:
  459. return XK_Control_R;
  460. case FL_Caps_Lock:
  461. return XK_Caps_Lock;
  462. case FL_Meta_L:
  463. return XK_Super_L;
  464. case FL_Meta_R:
  465. return XK_Super_R;
  466. case FL_Alt_L:
  467. return XK_Alt_L;
  468. case FL_Alt_R:
  469. return XK_Alt_R;
  470. case FL_Delete:
  471. return XK_Delete;
  472. case FL_KP_Enter:
  473. return XK_KP_Enter;
  474. case FL_KP + '=':
  475. return XK_KP_Equal;
  476. case FL_KP + '*':
  477. return XK_KP_Multiply;
  478. case FL_KP + '+':
  479. return XK_KP_Add;
  480. case FL_KP + ',':
  481. return XK_KP_Separator;
  482. case FL_KP + '-':
  483. return XK_KP_Subtract;
  484. case FL_KP + '.':
  485. return XK_KP_Decimal;
  486. case FL_KP + '/':
  487. return XK_KP_Divide;
  488. #ifdef HAVE_FLTK_MEDIAKEYS
  489. case FL_Volume_Down:
  490. return XF86XK_AudioLowerVolume;
  491. case FL_Volume_Mute:
  492. return XF86XK_AudioMute;
  493. case FL_Volume_Up:
  494. return XF86XK_AudioRaiseVolume;
  495. case FL_Media_Play:
  496. return XF86XK_AudioPlay;
  497. case FL_Media_Stop:
  498. return XF86XK_AudioStop;
  499. case FL_Media_Prev:
  500. return XF86XK_AudioPrev;
  501. case FL_Media_Next:
  502. return XF86XK_AudioNext;
  503. case FL_Home_Page:
  504. return XF86XK_HomePage;
  505. case FL_Mail:
  506. return XF86XK_Mail;
  507. case FL_Search:
  508. return XF86XK_Search;
  509. case FL_Back:
  510. return XF86XK_Back;
  511. case FL_Forward:
  512. return XF86XK_Forward;
  513. case FL_Stop:
  514. return XF86XK_Stop;
  515. case FL_Refresh:
  516. return XF86XK_Refresh;
  517. case FL_Sleep:
  518. return XF86XK_Sleep;
  519. case FL_Favorites:
  520. return XF86XK_Favorites;
  521. #endif
  522. case XK_ISO_Level3_Shift:
  523. // FLTK tends to let this one leak through on X11...
  524. return XK_ISO_Level3_Shift;
  525. case XK_Multi_key:
  526. // Same for this...
  527. return XK_Multi_key;
  528. }
  529. // Unknown special key?
  530. if (keyText[0] == '\0') {
  531. vlog.error(_("Unknown FLTK key code %d (0x%04x)"), keyCode, keyCode);
  532. return XK_VoidSymbol;
  533. }
  534. // Look up the symbol the key produces and translate that from Unicode
  535. // to a X11 keysym.
  536. if (fl_utf_nb_char((const unsigned char*)keyText, strlen(keyText)) != 1) {
  537. vlog.error(_("Multiple characters given for key code %d (0x%04x): '%s'"),
  538. keyCode, keyCode, keyText);
  539. return XK_VoidSymbol;
  540. }
  541. ucs = fl_utf8decode(keyText, NULL, NULL);
  542. return ucs2keysym(ucs);
  543. }
  544. void Viewport::handleKeyEvent(int keyCode, int origKeyCode, const char *keyText, bool down)
  545. {
  546. rdr::U32 keySym;
  547. if (viewOnly)
  548. return;
  549. // Because of the way keyboards work, we cannot expect to have the same
  550. // symbol on release as when pressed. This breaks the VNC protocol however,
  551. // so we need to keep track of what keysym a key _code_ generated on press
  552. // and send the same on release.
  553. if (!down) {
  554. DownMap::iterator iter;
  555. iter = downKeySym.find(origKeyCode);
  556. if (iter == downKeySym.end()) {
  557. vlog.error(_("Unexpected release of FLTK key code %d (0x%04x)"),
  558. origKeyCode, origKeyCode);
  559. return;
  560. }
  561. vlog.debug("Key released: 0x%04x => 0x%04x", origKeyCode, iter->second);
  562. cc->writer()->keyEvent(iter->second, false);
  563. downKeySym.erase(iter);
  564. return;
  565. }
  566. keySym = translateKeyEvent(keyCode, origKeyCode, keyText);
  567. if (keySym == XK_VoidSymbol)
  568. return;
  569. vlog.debug("Key pressed: 0x%04x (0x%04x) '%s' => 0x%04x",
  570. origKeyCode, keyCode, keyText, keySym);
  571. downKeySym[origKeyCode] = keySym;
  572. cc->writer()->keyEvent(keySym, down);
  573. }
  574. void Viewport::initContextMenu()
  575. {
  576. contextMenu->clear();
  577. contextMenu->add(_("Exit viewer"), 0, NULL, (void*)ID_EXIT, FL_MENU_DIVIDER);
  578. #ifdef HAVE_FLTK_FULLSCREEN
  579. contextMenu->add(_("Full screen"), 0, NULL, (void*)ID_FULLSCREEN, FL_MENU_DIVIDER);
  580. #endif
  581. contextMenu->add(_("Ctrl"), 0, NULL, (void*)ID_CTRL, FL_MENU_TOGGLE);
  582. contextMenu->add(_("Alt"), 0, NULL, (void*)ID_ALT, FL_MENU_TOGGLE);
  583. if (menuKeyCode) {
  584. char sendMenuKey[64];
  585. snprintf(sendMenuKey, 64, _("Send %s"), (const char *)menuKey);
  586. contextMenu->add(sendMenuKey, 0, NULL, (void*)ID_MENUKEY, 0);
  587. contextMenu->add("Secret shortcut menu key", menuKeyCode, NULL, (void*)ID_MENUKEY, FL_MENU_INVISIBLE);
  588. }
  589. contextMenu->add(_("Send Ctrl-Alt-Del"), 0, NULL, (void*)ID_CTRLALTDEL, FL_MENU_DIVIDER);
  590. contextMenu->add(_("Refresh screen"), 0, NULL, (void*)ID_REFRESH, FL_MENU_DIVIDER);
  591. contextMenu->add(_("Options..."), 0, NULL, (void*)ID_OPTIONS, 0);
  592. contextMenu->add(_("Connection info..."), 0, NULL, (void*)ID_INFO, 0);
  593. contextMenu->add(_("About TigerVNC viewer..."), 0, NULL, (void*)ID_ABOUT, FL_MENU_DIVIDER);
  594. contextMenu->add(_("Dismiss menu"), 0, NULL, (void*)ID_DISMISS, 0);
  595. }
  596. void Viewport::popupContextMenu()
  597. {
  598. const Fl_Menu_Item *m;
  599. char buffer[1024];
  600. // FIXME: Hacky workaround for the fact that menus grab and ungrab the
  601. // keyboard. Releasing focus here triggers some events on ungrab
  602. // that DesktopWindow can catch and trigger some voodoo.
  603. // See DesktopWindow::handle().
  604. if (window()->contains(Fl::focus()))
  605. Fl::focus(NULL);
  606. // Make sure the menu is reset to its initial state between goes or
  607. // it will start up highlighting the previously selected entry.
  608. contextMenu->value(-1);
  609. m = contextMenu->popup();
  610. if (m == NULL)
  611. return;
  612. switch (m->argument()) {
  613. case ID_EXIT:
  614. exit_vncviewer();
  615. break;
  616. #ifdef HAVE_FLTK_FULLSCREEN
  617. case ID_FULLSCREEN:
  618. if (window()->fullscreen_active())
  619. window()->fullscreen_off();
  620. else
  621. window()->fullscreen();
  622. break;
  623. #endif
  624. case ID_CTRL:
  625. if (!viewOnly)
  626. cc->writer()->keyEvent(XK_Control_L, m->value());
  627. break;
  628. case ID_ALT:
  629. if (!viewOnly)
  630. cc->writer()->keyEvent(XK_Alt_L, m->value());
  631. break;
  632. case ID_MENUKEY:
  633. if (!viewOnly) {
  634. handleKeyEvent(menuKeyCode, menuKeyCode, "", true);
  635. handleKeyEvent(menuKeyCode, menuKeyCode, "", false);
  636. }
  637. break;
  638. case ID_CTRLALTDEL:
  639. if (!viewOnly) {
  640. cc->writer()->keyEvent(XK_Control_L, true);
  641. cc->writer()->keyEvent(XK_Alt_L, true);
  642. cc->writer()->keyEvent(XK_Delete, true);
  643. cc->writer()->keyEvent(XK_Delete, false);
  644. cc->writer()->keyEvent(XK_Alt_L, false);
  645. cc->writer()->keyEvent(XK_Control_L, false);
  646. }
  647. break;
  648. case ID_REFRESH:
  649. cc->refreshFramebuffer();
  650. break;
  651. case ID_OPTIONS:
  652. OptionsDialog::showDialog();
  653. break;
  654. case ID_INFO:
  655. if (fltk_escape(cc->connectionInfo(), buffer, sizeof(buffer)) < sizeof(buffer)) {
  656. fl_message_title(_("VNC connection info"));
  657. fl_message(buffer);
  658. }
  659. break;
  660. case ID_ABOUT:
  661. about_vncviewer();
  662. break;
  663. case ID_DISMISS:
  664. // Don't need to do anything
  665. break;
  666. }
  667. }
  668. void Viewport::setMenuKey()
  669. {
  670. const char *menuKeyStr;
  671. menuKeyCode = 0;
  672. menuKeyStr = menuKey;
  673. if (menuKeyStr[0] == 'F') {
  674. int num = atoi(menuKeyStr + 1);
  675. if ((num >= 1) && (num <= 12))
  676. menuKeyCode = FL_F + num;
  677. }
  678. // Need to repopulate the context menu as it contains references to
  679. // the menu key
  680. initContextMenu();
  681. }
  682. void Viewport::handleOptions(void *data)
  683. {
  684. Viewport *self = (Viewport*)data;
  685. self->setMenuKey();
  686. }