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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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. fullscreen();
  616. }
  617. void DesktopWindow::grabKeyboard()
  618. {
  619. // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
  620. // correct widget regardless of which low level window got the system
  621. // event.
  622. // FIXME: Push this stuff into FLTK.
  623. #if defined(WIN32)
  624. int ret;
  625. ret = win32_enable_lowlevel_keyboard(fl_xid(this));
  626. if (ret != 0) {
  627. vlog.error(_("Failure grabbing keyboard"));
  628. return;
  629. }
  630. #elif defined(__APPLE__)
  631. int ret;
  632. ret = cocoa_capture_display(this, fullScreenAllMonitors);
  633. if (ret != 0) {
  634. vlog.error(_("Failure grabbing keyboard"));
  635. return;
  636. }
  637. #else
  638. int ret;
  639. ret = XGrabKeyboard(fl_display, fl_xid(this), True,
  640. GrabModeAsync, GrabModeAsync, CurrentTime);
  641. if (ret) {
  642. if (ret == AlreadyGrabbed) {
  643. // It seems like we can race with the WM in some cases.
  644. // Try again in a bit.
  645. if (!Fl::has_timeout(handleGrab, this))
  646. Fl::add_timeout(0.500, handleGrab, this);
  647. } else {
  648. vlog.error(_("Failure grabbing keyboard"));
  649. }
  650. return;
  651. }
  652. #endif
  653. keyboardGrabbed = true;
  654. if (contains(Fl::belowmouse()))
  655. grabPointer();
  656. }
  657. void DesktopWindow::ungrabKeyboard()
  658. {
  659. Fl::remove_timeout(handleGrab, this);
  660. keyboardGrabbed = false;
  661. ungrabPointer();
  662. #if defined(WIN32)
  663. win32_disable_lowlevel_keyboard(fl_xid(this));
  664. #elif defined(__APPLE__)
  665. cocoa_release_display(this);
  666. #else
  667. // FLTK has a grab so lets not mess with it
  668. if (Fl::grab())
  669. return;
  670. XUngrabKeyboard(fl_display, CurrentTime);
  671. #endif
  672. }
  673. void DesktopWindow::grabPointer()
  674. {
  675. #if !defined(WIN32) && !defined(__APPLE__)
  676. int ret;
  677. // We also need to grab the pointer as some WMs like to grab buttons
  678. // combined with modifies (e.g. Alt+Button0 in metacity).
  679. ret = XGrabPointer(fl_display, fl_xid(this), True,
  680. ButtonPressMask|ButtonReleaseMask|
  681. ButtonMotionMask|PointerMotionMask,
  682. GrabModeAsync, GrabModeAsync,
  683. None, None, CurrentTime);
  684. if (ret) {
  685. // Having a button pressed prevents us from grabbing, we make
  686. // a new attempt in fltkHandle()
  687. if (ret == AlreadyGrabbed)
  688. return;
  689. vlog.error(_("Failure grabbing mouse"));
  690. return;
  691. }
  692. #endif
  693. mouseGrabbed = true;
  694. }
  695. void DesktopWindow::ungrabPointer()
  696. {
  697. mouseGrabbed = false;
  698. #if !defined(WIN32) && !defined(__APPLE__)
  699. XUngrabPointer(fl_display, CurrentTime);
  700. #endif
  701. }
  702. void DesktopWindow::handleGrab(void *data)
  703. {
  704. DesktopWindow *self = (DesktopWindow*)data;
  705. assert(self);
  706. if (!fullscreenSystemKeys)
  707. return;
  708. if (!self->fullscreen_active())
  709. return;
  710. self->grabKeyboard();
  711. }
  712. #define _NET_WM_STATE_ADD 1 /* add/set property */
  713. void DesktopWindow::maximizeWindow()
  714. {
  715. #if defined(WIN32)
  716. // We cannot use ShowWindow() in full screen mode as it will
  717. // resize things implicitly. Fortunately modifying the style
  718. // directly results in a maximized state once we leave full screen.
  719. if (fullscreen_active()) {
  720. WINDOWINFO wi;
  721. wi.cbSize = sizeof(WINDOWINFO);
  722. GetWindowInfo(fl_xid(this), &wi);
  723. SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
  724. } else
  725. ShowWindow(fl_xid(this), SW_MAXIMIZE);
  726. #elif defined(__APPLE__)
  727. // OS X is somewhat strange and does not really have a concept of a
  728. // maximized window, so we can simply resize the window to the workarea.
  729. // Note that we shouldn't do this whilst in full screen as that will
  730. // incorrectly adjust things.
  731. if (fullscreen_active())
  732. return;
  733. int X, Y, W, H;
  734. Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
  735. size(W, H);
  736. #else
  737. // X11
  738. fl_open_display();
  739. Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
  740. Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
  741. Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
  742. XEvent e;
  743. e.xany.type = ClientMessage;
  744. e.xany.window = fl_xid(this);
  745. e.xclient.message_type = net_wm_state;
  746. e.xclient.format = 32;
  747. e.xclient.data.l[0] = _NET_WM_STATE_ADD;
  748. e.xclient.data.l[1] = net_wm_state_maximized_vert;
  749. e.xclient.data.l[2] = net_wm_state_maximized_horz;
  750. e.xclient.data.l[3] = 0;
  751. e.xclient.data.l[4] = 0;
  752. XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
  753. #endif
  754. }
  755. void DesktopWindow::handleDesktopSize()
  756. {
  757. if (strcmp(desktopSize, "") != 0) {
  758. int width, height;
  759. // An explicit size has been requested
  760. if (sscanf(desktopSize, "%dx%d", &width, &height) != 2)
  761. return;
  762. remoteResize(width, height);
  763. } else if (::remoteResize) {
  764. // No explicit size, but remote resizing is on so make sure it
  765. // matches whatever size the window ended up being
  766. remoteResize(w(), h());
  767. }
  768. }
  769. void DesktopWindow::handleResizeTimeout(void *data)
  770. {
  771. DesktopWindow *self = (DesktopWindow *)data;
  772. assert(self);
  773. self->remoteResize(self->w(), self->h());
  774. }
  775. void DesktopWindow::remoteResize(int width, int height)
  776. {
  777. ScreenSet layout;
  778. ScreenSet::iterator iter;
  779. if (!fullscreen_active() || (width > w()) || (height > h())) {
  780. // In windowed mode (or the framebuffer is so large that we need
  781. // to scroll) we just report a single virtual screen that covers
  782. // the entire framebuffer.
  783. layout = cc->cp.screenLayout;
  784. // Not sure why we have no screens, but adding a new one should be
  785. // safe as there is nothing to conflict with...
  786. if (layout.num_screens() == 0)
  787. layout.add_screen(rfb::Screen());
  788. else if (layout.num_screens() != 1) {
  789. // More than one screen. Remove all but the first (which we
  790. // assume is the "primary").
  791. while (true) {
  792. iter = layout.begin();
  793. ++iter;
  794. if (iter == layout.end())
  795. break;
  796. layout.remove_screen(iter->id);
  797. }
  798. }
  799. // Resize the remaining single screen to the complete framebuffer
  800. layout.begin()->dimensions.tl.x = 0;
  801. layout.begin()->dimensions.tl.y = 0;
  802. layout.begin()->dimensions.br.x = width;
  803. layout.begin()->dimensions.br.y = height;
  804. } else {
  805. int i;
  806. rdr::U32 id;
  807. int sx, sy, sw, sh;
  808. rfb::Rect viewport_rect, screen_rect;
  809. // In full screen we report all screens that are fully covered.
  810. viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
  811. width, height);
  812. // If we can find a matching screen in the existing set, we use
  813. // that, otherwise we create a brand new screen.
  814. //
  815. // FIXME: We should really track screens better so we can handle
  816. // a resized one.
  817. //
  818. for (i = 0;i < Fl::screen_count();i++) {
  819. Fl::screen_xywh(sx, sy, sw, sh, i);
  820. // Check that the screen is fully inside the framebuffer
  821. screen_rect.setXYWH(sx, sy, sw, sh);
  822. if (!screen_rect.enclosed_by(viewport_rect))
  823. continue;
  824. // Adjust the coordinates so they are relative to our viewport
  825. sx -= viewport_rect.tl.x;
  826. sy -= viewport_rect.tl.y;
  827. // Look for perfectly matching existing screen...
  828. for (iter = cc->cp.screenLayout.begin();
  829. iter != cc->cp.screenLayout.end(); ++iter) {
  830. if ((iter->dimensions.tl.x == sx) &&
  831. (iter->dimensions.tl.y == sy) &&
  832. (iter->dimensions.width() == sw) &&
  833. (iter->dimensions.height() == sh))
  834. break;
  835. }
  836. // Found it?
  837. if (iter != cc->cp.screenLayout.end()) {
  838. layout.add_screen(*iter);
  839. continue;
  840. }
  841. // Need to add a new one, which means we need to find an unused id
  842. while (true) {
  843. id = rand();
  844. for (iter = cc->cp.screenLayout.begin();
  845. iter != cc->cp.screenLayout.end(); ++iter) {
  846. if (iter->id == id)
  847. break;
  848. }
  849. if (iter == cc->cp.screenLayout.end())
  850. break;
  851. }
  852. layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
  853. }
  854. // If the viewport doesn't match a physical screen, then we might
  855. // end up with no screens in the layout. Add a fake one...
  856. if (layout.num_screens() == 0)
  857. layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
  858. }
  859. // Do we actually change anything?
  860. if ((width == cc->cp.width) &&
  861. (height == cc->cp.height) &&
  862. (layout == cc->cp.screenLayout))
  863. return;
  864. char buffer[2048];
  865. vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
  866. cc->cp.width, cc->cp.height, width, height);
  867. layout.print(buffer, sizeof(buffer));
  868. vlog.debug("%s", buffer);
  869. if (!layout.validate(width, height)) {
  870. vlog.error(_("Invalid screen layout computed for resize request!"));
  871. return;
  872. }
  873. cc->writer()->writeSetDesktopSize(width, height, layout);
  874. }
  875. void DesktopWindow::repositionWidgets()
  876. {
  877. int new_x, new_y;
  878. // Viewport position
  879. new_x = viewport->x();
  880. new_y = viewport->y();
  881. if (w() > viewport->w())
  882. new_x = (w() - viewport->w()) / 2;
  883. else {
  884. if (viewport->x() > 0)
  885. new_x = 0;
  886. else if (w() > (viewport->x() + viewport->w()))
  887. new_x = w() - viewport->w();
  888. }
  889. // Same thing for y axis
  890. if (h() > viewport->h())
  891. new_y = (h() - viewport->h()) / 2;
  892. else {
  893. if (viewport->y() > 0)
  894. new_y = 0;
  895. else if (h() > (viewport->y() + viewport->h()))
  896. new_y = h() - viewport->h();
  897. }
  898. if ((new_x != viewport->x()) || (new_y != viewport->y())) {
  899. viewport->position(new_x, new_y);
  900. damage(FL_DAMAGE_SCROLL);
  901. }
  902. // Scrollbars visbility
  903. if (fullscreen_active()) {
  904. hscroll->hide();
  905. vscroll->hide();
  906. } else {
  907. // Decide whether to show a scrollbar by checking if the window
  908. // size (possibly minus scrollbar_size) is less than the viewport
  909. // (remote framebuffer) size.
  910. //
  911. // We decide whether to subtract scrollbar_size on an axis by
  912. // checking if the other axis *definitely* needs a scrollbar. You
  913. // might be tempted to think that this becomes a weird recursive
  914. // problem, but it isn't: If the window size is less than the
  915. // viewport size (without subtracting the scrollbar_size), then
  916. // that axis *definitely* needs a scrollbar; if the check changes
  917. // when we subtract scrollbar_size, then that axis only *maybe*
  918. // needs a scrollbar. If both axes only "maybe" need a scrollbar,
  919. // then neither does; so we don't need to recurse on the "maybe"
  920. // cases.
  921. if (w() - (h() < viewport->h() ? Fl::scrollbar_size() : 0) < viewport->w())
  922. hscroll->show();
  923. else
  924. hscroll->hide();
  925. if (h() - (w() < viewport->w() ? Fl::scrollbar_size() : 0) < viewport->h())
  926. vscroll->show();
  927. else
  928. vscroll->hide();
  929. }
  930. // Scrollbars positions
  931. hscroll->resize(0, h() - Fl::scrollbar_size(),
  932. w() - (vscroll->visible() ? Fl::scrollbar_size() : 0),
  933. Fl::scrollbar_size());
  934. vscroll->resize(w() - Fl::scrollbar_size(), 0,
  935. Fl::scrollbar_size(),
  936. h() - (hscroll->visible() ? Fl::scrollbar_size() : 0));
  937. // Scrollbars range
  938. hscroll->value(-viewport->x(),
  939. w() - (vscroll->visible() ? vscroll->w() : 0),
  940. 0, viewport->w());
  941. vscroll->value(-viewport->y(),
  942. h() - (hscroll->visible() ? hscroll->h() : 0),
  943. 0, viewport->h());
  944. hscroll->value(hscroll->clamp(hscroll->value()));
  945. vscroll->value(vscroll->clamp(vscroll->value()));
  946. }
  947. void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
  948. {
  949. exit_vncviewer();
  950. }
  951. void DesktopWindow::handleOptions(void *data)
  952. {
  953. DesktopWindow *self = (DesktopWindow*)data;
  954. if (self->fullscreen_active() && fullscreenSystemKeys)
  955. self->grabKeyboard();
  956. else
  957. self->ungrabKeyboard();
  958. if (fullScreen && !self->fullscreen_active())
  959. self->fullscreen_on();
  960. else if (!fullScreen && self->fullscreen_active())
  961. self->fullscreen_off();
  962. }
  963. void DesktopWindow::handleFullscreenTimeout(void *data)
  964. {
  965. DesktopWindow *self = (DesktopWindow *)data;
  966. assert(self);
  967. self->delayedFullscreen = false;
  968. if (self->delayedDesktopSize) {
  969. self->handleDesktopSize();
  970. self->delayedDesktopSize = false;
  971. }
  972. }
  973. void DesktopWindow::scrollTo(int x, int y)
  974. {
  975. x = hscroll->clamp(x);
  976. y = vscroll->clamp(y);
  977. hscroll->value(x);
  978. vscroll->value(y);
  979. // Scrollbar position results in inverse movement of
  980. // the viewport widget
  981. x = -x;
  982. y = -y;
  983. if ((viewport->x() == x) && (viewport->y() == y))
  984. return;
  985. viewport->position(x, y);
  986. damage(FL_DAMAGE_SCROLL);
  987. }
  988. void DesktopWindow::handleScroll(Fl_Widget *widget, void *data)
  989. {
  990. DesktopWindow *self = (DesktopWindow *)data;
  991. self->scrollTo(self->hscroll->value(), self->vscroll->value());
  992. }
  993. void DesktopWindow::handleEdgeScroll(void *data)
  994. {
  995. DesktopWindow *self = (DesktopWindow *)data;
  996. int mx, my;
  997. int dx, dy;
  998. assert(self);
  999. if (!self->fullscreen_active())
  1000. return;
  1001. mx = Fl::event_x();
  1002. my = Fl::event_y();
  1003. dx = dy = 0;
  1004. // Clamp mouse position in case it is outside the window
  1005. if (mx < 0)
  1006. mx = 0;
  1007. if (mx > self->w())
  1008. mx = self->w();
  1009. if (my < 0)
  1010. my = 0;
  1011. if (my > self->h())
  1012. my = self->h();
  1013. if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
  1014. dx = EDGE_SCROLL_SPEED -
  1015. EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
  1016. if ((self->viewport->x() + self->viewport->w() > self->w()) &&
  1017. (mx > self->w() - EDGE_SCROLL_SIZE))
  1018. dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
  1019. EDGE_SCROLL_SPEED;
  1020. if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
  1021. dy = EDGE_SCROLL_SPEED -
  1022. EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
  1023. if ((self->viewport->y() + self->viewport->h() > self->h()) &&
  1024. (my > self->h() - EDGE_SCROLL_SIZE))
  1025. dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
  1026. EDGE_SCROLL_SPEED;
  1027. if ((dx == 0) && (dy == 0))
  1028. return;
  1029. self->scrollTo(self->hscroll->value() - dx, self->vscroll->value() - dy);
  1030. Fl::repeat_timeout(0.1, handleEdgeScroll, data);
  1031. }
  1032. void DesktopWindow::handleStatsTimeout(void *data)
  1033. {
  1034. DesktopWindow *self = (DesktopWindow*)data;
  1035. const size_t statsCount = sizeof(self->stats)/sizeof(self->stats[0]);
  1036. unsigned updates, pixels, pos;
  1037. unsigned elapsed;
  1038. const unsigned statsWidth = 200;
  1039. const unsigned statsHeight = 100;
  1040. const unsigned graphWidth = statsWidth - 10;
  1041. const unsigned graphHeight = statsHeight - 25;
  1042. Fl_Image_Surface *surface;
  1043. Fl_RGB_Image *image;
  1044. unsigned maxUPS, maxPPS, maxBPS;
  1045. size_t i;
  1046. char buffer[256];
  1047. updates = self->cc->getUpdateCount();
  1048. pixels = self->cc->getPixelCount();
  1049. pos = self->cc->getPosition();
  1050. elapsed = msSince(&self->statsLastTime);
  1051. if (elapsed < 1)
  1052. elapsed = 1;
  1053. memmove(&self->stats[0], &self->stats[1], sizeof(self->stats[0])*(statsCount-1));
  1054. self->stats[statsCount-1].ups = (updates - self->statsLastUpdates) * 1000 / elapsed;
  1055. self->stats[statsCount-1].pps = (pixels - self->statsLastPixels) * 1000 / elapsed;
  1056. self->stats[statsCount-1].bps = (pos - self->statsLastPosition) * 1000 / elapsed;
  1057. gettimeofday(&self->statsLastTime, NULL);
  1058. self->statsLastUpdates = updates;
  1059. self->statsLastPixels = pixels;
  1060. self->statsLastPosition = pos;
  1061. #if !defined(WIN32) && !defined(__APPLE__)
  1062. // FLTK < 1.3.5 crashes if fl_gc is unset
  1063. if (!fl_gc)
  1064. fl_gc = XDefaultGC(fl_display, 0);
  1065. #endif
  1066. surface = new Fl_Image_Surface(statsWidth, statsHeight);
  1067. surface->set_current();
  1068. fl_rectf(0, 0, statsWidth, statsHeight, FL_BLACK);
  1069. fl_rect(5, 5, graphWidth, graphHeight, FL_WHITE);
  1070. maxUPS = maxPPS = maxBPS = 0;
  1071. for (i = 0;i < statsCount;i++) {
  1072. if (self->stats[i].ups > maxUPS)
  1073. maxUPS = self->stats[i].ups;
  1074. if (self->stats[i].pps > maxPPS)
  1075. maxPPS = self->stats[i].pps;
  1076. if (self->stats[i].bps > maxBPS)
  1077. maxBPS = self->stats[i].bps;
  1078. }
  1079. if (maxUPS != 0) {
  1080. fl_color(FL_GREEN);
  1081. for (i = 0;i < statsCount-1;i++) {
  1082. fl_line(5 + i * graphWidth / statsCount,
  1083. 5 + graphHeight - graphHeight * self->stats[i].ups / maxUPS,
  1084. 5 + (i+1) * graphWidth / statsCount,
  1085. 5 + graphHeight - graphHeight * self->stats[i+1].ups / maxUPS);
  1086. }
  1087. }
  1088. if (maxPPS != 0) {
  1089. fl_color(FL_YELLOW);
  1090. for (i = 0;i < statsCount-1;i++) {
  1091. fl_line(5 + i * graphWidth / statsCount,
  1092. 5 + graphHeight - graphHeight * self->stats[i].pps / maxPPS,
  1093. 5 + (i+1) * graphWidth / statsCount,
  1094. 5 + graphHeight - graphHeight * self->stats[i+1].pps / maxPPS);
  1095. }
  1096. }
  1097. if (maxBPS != 0) {
  1098. fl_color(FL_RED);
  1099. for (i = 0;i < statsCount-1;i++) {
  1100. fl_line(5 + i * graphWidth / statsCount,
  1101. 5 + graphHeight - graphHeight * self->stats[i].bps / maxBPS,
  1102. 5 + (i+1) * graphWidth / statsCount,
  1103. 5 + graphHeight - graphHeight * self->stats[i+1].bps / maxBPS);
  1104. }
  1105. }
  1106. fl_font(FL_HELVETICA, 10);
  1107. fl_color(FL_GREEN);
  1108. snprintf(buffer, sizeof(buffer), "%u upd/s", self->stats[statsCount-1].ups);
  1109. fl_draw(buffer, 5, statsHeight - 5);
  1110. fl_color(FL_YELLOW);
  1111. siPrefix(self->stats[statsCount-1].pps, "pix/s",
  1112. buffer, sizeof(buffer), 3);
  1113. fl_draw(buffer, 5 + (statsWidth-10)/3, statsHeight - 5);
  1114. fl_color(FL_RED);
  1115. siPrefix(self->stats[statsCount-1].bps * 8, "bps",
  1116. buffer, sizeof(buffer), 3);
  1117. fl_draw(buffer, 5 + (statsWidth-10)*2/3, statsHeight - 5);
  1118. image = surface->image();
  1119. delete surface;
  1120. Fl_Display_Device::display_device()->set_current();
  1121. delete self->statsGraph;
  1122. self->statsGraph = new Surface(image);
  1123. delete image;
  1124. self->damage(FL_DAMAGE_CHILD, self->w() - statsWidth - 30,
  1125. self->h() - statsHeight - 30,
  1126. statsWidth, statsHeight);
  1127. Fl::repeat_timeout(0.5, handleStatsTimeout, data);
  1128. }