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