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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 <rfb/LogWriter.h>
  26. #include <rfb/CMsgWriter.h>
  27. #include "DesktopWindow.h"
  28. #include "OptionsDialog.h"
  29. #include "i18n.h"
  30. #include "parameters.h"
  31. #include "vncviewer.h"
  32. #include "CConn.h"
  33. #include <FL/Fl_Scroll.H>
  34. #include <FL/x.H>
  35. #ifdef WIN32
  36. #include "win32.h"
  37. #endif
  38. #ifdef __APPLE__
  39. #include "cocoa.h"
  40. #endif
  41. #define EDGE_SCROLL_SIZE 32
  42. #define EDGE_SCROLL_SPEED 20
  43. using namespace rfb;
  44. static rfb::LogWriter vlog("DesktopWindow");
  45. DesktopWindow::DesktopWindow(int w, int h, const char *name,
  46. const rfb::PixelFormat& serverPF,
  47. CConn* cc_)
  48. : Fl_Window(w, h), cc(cc_), firstUpdate(true),
  49. delayedFullscreen(false), delayedDesktopSize(false)
  50. {
  51. scroll = new Fl_Scroll(0, 0, w, h);
  52. scroll->color(FL_BLACK);
  53. // Automatically adjust the scroll box to the window
  54. resizable(scroll);
  55. viewport = new Viewport(w, h, serverPF, cc);
  56. scroll->end();
  57. callback(handleClose, this);
  58. setName(name);
  59. OptionsDialog::addCallback(handleOptions, this);
  60. // Hack. See below...
  61. Fl::event_dispatch(&fltkHandle);
  62. // Support for -geometry option. Note that although we do support
  63. // negative coordinates, we do not support -XOFF-YOFF (ie
  64. // coordinates relative to the right edge / bottom edge) at this
  65. // time.
  66. int geom_x = 0, geom_y = 0;
  67. if (geometry.hasBeenSet()) {
  68. int matched;
  69. matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
  70. if (matched == 2) {
  71. force_position(1);
  72. } else {
  73. int geom_w, geom_h;
  74. matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
  75. switch (matched) {
  76. case 4:
  77. force_position(1);
  78. /* fall through */
  79. case 2:
  80. w = geom_w;
  81. h = geom_h;
  82. break;
  83. default:
  84. geom_x = geom_y = 0;
  85. vlog.error("Invalid geometry specified!");
  86. }
  87. }
  88. }
  89. #ifdef __APPLE__
  90. // On OS X we can do the maximize thing properly before the
  91. // window is showned. Other platforms handled further down...
  92. if (maximize) {
  93. #ifdef HAVE_FLTK_WORK_AREA
  94. int dummy;
  95. Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
  96. #else
  97. w = Fl::w();
  98. h = Fl::h();
  99. #endif
  100. }
  101. #endif
  102. if (force_position()) {
  103. resize(geom_x, geom_y, w, h);
  104. } else {
  105. size(w, h);
  106. }
  107. #ifdef HAVE_FLTK_FULLSCREEN
  108. if (fullScreen) {
  109. // Hack: Window managers seem to be rather crappy at respecting
  110. // fullscreen hints on initial windows. So on X11 we'll have to
  111. // wait until after we've been mapped.
  112. #if defined(WIN32) || defined(__APPLE__)
  113. fullscreen_on();
  114. #else
  115. delayedFullscreen = true;
  116. #endif
  117. }
  118. #endif
  119. show();
  120. // Full screen events are not sent out for a hidden window,
  121. // so send a fake one here to set up things properly.
  122. #ifdef HAVE_FLTK_FULLSCREEN
  123. if (fullscreen_active())
  124. handle(FL_FULLSCREEN);
  125. #endif
  126. // Unfortunately, current FLTK does not allow us to set the
  127. // maximized property on Windows and X11 before showing the window.
  128. // See STR #2083 and STR #2178
  129. #ifndef __APPLE__
  130. if (maximize) {
  131. maximizeWindow();
  132. }
  133. #endif
  134. // The window manager might give us an initial window size that is different
  135. // than the one we requested, and in those cases we need to manually adjust
  136. // the scroll widget for things to behave sanely.
  137. if ((w != this->w()) || (h != this->h()))
  138. scroll->size(this->w(), this->h());
  139. #ifdef HAVE_FLTK_FULLSCREEN
  140. if (delayedFullscreen) {
  141. // Hack: Fullscreen requests may be ignored, so we need a timeout for
  142. // when we should stop waiting. We also really need to wait for the
  143. // resize, which can come after the fullscreen event.
  144. Fl::add_timeout(0.5, handleFullscreenTimeout, this);
  145. fullscreen_on();
  146. }
  147. #endif
  148. }
  149. DesktopWindow::~DesktopWindow()
  150. {
  151. // Unregister all timeouts in case they get a change tro trigger
  152. // again later when this object is already gone.
  153. Fl::remove_timeout(handleGrab, this);
  154. Fl::remove_timeout(handleResizeTimeout, this);
  155. Fl::remove_timeout(handleFullscreenTimeout, this);
  156. Fl::remove_timeout(handleEdgeScroll, this);
  157. OptionsDialog::removeCallback(handleOptions);
  158. // FLTK automatically deletes all child widgets, so we shouldn't touch
  159. // them ourselves here
  160. }
  161. void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
  162. {
  163. viewport->setServerPF(pf);
  164. }
  165. const rfb::PixelFormat &DesktopWindow::getPreferredPF()
  166. {
  167. return viewport->getPreferredPF();
  168. }
  169. void DesktopWindow::setName(const char *name)
  170. {
  171. CharArray windowNameStr;
  172. windowNameStr.replaceBuf(new char[256]);
  173. snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
  174. copy_label(windowNameStr.buf);
  175. }
  176. void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
  177. rdr::U16* rgbs)
  178. {
  179. viewport->setColourMapEntries(firstColour, nColours, rgbs);
  180. }
  181. // Copy the areas of the framebuffer that have been changed (damaged)
  182. // to the displayed window.
  183. void DesktopWindow::updateWindow()
  184. {
  185. if (firstUpdate) {
  186. if (cc->cp.supportsSetDesktopSize) {
  187. // Hack: Wait until we're in the proper mode and position until
  188. // resizing things, otherwise we might send the wrong thing.
  189. if (delayedFullscreen)
  190. delayedDesktopSize = true;
  191. else
  192. handleDesktopSize();
  193. }
  194. firstUpdate = false;
  195. }
  196. viewport->updateWindow();
  197. }
  198. void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
  199. {
  200. if ((new_w == viewport->w()) && (new_h == viewport->h()))
  201. return;
  202. // If we're letting the viewport match the window perfectly, then
  203. // keep things that way for the new size, otherwise just keep things
  204. // like they are.
  205. #ifdef HAVE_FLTK_FULLSCREEN
  206. if (!fullscreen_active()) {
  207. #endif
  208. if ((w() == viewport->w()) && (h() == viewport->h()))
  209. size(new_w, new_h);
  210. else {
  211. // Make sure the window isn't too big. We do this manually because
  212. // we have to disable the window size restriction (and it isn't
  213. // entirely trustworthy to begin with).
  214. if ((w() > new_w) || (h() > new_h))
  215. size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
  216. }
  217. #ifdef HAVE_FLTK_FULLSCREEN
  218. }
  219. #endif
  220. viewport->size(new_w, new_h);
  221. // We might not resize the main window, so we need to manually call this
  222. // to make sure the viewport is centered.
  223. repositionViewport();
  224. // repositionViewport() makes sure the scroll widget notices any changes
  225. // in position, but it might be just the size that changes so we also
  226. // need a poke here as well.
  227. redraw();
  228. }
  229. void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
  230. void* data, void* mask)
  231. {
  232. viewport->setCursor(width, height, hotspot, data, mask);
  233. }
  234. void DesktopWindow::resize(int x, int y, int w, int h)
  235. {
  236. bool resizing;
  237. #if ! (defined(WIN32) || defined(__APPLE__))
  238. // X11 window managers will treat a resize to cover the entire
  239. // monitor as a request to go full screen. Make sure we avoid this.
  240. #ifdef HAVE_FLTK_FULLSCREEN
  241. if (!fullscreen_active())
  242. #endif
  243. {
  244. bool resize_req;
  245. // If there is no X11 window, then this must be a resize request,
  246. // not a notification from the X server.
  247. if (!shown())
  248. resize_req = true;
  249. else {
  250. // Otherwise we need to get the real window coordinates to tell
  251. // the difference
  252. XWindowAttributes actual;
  253. Window cr;
  254. int wx, wy;
  255. XGetWindowAttributes(fl_display, fl_xid(this), &actual);
  256. XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
  257. 0, 0, &wx, &wy, &cr);
  258. // Actual resize request?
  259. if ((wx != x) || (wy != y) ||
  260. (actual.width != w) || (actual.height != h))
  261. resize_req = true;
  262. else
  263. resize_req = false;
  264. }
  265. if (resize_req) {
  266. for (int i = 0;i < Fl::screen_count();i++) {
  267. int sx, sy, sw, sh;
  268. Fl::screen_xywh(sx, sy, sw, sh, i);
  269. if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
  270. vlog.info("Adjusting window size to avoid accidental full screen request");
  271. // Assume a panel of some form and adjust the height
  272. y += 20;
  273. h -= 40;
  274. }
  275. }
  276. }
  277. }
  278. #endif
  279. if ((this->w() != w) || (this->h() != h))
  280. resizing = true;
  281. else
  282. resizing = false;
  283. Fl_Window::resize(x, y, w, h);
  284. if (resizing) {
  285. // Try to get the remote size to match our window size, provided
  286. // the following conditions are true:
  287. //
  288. // a) The user has this feature turned on
  289. // b) The server supports it
  290. // c) We're not still waiting for a chance to handle DesktopSize
  291. // d) We're not still waiting for startup fullscreen to kick in
  292. //
  293. if (not firstUpdate and not delayedFullscreen and
  294. ::remoteResize and cc->cp.supportsSetDesktopSize) {
  295. // We delay updating the remote desktop as we tend to get a flood
  296. // of resize events as the user is dragging the window.
  297. Fl::remove_timeout(handleResizeTimeout, this);
  298. Fl::add_timeout(0.5, handleResizeTimeout, this);
  299. }
  300. // Deal with some scrolling corner cases
  301. repositionViewport();
  302. }
  303. }
  304. int DesktopWindow::handle(int event)
  305. {
  306. switch (event) {
  307. #ifdef HAVE_FLTK_FULLSCREEN
  308. case FL_FULLSCREEN:
  309. fullScreen.setParam(fullscreen_active());
  310. if (fullscreen_active())
  311. scroll->type(0);
  312. else
  313. scroll->type(Fl_Scroll::BOTH);
  314. // The scroll widget isn't clever enough to actually redraw the
  315. // scroll bars when they are added/removed, so we need to give
  316. // it a push.
  317. scroll->redraw();
  318. if (!fullscreenSystemKeys)
  319. break;
  320. if (fullscreen_active())
  321. grabKeyboard();
  322. else
  323. ungrabKeyboard();
  324. break;
  325. case FL_ENTER:
  326. case FL_LEAVE:
  327. case FL_DRAG:
  328. case FL_MOVE:
  329. if (fullscreen_active()) {
  330. if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
  331. ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
  332. ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
  333. ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
  334. if (!Fl::has_timeout(handleEdgeScroll, this))
  335. Fl::add_timeout(0.1, handleEdgeScroll, this);
  336. }
  337. }
  338. // Continue processing so that the viewport also gets mouse events
  339. break;
  340. #endif
  341. case FL_SHORTCUT:
  342. // Sometimes the focus gets out of whack and we fall through to the
  343. // shortcut dispatching. Try to make things sane again...
  344. if (Fl::focus() == NULL) {
  345. take_focus();
  346. Fl::handle(FL_KEYDOWN, this);
  347. }
  348. return 1;
  349. }
  350. return Fl_Window::handle(event);
  351. }
  352. int DesktopWindow::fltkHandle(int event, Fl_Window *win)
  353. {
  354. int ret;
  355. ret = Fl::handle_(event, win);
  356. #ifdef HAVE_FLTK_FULLSCREEN
  357. // This is hackish and the result of the dodgy focus handling in FLTK.
  358. // The basic problem is that FLTK's view of focus and the system's tend
  359. // to differ, and as a result we do not see all the FL_FOCUS events we
  360. // need. Fortunately we can grab them here...
  361. DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
  362. if (dw && fullscreenSystemKeys) {
  363. switch (event) {
  364. case FL_FOCUS:
  365. // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
  366. // some issues we need to work around:
  367. // a) Fl::grab(0) on X11 will release the keyboard grab for us.
  368. // b) Gaining focus on the system level causes FLTK to switch
  369. // window level on OS X.
  370. if (dw->fullscreen_active())
  371. dw->grabKeyboard();
  372. break;
  373. case FL_UNFOCUS:
  374. // FIXME: We need to relinquish control when the entire window loses
  375. // focus as it is very tied to this specific window on some
  376. // platforms and we want to be able to open subwindows.
  377. dw->ungrabKeyboard();
  378. break;
  379. }
  380. }
  381. #endif
  382. return ret;
  383. }
  384. void DesktopWindow::fullscreen_on()
  385. {
  386. #ifdef HAVE_FLTK_FULLSCREEN
  387. #ifdef HAVE_FLTK_FULLSCREEN_SCREENS
  388. if (not fullScreenAllMonitors)
  389. fullscreen_screens(-1, -1, -1, -1);
  390. else {
  391. int top, bottom, left, right;
  392. int top_y, bottom_y, left_x, right_x;
  393. int sx, sy, sw, sh;
  394. top = bottom = left = right = 0;
  395. Fl::screen_xywh(sx, sy, sw, sh, 0);
  396. top_y = sy;
  397. bottom_y = sy + sh;
  398. left_x = sx;
  399. right_x = sx + sw;
  400. for (int i = 1;i < Fl::screen_count();i++) {
  401. Fl::screen_xywh(sx, sy, sw, sh, i);
  402. if (sy < top_y) {
  403. top = i;
  404. top_y = sy;
  405. }
  406. if ((sy + sh) > bottom_y) {
  407. bottom = i;
  408. bottom_y = sy + sh;
  409. }
  410. if (sx < left_x) {
  411. left = i;
  412. left_x = sx;
  413. }
  414. if ((sx + sw) > right_x) {
  415. right = i;
  416. right_x = sx + sw;
  417. }
  418. }
  419. fullscreen_screens(top, bottom, left, right);
  420. }
  421. #endif // HAVE_FLTK_FULLSCREEN_SCREENS
  422. fullscreen();
  423. #endif // HAVE_FLTK_FULLSCREEN
  424. }
  425. void DesktopWindow::grabKeyboard()
  426. {
  427. // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
  428. // correct widget regardless of which low level window got the system
  429. // event.
  430. // FIXME: Push this stuff into FLTK.
  431. #if defined(WIN32)
  432. int ret;
  433. ret = win32_enable_lowlevel_keyboard(fl_xid(this));
  434. if (ret != 0)
  435. vlog.error(_("Failure grabbing keyboard"));
  436. #elif defined(__APPLE__)
  437. int ret;
  438. ret = cocoa_capture_display(this,
  439. #ifdef HAVE_FLTK_FULLSCREEN_SCREENS
  440. fullScreenAllMonitors
  441. #else
  442. false
  443. #endif
  444. );
  445. if (ret != 0)
  446. vlog.error(_("Failure grabbing keyboard"));
  447. #else
  448. int ret;
  449. ret = XGrabKeyboard(fl_display, fl_xid(this), True,
  450. GrabModeAsync, GrabModeAsync, CurrentTime);
  451. if (ret) {
  452. if (ret == AlreadyGrabbed) {
  453. // It seems like we can race with the WM in some cases.
  454. // Try again in a bit.
  455. if (!Fl::has_timeout(handleGrab, this))
  456. Fl::add_timeout(0.500, handleGrab, this);
  457. } else {
  458. vlog.error(_("Failure grabbing keyboard"));
  459. }
  460. }
  461. // We also need to grab the pointer as some WMs like to grab buttons
  462. // combined with modifies (e.g. Alt+Button0 in metacity).
  463. ret = XGrabPointer(fl_display, fl_xid(this), True,
  464. ButtonPressMask|ButtonReleaseMask|
  465. ButtonMotionMask|PointerMotionMask,
  466. GrabModeAsync, GrabModeAsync,
  467. None, None, CurrentTime);
  468. if (ret)
  469. vlog.error(_("Failure grabbing mouse"));
  470. #endif
  471. }
  472. void DesktopWindow::ungrabKeyboard()
  473. {
  474. Fl::remove_timeout(handleGrab, this);
  475. #if defined(WIN32)
  476. win32_disable_lowlevel_keyboard(fl_xid(this));
  477. #elif defined(__APPLE__)
  478. cocoa_release_display(this);
  479. #else
  480. // FLTK has a grab so lets not mess with it
  481. if (Fl::grab())
  482. return;
  483. XUngrabPointer(fl_display, fl_event_time);
  484. XUngrabKeyboard(fl_display, fl_event_time);
  485. #endif
  486. }
  487. void DesktopWindow::handleGrab(void *data)
  488. {
  489. DesktopWindow *self = (DesktopWindow*)data;
  490. assert(self);
  491. #ifdef HAVE_FLTK_FULLSCREEN
  492. if (!fullscreenSystemKeys)
  493. return;
  494. if (!self->fullscreen_active())
  495. return;
  496. self->grabKeyboard();
  497. #endif
  498. }
  499. #define _NET_WM_STATE_ADD 1 /* add/set property */
  500. void DesktopWindow::maximizeWindow()
  501. {
  502. #if defined(WIN32)
  503. // We cannot use ShowWindow() in full screen mode as it will
  504. // resize things implicitly. Fortunately modifying the style
  505. // directly results in a maximized state once we leave full screen.
  506. #ifdef HAVE_FLTK_FULLSCREEN
  507. if (fullscreen_active()) {
  508. WINDOWINFO wi;
  509. wi.cbSize = sizeof(WINDOWINFO);
  510. GetWindowInfo(fl_xid(this), &wi);
  511. SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
  512. } else
  513. #endif
  514. ShowWindow(fl_xid(this), SW_MAXIMIZE);
  515. #elif defined(__APPLE__)
  516. // OS X is somewhat strange and does not really have a concept of a
  517. // maximized window, so we can simply resize the window to the workarea.
  518. // Note that we shouldn't do this whilst in full screen as that will
  519. // incorrectly adjust things.
  520. #ifdef HAVE_FLTK_FULLSCREEN
  521. if (fullscreen_active())
  522. return;
  523. #endif
  524. int X, Y, W, H;
  525. #ifdef HAVE_FLTK_WORK_AREA
  526. Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
  527. #else
  528. W = Fl::w();
  529. H = Fl::h();
  530. #endif
  531. size(W, H);
  532. #else
  533. // X11
  534. fl_open_display();
  535. Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
  536. Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
  537. Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
  538. XEvent e;
  539. e.xany.type = ClientMessage;
  540. e.xany.window = fl_xid(this);
  541. e.xclient.message_type = net_wm_state;
  542. e.xclient.format = 32;
  543. e.xclient.data.l[0] = _NET_WM_STATE_ADD;
  544. e.xclient.data.l[1] = net_wm_state_maximized_vert;
  545. e.xclient.data.l[2] = net_wm_state_maximized_horz;
  546. e.xclient.data.l[3] = 0;
  547. e.xclient.data.l[4] = 0;
  548. XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
  549. #endif
  550. }
  551. void DesktopWindow::handleDesktopSize()
  552. {
  553. int width, height;
  554. if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
  555. return;
  556. remoteResize(width, height);
  557. }
  558. void DesktopWindow::handleResizeTimeout(void *data)
  559. {
  560. DesktopWindow *self = (DesktopWindow *)data;
  561. assert(self);
  562. self->remoteResize(self->w(), self->h());
  563. }
  564. void DesktopWindow::remoteResize(int width, int height)
  565. {
  566. ScreenSet layout;
  567. ScreenSet::iterator iter;
  568. #ifdef HAVE_FLTK_FULLSCREEN
  569. if (!fullscreen_active() || (width > w()) || (height > h())) {
  570. #endif
  571. // In windowed mode (or the framebuffer is so large that we need
  572. // to scroll) we just report a single virtual screen that covers
  573. // the entire framebuffer.
  574. layout = cc->cp.screenLayout;
  575. // Not sure why we have no screens, but adding a new one should be
  576. // safe as there is nothing to conflict with...
  577. if (layout.num_screens() == 0)
  578. layout.add_screen(rfb::Screen());
  579. else if (layout.num_screens() != 1) {
  580. // More than one screen. Remove all but the first (which we
  581. // assume is the "primary").
  582. while (true) {
  583. iter = layout.begin();
  584. ++iter;
  585. if (iter == layout.end())
  586. break;
  587. layout.remove_screen(iter->id);
  588. }
  589. }
  590. // Resize the remaining single screen to the complete framebuffer
  591. layout.begin()->dimensions.tl.x = 0;
  592. layout.begin()->dimensions.tl.y = 0;
  593. layout.begin()->dimensions.br.x = width;
  594. layout.begin()->dimensions.br.y = height;
  595. #ifdef HAVE_FLTK_FULLSCREEN
  596. } else {
  597. int i;
  598. rdr::U32 id;
  599. int sx, sy, sw, sh;
  600. Rect viewport_rect, screen_rect;
  601. // In full screen we report all screens that are fully covered.
  602. viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
  603. width, height);
  604. // If we can find a matching screen in the existing set, we use
  605. // that, otherwise we create a brand new screen.
  606. //
  607. // FIXME: We should really track screens better so we can handle
  608. // a resized one.
  609. //
  610. for (i = 0;i < Fl::screen_count();i++) {
  611. Fl::screen_xywh(sx, sy, sw, sh, i);
  612. // Check that the screen is fully inside the framebuffer
  613. screen_rect.setXYWH(sx, sy, sw, sh);
  614. if (!screen_rect.enclosed_by(viewport_rect))
  615. continue;
  616. // Adjust the coordinates so they are relative to our viewport
  617. sx -= viewport_rect.tl.x;
  618. sy -= viewport_rect.tl.y;
  619. // Look for perfectly matching existing screen...
  620. for (iter = cc->cp.screenLayout.begin();
  621. iter != cc->cp.screenLayout.end(); ++iter) {
  622. if ((iter->dimensions.tl.x == sx) &&
  623. (iter->dimensions.tl.y == sy) &&
  624. (iter->dimensions.width() == sw) &&
  625. (iter->dimensions.height() == sh))
  626. break;
  627. }
  628. // Found it?
  629. if (iter != cc->cp.screenLayout.end()) {
  630. layout.add_screen(*iter);
  631. continue;
  632. }
  633. // Need to add a new one, which means we need to find an unused id
  634. while (true) {
  635. id = rand();
  636. for (iter = cc->cp.screenLayout.begin();
  637. iter != cc->cp.screenLayout.end(); ++iter) {
  638. if (iter->id == id)
  639. break;
  640. }
  641. if (iter == cc->cp.screenLayout.end())
  642. break;
  643. }
  644. layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
  645. }
  646. // If the viewport doesn't match a physical screen, then we might
  647. // end up with no screens in the layout. Add a fake one...
  648. if (layout.num_screens() == 0)
  649. layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
  650. }
  651. #endif
  652. // Do we actually change anything?
  653. if ((width == cc->cp.width) &&
  654. (height == cc->cp.height) &&
  655. (layout == cc->cp.screenLayout))
  656. return;
  657. vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
  658. cc->cp.width, cc->cp.height, width, height, layout.num_screens());
  659. if (!layout.validate(width, height)) {
  660. vlog.error("Invalid screen layout computed for resize request!");
  661. return;
  662. }
  663. cc->writer()->writeSetDesktopSize(width, height, layout);
  664. }
  665. void DesktopWindow::repositionViewport()
  666. {
  667. int new_x, new_y;
  668. // Deal with some scrolling corner cases:
  669. //
  670. // a) If the window is larger then the viewport, center the viewport.
  671. // b) If the window is smaller than the viewport, make sure there is
  672. // no wasted space on the sides.
  673. //
  674. // FIXME: Doesn't compensate for scroll widget size properly.
  675. new_x = viewport->x();
  676. new_y = viewport->y();
  677. if (w() > viewport->w())
  678. new_x = (w() - viewport->w()) / 2;
  679. else {
  680. if (viewport->x() > 0)
  681. new_x = 0;
  682. else if (w() > (viewport->x() + viewport->w()))
  683. new_x = w() - viewport->w();
  684. }
  685. // Same thing for y axis
  686. if (h() > viewport->h())
  687. new_y = (h() - viewport->h()) / 2;
  688. else {
  689. if (viewport->y() > 0)
  690. new_y = 0;
  691. else if (h() > (viewport->y() + viewport->h()))
  692. new_y = h() - viewport->h();
  693. }
  694. if ((new_x != viewport->x()) || (new_y != viewport->y())) {
  695. viewport->position(new_x, new_y);
  696. // The scroll widget does not notice when you move around child widgets,
  697. // so redraw everything to make sure things update.
  698. redraw();
  699. }
  700. }
  701. void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
  702. {
  703. exit_vncviewer();
  704. }
  705. void DesktopWindow::handleOptions(void *data)
  706. {
  707. DesktopWindow *self = (DesktopWindow*)data;
  708. #ifdef HAVE_FLTK_FULLSCREEN
  709. if (self->fullscreen_active() && fullscreenSystemKeys)
  710. self->grabKeyboard();
  711. else
  712. self->ungrabKeyboard();
  713. if (fullScreen && !self->fullscreen_active())
  714. self->fullscreen_on();
  715. else if (!fullScreen && self->fullscreen_active())
  716. self->fullscreen_off();
  717. #endif
  718. }
  719. void DesktopWindow::handleFullscreenTimeout(void *data)
  720. {
  721. DesktopWindow *self = (DesktopWindow *)data;
  722. assert(self);
  723. self->delayedFullscreen = false;
  724. if (self->delayedDesktopSize) {
  725. self->handleDesktopSize();
  726. self->delayedDesktopSize = false;
  727. }
  728. }
  729. void DesktopWindow::handleEdgeScroll(void *data)
  730. {
  731. #ifdef HAVE_FLTK_FULLSCREEN
  732. DesktopWindow *self = (DesktopWindow *)data;
  733. int mx, my;
  734. int dx, dy;
  735. assert(self);
  736. if (!self->fullscreen_active())
  737. return;
  738. mx = Fl::event_x();
  739. my = Fl::event_y();
  740. dx = dy = 0;
  741. // Clamp mouse position in case it is outside the window
  742. if (mx < 0)
  743. mx = 0;
  744. if (mx > self->w())
  745. mx = self->w();
  746. if (my < 0)
  747. my = 0;
  748. if (my > self->h())
  749. my = self->h();
  750. if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
  751. dx = EDGE_SCROLL_SPEED -
  752. EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
  753. if ((self->viewport->x() + self->viewport->w() > self->w()) &&
  754. (mx > self->w() - EDGE_SCROLL_SIZE))
  755. dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
  756. EDGE_SCROLL_SPEED;
  757. if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
  758. dy = EDGE_SCROLL_SPEED -
  759. EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
  760. if ((self->viewport->y() + self->viewport->h() > self->h()) &&
  761. (my > self->h() - EDGE_SCROLL_SIZE))
  762. dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
  763. EDGE_SCROLL_SPEED;
  764. if ((dx == 0) && (dy == 0))
  765. return;
  766. // Make sure we don't move the viewport too much
  767. if (self->viewport->x() + dx > 0)
  768. dx = -self->viewport->x();
  769. if (self->viewport->x() + dx + self->viewport->w() < self->w())
  770. dx = self->w() - (self->viewport->x() + self->viewport->w());
  771. if (self->viewport->y() + dy > 0)
  772. dy = -self->viewport->y();
  773. if (self->viewport->y() + dy + self->viewport->h() < self->h())
  774. dy = self->h() - (self->viewport->y() + self->viewport->h());
  775. self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
  776. Fl::repeat_timeout(0.1, handleEdgeScroll, data);
  777. #endif
  778. }