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