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