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> |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 27 | #include <sys/time.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 28 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 29 | #include <rfb/LogWriter.h> |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 30 | #include <rfb/CMsgWriter.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 31 | |
| 32 | #include "DesktopWindow.h" |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 33 | #include "OptionsDialog.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 34 | #include "i18n.h" |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 35 | #include "parameters.h" |
Pierre Ossman | 39ceb50 | 2011-07-12 15:54:25 +0000 | [diff] [blame] | 36 | #include "vncviewer.h" |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 37 | #include "CConn.h" |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 38 | #include "Surface.h" |
Pierre Ossman | 947b48d | 2014-01-27 16:52:35 +0100 | [diff] [blame] | 39 | #include "Viewport.h" |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 40 | |
Pierre Ossman | 947b48d | 2014-01-27 16:52:35 +0100 | [diff] [blame] | 41 | #include <FL/Fl.H> |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 42 | #include <FL/Fl_Image_Surface.H> |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 43 | #include <FL/Fl_Scrollbar.H> |
| 44 | #include <FL/fl_draw.H> |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 45 | #include <FL/x.H> |
| 46 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 47 | #ifdef WIN32 |
| 48 | #include "win32.h" |
| 49 | #endif |
| 50 | |
| 51 | #ifdef __APPLE__ |
| 52 | #include "cocoa.h" |
| 53 | #endif |
Pierre Ossman | 89f868a | 2011-04-11 11:59:31 +0000 | [diff] [blame] | 54 | |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 55 | #define EDGE_SCROLL_SIZE 32 |
| 56 | #define EDGE_SCROLL_SPEED 20 |
| 57 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 58 | using namespace rfb; |
| 59 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 60 | static rfb::LogWriter vlog("DesktopWindow"); |
| 61 | |
| 62 | DesktopWindow::DesktopWindow(int w, int h, const char *name, |
| 63 | const rfb::PixelFormat& serverPF, |
| 64 | CConn* cc_) |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 65 | : Fl_Window(w, h), cc(cc_), offscreen(NULL), overlay(NULL), |
| 66 | firstUpdate(true), |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 67 | delayedFullscreen(false), delayedDesktopSize(false), |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 68 | keyboardGrabbed(false), mouseGrabbed(false), |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 69 | statsLastFrame(0), statsLastPixels(0), statsLastPosition(0), |
| 70 | statsGraph(NULL) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 71 | { |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 72 | Fl_Group* group; |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 73 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 74 | // Dummy group to prevent FLTK from moving our widgets around |
| 75 | group = new Fl_Group(0, 0, w, h); |
| 76 | group->resizable(NULL); |
| 77 | resizable(group); |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 78 | |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 79 | viewport = new Viewport(w, h, serverPF, cc); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 80 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 81 | // Position will be adjusted later |
| 82 | hscroll = new Fl_Scrollbar(0, 0, 0, 0); |
| 83 | vscroll = new Fl_Scrollbar(0, 0, 0, 0); |
| 84 | hscroll->type(FL_HORIZONTAL); |
| 85 | hscroll->callback(handleScroll, this); |
| 86 | vscroll->callback(handleScroll, this); |
| 87 | |
| 88 | group->end(); |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 89 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 90 | callback(handleClose, this); |
| 91 | |
| 92 | setName(name); |
| 93 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 94 | OptionsDialog::addCallback(handleOptions, this); |
| 95 | |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 96 | // Hack. See below... |
| 97 | Fl::event_dispatch(&fltkHandle); |
| 98 | |
Peter Åstrand | c6cdc1f | 2012-08-29 07:14:31 +0000 | [diff] [blame] | 99 | // Support for -geometry option. Note that although we do support |
| 100 | // negative coordinates, we do not support -XOFF-YOFF (ie |
| 101 | // coordinates relative to the right edge / bottom edge) at this |
| 102 | // time. |
| 103 | int geom_x = 0, geom_y = 0; |
Pierre Ossman | 135906e | 2015-04-27 12:48:47 +0200 | [diff] [blame] | 104 | if (strcmp(geometry, "") != 0) { |
Peter Åstrand | c6cdc1f | 2012-08-29 07:14:31 +0000 | [diff] [blame] | 105 | int matched; |
| 106 | matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y); |
| 107 | if (matched == 2) { |
| 108 | force_position(1); |
| 109 | } else { |
| 110 | int geom_w, geom_h; |
| 111 | matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y); |
| 112 | switch (matched) { |
| 113 | case 4: |
Pierre Ossman | 9d267c5 | 2012-10-02 14:30:22 +0000 | [diff] [blame] | 114 | force_position(1); |
| 115 | /* fall through */ |
Peter Åstrand | c6cdc1f | 2012-08-29 07:14:31 +0000 | [diff] [blame] | 116 | case 2: |
Pierre Ossman | 9d267c5 | 2012-10-02 14:30:22 +0000 | [diff] [blame] | 117 | w = geom_w; |
| 118 | h = geom_h; |
| 119 | break; |
Peter Åstrand | c6cdc1f | 2012-08-29 07:14:31 +0000 | [diff] [blame] | 120 | default: |
Pierre Ossman | 9d267c5 | 2012-10-02 14:30:22 +0000 | [diff] [blame] | 121 | geom_x = geom_y = 0; |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame] | 122 | vlog.error(_("Invalid geometry specified!")); |
Peter Åstrand | c6cdc1f | 2012-08-29 07:14:31 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Pierre Ossman | 002cc5d | 2012-10-02 14:45:10 +0000 | [diff] [blame] | 127 | #ifdef __APPLE__ |
| 128 | // On OS X we can do the maximize thing properly before the |
| 129 | // window is showned. Other platforms handled further down... |
| 130 | if (maximize) { |
| 131 | int dummy; |
| 132 | Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y); |
| 133 | } |
| 134 | #endif |
| 135 | |
Peter Åstrand | c6cdc1f | 2012-08-29 07:14:31 +0000 | [diff] [blame] | 136 | if (force_position()) { |
| 137 | resize(geom_x, geom_y, w, h); |
| 138 | } else { |
| 139 | size(w, h); |
| 140 | } |
| 141 | |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 142 | if (fullScreen) { |
| 143 | // Hack: Window managers seem to be rather crappy at respecting |
| 144 | // fullscreen hints on initial windows. So on X11 we'll have to |
| 145 | // wait until after we've been mapped. |
| 146 | #if defined(WIN32) || defined(__APPLE__) |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 147 | fullscreen_on(); |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 148 | #else |
| 149 | delayedFullscreen = true; |
| 150 | #endif |
Peter Åstrand | d62482e | 2011-08-02 08:33:27 +0000 | [diff] [blame] | 151 | } |
Pierre Ossman | 63ca58e | 2011-05-26 14:59:32 +0000 | [diff] [blame] | 152 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 153 | show(); |
Pierre Ossman | 3f6c4d0 | 2011-04-15 14:09:09 +0000 | [diff] [blame] | 154 | |
Pierre Ossman | cb68c19 | 2012-10-17 07:59:20 +0000 | [diff] [blame] | 155 | // Full screen events are not sent out for a hidden window, |
| 156 | // so send a fake one here to set up things properly. |
Pierre Ossman | cb68c19 | 2012-10-17 07:59:20 +0000 | [diff] [blame] | 157 | if (fullscreen_active()) |
| 158 | handle(FL_FULLSCREEN); |
Pierre Ossman | cb68c19 | 2012-10-17 07:59:20 +0000 | [diff] [blame] | 159 | |
Peter Åstrand | 49b1157 | 2012-08-01 08:09:09 +0000 | [diff] [blame] | 160 | // Unfortunately, current FLTK does not allow us to set the |
Pierre Ossman | 002cc5d | 2012-10-02 14:45:10 +0000 | [diff] [blame] | 161 | // maximized property on Windows and X11 before showing the window. |
| 162 | // See STR #2083 and STR #2178 |
| 163 | #ifndef __APPLE__ |
Peter Åstrand | 49b1157 | 2012-08-01 08:09:09 +0000 | [diff] [blame] | 164 | if (maximize) { |
| 165 | maximizeWindow(); |
| 166 | } |
Pierre Ossman | 002cc5d | 2012-10-02 14:45:10 +0000 | [diff] [blame] | 167 | #endif |
Peter Åstrand | 49b1157 | 2012-08-01 08:09:09 +0000 | [diff] [blame] | 168 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 169 | // Adjust layout now that we're visible and know our final size |
| 170 | repositionWidgets(); |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 171 | |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 172 | if (delayedFullscreen) { |
| 173 | // Hack: Fullscreen requests may be ignored, so we need a timeout for |
| 174 | // when we should stop waiting. We also really need to wait for the |
| 175 | // resize, which can come after the fullscreen event. |
| 176 | Fl::add_timeout(0.5, handleFullscreenTimeout, this); |
| 177 | fullscreen_on(); |
| 178 | } |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 179 | |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 180 | // Throughput graph for debugging |
| 181 | if (vlog.getLevel() >= LogWriter::LEVEL_DEBUG) { |
| 182 | memset(&stats, 0, sizeof(stats)); |
| 183 | Fl::add_timeout(0, handleStatsTimeout, this); |
| 184 | } |
| 185 | |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 186 | // Show hint about menu key |
| 187 | Fl::add_timeout(0.5, menuOverlay, this); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
| 191 | DesktopWindow::~DesktopWindow() |
| 192 | { |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 193 | // Unregister all timeouts in case they get a change tro trigger |
| 194 | // again later when this object is already gone. |
| 195 | Fl::remove_timeout(handleGrab, this); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 196 | Fl::remove_timeout(handleResizeTimeout, this); |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 197 | Fl::remove_timeout(handleFullscreenTimeout, this); |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 198 | Fl::remove_timeout(handleEdgeScroll, this); |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 199 | Fl::remove_timeout(handleStatsTimeout, this); |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 200 | Fl::remove_timeout(menuOverlay, this); |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 201 | Fl::remove_timeout(updateOverlay, this); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 202 | |
| 203 | OptionsDialog::removeCallback(handleOptions); |
| 204 | |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 205 | delete overlay; |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 206 | delete offscreen; |
| 207 | |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 208 | delete statsGraph; |
| 209 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 210 | // FLTK automatically deletes all child widgets, so we shouldn't touch |
| 211 | // them ourselves here |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 215 | const rfb::PixelFormat &DesktopWindow::getPreferredPF() |
| 216 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 217 | return viewport->getPreferredPF(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 221 | void DesktopWindow::setName(const char *name) |
| 222 | { |
| 223 | CharArray windowNameStr; |
| 224 | windowNameStr.replaceBuf(new char[256]); |
| 225 | |
Pierre Ossman | 27820ba | 2011-09-30 12:54:24 +0000 | [diff] [blame] | 226 | snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 227 | |
| 228 | copy_label(windowNameStr.buf); |
| 229 | } |
| 230 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 231 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 232 | // Copy the areas of the framebuffer that have been changed (damaged) |
| 233 | // to the displayed window. |
| 234 | |
| 235 | void DesktopWindow::updateWindow() |
| 236 | { |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 237 | if (firstUpdate) { |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 238 | if (cc->cp.supportsSetDesktopSize) { |
| 239 | // Hack: Wait until we're in the proper mode and position until |
| 240 | // resizing things, otherwise we might send the wrong thing. |
| 241 | if (delayedFullscreen) |
| 242 | delayedDesktopSize = true; |
| 243 | else |
| 244 | handleDesktopSize(); |
| 245 | } |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 246 | firstUpdate = false; |
| 247 | } |
| 248 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 249 | viewport->updateWindow(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 253 | void DesktopWindow::resizeFramebuffer(int new_w, int new_h) |
| 254 | { |
| 255 | if ((new_w == viewport->w()) && (new_h == viewport->h())) |
| 256 | return; |
| 257 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 258 | // If we're letting the viewport match the window perfectly, then |
| 259 | // keep things that way for the new size, otherwise just keep things |
| 260 | // like they are. |
Peter Åstrand | 1d03cbc | 2011-08-01 10:34:38 +0000 | [diff] [blame] | 261 | if (!fullscreen_active()) { |
Pierre Ossman | 29e4a16 | 2011-11-21 14:03:31 +0000 | [diff] [blame] | 262 | if ((w() == viewport->w()) && (h() == viewport->h())) |
| 263 | size(new_w, new_h); |
| 264 | else { |
| 265 | // Make sure the window isn't too big. We do this manually because |
| 266 | // we have to disable the window size restriction (and it isn't |
| 267 | // entirely trustworthy to begin with). |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 268 | if ((w() > new_w) || (h() > new_h)) |
| 269 | size(__rfbmin(w(), new_w), __rfbmin(h(), new_h)); |
Pierre Ossman | 29e4a16 | 2011-11-21 14:03:31 +0000 | [diff] [blame] | 270 | } |
Peter Åstrand | 1d03cbc | 2011-08-01 10:34:38 +0000 | [diff] [blame] | 271 | } |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 272 | |
| 273 | viewport->size(new_w, new_h); |
| 274 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 275 | repositionWidgets(); |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 279 | void DesktopWindow::setCursor(int width, int height, |
| 280 | const rfb::Point& hotspot, |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 281 | const rdr::U8* data) |
Pierre Ossman | 835b4ef | 2011-06-08 17:02:36 +0000 | [diff] [blame] | 282 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 283 | viewport->setCursor(width, height, hotspot, data); |
Pierre Ossman | 835b4ef | 2011-06-08 17:02:36 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 287 | void DesktopWindow::draw() |
| 288 | { |
| 289 | bool redraw; |
| 290 | |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 291 | int X, Y, W, H; |
| 292 | |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 293 | // X11 needs an off screen buffer for compositing to avoid flicker, |
| 294 | // and alpha blending doesn't work for windows on Win32 |
| 295 | #if !defined(__APPLE__) |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 296 | |
| 297 | // Adjust offscreen surface dimensions |
| 298 | if ((offscreen == NULL) || |
| 299 | (offscreen->width() != w()) || (offscreen->height() != h())) { |
| 300 | delete offscreen; |
| 301 | offscreen = new Surface(w(), h()); |
| 302 | } |
| 303 | |
| 304 | #endif |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 305 | |
| 306 | // Active area inside scrollbars |
| 307 | W = w() - (vscroll->visible() ? vscroll->w() : 0); |
| 308 | H = h() - (hscroll->visible() ? hscroll->h() : 0); |
| 309 | |
| 310 | // Full redraw? |
| 311 | redraw = (damage() & ~FL_DAMAGE_CHILD); |
| 312 | |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 313 | // Simplify the clip region to a simple rectangle in order to |
| 314 | // properly draw all the layers even if they only partially overlap |
| 315 | if (redraw) |
| 316 | X = Y = 0; |
| 317 | else |
| 318 | fl_clip_box(0, 0, W, H, X, Y, W, H); |
| 319 | fl_push_no_clip(); |
| 320 | fl_push_clip(X, Y, W, H); |
| 321 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 322 | // Redraw background only on full redraws |
| 323 | if (redraw) { |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 324 | if (offscreen) |
| 325 | offscreen->clear(40, 40, 40); |
| 326 | else |
| 327 | fl_rectf(0, 0, W, H, 40, 40, 40); |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 330 | if (offscreen) { |
| 331 | viewport->draw(offscreen); |
| 332 | viewport->clear_damage(); |
| 333 | } else { |
| 334 | if (redraw) |
| 335 | draw_child(*viewport); |
| 336 | else |
| 337 | update_child(*viewport); |
| 338 | } |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 339 | |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 340 | // Debug graph (if active) |
| 341 | if (statsGraph) { |
| 342 | int ox, oy, ow, oh; |
| 343 | |
| 344 | ox = X = w() - statsGraph->width() - 30; |
| 345 | oy = Y = h() - statsGraph->height() - 30; |
| 346 | ow = statsGraph->width(); |
| 347 | oh = statsGraph->height(); |
| 348 | |
| 349 | fl_clip_box(ox, oy, ow, oh, ox, oy, ow, oh); |
| 350 | |
Pierre Ossman | 0630434 | 2017-04-28 10:20:29 +0200 | [diff] [blame] | 351 | if ((ow != 0) && (oh != 0)) { |
| 352 | if (offscreen) |
| 353 | statsGraph->blend(offscreen, ox - X, oy - Y, ox, oy, ow, oh, 204); |
| 354 | else |
| 355 | statsGraph->blend(ox - X, oy - Y, ox, oy, ow, oh, 204); |
| 356 | } |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 359 | // Overlay (if active) |
| 360 | if (overlay) { |
| 361 | int ox, oy, ow, oh; |
| 362 | |
| 363 | ox = X = (w() - overlay->width()) / 2; |
| 364 | oy = Y = 50; |
| 365 | ow = overlay->width(); |
| 366 | oh = overlay->height(); |
| 367 | |
| 368 | fl_clip_box(ox, oy, ow, oh, ox, oy, ow, oh); |
| 369 | |
Pierre Ossman | 0630434 | 2017-04-28 10:20:29 +0200 | [diff] [blame] | 370 | if ((ow != 0) && (oh != 0)) { |
| 371 | if (offscreen) |
| 372 | overlay->blend(offscreen, ox - X, oy - Y, ox, oy, ow, oh, overlayAlpha); |
| 373 | else |
| 374 | overlay->blend(ox - X, oy - Y, ox, oy, ow, oh, overlayAlpha); |
| 375 | } |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 376 | } |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 377 | |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 378 | // Flush offscreen surface to screen |
| 379 | if (offscreen) { |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 380 | fl_clip_box(0, 0, w(), h(), X, Y, W, H); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 381 | offscreen->draw(X, Y, X, Y, W, H); |
| 382 | } |
| 383 | |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 384 | fl_pop_clip(); |
| 385 | fl_pop_clip(); |
| 386 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 387 | // Finally the scrollbars |
| 388 | |
| 389 | if (redraw) { |
| 390 | draw_child(*hscroll); |
| 391 | draw_child(*vscroll); |
| 392 | } else { |
| 393 | update_child(*hscroll); |
| 394 | update_child(*vscroll); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
Pierre Ossman | 2fa63f8 | 2016-12-05 15:26:21 +0100 | [diff] [blame] | 399 | void DesktopWindow::setLEDState(unsigned int state) |
| 400 | { |
| 401 | viewport->setLEDState(state); |
| 402 | } |
| 403 | |
| 404 | |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 405 | void DesktopWindow::resize(int x, int y, int w, int h) |
| 406 | { |
Pierre Ossman | 9f32e3c | 2012-08-23 14:40:52 +0000 | [diff] [blame] | 407 | bool resizing; |
| 408 | |
Pierre Ossman | 0e59346 | 2012-09-03 09:43:23 +0000 | [diff] [blame] | 409 | #if ! (defined(WIN32) || defined(__APPLE__)) |
| 410 | // X11 window managers will treat a resize to cover the entire |
| 411 | // 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] | 412 | if (!fullscreen_active()) { |
Pierre Ossman | 1f884e0 | 2012-10-30 10:26:23 +0000 | [diff] [blame] | 413 | bool resize_req; |
Pierre Ossman | 0e59346 | 2012-09-03 09:43:23 +0000 | [diff] [blame] | 414 | |
Pierre Ossman | 1f884e0 | 2012-10-30 10:26:23 +0000 | [diff] [blame] | 415 | // If there is no X11 window, then this must be a resize request, |
| 416 | // not a notification from the X server. |
| 417 | if (!shown()) |
| 418 | resize_req = true; |
| 419 | else { |
| 420 | // Otherwise we need to get the real window coordinates to tell |
| 421 | // the difference |
| 422 | XWindowAttributes actual; |
| 423 | Window cr; |
| 424 | int wx, wy; |
Pierre Ossman | 0e59346 | 2012-09-03 09:43:23 +0000 | [diff] [blame] | 425 | |
Pierre Ossman | 1f884e0 | 2012-10-30 10:26:23 +0000 | [diff] [blame] | 426 | XGetWindowAttributes(fl_display, fl_xid(this), &actual); |
| 427 | XTranslateCoordinates(fl_display, fl_xid(this), actual.root, |
| 428 | 0, 0, &wx, &wy, &cr); |
| 429 | |
| 430 | // Actual resize request? |
| 431 | if ((wx != x) || (wy != y) || |
| 432 | (actual.width != w) || (actual.height != h)) |
| 433 | resize_req = true; |
| 434 | else |
| 435 | resize_req = false; |
| 436 | } |
| 437 | |
| 438 | if (resize_req) { |
Pierre Ossman | 9e0e754 | 2012-10-03 12:21:54 +0000 | [diff] [blame] | 439 | for (int i = 0;i < Fl::screen_count();i++) { |
| 440 | int sx, sy, sw, sh; |
| 441 | |
| 442 | Fl::screen_xywh(sx, sy, sw, sh, i); |
| 443 | |
| 444 | if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) { |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame] | 445 | vlog.info(_("Adjusting window size to avoid accidental full screen request")); |
Pierre Ossman | 9e0e754 | 2012-10-03 12:21:54 +0000 | [diff] [blame] | 446 | // Assume a panel of some form and adjust the height |
| 447 | y += 20; |
| 448 | h -= 40; |
| 449 | } |
Pierre Ossman | 0e59346 | 2012-09-03 09:43:23 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | #endif |
| 454 | |
Pierre Ossman | 9f32e3c | 2012-08-23 14:40:52 +0000 | [diff] [blame] | 455 | if ((this->w() != w) || (this->h() != h)) |
| 456 | resizing = true; |
| 457 | else |
| 458 | resizing = false; |
| 459 | |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 460 | Fl_Window::resize(x, y, w, h); |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 461 | |
Pierre Ossman | 9f32e3c | 2012-08-23 14:40:52 +0000 | [diff] [blame] | 462 | if (resizing) { |
| 463 | // Try to get the remote size to match our window size, provided |
| 464 | // the following conditions are true: |
| 465 | // |
| 466 | // a) The user has this feature turned on |
| 467 | // b) The server supports it |
| 468 | // c) We're not still waiting for a chance to handle DesktopSize |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 469 | // d) We're not still waiting for startup fullscreen to kick in |
Pierre Ossman | 9f32e3c | 2012-08-23 14:40:52 +0000 | [diff] [blame] | 470 | // |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 471 | if (not firstUpdate and not delayedFullscreen and |
| 472 | ::remoteResize and cc->cp.supportsSetDesktopSize) { |
Pierre Ossman | 9f32e3c | 2012-08-23 14:40:52 +0000 | [diff] [blame] | 473 | // We delay updating the remote desktop as we tend to get a flood |
| 474 | // of resize events as the user is dragging the window. |
| 475 | Fl::remove_timeout(handleResizeTimeout, this); |
| 476 | Fl::add_timeout(0.5, handleResizeTimeout, this); |
| 477 | } |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 478 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 479 | repositionWidgets(); |
Pierre Ossman | 9f32e3c | 2012-08-23 14:40:52 +0000 | [diff] [blame] | 480 | } |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 484 | void DesktopWindow::menuOverlay(void* data) |
| 485 | { |
| 486 | DesktopWindow *self; |
| 487 | |
| 488 | self = (DesktopWindow*)data; |
| 489 | self->setOverlay(_("Press %s to open the context menu"), |
| 490 | (const char*)menuKey); |
| 491 | } |
| 492 | |
| 493 | void DesktopWindow::setOverlay(const char* text, ...) |
| 494 | { |
| 495 | va_list ap; |
| 496 | char textbuf[1024]; |
| 497 | |
| 498 | Fl_Image_Surface *surface; |
| 499 | |
| 500 | Fl_RGB_Image* imageText; |
| 501 | Fl_RGB_Image* image; |
| 502 | |
| 503 | unsigned char* buffer; |
| 504 | |
| 505 | int x, y; |
| 506 | int w, h; |
| 507 | |
| 508 | unsigned char* a; |
| 509 | const unsigned char* b; |
| 510 | |
| 511 | delete overlay; |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 512 | Fl::remove_timeout(updateOverlay, this); |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 513 | |
| 514 | va_start(ap, text); |
| 515 | vsnprintf(textbuf, sizeof(textbuf), text, ap); |
| 516 | textbuf[sizeof(textbuf)-1] = '\0'; |
| 517 | va_end(ap); |
| 518 | |
| 519 | #if !defined(WIN32) && !defined(__APPLE__) |
| 520 | // FLTK < 1.3.5 crashes if fl_gc is unset |
| 521 | if (!fl_gc) |
| 522 | fl_gc = XDefaultGC(fl_display, 0); |
| 523 | #endif |
| 524 | |
| 525 | fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2); |
Pierre Ossman | 2e7c744 | 2017-02-24 09:23:47 +0100 | [diff] [blame] | 526 | w = 0; |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 527 | fl_measure(textbuf, w, h); |
| 528 | |
| 529 | // Margins |
| 530 | w += 80; |
| 531 | h += 40; |
| 532 | |
| 533 | surface = new Fl_Image_Surface(w, h); |
| 534 | surface->set_current(); |
| 535 | |
| 536 | fl_rectf(0, 0, w, h, 0, 0, 0); |
| 537 | |
| 538 | fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2); |
| 539 | fl_color(FL_WHITE); |
| 540 | fl_draw(textbuf, 40, 20 + fl_height() - fl_descent()); |
| 541 | |
| 542 | imageText = surface->image(); |
| 543 | delete surface; |
| 544 | |
| 545 | Fl_Display_Device::display_device()->set_current(); |
| 546 | |
| 547 | buffer = new unsigned char[w * h * 4]; |
| 548 | image = new Fl_RGB_Image(buffer, w, h, 4); |
| 549 | |
| 550 | a = buffer; |
| 551 | for (x = 0;x < image->w() * image->h();x++) { |
| 552 | a[0] = a[1] = a[2] = 0x40; |
| 553 | a[3] = 0xcc; |
| 554 | a += 4; |
| 555 | } |
| 556 | |
| 557 | a = buffer; |
| 558 | b = (const unsigned char*)imageText->data()[0]; |
| 559 | for (y = 0;y < h;y++) { |
| 560 | for (x = 0;x < w;x++) { |
| 561 | unsigned char alpha; |
| 562 | alpha = *b; |
| 563 | a[0] = (unsigned)a[0] * (255 - alpha) / 255 + alpha; |
| 564 | a[1] = (unsigned)a[1] * (255 - alpha) / 255 + alpha; |
| 565 | a[2] = (unsigned)a[2] * (255 - alpha) / 255 + alpha; |
| 566 | a[3] = 255 - (255 - a[3]) * (255 - alpha) / 255; |
| 567 | a += 4; |
| 568 | b += imageText->d(); |
| 569 | } |
| 570 | if (imageText->ld() != 0) |
| 571 | b += imageText->ld() - w * imageText->d(); |
| 572 | } |
| 573 | |
| 574 | delete imageText; |
| 575 | |
| 576 | x = (this->w() - image->w()) / 2; |
| 577 | y = 50; |
| 578 | w = image->w(); |
| 579 | h = image->h(); |
| 580 | |
| 581 | overlay = new Surface(image); |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 582 | overlayAlpha = 0; |
| 583 | gettimeofday(&overlayStart, NULL); |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 584 | |
| 585 | delete image; |
| 586 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 587 | Fl::add_timeout(1.0/60, updateOverlay, this); |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 590 | void DesktopWindow::updateOverlay(void *data) |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 591 | { |
| 592 | DesktopWindow *self; |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 593 | unsigned elapsed; |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 594 | |
| 595 | self = (DesktopWindow*)data; |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 596 | |
| 597 | elapsed = msSince(&self->overlayStart); |
| 598 | |
| 599 | if (elapsed < 500) { |
| 600 | self->overlayAlpha = (unsigned)255 * elapsed / 500; |
| 601 | Fl::add_timeout(1.0/60, updateOverlay, self); |
| 602 | } else if (elapsed < 3500) { |
| 603 | self->overlayAlpha = 255; |
| 604 | Fl::add_timeout(3.0, updateOverlay, self); |
| 605 | } else if (elapsed < 4000) { |
| 606 | self->overlayAlpha = (unsigned)255 * (4000 - elapsed) / 500; |
| 607 | Fl::add_timeout(1.0/60, updateOverlay, self); |
| 608 | } else { |
| 609 | delete self->overlay; |
| 610 | self->overlay = NULL; |
| 611 | } |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 612 | |
| 613 | self->damage(FL_DAMAGE_USER1); |
| 614 | } |
| 615 | |
| 616 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 617 | int DesktopWindow::handle(int event) |
| 618 | { |
| 619 | switch (event) { |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 620 | case FL_FULLSCREEN: |
Pierre Ossman | 29e4a16 | 2011-11-21 14:03:31 +0000 | [diff] [blame] | 621 | fullScreen.setParam(fullscreen_active()); |
| 622 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 623 | // Update scroll bars |
| 624 | repositionWidgets(); |
Pierre Ossman | 2f3a04e | 2012-10-24 12:15:19 +0000 | [diff] [blame] | 625 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 626 | if (!fullscreenSystemKeys) |
| 627 | break; |
| 628 | |
| 629 | if (fullscreen_active()) |
| 630 | grabKeyboard(); |
| 631 | else |
| 632 | ungrabKeyboard(); |
| 633 | |
| 634 | break; |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 635 | |
| 636 | case FL_ENTER: |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 637 | if (keyboardGrabbed) |
| 638 | grabPointer(); |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 639 | case FL_LEAVE: |
| 640 | case FL_DRAG: |
| 641 | case FL_MOVE: |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 642 | // We don't get FL_LEAVE with a grabbed pointer, so check manually |
| 643 | if (mouseGrabbed) { |
| 644 | if ((Fl::event_x() < 0) || (Fl::event_x() >= w()) || |
| 645 | (Fl::event_y() < 0) || (Fl::event_y() >= h())) { |
| 646 | ungrabPointer(); |
| 647 | } |
| 648 | } |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 649 | if (fullscreen_active()) { |
| 650 | if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) || |
| 651 | ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) || |
| 652 | ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) || |
| 653 | ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) { |
| 654 | if (!Fl::has_timeout(handleEdgeScroll, this)) |
| 655 | Fl::add_timeout(0.1, handleEdgeScroll, this); |
| 656 | } |
| 657 | } |
Pierre Ossman | b2e712b | 2012-09-10 11:46:08 +0000 | [diff] [blame] | 658 | // Continue processing so that the viewport also gets mouse events |
| 659 | break; |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | return Fl_Window::handle(event); |
| 663 | } |
| 664 | |
| 665 | |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 666 | int DesktopWindow::fltkHandle(int event, Fl_Window *win) |
| 667 | { |
| 668 | int ret; |
| 669 | |
| 670 | ret = Fl::handle_(event, win); |
| 671 | |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 672 | // This is hackish and the result of the dodgy focus handling in FLTK. |
| 673 | // The basic problem is that FLTK's view of focus and the system's tend |
| 674 | // to differ, and as a result we do not see all the FL_FOCUS events we |
| 675 | // need. Fortunately we can grab them here... |
| 676 | |
| 677 | DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win); |
| 678 | |
Pierre Ossman | 1668cfa | 2016-12-10 17:13:40 +0100 | [diff] [blame] | 679 | if (dw) { |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 680 | switch (event) { |
| 681 | case FL_FOCUS: |
Pierre Ossman | 1668cfa | 2016-12-10 17:13:40 +0100 | [diff] [blame] | 682 | if (fullscreenSystemKeys) { |
| 683 | // FIXME: We reassert the keyboard grabbing on focus as FLTK there are |
| 684 | // some issues we need to work around: |
| 685 | // a) Fl::grab(0) on X11 will release the keyboard grab for us. |
| 686 | // b) Gaining focus on the system level causes FLTK to switch |
| 687 | // window level on OS X. |
| 688 | if (dw->fullscreen_active()) |
| 689 | dw->grabKeyboard(); |
| 690 | } |
| 691 | |
| 692 | // We may have gotten our lock keys out of sync with the server |
| 693 | // whilst we didn't have focus. Try to sort this out. |
| 694 | dw->viewport->pushLEDState(); |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 695 | break; |
| 696 | |
Pierre Ossman | b136980 | 2012-10-17 07:59:36 +0000 | [diff] [blame] | 697 | case FL_UNFOCUS: |
Pierre Ossman | 1668cfa | 2016-12-10 17:13:40 +0100 | [diff] [blame] | 698 | if (fullscreenSystemKeys) { |
| 699 | // FIXME: We need to relinquish control when the entire window loses |
| 700 | // focus as it is very tied to this specific window on some |
| 701 | // platforms and we want to be able to open subwindows. |
| 702 | dw->ungrabKeyboard(); |
| 703 | } |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 704 | break; |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 705 | |
| 706 | case FL_RELEASE: |
| 707 | // We usually fail to grab the mouse if a mouse button was |
| 708 | // pressed when we gained focus (e.g. clicking on our window), |
| 709 | // so we may need to try again when the button is released. |
| 710 | // (We do it here rather than handle() because a window does not |
| 711 | // see FL_RELEASE events if a child widget grabs it first) |
| 712 | if (dw->keyboardGrabbed && !dw->mouseGrabbed) |
| 713 | dw->grabPointer(); |
| 714 | break; |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 715 | } |
| 716 | } |
Pierre Ossman | a4f0f18 | 2011-06-14 13:36:57 +0000 | [diff] [blame] | 717 | |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 722 | void DesktopWindow::fullscreen_on() |
| 723 | { |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 724 | if (not fullScreenAllMonitors) |
| 725 | fullscreen_screens(-1, -1, -1, -1); |
| 726 | else { |
| 727 | int top, bottom, left, right; |
| 728 | int top_y, bottom_y, left_x, right_x; |
| 729 | |
| 730 | int sx, sy, sw, sh; |
| 731 | |
| 732 | top = bottom = left = right = 0; |
| 733 | |
| 734 | Fl::screen_xywh(sx, sy, sw, sh, 0); |
| 735 | top_y = sy; |
| 736 | bottom_y = sy + sh; |
| 737 | left_x = sx; |
| 738 | right_x = sx + sw; |
| 739 | |
| 740 | for (int i = 1;i < Fl::screen_count();i++) { |
| 741 | Fl::screen_xywh(sx, sy, sw, sh, i); |
| 742 | if (sy < top_y) { |
| 743 | top = i; |
| 744 | top_y = sy; |
| 745 | } |
| 746 | if ((sy + sh) > bottom_y) { |
| 747 | bottom = i; |
| 748 | bottom_y = sy + sh; |
| 749 | } |
| 750 | if (sx < left_x) { |
| 751 | left = i; |
| 752 | left_x = sx; |
| 753 | } |
| 754 | if ((sx + sw) > right_x) { |
| 755 | right = i; |
| 756 | right_x = sx + sw; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | fullscreen_screens(top, bottom, left, right); |
| 761 | } |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 762 | |
| 763 | fullscreen(); |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 766 | void DesktopWindow::grabKeyboard() |
| 767 | { |
| 768 | // Grabbing the keyboard is fairly safe as FLTK reroutes events to the |
| 769 | // correct widget regardless of which low level window got the system |
| 770 | // event. |
| 771 | |
| 772 | // FIXME: Push this stuff into FLTK. |
| 773 | |
| 774 | #if defined(WIN32) |
| 775 | int ret; |
| 776 | |
| 777 | ret = win32_enable_lowlevel_keyboard(fl_xid(this)); |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 778 | if (ret != 0) { |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 779 | vlog.error(_("Failure grabbing keyboard")); |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 780 | return; |
| 781 | } |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 782 | #elif defined(__APPLE__) |
| 783 | int ret; |
| 784 | |
Pierre Ossman | 56610fb | 2015-01-27 16:28:15 +0100 | [diff] [blame] | 785 | ret = cocoa_capture_display(this, fullScreenAllMonitors); |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 786 | if (ret != 0) { |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 787 | vlog.error(_("Failure grabbing keyboard")); |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 788 | return; |
| 789 | } |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 790 | #else |
| 791 | int ret; |
| 792 | |
| 793 | ret = XGrabKeyboard(fl_display, fl_xid(this), True, |
Peter Åstrand | f52860b | 2011-07-18 07:42:16 +0000 | [diff] [blame] | 794 | GrabModeAsync, GrabModeAsync, CurrentTime); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 795 | if (ret) { |
| 796 | if (ret == AlreadyGrabbed) { |
| 797 | // It seems like we can race with the WM in some cases. |
| 798 | // Try again in a bit. |
| 799 | if (!Fl::has_timeout(handleGrab, this)) |
| 800 | Fl::add_timeout(0.500, handleGrab, this); |
| 801 | } else { |
| 802 | vlog.error(_("Failure grabbing keyboard")); |
| 803 | } |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 804 | return; |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 805 | } |
| 806 | #endif |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 807 | |
| 808 | keyboardGrabbed = true; |
| 809 | |
| 810 | if (contains(Fl::belowmouse())) |
| 811 | grabPointer(); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | |
| 815 | void DesktopWindow::ungrabKeyboard() |
| 816 | { |
| 817 | Fl::remove_timeout(handleGrab, this); |
| 818 | |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 819 | keyboardGrabbed = false; |
| 820 | |
| 821 | ungrabPointer(); |
| 822 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 823 | #if defined(WIN32) |
| 824 | win32_disable_lowlevel_keyboard(fl_xid(this)); |
| 825 | #elif defined(__APPLE__) |
| 826 | cocoa_release_display(this); |
| 827 | #else |
| 828 | // FLTK has a grab so lets not mess with it |
| 829 | if (Fl::grab()) |
| 830 | return; |
| 831 | |
Pierre Ossman | 841e9f3 | 2017-10-02 11:05:10 +0200 | [diff] [blame] | 832 | XUngrabKeyboard(fl_display, CurrentTime); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 833 | #endif |
| 834 | } |
| 835 | |
| 836 | |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 837 | void DesktopWindow::grabPointer() |
| 838 | { |
| 839 | #if !defined(WIN32) && !defined(__APPLE__) |
| 840 | int ret; |
| 841 | |
| 842 | // We also need to grab the pointer as some WMs like to grab buttons |
| 843 | // combined with modifies (e.g. Alt+Button0 in metacity). |
| 844 | ret = XGrabPointer(fl_display, fl_xid(this), True, |
| 845 | ButtonPressMask|ButtonReleaseMask| |
| 846 | ButtonMotionMask|PointerMotionMask, |
| 847 | GrabModeAsync, GrabModeAsync, |
| 848 | None, None, CurrentTime); |
| 849 | if (ret) { |
| 850 | // Having a button pressed prevents us from grabbing, we make |
| 851 | // a new attempt in fltkHandle() |
| 852 | if (ret == AlreadyGrabbed) |
| 853 | return; |
| 854 | vlog.error(_("Failure grabbing mouse")); |
| 855 | return; |
| 856 | } |
| 857 | #endif |
| 858 | |
| 859 | mouseGrabbed = true; |
| 860 | } |
| 861 | |
| 862 | |
| 863 | void DesktopWindow::ungrabPointer() |
| 864 | { |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 865 | mouseGrabbed = false; |
| 866 | #if !defined(WIN32) && !defined(__APPLE__) |
Peter Åstrand (astrand) | 5f49725 | 2018-01-15 08:27:19 +0100 | [diff] [blame] | 867 | XUngrabPointer(fl_display, CurrentTime); |
Pierre Ossman | 7e546fe | 2017-09-18 16:05:48 +0200 | [diff] [blame] | 868 | #endif |
| 869 | } |
| 870 | |
| 871 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 872 | void DesktopWindow::handleGrab(void *data) |
| 873 | { |
| 874 | DesktopWindow *self = (DesktopWindow*)data; |
| 875 | |
| 876 | assert(self); |
| 877 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 878 | if (!fullscreenSystemKeys) |
| 879 | return; |
| 880 | if (!self->fullscreen_active()) |
| 881 | return; |
| 882 | |
| 883 | self->grabKeyboard(); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | |
Peter Åstrand | 49b1157 | 2012-08-01 08:09:09 +0000 | [diff] [blame] | 887 | #define _NET_WM_STATE_ADD 1 /* add/set property */ |
| 888 | void DesktopWindow::maximizeWindow() |
| 889 | { |
| 890 | #if defined(WIN32) |
Pierre Ossman | 897067b | 2012-10-11 09:17:19 +0000 | [diff] [blame] | 891 | // We cannot use ShowWindow() in full screen mode as it will |
| 892 | // resize things implicitly. Fortunately modifying the style |
| 893 | // directly results in a maximized state once we leave full screen. |
Pierre Ossman | 897067b | 2012-10-11 09:17:19 +0000 | [diff] [blame] | 894 | if (fullscreen_active()) { |
| 895 | WINDOWINFO wi; |
| 896 | wi.cbSize = sizeof(WINDOWINFO); |
| 897 | GetWindowInfo(fl_xid(this), &wi); |
| 898 | SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE); |
| 899 | } else |
Pierre Ossman | 897067b | 2012-10-11 09:17:19 +0000 | [diff] [blame] | 900 | ShowWindow(fl_xid(this), SW_MAXIMIZE); |
Peter Åstrand | 49b1157 | 2012-08-01 08:09:09 +0000 | [diff] [blame] | 901 | #elif defined(__APPLE__) |
Pierre Ossman | 002cc5d | 2012-10-02 14:45:10 +0000 | [diff] [blame] | 902 | // OS X is somewhat strange and does not really have a concept of a |
| 903 | // maximized window, so we can simply resize the window to the workarea. |
| 904 | // Note that we shouldn't do this whilst in full screen as that will |
| 905 | // incorrectly adjust things. |
Pierre Ossman | 002cc5d | 2012-10-02 14:45:10 +0000 | [diff] [blame] | 906 | if (fullscreen_active()) |
| 907 | return; |
Peter Åstrand | 49b1157 | 2012-08-01 08:09:09 +0000 | [diff] [blame] | 908 | int X, Y, W, H; |
| 909 | Fl::screen_work_area(X, Y, W, H, this->x(), this->y()); |
| 910 | size(W, H); |
| 911 | #else |
| 912 | // X11 |
| 913 | fl_open_display(); |
| 914 | Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0); |
| 915 | Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0); |
| 916 | Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0); |
| 917 | |
| 918 | XEvent e; |
| 919 | e.xany.type = ClientMessage; |
| 920 | e.xany.window = fl_xid(this); |
| 921 | e.xclient.message_type = net_wm_state; |
| 922 | e.xclient.format = 32; |
| 923 | e.xclient.data.l[0] = _NET_WM_STATE_ADD; |
| 924 | e.xclient.data.l[1] = net_wm_state_maximized_vert; |
| 925 | e.xclient.data.l[2] = net_wm_state_maximized_horz; |
| 926 | e.xclient.data.l[3] = 0; |
| 927 | e.xclient.data.l[4] = 0; |
| 928 | XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e); |
| 929 | #endif |
| 930 | } |
| 931 | |
| 932 | |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 933 | void DesktopWindow::handleDesktopSize() |
| 934 | { |
Pierre Ossman | 135906e | 2015-04-27 12:48:47 +0200 | [diff] [blame] | 935 | if (strcmp(desktopSize, "") != 0) { |
Pierre Ossman | bad31c2 | 2014-11-11 13:59:01 +0100 | [diff] [blame] | 936 | int width, height; |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 937 | |
Pierre Ossman | bad31c2 | 2014-11-11 13:59:01 +0100 | [diff] [blame] | 938 | // An explicit size has been requested |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 939 | |
Pierre Ossman | 135906e | 2015-04-27 12:48:47 +0200 | [diff] [blame] | 940 | if (sscanf(desktopSize, "%dx%d", &width, &height) != 2) |
Pierre Ossman | bad31c2 | 2014-11-11 13:59:01 +0100 | [diff] [blame] | 941 | return; |
| 942 | |
| 943 | remoteResize(width, height); |
| 944 | } else if (::remoteResize) { |
| 945 | // No explicit size, but remote resizing is on so make sure it |
| 946 | // matches whatever size the window ended up being |
| 947 | remoteResize(w(), h()); |
| 948 | } |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 952 | void DesktopWindow::handleResizeTimeout(void *data) |
| 953 | { |
| 954 | DesktopWindow *self = (DesktopWindow *)data; |
| 955 | |
| 956 | assert(self); |
| 957 | |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 958 | self->remoteResize(self->w(), self->h()); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 962 | void DesktopWindow::remoteResize(int width, int height) |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 963 | { |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 964 | ScreenSet layout; |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 965 | ScreenSet::iterator iter; |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 966 | |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame] | 967 | if (!fullscreen_active() || (width > w()) || (height > h())) { |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame] | 968 | // In windowed mode (or the framebuffer is so large that we need |
| 969 | // to scroll) we just report a single virtual screen that covers |
| 970 | // the entire framebuffer. |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 971 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 972 | layout = cc->cp.screenLayout; |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 973 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 974 | // Not sure why we have no screens, but adding a new one should be |
| 975 | // safe as there is nothing to conflict with... |
| 976 | if (layout.num_screens() == 0) |
| 977 | layout.add_screen(rfb::Screen()); |
| 978 | else if (layout.num_screens() != 1) { |
| 979 | // More than one screen. Remove all but the first (which we |
| 980 | // assume is the "primary"). |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 981 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 982 | while (true) { |
| 983 | iter = layout.begin(); |
| 984 | ++iter; |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 985 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 986 | if (iter == layout.end()) |
| 987 | break; |
| 988 | |
| 989 | layout.remove_screen(iter->id); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | // Resize the remaining single screen to the complete framebuffer |
| 994 | layout.begin()->dimensions.tl.x = 0; |
| 995 | layout.begin()->dimensions.tl.y = 0; |
| 996 | layout.begin()->dimensions.br.x = width; |
| 997 | layout.begin()->dimensions.br.y = height; |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 998 | } else { |
| 999 | int i; |
| 1000 | rdr::U32 id; |
| 1001 | int sx, sy, sw, sh; |
Pierre Ossman | 4f0647d | 2017-01-04 15:34:26 +0100 | [diff] [blame] | 1002 | rfb::Rect viewport_rect, screen_rect; |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 1003 | |
| 1004 | // In full screen we report all screens that are fully covered. |
| 1005 | |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame] | 1006 | viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2, |
| 1007 | width, height); |
Pierre Ossman | 93d2d92 | 2012-07-20 12:32:52 +0000 | [diff] [blame] | 1008 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 1009 | // If we can find a matching screen in the existing set, we use |
| 1010 | // that, otherwise we create a brand new screen. |
| 1011 | // |
| 1012 | // FIXME: We should really track screens better so we can handle |
| 1013 | // a resized one. |
| 1014 | // |
| 1015 | for (i = 0;i < Fl::screen_count();i++) { |
| 1016 | Fl::screen_xywh(sx, sy, sw, sh, i); |
| 1017 | |
Pierre Ossman | 93d2d92 | 2012-07-20 12:32:52 +0000 | [diff] [blame] | 1018 | // Check that the screen is fully inside the framebuffer |
| 1019 | screen_rect.setXYWH(sx, sy, sw, sh); |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame] | 1020 | if (!screen_rect.enclosed_by(viewport_rect)) |
Pierre Ossman | 93d2d92 | 2012-07-20 12:32:52 +0000 | [diff] [blame] | 1021 | continue; |
| 1022 | |
Pierre Ossman | f44f6c0 | 2012-07-20 12:39:27 +0000 | [diff] [blame] | 1023 | // Adjust the coordinates so they are relative to our viewport |
| 1024 | sx -= viewport_rect.tl.x; |
| 1025 | sy -= viewport_rect.tl.y; |
| 1026 | |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 1027 | // Look for perfectly matching existing screen... |
| 1028 | for (iter = cc->cp.screenLayout.begin(); |
| 1029 | iter != cc->cp.screenLayout.end(); ++iter) { |
| 1030 | if ((iter->dimensions.tl.x == sx) && |
| 1031 | (iter->dimensions.tl.y == sy) && |
| 1032 | (iter->dimensions.width() == sw) && |
| 1033 | (iter->dimensions.height() == sh)) |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | // Found it? |
| 1038 | if (iter != cc->cp.screenLayout.end()) { |
| 1039 | layout.add_screen(*iter); |
| 1040 | continue; |
| 1041 | } |
| 1042 | |
| 1043 | // Need to add a new one, which means we need to find an unused id |
| 1044 | while (true) { |
| 1045 | id = rand(); |
| 1046 | for (iter = cc->cp.screenLayout.begin(); |
| 1047 | iter != cc->cp.screenLayout.end(); ++iter) { |
| 1048 | if (iter->id == id) |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | if (iter == cc->cp.screenLayout.end()) |
| 1053 | break; |
| 1054 | } |
| 1055 | |
| 1056 | layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0)); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 1057 | } |
Pierre Ossman | 72b4adf | 2012-07-20 14:11:26 +0000 | [diff] [blame] | 1058 | |
| 1059 | // If the viewport doesn't match a physical screen, then we might |
| 1060 | // end up with no screens in the layout. Add a fake one... |
| 1061 | if (layout.num_screens() == 0) |
| 1062 | layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0)); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 1063 | } |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 1064 | |
| 1065 | // Do we actually change anything? |
| 1066 | if ((width == cc->cp.width) && |
| 1067 | (height == cc->cp.height) && |
| 1068 | (layout == cc->cp.screenLayout)) |
| 1069 | return; |
| 1070 | |
Pierre Ossman | 9018af4 | 2015-01-26 15:15:47 +0100 | [diff] [blame] | 1071 | char buffer[2048]; |
| 1072 | vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d", |
| 1073 | cc->cp.width, cc->cp.height, width, height); |
| 1074 | layout.print(buffer, sizeof(buffer)); |
| 1075 | vlog.debug("%s", buffer); |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 1076 | |
| 1077 | if (!layout.validate(width, height)) { |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame] | 1078 | vlog.error(_("Invalid screen layout computed for resize request!")); |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 1079 | return; |
| 1080 | } |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 1081 | |
| 1082 | cc->writer()->writeSetDesktopSize(width, height, layout); |
| 1083 | } |
| 1084 | |
| 1085 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1086 | void DesktopWindow::repositionWidgets() |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 1087 | { |
| 1088 | int new_x, new_y; |
| 1089 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1090 | // Viewport position |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 1091 | |
| 1092 | new_x = viewport->x(); |
| 1093 | new_y = viewport->y(); |
| 1094 | |
| 1095 | if (w() > viewport->w()) |
| 1096 | new_x = (w() - viewport->w()) / 2; |
| 1097 | else { |
| 1098 | if (viewport->x() > 0) |
| 1099 | new_x = 0; |
| 1100 | else if (w() > (viewport->x() + viewport->w())) |
| 1101 | new_x = w() - viewport->w(); |
| 1102 | } |
| 1103 | |
| 1104 | // Same thing for y axis |
| 1105 | if (h() > viewport->h()) |
| 1106 | new_y = (h() - viewport->h()) / 2; |
| 1107 | else { |
| 1108 | if (viewport->y() > 0) |
| 1109 | new_y = 0; |
| 1110 | else if (h() > (viewport->y() + viewport->h())) |
| 1111 | new_y = h() - viewport->h(); |
| 1112 | } |
| 1113 | |
| 1114 | if ((new_x != viewport->x()) || (new_y != viewport->y())) { |
| 1115 | viewport->position(new_x, new_y); |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1116 | damage(FL_DAMAGE_SCROLL); |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 1117 | } |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1118 | |
| 1119 | // Scrollbars visbility |
| 1120 | |
Luke Shumaker | 0a8c4d4 | 2017-05-23 14:03:15 -0400 | [diff] [blame] | 1121 | if (fullscreen_active()) { |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1122 | hscroll->hide(); |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1123 | vscroll->hide(); |
Luke Shumaker | 0a8c4d4 | 2017-05-23 14:03:15 -0400 | [diff] [blame] | 1124 | } else { |
| 1125 | // Decide whether to show a scrollbar by checking if the window |
| 1126 | // size (possibly minus scrollbar_size) is less than the viewport |
| 1127 | // (remote framebuffer) size. |
| 1128 | // |
| 1129 | // We decide whether to subtract scrollbar_size on an axis by |
| 1130 | // checking if the other axis *definitely* needs a scrollbar. You |
| 1131 | // might be tempted to think that this becomes a weird recursive |
| 1132 | // problem, but it isn't: If the window size is less than the |
| 1133 | // viewport size (without subtracting the scrollbar_size), then |
| 1134 | // that axis *definitely* needs a scrollbar; if the check changes |
| 1135 | // when we subtract scrollbar_size, then that axis only *maybe* |
| 1136 | // needs a scrollbar. If both axes only "maybe" need a scrollbar, |
| 1137 | // then neither does; so we don't need to recurse on the "maybe" |
| 1138 | // cases. |
| 1139 | |
| 1140 | if (w() - (h() < viewport->h() ? Fl::scrollbar_size() : 0) < viewport->w()) |
| 1141 | hscroll->show(); |
| 1142 | else |
| 1143 | hscroll->hide(); |
| 1144 | |
| 1145 | if (h() - (w() < viewport->w() ? Fl::scrollbar_size() : 0) < viewport->h()) |
| 1146 | vscroll->show(); |
| 1147 | else |
| 1148 | vscroll->hide(); |
| 1149 | } |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1150 | |
| 1151 | // Scrollbars positions |
| 1152 | |
| 1153 | hscroll->resize(0, h() - Fl::scrollbar_size(), |
| 1154 | w() - (vscroll->visible() ? Fl::scrollbar_size() : 0), |
| 1155 | Fl::scrollbar_size()); |
| 1156 | vscroll->resize(w() - Fl::scrollbar_size(), 0, |
| 1157 | Fl::scrollbar_size(), |
| 1158 | h() - (hscroll->visible() ? Fl::scrollbar_size() : 0)); |
| 1159 | |
| 1160 | // Scrollbars range |
| 1161 | |
| 1162 | hscroll->value(-viewport->x(), |
| 1163 | w() - (vscroll->visible() ? vscroll->w() : 0), |
| 1164 | 0, viewport->w()); |
| 1165 | vscroll->value(-viewport->y(), |
| 1166 | h() - (hscroll->visible() ? hscroll->h() : 0), |
| 1167 | 0, viewport->h()); |
| 1168 | hscroll->value(hscroll->clamp(hscroll->value())); |
| 1169 | vscroll->value(vscroll->clamp(vscroll->value())); |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 1172 | void DesktopWindow::handleClose(Fl_Widget *wnd, void *data) |
| 1173 | { |
| 1174 | exit_vncviewer(); |
| 1175 | } |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
| 1178 | void DesktopWindow::handleOptions(void *data) |
| 1179 | { |
| 1180 | DesktopWindow *self = (DesktopWindow*)data; |
| 1181 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 1182 | if (self->fullscreen_active() && fullscreenSystemKeys) |
| 1183 | self->grabKeyboard(); |
| 1184 | else |
| 1185 | self->ungrabKeyboard(); |
Pierre Ossman | 9191164 | 2011-05-26 14:57:51 +0000 | [diff] [blame] | 1186 | |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 1187 | if (fullScreen && !self->fullscreen_active()) |
Pierre Ossman | aae3891 | 2012-07-13 11:22:55 +0000 | [diff] [blame] | 1188 | self->fullscreen_on(); |
Pierre Ossman | ff47340 | 2012-07-04 11:27:47 +0000 | [diff] [blame] | 1189 | else if (!fullScreen && self->fullscreen_active()) |
Pierre Ossman | 9191164 | 2011-05-26 14:57:51 +0000 | [diff] [blame] | 1190 | self->fullscreen_off(); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 1191 | } |
Pierre Ossman | 4c4b66f | 2012-08-23 14:53:36 +0000 | [diff] [blame] | 1192 | |
| 1193 | void DesktopWindow::handleFullscreenTimeout(void *data) |
| 1194 | { |
| 1195 | DesktopWindow *self = (DesktopWindow *)data; |
| 1196 | |
| 1197 | assert(self); |
| 1198 | |
| 1199 | self->delayedFullscreen = false; |
| 1200 | |
| 1201 | if (self->delayedDesktopSize) { |
| 1202 | self->handleDesktopSize(); |
| 1203 | self->delayedDesktopSize = false; |
| 1204 | } |
| 1205 | } |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 1206 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1207 | void DesktopWindow::scrollTo(int x, int y) |
| 1208 | { |
| 1209 | x = hscroll->clamp(x); |
| 1210 | y = vscroll->clamp(y); |
| 1211 | |
| 1212 | hscroll->value(x); |
| 1213 | vscroll->value(y); |
| 1214 | |
Pierre Ossman | 1354881 | 2017-01-03 16:12:30 +0100 | [diff] [blame] | 1215 | // Scrollbar position results in inverse movement of |
| 1216 | // the viewport widget |
| 1217 | x = -x; |
| 1218 | y = -y; |
| 1219 | |
| 1220 | if ((viewport->x() == x) && (viewport->y() == y)) |
| 1221 | return; |
| 1222 | |
| 1223 | viewport->position(x, y); |
| 1224 | damage(FL_DAMAGE_SCROLL); |
| 1225 | } |
| 1226 | |
| 1227 | void DesktopWindow::handleScroll(Fl_Widget *widget, void *data) |
| 1228 | { |
| 1229 | DesktopWindow *self = (DesktopWindow *)data; |
| 1230 | |
| 1231 | self->scrollTo(self->hscroll->value(), self->vscroll->value()); |
| 1232 | } |
| 1233 | |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 1234 | void DesktopWindow::handleEdgeScroll(void *data) |
| 1235 | { |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 1236 | DesktopWindow *self = (DesktopWindow *)data; |
| 1237 | |
| 1238 | int mx, my; |
| 1239 | int dx, dy; |
| 1240 | |
| 1241 | assert(self); |
| 1242 | |
| 1243 | if (!self->fullscreen_active()) |
| 1244 | return; |
| 1245 | |
| 1246 | mx = Fl::event_x(); |
| 1247 | my = Fl::event_y(); |
| 1248 | |
| 1249 | dx = dy = 0; |
| 1250 | |
| 1251 | // Clamp mouse position in case it is outside the window |
| 1252 | if (mx < 0) |
| 1253 | mx = 0; |
| 1254 | if (mx > self->w()) |
| 1255 | mx = self->w(); |
| 1256 | if (my < 0) |
| 1257 | my = 0; |
| 1258 | if (my > self->h()) |
| 1259 | my = self->h(); |
| 1260 | |
| 1261 | if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE)) |
Pierre Ossman | dd13844 | 2012-09-03 09:45:40 +0000 | [diff] [blame] | 1262 | dx = EDGE_SCROLL_SPEED - |
| 1263 | EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE; |
| 1264 | if ((self->viewport->x() + self->viewport->w() > self->w()) && |
| 1265 | (mx > self->w() - EDGE_SCROLL_SIZE)) |
| 1266 | dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE - |
| 1267 | EDGE_SCROLL_SPEED; |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 1268 | if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE)) |
Pierre Ossman | dd13844 | 2012-09-03 09:45:40 +0000 | [diff] [blame] | 1269 | dy = EDGE_SCROLL_SPEED - |
| 1270 | EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE; |
| 1271 | if ((self->viewport->y() + self->viewport->h() > self->h()) && |
| 1272 | (my > self->h() - EDGE_SCROLL_SIZE)) |
| 1273 | dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE - |
| 1274 | EDGE_SCROLL_SPEED; |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 1275 | |
| 1276 | if ((dx == 0) && (dy == 0)) |
| 1277 | return; |
| 1278 | |
Luke Shumaker | e8d25d8 | 2017-05-23 02:16:27 -0400 | [diff] [blame] | 1279 | self->scrollTo(self->hscroll->value() - dx, self->vscroll->value() - dy); |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 1280 | |
| 1281 | Fl::repeat_timeout(0.1, handleEdgeScroll, data); |
Pierre Ossman | 1d867b6 | 2012-09-03 09:25:07 +0000 | [diff] [blame] | 1282 | } |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 1283 | |
| 1284 | void DesktopWindow::handleStatsTimeout(void *data) |
| 1285 | { |
| 1286 | DesktopWindow *self = (DesktopWindow*)data; |
| 1287 | |
Brian P. Hinz | fc21714 | 2017-03-09 19:25:39 -0500 | [diff] [blame] | 1288 | const size_t statsCount = sizeof(self->stats)/sizeof(self->stats[0]); |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 1289 | |
| 1290 | unsigned frame, pixels, pos; |
| 1291 | unsigned elapsed; |
| 1292 | |
| 1293 | const unsigned statsWidth = 200; |
| 1294 | const unsigned statsHeight = 100; |
| 1295 | const unsigned graphWidth = statsWidth - 10; |
| 1296 | const unsigned graphHeight = statsHeight - 25; |
| 1297 | |
| 1298 | Fl_Image_Surface *surface; |
| 1299 | Fl_RGB_Image *image; |
| 1300 | |
| 1301 | unsigned maxFPS, maxPPS, maxBPS; |
| 1302 | size_t i; |
| 1303 | |
| 1304 | char buffer[256]; |
| 1305 | |
| 1306 | frame = self->cc->getFrameCount(); |
| 1307 | pixels = self->cc->getPixelCount(); |
| 1308 | pos = self->cc->getPosition(); |
| 1309 | elapsed = msSince(&self->statsLastTime); |
| 1310 | if (elapsed < 1) |
| 1311 | elapsed = 1; |
| 1312 | |
Brian P. Hinz | fc21714 | 2017-03-09 19:25:39 -0500 | [diff] [blame] | 1313 | memmove(&self->stats[0], &self->stats[1], sizeof(self->stats[0])*(statsCount-1)); |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 1314 | |
| 1315 | self->stats[statsCount-1].fps = (frame - self->statsLastFrame) * 1000 / elapsed; |
| 1316 | self->stats[statsCount-1].pps = (pixels - self->statsLastPixels) * 1000 / elapsed; |
| 1317 | self->stats[statsCount-1].bps = (pos - self->statsLastPosition) * 1000 / elapsed; |
| 1318 | |
| 1319 | gettimeofday(&self->statsLastTime, NULL); |
| 1320 | self->statsLastFrame = frame; |
| 1321 | self->statsLastPixels = pixels; |
| 1322 | self->statsLastPosition = pos; |
| 1323 | |
| 1324 | #if !defined(WIN32) && !defined(__APPLE__) |
| 1325 | // FLTK < 1.3.5 crashes if fl_gc is unset |
| 1326 | if (!fl_gc) |
| 1327 | fl_gc = XDefaultGC(fl_display, 0); |
| 1328 | #endif |
| 1329 | |
| 1330 | surface = new Fl_Image_Surface(statsWidth, statsHeight); |
| 1331 | surface->set_current(); |
| 1332 | |
| 1333 | fl_rectf(0, 0, statsWidth, statsHeight, FL_BLACK); |
| 1334 | |
| 1335 | fl_rect(5, 5, graphWidth, graphHeight, FL_WHITE); |
| 1336 | |
| 1337 | maxFPS = maxPPS = maxBPS = 0; |
| 1338 | for (i = 0;i < statsCount;i++) { |
| 1339 | if (self->stats[i].fps > maxFPS) |
| 1340 | maxFPS = self->stats[i].fps; |
| 1341 | if (self->stats[i].pps > maxPPS) |
| 1342 | maxPPS = self->stats[i].pps; |
| 1343 | if (self->stats[i].bps > maxBPS) |
| 1344 | maxBPS = self->stats[i].bps; |
| 1345 | } |
| 1346 | |
| 1347 | if (maxFPS != 0) { |
| 1348 | fl_color(FL_GREEN); |
| 1349 | for (i = 0;i < statsCount-1;i++) { |
| 1350 | fl_line(5 + i * graphWidth / statsCount, |
| 1351 | 5 + graphHeight - graphHeight * self->stats[i].fps / maxFPS, |
| 1352 | 5 + (i+1) * graphWidth / statsCount, |
| 1353 | 5 + graphHeight - graphHeight * self->stats[i+1].fps / maxFPS); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | if (maxPPS != 0) { |
| 1358 | fl_color(FL_YELLOW); |
| 1359 | for (i = 0;i < statsCount-1;i++) { |
| 1360 | fl_line(5 + i * graphWidth / statsCount, |
| 1361 | 5 + graphHeight - graphHeight * self->stats[i].pps / maxPPS, |
| 1362 | 5 + (i+1) * graphWidth / statsCount, |
| 1363 | 5 + graphHeight - graphHeight * self->stats[i+1].pps / maxPPS); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | if (maxBPS != 0) { |
| 1368 | fl_color(FL_RED); |
| 1369 | for (i = 0;i < statsCount-1;i++) { |
| 1370 | fl_line(5 + i * graphWidth / statsCount, |
| 1371 | 5 + graphHeight - graphHeight * self->stats[i].bps / maxBPS, |
| 1372 | 5 + (i+1) * graphWidth / statsCount, |
| 1373 | 5 + graphHeight - graphHeight * self->stats[i+1].bps / maxBPS); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | fl_font(FL_HELVETICA, 10); |
| 1378 | |
| 1379 | fl_color(FL_GREEN); |
| 1380 | snprintf(buffer, sizeof(buffer), "%u fps", self->stats[statsCount-1].fps); |
| 1381 | fl_draw(buffer, 5, statsHeight - 5); |
| 1382 | |
| 1383 | fl_color(FL_YELLOW); |
Pierre Ossman | 52d3edb | 2018-02-26 17:00:28 +0100 | [diff] [blame] | 1384 | siPrefix(self->stats[statsCount-1].pps, "pix/s", |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 1385 | buffer, sizeof(buffer), 3); |
| 1386 | fl_draw(buffer, 5 + (statsWidth-10)/3, statsHeight - 5); |
| 1387 | |
| 1388 | fl_color(FL_RED); |
Pierre Ossman | 52d3edb | 2018-02-26 17:00:28 +0100 | [diff] [blame] | 1389 | siPrefix(self->stats[statsCount-1].bps * 8, "bps", |
| 1390 | buffer, sizeof(buffer), 3); |
Pierre Ossman | 921f6c8 | 2017-02-24 12:33:09 +0100 | [diff] [blame] | 1391 | fl_draw(buffer, 5 + (statsWidth-10)*2/3, statsHeight - 5); |
| 1392 | |
| 1393 | image = surface->image(); |
| 1394 | delete surface; |
| 1395 | |
| 1396 | Fl_Display_Device::display_device()->set_current(); |
| 1397 | |
| 1398 | delete self->statsGraph; |
| 1399 | self->statsGraph = new Surface(image); |
| 1400 | delete image; |
| 1401 | |
| 1402 | self->damage(FL_DAMAGE_CHILD, self->w() - statsWidth - 30, |
| 1403 | self->h() - statsHeight - 30, |
| 1404 | statsWidth, statsHeight); |
| 1405 | |
| 1406 | Fl::repeat_timeout(0.5, handleStatsTimeout, data); |
| 1407 | } |