blob: d30dfc2f438a6613e49e9d166e75d9611c63bd4e [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) {
115 int dummy;
116 Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
117 }
118#endif
119
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000120 if (force_position()) {
121 resize(geom_x, geom_y, w, h);
122 } else {
123 size(w, h);
124 }
125
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000126 if (fullScreen) {
127 // Hack: Window managers seem to be rather crappy at respecting
128 // fullscreen hints on initial windows. So on X11 we'll have to
129 // wait until after we've been mapped.
130#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000131 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000132#else
133 delayedFullscreen = true;
134#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000135 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000136
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000137 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000138
Pierre Ossmancb68c192012-10-17 07:59:20 +0000139 // Full screen events are not sent out for a hidden window,
140 // so send a fake one here to set up things properly.
Pierre Ossmancb68c192012-10-17 07:59:20 +0000141 if (fullscreen_active())
142 handle(FL_FULLSCREEN);
Pierre Ossmancb68c192012-10-17 07:59:20 +0000143
Peter Åstrand49b11572012-08-01 08:09:09 +0000144 // Unfortunately, current FLTK does not allow us to set the
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000145 // maximized property on Windows and X11 before showing the window.
146 // See STR #2083 and STR #2178
147#ifndef __APPLE__
Peter Åstrand49b11572012-08-01 08:09:09 +0000148 if (maximize) {
149 maximizeWindow();
150 }
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000151#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000152
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000153 // The window manager might give us an initial window size that is different
154 // than the one we requested, and in those cases we need to manually adjust
155 // the scroll widget for things to behave sanely.
156 if ((w != this->w()) || (h != this->h()))
157 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000158
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000159 if (delayedFullscreen) {
160 // Hack: Fullscreen requests may be ignored, so we need a timeout for
161 // when we should stop waiting. We also really need to wait for the
162 // resize, which can come after the fullscreen event.
163 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
164 fullscreen_on();
165 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000166}
167
168
169DesktopWindow::~DesktopWindow()
170{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000171 // Unregister all timeouts in case they get a change tro trigger
172 // again later when this object is already gone.
173 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000174 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000175 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000176 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000177
178 OptionsDialog::removeCallback(handleOptions);
179
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000180 // FLTK automatically deletes all child widgets, so we shouldn't touch
181 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000182}
183
184
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000185const rfb::PixelFormat &DesktopWindow::getPreferredPF()
186{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000187 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000188}
189
190
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000191void DesktopWindow::setName(const char *name)
192{
193 CharArray windowNameStr;
194 windowNameStr.replaceBuf(new char[256]);
195
Pierre Ossman27820ba2011-09-30 12:54:24 +0000196 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000197
198 copy_label(windowNameStr.buf);
199}
200
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000201
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200202rfb::ModifiablePixelBuffer* DesktopWindow::getFramebuffer(void)
203{
204 return viewport->getFramebuffer();
Pierre Ossman947b48d2014-01-27 16:52:35 +0100205}
206
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000207
208// Copy the areas of the framebuffer that have been changed (damaged)
209// to the displayed window.
210
211void DesktopWindow::updateWindow()
212{
Pierre Ossmanff473402012-07-04 11:27:47 +0000213 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000214 if (cc->cp.supportsSetDesktopSize) {
215 // Hack: Wait until we're in the proper mode and position until
216 // resizing things, otherwise we might send the wrong thing.
217 if (delayedFullscreen)
218 delayedDesktopSize = true;
219 else
220 handleDesktopSize();
221 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000222 firstUpdate = false;
223 }
224
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000225 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000226}
227
228
Pierre Ossman6455d852011-06-01 09:33:00 +0000229void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
230{
231 if ((new_w == viewport->w()) && (new_h == viewport->h()))
232 return;
233
Pierre Ossman6455d852011-06-01 09:33:00 +0000234 // If we're letting the viewport match the window perfectly, then
235 // keep things that way for the new size, otherwise just keep things
236 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000237 if (!fullscreen_active()) {
Pierre Ossman29e4a162011-11-21 14:03:31 +0000238 if ((w() == viewport->w()) && (h() == viewport->h()))
239 size(new_w, new_h);
240 else {
241 // Make sure the window isn't too big. We do this manually because
242 // we have to disable the window size restriction (and it isn't
243 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000244 if ((w() > new_w) || (h() > new_h))
245 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000246 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000247 }
Pierre Ossman6455d852011-06-01 09:33:00 +0000248
249 viewport->size(new_w, new_h);
250
251 // We might not resize the main window, so we need to manually call this
252 // to make sure the viewport is centered.
253 repositionViewport();
254
Pierre Ossman6455d852011-06-01 09:33:00 +0000255 // repositionViewport() makes sure the scroll widget notices any changes
256 // in position, but it might be just the size that changes so we also
257 // need a poke here as well.
258 redraw();
259}
260
261
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000262void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
263 void* data, void* mask)
264{
265 viewport->setCursor(width, height, hotspot, data, mask);
266}
267
268
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000269void DesktopWindow::resize(int x, int y, int w, int h)
270{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000271 bool resizing;
272
Pierre Ossman0e593462012-09-03 09:43:23 +0000273#if ! (defined(WIN32) || defined(__APPLE__))
274 // X11 window managers will treat a resize to cover the entire
275 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman56610fb2015-01-27 16:28:15 +0100276 if (!fullscreen_active()) {
Pierre Ossman1f884e02012-10-30 10:26:23 +0000277 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000278
Pierre Ossman1f884e02012-10-30 10:26:23 +0000279 // If there is no X11 window, then this must be a resize request,
280 // not a notification from the X server.
281 if (!shown())
282 resize_req = true;
283 else {
284 // Otherwise we need to get the real window coordinates to tell
285 // the difference
286 XWindowAttributes actual;
287 Window cr;
288 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000289
Pierre Ossman1f884e02012-10-30 10:26:23 +0000290 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
291 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
292 0, 0, &wx, &wy, &cr);
293
294 // Actual resize request?
295 if ((wx != x) || (wy != y) ||
296 (actual.width != w) || (actual.height != h))
297 resize_req = true;
298 else
299 resize_req = false;
300 }
301
302 if (resize_req) {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000303 for (int i = 0;i < Fl::screen_count();i++) {
304 int sx, sy, sw, sh;
305
306 Fl::screen_xywh(sx, sy, sw, sh, i);
307
308 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200309 vlog.info(_("Adjusting window size to avoid accidental full screen request"));
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000310 // Assume a panel of some form and adjust the height
311 y += 20;
312 h -= 40;
313 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000314 }
315 }
316 }
317#endif
318
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000319 if ((this->w() != w) || (this->h() != h))
320 resizing = true;
321 else
322 resizing = false;
323
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000324 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000325
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000326 if (resizing) {
327 // Try to get the remote size to match our window size, provided
328 // the following conditions are true:
329 //
330 // a) The user has this feature turned on
331 // b) The server supports it
332 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000333 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000334 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000335 if (not firstUpdate and not delayedFullscreen and
336 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000337 // We delay updating the remote desktop as we tend to get a flood
338 // of resize events as the user is dragging the window.
339 Fl::remove_timeout(handleResizeTimeout, this);
340 Fl::add_timeout(0.5, handleResizeTimeout, this);
341 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000342
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000343 // Deal with some scrolling corner cases
344 repositionViewport();
345 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000346}
347
348
Pierre Ossman407a5c32011-05-26 14:48:29 +0000349int DesktopWindow::handle(int event)
350{
351 switch (event) {
Pierre Ossman407a5c32011-05-26 14:48:29 +0000352 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000353 fullScreen.setParam(fullscreen_active());
354
Pierre Ossman1d867b62012-09-03 09:25:07 +0000355 if (fullscreen_active())
356 scroll->type(0);
357 else
358 scroll->type(Fl_Scroll::BOTH);
359
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000360 // The scroll widget isn't clever enough to actually redraw the
361 // scroll bars when they are added/removed, so we need to give
362 // it a push.
363 scroll->redraw();
364
Pierre Ossman407a5c32011-05-26 14:48:29 +0000365 if (!fullscreenSystemKeys)
366 break;
367
368 if (fullscreen_active())
369 grabKeyboard();
370 else
371 ungrabKeyboard();
372
373 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000374
375 case FL_ENTER:
376 case FL_LEAVE:
377 case FL_DRAG:
378 case FL_MOVE:
379 if (fullscreen_active()) {
380 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
381 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
382 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
383 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
384 if (!Fl::has_timeout(handleEdgeScroll, this))
385 Fl::add_timeout(0.1, handleEdgeScroll, this);
386 }
387 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000388 // Continue processing so that the viewport also gets mouse events
389 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000390
Pierre Ossman407a5c32011-05-26 14:48:29 +0000391 case FL_SHORTCUT:
392 // Sometimes the focus gets out of whack and we fall through to the
393 // shortcut dispatching. Try to make things sane again...
394 if (Fl::focus() == NULL) {
395 take_focus();
396 Fl::handle(FL_KEYDOWN, this);
397 }
398 return 1;
399 }
400
401 return Fl_Window::handle(event);
402}
403
404
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000405int DesktopWindow::fltkHandle(int event, Fl_Window *win)
406{
407 int ret;
408
409 ret = Fl::handle_(event, win);
410
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000411 // This is hackish and the result of the dodgy focus handling in FLTK.
412 // The basic problem is that FLTK's view of focus and the system's tend
413 // to differ, and as a result we do not see all the FL_FOCUS events we
414 // need. Fortunately we can grab them here...
415
416 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
417
418 if (dw && fullscreenSystemKeys) {
419 switch (event) {
420 case FL_FOCUS:
421 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
422 // some issues we need to work around:
423 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
424 // b) Gaining focus on the system level causes FLTK to switch
425 // window level on OS X.
426 if (dw->fullscreen_active())
427 dw->grabKeyboard();
428 break;
429
Pierre Ossmanb1369802012-10-17 07:59:36 +0000430 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000431 // FIXME: We need to relinquish control when the entire window loses
432 // focus as it is very tied to this specific window on some
433 // platforms and we want to be able to open subwindows.
434 dw->ungrabKeyboard();
435 break;
436 }
437 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000438
439 return ret;
440}
441
442
Pierre Ossmanaae38912012-07-13 11:22:55 +0000443void DesktopWindow::fullscreen_on()
444{
Pierre Ossmanaae38912012-07-13 11:22:55 +0000445 if (not fullScreenAllMonitors)
446 fullscreen_screens(-1, -1, -1, -1);
447 else {
448 int top, bottom, left, right;
449 int top_y, bottom_y, left_x, right_x;
450
451 int sx, sy, sw, sh;
452
453 top = bottom = left = right = 0;
454
455 Fl::screen_xywh(sx, sy, sw, sh, 0);
456 top_y = sy;
457 bottom_y = sy + sh;
458 left_x = sx;
459 right_x = sx + sw;
460
461 for (int i = 1;i < Fl::screen_count();i++) {
462 Fl::screen_xywh(sx, sy, sw, sh, i);
463 if (sy < top_y) {
464 top = i;
465 top_y = sy;
466 }
467 if ((sy + sh) > bottom_y) {
468 bottom = i;
469 bottom_y = sy + sh;
470 }
471 if (sx < left_x) {
472 left = i;
473 left_x = sx;
474 }
475 if ((sx + sw) > right_x) {
476 right = i;
477 right_x = sx + sw;
478 }
479 }
480
481 fullscreen_screens(top, bottom, left, right);
482 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000483
484 fullscreen();
Pierre Ossmanaae38912012-07-13 11:22:55 +0000485}
486
Pierre Ossman407a5c32011-05-26 14:48:29 +0000487void DesktopWindow::grabKeyboard()
488{
489 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
490 // correct widget regardless of which low level window got the system
491 // event.
492
493 // FIXME: Push this stuff into FLTK.
494
495#if defined(WIN32)
496 int ret;
497
498 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
499 if (ret != 0)
500 vlog.error(_("Failure grabbing keyboard"));
501#elif defined(__APPLE__)
502 int ret;
503
Pierre Ossman56610fb2015-01-27 16:28:15 +0100504 ret = cocoa_capture_display(this, fullScreenAllMonitors);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000505 if (ret != 0)
506 vlog.error(_("Failure grabbing keyboard"));
507#else
508 int ret;
509
510 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000511 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000512 if (ret) {
513 if (ret == AlreadyGrabbed) {
514 // It seems like we can race with the WM in some cases.
515 // Try again in a bit.
516 if (!Fl::has_timeout(handleGrab, this))
517 Fl::add_timeout(0.500, handleGrab, this);
518 } else {
519 vlog.error(_("Failure grabbing keyboard"));
520 }
521 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000522
523 // We also need to grab the pointer as some WMs like to grab buttons
524 // combined with modifies (e.g. Alt+Button0 in metacity).
525 ret = XGrabPointer(fl_display, fl_xid(this), True,
526 ButtonPressMask|ButtonReleaseMask|
527 ButtonMotionMask|PointerMotionMask,
528 GrabModeAsync, GrabModeAsync,
529 None, None, CurrentTime);
530 if (ret)
531 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000532#endif
533}
534
535
536void DesktopWindow::ungrabKeyboard()
537{
538 Fl::remove_timeout(handleGrab, this);
539
540#if defined(WIN32)
541 win32_disable_lowlevel_keyboard(fl_xid(this));
542#elif defined(__APPLE__)
543 cocoa_release_display(this);
544#else
545 // FLTK has a grab so lets not mess with it
546 if (Fl::grab())
547 return;
548
Pierre Ossman53fd5442011-11-17 10:19:19 +0000549 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000550 XUngrabKeyboard(fl_display, fl_event_time);
551#endif
552}
553
554
555void DesktopWindow::handleGrab(void *data)
556{
557 DesktopWindow *self = (DesktopWindow*)data;
558
559 assert(self);
560
Pierre Ossman407a5c32011-05-26 14:48:29 +0000561 if (!fullscreenSystemKeys)
562 return;
563 if (!self->fullscreen_active())
564 return;
565
566 self->grabKeyboard();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000567}
568
569
Peter Åstrand49b11572012-08-01 08:09:09 +0000570#define _NET_WM_STATE_ADD 1 /* add/set property */
571void DesktopWindow::maximizeWindow()
572{
573#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000574 // We cannot use ShowWindow() in full screen mode as it will
575 // resize things implicitly. Fortunately modifying the style
576 // directly results in a maximized state once we leave full screen.
Pierre Ossman897067b2012-10-11 09:17:19 +0000577 if (fullscreen_active()) {
578 WINDOWINFO wi;
579 wi.cbSize = sizeof(WINDOWINFO);
580 GetWindowInfo(fl_xid(this), &wi);
581 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
582 } else
Pierre Ossman897067b2012-10-11 09:17:19 +0000583 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000584#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000585 // OS X is somewhat strange and does not really have a concept of a
586 // maximized window, so we can simply resize the window to the workarea.
587 // Note that we shouldn't do this whilst in full screen as that will
588 // incorrectly adjust things.
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000589 if (fullscreen_active())
590 return;
Peter Åstrand49b11572012-08-01 08:09:09 +0000591 int X, Y, W, H;
592 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
593 size(W, H);
594#else
595 // X11
596 fl_open_display();
597 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
598 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
599 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
600
601 XEvent e;
602 e.xany.type = ClientMessage;
603 e.xany.window = fl_xid(this);
604 e.xclient.message_type = net_wm_state;
605 e.xclient.format = 32;
606 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
607 e.xclient.data.l[1] = net_wm_state_maximized_vert;
608 e.xclient.data.l[2] = net_wm_state_maximized_horz;
609 e.xclient.data.l[3] = 0;
610 e.xclient.data.l[4] = 0;
611 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
612#endif
613}
614
615
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000616void DesktopWindow::handleDesktopSize()
617{
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100618 if (desktopSize.hasBeenSet()) {
619 int width, height;
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000620
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100621 // An explicit size has been requested
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000622
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100623 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
624 return;
625
626 remoteResize(width, height);
627 } else if (::remoteResize) {
628 // No explicit size, but remote resizing is on so make sure it
629 // matches whatever size the window ended up being
630 remoteResize(w(), h());
631 }
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000632}
633
634
Pierre Ossmanff473402012-07-04 11:27:47 +0000635void DesktopWindow::handleResizeTimeout(void *data)
636{
637 DesktopWindow *self = (DesktopWindow *)data;
638
639 assert(self);
640
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000641 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000642}
643
644
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000645void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000646{
Pierre Ossmanff473402012-07-04 11:27:47 +0000647 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000648 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000649
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000650 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000651 // In windowed mode (or the framebuffer is so large that we need
652 // to scroll) we just report a single virtual screen that covers
653 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000654
Pierre Ossmanaae38912012-07-13 11:22:55 +0000655 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000656
Pierre Ossmanaae38912012-07-13 11:22:55 +0000657 // Not sure why we have no screens, but adding a new one should be
658 // safe as there is nothing to conflict with...
659 if (layout.num_screens() == 0)
660 layout.add_screen(rfb::Screen());
661 else if (layout.num_screens() != 1) {
662 // More than one screen. Remove all but the first (which we
663 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000664
Pierre Ossmanaae38912012-07-13 11:22:55 +0000665 while (true) {
666 iter = layout.begin();
667 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000668
Pierre Ossmanaae38912012-07-13 11:22:55 +0000669 if (iter == layout.end())
670 break;
671
672 layout.remove_screen(iter->id);
673 }
674 }
675
676 // Resize the remaining single screen to the complete framebuffer
677 layout.begin()->dimensions.tl.x = 0;
678 layout.begin()->dimensions.tl.y = 0;
679 layout.begin()->dimensions.br.x = width;
680 layout.begin()->dimensions.br.y = height;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000681 } else {
682 int i;
683 rdr::U32 id;
684 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000685 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000686
687 // In full screen we report all screens that are fully covered.
688
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000689 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
690 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000691
Pierre Ossmanaae38912012-07-13 11:22:55 +0000692 // If we can find a matching screen in the existing set, we use
693 // that, otherwise we create a brand new screen.
694 //
695 // FIXME: We should really track screens better so we can handle
696 // a resized one.
697 //
698 for (i = 0;i < Fl::screen_count();i++) {
699 Fl::screen_xywh(sx, sy, sw, sh, i);
700
Pierre Ossman93d2d922012-07-20 12:32:52 +0000701 // Check that the screen is fully inside the framebuffer
702 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000703 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000704 continue;
705
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000706 // Adjust the coordinates so they are relative to our viewport
707 sx -= viewport_rect.tl.x;
708 sy -= viewport_rect.tl.y;
709
Pierre Ossmanaae38912012-07-13 11:22:55 +0000710 // Look for perfectly matching existing screen...
711 for (iter = cc->cp.screenLayout.begin();
712 iter != cc->cp.screenLayout.end(); ++iter) {
713 if ((iter->dimensions.tl.x == sx) &&
714 (iter->dimensions.tl.y == sy) &&
715 (iter->dimensions.width() == sw) &&
716 (iter->dimensions.height() == sh))
717 break;
718 }
719
720 // Found it?
721 if (iter != cc->cp.screenLayout.end()) {
722 layout.add_screen(*iter);
723 continue;
724 }
725
726 // Need to add a new one, which means we need to find an unused id
727 while (true) {
728 id = rand();
729 for (iter = cc->cp.screenLayout.begin();
730 iter != cc->cp.screenLayout.end(); ++iter) {
731 if (iter->id == id)
732 break;
733 }
734
735 if (iter == cc->cp.screenLayout.end())
736 break;
737 }
738
739 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000740 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000741
742 // If the viewport doesn't match a physical screen, then we might
743 // end up with no screens in the layout. Add a fake one...
744 if (layout.num_screens() == 0)
745 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000746 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000747
748 // Do we actually change anything?
749 if ((width == cc->cp.width) &&
750 (height == cc->cp.height) &&
751 (layout == cc->cp.screenLayout))
752 return;
753
Pierre Ossman9018af42015-01-26 15:15:47 +0100754 char buffer[2048];
755 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
756 cc->cp.width, cc->cp.height, width, height);
757 layout.print(buffer, sizeof(buffer));
758 vlog.debug("%s", buffer);
Pierre Ossmanaae38912012-07-13 11:22:55 +0000759
760 if (!layout.validate(width, height)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200761 vlog.error(_("Invalid screen layout computed for resize request!"));
Pierre Ossmanaae38912012-07-13 11:22:55 +0000762 return;
763 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000764
765 cc->writer()->writeSetDesktopSize(width, height, layout);
766}
767
768
Pierre Ossman6455d852011-06-01 09:33:00 +0000769void DesktopWindow::repositionViewport()
770{
771 int new_x, new_y;
772
773 // Deal with some scrolling corner cases:
774 //
775 // a) If the window is larger then the viewport, center the viewport.
776 // b) If the window is smaller than the viewport, make sure there is
777 // no wasted space on the sides.
778 //
779 // FIXME: Doesn't compensate for scroll widget size properly.
780
781 new_x = viewport->x();
782 new_y = viewport->y();
783
784 if (w() > viewport->w())
785 new_x = (w() - viewport->w()) / 2;
786 else {
787 if (viewport->x() > 0)
788 new_x = 0;
789 else if (w() > (viewport->x() + viewport->w()))
790 new_x = w() - viewport->w();
791 }
792
793 // Same thing for y axis
794 if (h() > viewport->h())
795 new_y = (h() - viewport->h()) / 2;
796 else {
797 if (viewport->y() > 0)
798 new_y = 0;
799 else if (h() > (viewport->y() + viewport->h()))
800 new_y = h() - viewport->h();
801 }
802
803 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
804 viewport->position(new_x, new_y);
805
806 // The scroll widget does not notice when you move around child widgets,
807 // so redraw everything to make sure things update.
808 redraw();
809 }
810}
811
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000812void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
813{
814 exit_vncviewer();
815}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000816
817
818void DesktopWindow::handleOptions(void *data)
819{
820 DesktopWindow *self = (DesktopWindow*)data;
821
Pierre Ossman407a5c32011-05-26 14:48:29 +0000822 if (self->fullscreen_active() && fullscreenSystemKeys)
823 self->grabKeyboard();
824 else
825 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000826
Pierre Ossmanff473402012-07-04 11:27:47 +0000827 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000828 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000829 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000830 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000831}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000832
833void DesktopWindow::handleFullscreenTimeout(void *data)
834{
835 DesktopWindow *self = (DesktopWindow *)data;
836
837 assert(self);
838
839 self->delayedFullscreen = false;
840
841 if (self->delayedDesktopSize) {
842 self->handleDesktopSize();
843 self->delayedDesktopSize = false;
844 }
845}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000846
847void DesktopWindow::handleEdgeScroll(void *data)
848{
Pierre Ossman1d867b62012-09-03 09:25:07 +0000849 DesktopWindow *self = (DesktopWindow *)data;
850
851 int mx, my;
852 int dx, dy;
853
854 assert(self);
855
856 if (!self->fullscreen_active())
857 return;
858
859 mx = Fl::event_x();
860 my = Fl::event_y();
861
862 dx = dy = 0;
863
864 // Clamp mouse position in case it is outside the window
865 if (mx < 0)
866 mx = 0;
867 if (mx > self->w())
868 mx = self->w();
869 if (my < 0)
870 my = 0;
871 if (my > self->h())
872 my = self->h();
873
874 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000875 dx = EDGE_SCROLL_SPEED -
876 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
877 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
878 (mx > self->w() - EDGE_SCROLL_SIZE))
879 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
880 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000881 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000882 dy = EDGE_SCROLL_SPEED -
883 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
884 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
885 (my > self->h() - EDGE_SCROLL_SIZE))
886 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
887 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000888
889 if ((dx == 0) && (dy == 0))
890 return;
891
892 // Make sure we don't move the viewport too much
893 if (self->viewport->x() + dx > 0)
894 dx = -self->viewport->x();
895 if (self->viewport->x() + dx + self->viewport->w() < self->w())
896 dx = self->w() - (self->viewport->x() + self->viewport->w());
897 if (self->viewport->y() + dy > 0)
898 dy = -self->viewport->y();
899 if (self->viewport->y() + dy + self->viewport->h() < self->h())
900 dy = self->h() - (self->viewport->y() + self->viewport->h());
901
902 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
903
904 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000905}