blob: 897f9557382b3ef679d462563d9051404c0a43f5 [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
Pierre Ossman1668cfa2016-12-10 17:13:40 +0100669 if (dw) {
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000670 switch (event) {
671 case FL_FOCUS:
Pierre Ossman1668cfa2016-12-10 17:13:40 +0100672 if (fullscreenSystemKeys) {
673 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
674 // some issues we need to work around:
675 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
676 // b) Gaining focus on the system level causes FLTK to switch
677 // window level on OS X.
678 if (dw->fullscreen_active())
679 dw->grabKeyboard();
680 }
681
682 // We may have gotten our lock keys out of sync with the server
683 // whilst we didn't have focus. Try to sort this out.
684 dw->viewport->pushLEDState();
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000685 break;
686
Pierre Ossmanb1369802012-10-17 07:59:36 +0000687 case FL_UNFOCUS:
Pierre Ossman1668cfa2016-12-10 17:13:40 +0100688 if (fullscreenSystemKeys) {
689 // FIXME: We need to relinquish control when the entire window loses
690 // focus as it is very tied to this specific window on some
691 // platforms and we want to be able to open subwindows.
692 dw->ungrabKeyboard();
693 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000694 break;
695 }
696 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000697
698 return ret;
699}
700
701
Pierre Ossmanaae38912012-07-13 11:22:55 +0000702void DesktopWindow::fullscreen_on()
703{
Pierre Ossmanaae38912012-07-13 11:22:55 +0000704 if (not fullScreenAllMonitors)
705 fullscreen_screens(-1, -1, -1, -1);
706 else {
707 int top, bottom, left, right;
708 int top_y, bottom_y, left_x, right_x;
709
710 int sx, sy, sw, sh;
711
712 top = bottom = left = right = 0;
713
714 Fl::screen_xywh(sx, sy, sw, sh, 0);
715 top_y = sy;
716 bottom_y = sy + sh;
717 left_x = sx;
718 right_x = sx + sw;
719
720 for (int i = 1;i < Fl::screen_count();i++) {
721 Fl::screen_xywh(sx, sy, sw, sh, i);
722 if (sy < top_y) {
723 top = i;
724 top_y = sy;
725 }
726 if ((sy + sh) > bottom_y) {
727 bottom = i;
728 bottom_y = sy + sh;
729 }
730 if (sx < left_x) {
731 left = i;
732 left_x = sx;
733 }
734 if ((sx + sw) > right_x) {
735 right = i;
736 right_x = sx + sw;
737 }
738 }
739
740 fullscreen_screens(top, bottom, left, right);
741 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000742
743 fullscreen();
Pierre Ossmanaae38912012-07-13 11:22:55 +0000744}
745
Pierre Ossman407a5c32011-05-26 14:48:29 +0000746void DesktopWindow::grabKeyboard()
747{
748 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
749 // correct widget regardless of which low level window got the system
750 // event.
751
752 // FIXME: Push this stuff into FLTK.
753
754#if defined(WIN32)
755 int ret;
756
757 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
758 if (ret != 0)
759 vlog.error(_("Failure grabbing keyboard"));
760#elif defined(__APPLE__)
761 int ret;
762
Pierre Ossman56610fb2015-01-27 16:28:15 +0100763 ret = cocoa_capture_display(this, fullScreenAllMonitors);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000764 if (ret != 0)
765 vlog.error(_("Failure grabbing keyboard"));
766#else
767 int ret;
768
769 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000770 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000771 if (ret) {
772 if (ret == AlreadyGrabbed) {
773 // It seems like we can race with the WM in some cases.
774 // Try again in a bit.
775 if (!Fl::has_timeout(handleGrab, this))
776 Fl::add_timeout(0.500, handleGrab, this);
777 } else {
778 vlog.error(_("Failure grabbing keyboard"));
779 }
780 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000781
782 // We also need to grab the pointer as some WMs like to grab buttons
783 // combined with modifies (e.g. Alt+Button0 in metacity).
784 ret = XGrabPointer(fl_display, fl_xid(this), True,
785 ButtonPressMask|ButtonReleaseMask|
786 ButtonMotionMask|PointerMotionMask,
787 GrabModeAsync, GrabModeAsync,
788 None, None, CurrentTime);
789 if (ret)
790 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000791#endif
792}
793
794
795void DesktopWindow::ungrabKeyboard()
796{
797 Fl::remove_timeout(handleGrab, this);
798
799#if defined(WIN32)
800 win32_disable_lowlevel_keyboard(fl_xid(this));
801#elif defined(__APPLE__)
802 cocoa_release_display(this);
803#else
804 // FLTK has a grab so lets not mess with it
805 if (Fl::grab())
806 return;
807
Pierre Ossman53fd5442011-11-17 10:19:19 +0000808 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000809 XUngrabKeyboard(fl_display, fl_event_time);
810#endif
811}
812
813
814void DesktopWindow::handleGrab(void *data)
815{
816 DesktopWindow *self = (DesktopWindow*)data;
817
818 assert(self);
819
Pierre Ossman407a5c32011-05-26 14:48:29 +0000820 if (!fullscreenSystemKeys)
821 return;
822 if (!self->fullscreen_active())
823 return;
824
825 self->grabKeyboard();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000826}
827
828
Peter Åstrand49b11572012-08-01 08:09:09 +0000829#define _NET_WM_STATE_ADD 1 /* add/set property */
830void DesktopWindow::maximizeWindow()
831{
832#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000833 // We cannot use ShowWindow() in full screen mode as it will
834 // resize things implicitly. Fortunately modifying the style
835 // directly results in a maximized state once we leave full screen.
Pierre Ossman897067b2012-10-11 09:17:19 +0000836 if (fullscreen_active()) {
837 WINDOWINFO wi;
838 wi.cbSize = sizeof(WINDOWINFO);
839 GetWindowInfo(fl_xid(this), &wi);
840 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
841 } else
Pierre Ossman897067b2012-10-11 09:17:19 +0000842 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000843#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000844 // OS X is somewhat strange and does not really have a concept of a
845 // maximized window, so we can simply resize the window to the workarea.
846 // Note that we shouldn't do this whilst in full screen as that will
847 // incorrectly adjust things.
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000848 if (fullscreen_active())
849 return;
Peter Åstrand49b11572012-08-01 08:09:09 +0000850 int X, Y, W, H;
851 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
852 size(W, H);
853#else
854 // X11
855 fl_open_display();
856 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
857 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
858 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
859
860 XEvent e;
861 e.xany.type = ClientMessage;
862 e.xany.window = fl_xid(this);
863 e.xclient.message_type = net_wm_state;
864 e.xclient.format = 32;
865 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
866 e.xclient.data.l[1] = net_wm_state_maximized_vert;
867 e.xclient.data.l[2] = net_wm_state_maximized_horz;
868 e.xclient.data.l[3] = 0;
869 e.xclient.data.l[4] = 0;
870 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
871#endif
872}
873
874
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000875void DesktopWindow::handleDesktopSize()
876{
Pierre Ossman135906e2015-04-27 12:48:47 +0200877 if (strcmp(desktopSize, "") != 0) {
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100878 int width, height;
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000879
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100880 // An explicit size has been requested
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000881
Pierre Ossman135906e2015-04-27 12:48:47 +0200882 if (sscanf(desktopSize, "%dx%d", &width, &height) != 2)
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100883 return;
884
885 remoteResize(width, height);
886 } else if (::remoteResize) {
887 // No explicit size, but remote resizing is on so make sure it
888 // matches whatever size the window ended up being
889 remoteResize(w(), h());
890 }
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000891}
892
893
Pierre Ossmanff473402012-07-04 11:27:47 +0000894void DesktopWindow::handleResizeTimeout(void *data)
895{
896 DesktopWindow *self = (DesktopWindow *)data;
897
898 assert(self);
899
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000900 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000901}
902
903
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000904void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000905{
Pierre Ossmanff473402012-07-04 11:27:47 +0000906 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000907 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000908
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000909 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000910 // In windowed mode (or the framebuffer is so large that we need
911 // to scroll) we just report a single virtual screen that covers
912 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000913
Pierre Ossmanaae38912012-07-13 11:22:55 +0000914 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000915
Pierre Ossmanaae38912012-07-13 11:22:55 +0000916 // Not sure why we have no screens, but adding a new one should be
917 // safe as there is nothing to conflict with...
918 if (layout.num_screens() == 0)
919 layout.add_screen(rfb::Screen());
920 else if (layout.num_screens() != 1) {
921 // More than one screen. Remove all but the first (which we
922 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000923
Pierre Ossmanaae38912012-07-13 11:22:55 +0000924 while (true) {
925 iter = layout.begin();
926 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000927
Pierre Ossmanaae38912012-07-13 11:22:55 +0000928 if (iter == layout.end())
929 break;
930
931 layout.remove_screen(iter->id);
932 }
933 }
934
935 // Resize the remaining single screen to the complete framebuffer
936 layout.begin()->dimensions.tl.x = 0;
937 layout.begin()->dimensions.tl.y = 0;
938 layout.begin()->dimensions.br.x = width;
939 layout.begin()->dimensions.br.y = height;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000940 } else {
941 int i;
942 rdr::U32 id;
943 int sx, sy, sw, sh;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100944 rfb::Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000945
946 // In full screen we report all screens that are fully covered.
947
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000948 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
949 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000950
Pierre Ossmanaae38912012-07-13 11:22:55 +0000951 // If we can find a matching screen in the existing set, we use
952 // that, otherwise we create a brand new screen.
953 //
954 // FIXME: We should really track screens better so we can handle
955 // a resized one.
956 //
957 for (i = 0;i < Fl::screen_count();i++) {
958 Fl::screen_xywh(sx, sy, sw, sh, i);
959
Pierre Ossman93d2d922012-07-20 12:32:52 +0000960 // Check that the screen is fully inside the framebuffer
961 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000962 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000963 continue;
964
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000965 // Adjust the coordinates so they are relative to our viewport
966 sx -= viewport_rect.tl.x;
967 sy -= viewport_rect.tl.y;
968
Pierre Ossmanaae38912012-07-13 11:22:55 +0000969 // Look for perfectly matching existing screen...
970 for (iter = cc->cp.screenLayout.begin();
971 iter != cc->cp.screenLayout.end(); ++iter) {
972 if ((iter->dimensions.tl.x == sx) &&
973 (iter->dimensions.tl.y == sy) &&
974 (iter->dimensions.width() == sw) &&
975 (iter->dimensions.height() == sh))
976 break;
977 }
978
979 // Found it?
980 if (iter != cc->cp.screenLayout.end()) {
981 layout.add_screen(*iter);
982 continue;
983 }
984
985 // Need to add a new one, which means we need to find an unused id
986 while (true) {
987 id = rand();
988 for (iter = cc->cp.screenLayout.begin();
989 iter != cc->cp.screenLayout.end(); ++iter) {
990 if (iter->id == id)
991 break;
992 }
993
994 if (iter == cc->cp.screenLayout.end())
995 break;
996 }
997
998 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000999 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +00001000
1001 // If the viewport doesn't match a physical screen, then we might
1002 // end up with no screens in the layout. Add a fake one...
1003 if (layout.num_screens() == 0)
1004 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +00001005 }
Pierre Ossmanff473402012-07-04 11:27:47 +00001006
1007 // Do we actually change anything?
1008 if ((width == cc->cp.width) &&
1009 (height == cc->cp.height) &&
1010 (layout == cc->cp.screenLayout))
1011 return;
1012
Pierre Ossman9018af42015-01-26 15:15:47 +01001013 char buffer[2048];
1014 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
1015 cc->cp.width, cc->cp.height, width, height);
1016 layout.print(buffer, sizeof(buffer));
1017 vlog.debug("%s", buffer);
Pierre Ossmanaae38912012-07-13 11:22:55 +00001018
1019 if (!layout.validate(width, height)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +02001020 vlog.error(_("Invalid screen layout computed for resize request!"));
Pierre Ossmanaae38912012-07-13 11:22:55 +00001021 return;
1022 }
Pierre Ossmanff473402012-07-04 11:27:47 +00001023
1024 cc->writer()->writeSetDesktopSize(width, height, layout);
1025}
1026
1027
Pierre Ossman13548812017-01-03 16:12:30 +01001028void DesktopWindow::repositionWidgets()
Pierre Ossman6455d852011-06-01 09:33:00 +00001029{
1030 int new_x, new_y;
1031
Pierre Ossman13548812017-01-03 16:12:30 +01001032 // Viewport position
Pierre Ossman6455d852011-06-01 09:33:00 +00001033
1034 new_x = viewport->x();
1035 new_y = viewport->y();
1036
1037 if (w() > viewport->w())
1038 new_x = (w() - viewport->w()) / 2;
1039 else {
1040 if (viewport->x() > 0)
1041 new_x = 0;
1042 else if (w() > (viewport->x() + viewport->w()))
1043 new_x = w() - viewport->w();
1044 }
1045
1046 // Same thing for y axis
1047 if (h() > viewport->h())
1048 new_y = (h() - viewport->h()) / 2;
1049 else {
1050 if (viewport->y() > 0)
1051 new_y = 0;
1052 else if (h() > (viewport->y() + viewport->h()))
1053 new_y = h() - viewport->h();
1054 }
1055
1056 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
1057 viewport->position(new_x, new_y);
Pierre Ossman13548812017-01-03 16:12:30 +01001058 damage(FL_DAMAGE_SCROLL);
Pierre Ossman6455d852011-06-01 09:33:00 +00001059 }
Pierre Ossman13548812017-01-03 16:12:30 +01001060
1061 // Scrollbars visbility
1062
Luke Shumaker0a8c4d42017-05-23 14:03:15 -04001063 if (fullscreen_active()) {
Pierre Ossman13548812017-01-03 16:12:30 +01001064 hscroll->hide();
Pierre Ossman13548812017-01-03 16:12:30 +01001065 vscroll->hide();
Luke Shumaker0a8c4d42017-05-23 14:03:15 -04001066 } else {
1067 // Decide whether to show a scrollbar by checking if the window
1068 // size (possibly minus scrollbar_size) is less than the viewport
1069 // (remote framebuffer) size.
1070 //
1071 // We decide whether to subtract scrollbar_size on an axis by
1072 // checking if the other axis *definitely* needs a scrollbar. You
1073 // might be tempted to think that this becomes a weird recursive
1074 // problem, but it isn't: If the window size is less than the
1075 // viewport size (without subtracting the scrollbar_size), then
1076 // that axis *definitely* needs a scrollbar; if the check changes
1077 // when we subtract scrollbar_size, then that axis only *maybe*
1078 // needs a scrollbar. If both axes only "maybe" need a scrollbar,
1079 // then neither does; so we don't need to recurse on the "maybe"
1080 // cases.
1081
1082 if (w() - (h() < viewport->h() ? Fl::scrollbar_size() : 0) < viewport->w())
1083 hscroll->show();
1084 else
1085 hscroll->hide();
1086
1087 if (h() - (w() < viewport->w() ? Fl::scrollbar_size() : 0) < viewport->h())
1088 vscroll->show();
1089 else
1090 vscroll->hide();
1091 }
Pierre Ossman13548812017-01-03 16:12:30 +01001092
1093 // Scrollbars positions
1094
1095 hscroll->resize(0, h() - Fl::scrollbar_size(),
1096 w() - (vscroll->visible() ? Fl::scrollbar_size() : 0),
1097 Fl::scrollbar_size());
1098 vscroll->resize(w() - Fl::scrollbar_size(), 0,
1099 Fl::scrollbar_size(),
1100 h() - (hscroll->visible() ? Fl::scrollbar_size() : 0));
1101
1102 // Scrollbars range
1103
1104 hscroll->value(-viewport->x(),
1105 w() - (vscroll->visible() ? vscroll->w() : 0),
1106 0, viewport->w());
1107 vscroll->value(-viewport->y(),
1108 h() - (hscroll->visible() ? hscroll->h() : 0),
1109 0, viewport->h());
1110 hscroll->value(hscroll->clamp(hscroll->value()));
1111 vscroll->value(vscroll->clamp(vscroll->value()));
Pierre Ossman6455d852011-06-01 09:33:00 +00001112}
1113
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001114void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
1115{
1116 exit_vncviewer();
1117}
Pierre Ossman407a5c32011-05-26 14:48:29 +00001118
1119
1120void DesktopWindow::handleOptions(void *data)
1121{
1122 DesktopWindow *self = (DesktopWindow*)data;
1123
Pierre Ossman407a5c32011-05-26 14:48:29 +00001124 if (self->fullscreen_active() && fullscreenSystemKeys)
1125 self->grabKeyboard();
1126 else
1127 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +00001128
Pierre Ossmanff473402012-07-04 11:27:47 +00001129 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +00001130 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +00001131 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +00001132 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +00001133}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +00001134
1135void DesktopWindow::handleFullscreenTimeout(void *data)
1136{
1137 DesktopWindow *self = (DesktopWindow *)data;
1138
1139 assert(self);
1140
1141 self->delayedFullscreen = false;
1142
1143 if (self->delayedDesktopSize) {
1144 self->handleDesktopSize();
1145 self->delayedDesktopSize = false;
1146 }
1147}
Pierre Ossman1d867b62012-09-03 09:25:07 +00001148
Pierre Ossman13548812017-01-03 16:12:30 +01001149void DesktopWindow::scrollTo(int x, int y)
1150{
1151 x = hscroll->clamp(x);
1152 y = vscroll->clamp(y);
1153
1154 hscroll->value(x);
1155 vscroll->value(y);
1156
Pierre Ossman13548812017-01-03 16:12:30 +01001157 // Scrollbar position results in inverse movement of
1158 // the viewport widget
1159 x = -x;
1160 y = -y;
1161
1162 if ((viewport->x() == x) && (viewport->y() == y))
1163 return;
1164
1165 viewport->position(x, y);
1166 damage(FL_DAMAGE_SCROLL);
1167}
1168
1169void DesktopWindow::handleScroll(Fl_Widget *widget, void *data)
1170{
1171 DesktopWindow *self = (DesktopWindow *)data;
1172
1173 self->scrollTo(self->hscroll->value(), self->vscroll->value());
1174}
1175
Pierre Ossman1d867b62012-09-03 09:25:07 +00001176void DesktopWindow::handleEdgeScroll(void *data)
1177{
Pierre Ossman1d867b62012-09-03 09:25:07 +00001178 DesktopWindow *self = (DesktopWindow *)data;
1179
1180 int mx, my;
1181 int dx, dy;
1182
1183 assert(self);
1184
1185 if (!self->fullscreen_active())
1186 return;
1187
1188 mx = Fl::event_x();
1189 my = Fl::event_y();
1190
1191 dx = dy = 0;
1192
1193 // Clamp mouse position in case it is outside the window
1194 if (mx < 0)
1195 mx = 0;
1196 if (mx > self->w())
1197 mx = self->w();
1198 if (my < 0)
1199 my = 0;
1200 if (my > self->h())
1201 my = self->h();
1202
1203 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001204 dx = EDGE_SCROLL_SPEED -
1205 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
1206 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
1207 (mx > self->w() - EDGE_SCROLL_SIZE))
1208 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
1209 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +00001210 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001211 dy = EDGE_SCROLL_SPEED -
1212 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
1213 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
1214 (my > self->h() - EDGE_SCROLL_SIZE))
1215 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
1216 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +00001217
1218 if ((dx == 0) && (dy == 0))
1219 return;
1220
Luke Shumakere8d25d82017-05-23 02:16:27 -04001221 self->scrollTo(self->hscroll->value() - dx, self->vscroll->value() - dy);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001222
1223 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001224}
Pierre Ossman921f6c82017-02-24 12:33:09 +01001225
1226void DesktopWindow::handleStatsTimeout(void *data)
1227{
1228 DesktopWindow *self = (DesktopWindow*)data;
1229
Brian P. Hinzfc217142017-03-09 19:25:39 -05001230 const size_t statsCount = sizeof(self->stats)/sizeof(self->stats[0]);
Pierre Ossman921f6c82017-02-24 12:33:09 +01001231
1232 unsigned frame, pixels, pos;
1233 unsigned elapsed;
1234
1235 const unsigned statsWidth = 200;
1236 const unsigned statsHeight = 100;
1237 const unsigned graphWidth = statsWidth - 10;
1238 const unsigned graphHeight = statsHeight - 25;
1239
1240 Fl_Image_Surface *surface;
1241 Fl_RGB_Image *image;
1242
1243 unsigned maxFPS, maxPPS, maxBPS;
1244 size_t i;
1245
1246 char buffer[256];
1247
1248 frame = self->cc->getFrameCount();
1249 pixels = self->cc->getPixelCount();
1250 pos = self->cc->getPosition();
1251 elapsed = msSince(&self->statsLastTime);
1252 if (elapsed < 1)
1253 elapsed = 1;
1254
Brian P. Hinzfc217142017-03-09 19:25:39 -05001255 memmove(&self->stats[0], &self->stats[1], sizeof(self->stats[0])*(statsCount-1));
Pierre Ossman921f6c82017-02-24 12:33:09 +01001256
1257 self->stats[statsCount-1].fps = (frame - self->statsLastFrame) * 1000 / elapsed;
1258 self->stats[statsCount-1].pps = (pixels - self->statsLastPixels) * 1000 / elapsed;
1259 self->stats[statsCount-1].bps = (pos - self->statsLastPosition) * 1000 / elapsed;
1260
1261 gettimeofday(&self->statsLastTime, NULL);
1262 self->statsLastFrame = frame;
1263 self->statsLastPixels = pixels;
1264 self->statsLastPosition = pos;
1265
1266#if !defined(WIN32) && !defined(__APPLE__)
1267 // FLTK < 1.3.5 crashes if fl_gc is unset
1268 if (!fl_gc)
1269 fl_gc = XDefaultGC(fl_display, 0);
1270#endif
1271
1272 surface = new Fl_Image_Surface(statsWidth, statsHeight);
1273 surface->set_current();
1274
1275 fl_rectf(0, 0, statsWidth, statsHeight, FL_BLACK);
1276
1277 fl_rect(5, 5, graphWidth, graphHeight, FL_WHITE);
1278
1279 maxFPS = maxPPS = maxBPS = 0;
1280 for (i = 0;i < statsCount;i++) {
1281 if (self->stats[i].fps > maxFPS)
1282 maxFPS = self->stats[i].fps;
1283 if (self->stats[i].pps > maxPPS)
1284 maxPPS = self->stats[i].pps;
1285 if (self->stats[i].bps > maxBPS)
1286 maxBPS = self->stats[i].bps;
1287 }
1288
1289 if (maxFPS != 0) {
1290 fl_color(FL_GREEN);
1291 for (i = 0;i < statsCount-1;i++) {
1292 fl_line(5 + i * graphWidth / statsCount,
1293 5 + graphHeight - graphHeight * self->stats[i].fps / maxFPS,
1294 5 + (i+1) * graphWidth / statsCount,
1295 5 + graphHeight - graphHeight * self->stats[i+1].fps / maxFPS);
1296 }
1297 }
1298
1299 if (maxPPS != 0) {
1300 fl_color(FL_YELLOW);
1301 for (i = 0;i < statsCount-1;i++) {
1302 fl_line(5 + i * graphWidth / statsCount,
1303 5 + graphHeight - graphHeight * self->stats[i].pps / maxPPS,
1304 5 + (i+1) * graphWidth / statsCount,
1305 5 + graphHeight - graphHeight * self->stats[i+1].pps / maxPPS);
1306 }
1307 }
1308
1309 if (maxBPS != 0) {
1310 fl_color(FL_RED);
1311 for (i = 0;i < statsCount-1;i++) {
1312 fl_line(5 + i * graphWidth / statsCount,
1313 5 + graphHeight - graphHeight * self->stats[i].bps / maxBPS,
1314 5 + (i+1) * graphWidth / statsCount,
1315 5 + graphHeight - graphHeight * self->stats[i+1].bps / maxBPS);
1316 }
1317 }
1318
1319 fl_font(FL_HELVETICA, 10);
1320
1321 fl_color(FL_GREEN);
1322 snprintf(buffer, sizeof(buffer), "%u fps", self->stats[statsCount-1].fps);
1323 fl_draw(buffer, 5, statsHeight - 5);
1324
1325 fl_color(FL_YELLOW);
1326 siPrefix(self->stats[statsCount-1].pps * 8, "pix/s",
1327 buffer, sizeof(buffer), 3);
1328 fl_draw(buffer, 5 + (statsWidth-10)/3, statsHeight - 5);
1329
1330 fl_color(FL_RED);
1331 iecPrefix(self->stats[statsCount-1].bps * 8, "Bps",
1332 buffer, sizeof(buffer), 3);
1333 fl_draw(buffer, 5 + (statsWidth-10)*2/3, statsHeight - 5);
1334
1335 image = surface->image();
1336 delete surface;
1337
1338 Fl_Display_Device::display_device()->set_current();
1339
1340 delete self->statsGraph;
1341 self->statsGraph = new Surface(image);
1342 delete image;
1343
1344 self->damage(FL_DAMAGE_CHILD, self->w() - statsWidth - 30,
1345 self->h() - statsHeight - 30,
1346 statsWidth, statsHeight);
1347
1348 Fl::repeat_timeout(0.5, handleStatsTimeout, data);
1349}