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