blob: 47bdd5a661f33578d137e03a2145a04802e5af4e [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* 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 Åstrandc359f362011-08-23 12:04:46 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Pierre Ossman5156d5e2011-03-09 09:42:34 +000024#include <assert.h>
25#include <stdio.h>
26#include <string.h>
Pierre Ossman455566e2017-02-10 16:37:52 +010027#include <sys/time.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000028
Pierre Ossman5156d5e2011-03-09 09:42:34 +000029#include <rfb/LogWriter.h>
Pierre Ossmanff473402012-07-04 11:27:47 +000030#include <rfb/CMsgWriter.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000031
32#include "DesktopWindow.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000033#include "OptionsDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000034#include "i18n.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000035#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000036#include "vncviewer.h"
Pierre Ossmanff473402012-07-04 11:27:47 +000037#include "CConn.h"
Pierre Ossman3d74d882017-01-02 19:49:52 +010038#include "Surface.h"
Pierre Ossman947b48d2014-01-27 16:52:35 +010039#include "Viewport.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000040
Pierre Ossman947b48d2014-01-27 16:52:35 +010041#include <FL/Fl.H>
Pierre Ossman4f0647d2017-01-04 15:34:26 +010042#include <FL/Fl_Image_Surface.H>
Pierre Ossman13548812017-01-03 16:12:30 +010043#include <FL/Fl_Scrollbar.H>
44#include <FL/fl_draw.H>
DRCb65bb932011-06-24 03:17:00 +000045#include <FL/x.H>
46
Pierre Ossman407a5c32011-05-26 14:48:29 +000047#ifdef WIN32
48#include "win32.h"
49#endif
50
51#ifdef __APPLE__
52#include "cocoa.h"
53#endif
Pierre Ossman89f868a2011-04-11 11:59:31 +000054
Pierre Ossman1d867b62012-09-03 09:25:07 +000055#define EDGE_SCROLL_SIZE 32
56#define EDGE_SCROLL_SPEED 20
57
Pierre Ossman5156d5e2011-03-09 09:42:34 +000058using namespace rfb;
59
Pierre Ossman5156d5e2011-03-09 09:42:34 +000060static rfb::LogWriter vlog("DesktopWindow");
61
62DesktopWindow::DesktopWindow(int w, int h, const char *name,
63 const rfb::PixelFormat& serverPF,
64 CConn* cc_)
Pierre Ossman4f0647d2017-01-04 15:34:26 +010065 : Fl_Window(w, h), cc(cc_), offscreen(NULL), overlay(NULL),
66 firstUpdate(true),
Pierre Ossman921f6c82017-02-24 12:33:09 +010067 delayedFullscreen(false), delayedDesktopSize(false),
Pierre Ossman7e546fe2017-09-18 16:05:48 +020068 keyboardGrabbed(false), mouseGrabbed(false),
Pierre Ossman921f6c82017-02-24 12:33:09 +010069 statsLastFrame(0), statsLastPixels(0), statsLastPosition(0),
70 statsGraph(NULL)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000071{
Pierre Ossman13548812017-01-03 16:12:30 +010072 Fl_Group* group;
Pierre Ossman4ae229f2011-04-15 12:58:31 +000073
Pierre Ossman13548812017-01-03 16:12:30 +010074 // 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 Ossman4ae229f2011-04-15 12:58:31 +000078
Pierre Ossmanff473402012-07-04 11:27:47 +000079 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000080
Pierre Ossman13548812017-01-03 16:12:30 +010081 // 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 Ossman4ae229f2011-04-15 12:58:31 +000089
Pierre Ossman5156d5e2011-03-09 09:42:34 +000090 callback(handleClose, this);
91
92 setName(name);
93
Pierre Ossman407a5c32011-05-26 14:48:29 +000094 OptionsDialog::addCallback(handleOptions, this);
95
Pierre Ossmana4f0f182011-06-14 13:36:57 +000096 // Hack. See below...
97 Fl::event_dispatch(&fltkHandle);
98
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000099 // 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 Ossman135906e2015-04-27 12:48:47 +0200104 if (strcmp(geometry, "") != 0) {
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000105 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 Ossman9d267c52012-10-02 14:30:22 +0000114 force_position(1);
115 /* fall through */
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000116 case 2:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000117 w = geom_w;
118 h = geom_h;
119 break;
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000120 default:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000121 geom_x = geom_y = 0;
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200122 vlog.error(_("Invalid geometry specified!"));
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000123 }
124 }
125 }
126
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000127#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 Åstrandc6cdc1f2012-08-29 07:14:31 +0000136 if (force_position()) {
137 resize(geom_x, geom_y, w, h);
138 } else {
139 size(w, h);
140 }
141
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000142 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 Ossmanaae38912012-07-13 11:22:55 +0000147 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000148#else
149 delayedFullscreen = true;
150#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000151 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000152
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000153 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000154
Pierre Ossmancb68c192012-10-17 07:59:20 +0000155 // Full screen events are not sent out for a hidden window,
156 // so send a fake one here to set up things properly.
Pierre Ossmancb68c192012-10-17 07:59:20 +0000157 if (fullscreen_active())
158 handle(FL_FULLSCREEN);
Pierre Ossmancb68c192012-10-17 07:59:20 +0000159
Peter Åstrand49b11572012-08-01 08:09:09 +0000160 // Unfortunately, current FLTK does not allow us to set the
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000161 // maximized property on Windows and X11 before showing the window.
162 // See STR #2083 and STR #2178
163#ifndef __APPLE__
Peter Åstrand49b11572012-08-01 08:09:09 +0000164 if (maximize) {
165 maximizeWindow();
166 }
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000167#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000168
Pierre Ossman13548812017-01-03 16:12:30 +0100169 // Adjust layout now that we're visible and know our final size
170 repositionWidgets();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000171
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000172 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 Ossman4f0647d2017-01-04 15:34:26 +0100179
Pierre Ossman921f6c82017-02-24 12:33:09 +0100180 // 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 Ossman4f0647d2017-01-04 15:34:26 +0100186 // Show hint about menu key
187 Fl::add_timeout(0.5, menuOverlay, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000188}
189
190
191DesktopWindow::~DesktopWindow()
192{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000193 // 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 Ossmanff473402012-07-04 11:27:47 +0000196 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000197 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000198 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman921f6c82017-02-24 12:33:09 +0100199 Fl::remove_timeout(handleStatsTimeout, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100200 Fl::remove_timeout(menuOverlay, this);
Pierre Ossman455566e2017-02-10 16:37:52 +0100201 Fl::remove_timeout(updateOverlay, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000202
203 OptionsDialog::removeCallback(handleOptions);
204
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100205 delete overlay;
Pierre Ossman3d74d882017-01-02 19:49:52 +0100206 delete offscreen;
207
Pierre Ossman921f6c82017-02-24 12:33:09 +0100208 delete statsGraph;
209
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000210 // FLTK automatically deletes all child widgets, so we shouldn't touch
211 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000212}
213
214
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000215const rfb::PixelFormat &DesktopWindow::getPreferredPF()
216{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000217 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000218}
219
220
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000221void DesktopWindow::setName(const char *name)
222{
223 CharArray windowNameStr;
224 windowNameStr.replaceBuf(new char[256]);
225
Pierre Ossman27820ba2011-09-30 12:54:24 +0000226 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000227
228 copy_label(windowNameStr.buf);
229}
230
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000231
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000232// Copy the areas of the framebuffer that have been changed (damaged)
233// to the displayed window.
234
235void DesktopWindow::updateWindow()
236{
Pierre Ossmanff473402012-07-04 11:27:47 +0000237 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000238 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 Ossmanff473402012-07-04 11:27:47 +0000246 firstUpdate = false;
247 }
248
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000249 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000250}
251
252
Pierre Ossman6455d852011-06-01 09:33:00 +0000253void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
254{
255 if ((new_w == viewport->w()) && (new_h == viewport->h()))
256 return;
257
Pierre Ossman6455d852011-06-01 09:33:00 +0000258 // 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 Åstrand1d03cbc2011-08-01 10:34:38 +0000261 if (!fullscreen_active()) {
Pierre Ossman29e4a162011-11-21 14:03:31 +0000262 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 Ossman6455d852011-06-01 09:33:00 +0000268 if ((w() > new_w) || (h() > new_h))
269 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000270 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000271 }
Pierre Ossman6455d852011-06-01 09:33:00 +0000272
273 viewport->size(new_w, new_h);
274
Pierre Ossman13548812017-01-03 16:12:30 +0100275 repositionWidgets();
Pierre Ossman6455d852011-06-01 09:33:00 +0000276}
277
278
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100279void DesktopWindow::setCursor(int width, int height,
280 const rfb::Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100281 const rdr::U8* data)
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000282{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100283 viewport->setCursor(width, height, hotspot, data);
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000284}
285
286
Pierre Ossman13548812017-01-03 16:12:30 +0100287void DesktopWindow::draw()
288{
289 bool redraw;
290
Pierre Ossman3d74d882017-01-02 19:49:52 +0100291 int X, Y, W, H;
292
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100293 // 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 Ossman3d74d882017-01-02 19:49:52 +0100296
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 Ossman13548812017-01-03 16:12:30 +0100305
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 Ossman4f0647d2017-01-04 15:34:26 +0100313 // 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 Ossman13548812017-01-03 16:12:30 +0100322 // Redraw background only on full redraws
323 if (redraw) {
Pierre Ossman3d74d882017-01-02 19:49:52 +0100324 if (offscreen)
325 offscreen->clear(40, 40, 40);
326 else
327 fl_rectf(0, 0, W, H, 40, 40, 40);
Pierre Ossman13548812017-01-03 16:12:30 +0100328 }
329
Pierre Ossman3d74d882017-01-02 19:49:52 +0100330 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 Ossman13548812017-01-03 16:12:30 +0100339
Pierre Ossman921f6c82017-02-24 12:33:09 +0100340 // 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 Ossman06304342017-04-28 10:20:29 +0200351 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 Ossman921f6c82017-02-24 12:33:09 +0100357 }
358
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100359 // 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 Ossman06304342017-04-28 10:20:29 +0200370 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 Ossman4f0647d2017-01-04 15:34:26 +0100376 }
Pierre Ossman13548812017-01-03 16:12:30 +0100377
Pierre Ossman3d74d882017-01-02 19:49:52 +0100378 // Flush offscreen surface to screen
379 if (offscreen) {
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100380 fl_clip_box(0, 0, w(), h(), X, Y, W, H);
Pierre Ossman3d74d882017-01-02 19:49:52 +0100381 offscreen->draw(X, Y, X, Y, W, H);
382 }
383
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100384 fl_pop_clip();
385 fl_pop_clip();
386
Pierre Ossman13548812017-01-03 16:12:30 +0100387 // 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 Ossman2fa63f82016-12-05 15:26:21 +0100399void DesktopWindow::setLEDState(unsigned int state)
400{
401 viewport->setLEDState(state);
402}
403
404
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000405void DesktopWindow::resize(int x, int y, int w, int h)
406{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000407 bool resizing;
408
Pierre Ossman0e593462012-09-03 09:43:23 +0000409#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 Ossman56610fb2015-01-27 16:28:15 +0100412 if (!fullscreen_active()) {
Pierre Ossman1f884e02012-10-30 10:26:23 +0000413 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000414
Pierre Ossman1f884e02012-10-30 10:26:23 +0000415 // 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 Ossman0e593462012-09-03 09:43:23 +0000425
Pierre Ossman1f884e02012-10-30 10:26:23 +0000426 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 Ossman9e0e7542012-10-03 12:21:54 +0000439 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 Ossman8ca4c1d2014-09-22 12:54:26 +0200445 vlog.info(_("Adjusting window size to avoid accidental full screen request"));
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000446 // Assume a panel of some form and adjust the height
447 y += 20;
448 h -= 40;
449 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000450 }
451 }
452 }
453#endif
454
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000455 if ((this->w() != w) || (this->h() != h))
456 resizing = true;
457 else
458 resizing = false;
459
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000460 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000461
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000462 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 Ossman4c4b66f2012-08-23 14:53:36 +0000469 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000470 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000471 if (not firstUpdate and not delayedFullscreen and
472 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000473 // 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 Ossmanff473402012-07-04 11:27:47 +0000478
Pierre Ossman13548812017-01-03 16:12:30 +0100479 repositionWidgets();
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000480 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000481}
482
483
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100484void 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
493void 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 Ossman455566e2017-02-10 16:37:52 +0100512 Fl::remove_timeout(updateOverlay, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100513
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 Ossman2e7c7442017-02-24 09:23:47 +0100526 w = 0;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100527 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 Ossman455566e2017-02-10 16:37:52 +0100582 overlayAlpha = 0;
583 gettimeofday(&overlayStart, NULL);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100584
585 delete image;
586
Pierre Ossman455566e2017-02-10 16:37:52 +0100587 Fl::add_timeout(1.0/60, updateOverlay, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100588}
589
Pierre Ossman455566e2017-02-10 16:37:52 +0100590void DesktopWindow::updateOverlay(void *data)
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100591{
592 DesktopWindow *self;
Pierre Ossman455566e2017-02-10 16:37:52 +0100593 unsigned elapsed;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100594
595 self = (DesktopWindow*)data;
Pierre Ossman455566e2017-02-10 16:37:52 +0100596
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 Ossman4f0647d2017-01-04 15:34:26 +0100612
613 self->damage(FL_DAMAGE_USER1);
614}
615
616
Pierre Ossman407a5c32011-05-26 14:48:29 +0000617int DesktopWindow::handle(int event)
618{
619 switch (event) {
Pierre Ossman407a5c32011-05-26 14:48:29 +0000620 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000621 fullScreen.setParam(fullscreen_active());
622
Pierre Ossman13548812017-01-03 16:12:30 +0100623 // Update scroll bars
624 repositionWidgets();
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000625
Pierre Ossman407a5c32011-05-26 14:48:29 +0000626 if (!fullscreenSystemKeys)
627 break;
628
629 if (fullscreen_active())
630 grabKeyboard();
631 else
632 ungrabKeyboard();
633
634 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000635
636 case FL_ENTER:
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200637 if (keyboardGrabbed)
638 grabPointer();
Pierre Ossman1d867b62012-09-03 09:25:07 +0000639 case FL_LEAVE:
640 case FL_DRAG:
641 case FL_MOVE:
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200642 // 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 Ossman1d867b62012-09-03 09:25:07 +0000649 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 Ossmanb2e712b2012-09-10 11:46:08 +0000658 // Continue processing so that the viewport also gets mouse events
659 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000660 }
661
662 return Fl_Window::handle(event);
663}
664
665
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000666int DesktopWindow::fltkHandle(int event, Fl_Window *win)
667{
668 int ret;
669
670 ret = Fl::handle_(event, win);
671
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000672 // 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 Ossman1668cfa2016-12-10 17:13:40 +0100679 if (dw) {
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000680 switch (event) {
681 case FL_FOCUS:
Pierre Ossman1668cfa2016-12-10 17:13:40 +0100682 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 Ossmana4f0f182011-06-14 13:36:57 +0000695 break;
696
Pierre Ossmanb1369802012-10-17 07:59:36 +0000697 case FL_UNFOCUS:
Pierre Ossman1668cfa2016-12-10 17:13:40 +0100698 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 Ossmana4f0f182011-06-14 13:36:57 +0000704 break;
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200705
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 Ossmana4f0f182011-06-14 13:36:57 +0000715 }
716 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000717
718 return ret;
719}
720
721
Pierre Ossmanaae38912012-07-13 11:22:55 +0000722void DesktopWindow::fullscreen_on()
723{
Pierre Ossmanaae38912012-07-13 11:22:55 +0000724 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 Ossmanaae38912012-07-13 11:22:55 +0000762
763 fullscreen();
Pierre Ossmanaae38912012-07-13 11:22:55 +0000764}
765
Pierre Ossman407a5c32011-05-26 14:48:29 +0000766void 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 Ossman7e546fe2017-09-18 16:05:48 +0200778 if (ret != 0) {
Pierre Ossman407a5c32011-05-26 14:48:29 +0000779 vlog.error(_("Failure grabbing keyboard"));
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200780 return;
781 }
Pierre Ossman407a5c32011-05-26 14:48:29 +0000782#elif defined(__APPLE__)
783 int ret;
784
Pierre Ossman56610fb2015-01-27 16:28:15 +0100785 ret = cocoa_capture_display(this, fullScreenAllMonitors);
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200786 if (ret != 0) {
Pierre Ossman407a5c32011-05-26 14:48:29 +0000787 vlog.error(_("Failure grabbing keyboard"));
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200788 return;
789 }
Pierre Ossman407a5c32011-05-26 14:48:29 +0000790#else
791 int ret;
792
793 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000794 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000795 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 Ossman7e546fe2017-09-18 16:05:48 +0200804 return;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000805 }
806#endif
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200807
808 keyboardGrabbed = true;
809
810 if (contains(Fl::belowmouse()))
811 grabPointer();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000812}
813
814
815void DesktopWindow::ungrabKeyboard()
816{
817 Fl::remove_timeout(handleGrab, this);
818
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200819 keyboardGrabbed = false;
820
821 ungrabPointer();
822
Pierre Ossman407a5c32011-05-26 14:48:29 +0000823#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 Ossman841e9f32017-10-02 11:05:10 +0200832 XUngrabKeyboard(fl_display, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000833#endif
834}
835
836
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200837void 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
863void DesktopWindow::ungrabPointer()
864{
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200865 mouseGrabbed = false;
866#if !defined(WIN32) && !defined(__APPLE__)
Peter Åstrand (astrand)5f497252018-01-15 08:27:19 +0100867 XUngrabPointer(fl_display, CurrentTime);
Pierre Ossman7e546fe2017-09-18 16:05:48 +0200868#endif
869}
870
871
Pierre Ossman407a5c32011-05-26 14:48:29 +0000872void DesktopWindow::handleGrab(void *data)
873{
874 DesktopWindow *self = (DesktopWindow*)data;
875
876 assert(self);
877
Pierre Ossman407a5c32011-05-26 14:48:29 +0000878 if (!fullscreenSystemKeys)
879 return;
880 if (!self->fullscreen_active())
881 return;
882
883 self->grabKeyboard();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000884}
885
886
Peter Åstrand49b11572012-08-01 08:09:09 +0000887#define _NET_WM_STATE_ADD 1 /* add/set property */
888void DesktopWindow::maximizeWindow()
889{
890#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000891 // 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 Ossman897067b2012-10-11 09:17:19 +0000894 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 Ossman897067b2012-10-11 09:17:19 +0000900 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000901#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000902 // 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 Ossman002cc5d2012-10-02 14:45:10 +0000906 if (fullscreen_active())
907 return;
Peter Åstrand49b11572012-08-01 08:09:09 +0000908 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 Ossman4c4b66f2012-08-23 14:53:36 +0000933void DesktopWindow::handleDesktopSize()
934{
Pierre Ossman135906e2015-04-27 12:48:47 +0200935 if (strcmp(desktopSize, "") != 0) {
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100936 int width, height;
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000937
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100938 // An explicit size has been requested
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000939
Pierre Ossman135906e2015-04-27 12:48:47 +0200940 if (sscanf(desktopSize, "%dx%d", &width, &height) != 2)
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100941 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 Ossman4c4b66f2012-08-23 14:53:36 +0000949}
950
951
Pierre Ossmanff473402012-07-04 11:27:47 +0000952void DesktopWindow::handleResizeTimeout(void *data)
953{
954 DesktopWindow *self = (DesktopWindow *)data;
955
956 assert(self);
957
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000958 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000959}
960
961
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000962void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000963{
Pierre Ossmanff473402012-07-04 11:27:47 +0000964 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000965 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000966
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000967 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000968 // 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 Ossmanff473402012-07-04 11:27:47 +0000971
Pierre Ossmanaae38912012-07-13 11:22:55 +0000972 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000973
Pierre Ossmanaae38912012-07-13 11:22:55 +0000974 // 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 Ossmanff473402012-07-04 11:27:47 +0000981
Pierre Ossmanaae38912012-07-13 11:22:55 +0000982 while (true) {
983 iter = layout.begin();
984 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000985
Pierre Ossmanaae38912012-07-13 11:22:55 +0000986 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 Ossmanaae38912012-07-13 11:22:55 +0000998 } else {
999 int i;
1000 rdr::U32 id;
1001 int sx, sy, sw, sh;
Pierre Ossman4f0647d2017-01-04 15:34:26 +01001002 rfb::Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +00001003
1004 // In full screen we report all screens that are fully covered.
1005
Pierre Ossmanf44f6c02012-07-20 12:39:27 +00001006 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
1007 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +00001008
Pierre Ossmanaae38912012-07-13 11:22:55 +00001009 // 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 Ossman93d2d922012-07-20 12:32:52 +00001018 // Check that the screen is fully inside the framebuffer
1019 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +00001020 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +00001021 continue;
1022
Pierre Ossmanf44f6c02012-07-20 12:39:27 +00001023 // Adjust the coordinates so they are relative to our viewport
1024 sx -= viewport_rect.tl.x;
1025 sy -= viewport_rect.tl.y;
1026
Pierre Ossmanaae38912012-07-13 11:22:55 +00001027 // 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 Ossmanff473402012-07-04 11:27:47 +00001057 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +00001058
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 Ossmanff473402012-07-04 11:27:47 +00001063 }
Pierre Ossmanff473402012-07-04 11:27:47 +00001064
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 Ossman9018af42015-01-26 15:15:47 +01001071 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 Ossmanaae38912012-07-13 11:22:55 +00001076
1077 if (!layout.validate(width, height)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +02001078 vlog.error(_("Invalid screen layout computed for resize request!"));
Pierre Ossmanaae38912012-07-13 11:22:55 +00001079 return;
1080 }
Pierre Ossmanff473402012-07-04 11:27:47 +00001081
1082 cc->writer()->writeSetDesktopSize(width, height, layout);
1083}
1084
1085
Pierre Ossman13548812017-01-03 16:12:30 +01001086void DesktopWindow::repositionWidgets()
Pierre Ossman6455d852011-06-01 09:33:00 +00001087{
1088 int new_x, new_y;
1089
Pierre Ossman13548812017-01-03 16:12:30 +01001090 // Viewport position
Pierre Ossman6455d852011-06-01 09:33:00 +00001091
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 Ossman13548812017-01-03 16:12:30 +01001116 damage(FL_DAMAGE_SCROLL);
Pierre Ossman6455d852011-06-01 09:33:00 +00001117 }
Pierre Ossman13548812017-01-03 16:12:30 +01001118
1119 // Scrollbars visbility
1120
Luke Shumaker0a8c4d42017-05-23 14:03:15 -04001121 if (fullscreen_active()) {
Pierre Ossman13548812017-01-03 16:12:30 +01001122 hscroll->hide();
Pierre Ossman13548812017-01-03 16:12:30 +01001123 vscroll->hide();
Luke Shumaker0a8c4d42017-05-23 14:03:15 -04001124 } 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 Ossman13548812017-01-03 16:12:30 +01001150
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 Ossman6455d852011-06-01 09:33:00 +00001170}
1171
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001172void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
1173{
1174 exit_vncviewer();
1175}
Pierre Ossman407a5c32011-05-26 14:48:29 +00001176
1177
1178void DesktopWindow::handleOptions(void *data)
1179{
1180 DesktopWindow *self = (DesktopWindow*)data;
1181
Pierre Ossman407a5c32011-05-26 14:48:29 +00001182 if (self->fullscreen_active() && fullscreenSystemKeys)
1183 self->grabKeyboard();
1184 else
1185 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +00001186
Pierre Ossmanff473402012-07-04 11:27:47 +00001187 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +00001188 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +00001189 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +00001190 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +00001191}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +00001192
1193void 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 Ossman1d867b62012-09-03 09:25:07 +00001206
Pierre Ossman13548812017-01-03 16:12:30 +01001207void 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 Ossman13548812017-01-03 16:12:30 +01001215 // 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
1227void 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 Ossman1d867b62012-09-03 09:25:07 +00001234void DesktopWindow::handleEdgeScroll(void *data)
1235{
Pierre Ossman1d867b62012-09-03 09:25:07 +00001236 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 Ossmandd138442012-09-03 09:45:40 +00001262 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 Ossman1d867b62012-09-03 09:25:07 +00001268 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001269 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 Ossman1d867b62012-09-03 09:25:07 +00001275
1276 if ((dx == 0) && (dy == 0))
1277 return;
1278
Luke Shumakere8d25d82017-05-23 02:16:27 -04001279 self->scrollTo(self->hscroll->value() - dx, self->vscroll->value() - dy);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001280
1281 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001282}
Pierre Ossman921f6c82017-02-24 12:33:09 +01001283
1284void DesktopWindow::handleStatsTimeout(void *data)
1285{
1286 DesktopWindow *self = (DesktopWindow*)data;
1287
Brian P. Hinzfc217142017-03-09 19:25:39 -05001288 const size_t statsCount = sizeof(self->stats)/sizeof(self->stats[0]);
Pierre Ossman921f6c82017-02-24 12:33:09 +01001289
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. Hinzfc217142017-03-09 19:25:39 -05001313 memmove(&self->stats[0], &self->stats[1], sizeof(self->stats[0])*(statsCount-1));
Pierre Ossman921f6c82017-02-24 12:33:09 +01001314
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 Ossman52d3edb2018-02-26 17:00:28 +01001384 siPrefix(self->stats[statsCount-1].pps, "pix/s",
Pierre Ossman921f6c82017-02-24 12:33:09 +01001385 buffer, sizeof(buffer), 3);
1386 fl_draw(buffer, 5 + (statsWidth-10)/3, statsHeight - 5);
1387
1388 fl_color(FL_RED);
Pierre Ossman52d3edb2018-02-26 17:00:28 +01001389 siPrefix(self->stats[statsCount-1].bps * 8, "bps",
1390 buffer, sizeof(buffer), 3);
Pierre Ossman921f6c82017-02-24 12:33:09 +01001391 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}