blob: 1f0f55f2b6fd0c9b3ac9102a44320bbf61506b8b [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
350 if (offscreen)
351 statsGraph->blend(offscreen, ox - X, oy - Y, ox, oy, ow, oh, 204);
352 else
353 statsGraph->blend(ox - X, oy - Y, ox, oy, ow, oh, 204);
354 }
355
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100356 // Overlay (if active)
357 if (overlay) {
358 int ox, oy, ow, oh;
359
360 ox = X = (w() - overlay->width()) / 2;
361 oy = Y = 50;
362 ow = overlay->width();
363 oh = overlay->height();
364
365 fl_clip_box(ox, oy, ow, oh, ox, oy, ow, oh);
366
367 if (offscreen)
Pierre Ossman455566e2017-02-10 16:37:52 +0100368 overlay->blend(offscreen, ox - X, oy - Y, ox, oy, ow, oh, overlayAlpha);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100369 else
Pierre Ossman455566e2017-02-10 16:37:52 +0100370 overlay->blend(ox - X, oy - Y, ox, oy, ow, oh, overlayAlpha);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100371 }
Pierre Ossman13548812017-01-03 16:12:30 +0100372
Pierre Ossman3d74d882017-01-02 19:49:52 +0100373 // Flush offscreen surface to screen
374 if (offscreen) {
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100375 fl_clip_box(0, 0, w(), h(), X, Y, W, H);
Pierre Ossman3d74d882017-01-02 19:49:52 +0100376 offscreen->draw(X, Y, X, Y, W, H);
377 }
378
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100379 fl_pop_clip();
380 fl_pop_clip();
381
Pierre Ossman13548812017-01-03 16:12:30 +0100382 // Finally the scrollbars
383
384 if (redraw) {
385 draw_child(*hscroll);
386 draw_child(*vscroll);
387 } else {
388 update_child(*hscroll);
389 update_child(*vscroll);
390 }
391}
392
393
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000394void DesktopWindow::resize(int x, int y, int w, int h)
395{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000396 bool resizing;
397
Pierre Ossman0e593462012-09-03 09:43:23 +0000398#if ! (defined(WIN32) || defined(__APPLE__))
399 // X11 window managers will treat a resize to cover the entire
400 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman56610fb2015-01-27 16:28:15 +0100401 if (!fullscreen_active()) {
Pierre Ossman1f884e02012-10-30 10:26:23 +0000402 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000403
Pierre Ossman1f884e02012-10-30 10:26:23 +0000404 // If there is no X11 window, then this must be a resize request,
405 // not a notification from the X server.
406 if (!shown())
407 resize_req = true;
408 else {
409 // Otherwise we need to get the real window coordinates to tell
410 // the difference
411 XWindowAttributes actual;
412 Window cr;
413 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000414
Pierre Ossman1f884e02012-10-30 10:26:23 +0000415 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
416 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
417 0, 0, &wx, &wy, &cr);
418
419 // Actual resize request?
420 if ((wx != x) || (wy != y) ||
421 (actual.width != w) || (actual.height != h))
422 resize_req = true;
423 else
424 resize_req = false;
425 }
426
427 if (resize_req) {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000428 for (int i = 0;i < Fl::screen_count();i++) {
429 int sx, sy, sw, sh;
430
431 Fl::screen_xywh(sx, sy, sw, sh, i);
432
433 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200434 vlog.info(_("Adjusting window size to avoid accidental full screen request"));
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000435 // Assume a panel of some form and adjust the height
436 y += 20;
437 h -= 40;
438 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000439 }
440 }
441 }
442#endif
443
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000444 if ((this->w() != w) || (this->h() != h))
445 resizing = true;
446 else
447 resizing = false;
448
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000449 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000450
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000451 if (resizing) {
452 // Try to get the remote size to match our window size, provided
453 // the following conditions are true:
454 //
455 // a) The user has this feature turned on
456 // b) The server supports it
457 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000458 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000459 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000460 if (not firstUpdate and not delayedFullscreen and
461 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000462 // We delay updating the remote desktop as we tend to get a flood
463 // of resize events as the user is dragging the window.
464 Fl::remove_timeout(handleResizeTimeout, this);
465 Fl::add_timeout(0.5, handleResizeTimeout, this);
466 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000467
Pierre Ossman13548812017-01-03 16:12:30 +0100468 repositionWidgets();
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000469 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000470}
471
472
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100473void DesktopWindow::menuOverlay(void* data)
474{
475 DesktopWindow *self;
476
477 self = (DesktopWindow*)data;
478 self->setOverlay(_("Press %s to open the context menu"),
479 (const char*)menuKey);
480}
481
482void DesktopWindow::setOverlay(const char* text, ...)
483{
484 va_list ap;
485 char textbuf[1024];
486
487 Fl_Image_Surface *surface;
488
489 Fl_RGB_Image* imageText;
490 Fl_RGB_Image* image;
491
492 unsigned char* buffer;
493
494 int x, y;
495 int w, h;
496
497 unsigned char* a;
498 const unsigned char* b;
499
500 delete overlay;
Pierre Ossman455566e2017-02-10 16:37:52 +0100501 Fl::remove_timeout(updateOverlay, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100502
503 va_start(ap, text);
504 vsnprintf(textbuf, sizeof(textbuf), text, ap);
505 textbuf[sizeof(textbuf)-1] = '\0';
506 va_end(ap);
507
508#if !defined(WIN32) && !defined(__APPLE__)
509 // FLTK < 1.3.5 crashes if fl_gc is unset
510 if (!fl_gc)
511 fl_gc = XDefaultGC(fl_display, 0);
512#endif
513
514 fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2);
Pierre Ossman2e7c7442017-02-24 09:23:47 +0100515 w = 0;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100516 fl_measure(textbuf, w, h);
517
518 // Margins
519 w += 80;
520 h += 40;
521
522 surface = new Fl_Image_Surface(w, h);
523 surface->set_current();
524
525 fl_rectf(0, 0, w, h, 0, 0, 0);
526
527 fl_font(FL_HELVETICA, FL_NORMAL_SIZE * 2);
528 fl_color(FL_WHITE);
529 fl_draw(textbuf, 40, 20 + fl_height() - fl_descent());
530
531 imageText = surface->image();
532 delete surface;
533
534 Fl_Display_Device::display_device()->set_current();
535
536 buffer = new unsigned char[w * h * 4];
537 image = new Fl_RGB_Image(buffer, w, h, 4);
538
539 a = buffer;
540 for (x = 0;x < image->w() * image->h();x++) {
541 a[0] = a[1] = a[2] = 0x40;
542 a[3] = 0xcc;
543 a += 4;
544 }
545
546 a = buffer;
547 b = (const unsigned char*)imageText->data()[0];
548 for (y = 0;y < h;y++) {
549 for (x = 0;x < w;x++) {
550 unsigned char alpha;
551 alpha = *b;
552 a[0] = (unsigned)a[0] * (255 - alpha) / 255 + alpha;
553 a[1] = (unsigned)a[1] * (255 - alpha) / 255 + alpha;
554 a[2] = (unsigned)a[2] * (255 - alpha) / 255 + alpha;
555 a[3] = 255 - (255 - a[3]) * (255 - alpha) / 255;
556 a += 4;
557 b += imageText->d();
558 }
559 if (imageText->ld() != 0)
560 b += imageText->ld() - w * imageText->d();
561 }
562
563 delete imageText;
564
565 x = (this->w() - image->w()) / 2;
566 y = 50;
567 w = image->w();
568 h = image->h();
569
570 overlay = new Surface(image);
Pierre Ossman455566e2017-02-10 16:37:52 +0100571 overlayAlpha = 0;
572 gettimeofday(&overlayStart, NULL);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100573
574 delete image;
575
Pierre Ossman455566e2017-02-10 16:37:52 +0100576 Fl::add_timeout(1.0/60, updateOverlay, this);
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100577}
578
Pierre Ossman455566e2017-02-10 16:37:52 +0100579void DesktopWindow::updateOverlay(void *data)
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100580{
581 DesktopWindow *self;
Pierre Ossman455566e2017-02-10 16:37:52 +0100582 unsigned elapsed;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100583
584 self = (DesktopWindow*)data;
Pierre Ossman455566e2017-02-10 16:37:52 +0100585
586 elapsed = msSince(&self->overlayStart);
587
588 if (elapsed < 500) {
589 self->overlayAlpha = (unsigned)255 * elapsed / 500;
590 Fl::add_timeout(1.0/60, updateOverlay, self);
591 } else if (elapsed < 3500) {
592 self->overlayAlpha = 255;
593 Fl::add_timeout(3.0, updateOverlay, self);
594 } else if (elapsed < 4000) {
595 self->overlayAlpha = (unsigned)255 * (4000 - elapsed) / 500;
596 Fl::add_timeout(1.0/60, updateOverlay, self);
597 } else {
598 delete self->overlay;
599 self->overlay = NULL;
600 }
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100601
602 self->damage(FL_DAMAGE_USER1);
603}
604
605
Pierre Ossman407a5c32011-05-26 14:48:29 +0000606int DesktopWindow::handle(int event)
607{
608 switch (event) {
Pierre Ossman407a5c32011-05-26 14:48:29 +0000609 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000610 fullScreen.setParam(fullscreen_active());
611
Pierre Ossman13548812017-01-03 16:12:30 +0100612 // Update scroll bars
613 repositionWidgets();
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000614
Pierre Ossman407a5c32011-05-26 14:48:29 +0000615 if (!fullscreenSystemKeys)
616 break;
617
618 if (fullscreen_active())
619 grabKeyboard();
620 else
621 ungrabKeyboard();
622
623 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000624
625 case FL_ENTER:
626 case FL_LEAVE:
627 case FL_DRAG:
628 case FL_MOVE:
629 if (fullscreen_active()) {
630 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
631 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
632 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
633 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
634 if (!Fl::has_timeout(handleEdgeScroll, this))
635 Fl::add_timeout(0.1, handleEdgeScroll, this);
636 }
637 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000638 // Continue processing so that the viewport also gets mouse events
639 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000640 }
641
642 return Fl_Window::handle(event);
643}
644
645
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000646int DesktopWindow::fltkHandle(int event, Fl_Window *win)
647{
648 int ret;
649
650 ret = Fl::handle_(event, win);
651
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000652 // This is hackish and the result of the dodgy focus handling in FLTK.
653 // The basic problem is that FLTK's view of focus and the system's tend
654 // to differ, and as a result we do not see all the FL_FOCUS events we
655 // need. Fortunately we can grab them here...
656
657 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
658
659 if (dw && fullscreenSystemKeys) {
660 switch (event) {
661 case FL_FOCUS:
662 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
663 // some issues we need to work around:
664 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
665 // b) Gaining focus on the system level causes FLTK to switch
666 // window level on OS X.
667 if (dw->fullscreen_active())
668 dw->grabKeyboard();
669 break;
670
Pierre Ossmanb1369802012-10-17 07:59:36 +0000671 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000672 // FIXME: We need to relinquish control when the entire window loses
673 // focus as it is very tied to this specific window on some
674 // platforms and we want to be able to open subwindows.
675 dw->ungrabKeyboard();
676 break;
677 }
678 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000679
680 return ret;
681}
682
683
Pierre Ossmanaae38912012-07-13 11:22:55 +0000684void DesktopWindow::fullscreen_on()
685{
Pierre Ossmanaae38912012-07-13 11:22:55 +0000686 if (not fullScreenAllMonitors)
687 fullscreen_screens(-1, -1, -1, -1);
688 else {
689 int top, bottom, left, right;
690 int top_y, bottom_y, left_x, right_x;
691
692 int sx, sy, sw, sh;
693
694 top = bottom = left = right = 0;
695
696 Fl::screen_xywh(sx, sy, sw, sh, 0);
697 top_y = sy;
698 bottom_y = sy + sh;
699 left_x = sx;
700 right_x = sx + sw;
701
702 for (int i = 1;i < Fl::screen_count();i++) {
703 Fl::screen_xywh(sx, sy, sw, sh, i);
704 if (sy < top_y) {
705 top = i;
706 top_y = sy;
707 }
708 if ((sy + sh) > bottom_y) {
709 bottom = i;
710 bottom_y = sy + sh;
711 }
712 if (sx < left_x) {
713 left = i;
714 left_x = sx;
715 }
716 if ((sx + sw) > right_x) {
717 right = i;
718 right_x = sx + sw;
719 }
720 }
721
722 fullscreen_screens(top, bottom, left, right);
723 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000724
725 fullscreen();
Pierre Ossmanaae38912012-07-13 11:22:55 +0000726}
727
Pierre Ossman407a5c32011-05-26 14:48:29 +0000728void DesktopWindow::grabKeyboard()
729{
730 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
731 // correct widget regardless of which low level window got the system
732 // event.
733
734 // FIXME: Push this stuff into FLTK.
735
736#if defined(WIN32)
737 int ret;
738
739 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
740 if (ret != 0)
741 vlog.error(_("Failure grabbing keyboard"));
742#elif defined(__APPLE__)
743 int ret;
744
Pierre Ossman56610fb2015-01-27 16:28:15 +0100745 ret = cocoa_capture_display(this, fullScreenAllMonitors);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000746 if (ret != 0)
747 vlog.error(_("Failure grabbing keyboard"));
748#else
749 int ret;
750
751 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000752 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000753 if (ret) {
754 if (ret == AlreadyGrabbed) {
755 // It seems like we can race with the WM in some cases.
756 // Try again in a bit.
757 if (!Fl::has_timeout(handleGrab, this))
758 Fl::add_timeout(0.500, handleGrab, this);
759 } else {
760 vlog.error(_("Failure grabbing keyboard"));
761 }
762 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000763
764 // We also need to grab the pointer as some WMs like to grab buttons
765 // combined with modifies (e.g. Alt+Button0 in metacity).
766 ret = XGrabPointer(fl_display, fl_xid(this), True,
767 ButtonPressMask|ButtonReleaseMask|
768 ButtonMotionMask|PointerMotionMask,
769 GrabModeAsync, GrabModeAsync,
770 None, None, CurrentTime);
771 if (ret)
772 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000773#endif
774}
775
776
777void DesktopWindow::ungrabKeyboard()
778{
779 Fl::remove_timeout(handleGrab, this);
780
781#if defined(WIN32)
782 win32_disable_lowlevel_keyboard(fl_xid(this));
783#elif defined(__APPLE__)
784 cocoa_release_display(this);
785#else
786 // FLTK has a grab so lets not mess with it
787 if (Fl::grab())
788 return;
789
Pierre Ossman53fd5442011-11-17 10:19:19 +0000790 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000791 XUngrabKeyboard(fl_display, fl_event_time);
792#endif
793}
794
795
796void DesktopWindow::handleGrab(void *data)
797{
798 DesktopWindow *self = (DesktopWindow*)data;
799
800 assert(self);
801
Pierre Ossman407a5c32011-05-26 14:48:29 +0000802 if (!fullscreenSystemKeys)
803 return;
804 if (!self->fullscreen_active())
805 return;
806
807 self->grabKeyboard();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000808}
809
810
Peter Åstrand49b11572012-08-01 08:09:09 +0000811#define _NET_WM_STATE_ADD 1 /* add/set property */
812void DesktopWindow::maximizeWindow()
813{
814#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000815 // We cannot use ShowWindow() in full screen mode as it will
816 // resize things implicitly. Fortunately modifying the style
817 // directly results in a maximized state once we leave full screen.
Pierre Ossman897067b2012-10-11 09:17:19 +0000818 if (fullscreen_active()) {
819 WINDOWINFO wi;
820 wi.cbSize = sizeof(WINDOWINFO);
821 GetWindowInfo(fl_xid(this), &wi);
822 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
823 } else
Pierre Ossman897067b2012-10-11 09:17:19 +0000824 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000825#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000826 // OS X is somewhat strange and does not really have a concept of a
827 // maximized window, so we can simply resize the window to the workarea.
828 // Note that we shouldn't do this whilst in full screen as that will
829 // incorrectly adjust things.
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000830 if (fullscreen_active())
831 return;
Peter Åstrand49b11572012-08-01 08:09:09 +0000832 int X, Y, W, H;
833 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
834 size(W, H);
835#else
836 // X11
837 fl_open_display();
838 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
839 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
840 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
841
842 XEvent e;
843 e.xany.type = ClientMessage;
844 e.xany.window = fl_xid(this);
845 e.xclient.message_type = net_wm_state;
846 e.xclient.format = 32;
847 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
848 e.xclient.data.l[1] = net_wm_state_maximized_vert;
849 e.xclient.data.l[2] = net_wm_state_maximized_horz;
850 e.xclient.data.l[3] = 0;
851 e.xclient.data.l[4] = 0;
852 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
853#endif
854}
855
856
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000857void DesktopWindow::handleDesktopSize()
858{
Pierre Ossman135906e2015-04-27 12:48:47 +0200859 if (strcmp(desktopSize, "") != 0) {
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100860 int width, height;
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000861
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100862 // An explicit size has been requested
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000863
Pierre Ossman135906e2015-04-27 12:48:47 +0200864 if (sscanf(desktopSize, "%dx%d", &width, &height) != 2)
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100865 return;
866
867 remoteResize(width, height);
868 } else if (::remoteResize) {
869 // No explicit size, but remote resizing is on so make sure it
870 // matches whatever size the window ended up being
871 remoteResize(w(), h());
872 }
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000873}
874
875
Pierre Ossmanff473402012-07-04 11:27:47 +0000876void DesktopWindow::handleResizeTimeout(void *data)
877{
878 DesktopWindow *self = (DesktopWindow *)data;
879
880 assert(self);
881
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000882 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000883}
884
885
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000886void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000887{
Pierre Ossmanff473402012-07-04 11:27:47 +0000888 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000889 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000890
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000891 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000892 // In windowed mode (or the framebuffer is so large that we need
893 // to scroll) we just report a single virtual screen that covers
894 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000895
Pierre Ossmanaae38912012-07-13 11:22:55 +0000896 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000897
Pierre Ossmanaae38912012-07-13 11:22:55 +0000898 // Not sure why we have no screens, but adding a new one should be
899 // safe as there is nothing to conflict with...
900 if (layout.num_screens() == 0)
901 layout.add_screen(rfb::Screen());
902 else if (layout.num_screens() != 1) {
903 // More than one screen. Remove all but the first (which we
904 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000905
Pierre Ossmanaae38912012-07-13 11:22:55 +0000906 while (true) {
907 iter = layout.begin();
908 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000909
Pierre Ossmanaae38912012-07-13 11:22:55 +0000910 if (iter == layout.end())
911 break;
912
913 layout.remove_screen(iter->id);
914 }
915 }
916
917 // Resize the remaining single screen to the complete framebuffer
918 layout.begin()->dimensions.tl.x = 0;
919 layout.begin()->dimensions.tl.y = 0;
920 layout.begin()->dimensions.br.x = width;
921 layout.begin()->dimensions.br.y = height;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000922 } else {
923 int i;
924 rdr::U32 id;
925 int sx, sy, sw, sh;
Pierre Ossman4f0647d2017-01-04 15:34:26 +0100926 rfb::Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000927
928 // In full screen we report all screens that are fully covered.
929
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000930 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
931 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000932
Pierre Ossmanaae38912012-07-13 11:22:55 +0000933 // If we can find a matching screen in the existing set, we use
934 // that, otherwise we create a brand new screen.
935 //
936 // FIXME: We should really track screens better so we can handle
937 // a resized one.
938 //
939 for (i = 0;i < Fl::screen_count();i++) {
940 Fl::screen_xywh(sx, sy, sw, sh, i);
941
Pierre Ossman93d2d922012-07-20 12:32:52 +0000942 // Check that the screen is fully inside the framebuffer
943 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000944 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000945 continue;
946
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000947 // Adjust the coordinates so they are relative to our viewport
948 sx -= viewport_rect.tl.x;
949 sy -= viewport_rect.tl.y;
950
Pierre Ossmanaae38912012-07-13 11:22:55 +0000951 // Look for perfectly matching existing screen...
952 for (iter = cc->cp.screenLayout.begin();
953 iter != cc->cp.screenLayout.end(); ++iter) {
954 if ((iter->dimensions.tl.x == sx) &&
955 (iter->dimensions.tl.y == sy) &&
956 (iter->dimensions.width() == sw) &&
957 (iter->dimensions.height() == sh))
958 break;
959 }
960
961 // Found it?
962 if (iter != cc->cp.screenLayout.end()) {
963 layout.add_screen(*iter);
964 continue;
965 }
966
967 // Need to add a new one, which means we need to find an unused id
968 while (true) {
969 id = rand();
970 for (iter = cc->cp.screenLayout.begin();
971 iter != cc->cp.screenLayout.end(); ++iter) {
972 if (iter->id == id)
973 break;
974 }
975
976 if (iter == cc->cp.screenLayout.end())
977 break;
978 }
979
980 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000981 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000982
983 // If the viewport doesn't match a physical screen, then we might
984 // end up with no screens in the layout. Add a fake one...
985 if (layout.num_screens() == 0)
986 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000987 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000988
989 // Do we actually change anything?
990 if ((width == cc->cp.width) &&
991 (height == cc->cp.height) &&
992 (layout == cc->cp.screenLayout))
993 return;
994
Pierre Ossman9018af42015-01-26 15:15:47 +0100995 char buffer[2048];
996 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
997 cc->cp.width, cc->cp.height, width, height);
998 layout.print(buffer, sizeof(buffer));
999 vlog.debug("%s", buffer);
Pierre Ossmanaae38912012-07-13 11:22:55 +00001000
1001 if (!layout.validate(width, height)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +02001002 vlog.error(_("Invalid screen layout computed for resize request!"));
Pierre Ossmanaae38912012-07-13 11:22:55 +00001003 return;
1004 }
Pierre Ossmanff473402012-07-04 11:27:47 +00001005
1006 cc->writer()->writeSetDesktopSize(width, height, layout);
1007}
1008
1009
Pierre Ossman13548812017-01-03 16:12:30 +01001010void DesktopWindow::repositionWidgets()
Pierre Ossman6455d852011-06-01 09:33:00 +00001011{
1012 int new_x, new_y;
1013
Pierre Ossman13548812017-01-03 16:12:30 +01001014 // Viewport position
Pierre Ossman6455d852011-06-01 09:33:00 +00001015
1016 new_x = viewport->x();
1017 new_y = viewport->y();
1018
1019 if (w() > viewport->w())
1020 new_x = (w() - viewport->w()) / 2;
1021 else {
1022 if (viewport->x() > 0)
1023 new_x = 0;
1024 else if (w() > (viewport->x() + viewport->w()))
1025 new_x = w() - viewport->w();
1026 }
1027
1028 // Same thing for y axis
1029 if (h() > viewport->h())
1030 new_y = (h() - viewport->h()) / 2;
1031 else {
1032 if (viewport->y() > 0)
1033 new_y = 0;
1034 else if (h() > (viewport->y() + viewport->h()))
1035 new_y = h() - viewport->h();
1036 }
1037
1038 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
1039 viewport->position(new_x, new_y);
Pierre Ossman13548812017-01-03 16:12:30 +01001040 damage(FL_DAMAGE_SCROLL);
Pierre Ossman6455d852011-06-01 09:33:00 +00001041 }
Pierre Ossman13548812017-01-03 16:12:30 +01001042
1043 // Scrollbars visbility
1044
1045 if (!fullscreen_active() && (w() < viewport->w()))
1046 hscroll->show();
1047 else
1048 hscroll->hide();
1049
1050 if (!fullscreen_active() && (h() < viewport->h()))
1051 vscroll->show();
1052 else
1053 vscroll->hide();
1054
1055 // Scrollbars positions
1056
1057 hscroll->resize(0, h() - Fl::scrollbar_size(),
1058 w() - (vscroll->visible() ? Fl::scrollbar_size() : 0),
1059 Fl::scrollbar_size());
1060 vscroll->resize(w() - Fl::scrollbar_size(), 0,
1061 Fl::scrollbar_size(),
1062 h() - (hscroll->visible() ? Fl::scrollbar_size() : 0));
1063
1064 // Scrollbars range
1065
1066 hscroll->value(-viewport->x(),
1067 w() - (vscroll->visible() ? vscroll->w() : 0),
1068 0, viewport->w());
1069 vscroll->value(-viewport->y(),
1070 h() - (hscroll->visible() ? hscroll->h() : 0),
1071 0, viewport->h());
1072 hscroll->value(hscroll->clamp(hscroll->value()));
1073 vscroll->value(vscroll->clamp(vscroll->value()));
Pierre Ossman6455d852011-06-01 09:33:00 +00001074}
1075
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001076void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
1077{
1078 exit_vncviewer();
1079}
Pierre Ossman407a5c32011-05-26 14:48:29 +00001080
1081
1082void DesktopWindow::handleOptions(void *data)
1083{
1084 DesktopWindow *self = (DesktopWindow*)data;
1085
Pierre Ossman407a5c32011-05-26 14:48:29 +00001086 if (self->fullscreen_active() && fullscreenSystemKeys)
1087 self->grabKeyboard();
1088 else
1089 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +00001090
Pierre Ossmanff473402012-07-04 11:27:47 +00001091 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +00001092 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +00001093 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +00001094 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +00001095}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +00001096
1097void DesktopWindow::handleFullscreenTimeout(void *data)
1098{
1099 DesktopWindow *self = (DesktopWindow *)data;
1100
1101 assert(self);
1102
1103 self->delayedFullscreen = false;
1104
1105 if (self->delayedDesktopSize) {
1106 self->handleDesktopSize();
1107 self->delayedDesktopSize = false;
1108 }
1109}
Pierre Ossman1d867b62012-09-03 09:25:07 +00001110
Pierre Ossman13548812017-01-03 16:12:30 +01001111void DesktopWindow::scrollTo(int x, int y)
1112{
1113 x = hscroll->clamp(x);
1114 y = vscroll->clamp(y);
1115
1116 hscroll->value(x);
1117 vscroll->value(y);
1118
1119 if (!hscroll->visible())
1120 x = -viewport->x();
1121 if (!vscroll->visible())
1122 y = -viewport->y();
1123
1124 // Scrollbar position results in inverse movement of
1125 // the viewport widget
1126 x = -x;
1127 y = -y;
1128
1129 if ((viewport->x() == x) && (viewport->y() == y))
1130 return;
1131
1132 viewport->position(x, y);
1133 damage(FL_DAMAGE_SCROLL);
1134}
1135
1136void DesktopWindow::handleScroll(Fl_Widget *widget, void *data)
1137{
1138 DesktopWindow *self = (DesktopWindow *)data;
1139
1140 self->scrollTo(self->hscroll->value(), self->vscroll->value());
1141}
1142
Pierre Ossman1d867b62012-09-03 09:25:07 +00001143void DesktopWindow::handleEdgeScroll(void *data)
1144{
Pierre Ossman1d867b62012-09-03 09:25:07 +00001145 DesktopWindow *self = (DesktopWindow *)data;
1146
1147 int mx, my;
1148 int dx, dy;
1149
1150 assert(self);
1151
1152 if (!self->fullscreen_active())
1153 return;
1154
1155 mx = Fl::event_x();
1156 my = Fl::event_y();
1157
1158 dx = dy = 0;
1159
1160 // Clamp mouse position in case it is outside the window
1161 if (mx < 0)
1162 mx = 0;
1163 if (mx > self->w())
1164 mx = self->w();
1165 if (my < 0)
1166 my = 0;
1167 if (my > self->h())
1168 my = self->h();
1169
1170 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001171 dx = EDGE_SCROLL_SPEED -
1172 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
1173 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
1174 (mx > self->w() - EDGE_SCROLL_SIZE))
1175 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
1176 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +00001177 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +00001178 dy = EDGE_SCROLL_SPEED -
1179 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
1180 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
1181 (my > self->h() - EDGE_SCROLL_SIZE))
1182 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
1183 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +00001184
1185 if ((dx == 0) && (dy == 0))
1186 return;
1187
Pierre Ossman13548812017-01-03 16:12:30 +01001188 self->scrollTo(self->hscroll->value() + dx, self->vscroll->value() + dy);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001189
1190 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
Pierre Ossman1d867b62012-09-03 09:25:07 +00001191}
Pierre Ossman921f6c82017-02-24 12:33:09 +01001192
1193void DesktopWindow::handleStatsTimeout(void *data)
1194{
1195 DesktopWindow *self = (DesktopWindow*)data;
1196
1197 const size_t statsCount = sizeof(stats)/sizeof(stats[0]);
1198
1199 unsigned frame, pixels, pos;
1200 unsigned elapsed;
1201
1202 const unsigned statsWidth = 200;
1203 const unsigned statsHeight = 100;
1204 const unsigned graphWidth = statsWidth - 10;
1205 const unsigned graphHeight = statsHeight - 25;
1206
1207 Fl_Image_Surface *surface;
1208 Fl_RGB_Image *image;
1209
1210 unsigned maxFPS, maxPPS, maxBPS;
1211 size_t i;
1212
1213 char buffer[256];
1214
1215 frame = self->cc->getFrameCount();
1216 pixels = self->cc->getPixelCount();
1217 pos = self->cc->getPosition();
1218 elapsed = msSince(&self->statsLastTime);
1219 if (elapsed < 1)
1220 elapsed = 1;
1221
1222 memmove(&self->stats[0], &self->stats[1], sizeof(stats[0])*(statsCount-1));
1223
1224 self->stats[statsCount-1].fps = (frame - self->statsLastFrame) * 1000 / elapsed;
1225 self->stats[statsCount-1].pps = (pixels - self->statsLastPixels) * 1000 / elapsed;
1226 self->stats[statsCount-1].bps = (pos - self->statsLastPosition) * 1000 / elapsed;
1227
1228 gettimeofday(&self->statsLastTime, NULL);
1229 self->statsLastFrame = frame;
1230 self->statsLastPixels = pixels;
1231 self->statsLastPosition = pos;
1232
1233#if !defined(WIN32) && !defined(__APPLE__)
1234 // FLTK < 1.3.5 crashes if fl_gc is unset
1235 if (!fl_gc)
1236 fl_gc = XDefaultGC(fl_display, 0);
1237#endif
1238
1239 surface = new Fl_Image_Surface(statsWidth, statsHeight);
1240 surface->set_current();
1241
1242 fl_rectf(0, 0, statsWidth, statsHeight, FL_BLACK);
1243
1244 fl_rect(5, 5, graphWidth, graphHeight, FL_WHITE);
1245
1246 maxFPS = maxPPS = maxBPS = 0;
1247 for (i = 0;i < statsCount;i++) {
1248 if (self->stats[i].fps > maxFPS)
1249 maxFPS = self->stats[i].fps;
1250 if (self->stats[i].pps > maxPPS)
1251 maxPPS = self->stats[i].pps;
1252 if (self->stats[i].bps > maxBPS)
1253 maxBPS = self->stats[i].bps;
1254 }
1255
1256 if (maxFPS != 0) {
1257 fl_color(FL_GREEN);
1258 for (i = 0;i < statsCount-1;i++) {
1259 fl_line(5 + i * graphWidth / statsCount,
1260 5 + graphHeight - graphHeight * self->stats[i].fps / maxFPS,
1261 5 + (i+1) * graphWidth / statsCount,
1262 5 + graphHeight - graphHeight * self->stats[i+1].fps / maxFPS);
1263 }
1264 }
1265
1266 if (maxPPS != 0) {
1267 fl_color(FL_YELLOW);
1268 for (i = 0;i < statsCount-1;i++) {
1269 fl_line(5 + i * graphWidth / statsCount,
1270 5 + graphHeight - graphHeight * self->stats[i].pps / maxPPS,
1271 5 + (i+1) * graphWidth / statsCount,
1272 5 + graphHeight - graphHeight * self->stats[i+1].pps / maxPPS);
1273 }
1274 }
1275
1276 if (maxBPS != 0) {
1277 fl_color(FL_RED);
1278 for (i = 0;i < statsCount-1;i++) {
1279 fl_line(5 + i * graphWidth / statsCount,
1280 5 + graphHeight - graphHeight * self->stats[i].bps / maxBPS,
1281 5 + (i+1) * graphWidth / statsCount,
1282 5 + graphHeight - graphHeight * self->stats[i+1].bps / maxBPS);
1283 }
1284 }
1285
1286 fl_font(FL_HELVETICA, 10);
1287
1288 fl_color(FL_GREEN);
1289 snprintf(buffer, sizeof(buffer), "%u fps", self->stats[statsCount-1].fps);
1290 fl_draw(buffer, 5, statsHeight - 5);
1291
1292 fl_color(FL_YELLOW);
1293 siPrefix(self->stats[statsCount-1].pps * 8, "pix/s",
1294 buffer, sizeof(buffer), 3);
1295 fl_draw(buffer, 5 + (statsWidth-10)/3, statsHeight - 5);
1296
1297 fl_color(FL_RED);
1298 iecPrefix(self->stats[statsCount-1].bps * 8, "Bps",
1299 buffer, sizeof(buffer), 3);
1300 fl_draw(buffer, 5 + (statsWidth-10)*2/3, statsHeight - 5);
1301
1302 image = surface->image();
1303 delete surface;
1304
1305 Fl_Display_Device::display_device()->set_current();
1306
1307 delete self->statsGraph;
1308 self->statsGraph = new Surface(image);
1309 delete image;
1310
1311 self->damage(FL_DAMAGE_CHILD, self->w() - statsWidth - 30,
1312 self->h() - statsHeight - 30,
1313 statsWidth, statsHeight);
1314
1315 Fl::repeat_timeout(0.5, handleStatsTimeout, data);
1316}