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.

DesktopWindow.cxx 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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 <sys/time.h>
  26. #include <rfb/LogWriter.h>
  27. #include <rfb/CMsgWriter.h>
  28. #include "DesktopWindow.h"
  29. #include "OptionsDialog.h"
  30. #include "i18n.h"
  31. #include "parameters.h"
  32. #include "vncviewer.h"
  33. #include "CConn.h"
  34. #include "Surface.h"
  35. #include "Viewport.h"
  36. #include <FL/Fl.H>
  37. #include <FL/Fl_Image_Surface.H>
  38. #include <FL/Fl_Scrollbar.H>
  39. #include <FL/fl_draw.H>
  40. #include <FL/x.H>
  41. #ifdef WIN32
  42. #include "win32.h"
  43. #endif
  44. #ifdef __APPLE__
  45. #include "cocoa.h"
  46. #endif
  47. #define EDGE_SCROLL_SIZE 32
  48. #define EDGE_SCROLL_SPEED 20
  49. using namespace rfb;
  50. static rfb::LogWriter vlog("DesktopWindow");
  51. DesktopWindow::DesktopWindow(int w, int h, const char *name,
  52. const rfb::PixelFormat& serverPF,
  53. CConn* cc_)
  54. : Fl_Window(w, h), cc(cc_), offscreen(NULL), overlay(NULL),
  55. firstUpdate(true),
  56. delayedFullscreen(false), delayedDesktopSize(false),
  57. statsLastFrame(0), statsLastPixels(0), statsLastPosition(0),
  58. statsGraph(NULL)
  59. {
  60. Fl_Group* group;
  61. // Dummy group to prevent FLTK from moving our widgets around
  62. group = new Fl_Group(0, 0, w, h);
  63. group->resizable(NULL);
  64. resizable(group);
  65. viewport = new Viewport(w, h, serverPF, cc);
  66. // Position will be adjusted later
  67. hscroll = new Fl_Scrollbar(0, 0, 0, 0);
  68. vscroll = new Fl_Scrollbar(0, 0, 0, 0);
  69. hscroll->type(FL_HORIZONTAL);
  70. hscroll->callback(handleScroll, this);
  71. vscroll->callback(handleScroll, this);
  72. group->end();
  73. callback(handleClose, this);
  74. setName(name);
  75. OptionsDialog::addCallback(handleOptions, this);
  76. // Hack. See below...
  77. Fl::event_dispatch(&fltkHandle);
  78. // Support for -geometry option. Note that although we do support
  79. // negative coordinates, we do not support -XOFF-YOFF (ie
  80. // coordinates relative to the right edge / bottom edge) at this
  81. // time.
  82. int geom_x = 0, geom_y = 0;
  83. if (strcmp(geometry, "") != 0) {
  84. int matched;
  85. matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
  86. if (matched == 2) {
  87. force_position(1);
  88. } else {
  89. int geom_w, geom_h;
  90. matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
  91. switch (matched) {
  92. case 4:
  93. force_position(1);
  94. /* fall through */
  95. case 2:
  96. w = geom_w;
  97. h = geom_h;
  98. break;
  99. default:
  100. geom_x = geom_y = 0;
  101. vlog.error(_("Invalid geometry specified!"));
  102. }
  103. }
  104. }
  105. #ifdef __APPLE__
  106. // On OS X we can do the maximize thing properly before the
  107. // window is showned. Other platforms handled further down...
  108. if (maximize) {
  109. int dummy;
  110. Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
  111. }
  112. #endif
  113. if (force_position()) {
  114. resize(geom_x, geom_y, w, h);
  115. } else {
  116. size(w, h);
  117. }
  118. if (fullScreen) {
  119. // Hack: Window managers seem to be rather crappy at respecting
  120. // fullscreen hints on initial windows. So on X11 we'll have to
  121. // wait until after we've been mapped.
  122. #if defined(WIN32) || defined(__APPLE__)
  123. fullscreen_on();
  124. #else
  125. delayedFullscreen = true;
  126. #endif
  127. }
  128. show();
  129. // Full screen events are not sent out for a hidden window,
  130. // so send a fake one here to set up things properly.
  131. if (fullscreen_active())
  132. handle(FL_FULLSCREEN);
  133. // Unfortunately, current FLTK does not allow us to set the
  134. // maximized property on Windows and X11 before showing the window.
  135. // See STR #2083 and STR #2178
  136. #ifndef __APPLE__
  137. if (maximize) {
  138. maximizeWindow();
  139. }
  140. #endif
  141. // Adjust layout now that we're visible and know our final size
  142. repositionWidgets();
  143. if (delayedFullscreen) {
  144. // Hack: Fullscreen requests may be ignored, so we need a timeout for
  145. // when we should stop waiting. We also really need to wait for the
  146. // resize, which can come after the fullscreen event.
  147. Fl::add_timeout(0.5, handleFullscreenTimeout, this);
  148. fullscreen_on();
  149. }
  150. // Throughput graph for debugging
  151. if (vlog.getLevel() >= LogWriter::LEVEL_DEBUG) {
  152. memset(&stats, 0, sizeof(stats));
  153. Fl::add_timeout(0, handleStatsTimeout, this);
  154. }
  155. // Show hint about menu key
  156. Fl::add_timeout(0.5, menuOverlay, this);
  157. }
  158. DesktopWindow::~DesktopWindow()
  159. {
  160. // Unregister all timeouts in case they get a change tro trigger
  161. // again later when this object is already gone.
  162. Fl::remove_timeout(handleGrab, this);
  163. Fl::remove_timeout(handleResizeTimeout, this);
  164. Fl::remove_timeout(handleFullscreenTimeout, this);
  165. Fl::remove_timeout(handleEdgeScroll, this);
  166. Fl::remove_timeout(handleStatsTimeout, this);
  167. Fl::remove_timeout(menuOverlay, this);
  168. Fl::remove_timeout(updateOverlay, this);
  169. OptionsDialog::removeCallback(handleOptions);
  170. delete overlay;
  171. delete offscreen;
  172. delete statsGraph;
  173. // FLTK automatically deletes all child widgets, so we shouldn't touch
  174. // them ourselves here
  175. }
  176. const rfb::PixelFormat &DesktopWindow::getPreferredPF()
  177. {
  178. return viewport->getPreferredPF();
  179. }
  180. void DesktopWindow::setName(const char *name)
  181. {
  182. CharArray windowNameStr;
  183. windowNameStr.replaceBuf(new char[256]);
  184. snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
  185. copy_label(windowNameStr.buf);
  186. }
  187. // Copy the areas of the framebuffer that have been changed (damaged)
  188. // to the displayed window.
  189. void DesktopWindow::updateWindow()
  190. {
  191. if (firstUpdate) {
  192. if (cc->cp.supportsSetDesktopSize) {
  193. // Hack: Wait until we're in the proper mode and position until
  194. // resizing things, otherwise we might send the wrong thing.
  195. if (delayedFullscreen)
  196. delayedDesktopSize = true;
  197. else
  198. handleDesktopSize();
  199. }
  200. firstUpdate = false;
  201. }
  202. viewport->updateWindow();
  203. }
  204. void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
  205. {
  206. if ((new_w == viewport->w()) && (new_h == viewport->h()))
  207. return;
  208. // If we're letting the viewport match the window perfectly, then
  209. // keep things that way for the new size, otherwise just keep things
  210. // like they are.
  211. if (!fullscreen_active()) {
  212. if ((w() == viewport->w()) && (h() == viewport->h()))
  213. size(new_w, new_h);
  214. else {
  215. // Make sure the window isn't too big. We do this manually because
  216. // we have to disable the window size restriction (and it isn't
  217. // entirely trustworthy to begin with).
  218. if ((w() > new_w) || (h() > new_h))
  219. size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
  220. }
  221. }
  222. viewport->size(new_w, new_h);
  223. repositionWidgets();
  224. }
  225. void DesktopWindow::setCursor(int width, int height,
  226. const rfb::Point& hotspot,
  227. const rdr::U8* data)
  228. {
  229. viewport->setCursor(width, height, hotspot, data);
  230. }
  231. void DesktopWindow::draw()
  232. {
  233. bool redraw;
  234. int X, Y, W, H;
  235. // X11 needs an off screen buffer for compositing to avoid flicker,
  236. // and alpha blending doesn't work for windows on Win32
  237. #if !defined(__APPLE__)
  238. // Adjust offscreen surface dimensions
  239. if ((offscreen == NULL) ||
  240. (offscreen->width() != w()) || (offscreen->height() != h())) {
  241. delete offscreen;
  242. offscreen = new Surface(w(), h());
  243. }
  244. #endif
  245. // Active area inside scrollbars
  246. W = w() - (vscroll->visible() ? vscroll->w() : 0);
  247. H = h() - (hscroll->visible() ? hscroll->h() : 0);
  248. // Full redraw?
  249. redraw = (damage() & ~FL_DAMAGE_CHILD);
  250. // Simplify the clip region to a simple rectangle in order to
  251. // properly draw all the layers even if they only partially overlap
  252. if (redraw)
  253. X = Y = 0;
  254. else
  255. fl_clip_box(0, 0, W, H, X, Y, W, H);
  256. fl_push_no_clip();
  257. fl_push_clip(X, Y, W, H);
  258. // Redraw background only on full redraws
  259. if (redraw) {
  260. if (offscreen)
  261. offscreen->clear(40, 40, 40);
  262. else
  263. fl_rectf(0, 0, W, H, 40, 40, 40);
  264. }
  265. if (offscreen) {
  266. viewport->draw(offscreen);
  267. viewport->clear_damage();
  268. } else {
  269. if (redraw)
  270. draw_child(*viewport);
  271. else
  272. update_child(*viewport);
  273. }
  274. // Debug graph (if active)
  275. if (statsGraph) {
  276. int ox, oy, ow, oh;
  277. ox = X = w() - statsGraph->width() - 30;
  278. oy = Y = h() - statsGraph->height() - 30;
  279. ow = statsGraph->width();
  280. oh = statsGraph->height();
  281. fl_clip_box(ox, oy, ow, oh, ox, oy, ow, oh);
  282. if ((ow != 0) && (oh != 0)) {
  283. if (offscreen)
  284. statsGraph->blend(offscreen, ox - X, oy - Y, ox, oy, ow, oh, 204);
  285. else
  286. statsGraph->blend(ox - X, oy - Y, ox, oy, ow, oh, 204);
  287. }
  288. }
  289. // Overlay (if active)
  290. if (overlay) {
  291. int ox, oy, ow, oh;
  292. ox = X = (w() - overlay->width()) / 2;
  293. oy = Y = 50;
  294. ow = overlay->width();
  295. oh = overlay->height();
  296. fl_clip_box(ox, oy, ow, oh, ox, oy, ow, oh);
  297. if ((ow != 0) && (oh != 0)) {
  298. if (offscreen)
  299. overlay->blend(offscreen, ox - X, oy - Y, ox, oy, ow, oh, overlayAlpha);
  300. else
  301. overlay->blend(ox - X, oy - Y, ox, oy, ow, oh, overlayAlpha);
  302. }
  303. }
  304. // Flush offscreen surface to screen
  305. if (offscreen) {
  306. fl_clip_box(0, 0, w(), h(), X, Y, W, H);
  307. offscreen->draw(X, Y, X, Y, W, H);
  308. }
  309. fl_pop_clip();
  310. fl_pop_clip();
  311. // Finally the scrollbars
  312. if (redraw) {
  313. draw_child(*hscroll);
  314. draw_child(*vscroll);
  315. } else {
  316. update_child(*hscroll);
  317. update_child(*vscroll);
  318. }
  319. }
  320. void DesktopWindow::resize(int x, int y, int w, int h)
  321. {
  322. bool resizing;
  323. #if ! (defined(WIN32) || defined(__APPLE__))
  324. // X11 window managers will treat a resize to cover the entire
  325. // monitor as a request to go full screen. Make sure we avoid this.
  326. if (!fullscreen_active()) {
  327. bool resize_req;
  328. // If there is no X11 window, then this must be a resize request,
  329. // not a notification from the X server.
  330. if (!shown())
  331. resize_req = true;
  332. else {
  333. // Otherwise we need to get the real window coordinates to tell
  334. // the difference
  335. XWindowAttributes actual;
  336. Window cr;
  337. int wx, wy;
  338. XGetWindowAttributes(fl_display, fl_xid(this), &actual);
  339. XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
  340. 0, 0, &wx, &wy, &cr);
  341. // Actual resize request?
  342. if ((wx != x) || (wy != y) ||
  343. (actual.width != w) || (actual.height != h))
  344. resize_req = true;
  345. else
  346. resize_req = false;
  347. }
  348. if (resize_req) {
  349. for (int i = 0;i < Fl::screen_count();i++) {
  350. int sx, sy, sw, sh;
  351. Fl::screen_xywh(sx, sy, sw, sh, i);
  352. if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
  353. vlog.info(_("Adjusting window size to avoid accidental full screen request"));
  354. // Assume a panel of some form and adjust the height
  355. y += 20;
  356. h -= 40;
  357. }
  358. }
  359. }
  360. }
  361. #endif
  362. if ((this->w() != w) || (this->h() != h))
  363. resizing = true;
  364. else
  365. resizing = false;
  366. Fl_Window::resize(x, y, w, h);
  367. if (resizing) {
  368. // Try to get the remote size to match our window size, provided
  369. // the following conditions are true:
  370. //
  371. // a) The user has this feature turned on
  372. // b) The server supports it
  373. // c) We're not still waiting for a chance to handle DesktopSize
  374. // d) We're not still waiting for startup fullscreen to kick in
  375. //
  376. if (not firstUpdate and not delayedFullscreen and
  377. ::remoteResize and cc->cp.supportsSetDesktopSize) {
  378. // We delay updating the remote desktop as we tend to get a flood
  379. // of resize events as the user is dragging the window.
  380. Fl::remove_timeout(handleResizeTimeout, this);
  381. Fl::add_timeout(0.5, handleResizeTimeout, this);
  382. }
  383. repositionWidgets();
  384. }
  385. }
  386. void DesktopWindow::menuOverlay(void* data)
  387. {
  388. DesktopWindow *self;
  389. self = (DesktopWindow*)data;
  390. self->setOverlay(_("Press %s to open the context menu"),
  391. (const char*)menuKey);
  392. }
  393. void DesktopWindow::setOverlay(const char* text, ...)
  394. {
  395. va_list ap;
  396. char textbuf[1024];
  397. Fl_Image_Surface *surface;
  398. Fl_RGB_Image* imageText;
  399. Fl_RGB_Image* image;
  400. unsigned char* buffer;
  401. int x, y;
  402. int w, h;
  403. unsigned char* a;
  404. const unsigned char* b;
  405. delete overlay;
  406. Fl::remove_timeout(updateOverlay, this);
  407. va_start(ap, text);
  408. vsnprintf(textbuf, sizeof(textbuf), text, ap);
  409. textbuf[sizeof(textbuf)-1] = '\0';
  410. va_end(ap);
  411. #if !defined(WIN32) && !defined(__APPLE__)
  412. // FLTK < 1.3.5 crashes if fl_gc is unset
  413. if (!fl_gc)
  414. fl_gc = XDefaultGC(fl_display, 0);
  415. #endif
  416. fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2);
  417. w = 0;
  418. fl_measure(textbuf, w, h);
  419. // Margins
  420. w += 80;
  421. h += 40;
  422. surface = new Fl_Image_Surface(w, h);
  423. surface->set_current();
  424. fl_rectf(0, 0, w, h, 0, 0, 0);
  425. fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2);
  426. fl_color(FL_WHITE);
  427. fl_draw(textbuf, 40, 20 + fl_height() - fl_descent());
  428. imageText = surface->image();
  429. delete surface;
  430. Fl_Display_Device::display_device()->set_current();
  431. buffer = new unsigned char[w * h * 4];
  432. image = new Fl_RGB_Image(buffer, w, h, 4);
  433. a = buffer;
  434. for (x = 0;x < image->w() * image->h();x++) {
  435. a[0] = a[1] = a[2] = 0x40;
  436. a[3] = 0xcc;
  437. a += 4;
  438. }
  439. a = buffer;
  440. b = (const unsigned char*)imageText->data()[0];
  441. for (y = 0;y < h;y++) {
  442. for (x = 0;x < w;x++) {
  443. unsigned char alpha;
  444. alpha = *b;
  445. a[0] = (unsigned)a[0] * (255 - alpha) / 255 + alpha;
  446. a[1] = (unsigned)a[1] * (255 - alpha) / 255 + alpha;
  447. a[2] = (unsigned)a[2] * (255 - alpha) / 255 + alpha;
  448. a[3] = 255 - (255 - a[3]) * (255 - alpha) / 255;
  449. a += 4;
  450. b += imageText->d();
  451. }
  452. if (imageText->ld() != 0)
  453. b += imageText->ld() - w * imageText->d();
  454. }
  455. delete imageText;
  456. x = (this->w() - image->w()) / 2;
  457. y = 50;
  458. w = image->w();
  459. h = image->h();
  460. overlay = new Surface(image);
  461. overlayAlpha = 0;
  462. gettimeofday(&overlayStart, NULL);
  463. delete image;
  464. Fl::add_timeout(1.0/60, updateOverlay, this);
  465. }
  466. void DesktopWindow::updateOverlay(void *data)
  467. {
  468. DesktopWindow *self;
  469. unsigned elapsed;
  470. self = (DesktopWindow*)data;
  471. elapsed = msSince(&self->overlayStart);
  472. if (elapsed < 500) {
  473. self->overlayAlpha = (unsigned)255 * elapsed / 500;
  474. Fl::add_timeout(1.0/60, updateOverlay, self);
  475. } else if (elapsed < 3500) {
  476. self->overlayAlpha = 255;
  477. Fl::add_timeout(3.0, updateOverlay, self);
  478. } else if (elapsed < 4000) {
  479. self->overlayAlpha = (unsigned)255 * (4000 - elapsed) / 500;
  480. Fl::add_timeout(1.0/60, updateOverlay, self);
  481. } else {
  482. delete self->overlay;
  483. self->overlay = NULL;
  484. }
  485. self->damage(FL_DAMAGE_USER1);
  486. }
  487. int DesktopWindow::handle(int event)
  488. {
  489. switch (event) {
  490. case FL_FULLSCREEN:
  491. fullScreen.setParam(fullscreen_active());
  492. // Update scroll bars
  493. repositionWidgets();
  494. if (!fullscreenSystemKeys)
  495. break;
  496. if (fullscreen_active())
  497. grabKeyboard();
  498. else
  499. ungrabKeyboard();
  500. break;
  501. case FL_ENTER:
  502. case FL_LEAVE:
  503. case FL_DRAG:
  504. case FL_MOVE:
  505. if (fullscreen_active()) {
  506. if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
  507. ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
  508. ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
  509. ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
  510. if (!Fl::has_timeout(handleEdgeScroll, this))
  511. Fl::add_timeout(0.1, handleEdgeScroll, this);
  512. }
  513. }
  514. // Continue processing so that the viewport also gets mouse events
  515. break;
  516. }
  517. return Fl_Window::handle(event);
  518. }
  519. int DesktopWindow::fltkHandle(int event, Fl_Window *win)
  520. {
  521. int ret;
  522. ret = Fl::handle_(event, win);
  523. // This is hackish and the result of the dodgy focus handling in FLTK.
  524. // The basic problem is that FLTK's view of focus and the system's tend
  525. // to differ, and as a result we do not see all the FL_FOCUS events we
  526. // need. Fortunately we can grab them here...
  527. DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
  528. if (dw && fullscreenSystemKeys) {
  529. switch (event) {
  530. case FL_FOCUS:
  531. // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
  532. // some issues we need to work around:
  533. // a) Fl::grab(0) on X11 will release the keyboard grab for us.
  534. // b) Gaining focus on the system level causes FLTK to switch
  535. // window level on OS X.
  536. if (dw->fullscreen_active())
  537. dw->grabKeyboard();
  538. break;
  539. case FL_UNFOCUS:
  540. // FIXME: We need to relinquish control when the entire window loses
  541. // focus as it is very tied to this specific window on some
  542. // platforms and we want to be able to open subwindows.
  543. dw->ungrabKeyboard();
  544. break;
  545. }
  546. }
  547. return ret;
  548. }
  549. void DesktopWindow::fullscreen_on()
  550. {
  551. if (not fullScreenAllMonitors)
  552. fullscreen_screens(-1, -1, -1, -1);
  553. else {
  554. int top, bottom, left, right;
  555. int top_y, bottom_y, left_x, right_x;
  556. int sx, sy, sw, sh;
  557. top = bottom = left = right = 0;
  558. Fl::screen_xywh(sx, sy, sw, sh, 0);
  559. top_y = sy;
  560. bottom_y = sy + sh;
  561. left_x = sx;
  562. right_x = sx + sw;
  563. for (int i = 1;i < Fl::screen_count();i++) {
  564. Fl::screen_xywh(sx, sy, sw, sh, i);
  565. if (sy < top_y) {
  566. top = i;
  567. top_y = sy;
  568. }
  569. if ((sy + sh) > bottom_y) {
  570. bottom = i;
  571. bottom_y = sy + sh;
  572. }
  573. if (sx < left_x) {
  574. left = i;
  575. left_x = sx;
  576. }
  577. if ((sx + sw) > right_x) {
  578. right = i;
  579. right_x = sx + sw;
  580. }
  581. }
  582. fullscreen_screens(top, bottom, left, right);
  583. }
  584. fullscreen();
  585. }
  586. void DesktopWindow::grabKeyboard()
  587. {
  588. // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
  589. // correct widget regardless of which low level window got the system
  590. // event.
  591. // FIXME: Push this stuff into FLTK.
  592. #if defined(WIN32)
  593. int ret;
  594. ret = win32_enable_lowlevel_keyboard(fl_xid(this));
  595. if (ret != 0)
  596. vlog.error(_("Failure grabbing keyboard"));
  597. #elif defined(__APPLE__)
  598. int ret;
  599. ret = cocoa_capture_display(this, fullScreenAllMonitors);
  600. if (ret != 0)
  601. vlog.error(_("Failure grabbing keyboard"));
  602. #else
  603. int ret;
  604. ret = XGrabKeyboard(fl_display, fl_xid(this), True,
  605. GrabModeAsync, GrabModeAsync, CurrentTime);
  606. if (ret) {
  607. if (ret == AlreadyGrabbed) {
  608. // It seems like we can race with the WM in some cases.
  609. // Try again in a bit.
  610. if (!Fl::has_timeout(handleGrab, this))
  611. Fl::add_timeout(0.500, handleGrab, this);
  612. } else {
  613. vlog.error(_("Failure grabbing keyboard"));
  614. }
  615. }
  616. // We also need to grab the pointer as some WMs like to grab buttons
  617. // combined with modifies (e.g. Alt+Button0 in metacity).
  618. ret = XGrabPointer(fl_display, fl_xid(this), True,
  619. ButtonPressMask|ButtonReleaseMask|
  620. ButtonMotionMask|PointerMotionMask,
  621. GrabModeAsync, GrabModeAsync,
  622. None, None, CurrentTime);
  623. if (ret)
  624. vlog.error(_("Failure grabbing mouse"));
  625. #endif
  626. }
  627. void DesktopWindow::ungrabKeyboard()
  628. {
  629. Fl::remove_timeout(handleGrab, this);
  630. #if defined(WIN32)
  631. win32_disable_lowlevel_keyboard(fl_xid(this));
  632. #elif defined(__APPLE__)
  633. cocoa_release_display(this);
  634. #else
  635. // FLTK has a grab so lets not mess with it
  636. if (Fl::grab())
  637. return;
  638. XUngrabPointer(fl_display, fl_event_time);
  639. XUngrabKeyboard(fl_display, fl_event_time);
  640. #endif
  641. }
  642. void DesktopWindow::handleGrab(void *data)
  643. {
  644. DesktopWindow *self = (DesktopWindow*)data;
  645. assert(self);
  646. if (!fullscreenSystemKeys)
  647. return;
  648. if (!self->fullscreen_active())
  649. return;
  650. self->grabKeyboard();
  651. }
  652. #define _NET_WM_STATE_ADD 1 /* add/set property */
  653. void DesktopWindow::maximizeWindow()
  654. {
  655. #if defined(WIN32)
  656. // We cannot use ShowWindow() in full screen mode as it will
  657. // resize things implicitly. Fortunately modifying the style
  658. // directly results in a maximized state once we leave full screen.
  659. if (fullscreen_active()) {
  660. WINDOWINFO wi;
  661. wi.cbSize = sizeof(WINDOWINFO);
  662. GetWindowInfo(fl_xid(this), &wi);
  663. SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
  664. } else
  665. ShowWindow(fl_xid(this), SW_MAXIMIZE);
  666. #elif defined(__APPLE__)
  667. // OS X is somewhat strange and does not really have a concept of a
  668. // maximized window, so we can simply resize the window to the workarea.
  669. // Note that we shouldn't do this whilst in full screen as that will
  670. // incorrectly adjust things.
  671. if (fullscreen_active())
  672. return;
  673. int X, Y, W, H;
  674. Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
  675. size(W, H);
  676. #else
  677. // X11
  678. fl_open_display();
  679. Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
  680. Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
  681. Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
  682. XEvent e;
  683. e.xany.type = ClientMessage;
  684. e.xany.window = fl_xid(this);
  685. e.xclient.message_type = net_wm_state;
  686. e.xclient.format = 32;
  687. e.xclient.data.l[0] = _NET_WM_STATE_ADD;
  688. e.xclient.data.l[1] = net_wm_state_maximized_vert;
  689. e.xclient.data.l[2] = net_wm_state_maximized_horz;
  690. e.xclient.data.l[3] = 0;
  691. e.xclient.data.l[4] = 0;
  692. XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
  693. #endif
  694. }
  695. void DesktopWindow::handleDesktopSize()
  696. {
  697. if (strcmp(desktopSize, "") != 0) {
  698. int width, height;
  699. // An explicit size has been requested
  700. if (sscanf(desktopSize, "%dx%d", &width, &height) != 2)
  701. return;
  702. remoteResize(width, height);
  703. } else if (::remoteResize) {
  704. // No explicit size, but remote resizing is on so make sure it
  705. // matches whatever size the window ended up being
  706. remoteResize(w(), h());
  707. }
  708. }
  709. void DesktopWindow::handleResizeTimeout(void *data)
  710. {
  711. DesktopWindow *self = (DesktopWindow *)data;
  712. assert(self);
  713. self->remoteResize(self->w(), self->h());
  714. }
  715. void DesktopWindow::remoteResize(int width, int height)
  716. {
  717. ScreenSet layout;
  718. ScreenSet::iterator iter;
  719. if (!fullscreen_active() || (width > w()) || (height > h())) {
  720. // In windowed mode (or the framebuffer is so large that we need
  721. // to scroll) we just report a single virtual screen that covers
  722. // the entire framebuffer.
  723. layout = cc->cp.screenLayout;
  724. // Not sure why we have no screens, but adding a new one should be
  725. // safe as there is nothing to conflict with...
  726. if (layout.num_screens() == 0)
  727. layout.add_screen(rfb::Screen());
  728. else if (layout.num_screens() != 1) {
  729. // More than one screen. Remove all but the first (which we
  730. // assume is the "primary").
  731. while (true) {
  732. iter = layout.begin();
  733. ++iter;
  734. if (iter == layout.end())
  735. break;
  736. layout.remove_screen(iter->id);
  737. }
  738. }
  739. // Resize the remaining single screen to the complete framebuffer
  740. layout.begin()->dimensions.tl.x = 0;
  741. layout.begin()->dimensions.tl.y = 0;
  742. layout.begin()->dimensions.br.x = width;
  743. layout.begin()->dimensions.br.y = height;
  744. } else {
  745. int i;
  746. rdr::U32 id;
  747. int sx, sy, sw, sh;
  748. rfb::Rect viewport_rect, screen_rect;
  749. // In full screen we report all screens that are fully covered.
  750. viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
  751. width, height);
  752. // If we can find a matching screen in the existing set, we use
  753. // that, otherwise we create a brand new screen.
  754. //
  755. // FIXME: We should really track screens better so we can handle
  756. // a resized one.
  757. //
  758. for (i = 0;i < Fl::screen_count();i++) {
  759. Fl::screen_xywh(sx, sy, sw, sh, i);
  760. // Check that the screen is fully inside the framebuffer
  761. screen_rect.setXYWH(sx, sy, sw, sh);
  762. if (!screen_rect.enclosed_by(viewport_rect))
  763. continue;
  764. // Adjust the coordinates so they are relative to our viewport
  765. sx -= viewport_rect.tl.x;
  766. sy -= viewport_rect.tl.y;
  767. // Look for perfectly matching existing screen...
  768. for (iter = cc->cp.screenLayout.begin();
  769. iter != cc->cp.screenLayout.end(); ++iter) {
  770. if ((iter->dimensions.tl.x == sx) &&
  771. (iter->dimensions.tl.y == sy) &&
  772. (iter->dimensions.width() == sw) &&
  773. (iter->dimensions.height() == sh))
  774. break;
  775. }
  776. // Found it?
  777. if (iter != cc->cp.screenLayout.end()) {
  778. layout.add_screen(*iter);
  779. continue;
  780. }
  781. // Need to add a new one, which means we need to find an unused id
  782. while (true) {
  783. id = rand();
  784. for (iter = cc->cp.screenLayout.begin();
  785. iter != cc->cp.screenLayout.end(); ++iter) {
  786. if (iter->id == id)
  787. break;
  788. }
  789. if (iter == cc->cp.screenLayout.end())
  790. break;
  791. }
  792. layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
  793. }
  794. // If the viewport doesn't match a physical screen, then we might
  795. // end up with no screens in the layout. Add a fake one...
  796. if (layout.num_screens() == 0)
  797. layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
  798. }
  799. // Do we actually change anything?
  800. if ((width == cc->cp.width) &&
  801. (height == cc->cp.height) &&
  802. (layout == cc->cp.screenLayout))
  803. return;
  804. char buffer[2048];
  805. vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
  806. cc->cp.width, cc->cp.height, width, height);
  807. layout.print(buffer, sizeof(buffer));
  808. vlog.debug("%s", buffer);
  809. if (!layout.validate(width, height)) {
  810. vlog.error(_("Invalid screen layout computed for resize request!"));
  811. return;
  812. }
  813. cc->writer()->writeSetDesktopSize(width, height, layout);
  814. }
  815. void DesktopWindow::repositionWidgets()
  816. {
  817. int new_x, new_y;
  818. // Viewport position
  819. new_x = viewport->x();
  820. new_y = viewport->y();
  821. if (w() > viewport->w())
  822. new_x = (w() - viewport->w()) / 2;
  823. else {
  824. if (viewport->x() > 0)
  825. new_x = 0;
  826. else if (w() > (viewport->x() + viewport->w()))
  827. new_x = w() - viewport->w();
  828. }
  829. // Same thing for y axis
  830. if (h() > viewport->h())
  831. new_y = (h() - viewport->h()) / 2;
  832. else {
  833. if (viewport->y() > 0)
  834. new_y = 0;
  835. else if (h() > (viewport->y() + viewport->h()))
  836. new_y = h() - viewport->h();
  837. }
  838. if ((new_x != viewport->x()) || (new_y != viewport->y())) {
  839. viewport->position(new_x, new_y);
  840. damage(FL_DAMAGE_SCROLL);
  841. }
  842. // Scrollbars visbility
  843. if (!fullscreen_active() && (w() < viewport->w()))
  844. hscroll->show();
  845. else
  846. hscroll->hide();
  847. if (!fullscreen_active() && (h() < viewport->h()))
  848. vscroll->show();
  849. else
  850. vscroll->hide();
  851. // Scrollbars positions
  852. hscroll->resize(0, h() - Fl::scrollbar_size(),
  853. w() - (vscroll->visible() ? Fl::scrollbar_size() : 0),
  854. Fl::scrollbar_size());
  855. vscroll->resize(w() - Fl::scrollbar_size(), 0,
  856. Fl::scrollbar_size(),
  857. h() - (hscroll->visible() ? Fl::scrollbar_size() : 0));
  858. // Scrollbars range
  859. hscroll->value(-viewport->x(),
  860. w() - (vscroll->visible() ? vscroll->w() : 0),
  861. 0, viewport->w());
  862. vscroll->value(-viewport->y(),
  863. h() - (hscroll->visible() ? hscroll->h() : 0),
  864. 0, viewport->h());
  865. hscroll->value(hscroll->clamp(hscroll->value()));
  866. vscroll->value(vscroll->clamp(vscroll->value()));
  867. }
  868. void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
  869. {
  870. exit_vncviewer();
  871. }
  872. void DesktopWindow::handleOptions(void *data)
  873. {
  874. DesktopWindow *self = (DesktopWindow*)data;
  875. if (self->fullscreen_active() && fullscreenSystemKeys)
  876. self->grabKeyboard();
  877. else
  878. self->ungrabKeyboard();
  879. if (fullScreen && !self->fullscreen_active())
  880. self->fullscreen_on();
  881. else if (!fullScreen && self->fullscreen_active())
  882. self->fullscreen_off();
  883. }
  884. void DesktopWindow::handleFullscreenTimeout(void *data)
  885. {
  886. DesktopWindow *self = (DesktopWindow *)data;
  887. assert(self);
  888. self->delayedFullscreen = false;
  889. if (self->delayedDesktopSize) {
  890. self->handleDesktopSize();
  891. self->delayedDesktopSize = false;
  892. }
  893. }
  894. void DesktopWindow::scrollTo(int x, int y)
  895. {
  896. x = hscroll->clamp(x);
  897. y = vscroll->clamp(y);
  898. hscroll->value(x);
  899. vscroll->value(y);
  900. if (!hscroll->visible())
  901. x = -viewport->x();
  902. if (!vscroll->visible())
  903. y = -viewport->y();
  904. // Scrollbar position results in inverse movement of
  905. // the viewport widget
  906. x = -x;
  907. y = -y;
  908. if ((viewport->x() == x) && (viewport->y() == y))
  909. return;
  910. viewport->position(x, y);
  911. damage(FL_DAMAGE_SCROLL);
  912. }
  913. void DesktopWindow::handleScroll(Fl_Widget *widget, void *data)
  914. {
  915. DesktopWindow *self = (DesktopWindow *)data;
  916. self->scrollTo(self->hscroll->value(), self->vscroll->value());
  917. }
  918. void DesktopWindow::handleEdgeScroll(void *data)
  919. {
  920. DesktopWindow *self = (DesktopWindow *)data;
  921. int mx, my;
  922. int dx, dy;
  923. assert(self);
  924. if (!self->fullscreen_active())
  925. return;
  926. mx = Fl::event_x();
  927. my = Fl::event_y();
  928. dx = dy = 0;
  929. // Clamp mouse position in case it is outside the window
  930. if (mx < 0)
  931. mx = 0;
  932. if (mx > self->w())
  933. mx = self->w();
  934. if (my < 0)
  935. my = 0;
  936. if (my > self->h())
  937. my = self->h();
  938. if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
  939. dx = EDGE_SCROLL_SPEED -
  940. EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
  941. if ((self->viewport->x() + self->viewport->w() > self->w()) &&
  942. (mx > self->w() - EDGE_SCROLL_SIZE))
  943. dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
  944. EDGE_SCROLL_SPEED;
  945. if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
  946. dy = EDGE_SCROLL_SPEED -
  947. EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
  948. if ((self->viewport->y() + self->viewport->h() > self->h()) &&
  949. (my > self->h() - EDGE_SCROLL_SIZE))
  950. dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
  951. EDGE_SCROLL_SPEED;
  952. if ((dx == 0) && (dy == 0))
  953. return;
  954. self->scrollTo(self->hscroll->value() + dx, self->vscroll->value() + dy);
  955. Fl::repeat_timeout(0.1, handleEdgeScroll, data);
  956. }
  957. void DesktopWindow::handleStatsTimeout(void *data)
  958. {
  959. DesktopWindow *self = (DesktopWindow*)data;
  960. const size_t statsCount = sizeof(self->stats)/sizeof(self->stats[0]);
  961. unsigned frame, pixels, pos;
  962. unsigned elapsed;
  963. const unsigned statsWidth = 200;
  964. const unsigned statsHeight = 100;
  965. const unsigned graphWidth = statsWidth - 10;
  966. const unsigned graphHeight = statsHeight - 25;
  967. Fl_Image_Surface *surface;
  968. Fl_RGB_Image *image;
  969. unsigned maxFPS, maxPPS, maxBPS;
  970. size_t i;
  971. char buffer[256];
  972. frame = self->cc->getFrameCount();
  973. pixels = self->cc->getPixelCount();
  974. pos = self->cc->getPosition();
  975. elapsed = msSince(&self->statsLastTime);
  976. if (elapsed < 1)
  977. elapsed = 1;
  978. memmove(&self->stats[0], &self->stats[1], sizeof(self->stats[0])*(statsCount-1));
  979. self->stats[statsCount-1].fps = (frame - self->statsLastFrame) * 1000 / elapsed;
  980. self->stats[statsCount-1].pps = (pixels - self->statsLastPixels) * 1000 / elapsed;
  981. self->stats[statsCount-1].bps = (pos - self->statsLastPosition) * 1000 / elapsed;
  982. gettimeofday(&self->statsLastTime, NULL);
  983. self->statsLastFrame = frame;
  984. self->statsLastPixels = pixels;
  985. self->statsLastPosition = pos;
  986. #if !defined(WIN32) && !defined(__APPLE__)
  987. // FLTK < 1.3.5 crashes if fl_gc is unset
  988. if (!fl_gc)
  989. fl_gc = XDefaultGC(fl_display, 0);
  990. #endif
  991. surface = new Fl_Image_Surface(statsWidth, statsHeight);
  992. surface->set_current();
  993. fl_rectf(0, 0, statsWidth, statsHeight, FL_BLACK);
  994. fl_rect(5, 5, graphWidth, graphHeight, FL_WHITE);
  995. maxFPS = maxPPS = maxBPS = 0;
  996. for (i = 0;i < statsCount;i++) {
  997. if (self->stats[i].fps > maxFPS)
  998. maxFPS = self->stats[i].fps;
  999. if (self->stats[i].pps > maxPPS)
  1000. maxPPS = self->stats[i].pps;
  1001. if (self->stats[i].bps > maxBPS)
  1002. maxBPS = self->stats[i].bps;
  1003. }
  1004. if (maxFPS != 0) {
  1005. fl_color(FL_GREEN);
  1006. for (i = 0;i < statsCount-1;i++) {
  1007. fl_line(5 + i * graphWidth / statsCount,
  1008. 5 + graphHeight - graphHeight * self->stats[i].fps / maxFPS,
  1009. 5 + (i+1) * graphWidth / statsCount,
  1010. 5 + graphHeight - graphHeight * self->stats[i+1].fps / maxFPS);
  1011. }
  1012. }
  1013. if (maxPPS != 0) {
  1014. fl_color(FL_YELLOW);
  1015. for (i = 0;i < statsCount-1;i++) {
  1016. fl_line(5 + i * graphWidth / statsCount,
  1017. 5 + graphHeight - graphHeight * self->stats[i].pps / maxPPS,
  1018. 5 + (i+1) * graphWidth / statsCount,
  1019. 5 + graphHeight - graphHeight * self->stats[i+1].pps / maxPPS);
  1020. }
  1021. }
  1022. if (maxBPS != 0) {
  1023. fl_color(FL_RED);
  1024. for (i = 0;i < statsCount-1;i++) {
  1025. fl_line(5 + i * graphWidth / statsCount,
  1026. 5 + graphHeight - graphHeight * self->stats[i].bps / maxBPS,
  1027. 5 + (i+1) * graphWidth / statsCount,
  1028. 5 + graphHeight - graphHeight * self->stats[i+1].bps / maxBPS);
  1029. }
  1030. }
  1031. fl_font(FL_HELVETICA, 10);
  1032. fl_color(FL_GREEN);
  1033. snprintf(buffer, sizeof(buffer), "%u fps", self->stats[statsCount-1].fps);
  1034. fl_draw(buffer, 5, statsHeight - 5);
  1035. fl_color(FL_YELLOW);
  1036. siPrefix(self->stats[statsCount-1].pps * 8, "pix/s",
  1037. buffer, sizeof(buffer), 3);
  1038. fl_draw(buffer, 5 + (statsWidth-10)/3, statsHeight - 5);
  1039. fl_color(FL_RED);
  1040. iecPrefix(self->stats[statsCount-1].bps * 8, "Bps",
  1041. buffer, sizeof(buffer), 3);
  1042. fl_draw(buffer, 5 + (statsWidth-10)*2/3, statsHeight - 5);
  1043. image = surface->image();
  1044. delete surface;
  1045. Fl_Display_Device::display_device()->set_current();
  1046. delete self->statsGraph;
  1047. self->statsGraph = new Surface(image);
  1048. delete image;
  1049. self->damage(FL_DAMAGE_CHILD, self->w() - statsWidth - 30,
  1050. self->h() - statsHeight - 30,
  1051. statsWidth, statsHeight);
  1052. Fl::repeat_timeout(0.5, handleStatsTimeout, data);
  1053. }