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

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