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

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