Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 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 | |
Peter Åstrand | c359f36 | 2011-08-23 12:04:46 +0000 | [diff] [blame] | 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 28 | #include <rfb/LogWriter.h> |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 29 | #include <rfb/CMsgWriter.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 30 | |
| 31 | #include "DesktopWindow.h" |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 32 | #include "OptionsDialog.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 33 | #include "i18n.h" |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 34 | #include "parameters.h" |
Pierre Ossman | 39ceb50 | 2011-07-12 15:54:25 +0000 | [diff] [blame] | 35 | #include "vncviewer.h" |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 36 | #include "CConn.h" |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 37 | |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 38 | #include <FL/Fl_Scroll.H> |
| 39 | #include <FL/x.H> |
| 40 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 41 | #ifdef WIN32 |
| 42 | #include "win32.h" |
| 43 | #endif |
| 44 | |
| 45 | #ifdef __APPLE__ |
| 46 | #include "cocoa.h" |
| 47 | #endif |
Pierre Ossman | 89f868a | 2011-04-11 11:59:31 +0000 | [diff] [blame] | 48 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 49 | using namespace rfb; |
| 50 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 51 | static rfb::LogWriter vlog("DesktopWindow"); |
| 52 | |
| 53 | DesktopWindow::DesktopWindow(int w, int h, const char *name, |
| 54 | const rfb::PixelFormat& serverPF, |
| 55 | CConn* cc_) |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 56 | : Fl_Window(w, h), cc(cc_), firstUpdate(true) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 57 | { |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 58 | Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h); |
| 59 | scroll->color(FL_BLACK); |
| 60 | |
| 61 | // Automatically adjust the scroll box to the window |
| 62 | resizable(scroll); |
| 63 | |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 64 | viewport = new Viewport(w, h, serverPF, cc); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 65 | |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 66 | scroll->end(); |
| 67 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 68 | callback(handleClose, this); |
| 69 | |
| 70 | setName(name); |
| 71 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 72 | OptionsDialog::addCallback(handleOptions, this); |
| 73 | |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 74 | // Hack. See below... |
| 75 | Fl::event_dispatch(&fltkHandle); |
| 76 | |
Pierre Ossman | 63ca58e | 2011-05-26 14:59:32 +0000 | [diff] [blame] | 77 | #ifdef HAVE_FLTK_FULLSCREEN |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 78 | if (fullScreen) |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 79 | fullscreen_on(); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 80 | else |
Pierre Ossman | 63ca58e | 2011-05-26 14:59:32 +0000 | [diff] [blame] | 81 | #endif |
Peter Åstrand | d62482e | 2011-08-02 08:33:27 +0000 | [diff] [blame] | 82 | { |
| 83 | // If we are creating a window which is equal to the size on the |
| 84 | // screen on X11, many WMs will treat this as a legacy fullscreen |
| 85 | // request. This is not what we want. Besides, it doesn't really |
| 86 | // make sense to try to create a window which is larger than the |
| 87 | // available work space. |
| 88 | size(__rfbmin(w, Fl::w()), __rfbmin(h, Fl::h())); |
| 89 | } |
Pierre Ossman | 63ca58e | 2011-05-26 14:59:32 +0000 | [diff] [blame] | 90 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 91 | show(); |
Pierre Ossman | 3f6c4d0 | 2011-04-15 14:09:09 +0000 | [diff] [blame] | 92 | |
| 93 | // The window manager might give us an initial window size that is different |
| 94 | // than the one we requested, and in those cases we need to manually adjust |
| 95 | // the scroll widget for things to behave sanely. |
| 96 | if ((w != this->w()) || (h != this->h())) |
| 97 | scroll->size(this->w(), this->h()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
| 101 | DesktopWindow::~DesktopWindow() |
| 102 | { |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 103 | // Unregister all timeouts in case they get a change tro trigger |
| 104 | // again later when this object is already gone. |
| 105 | Fl::remove_timeout(handleGrab, this); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 106 | Fl::remove_timeout(handleResizeTimeout, this); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 107 | |
| 108 | OptionsDialog::removeCallback(handleOptions); |
| 109 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 110 | // FLTK automatically deletes all child widgets, so we shouldn't touch |
| 111 | // them ourselves here |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
| 115 | void DesktopWindow::setServerPF(const rfb::PixelFormat& pf) |
| 116 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 117 | viewport->setServerPF(pf); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | const rfb::PixelFormat &DesktopWindow::getPreferredPF() |
| 122 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 123 | return viewport->getPreferredPF(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 127 | void DesktopWindow::setName(const char *name) |
| 128 | { |
| 129 | CharArray windowNameStr; |
| 130 | windowNameStr.replaceBuf(new char[256]); |
| 131 | |
Pierre Ossman | 27820ba | 2011-09-30 12:54:24 +0000 | [diff] [blame] | 132 | snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 133 | |
| 134 | copy_label(windowNameStr.buf); |
| 135 | } |
| 136 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 137 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 138 | void DesktopWindow::setColourMapEntries(int firstColour, int nColours, |
| 139 | rdr::U16* rgbs) |
| 140 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 141 | viewport->setColourMapEntries(firstColour, nColours, rgbs); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | // Copy the areas of the framebuffer that have been changed (damaged) |
| 146 | // to the displayed window. |
| 147 | |
| 148 | void DesktopWindow::updateWindow() |
| 149 | { |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 150 | if (firstUpdate) { |
| 151 | if (cc->cp.supportsSetDesktopSize) |
| 152 | remoteResize(); |
| 153 | firstUpdate = false; |
| 154 | } |
| 155 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 156 | viewport->updateWindow(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 160 | void DesktopWindow::resizeFramebuffer(int new_w, int new_h) |
| 161 | { |
| 162 | if ((new_w == viewport->w()) && (new_h == viewport->h())) |
| 163 | return; |
| 164 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 165 | // If we're letting the viewport match the window perfectly, then |
| 166 | // keep things that way for the new size, otherwise just keep things |
| 167 | // like they are. |
Peter Åstrand | 1d03cbc | 2011-08-01 10:34:38 +0000 | [diff] [blame] | 168 | #ifdef HAVE_FLTK_FULLSCREEN |
| 169 | if (!fullscreen_active()) { |
| 170 | #endif |
Pierre Ossman | 29e4a16 | 2011-11-21 14:03:31 +0000 | [diff] [blame] | 171 | if ((w() == viewport->w()) && (h() == viewport->h())) |
| 172 | size(new_w, new_h); |
| 173 | else { |
| 174 | // Make sure the window isn't too big. We do this manually because |
| 175 | // we have to disable the window size restriction (and it isn't |
| 176 | // entirely trustworthy to begin with). |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 177 | if ((w() > new_w) || (h() > new_h)) |
| 178 | size(__rfbmin(w(), new_w), __rfbmin(h(), new_h)); |
Pierre Ossman | 29e4a16 | 2011-11-21 14:03:31 +0000 | [diff] [blame] | 179 | } |
Peter Åstrand | 1d03cbc | 2011-08-01 10:34:38 +0000 | [diff] [blame] | 180 | #ifdef HAVE_FLTK_FULLSCREEN |
| 181 | } |
| 182 | #endif |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 183 | |
| 184 | viewport->size(new_w, new_h); |
| 185 | |
| 186 | // We might not resize the main window, so we need to manually call this |
| 187 | // to make sure the viewport is centered. |
| 188 | repositionViewport(); |
| 189 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 190 | // repositionViewport() makes sure the scroll widget notices any changes |
| 191 | // in position, but it might be just the size that changes so we also |
| 192 | // need a poke here as well. |
| 193 | redraw(); |
| 194 | } |
| 195 | |
| 196 | |
Pierre Ossman | 835b4ef | 2011-06-08 17:02:36 +0000 | [diff] [blame] | 197 | void DesktopWindow::setCursor(int width, int height, const Point& hotspot, |
| 198 | void* data, void* mask) |
| 199 | { |
| 200 | viewport->setCursor(width, height, hotspot, data, mask); |
| 201 | } |
| 202 | |
| 203 | |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 204 | void DesktopWindow::resize(int x, int y, int w, int h) |
| 205 | { |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 206 | Fl_Window::resize(x, y, w, h); |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 207 | |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 208 | // Try to get the remote size to match our window size, provided |
| 209 | // the following conditions are true: |
| 210 | // |
| 211 | // a) The user has this feature turned on |
| 212 | // b) The server supports it |
| 213 | // c) We're not still waiting for a chance to handle DesktopSize |
| 214 | // |
| 215 | if (not firstUpdate and ::remoteResize and cc->cp.supportsSetDesktopSize) { |
| 216 | // We delay updating the remote desktop as we tend to get a flood |
| 217 | // of resize events as the user is dragging the window. |
| 218 | Fl::remove_timeout(handleResizeTimeout, this); |
| 219 | Fl::add_timeout(0.5, handleResizeTimeout, this); |
| 220 | } |
| 221 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 222 | // Deal with some scrolling corner cases |
| 223 | repositionViewport(); |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 227 | int DesktopWindow::handle(int event) |
| 228 | { |
| 229 | switch (event) { |
| 230 | #ifdef HAVE_FLTK_FULLSCREEN |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 231 | case FL_FULLSCREEN: |
Pierre Ossman | 29e4a16 | 2011-11-21 14:03:31 +0000 | [diff] [blame] | 232 | fullScreen.setParam(fullscreen_active()); |
| 233 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 234 | if (!fullscreenSystemKeys) |
| 235 | break; |
| 236 | |
| 237 | if (fullscreen_active()) |
| 238 | grabKeyboard(); |
| 239 | else |
| 240 | ungrabKeyboard(); |
| 241 | |
| 242 | break; |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 243 | #endif |
| 244 | case FL_SHORTCUT: |
| 245 | // Sometimes the focus gets out of whack and we fall through to the |
| 246 | // shortcut dispatching. Try to make things sane again... |
| 247 | if (Fl::focus() == NULL) { |
| 248 | take_focus(); |
| 249 | Fl::handle(FL_KEYDOWN, this); |
| 250 | } |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | return Fl_Window::handle(event); |
| 255 | } |
| 256 | |
| 257 | |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 258 | int DesktopWindow::fltkHandle(int event, Fl_Window *win) |
| 259 | { |
| 260 | int ret; |
| 261 | |
| 262 | ret = Fl::handle_(event, win); |
| 263 | |
| 264 | #ifdef HAVE_FLTK_FULLSCREEN |
| 265 | // This is hackish and the result of the dodgy focus handling in FLTK. |
| 266 | // The basic problem is that FLTK's view of focus and the system's tend |
| 267 | // to differ, and as a result we do not see all the FL_FOCUS events we |
| 268 | // need. Fortunately we can grab them here... |
| 269 | |
| 270 | DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win); |
| 271 | |
| 272 | if (dw && fullscreenSystemKeys) { |
| 273 | switch (event) { |
| 274 | case FL_FOCUS: |
| 275 | // FIXME: We reassert the keyboard grabbing on focus as FLTK there are |
| 276 | // some issues we need to work around: |
| 277 | // a) Fl::grab(0) on X11 will release the keyboard grab for us. |
| 278 | // b) Gaining focus on the system level causes FLTK to switch |
| 279 | // window level on OS X. |
| 280 | if (dw->fullscreen_active()) |
| 281 | dw->grabKeyboard(); |
| 282 | break; |
| 283 | |
| 284 | case FL_UNFOCUS: |
| 285 | // FIXME: We need to relinquish control when the entire window loses |
| 286 | // focus as it is very tied to this specific window on some |
| 287 | // platforms and we want to be able to open subwindows. |
| 288 | dw->ungrabKeyboard(); |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | #endif |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 298 | void DesktopWindow::fullscreen_on() |
| 299 | { |
| 300 | #ifdef HAVE_FLTK_FULLSCREEN |
| 301 | #ifdef HAVE_FLTK_FULLSCREEN_SCREENS |
| 302 | if (not fullScreenAllMonitors) |
| 303 | fullscreen_screens(-1, -1, -1, -1); |
| 304 | else { |
| 305 | int top, bottom, left, right; |
| 306 | int top_y, bottom_y, left_x, right_x; |
| 307 | |
| 308 | int sx, sy, sw, sh; |
| 309 | |
| 310 | top = bottom = left = right = 0; |
| 311 | |
| 312 | Fl::screen_xywh(sx, sy, sw, sh, 0); |
| 313 | top_y = sy; |
| 314 | bottom_y = sy + sh; |
| 315 | left_x = sx; |
| 316 | right_x = sx + sw; |
| 317 | |
| 318 | for (int i = 1;i < Fl::screen_count();i++) { |
| 319 | Fl::screen_xywh(sx, sy, sw, sh, i); |
| 320 | if (sy < top_y) { |
| 321 | top = i; |
| 322 | top_y = sy; |
| 323 | } |
| 324 | if ((sy + sh) > bottom_y) { |
| 325 | bottom = i; |
| 326 | bottom_y = sy + sh; |
| 327 | } |
| 328 | if (sx < left_x) { |
| 329 | left = i; |
| 330 | left_x = sx; |
| 331 | } |
| 332 | if ((sx + sw) > right_x) { |
| 333 | right = i; |
| 334 | right_x = sx + sw; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | fullscreen_screens(top, bottom, left, right); |
| 339 | } |
| 340 | #endif // HAVE_FLTK_FULLSCREEN_SCREENS |
| 341 | |
| 342 | fullscreen(); |
| 343 | #endif // HAVE_FLTK_FULLSCREEN |
| 344 | } |
| 345 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 346 | void DesktopWindow::grabKeyboard() |
| 347 | { |
| 348 | // Grabbing the keyboard is fairly safe as FLTK reroutes events to the |
| 349 | // correct widget regardless of which low level window got the system |
| 350 | // event. |
| 351 | |
| 352 | // FIXME: Push this stuff into FLTK. |
| 353 | |
| 354 | #if defined(WIN32) |
| 355 | int ret; |
| 356 | |
| 357 | ret = win32_enable_lowlevel_keyboard(fl_xid(this)); |
| 358 | if (ret != 0) |
| 359 | vlog.error(_("Failure grabbing keyboard")); |
| 360 | #elif defined(__APPLE__) |
| 361 | int ret; |
| 362 | |
| 363 | ret = cocoa_capture_display(this); |
| 364 | if (ret != 0) |
| 365 | vlog.error(_("Failure grabbing keyboard")); |
| 366 | #else |
| 367 | int ret; |
| 368 | |
| 369 | ret = XGrabKeyboard(fl_display, fl_xid(this), True, |
Peter Åstrand | f52860b | 2011-07-18 07:42:16 +0000 | [diff] [blame] | 370 | GrabModeAsync, GrabModeAsync, CurrentTime); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 371 | if (ret) { |
| 372 | if (ret == AlreadyGrabbed) { |
| 373 | // It seems like we can race with the WM in some cases. |
| 374 | // Try again in a bit. |
| 375 | if (!Fl::has_timeout(handleGrab, this)) |
| 376 | Fl::add_timeout(0.500, handleGrab, this); |
| 377 | } else { |
| 378 | vlog.error(_("Failure grabbing keyboard")); |
| 379 | } |
| 380 | } |
Pierre Ossman | 53fd544 | 2011-11-17 10:19:19 +0000 | [diff] [blame] | 381 | |
| 382 | // We also need to grab the pointer as some WMs like to grab buttons |
| 383 | // combined with modifies (e.g. Alt+Button0 in metacity). |
| 384 | ret = XGrabPointer(fl_display, fl_xid(this), True, |
| 385 | ButtonPressMask|ButtonReleaseMask| |
| 386 | ButtonMotionMask|PointerMotionMask, |
| 387 | GrabModeAsync, GrabModeAsync, |
| 388 | None, None, CurrentTime); |
| 389 | if (ret) |
| 390 | vlog.error(_("Failure grabbing mouse")); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 391 | #endif |
| 392 | } |
| 393 | |
| 394 | |
| 395 | void DesktopWindow::ungrabKeyboard() |
| 396 | { |
| 397 | Fl::remove_timeout(handleGrab, this); |
| 398 | |
| 399 | #if defined(WIN32) |
| 400 | win32_disable_lowlevel_keyboard(fl_xid(this)); |
| 401 | #elif defined(__APPLE__) |
| 402 | cocoa_release_display(this); |
| 403 | #else |
| 404 | // FLTK has a grab so lets not mess with it |
| 405 | if (Fl::grab()) |
| 406 | return; |
| 407 | |
Pierre Ossman | 53fd544 | 2011-11-17 10:19:19 +0000 | [diff] [blame] | 408 | XUngrabPointer(fl_display, fl_event_time); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 409 | XUngrabKeyboard(fl_display, fl_event_time); |
| 410 | #endif |
| 411 | } |
| 412 | |
| 413 | |
| 414 | void DesktopWindow::handleGrab(void *data) |
| 415 | { |
| 416 | DesktopWindow *self = (DesktopWindow*)data; |
| 417 | |
| 418 | assert(self); |
| 419 | |
| 420 | #ifdef HAVE_FLTK_FULLSCREEN |
| 421 | if (!fullscreenSystemKeys) |
| 422 | return; |
| 423 | if (!self->fullscreen_active()) |
| 424 | return; |
| 425 | |
| 426 | self->grabKeyboard(); |
| 427 | #endif |
| 428 | } |
| 429 | |
| 430 | |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 431 | void DesktopWindow::handleResizeTimeout(void *data) |
| 432 | { |
| 433 | DesktopWindow *self = (DesktopWindow *)data; |
| 434 | |
| 435 | assert(self); |
| 436 | |
| 437 | self->remoteResize(); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | void DesktopWindow::remoteResize() |
| 442 | { |
| 443 | int width, height; |
| 444 | ScreenSet layout; |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 445 | ScreenSet::iterator iter; |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 446 | |
| 447 | if (firstUpdate) { |
| 448 | if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2) |
| 449 | return; |
| 450 | } else { |
| 451 | width = w(); |
| 452 | height = h(); |
| 453 | } |
| 454 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 455 | #ifdef HAVE_FLTK_FULLSCREEN |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame^] | 456 | if (!fullscreen_active() || (width > w()) || (height > h())) { |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 457 | #endif |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame^] | 458 | // In windowed mode (or the framebuffer is so large that we need |
| 459 | // to scroll) we just report a single virtual screen that covers |
| 460 | // the entire framebuffer. |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 461 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 462 | layout = cc->cp.screenLayout; |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 463 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 464 | // Not sure why we have no screens, but adding a new one should be |
| 465 | // safe as there is nothing to conflict with... |
| 466 | if (layout.num_screens() == 0) |
| 467 | layout.add_screen(rfb::Screen()); |
| 468 | else if (layout.num_screens() != 1) { |
| 469 | // More than one screen. Remove all but the first (which we |
| 470 | // assume is the "primary"). |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 471 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 472 | while (true) { |
| 473 | iter = layout.begin(); |
| 474 | ++iter; |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 475 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 476 | if (iter == layout.end()) |
| 477 | break; |
| 478 | |
| 479 | layout.remove_screen(iter->id); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // Resize the remaining single screen to the complete framebuffer |
| 484 | layout.begin()->dimensions.tl.x = 0; |
| 485 | layout.begin()->dimensions.tl.y = 0; |
| 486 | layout.begin()->dimensions.br.x = width; |
| 487 | layout.begin()->dimensions.br.y = height; |
| 488 | #ifdef HAVE_FLTK_FULLSCREEN |
| 489 | } else { |
| 490 | int i; |
| 491 | rdr::U32 id; |
| 492 | int sx, sy, sw, sh; |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame^] | 493 | Rect viewport_rect, screen_rect; |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 494 | |
| 495 | // In full screen we report all screens that are fully covered. |
| 496 | |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame^] | 497 | viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2, |
| 498 | width, height); |
Pierre Ossman | 93d2d92 | 2012-07-20 12:32:52 +0000 | [diff] [blame] | 499 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 500 | // If we can find a matching screen in the existing set, we use |
| 501 | // that, otherwise we create a brand new screen. |
| 502 | // |
| 503 | // FIXME: We should really track screens better so we can handle |
| 504 | // a resized one. |
| 505 | // |
| 506 | for (i = 0;i < Fl::screen_count();i++) { |
| 507 | Fl::screen_xywh(sx, sy, sw, sh, i); |
| 508 | |
Pierre Ossman | 93d2d92 | 2012-07-20 12:32:52 +0000 | [diff] [blame] | 509 | // Check that the screen is fully inside the framebuffer |
| 510 | screen_rect.setXYWH(sx, sy, sw, sh); |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame^] | 511 | if (!screen_rect.enclosed_by(viewport_rect)) |
Pierre Ossman | 93d2d92 | 2012-07-20 12:32:52 +0000 | [diff] [blame] | 512 | continue; |
| 513 | |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame^] | 514 | // Adjust the coordinates so they are relative to our viewport |
| 515 | sx -= viewport_rect.tl.x; |
| 516 | sy -= viewport_rect.tl.y; |
| 517 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 518 | // Look for perfectly matching existing screen... |
| 519 | for (iter = cc->cp.screenLayout.begin(); |
| 520 | iter != cc->cp.screenLayout.end(); ++iter) { |
| 521 | if ((iter->dimensions.tl.x == sx) && |
| 522 | (iter->dimensions.tl.y == sy) && |
| 523 | (iter->dimensions.width() == sw) && |
| 524 | (iter->dimensions.height() == sh)) |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | // Found it? |
| 529 | if (iter != cc->cp.screenLayout.end()) { |
| 530 | layout.add_screen(*iter); |
| 531 | continue; |
| 532 | } |
| 533 | |
| 534 | // Need to add a new one, which means we need to find an unused id |
| 535 | while (true) { |
| 536 | id = rand(); |
| 537 | for (iter = cc->cp.screenLayout.begin(); |
| 538 | iter != cc->cp.screenLayout.end(); ++iter) { |
| 539 | if (iter->id == id) |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | if (iter == cc->cp.screenLayout.end()) |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0)); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 550 | #endif |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 551 | |
| 552 | // Do we actually change anything? |
| 553 | if ((width == cc->cp.width) && |
| 554 | (height == cc->cp.height) && |
| 555 | (layout == cc->cp.screenLayout)) |
| 556 | return; |
| 557 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 558 | vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)", |
| 559 | cc->cp.width, cc->cp.height, width, height, layout.num_screens()); |
| 560 | |
| 561 | if (!layout.validate(width, height)) { |
| 562 | vlog.error("Invalid screen layout computed for resize request!"); |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 563 | return; |
| 564 | } |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 565 | |
| 566 | cc->writer()->writeSetDesktopSize(width, height, layout); |
| 567 | } |
| 568 | |
| 569 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 570 | void DesktopWindow::repositionViewport() |
| 571 | { |
| 572 | int new_x, new_y; |
| 573 | |
| 574 | // Deal with some scrolling corner cases: |
| 575 | // |
| 576 | // a) If the window is larger then the viewport, center the viewport. |
| 577 | // b) If the window is smaller than the viewport, make sure there is |
| 578 | // no wasted space on the sides. |
| 579 | // |
| 580 | // FIXME: Doesn't compensate for scroll widget size properly. |
| 581 | |
| 582 | new_x = viewport->x(); |
| 583 | new_y = viewport->y(); |
| 584 | |
| 585 | if (w() > viewport->w()) |
| 586 | new_x = (w() - viewport->w()) / 2; |
| 587 | else { |
| 588 | if (viewport->x() > 0) |
| 589 | new_x = 0; |
| 590 | else if (w() > (viewport->x() + viewport->w())) |
| 591 | new_x = w() - viewport->w(); |
| 592 | } |
| 593 | |
| 594 | // Same thing for y axis |
| 595 | if (h() > viewport->h()) |
| 596 | new_y = (h() - viewport->h()) / 2; |
| 597 | else { |
| 598 | if (viewport->y() > 0) |
| 599 | new_y = 0; |
| 600 | else if (h() > (viewport->y() + viewport->h())) |
| 601 | new_y = h() - viewport->h(); |
| 602 | } |
| 603 | |
| 604 | if ((new_x != viewport->x()) || (new_y != viewport->y())) { |
| 605 | viewport->position(new_x, new_y); |
| 606 | |
| 607 | // The scroll widget does not notice when you move around child widgets, |
| 608 | // so redraw everything to make sure things update. |
| 609 | redraw(); |
| 610 | } |
| 611 | } |
| 612 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 613 | void DesktopWindow::handleClose(Fl_Widget *wnd, void *data) |
| 614 | { |
| 615 | exit_vncviewer(); |
| 616 | } |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 617 | |
| 618 | |
| 619 | void DesktopWindow::handleOptions(void *data) |
| 620 | { |
| 621 | DesktopWindow *self = (DesktopWindow*)data; |
| 622 | |
| 623 | #ifdef HAVE_FLTK_FULLSCREEN |
| 624 | if (self->fullscreen_active() && fullscreenSystemKeys) |
| 625 | self->grabKeyboard(); |
| 626 | else |
| 627 | self->ungrabKeyboard(); |
Pierre Ossman | 9191164 | 2011-05-26 14:57:51 +0000 | [diff] [blame] | 628 | |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 629 | if (fullScreen && !self->fullscreen_active()) |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 630 | self->fullscreen_on(); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 631 | else if (!fullScreen && self->fullscreen_active()) |
Pierre Ossman | 9191164 | 2011-05-26 14:57:51 +0000 | [diff] [blame] | 632 | self->fullscreen_off(); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 633 | #endif |
| 634 | } |