blob: 3973cd66334844e92b8794658f4b6e17cb9a0e2d [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 Ossman2fa63f82016-12-05 15:26:21 +0100398void DesktopWindow::setLEDState(unsigned int state)
399{
400 viewport->setLEDState(state);
401}
402
403
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000404void DesktopWindow::resize(int x, int y, int w, int h)
405{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000406 bool resizing;
407
Pierre Ossman0e593462012-09-03 09:43:23 +0000408#if ! (defined(WIN32) || defined(__APPLE__))
409 // X11 window managers will treat a resize to cover the entire
410 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman56610fb2015-01-27 16:28:15 +0100411 if (!fullscreen_active()) {
Pierre Ossman1f884e02012-10-30 10:26:23 +0000412 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000413
Pierre Ossman1f884e02012-10-30 10:26:23 +0000414 // If there is no X11 window, then this must be a resize request,
415 // not a notification from the X server.
416 if (!shown())
417 resize_req = true;
418 else {
419 // Otherwise we need to get the real window coordinates to tell
420 // the difference
421 XWindowAttributes actual;
422 Window cr;
423 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000424
Pierre Ossman1f884e02012-10-30 10:26:23 +0000425 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
426 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
427 0, 0, &wx, &wy, &cr);
428
429 // Actual resize request?
430 if ((wx != x) || (wy != y) ||
431 (actual.width != w) || (actual.height != h))
432 resize_req = true;
433 else
434 resize_req = false;
435 }
436
437 if (resize_req) {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000438 for (int i = 0;i < Fl::screen_count();i++) {
439 int sx, sy, sw, sh;
440
441 Fl::screen_xywh(sx, sy, sw, sh, i);
442
443 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200444 vlog.info(_("Adjusting window size to avoid accidental full screen request"));
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000445 // Assume a panel of some form and adjust the height
446 y += 20;
447 h -= 40;
448 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000449 }
450 }
451 }
452#endif
453
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000454 if ((this->w() != w) || (this->h() != h))
455 resizing = true;
456 else
457 resizing = false;
458
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000459 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000460
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000461 if (resizing) {
462 // Try to get the remote size to match our window size, provided
463 // the following conditions are true:
464 //
465 // a) The user has this feature turned on
466 // b) The server supports it
467 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000468 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000469 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000470 if (not firstUpdate and not delayedFullscreen and
471 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000472 // We delay updating the remote desktop as we tend to get a flood
473 // of resize events as the user is dragging the window.
474 Fl::remove_timeout(handleResizeTimeout, this);
475 Fl::add_timeout(0.5, handleResizeTimeout, this);
476 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000477
Pierre Ossman13548812017-01-03 16:12:30 +0100478 repositionWidgets();
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000479 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000480}
481
482
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100483void DesktopWindow::menuOverlay(void* data)
484{
485 DesktopWindow *self;
486
487 self = (DesktopWindow*)data;
488 self->setOverlay(_("Press %s to open the context menu"),
489 (const char*)menuKey);
490}
491
492void DesktopWindow::setOverlay(const char* text, ...)
493{
494 va_list ap;
495 char textbuf[1024];
496
497 Fl_Image_Surface *surface;
498
499 Fl_RGB_Image* imageText;
500 Fl_RGB_Image* image;
501
502 unsigned char* buffer;
503
504 int x, y;
505 int w, h;
506
507 unsigned char* a;
508 const unsigned char* b;
509
510 delete overlay;
Pierre Ossman455566e2017-02-10 16:37:52 +0100511 Fl::remove_timeout(updateOverlay, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100512
513 va_start(ap, text);
514 vsnprintf(textbuf, sizeof(textbuf), text, ap);
515 textbuf[sizeof(textbuf)-1] = '\0';
516 va_end(ap);
517
518#if !defined(WIN32) && !defined(__APPLE__)
519 // FLTK < 1.3.5 crashes if fl_gc is unset
520 if (!fl_gc)
521 fl_gc = XDefaultGC(fl_display, 0);
522#endif
523
524 fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2);
Pierre Ossman2e7c7442017-02-24 09:23:47 +0100525 w = 0;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100526 fl_measure(textbuf, w, h);
527
528 // Margins
529 w += 80;
530 h += 40;
531
532 surface = new Fl_Image_Surface(w, h);
533 surface->set_current();
534
535 fl_rectf(0, 0, w, h, 0, 0, 0);
536
537 fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2);
538 fl_color(FL_WHITE);
539 fl_draw(textbuf, 40, 20 + fl_height() - fl_descent());
540
541 imageText = surface->image();
542 delete surface;
543
544 Fl_Display_Device::display_device()->set_current();
545
546 buffer = new unsigned char[w * h * 4];
547 image = new Fl_RGB_Image(buffer, w, h, 4);
548
549 a = buffer;
550 for (x = 0;x < image->w() * image->h();x++) {
551 a[0] = a[1] = a[2] = 0x40;
552 a[3] = 0xcc;
553 a += 4;
554 }
555
556 a = buffer;
557 b = (const unsigned char*)imageText->data()[0];
558 for (y = 0;y < h;y++) {
559 for (x = 0;x < w;x++) {
560 unsigned char alpha;
561 alpha = *b;
562 a[0] = (unsigned)a[0] * (255 - alpha) / 255 + alpha;
563 a[1] = (unsigned)a[1] * (255 - alpha) / 255 + alpha;
564 a[2] = (unsigned)a[2] * (255 - alpha) / 255 + alpha;
565 a[3] = 255 - (255 - a[3]) * (255 - alpha) / 255;
566 a += 4;
567 b += imageText->d();
568 }
569 if (imageText->ld() != 0)
570 b += imageText->ld() - w * imageText->d();
571 }
572
573 delete imageText;
574
575 x = (this->w() - image->w()) / 2;
576 y = 50;
577 w = image->w();
578 h = image->h();
579
580 overlay = new Surface(image);
Pierre Ossman455566e2017-02-10 16:37:52 +0100581 overlayAlpha = 0;
582 gettimeofday(&overlayStart, NULL);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100583
584 delete image;
585
Pierre Ossman455566e2017-02-10 16:37:52 +0100586 Fl::add_timeout(1.0/60, updateOverlay, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100587}
588
Pierre Ossman455566e2017-02-10 16:37:52 +0100589void DesktopWindow::updateOverlay(void *data)
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100590{
591 DesktopWindow *self;
Pierre Ossman455566e2017-02-10 16:37:52 +0100592 unsigned elapsed;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100593
594 self = (DesktopWindow*)data;
Pierre Ossman455566e2017-02-10 16:37:52 +0100595
596 elapsed = msSince(&self->overlayStart);
597
598 if (elapsed < 500) {
599 self->overlayAlpha = (unsigned)255 * elapsed / 500;
600 Fl::add_timeout(1.0/60, updateOverlay, self);
601 } else if (elapsed < 3500) {
602 self->overlayAlpha = 255;
603 Fl::add_timeout(3.0, updateOverlay, self);
604 } else if (elapsed < 4000) {
605 self->overlayAlpha = (unsigned)255 * (4000 - elapsed) / 500;
606 Fl::add_timeout(1.0/60, updateOverlay, self);
607 } else {
608 delete self->overlay;
609 self->overlay = NULL;
610 }
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100611
612 self->damage(FL_DAMAGE_USER1);
613}
614
615
Pierre Ossman407a5c32011-05-26 14:48:29 +0000616int DesktopWindow::handle(int event)
617{
618 switch (event) {
Pierre Ossman407a5c32011-05-26 14:48:29 +0000619 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000620 fullScreen.setParam(fullscreen_active());
621
Pierre Ossman13548812017-01-03 16:12:30 +0100622 // Update scroll bars
623 repositionWidgets();
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000624
Pierre Ossman407a5c32011-05-26 14:48:29 +0000625 if (!fullscreenSystemKeys)
626 break;
627
628 if (fullscreen_active())
629 grabKeyboard();
630 else
631 ungrabKeyboard();
632
633 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000634
635 case FL_ENTER:
636 case FL_LEAVE:
637 case FL_DRAG:
638 case FL_MOVE:
639 if (fullscreen_active()) {
640 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
641 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
642 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
643 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
644 if (!Fl::has_timeout(handleEdgeScroll, this))
645 Fl::add_timeout(0.1, handleEdgeScroll, this);
646 }
647 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000648 // Continue processing so that the viewport also gets mouse events
649 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000650 }
651
652 return Fl_Window::handle(event);
653}
654
655
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000656int DesktopWindow::fltkHandle(int event, Fl_Window *win)
657{
658 int ret;
659
660 ret = Fl::handle_(event, win);
661
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000662 // This is hackish and the result of the dodgy focus handling in FLTK.
663 // The basic problem is that FLTK's view of focus and the system's tend
664 // to differ, and as a result we do not see all the FL_FOCUS events we
665 // need. Fortunately we can grab them here...
666
667 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
668
669 if (dw && fullscreenSystemKeys) {
670 switch (event) {
671 case FL_FOCUS:
672 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
673 // some issues we need to work around:
674 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
675 // b) Gaining focus on the system level causes FLTK to switch
676 // window level on OS X.
677 if (dw->fullscreen_active())
678 dw->grabKeyboard();
679 break;
680
Pierre Ossmanb1369802012-10-17 07:59:36 +0000681 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000682 // FIXME: We need to relinquish control when the entire window loses
683 // focus as it is very tied to this specific window on some
684 // platforms and we want to be able to open subwindows.
685 dw->ungrabKeyboard();
686 break;
687 }
688 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000689
690 return ret;
691}
692
693
Pierre Ossmanaae38912012-07-13 11:22:55 +0000694void DesktopWindow::fullscreen_on()
695{
Pierre Ossmanaae38912012-07-13 11:22:55 +0000696 if (not fullScreenAllMonitors)
697 fullscreen_screens(-1, -1, -1, -1);
698 else {
699 int top, bottom, left, right;
700 int top_y, bottom_y, left_x, right_x;
701
702 int sx, sy, sw, sh;
703
704 top = bottom = left = right = 0;
705
706 Fl::screen_xywh(sx, sy, sw, sh, 0);
707 top_y = sy;
708 bottom_y = sy + sh;
709 left_x = sx;
710 right_x = sx + sw;
711
712 for (int i = 1;i < Fl::screen_count();i++) {
713 Fl::screen_xywh(sx, sy, sw, sh, i);
714 if (sy < top_y) {
715 top = i;
716 top_y = sy;
717 }
718 if ((sy + sh) > bottom_y) {
719 bottom = i;
720 bottom_y = sy + sh;
721 }
722 if (sx < left_x) {
723 left = i;
724 left_x = sx;
725 }
726 if ((sx + sw) > right_x) {
727 right = i;
728 right_x = sx + sw;
729 }
730 }
731
732 fullscreen_screens(top, bottom, left, right);
733 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000734
735 fullscreen();
Pierre Ossmanaae38912012-07-13 11:22:55 +0000736}
737
Pierre Ossman407a5c32011-05-26 14:48:29 +0000738void DesktopWindow::grabKeyboard()
739{
740 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
741 // correct widget regardless of which low level window got the system
742 // event.
743
744 // FIXME: Push this stuff into FLTK.
745
746#if defined(WIN32)
747 int ret;
748
749 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
750 if (ret != 0)
751 vlog.error(_("Failure grabbing keyboard"));
752#elif defined(__APPLE__)
753 int ret;
754
Pierre Ossman56610fb2015-01-27 16:28:15 +0100755 ret = cocoa_capture_display(this, fullScreenAllMonitors);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000756 if (ret != 0)
757 vlog.error(_("Failure grabbing keyboard"));
758#else
759 int ret;
760
761 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000762 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000763 if (ret) {
764 if (ret == AlreadyGrabbed) {
765 // It seems like we can race with the WM in some cases.
766 // Try again in a bit.
767 if (!Fl::has_timeout(handleGrab, this))
768 Fl::add_timeout(0.500, handleGrab, this);
769 } else {
770 vlog.error(_("Failure grabbing keyboard"));
771 }
772 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000773
774 // We also need to grab the pointer as some WMs like to grab buttons
775 // combined with modifies (e.g. Alt+Button0 in metacity).
776 ret = XGrabPointer(fl_display, fl_xid(this), True,
777 ButtonPressMask|ButtonReleaseMask|
778 ButtonMotionMask|PointerMotionMask,
779 GrabModeAsync, GrabModeAsync,
780 None, None, CurrentTime);
781 if (ret)
782 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000783#endif
784}
785
786
787void DesktopWindow::ungrabKeyboard()
788{
789 Fl::remove_timeout(handleGrab, this);
790
791#if defined(WIN32)
792 win32_disable_lowlevel_keyboard(fl_xid(this));
793#elif defined(__APPLE__)
794 cocoa_release_display(this);
795#else
796 // FLTK has a grab so lets not mess with it
797 if (Fl::grab())
798 return;
799
Pierre Ossman53fd5442011-11-17 10:19:19 +0000800 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000801 XUngrabKeyboard(fl_display, fl_event_time);
802#endif
803}
804
805
806void DesktopWindow::handleGrab(void *data)
807{
808 DesktopWindow *self = (DesktopWindow*)data;
809
810 assert(self);
811
Pierre Ossman407a5c32011-05-26 14:48:29 +0000812 if (!fullscreenSystemKeys)
813 return;
814 if (!self->fullscreen_active())
815 return;
816
817 self->grabKeyboard();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000818}
819
820
Peter Åstrand49b11572012-08-01 08:09:09 +0000821#define _NET_WM_STATE_ADD 1 /* add/set property */
822void DesktopWindow::maximizeWindow()
823{
824#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000825 // We cannot use ShowWindow() in full screen mode as it will
826 // resize things implicitly. Fortunately modifying the style
827 // directly results in a maximized state once we leave full screen.
Pierre Ossman897067b2012-10-11 09:17:19 +0000828 if (fullscreen_active()) {
829 WINDOWINFO wi;
830 wi.cbSize = sizeof(WINDOWINFO);
831 GetWindowInfo(fl_xid(this), &wi);
832 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
833 } else
Pierre Ossman897067b2012-10-11 09:17:19 +0000834 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000835#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000836 // OS X is somewhat strange and does not really have a concept of a
837 // maximized window, so we can simply resize the window to the workarea.
838 // Note that we shouldn't do this whilst in full screen as that will
839 // incorrectly adjust things.
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000840 if (fullscreen_active())
841 return;
Peter Åstrand49b11572012-08-01 08:09:09 +0000842 int X, Y, W, H;
843 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
844 size(W, H);
845#else
846 // X11
847 fl_open_display();
848 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
849 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
850 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
851
852 XEvent e;
853 e.xany.type = ClientMessage;
854 e.xany.window = fl_xid(this);
855 e.xclient.message_type = net_wm_state;
856 e.xclient.format = 32;
857 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
858 e.xclient.data.l[1] = net_wm_state_maximized_vert;
859 e.xclient.data.l[2] = net_wm_state_maximized_horz;
860 e.xclient.data.l[3] = 0;
861 e.xclient.data.l[4] = 0;
862 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
863#endif
864}
865
866
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000867void DesktopWindow::handleDesktopSize()
868{
Pierre Ossman135906e2015-04-27 12:48:47 +0200869 if (strcmp(desktopSize, "") != 0) {
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100870 int width, height;
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000871
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100872 // An explicit size has been requested
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000873
Pierre Ossman135906e2015-04-27 12:48:47 +0200874 if (sscanf(desktopSize, "%dx%d", &width, &height) != 2)
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100875 return;
876
877 remoteResize(width, height);
878 } else if (::remoteResize) {
879 // No explicit size, but remote resizing is on so make sure it
880 // matches whatever size the window ended up being
881 remoteResize(w(), h());
882 }
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000883}
884
885
Pierre Ossmanff473402012-07-04 11:27:47 +0000886void DesktopWindow::handleResizeTimeout(void *data)
887{
888 DesktopWindow *self = (DesktopWindow *)data;
889
890 assert(self);
891
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000892 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000893}
894
895
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000896void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000897{
Pierre Ossmanff473402012-07-04 11:27:47 +0000898 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000899 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000900
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000901 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000902 // In windowed mode (or the framebuffer is so large that we need
903 // to scroll) we just report a single virtual screen that covers
904 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000905
Pierre Ossmanaae38912012-07-13 11:22:55 +0000906 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000907
Pierre Ossmanaae38912012-07-13 11:22:55 +0000908 // Not sure why we have no screens, but adding a new one should be
909 // safe as there is nothing to conflict with...
910 if (layout.num_screens() == 0)
911 layout.add_screen(rfb::Screen());
912 else if (layout.num_screens() != 1) {
913 // More than one screen. Remove all but the first (which we
914 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000915
Pierre Ossmanaae38912012-07-13 11:22:55 +0000916 while (true) {
917 iter = layout.begin();
918 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000919
Pierre Ossmanaae38912012-07-13 11:22:55 +0000920 if (iter == layout.end())
921 break;
922
923 layout.remove_screen(iter->id);
924 }
925 }
926
927 // Resize the remaining single screen to the complete framebuffer
928 layout.begin()->dimensions.tl.x = 0;
929 layout.begin()->dimensions.tl.y = 0;
930 layout.begin()->dimensions.br.x = width;
931 layout.begin()->dimensions.br.y = height;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000932 } else {
933 int i;
934 rdr::U32 id;
935 int sx, sy, sw, sh;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100936 rfb::Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000937
938 // In full screen we report all screens that are fully covered.
939
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000940 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
941 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000942
Pierre Ossmanaae38912012-07-13 11:22:55 +0000943 // If we can find a matching screen in the existing set, we use
944 // that, otherwise we create a brand new screen.
945 //
946 // FIXME: We should really track screens better so we can handle
947 // a resized one.
948 //
949 for (i = 0;i < Fl::screen_count();i++) {
950 Fl::screen_xywh(sx, sy, sw, sh, i);
951
Pierre Ossman93d2d922012-07-20 12:32:52 +0000952 // Check that the screen is fully inside the framebuffer
953 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000954 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000955 continue;
956
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000957 // Adjust the coordinates so they are relative to our viewport
958 sx -= viewport_rect.tl.x;
959 sy -= viewport_rect.tl.y;
960
Pierre Ossmanaae38912012-07-13 11:22:55 +0000961 // Look for perfectly matching existing screen...
962 for (iter = cc->cp.screenLayout.begin();
963 iter != cc->cp.screenLayout.end(); ++iter) {
964 if ((iter->dimensions.tl.x == sx) &&
965 (iter->dimensions.tl.y == sy) &&
966 (iter->dimensions.width() == sw) &&
967 (iter->dimensions.height() == sh))
968 break;
969 }
970
971 // Found it?
972 if (iter != cc->cp.screenLayout.end()) {
973 layout.add_screen(*iter);
974 continue;
975 }
976
977 // Need to add a new one, which means we need to find an unused id
978 while (true) {
979 id = rand();
980 for (iter = cc->cp.screenLayout.begin();
981 iter != cc->cp.screenLayout.end(); ++iter) {
982 if (iter->id == id)
983 break;
984 }
985
986 if (iter == cc->cp.screenLayout.end())
987 break;
988 }
989
990 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000991 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000992
993 // If the viewport doesn't match a physical screen, then we might
994 // end up with no screens in the layout. Add a fake one...
995 if (layout.num_screens() == 0)
996 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000997 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000998
999 // Do we actually change anything?
1000 if ((width == cc->cp.width) &&
1001 (height == cc->cp.height) &&
1002 (layout == cc->cp.screenLayout))
1003 return;
1004
Pierre Ossman9018af42015-01-26 15:15:47 +01001005 char buffer[2048];
1006 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
1007 cc->cp.width, cc->cp.height, width, height);
1008 layout.print(buffer, sizeof(buffer));
1009 vlog.debug("%s", buffer);
Pierre Ossmanaae38912012-07-13 11:22:55 +00001010
1011 if (!layout.validate(width, height)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +02001012 vlog.error(_("Invalid screen layout computed for resize request!"));
Pierre Ossmanaae38912012-07-13 11:22:55 +00001013 return;
1014 }
Pierre Ossmanff473402012-07-04 11:27:47 +00001015
1016 cc->writer()->writeSetDesktopSize(width, height, layout);
1017}
1018
1019
Pierre Ossman13548812017-01-03 16:12:30 +01001020void DesktopWindow::repositionWidgets()
Pierre Ossman6455d852011-06-01 09:33:00 +00001021{
1022 int new_x, new_y;
1023
Pierre Ossman13548812017-01-03 16:12:30 +01001024 // Viewport position
Pierre Ossman6455d852011-06-01 09:33:00 +00001025
1026 new_x = viewport->x();
1027 new_y = viewport->y();
1028
1029 if (w() > viewport->w())
1030 new_x = (w() - viewport->w()) / 2;
1031 else {
1032 if (viewport->x() > 0)
1033 new_x = 0;
1034 else if (w() > (viewport->x() + viewport->w()))
1035 new_x = w() - viewport->w();
1036 }
1037
1038 // Same thing for y axis
1039 if (h() > viewport->h())
1040 new_y = (h() - viewport->h()) / 2;
1041 else {
1042 if (viewport->y() > 0)
1043 new_y = 0;
1044 else if (h() > (viewport->y() + viewport->h()))
1045 new_y = h() - viewport->h();
1046 }
1047
1048 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
1049 viewport->position(new_x, new_y);
Pierre Ossman13548812017-01-03 16:12:30 +01001050 damage(FL_DAMAGE_SCROLL);
Pierre Ossman6455d852011-06-01 09:33:00 +00001051 }
Pierre Ossman13548812017-01-03 16:12:30 +01001052
1053 // Scrollbars visbility
1054
Luke Shumaker0a8c4d42017-05-23 14:03:15 -04001055 if (fullscreen_active()) {
Pierre Ossman13548812017-01-03 16:12:30 +01001056 hscroll->hide();
Pierre Ossman13548812017-01-03 16:12:30 +01001057 vscroll->hide();
Luke Shumaker0a8c4d42017-05-23 14:03:15 -04001058 } else {
1059 // Decide whether to show a scrollbar by checking if the window
1060 // size (possibly minus scrollbar_size) is less than the viewport
1061 // (remote framebuffer) size.
1062 //
1063 // We decide whether to subtract scrollbar_size on an axis by
1064 // checking if the other axis *definitely* needs a scrollbar. You
1065 // might be tempted to think that this becomes a weird recursive
1066 // problem, but it isn't: If the window size is less than the
1067 // viewport size (without subtracting the scrollbar_size), then
1068 // that axis *definitely* needs a scrollbar; if the check changes
1069 // when we subtract scrollbar_size, then that axis only *maybe*
1070 // needs a scrollbar. If both axes only "maybe" need a scrollbar,
1071 // then neither does; so we don't need to recurse on the "maybe"
1072 // cases.
1073
1074 if (w() - (h() < viewport->h() ? Fl::scrollbar_size() : 0) < viewport->w())
1075 hscroll->show();
1076 else
1077 hscroll->hide();
1078
1079 if (h() - (w() < viewport->w() ? Fl::scrollbar_size() : 0) < viewport->h())
1080 vscroll->show();
1081 else
1082 vscroll->hide();
1083 }
Pierre Ossman13548812017-01-03 16:12:30 +01001084
1085 // Scrollbars positions
1086
1087 hscroll->resize(0, h() - Fl::scrollbar_size(),
1088 w() - (vscroll->visible() ? Fl::scrollbar_size() : 0),
1089 Fl::scrollbar_size());
1090 vscroll->resize(w() - Fl::scrollbar_size(), 0,
1091 Fl::scrollbar_size(),
1092 h() - (hscroll->visible() ? Fl::scrollbar_size() : 0));
1093
1094 // Scrollbars range
1095
1096 hscroll->value(-viewport->x(),
1097 w() - (vscroll->visible() ? vscroll->w() : 0),
1098 0, viewport->w());
1099 vscroll->value(-viewport->y(),
1100 h() - (hscroll->visible() ? hscroll->h() : 0),
1101 0, viewport->h());
1102 hscroll->value(hscroll->clamp(hscroll->value()));
1103 vscroll->value(vscroll->clamp(vscroll->value()));
Pierre Ossman6455d852011-06-01 09:33:00 +00001104}
1105
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001106void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
1107{
1108 exit_vncviewer();
1109}
Pierre Ossman407a5c32011-05-26 14:48:29 +00001110
1111
1112void DesktopWindow::handleOptions(void *data)
1113{
1114 DesktopWindow *self = (DesktopWindow*)data;
1115
Pierre Ossman407a5c32011-05-26 14:48:29 +00001116 if (self->fullscreen_active() && fullscreenSystemKeys)
1117 self->grabKeyboard();
1118 else
1119 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +00001120
Pierre Ossmanff473402012-07-04 11:27:47 +00001121 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +00001122 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +00001123 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +00001124 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +00001125}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +00001126
1127void DesktopWindow::handleFullscreenTimeout(void *data)
1128{
1129 DesktopWindow *self = (DesktopWindow *)data;
1130
1131 assert(self);
1132
1133 self->delayedFullscreen = false;
1134
1135 if (self->delayedDesktopSize) {
1136 self->handleDesktopSize();
1137 self->delayedDesktopSize = false;
1138 }
1139}
Pierre Ossman1d867b62012-09-03 09:25:07 +00001140
Pierre Ossman13548812017-01-03 16:12:30 +01001141void DesktopWindow::scrollTo(int x, int y)
1142{
1143 x = hscroll->clamp(x);
1144 y = vscroll->clamp(y);
1145
1146 hscroll->value(x);
1147 vscroll->value(y);
1148
Pierre Ossman13548812017-01-03 16:12:30 +01001149 // Scrollbar position results in inverse movement of
1150 // the viewport widget
1151 x = -x;
1152 y = -y;
1153
1154 if ((viewport->x() == x) && (viewport->y() == y))
1155 return;
1156
1157 viewport->position(x, y);
1158 damage(FL_DAMAGE_SCROLL);
1159}
1160
1161void DesktopWindow::handleScroll(Fl_Widget *widget, void *data)
1162{
1163 DesktopWindow *self = (DesktopWindow *)data;
1164
1165 self->scrollTo(self->hscroll->value(), self->vscroll->value());
1166}
1167
Pierre Ossman1d867b62012-09-03 09:25:07 +00001168void DesktopWindow::handleEdgeScroll(void *data)
1169{
Pierre Ossman1d867b62012-09-03 09:25:07 +00001170 DesktopWindow *self = (DesktopWindow *)data;
1171
1172 int mx, my;
1173 int dx, dy;
1174
1175 assert(self);
1176
1177 if (!self->fullscreen_active())
1178 return;
1179
1180 mx = Fl::event_x();
1181 my = Fl::event_y();
1182
1183 dx = dy = 0;
1184
1185 // Clamp mouse position in case it is outside the window
1186 if (mx < 0)
1187 mx = 0;
1188 if (mx > self->w())
1189 mx = self->w();
1190 if (my < 0)
1191 my = 0;
1192 if (my > self->h())
1193 my = self->h();
1194
1195 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001196 dx = EDGE_SCROLL_SPEED -
1197 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
1198 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
1199 (mx > self->w() - EDGE_SCROLL_SIZE))
1200 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
1201 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +00001202 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001203 dy = EDGE_SCROLL_SPEED -
1204 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
1205 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
1206 (my > self->h() - EDGE_SCROLL_SIZE))
1207 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
1208 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +00001209
1210 if ((dx == 0) && (dy == 0))
1211 return;
1212
Luke Shumakere8d25d82017-05-23 02:16:27 -04001213 self->scrollTo(self->hscroll->value() - dx, self->vscroll->value() - dy);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001214
1215 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001216}
Pierre Ossman921f6c82017-02-24 12:33:09 +01001217
1218void DesktopWindow::handleStatsTimeout(void *data)
1219{
1220 DesktopWindow *self = (DesktopWindow*)data;
1221
Brian P. Hinzfc217142017-03-09 19:25:39 -05001222 const size_t statsCount = sizeof(self->stats)/sizeof(self->stats[0]);
Pierre Ossman921f6c82017-02-24 12:33:09 +01001223
1224 unsigned frame, pixels, pos;
1225 unsigned elapsed;
1226
1227 const unsigned statsWidth = 200;
1228 const unsigned statsHeight = 100;
1229 const unsigned graphWidth = statsWidth - 10;
1230 const unsigned graphHeight = statsHeight - 25;
1231
1232 Fl_Image_Surface *surface;
1233 Fl_RGB_Image *image;
1234
1235 unsigned maxFPS, maxPPS, maxBPS;
1236 size_t i;
1237
1238 char buffer[256];
1239
1240 frame = self->cc->getFrameCount();
1241 pixels = self->cc->getPixelCount();
1242 pos = self->cc->getPosition();
1243 elapsed = msSince(&self->statsLastTime);
1244 if (elapsed < 1)
1245 elapsed = 1;
1246
Brian P. Hinzfc217142017-03-09 19:25:39 -05001247 memmove(&self->stats[0], &self->stats[1], sizeof(self->stats[0])*(statsCount-1));
Pierre Ossman921f6c82017-02-24 12:33:09 +01001248
1249 self->stats[statsCount-1].fps = (frame - self->statsLastFrame) * 1000 / elapsed;
1250 self->stats[statsCount-1].pps = (pixels - self->statsLastPixels) * 1000 / elapsed;
1251 self->stats[statsCount-1].bps = (pos - self->statsLastPosition) * 1000 / elapsed;
1252
1253 gettimeofday(&self->statsLastTime, NULL);
1254 self->statsLastFrame = frame;
1255 self->statsLastPixels = pixels;
1256 self->statsLastPosition = pos;
1257
1258#if !defined(WIN32) && !defined(__APPLE__)
1259 // FLTK < 1.3.5 crashes if fl_gc is unset
1260 if (!fl_gc)
1261 fl_gc = XDefaultGC(fl_display, 0);
1262#endif
1263
1264 surface = new Fl_Image_Surface(statsWidth, statsHeight);
1265 surface->set_current();
1266
1267 fl_rectf(0, 0, statsWidth, statsHeight, FL_BLACK);
1268
1269 fl_rect(5, 5, graphWidth, graphHeight, FL_WHITE);
1270
1271 maxFPS = maxPPS = maxBPS = 0;
1272 for (i = 0;i < statsCount;i++) {
1273 if (self->stats[i].fps > maxFPS)
1274 maxFPS = self->stats[i].fps;
1275 if (self->stats[i].pps > maxPPS)
1276 maxPPS = self->stats[i].pps;
1277 if (self->stats[i].bps > maxBPS)
1278 maxBPS = self->stats[i].bps;
1279 }
1280
1281 if (maxFPS != 0) {
1282 fl_color(FL_GREEN);
1283 for (i = 0;i < statsCount-1;i++) {
1284 fl_line(5 + i * graphWidth / statsCount,
1285 5 + graphHeight - graphHeight * self->stats[i].fps / maxFPS,
1286 5 + (i+1) * graphWidth / statsCount,
1287 5 + graphHeight - graphHeight * self->stats[i+1].fps / maxFPS);
1288 }
1289 }
1290
1291 if (maxPPS != 0) {
1292 fl_color(FL_YELLOW);
1293 for (i = 0;i < statsCount-1;i++) {
1294 fl_line(5 + i * graphWidth / statsCount,
1295 5 + graphHeight - graphHeight * self->stats[i].pps / maxPPS,
1296 5 + (i+1) * graphWidth / statsCount,
1297 5 + graphHeight - graphHeight * self->stats[i+1].pps / maxPPS);
1298 }
1299 }
1300
1301 if (maxBPS != 0) {
1302 fl_color(FL_RED);
1303 for (i = 0;i < statsCount-1;i++) {
1304 fl_line(5 + i * graphWidth / statsCount,
1305 5 + graphHeight - graphHeight * self->stats[i].bps / maxBPS,
1306 5 + (i+1) * graphWidth / statsCount,
1307 5 + graphHeight - graphHeight * self->stats[i+1].bps / maxBPS);
1308 }
1309 }
1310
1311 fl_font(FL_HELVETICA, 10);
1312
1313 fl_color(FL_GREEN);
1314 snprintf(buffer, sizeof(buffer), "%u fps", self->stats[statsCount-1].fps);
1315 fl_draw(buffer, 5, statsHeight - 5);
1316
1317 fl_color(FL_YELLOW);
1318 siPrefix(self->stats[statsCount-1].pps * 8, "pix/s",
1319 buffer, sizeof(buffer), 3);
1320 fl_draw(buffer, 5 + (statsWidth-10)/3, statsHeight - 5);
1321
1322 fl_color(FL_RED);
1323 iecPrefix(self->stats[statsCount-1].bps * 8, "Bps",
1324 buffer, sizeof(buffer), 3);
1325 fl_draw(buffer, 5 + (statsWidth-10)*2/3, statsHeight - 5);
1326
1327 image = surface->image();
1328 delete surface;
1329
1330 Fl_Display_Device::display_device()->set_current();
1331
1332 delete self->statsGraph;
1333 self->statsGraph = new Surface(image);
1334 delete image;
1335
1336 self->damage(FL_DAMAGE_CHILD, self->w() - statsWidth - 30,
1337 self->h() - statsHeight - 30,
1338 statsWidth, statsHeight);
1339
1340 Fl::repeat_timeout(0.5, handleStatsTimeout, data);
1341}