blob: e204662414974193bb67e369def3caa0d731e89f [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>
27
Pierre Ossman5156d5e2011-03-09 09:42:34 +000028#include <rfb/LogWriter.h>
Pierre Ossmanff473402012-07-04 11:27:47 +000029#include <rfb/CMsgWriter.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000030
31#include "DesktopWindow.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000032#include "OptionsDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000033#include "i18n.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000034#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000035#include "vncviewer.h"
Pierre Ossmanff473402012-07-04 11:27:47 +000036#include "CConn.h"
Pierre Ossman947b48d2014-01-27 16:52:35 +010037#include "Viewport.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000038
Pierre Ossman947b48d2014-01-27 16:52:35 +010039#include <FL/Fl.H>
DRCb65bb932011-06-24 03:17:00 +000040#include <FL/Fl_Scroll.H>
41#include <FL/x.H>
42
Pierre Ossman407a5c32011-05-26 14:48:29 +000043#ifdef WIN32
44#include "win32.h"
45#endif
46
47#ifdef __APPLE__
48#include "cocoa.h"
49#endif
Pierre Ossman89f868a2011-04-11 11:59:31 +000050
Pierre Ossman1d867b62012-09-03 09:25:07 +000051#define EDGE_SCROLL_SIZE 32
52#define EDGE_SCROLL_SPEED 20
53
Pierre Ossman5156d5e2011-03-09 09:42:34 +000054using namespace rfb;
55
Pierre Ossman5156d5e2011-03-09 09:42:34 +000056static rfb::LogWriter vlog("DesktopWindow");
57
58DesktopWindow::DesktopWindow(int w, int h, const char *name,
59 const rfb::PixelFormat& serverPF,
60 CConn* cc_)
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000061 : Fl_Window(w, h), cc(cc_), firstUpdate(true),
62 delayedFullscreen(false), delayedDesktopSize(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000063{
Pierre Ossman1d867b62012-09-03 09:25:07 +000064 scroll = new Fl_Scroll(0, 0, w, h);
Pierre Ossman4ae229f2011-04-15 12:58:31 +000065 scroll->color(FL_BLACK);
66
67 // Automatically adjust the scroll box to the window
68 resizable(scroll);
69
Pierre Ossmanff473402012-07-04 11:27:47 +000070 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000071
Pierre Ossman4ae229f2011-04-15 12:58:31 +000072 scroll->end();
73
Pierre Ossman5156d5e2011-03-09 09:42:34 +000074 callback(handleClose, this);
75
76 setName(name);
77
Pierre Ossman407a5c32011-05-26 14:48:29 +000078 OptionsDialog::addCallback(handleOptions, this);
79
Pierre Ossmana4f0f182011-06-14 13:36:57 +000080 // Hack. See below...
81 Fl::event_dispatch(&fltkHandle);
82
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000083 // Support for -geometry option. Note that although we do support
84 // negative coordinates, we do not support -XOFF-YOFF (ie
85 // coordinates relative to the right edge / bottom edge) at this
86 // time.
87 int geom_x = 0, geom_y = 0;
88 if (geometry.hasBeenSet()) {
89 int matched;
90 matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
91 if (matched == 2) {
92 force_position(1);
93 } else {
94 int geom_w, geom_h;
95 matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
96 switch (matched) {
97 case 4:
Pierre Ossman9d267c52012-10-02 14:30:22 +000098 force_position(1);
99 /* fall through */
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000100 case 2:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000101 w = geom_w;
102 h = geom_h;
103 break;
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000104 default:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000105 geom_x = geom_y = 0;
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200106 vlog.error(_("Invalid geometry specified!"));
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000107 }
108 }
109 }
110
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000111#ifdef __APPLE__
112 // On OS X we can do the maximize thing properly before the
113 // window is showned. Other platforms handled further down...
114 if (maximize) {
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000115#ifdef HAVE_FLTK_WORK_AREA
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000116 int dummy;
117 Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000118#else
119 w = Fl::w();
120 h = Fl::h();
121#endif
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000122 }
123#endif
124
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000125 if (force_position()) {
126 resize(geom_x, geom_y, w, h);
127 } else {
128 size(w, h);
129 }
130
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000131#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000132 if (fullScreen) {
133 // Hack: Window managers seem to be rather crappy at respecting
134 // fullscreen hints on initial windows. So on X11 we'll have to
135 // wait until after we've been mapped.
136#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000137 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000138#else
139 delayedFullscreen = true;
140#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000141 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000142#endif
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000143
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000144 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000145
Pierre Ossmancb68c192012-10-17 07:59:20 +0000146 // Full screen events are not sent out for a hidden window,
147 // so send a fake one here to set up things properly.
148#ifdef HAVE_FLTK_FULLSCREEN
149 if (fullscreen_active())
150 handle(FL_FULLSCREEN);
151#endif
152
Peter Åstrand49b11572012-08-01 08:09:09 +0000153 // Unfortunately, current FLTK does not allow us to set the
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000154 // maximized property on Windows and X11 before showing the window.
155 // See STR #2083 and STR #2178
156#ifndef __APPLE__
Peter Åstrand49b11572012-08-01 08:09:09 +0000157 if (maximize) {
158 maximizeWindow();
159 }
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000160#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000161
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000162 // The window manager might give us an initial window size that is different
163 // than the one we requested, and in those cases we need to manually adjust
164 // the scroll widget for things to behave sanely.
165 if ((w != this->w()) || (h != this->h()))
166 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000167
168#ifdef HAVE_FLTK_FULLSCREEN
169 if (delayedFullscreen) {
170 // Hack: Fullscreen requests may be ignored, so we need a timeout for
171 // when we should stop waiting. We also really need to wait for the
172 // resize, which can come after the fullscreen event.
173 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
174 fullscreen_on();
175 }
176#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000177}
178
179
180DesktopWindow::~DesktopWindow()
181{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000182 // Unregister all timeouts in case they get a change tro trigger
183 // again later when this object is already gone.
184 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000185 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000186 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000187 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000188
189 OptionsDialog::removeCallback(handleOptions);
190
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000191 // FLTK automatically deletes all child widgets, so we shouldn't touch
192 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000193}
194
195
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000196const rfb::PixelFormat &DesktopWindow::getPreferredPF()
197{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000198 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000199}
200
201
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000202void DesktopWindow::setName(const char *name)
203{
204 CharArray windowNameStr;
205 windowNameStr.replaceBuf(new char[256]);
206
Pierre Ossman27820ba2011-09-30 12:54:24 +0000207 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000208
209 copy_label(windowNameStr.buf);
210}
211
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000212
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200213rfb::ModifiablePixelBuffer* DesktopWindow::getFramebuffer(void)
214{
215 return viewport->getFramebuffer();
Pierre Ossman947b48d2014-01-27 16:52:35 +0100216}
217
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000218
219// Copy the areas of the framebuffer that have been changed (damaged)
220// to the displayed window.
221
222void DesktopWindow::updateWindow()
223{
Pierre Ossmanff473402012-07-04 11:27:47 +0000224 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000225 if (cc->cp.supportsSetDesktopSize) {
226 // Hack: Wait until we're in the proper mode and position until
227 // resizing things, otherwise we might send the wrong thing.
228 if (delayedFullscreen)
229 delayedDesktopSize = true;
230 else
231 handleDesktopSize();
232 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000233 firstUpdate = false;
234 }
235
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000236 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000237}
238
239
Pierre Ossman6455d852011-06-01 09:33:00 +0000240void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
241{
242 if ((new_w == viewport->w()) && (new_h == viewport->h()))
243 return;
244
Pierre Ossman6455d852011-06-01 09:33:00 +0000245 // If we're letting the viewport match the window perfectly, then
246 // keep things that way for the new size, otherwise just keep things
247 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000248#ifdef HAVE_FLTK_FULLSCREEN
249 if (!fullscreen_active()) {
250#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000251 if ((w() == viewport->w()) && (h() == viewport->h()))
252 size(new_w, new_h);
253 else {
254 // Make sure the window isn't too big. We do this manually because
255 // we have to disable the window size restriction (and it isn't
256 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000257 if ((w() > new_w) || (h() > new_h))
258 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000259 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000260#ifdef HAVE_FLTK_FULLSCREEN
261 }
262#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000263
264 viewport->size(new_w, new_h);
265
266 // We might not resize the main window, so we need to manually call this
267 // to make sure the viewport is centered.
268 repositionViewport();
269
Pierre Ossman6455d852011-06-01 09:33:00 +0000270 // repositionViewport() makes sure the scroll widget notices any changes
271 // in position, but it might be just the size that changes so we also
272 // need a poke here as well.
273 redraw();
274}
275
276
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000277void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
278 void* data, void* mask)
279{
280 viewport->setCursor(width, height, hotspot, data, mask);
281}
282
283
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000284void DesktopWindow::resize(int x, int y, int w, int h)
285{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000286 bool resizing;
287
Pierre Ossman0e593462012-09-03 09:43:23 +0000288#if ! (defined(WIN32) || defined(__APPLE__))
289 // X11 window managers will treat a resize to cover the entire
290 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman0e593462012-09-03 09:43:23 +0000291#ifdef HAVE_FLTK_FULLSCREEN
292 if (!fullscreen_active())
293#endif
294 {
Pierre Ossman1f884e02012-10-30 10:26:23 +0000295 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000296
Pierre Ossman1f884e02012-10-30 10:26:23 +0000297 // If there is no X11 window, then this must be a resize request,
298 // not a notification from the X server.
299 if (!shown())
300 resize_req = true;
301 else {
302 // Otherwise we need to get the real window coordinates to tell
303 // the difference
304 XWindowAttributes actual;
305 Window cr;
306 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000307
Pierre Ossman1f884e02012-10-30 10:26:23 +0000308 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
309 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
310 0, 0, &wx, &wy, &cr);
311
312 // Actual resize request?
313 if ((wx != x) || (wy != y) ||
314 (actual.width != w) || (actual.height != h))
315 resize_req = true;
316 else
317 resize_req = false;
318 }
319
320 if (resize_req) {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000321 for (int i = 0;i < Fl::screen_count();i++) {
322 int sx, sy, sw, sh;
323
324 Fl::screen_xywh(sx, sy, sw, sh, i);
325
326 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200327 vlog.info(_("Adjusting window size to avoid accidental full screen request"));
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000328 // Assume a panel of some form and adjust the height
329 y += 20;
330 h -= 40;
331 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000332 }
333 }
334 }
335#endif
336
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000337 if ((this->w() != w) || (this->h() != h))
338 resizing = true;
339 else
340 resizing = false;
341
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000342 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000343
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000344 if (resizing) {
345 // Try to get the remote size to match our window size, provided
346 // the following conditions are true:
347 //
348 // a) The user has this feature turned on
349 // b) The server supports it
350 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000351 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000352 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000353 if (not firstUpdate and not delayedFullscreen and
354 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000355 // We delay updating the remote desktop as we tend to get a flood
356 // of resize events as the user is dragging the window.
357 Fl::remove_timeout(handleResizeTimeout, this);
358 Fl::add_timeout(0.5, handleResizeTimeout, this);
359 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000360
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000361 // Deal with some scrolling corner cases
362 repositionViewport();
363 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000364}
365
366
Pierre Ossman407a5c32011-05-26 14:48:29 +0000367int DesktopWindow::handle(int event)
368{
369 switch (event) {
370#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000371 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000372 fullScreen.setParam(fullscreen_active());
373
Pierre Ossman1d867b62012-09-03 09:25:07 +0000374 if (fullscreen_active())
375 scroll->type(0);
376 else
377 scroll->type(Fl_Scroll::BOTH);
378
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000379 // The scroll widget isn't clever enough to actually redraw the
380 // scroll bars when they are added/removed, so we need to give
381 // it a push.
382 scroll->redraw();
383
Pierre Ossman407a5c32011-05-26 14:48:29 +0000384 if (!fullscreenSystemKeys)
385 break;
386
387 if (fullscreen_active())
388 grabKeyboard();
389 else
390 ungrabKeyboard();
391
392 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000393
394 case FL_ENTER:
395 case FL_LEAVE:
396 case FL_DRAG:
397 case FL_MOVE:
398 if (fullscreen_active()) {
399 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
400 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
401 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
402 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
403 if (!Fl::has_timeout(handleEdgeScroll, this))
404 Fl::add_timeout(0.1, handleEdgeScroll, this);
405 }
406 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000407 // Continue processing so that the viewport also gets mouse events
408 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000409#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000410
Pierre Ossman407a5c32011-05-26 14:48:29 +0000411 case FL_SHORTCUT:
412 // Sometimes the focus gets out of whack and we fall through to the
413 // shortcut dispatching. Try to make things sane again...
414 if (Fl::focus() == NULL) {
415 take_focus();
416 Fl::handle(FL_KEYDOWN, this);
417 }
418 return 1;
419 }
420
421 return Fl_Window::handle(event);
422}
423
424
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000425int DesktopWindow::fltkHandle(int event, Fl_Window *win)
426{
427 int ret;
428
429 ret = Fl::handle_(event, win);
430
431#ifdef HAVE_FLTK_FULLSCREEN
432 // This is hackish and the result of the dodgy focus handling in FLTK.
433 // The basic problem is that FLTK's view of focus and the system's tend
434 // to differ, and as a result we do not see all the FL_FOCUS events we
435 // need. Fortunately we can grab them here...
436
437 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
438
439 if (dw && fullscreenSystemKeys) {
440 switch (event) {
441 case FL_FOCUS:
442 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
443 // some issues we need to work around:
444 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
445 // b) Gaining focus on the system level causes FLTK to switch
446 // window level on OS X.
447 if (dw->fullscreen_active())
448 dw->grabKeyboard();
449 break;
450
Pierre Ossmanb1369802012-10-17 07:59:36 +0000451 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000452 // FIXME: We need to relinquish control when the entire window loses
453 // focus as it is very tied to this specific window on some
454 // platforms and we want to be able to open subwindows.
455 dw->ungrabKeyboard();
456 break;
457 }
458 }
459#endif
460
461 return ret;
462}
463
464
Pierre Ossmanaae38912012-07-13 11:22:55 +0000465void DesktopWindow::fullscreen_on()
466{
467#ifdef HAVE_FLTK_FULLSCREEN
468#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
469 if (not fullScreenAllMonitors)
470 fullscreen_screens(-1, -1, -1, -1);
471 else {
472 int top, bottom, left, right;
473 int top_y, bottom_y, left_x, right_x;
474
475 int sx, sy, sw, sh;
476
477 top = bottom = left = right = 0;
478
479 Fl::screen_xywh(sx, sy, sw, sh, 0);
480 top_y = sy;
481 bottom_y = sy + sh;
482 left_x = sx;
483 right_x = sx + sw;
484
485 for (int i = 1;i < Fl::screen_count();i++) {
486 Fl::screen_xywh(sx, sy, sw, sh, i);
487 if (sy < top_y) {
488 top = i;
489 top_y = sy;
490 }
491 if ((sy + sh) > bottom_y) {
492 bottom = i;
493 bottom_y = sy + sh;
494 }
495 if (sx < left_x) {
496 left = i;
497 left_x = sx;
498 }
499 if ((sx + sw) > right_x) {
500 right = i;
501 right_x = sx + sw;
502 }
503 }
504
505 fullscreen_screens(top, bottom, left, right);
506 }
507#endif // HAVE_FLTK_FULLSCREEN_SCREENS
508
509 fullscreen();
510#endif // HAVE_FLTK_FULLSCREEN
511}
512
Pierre Ossman407a5c32011-05-26 14:48:29 +0000513void DesktopWindow::grabKeyboard()
514{
515 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
516 // correct widget regardless of which low level window got the system
517 // event.
518
519 // FIXME: Push this stuff into FLTK.
520
521#if defined(WIN32)
522 int ret;
523
524 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
525 if (ret != 0)
526 vlog.error(_("Failure grabbing keyboard"));
527#elif defined(__APPLE__)
528 int ret;
529
Pierre Ossman3d759112012-08-27 14:40:51 +0000530 ret = cocoa_capture_display(this,
531#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
532 fullScreenAllMonitors
533#else
534 false
535#endif
536 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000537 if (ret != 0)
538 vlog.error(_("Failure grabbing keyboard"));
539#else
540 int ret;
541
542 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000543 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000544 if (ret) {
545 if (ret == AlreadyGrabbed) {
546 // It seems like we can race with the WM in some cases.
547 // Try again in a bit.
548 if (!Fl::has_timeout(handleGrab, this))
549 Fl::add_timeout(0.500, handleGrab, this);
550 } else {
551 vlog.error(_("Failure grabbing keyboard"));
552 }
553 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000554
555 // We also need to grab the pointer as some WMs like to grab buttons
556 // combined with modifies (e.g. Alt+Button0 in metacity).
557 ret = XGrabPointer(fl_display, fl_xid(this), True,
558 ButtonPressMask|ButtonReleaseMask|
559 ButtonMotionMask|PointerMotionMask,
560 GrabModeAsync, GrabModeAsync,
561 None, None, CurrentTime);
562 if (ret)
563 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000564#endif
565}
566
567
568void DesktopWindow::ungrabKeyboard()
569{
570 Fl::remove_timeout(handleGrab, this);
571
572#if defined(WIN32)
573 win32_disable_lowlevel_keyboard(fl_xid(this));
574#elif defined(__APPLE__)
575 cocoa_release_display(this);
576#else
577 // FLTK has a grab so lets not mess with it
578 if (Fl::grab())
579 return;
580
Pierre Ossman53fd5442011-11-17 10:19:19 +0000581 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000582 XUngrabKeyboard(fl_display, fl_event_time);
583#endif
584}
585
586
587void DesktopWindow::handleGrab(void *data)
588{
589 DesktopWindow *self = (DesktopWindow*)data;
590
591 assert(self);
592
593#ifdef HAVE_FLTK_FULLSCREEN
594 if (!fullscreenSystemKeys)
595 return;
596 if (!self->fullscreen_active())
597 return;
598
599 self->grabKeyboard();
600#endif
601}
602
603
Peter Åstrand49b11572012-08-01 08:09:09 +0000604#define _NET_WM_STATE_ADD 1 /* add/set property */
605void DesktopWindow::maximizeWindow()
606{
607#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000608 // We cannot use ShowWindow() in full screen mode as it will
609 // resize things implicitly. Fortunately modifying the style
610 // directly results in a maximized state once we leave full screen.
611#ifdef HAVE_FLTK_FULLSCREEN
612 if (fullscreen_active()) {
613 WINDOWINFO wi;
614 wi.cbSize = sizeof(WINDOWINFO);
615 GetWindowInfo(fl_xid(this), &wi);
616 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
617 } else
618#endif
619 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000620#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000621 // OS X is somewhat strange and does not really have a concept of a
622 // maximized window, so we can simply resize the window to the workarea.
623 // Note that we shouldn't do this whilst in full screen as that will
624 // incorrectly adjust things.
625#ifdef HAVE_FLTK_FULLSCREEN
626 if (fullscreen_active())
627 return;
628#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000629 int X, Y, W, H;
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000630#ifdef HAVE_FLTK_WORK_AREA
Peter Åstrand49b11572012-08-01 08:09:09 +0000631 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000632#else
633 W = Fl::w();
634 H = Fl::h();
635#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000636 size(W, H);
637#else
638 // X11
639 fl_open_display();
640 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
641 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
642 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
643
644 XEvent e;
645 e.xany.type = ClientMessage;
646 e.xany.window = fl_xid(this);
647 e.xclient.message_type = net_wm_state;
648 e.xclient.format = 32;
649 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
650 e.xclient.data.l[1] = net_wm_state_maximized_vert;
651 e.xclient.data.l[2] = net_wm_state_maximized_horz;
652 e.xclient.data.l[3] = 0;
653 e.xclient.data.l[4] = 0;
654 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
655#endif
656}
657
658
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000659void DesktopWindow::handleDesktopSize()
660{
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100661 if (desktopSize.hasBeenSet()) {
662 int width, height;
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000663
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100664 // An explicit size has been requested
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000665
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100666 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
667 return;
668
669 remoteResize(width, height);
670 } else if (::remoteResize) {
671 // No explicit size, but remote resizing is on so make sure it
672 // matches whatever size the window ended up being
673 remoteResize(w(), h());
674 }
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000675}
676
677
Pierre Ossmanff473402012-07-04 11:27:47 +0000678void DesktopWindow::handleResizeTimeout(void *data)
679{
680 DesktopWindow *self = (DesktopWindow *)data;
681
682 assert(self);
683
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000684 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000685}
686
687
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000688void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000689{
Pierre Ossmanff473402012-07-04 11:27:47 +0000690 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000691 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000692
Pierre Ossmanaae38912012-07-13 11:22:55 +0000693#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000694 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000695#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000696 // In windowed mode (or the framebuffer is so large that we need
697 // to scroll) we just report a single virtual screen that covers
698 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000699
Pierre Ossmanaae38912012-07-13 11:22:55 +0000700 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000701
Pierre Ossmanaae38912012-07-13 11:22:55 +0000702 // Not sure why we have no screens, but adding a new one should be
703 // safe as there is nothing to conflict with...
704 if (layout.num_screens() == 0)
705 layout.add_screen(rfb::Screen());
706 else if (layout.num_screens() != 1) {
707 // More than one screen. Remove all but the first (which we
708 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000709
Pierre Ossmanaae38912012-07-13 11:22:55 +0000710 while (true) {
711 iter = layout.begin();
712 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000713
Pierre Ossmanaae38912012-07-13 11:22:55 +0000714 if (iter == layout.end())
715 break;
716
717 layout.remove_screen(iter->id);
718 }
719 }
720
721 // Resize the remaining single screen to the complete framebuffer
722 layout.begin()->dimensions.tl.x = 0;
723 layout.begin()->dimensions.tl.y = 0;
724 layout.begin()->dimensions.br.x = width;
725 layout.begin()->dimensions.br.y = height;
726#ifdef HAVE_FLTK_FULLSCREEN
727 } else {
728 int i;
729 rdr::U32 id;
730 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000731 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000732
733 // In full screen we report all screens that are fully covered.
734
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000735 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
736 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000737
Pierre Ossmanaae38912012-07-13 11:22:55 +0000738 // If we can find a matching screen in the existing set, we use
739 // that, otherwise we create a brand new screen.
740 //
741 // FIXME: We should really track screens better so we can handle
742 // a resized one.
743 //
744 for (i = 0;i < Fl::screen_count();i++) {
745 Fl::screen_xywh(sx, sy, sw, sh, i);
746
Pierre Ossman93d2d922012-07-20 12:32:52 +0000747 // Check that the screen is fully inside the framebuffer
748 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000749 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000750 continue;
751
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000752 // Adjust the coordinates so they are relative to our viewport
753 sx -= viewport_rect.tl.x;
754 sy -= viewport_rect.tl.y;
755
Pierre Ossmanaae38912012-07-13 11:22:55 +0000756 // Look for perfectly matching existing screen...
757 for (iter = cc->cp.screenLayout.begin();
758 iter != cc->cp.screenLayout.end(); ++iter) {
759 if ((iter->dimensions.tl.x == sx) &&
760 (iter->dimensions.tl.y == sy) &&
761 (iter->dimensions.width() == sw) &&
762 (iter->dimensions.height() == sh))
763 break;
764 }
765
766 // Found it?
767 if (iter != cc->cp.screenLayout.end()) {
768 layout.add_screen(*iter);
769 continue;
770 }
771
772 // Need to add a new one, which means we need to find an unused id
773 while (true) {
774 id = rand();
775 for (iter = cc->cp.screenLayout.begin();
776 iter != cc->cp.screenLayout.end(); ++iter) {
777 if (iter->id == id)
778 break;
779 }
780
781 if (iter == cc->cp.screenLayout.end())
782 break;
783 }
784
785 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000786 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000787
788 // If the viewport doesn't match a physical screen, then we might
789 // end up with no screens in the layout. Add a fake one...
790 if (layout.num_screens() == 0)
791 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000792 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000793#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000794
795 // Do we actually change anything?
796 if ((width == cc->cp.width) &&
797 (height == cc->cp.height) &&
798 (layout == cc->cp.screenLayout))
799 return;
800
Pierre Ossman9018af42015-01-26 15:15:47 +0100801 char buffer[2048];
802 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
803 cc->cp.width, cc->cp.height, width, height);
804 layout.print(buffer, sizeof(buffer));
805 vlog.debug("%s", buffer);
Pierre Ossmanaae38912012-07-13 11:22:55 +0000806
807 if (!layout.validate(width, height)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200808 vlog.error(_("Invalid screen layout computed for resize request!"));
Pierre Ossmanaae38912012-07-13 11:22:55 +0000809 return;
810 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000811
812 cc->writer()->writeSetDesktopSize(width, height, layout);
813}
814
815
Pierre Ossman6455d852011-06-01 09:33:00 +0000816void DesktopWindow::repositionViewport()
817{
818 int new_x, new_y;
819
820 // Deal with some scrolling corner cases:
821 //
822 // a) If the window is larger then the viewport, center the viewport.
823 // b) If the window is smaller than the viewport, make sure there is
824 // no wasted space on the sides.
825 //
826 // FIXME: Doesn't compensate for scroll widget size properly.
827
828 new_x = viewport->x();
829 new_y = viewport->y();
830
831 if (w() > viewport->w())
832 new_x = (w() - viewport->w()) / 2;
833 else {
834 if (viewport->x() > 0)
835 new_x = 0;
836 else if (w() > (viewport->x() + viewport->w()))
837 new_x = w() - viewport->w();
838 }
839
840 // Same thing for y axis
841 if (h() > viewport->h())
842 new_y = (h() - viewport->h()) / 2;
843 else {
844 if (viewport->y() > 0)
845 new_y = 0;
846 else if (h() > (viewport->y() + viewport->h()))
847 new_y = h() - viewport->h();
848 }
849
850 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
851 viewport->position(new_x, new_y);
852
853 // The scroll widget does not notice when you move around child widgets,
854 // so redraw everything to make sure things update.
855 redraw();
856 }
857}
858
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000859void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
860{
861 exit_vncviewer();
862}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000863
864
865void DesktopWindow::handleOptions(void *data)
866{
867 DesktopWindow *self = (DesktopWindow*)data;
868
869#ifdef HAVE_FLTK_FULLSCREEN
870 if (self->fullscreen_active() && fullscreenSystemKeys)
871 self->grabKeyboard();
872 else
873 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000874
Pierre Ossmanff473402012-07-04 11:27:47 +0000875 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000876 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000877 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000878 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000879#endif
880}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000881
882void DesktopWindow::handleFullscreenTimeout(void *data)
883{
884 DesktopWindow *self = (DesktopWindow *)data;
885
886 assert(self);
887
888 self->delayedFullscreen = false;
889
890 if (self->delayedDesktopSize) {
891 self->handleDesktopSize();
892 self->delayedDesktopSize = false;
893 }
894}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000895
896void DesktopWindow::handleEdgeScroll(void *data)
897{
898#ifdef HAVE_FLTK_FULLSCREEN
899 DesktopWindow *self = (DesktopWindow *)data;
900
901 int mx, my;
902 int dx, dy;
903
904 assert(self);
905
906 if (!self->fullscreen_active())
907 return;
908
909 mx = Fl::event_x();
910 my = Fl::event_y();
911
912 dx = dy = 0;
913
914 // Clamp mouse position in case it is outside the window
915 if (mx < 0)
916 mx = 0;
917 if (mx > self->w())
918 mx = self->w();
919 if (my < 0)
920 my = 0;
921 if (my > self->h())
922 my = self->h();
923
924 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000925 dx = EDGE_SCROLL_SPEED -
926 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
927 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
928 (mx > self->w() - EDGE_SCROLL_SIZE))
929 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
930 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000931 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000932 dy = EDGE_SCROLL_SPEED -
933 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
934 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
935 (my > self->h() - EDGE_SCROLL_SIZE))
936 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
937 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000938
939 if ((dx == 0) && (dy == 0))
940 return;
941
942 // Make sure we don't move the viewport too much
943 if (self->viewport->x() + dx > 0)
944 dx = -self->viewport->x();
945 if (self->viewport->x() + dx + self->viewport->w() < self->w())
946 dx = self->w() - (self->viewport->x() + self->viewport->w());
947 if (self->viewport->y() + dy > 0)
948 dy = -self->viewport->y();
949 if (self->viewport->y() + dy + self->viewport->h() < self->h())
950 dy = self->h() - (self->viewport->y() + self->viewport->h());
951
952 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
953
954 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
955#endif
956}