選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DesktopWindow.cxx 38KB

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