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

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