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

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