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

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