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