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