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