blob: 408efd1980172a3fa354713781864516e424ec47 [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),
68 statsLastFrame(0), statsLastPixels(0), statsLastPosition(0),
69 statsGraph(NULL)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000070{
Pierre Ossman13548812017-01-03 16:12:30 +010071 Fl_Group* group;
Pierre Ossman4ae229f2011-04-15 12:58:31 +000072
Pierre Ossman13548812017-01-03 16:12:30 +010073 // Dummy group to prevent FLTK from moving our widgets around
74 group = new Fl_Group(0, 0, w, h);
75 group->resizable(NULL);
76 resizable(group);
Pierre Ossman4ae229f2011-04-15 12:58:31 +000077
Pierre Ossmanff473402012-07-04 11:27:47 +000078 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000079
Pierre Ossman13548812017-01-03 16:12:30 +010080 // Position will be adjusted later
81 hscroll = new Fl_Scrollbar(0, 0, 0, 0);
82 vscroll = new Fl_Scrollbar(0, 0, 0, 0);
83 hscroll->type(FL_HORIZONTAL);
84 hscroll->callback(handleScroll, this);
85 vscroll->callback(handleScroll, this);
86
87 group->end();
Pierre Ossman4ae229f2011-04-15 12:58:31 +000088
Pierre Ossman5156d5e2011-03-09 09:42:34 +000089 callback(handleClose, this);
90
91 setName(name);
92
Pierre Ossman407a5c32011-05-26 14:48:29 +000093 OptionsDialog::addCallback(handleOptions, this);
94
Pierre Ossmana4f0f182011-06-14 13:36:57 +000095 // Hack. See below...
96 Fl::event_dispatch(&fltkHandle);
97
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000098 // Support for -geometry option. Note that although we do support
99 // negative coordinates, we do not support -XOFF-YOFF (ie
100 // coordinates relative to the right edge / bottom edge) at this
101 // time.
102 int geom_x = 0, geom_y = 0;
Pierre Ossman135906e2015-04-27 12:48:47 +0200103 if (strcmp(geometry, "") != 0) {
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000104 int matched;
105 matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
106 if (matched == 2) {
107 force_position(1);
108 } else {
109 int geom_w, geom_h;
110 matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
111 switch (matched) {
112 case 4:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000113 force_position(1);
114 /* fall through */
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000115 case 2:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000116 w = geom_w;
117 h = geom_h;
118 break;
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000119 default:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000120 geom_x = geom_y = 0;
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200121 vlog.error(_("Invalid geometry specified!"));
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000122 }
123 }
124 }
125
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000126#ifdef __APPLE__
127 // On OS X we can do the maximize thing properly before the
128 // window is showned. Other platforms handled further down...
129 if (maximize) {
130 int dummy;
131 Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
132 }
133#endif
134
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000135 if (force_position()) {
136 resize(geom_x, geom_y, w, h);
137 } else {
138 size(w, h);
139 }
140
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000141 if (fullScreen) {
142 // Hack: Window managers seem to be rather crappy at respecting
143 // fullscreen hints on initial windows. So on X11 we'll have to
144 // wait until after we've been mapped.
145#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000146 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000147#else
148 delayedFullscreen = true;
149#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000150 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000151
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000152 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000153
Pierre Ossmancb68c192012-10-17 07:59:20 +0000154 // Full screen events are not sent out for a hidden window,
155 // so send a fake one here to set up things properly.
Pierre Ossmancb68c192012-10-17 07:59:20 +0000156 if (fullscreen_active())
157 handle(FL_FULLSCREEN);
Pierre Ossmancb68c192012-10-17 07:59:20 +0000158
Peter Åstrand49b11572012-08-01 08:09:09 +0000159 // Unfortunately, current FLTK does not allow us to set the
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000160 // maximized property on Windows and X11 before showing the window.
161 // See STR #2083 and STR #2178
162#ifndef __APPLE__
Peter Åstrand49b11572012-08-01 08:09:09 +0000163 if (maximize) {
164 maximizeWindow();
165 }
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000166#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000167
Pierre Ossman13548812017-01-03 16:12:30 +0100168 // Adjust layout now that we're visible and know our final size
169 repositionWidgets();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000170
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000171 if (delayedFullscreen) {
172 // Hack: Fullscreen requests may be ignored, so we need a timeout for
173 // when we should stop waiting. We also really need to wait for the
174 // resize, which can come after the fullscreen event.
175 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
176 fullscreen_on();
177 }
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100178
Pierre Ossman921f6c82017-02-24 12:33:09 +0100179 // Throughput graph for debugging
180 if (vlog.getLevel() >= LogWriter::LEVEL_DEBUG) {
181 memset(&stats, 0, sizeof(stats));
182 Fl::add_timeout(0, handleStatsTimeout, this);
183 }
184
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100185 // Show hint about menu key
186 Fl::add_timeout(0.5, menuOverlay, this);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000187}
188
189
190DesktopWindow::~DesktopWindow()
191{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000192 // Unregister all timeouts in case they get a change tro trigger
193 // again later when this object is already gone.
194 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000195 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000196 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000197 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman921f6c82017-02-24 12:33:09 +0100198 Fl::remove_timeout(handleStatsTimeout, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100199 Fl::remove_timeout(menuOverlay, this);
Pierre Ossman455566e2017-02-10 16:37:52 +0100200 Fl::remove_timeout(updateOverlay, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000201
202 OptionsDialog::removeCallback(handleOptions);
203
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100204 delete overlay;
Pierre Ossman3d74d882017-01-02 19:49:52 +0100205 delete offscreen;
206
Pierre Ossman921f6c82017-02-24 12:33:09 +0100207 delete statsGraph;
208
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000209 // FLTK automatically deletes all child widgets, so we shouldn't touch
210 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000211}
212
213
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000214const rfb::PixelFormat &DesktopWindow::getPreferredPF()
215{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000216 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000217}
218
219
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000220void DesktopWindow::setName(const char *name)
221{
222 CharArray windowNameStr;
223 windowNameStr.replaceBuf(new char[256]);
224
Pierre Ossman27820ba2011-09-30 12:54:24 +0000225 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000226
227 copy_label(windowNameStr.buf);
228}
229
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000230
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000231// Copy the areas of the framebuffer that have been changed (damaged)
232// to the displayed window.
233
234void DesktopWindow::updateWindow()
235{
Pierre Ossmanff473402012-07-04 11:27:47 +0000236 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000237 if (cc->cp.supportsSetDesktopSize) {
238 // Hack: Wait until we're in the proper mode and position until
239 // resizing things, otherwise we might send the wrong thing.
240 if (delayedFullscreen)
241 delayedDesktopSize = true;
242 else
243 handleDesktopSize();
244 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000245 firstUpdate = false;
246 }
247
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000248 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000249}
250
251
Pierre Ossman6455d852011-06-01 09:33:00 +0000252void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
253{
254 if ((new_w == viewport->w()) && (new_h == viewport->h()))
255 return;
256
Pierre Ossman6455d852011-06-01 09:33:00 +0000257 // If we're letting the viewport match the window perfectly, then
258 // keep things that way for the new size, otherwise just keep things
259 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000260 if (!fullscreen_active()) {
Pierre Ossman29e4a162011-11-21 14:03:31 +0000261 if ((w() == viewport->w()) && (h() == viewport->h()))
262 size(new_w, new_h);
263 else {
264 // Make sure the window isn't too big. We do this manually because
265 // we have to disable the window size restriction (and it isn't
266 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000267 if ((w() > new_w) || (h() > new_h))
268 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000269 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000270 }
Pierre Ossman6455d852011-06-01 09:33:00 +0000271
272 viewport->size(new_w, new_h);
273
Pierre Ossman13548812017-01-03 16:12:30 +0100274 repositionWidgets();
Pierre Ossman6455d852011-06-01 09:33:00 +0000275}
276
277
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100278void DesktopWindow::setCursor(int width, int height,
279 const rfb::Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100280 const rdr::U8* data)
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000281{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100282 viewport->setCursor(width, height, hotspot, data);
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000283}
284
285
Pierre Ossman13548812017-01-03 16:12:30 +0100286void DesktopWindow::draw()
287{
288 bool redraw;
289
Pierre Ossman3d74d882017-01-02 19:49:52 +0100290 int X, Y, W, H;
291
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100292 // X11 needs an off screen buffer for compositing to avoid flicker,
293 // and alpha blending doesn't work for windows on Win32
294#if !defined(__APPLE__)
Pierre Ossman3d74d882017-01-02 19:49:52 +0100295
296 // Adjust offscreen surface dimensions
297 if ((offscreen == NULL) ||
298 (offscreen->width() != w()) || (offscreen->height() != h())) {
299 delete offscreen;
300 offscreen = new Surface(w(), h());
301 }
302
303#endif
Pierre Ossman13548812017-01-03 16:12:30 +0100304
305 // Active area inside scrollbars
306 W = w() - (vscroll->visible() ? vscroll->w() : 0);
307 H = h() - (hscroll->visible() ? hscroll->h() : 0);
308
309 // Full redraw?
310 redraw = (damage() & ~FL_DAMAGE_CHILD);
311
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100312 // Simplify the clip region to a simple rectangle in order to
313 // properly draw all the layers even if they only partially overlap
314 if (redraw)
315 X = Y = 0;
316 else
317 fl_clip_box(0, 0, W, H, X, Y, W, H);
318 fl_push_no_clip();
319 fl_push_clip(X, Y, W, H);
320
Pierre Ossman13548812017-01-03 16:12:30 +0100321 // Redraw background only on full redraws
322 if (redraw) {
Pierre Ossman3d74d882017-01-02 19:49:52 +0100323 if (offscreen)
324 offscreen->clear(40, 40, 40);
325 else
326 fl_rectf(0, 0, W, H, 40, 40, 40);
Pierre Ossman13548812017-01-03 16:12:30 +0100327 }
328
Pierre Ossman3d74d882017-01-02 19:49:52 +0100329 if (offscreen) {
330 viewport->draw(offscreen);
331 viewport->clear_damage();
332 } else {
333 if (redraw)
334 draw_child(*viewport);
335 else
336 update_child(*viewport);
337 }
Pierre Ossman13548812017-01-03 16:12:30 +0100338
Pierre Ossman921f6c82017-02-24 12:33:09 +0100339 // Debug graph (if active)
340 if (statsGraph) {
341 int ox, oy, ow, oh;
342
343 ox = X = w() - statsGraph->width() - 30;
344 oy = Y = h() - statsGraph->height() - 30;
345 ow = statsGraph->width();
346 oh = statsGraph->height();
347
348 fl_clip_box(ox, oy, ow, oh, ox, oy, ow, oh);
349
Pierre Ossman06304342017-04-28 10:20:29 +0200350 if ((ow != 0) && (oh != 0)) {
351 if (offscreen)
352 statsGraph->blend(offscreen, ox - X, oy - Y, ox, oy, ow, oh, 204);
353 else
354 statsGraph->blend(ox - X, oy - Y, ox, oy, ow, oh, 204);
355 }
Pierre Ossman921f6c82017-02-24 12:33:09 +0100356 }
357
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100358 // Overlay (if active)
359 if (overlay) {
360 int ox, oy, ow, oh;
361
362 ox = X = (w() - overlay->width()) / 2;
363 oy = Y = 50;
364 ow = overlay->width();
365 oh = overlay->height();
366
367 fl_clip_box(ox, oy, ow, oh, ox, oy, ow, oh);
368
Pierre Ossman06304342017-04-28 10:20:29 +0200369 if ((ow != 0) && (oh != 0)) {
370 if (offscreen)
371 overlay->blend(offscreen, ox - X, oy - Y, ox, oy, ow, oh, overlayAlpha);
372 else
373 overlay->blend(ox - X, oy - Y, ox, oy, ow, oh, overlayAlpha);
374 }
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100375 }
Pierre Ossman13548812017-01-03 16:12:30 +0100376
Pierre Ossman3d74d882017-01-02 19:49:52 +0100377 // Flush offscreen surface to screen
378 if (offscreen) {
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100379 fl_clip_box(0, 0, w(), h(), X, Y, W, H);
Pierre Ossman3d74d882017-01-02 19:49:52 +0100380 offscreen->draw(X, Y, X, Y, W, H);
381 }
382
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100383 fl_pop_clip();
384 fl_pop_clip();
385
Pierre Ossman13548812017-01-03 16:12:30 +0100386 // Finally the scrollbars
387
388 if (redraw) {
389 draw_child(*hscroll);
390 draw_child(*vscroll);
391 } else {
392 update_child(*hscroll);
393 update_child(*vscroll);
394 }
395}
396
397
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000398void DesktopWindow::resize(int x, int y, int w, int h)
399{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000400 bool resizing;
401
Pierre Ossman0e593462012-09-03 09:43:23 +0000402#if ! (defined(WIN32) || defined(__APPLE__))
403 // X11 window managers will treat a resize to cover the entire
404 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman56610fb2015-01-27 16:28:15 +0100405 if (!fullscreen_active()) {
Pierre Ossman1f884e02012-10-30 10:26:23 +0000406 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000407
Pierre Ossman1f884e02012-10-30 10:26:23 +0000408 // If there is no X11 window, then this must be a resize request,
409 // not a notification from the X server.
410 if (!shown())
411 resize_req = true;
412 else {
413 // Otherwise we need to get the real window coordinates to tell
414 // the difference
415 XWindowAttributes actual;
416 Window cr;
417 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000418
Pierre Ossman1f884e02012-10-30 10:26:23 +0000419 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
420 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
421 0, 0, &wx, &wy, &cr);
422
423 // Actual resize request?
424 if ((wx != x) || (wy != y) ||
425 (actual.width != w) || (actual.height != h))
426 resize_req = true;
427 else
428 resize_req = false;
429 }
430
431 if (resize_req) {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000432 for (int i = 0;i < Fl::screen_count();i++) {
433 int sx, sy, sw, sh;
434
435 Fl::screen_xywh(sx, sy, sw, sh, i);
436
437 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200438 vlog.info(_("Adjusting window size to avoid accidental full screen request"));
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000439 // Assume a panel of some form and adjust the height
440 y += 20;
441 h -= 40;
442 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000443 }
444 }
445 }
446#endif
447
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000448 if ((this->w() != w) || (this->h() != h))
449 resizing = true;
450 else
451 resizing = false;
452
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000453 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000454
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000455 if (resizing) {
456 // Try to get the remote size to match our window size, provided
457 // the following conditions are true:
458 //
459 // a) The user has this feature turned on
460 // b) The server supports it
461 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000462 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000463 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000464 if (not firstUpdate and not delayedFullscreen and
465 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000466 // We delay updating the remote desktop as we tend to get a flood
467 // of resize events as the user is dragging the window.
468 Fl::remove_timeout(handleResizeTimeout, this);
469 Fl::add_timeout(0.5, handleResizeTimeout, this);
470 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000471
Pierre Ossman13548812017-01-03 16:12:30 +0100472 repositionWidgets();
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000473 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000474}
475
476
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100477void DesktopWindow::menuOverlay(void* data)
478{
479 DesktopWindow *self;
480
481 self = (DesktopWindow*)data;
482 self->setOverlay(_("Press %s to open the context menu"),
483 (const char*)menuKey);
484}
485
486void DesktopWindow::setOverlay(const char* text, ...)
487{
488 va_list ap;
489 char textbuf[1024];
490
491 Fl_Image_Surface *surface;
492
493 Fl_RGB_Image* imageText;
494 Fl_RGB_Image* image;
495
496 unsigned char* buffer;
497
498 int x, y;
499 int w, h;
500
501 unsigned char* a;
502 const unsigned char* b;
503
504 delete overlay;
Pierre Ossman455566e2017-02-10 16:37:52 +0100505 Fl::remove_timeout(updateOverlay, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100506
507 va_start(ap, text);
508 vsnprintf(textbuf, sizeof(textbuf), text, ap);
509 textbuf[sizeof(textbuf)-1] = '\0';
510 va_end(ap);
511
512#if !defined(WIN32) && !defined(__APPLE__)
513 // FLTK < 1.3.5 crashes if fl_gc is unset
514 if (!fl_gc)
515 fl_gc = XDefaultGC(fl_display, 0);
516#endif
517
518 fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2);
Pierre Ossman2e7c7442017-02-24 09:23:47 +0100519 w = 0;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100520 fl_measure(textbuf, w, h);
521
522 // Margins
523 w += 80;
524 h += 40;
525
526 surface = new Fl_Image_Surface(w, h);
527 surface->set_current();
528
529 fl_rectf(0, 0, w, h, 0, 0, 0);
530
531 fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2);
532 fl_color(FL_WHITE);
533 fl_draw(textbuf, 40, 20 + fl_height() - fl_descent());
534
535 imageText = surface->image();
536 delete surface;
537
538 Fl_Display_Device::display_device()->set_current();
539
540 buffer = new unsigned char[w * h * 4];
541 image = new Fl_RGB_Image(buffer, w, h, 4);
542
543 a = buffer;
544 for (x = 0;x < image->w() * image->h();x++) {
545 a[0] = a[1] = a[2] = 0x40;
546 a[3] = 0xcc;
547 a += 4;
548 }
549
550 a = buffer;
551 b = (const unsigned char*)imageText->data()[0];
552 for (y = 0;y < h;y++) {
553 for (x = 0;x < w;x++) {
554 unsigned char alpha;
555 alpha = *b;
556 a[0] = (unsigned)a[0] * (255 - alpha) / 255 + alpha;
557 a[1] = (unsigned)a[1] * (255 - alpha) / 255 + alpha;
558 a[2] = (unsigned)a[2] * (255 - alpha) / 255 + alpha;
559 a[3] = 255 - (255 - a[3]) * (255 - alpha) / 255;
560 a += 4;
561 b += imageText->d();
562 }
563 if (imageText->ld() != 0)
564 b += imageText->ld() - w * imageText->d();
565 }
566
567 delete imageText;
568
569 x = (this->w() - image->w()) / 2;
570 y = 50;
571 w = image->w();
572 h = image->h();
573
574 overlay = new Surface(image);
Pierre Ossman455566e2017-02-10 16:37:52 +0100575 overlayAlpha = 0;
576 gettimeofday(&overlayStart, NULL);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100577
578 delete image;
579
Pierre Ossman455566e2017-02-10 16:37:52 +0100580 Fl::add_timeout(1.0/60, updateOverlay, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100581}
582
Pierre Ossman455566e2017-02-10 16:37:52 +0100583void DesktopWindow::updateOverlay(void *data)
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100584{
585 DesktopWindow *self;
Pierre Ossman455566e2017-02-10 16:37:52 +0100586 unsigned elapsed;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100587
588 self = (DesktopWindow*)data;
Pierre Ossman455566e2017-02-10 16:37:52 +0100589
590 elapsed = msSince(&self->overlayStart);
591
592 if (elapsed < 500) {
593 self->overlayAlpha = (unsigned)255 * elapsed / 500;
594 Fl::add_timeout(1.0/60, updateOverlay, self);
595 } else if (elapsed < 3500) {
596 self->overlayAlpha = 255;
597 Fl::add_timeout(3.0, updateOverlay, self);
598 } else if (elapsed < 4000) {
599 self->overlayAlpha = (unsigned)255 * (4000 - elapsed) / 500;
600 Fl::add_timeout(1.0/60, updateOverlay, self);
601 } else {
602 delete self->overlay;
603 self->overlay = NULL;
604 }
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100605
606 self->damage(FL_DAMAGE_USER1);
607}
608
609
Pierre Ossman407a5c32011-05-26 14:48:29 +0000610int DesktopWindow::handle(int event)
611{
612 switch (event) {
Pierre Ossman407a5c32011-05-26 14:48:29 +0000613 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000614 fullScreen.setParam(fullscreen_active());
615
Pierre Ossman13548812017-01-03 16:12:30 +0100616 // Update scroll bars
617 repositionWidgets();
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000618
Pierre Ossman407a5c32011-05-26 14:48:29 +0000619 if (!fullscreenSystemKeys)
620 break;
621
622 if (fullscreen_active())
623 grabKeyboard();
624 else
625 ungrabKeyboard();
626
627 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000628
629 case FL_ENTER:
630 case FL_LEAVE:
631 case FL_DRAG:
632 case FL_MOVE:
633 if (fullscreen_active()) {
634 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
635 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
636 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
637 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
638 if (!Fl::has_timeout(handleEdgeScroll, this))
639 Fl::add_timeout(0.1, handleEdgeScroll, this);
640 }
641 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000642 // Continue processing so that the viewport also gets mouse events
643 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000644 }
645
646 return Fl_Window::handle(event);
647}
648
649
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000650int DesktopWindow::fltkHandle(int event, Fl_Window *win)
651{
652 int ret;
653
654 ret = Fl::handle_(event, win);
655
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000656 // This is hackish and the result of the dodgy focus handling in FLTK.
657 // The basic problem is that FLTK's view of focus and the system's tend
658 // to differ, and as a result we do not see all the FL_FOCUS events we
659 // need. Fortunately we can grab them here...
660
661 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
662
663 if (dw && fullscreenSystemKeys) {
664 switch (event) {
665 case FL_FOCUS:
666 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
667 // some issues we need to work around:
668 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
669 // b) Gaining focus on the system level causes FLTK to switch
670 // window level on OS X.
671 if (dw->fullscreen_active())
672 dw->grabKeyboard();
673 break;
674
Pierre Ossmanb1369802012-10-17 07:59:36 +0000675 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000676 // FIXME: We need to relinquish control when the entire window loses
677 // focus as it is very tied to this specific window on some
678 // platforms and we want to be able to open subwindows.
679 dw->ungrabKeyboard();
680 break;
681 }
682 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000683
684 return ret;
685}
686
687
Pierre Ossmanaae38912012-07-13 11:22:55 +0000688void DesktopWindow::fullscreen_on()
689{
Pierre Ossmanaae38912012-07-13 11:22:55 +0000690 if (not fullScreenAllMonitors)
691 fullscreen_screens(-1, -1, -1, -1);
692 else {
693 int top, bottom, left, right;
694 int top_y, bottom_y, left_x, right_x;
695
696 int sx, sy, sw, sh;
697
698 top = bottom = left = right = 0;
699
700 Fl::screen_xywh(sx, sy, sw, sh, 0);
701 top_y = sy;
702 bottom_y = sy + sh;
703 left_x = sx;
704 right_x = sx + sw;
705
706 for (int i = 1;i < Fl::screen_count();i++) {
707 Fl::screen_xywh(sx, sy, sw, sh, i);
708 if (sy < top_y) {
709 top = i;
710 top_y = sy;
711 }
712 if ((sy + sh) > bottom_y) {
713 bottom = i;
714 bottom_y = sy + sh;
715 }
716 if (sx < left_x) {
717 left = i;
718 left_x = sx;
719 }
720 if ((sx + sw) > right_x) {
721 right = i;
722 right_x = sx + sw;
723 }
724 }
725
726 fullscreen_screens(top, bottom, left, right);
727 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000728
729 fullscreen();
Pierre Ossmanaae38912012-07-13 11:22:55 +0000730}
731
Pierre Ossman407a5c32011-05-26 14:48:29 +0000732void DesktopWindow::grabKeyboard()
733{
734 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
735 // correct widget regardless of which low level window got the system
736 // event.
737
738 // FIXME: Push this stuff into FLTK.
739
740#if defined(WIN32)
741 int ret;
742
743 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
744 if (ret != 0)
745 vlog.error(_("Failure grabbing keyboard"));
746#elif defined(__APPLE__)
747 int ret;
748
Pierre Ossman56610fb2015-01-27 16:28:15 +0100749 ret = cocoa_capture_display(this, fullScreenAllMonitors);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000750 if (ret != 0)
751 vlog.error(_("Failure grabbing keyboard"));
752#else
753 int ret;
754
755 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000756 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000757 if (ret) {
758 if (ret == AlreadyGrabbed) {
759 // It seems like we can race with the WM in some cases.
760 // Try again in a bit.
761 if (!Fl::has_timeout(handleGrab, this))
762 Fl::add_timeout(0.500, handleGrab, this);
763 } else {
764 vlog.error(_("Failure grabbing keyboard"));
765 }
766 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000767
768 // We also need to grab the pointer as some WMs like to grab buttons
769 // combined with modifies (e.g. Alt+Button0 in metacity).
770 ret = XGrabPointer(fl_display, fl_xid(this), True,
771 ButtonPressMask|ButtonReleaseMask|
772 ButtonMotionMask|PointerMotionMask,
773 GrabModeAsync, GrabModeAsync,
774 None, None, CurrentTime);
775 if (ret)
776 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000777#endif
778}
779
780
781void DesktopWindow::ungrabKeyboard()
782{
783 Fl::remove_timeout(handleGrab, this);
784
785#if defined(WIN32)
786 win32_disable_lowlevel_keyboard(fl_xid(this));
787#elif defined(__APPLE__)
788 cocoa_release_display(this);
789#else
790 // FLTK has a grab so lets not mess with it
791 if (Fl::grab())
792 return;
793
Pierre Ossman53fd5442011-11-17 10:19:19 +0000794 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000795 XUngrabKeyboard(fl_display, fl_event_time);
796#endif
797}
798
799
800void DesktopWindow::handleGrab(void *data)
801{
802 DesktopWindow *self = (DesktopWindow*)data;
803
804 assert(self);
805
Pierre Ossman407a5c32011-05-26 14:48:29 +0000806 if (!fullscreenSystemKeys)
807 return;
808 if (!self->fullscreen_active())
809 return;
810
811 self->grabKeyboard();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000812}
813
814
Peter Åstrand49b11572012-08-01 08:09:09 +0000815#define _NET_WM_STATE_ADD 1 /* add/set property */
816void DesktopWindow::maximizeWindow()
817{
818#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000819 // We cannot use ShowWindow() in full screen mode as it will
820 // resize things implicitly. Fortunately modifying the style
821 // directly results in a maximized state once we leave full screen.
Pierre Ossman897067b2012-10-11 09:17:19 +0000822 if (fullscreen_active()) {
823 WINDOWINFO wi;
824 wi.cbSize = sizeof(WINDOWINFO);
825 GetWindowInfo(fl_xid(this), &wi);
826 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
827 } else
Pierre Ossman897067b2012-10-11 09:17:19 +0000828 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000829#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000830 // OS X is somewhat strange and does not really have a concept of a
831 // maximized window, so we can simply resize the window to the workarea.
832 // Note that we shouldn't do this whilst in full screen as that will
833 // incorrectly adjust things.
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000834 if (fullscreen_active())
835 return;
Peter Åstrand49b11572012-08-01 08:09:09 +0000836 int X, Y, W, H;
837 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
838 size(W, H);
839#else
840 // X11
841 fl_open_display();
842 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
843 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
844 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
845
846 XEvent e;
847 e.xany.type = ClientMessage;
848 e.xany.window = fl_xid(this);
849 e.xclient.message_type = net_wm_state;
850 e.xclient.format = 32;
851 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
852 e.xclient.data.l[1] = net_wm_state_maximized_vert;
853 e.xclient.data.l[2] = net_wm_state_maximized_horz;
854 e.xclient.data.l[3] = 0;
855 e.xclient.data.l[4] = 0;
856 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
857#endif
858}
859
860
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000861void DesktopWindow::handleDesktopSize()
862{
Pierre Ossman135906e2015-04-27 12:48:47 +0200863 if (strcmp(desktopSize, "") != 0) {
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100864 int width, height;
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000865
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100866 // An explicit size has been requested
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000867
Pierre Ossman135906e2015-04-27 12:48:47 +0200868 if (sscanf(desktopSize, "%dx%d", &width, &height) != 2)
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100869 return;
870
871 remoteResize(width, height);
872 } else if (::remoteResize) {
873 // No explicit size, but remote resizing is on so make sure it
874 // matches whatever size the window ended up being
875 remoteResize(w(), h());
876 }
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000877}
878
879
Pierre Ossmanff473402012-07-04 11:27:47 +0000880void DesktopWindow::handleResizeTimeout(void *data)
881{
882 DesktopWindow *self = (DesktopWindow *)data;
883
884 assert(self);
885
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000886 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000887}
888
889
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000890void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000891{
Pierre Ossmanff473402012-07-04 11:27:47 +0000892 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000893 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000894
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000895 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000896 // In windowed mode (or the framebuffer is so large that we need
897 // to scroll) we just report a single virtual screen that covers
898 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000899
Pierre Ossmanaae38912012-07-13 11:22:55 +0000900 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000901
Pierre Ossmanaae38912012-07-13 11:22:55 +0000902 // Not sure why we have no screens, but adding a new one should be
903 // safe as there is nothing to conflict with...
904 if (layout.num_screens() == 0)
905 layout.add_screen(rfb::Screen());
906 else if (layout.num_screens() != 1) {
907 // More than one screen. Remove all but the first (which we
908 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000909
Pierre Ossmanaae38912012-07-13 11:22:55 +0000910 while (true) {
911 iter = layout.begin();
912 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000913
Pierre Ossmanaae38912012-07-13 11:22:55 +0000914 if (iter == layout.end())
915 break;
916
917 layout.remove_screen(iter->id);
918 }
919 }
920
921 // Resize the remaining single screen to the complete framebuffer
922 layout.begin()->dimensions.tl.x = 0;
923 layout.begin()->dimensions.tl.y = 0;
924 layout.begin()->dimensions.br.x = width;
925 layout.begin()->dimensions.br.y = height;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000926 } else {
927 int i;
928 rdr::U32 id;
929 int sx, sy, sw, sh;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100930 rfb::Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000931
932 // In full screen we report all screens that are fully covered.
933
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000934 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
935 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000936
Pierre Ossmanaae38912012-07-13 11:22:55 +0000937 // If we can find a matching screen in the existing set, we use
938 // that, otherwise we create a brand new screen.
939 //
940 // FIXME: We should really track screens better so we can handle
941 // a resized one.
942 //
943 for (i = 0;i < Fl::screen_count();i++) {
944 Fl::screen_xywh(sx, sy, sw, sh, i);
945
Pierre Ossman93d2d922012-07-20 12:32:52 +0000946 // Check that the screen is fully inside the framebuffer
947 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000948 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000949 continue;
950
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000951 // Adjust the coordinates so they are relative to our viewport
952 sx -= viewport_rect.tl.x;
953 sy -= viewport_rect.tl.y;
954
Pierre Ossmanaae38912012-07-13 11:22:55 +0000955 // Look for perfectly matching existing screen...
956 for (iter = cc->cp.screenLayout.begin();
957 iter != cc->cp.screenLayout.end(); ++iter) {
958 if ((iter->dimensions.tl.x == sx) &&
959 (iter->dimensions.tl.y == sy) &&
960 (iter->dimensions.width() == sw) &&
961 (iter->dimensions.height() == sh))
962 break;
963 }
964
965 // Found it?
966 if (iter != cc->cp.screenLayout.end()) {
967 layout.add_screen(*iter);
968 continue;
969 }
970
971 // Need to add a new one, which means we need to find an unused id
972 while (true) {
973 id = rand();
974 for (iter = cc->cp.screenLayout.begin();
975 iter != cc->cp.screenLayout.end(); ++iter) {
976 if (iter->id == id)
977 break;
978 }
979
980 if (iter == cc->cp.screenLayout.end())
981 break;
982 }
983
984 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000985 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000986
987 // If the viewport doesn't match a physical screen, then we might
988 // end up with no screens in the layout. Add a fake one...
989 if (layout.num_screens() == 0)
990 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000991 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000992
993 // Do we actually change anything?
994 if ((width == cc->cp.width) &&
995 (height == cc->cp.height) &&
996 (layout == cc->cp.screenLayout))
997 return;
998
Pierre Ossman9018af42015-01-26 15:15:47 +0100999 char buffer[2048];
1000 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
1001 cc->cp.width, cc->cp.height, width, height);
1002 layout.print(buffer, sizeof(buffer));
1003 vlog.debug("%s", buffer);
Pierre Ossmanaae38912012-07-13 11:22:55 +00001004
1005 if (!layout.validate(width, height)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +02001006 vlog.error(_("Invalid screen layout computed for resize request!"));
Pierre Ossmanaae38912012-07-13 11:22:55 +00001007 return;
1008 }
Pierre Ossmanff473402012-07-04 11:27:47 +00001009
1010 cc->writer()->writeSetDesktopSize(width, height, layout);
1011}
1012
1013
Pierre Ossman13548812017-01-03 16:12:30 +01001014void DesktopWindow::repositionWidgets()
Pierre Ossman6455d852011-06-01 09:33:00 +00001015{
1016 int new_x, new_y;
1017
Pierre Ossman13548812017-01-03 16:12:30 +01001018 // Viewport position
Pierre Ossman6455d852011-06-01 09:33:00 +00001019
1020 new_x = viewport->x();
1021 new_y = viewport->y();
1022
1023 if (w() > viewport->w())
1024 new_x = (w() - viewport->w()) / 2;
1025 else {
1026 if (viewport->x() > 0)
1027 new_x = 0;
1028 else if (w() > (viewport->x() + viewport->w()))
1029 new_x = w() - viewport->w();
1030 }
1031
1032 // Same thing for y axis
1033 if (h() > viewport->h())
1034 new_y = (h() - viewport->h()) / 2;
1035 else {
1036 if (viewport->y() > 0)
1037 new_y = 0;
1038 else if (h() > (viewport->y() + viewport->h()))
1039 new_y = h() - viewport->h();
1040 }
1041
1042 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
1043 viewport->position(new_x, new_y);
Pierre Ossman13548812017-01-03 16:12:30 +01001044 damage(FL_DAMAGE_SCROLL);
Pierre Ossman6455d852011-06-01 09:33:00 +00001045 }
Pierre Ossman13548812017-01-03 16:12:30 +01001046
1047 // Scrollbars visbility
1048
Luke Shumaker0a8c4d42017-05-23 14:03:15 -04001049 if (fullscreen_active()) {
Pierre Ossman13548812017-01-03 16:12:30 +01001050 hscroll->hide();
Pierre Ossman13548812017-01-03 16:12:30 +01001051 vscroll->hide();
Luke Shumaker0a8c4d42017-05-23 14:03:15 -04001052 } else {
1053 // Decide whether to show a scrollbar by checking if the window
1054 // size (possibly minus scrollbar_size) is less than the viewport
1055 // (remote framebuffer) size.
1056 //
1057 // We decide whether to subtract scrollbar_size on an axis by
1058 // checking if the other axis *definitely* needs a scrollbar. You
1059 // might be tempted to think that this becomes a weird recursive
1060 // problem, but it isn't: If the window size is less than the
1061 // viewport size (without subtracting the scrollbar_size), then
1062 // that axis *definitely* needs a scrollbar; if the check changes
1063 // when we subtract scrollbar_size, then that axis only *maybe*
1064 // needs a scrollbar. If both axes only "maybe" need a scrollbar,
1065 // then neither does; so we don't need to recurse on the "maybe"
1066 // cases.
1067
1068 if (w() - (h() < viewport->h() ? Fl::scrollbar_size() : 0) < viewport->w())
1069 hscroll->show();
1070 else
1071 hscroll->hide();
1072
1073 if (h() - (w() < viewport->w() ? Fl::scrollbar_size() : 0) < viewport->h())
1074 vscroll->show();
1075 else
1076 vscroll->hide();
1077 }
Pierre Ossman13548812017-01-03 16:12:30 +01001078
1079 // Scrollbars positions
1080
1081 hscroll->resize(0, h() - Fl::scrollbar_size(),
1082 w() - (vscroll->visible() ? Fl::scrollbar_size() : 0),
1083 Fl::scrollbar_size());
1084 vscroll->resize(w() - Fl::scrollbar_size(), 0,
1085 Fl::scrollbar_size(),
1086 h() - (hscroll->visible() ? Fl::scrollbar_size() : 0));
1087
1088 // Scrollbars range
1089
1090 hscroll->value(-viewport->x(),
1091 w() - (vscroll->visible() ? vscroll->w() : 0),
1092 0, viewport->w());
1093 vscroll->value(-viewport->y(),
1094 h() - (hscroll->visible() ? hscroll->h() : 0),
1095 0, viewport->h());
1096 hscroll->value(hscroll->clamp(hscroll->value()));
1097 vscroll->value(vscroll->clamp(vscroll->value()));
Pierre Ossman6455d852011-06-01 09:33:00 +00001098}
1099
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001100void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
1101{
1102 exit_vncviewer();
1103}
Pierre Ossman407a5c32011-05-26 14:48:29 +00001104
1105
1106void DesktopWindow::handleOptions(void *data)
1107{
1108 DesktopWindow *self = (DesktopWindow*)data;
1109
Pierre Ossman407a5c32011-05-26 14:48:29 +00001110 if (self->fullscreen_active() && fullscreenSystemKeys)
1111 self->grabKeyboard();
1112 else
1113 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +00001114
Pierre Ossmanff473402012-07-04 11:27:47 +00001115 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +00001116 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +00001117 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +00001118 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +00001119}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +00001120
1121void DesktopWindow::handleFullscreenTimeout(void *data)
1122{
1123 DesktopWindow *self = (DesktopWindow *)data;
1124
1125 assert(self);
1126
1127 self->delayedFullscreen = false;
1128
1129 if (self->delayedDesktopSize) {
1130 self->handleDesktopSize();
1131 self->delayedDesktopSize = false;
1132 }
1133}
Pierre Ossman1d867b62012-09-03 09:25:07 +00001134
Pierre Ossman13548812017-01-03 16:12:30 +01001135void DesktopWindow::scrollTo(int x, int y)
1136{
1137 x = hscroll->clamp(x);
1138 y = vscroll->clamp(y);
1139
1140 hscroll->value(x);
1141 vscroll->value(y);
1142
Pierre Ossman13548812017-01-03 16:12:30 +01001143 // Scrollbar position results in inverse movement of
1144 // the viewport widget
1145 x = -x;
1146 y = -y;
1147
1148 if ((viewport->x() == x) && (viewport->y() == y))
1149 return;
1150
1151 viewport->position(x, y);
1152 damage(FL_DAMAGE_SCROLL);
1153}
1154
1155void DesktopWindow::handleScroll(Fl_Widget *widget, void *data)
1156{
1157 DesktopWindow *self = (DesktopWindow *)data;
1158
1159 self->scrollTo(self->hscroll->value(), self->vscroll->value());
1160}
1161
Pierre Ossman1d867b62012-09-03 09:25:07 +00001162void DesktopWindow::handleEdgeScroll(void *data)
1163{
Pierre Ossman1d867b62012-09-03 09:25:07 +00001164 DesktopWindow *self = (DesktopWindow *)data;
1165
1166 int mx, my;
1167 int dx, dy;
1168
1169 assert(self);
1170
1171 if (!self->fullscreen_active())
1172 return;
1173
1174 mx = Fl::event_x();
1175 my = Fl::event_y();
1176
1177 dx = dy = 0;
1178
1179 // Clamp mouse position in case it is outside the window
1180 if (mx < 0)
1181 mx = 0;
1182 if (mx > self->w())
1183 mx = self->w();
1184 if (my < 0)
1185 my = 0;
1186 if (my > self->h())
1187 my = self->h();
1188
1189 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001190 dx = EDGE_SCROLL_SPEED -
1191 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
1192 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
1193 (mx > self->w() - EDGE_SCROLL_SIZE))
1194 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
1195 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +00001196 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001197 dy = EDGE_SCROLL_SPEED -
1198 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
1199 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
1200 (my > self->h() - EDGE_SCROLL_SIZE))
1201 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
1202 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +00001203
1204 if ((dx == 0) && (dy == 0))
1205 return;
1206
Luke Shumakere8d25d82017-05-23 02:16:27 -04001207 self->scrollTo(self->hscroll->value() - dx, self->vscroll->value() - dy);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001208
1209 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001210}
Pierre Ossman921f6c82017-02-24 12:33:09 +01001211
1212void DesktopWindow::handleStatsTimeout(void *data)
1213{
1214 DesktopWindow *self = (DesktopWindow*)data;
1215
Brian P. Hinzfc217142017-03-09 19:25:39 -05001216 const size_t statsCount = sizeof(self->stats)/sizeof(self->stats[0]);
Pierre Ossman921f6c82017-02-24 12:33:09 +01001217
1218 unsigned frame, pixels, pos;
1219 unsigned elapsed;
1220
1221 const unsigned statsWidth = 200;
1222 const unsigned statsHeight = 100;
1223 const unsigned graphWidth = statsWidth - 10;
1224 const unsigned graphHeight = statsHeight - 25;
1225
1226 Fl_Image_Surface *surface;
1227 Fl_RGB_Image *image;
1228
1229 unsigned maxFPS, maxPPS, maxBPS;
1230 size_t i;
1231
1232 char buffer[256];
1233
1234 frame = self->cc->getFrameCount();
1235 pixels = self->cc->getPixelCount();
1236 pos = self->cc->getPosition();
1237 elapsed = msSince(&self->statsLastTime);
1238 if (elapsed < 1)
1239 elapsed = 1;
1240
Brian P. Hinzfc217142017-03-09 19:25:39 -05001241 memmove(&self->stats[0], &self->stats[1], sizeof(self->stats[0])*(statsCount-1));
Pierre Ossman921f6c82017-02-24 12:33:09 +01001242
1243 self->stats[statsCount-1].fps = (frame - self->statsLastFrame) * 1000 / elapsed;
1244 self->stats[statsCount-1].pps = (pixels - self->statsLastPixels) * 1000 / elapsed;
1245 self->stats[statsCount-1].bps = (pos - self->statsLastPosition) * 1000 / elapsed;
1246
1247 gettimeofday(&self->statsLastTime, NULL);
1248 self->statsLastFrame = frame;
1249 self->statsLastPixels = pixels;
1250 self->statsLastPosition = pos;
1251
1252#if !defined(WIN32) && !defined(__APPLE__)
1253 // FLTK < 1.3.5 crashes if fl_gc is unset
1254 if (!fl_gc)
1255 fl_gc = XDefaultGC(fl_display, 0);
1256#endif
1257
1258 surface = new Fl_Image_Surface(statsWidth, statsHeight);
1259 surface->set_current();
1260
1261 fl_rectf(0, 0, statsWidth, statsHeight, FL_BLACK);
1262
1263 fl_rect(5, 5, graphWidth, graphHeight, FL_WHITE);
1264
1265 maxFPS = maxPPS = maxBPS = 0;
1266 for (i = 0;i < statsCount;i++) {
1267 if (self->stats[i].fps > maxFPS)
1268 maxFPS = self->stats[i].fps;
1269 if (self->stats[i].pps > maxPPS)
1270 maxPPS = self->stats[i].pps;
1271 if (self->stats[i].bps > maxBPS)
1272 maxBPS = self->stats[i].bps;
1273 }
1274
1275 if (maxFPS != 0) {
1276 fl_color(FL_GREEN);
1277 for (i = 0;i < statsCount-1;i++) {
1278 fl_line(5 + i * graphWidth / statsCount,
1279 5 + graphHeight - graphHeight * self->stats[i].fps / maxFPS,
1280 5 + (i+1) * graphWidth / statsCount,
1281 5 + graphHeight - graphHeight * self->stats[i+1].fps / maxFPS);
1282 }
1283 }
1284
1285 if (maxPPS != 0) {
1286 fl_color(FL_YELLOW);
1287 for (i = 0;i < statsCount-1;i++) {
1288 fl_line(5 + i * graphWidth / statsCount,
1289 5 + graphHeight - graphHeight * self->stats[i].pps / maxPPS,
1290 5 + (i+1) * graphWidth / statsCount,
1291 5 + graphHeight - graphHeight * self->stats[i+1].pps / maxPPS);
1292 }
1293 }
1294
1295 if (maxBPS != 0) {
1296 fl_color(FL_RED);
1297 for (i = 0;i < statsCount-1;i++) {
1298 fl_line(5 + i * graphWidth / statsCount,
1299 5 + graphHeight - graphHeight * self->stats[i].bps / maxBPS,
1300 5 + (i+1) * graphWidth / statsCount,
1301 5 + graphHeight - graphHeight * self->stats[i+1].bps / maxBPS);
1302 }
1303 }
1304
1305 fl_font(FL_HELVETICA, 10);
1306
1307 fl_color(FL_GREEN);
1308 snprintf(buffer, sizeof(buffer), "%u fps", self->stats[statsCount-1].fps);
1309 fl_draw(buffer, 5, statsHeight - 5);
1310
1311 fl_color(FL_YELLOW);
1312 siPrefix(self->stats[statsCount-1].pps * 8, "pix/s",
1313 buffer, sizeof(buffer), 3);
1314 fl_draw(buffer, 5 + (statsWidth-10)/3, statsHeight - 5);
1315
1316 fl_color(FL_RED);
1317 iecPrefix(self->stats[statsCount-1].bps * 8, "Bps",
1318 buffer, sizeof(buffer), 3);
1319 fl_draw(buffer, 5 + (statsWidth-10)*2/3, statsHeight - 5);
1320
1321 image = surface->image();
1322 delete surface;
1323
1324 Fl_Display_Device::display_device()->set_current();
1325
1326 delete self->statsGraph;
1327 self->statsGraph = new Surface(image);
1328 delete image;
1329
1330 self->damage(FL_DAMAGE_CHILD, self->w() - statsWidth - 30,
1331 self->h() - statsHeight - 30,
1332 statsWidth, statsHeight);
1333
1334 Fl::repeat_timeout(0.5, handleStatsTimeout, data);
1335}