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

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