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

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