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

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