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