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