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