blob: eb62edd6284bd6f55abc2c72faf447d9ab2c2acd [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 Ossman407a5c32011-05-26 14:48:29 +0000390 }
391
392 return Fl_Window::handle(event);
393}
394
395
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000396int DesktopWindow::fltkHandle(int event, Fl_Window *win)
397{
398 int ret;
399
400 ret = Fl::handle_(event, win);
401
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000402 // This is hackish and the result of the dodgy focus handling in FLTK.
403 // The basic problem is that FLTK's view of focus and the system's tend
404 // to differ, and as a result we do not see all the FL_FOCUS events we
405 // need. Fortunately we can grab them here...
406
407 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
408
409 if (dw && fullscreenSystemKeys) {
410 switch (event) {
411 case FL_FOCUS:
412 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
413 // some issues we need to work around:
414 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
415 // b) Gaining focus on the system level causes FLTK to switch
416 // window level on OS X.
417 if (dw->fullscreen_active())
418 dw->grabKeyboard();
419 break;
420
Pierre Ossmanb1369802012-10-17 07:59:36 +0000421 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000422 // FIXME: We need to relinquish control when the entire window loses
423 // focus as it is very tied to this specific window on some
424 // platforms and we want to be able to open subwindows.
425 dw->ungrabKeyboard();
426 break;
427 }
428 }
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000429
430 return ret;
431}
432
433
Pierre Ossmanaae38912012-07-13 11:22:55 +0000434void DesktopWindow::fullscreen_on()
435{
Pierre Ossmanaae38912012-07-13 11:22:55 +0000436 if (not fullScreenAllMonitors)
437 fullscreen_screens(-1, -1, -1, -1);
438 else {
439 int top, bottom, left, right;
440 int top_y, bottom_y, left_x, right_x;
441
442 int sx, sy, sw, sh;
443
444 top = bottom = left = right = 0;
445
446 Fl::screen_xywh(sx, sy, sw, sh, 0);
447 top_y = sy;
448 bottom_y = sy + sh;
449 left_x = sx;
450 right_x = sx + sw;
451
452 for (int i = 1;i < Fl::screen_count();i++) {
453 Fl::screen_xywh(sx, sy, sw, sh, i);
454 if (sy < top_y) {
455 top = i;
456 top_y = sy;
457 }
458 if ((sy + sh) > bottom_y) {
459 bottom = i;
460 bottom_y = sy + sh;
461 }
462 if (sx < left_x) {
463 left = i;
464 left_x = sx;
465 }
466 if ((sx + sw) > right_x) {
467 right = i;
468 right_x = sx + sw;
469 }
470 }
471
472 fullscreen_screens(top, bottom, left, right);
473 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000474
475 fullscreen();
Pierre Ossmanaae38912012-07-13 11:22:55 +0000476}
477
Pierre Ossman407a5c32011-05-26 14:48:29 +0000478void DesktopWindow::grabKeyboard()
479{
480 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
481 // correct widget regardless of which low level window got the system
482 // event.
483
484 // FIXME: Push this stuff into FLTK.
485
486#if defined(WIN32)
487 int ret;
488
489 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
490 if (ret != 0)
491 vlog.error(_("Failure grabbing keyboard"));
492#elif defined(__APPLE__)
493 int ret;
494
Pierre Ossman56610fb2015-01-27 16:28:15 +0100495 ret = cocoa_capture_display(this, fullScreenAllMonitors);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000496 if (ret != 0)
497 vlog.error(_("Failure grabbing keyboard"));
498#else
499 int ret;
500
501 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000502 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000503 if (ret) {
504 if (ret == AlreadyGrabbed) {
505 // It seems like we can race with the WM in some cases.
506 // Try again in a bit.
507 if (!Fl::has_timeout(handleGrab, this))
508 Fl::add_timeout(0.500, handleGrab, this);
509 } else {
510 vlog.error(_("Failure grabbing keyboard"));
511 }
512 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000513
514 // We also need to grab the pointer as some WMs like to grab buttons
515 // combined with modifies (e.g. Alt+Button0 in metacity).
516 ret = XGrabPointer(fl_display, fl_xid(this), True,
517 ButtonPressMask|ButtonReleaseMask|
518 ButtonMotionMask|PointerMotionMask,
519 GrabModeAsync, GrabModeAsync,
520 None, None, CurrentTime);
521 if (ret)
522 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000523#endif
524}
525
526
527void DesktopWindow::ungrabKeyboard()
528{
529 Fl::remove_timeout(handleGrab, this);
530
531#if defined(WIN32)
532 win32_disable_lowlevel_keyboard(fl_xid(this));
533#elif defined(__APPLE__)
534 cocoa_release_display(this);
535#else
536 // FLTK has a grab so lets not mess with it
537 if (Fl::grab())
538 return;
539
Pierre Ossman53fd5442011-11-17 10:19:19 +0000540 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000541 XUngrabKeyboard(fl_display, fl_event_time);
542#endif
543}
544
545
546void DesktopWindow::handleGrab(void *data)
547{
548 DesktopWindow *self = (DesktopWindow*)data;
549
550 assert(self);
551
Pierre Ossman407a5c32011-05-26 14:48:29 +0000552 if (!fullscreenSystemKeys)
553 return;
554 if (!self->fullscreen_active())
555 return;
556
557 self->grabKeyboard();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000558}
559
560
Peter Åstrand49b11572012-08-01 08:09:09 +0000561#define _NET_WM_STATE_ADD 1 /* add/set property */
562void DesktopWindow::maximizeWindow()
563{
564#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000565 // We cannot use ShowWindow() in full screen mode as it will
566 // resize things implicitly. Fortunately modifying the style
567 // directly results in a maximized state once we leave full screen.
Pierre Ossman897067b2012-10-11 09:17:19 +0000568 if (fullscreen_active()) {
569 WINDOWINFO wi;
570 wi.cbSize = sizeof(WINDOWINFO);
571 GetWindowInfo(fl_xid(this), &wi);
572 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
573 } else
Pierre Ossman897067b2012-10-11 09:17:19 +0000574 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000575#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000576 // OS X is somewhat strange and does not really have a concept of a
577 // maximized window, so we can simply resize the window to the workarea.
578 // Note that we shouldn't do this whilst in full screen as that will
579 // incorrectly adjust things.
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000580 if (fullscreen_active())
581 return;
Peter Åstrand49b11572012-08-01 08:09:09 +0000582 int X, Y, W, H;
583 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
584 size(W, H);
585#else
586 // X11
587 fl_open_display();
588 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
589 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
590 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
591
592 XEvent e;
593 e.xany.type = ClientMessage;
594 e.xany.window = fl_xid(this);
595 e.xclient.message_type = net_wm_state;
596 e.xclient.format = 32;
597 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
598 e.xclient.data.l[1] = net_wm_state_maximized_vert;
599 e.xclient.data.l[2] = net_wm_state_maximized_horz;
600 e.xclient.data.l[3] = 0;
601 e.xclient.data.l[4] = 0;
602 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
603#endif
604}
605
606
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000607void DesktopWindow::handleDesktopSize()
608{
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100609 if (desktopSize.hasBeenSet()) {
610 int width, height;
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000611
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100612 // An explicit size has been requested
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000613
Pierre Ossmanbad31c22014-11-11 13:59:01 +0100614 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
615 return;
616
617 remoteResize(width, height);
618 } else if (::remoteResize) {
619 // No explicit size, but remote resizing is on so make sure it
620 // matches whatever size the window ended up being
621 remoteResize(w(), h());
622 }
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000623}
624
625
Pierre Ossmanff473402012-07-04 11:27:47 +0000626void DesktopWindow::handleResizeTimeout(void *data)
627{
628 DesktopWindow *self = (DesktopWindow *)data;
629
630 assert(self);
631
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000632 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000633}
634
635
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000636void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000637{
Pierre Ossmanff473402012-07-04 11:27:47 +0000638 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000639 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000640
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000641 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000642 // In windowed mode (or the framebuffer is so large that we need
643 // to scroll) we just report a single virtual screen that covers
644 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000645
Pierre Ossmanaae38912012-07-13 11:22:55 +0000646 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000647
Pierre Ossmanaae38912012-07-13 11:22:55 +0000648 // Not sure why we have no screens, but adding a new one should be
649 // safe as there is nothing to conflict with...
650 if (layout.num_screens() == 0)
651 layout.add_screen(rfb::Screen());
652 else if (layout.num_screens() != 1) {
653 // More than one screen. Remove all but the first (which we
654 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000655
Pierre Ossmanaae38912012-07-13 11:22:55 +0000656 while (true) {
657 iter = layout.begin();
658 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000659
Pierre Ossmanaae38912012-07-13 11:22:55 +0000660 if (iter == layout.end())
661 break;
662
663 layout.remove_screen(iter->id);
664 }
665 }
666
667 // Resize the remaining single screen to the complete framebuffer
668 layout.begin()->dimensions.tl.x = 0;
669 layout.begin()->dimensions.tl.y = 0;
670 layout.begin()->dimensions.br.x = width;
671 layout.begin()->dimensions.br.y = height;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000672 } else {
673 int i;
674 rdr::U32 id;
675 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000676 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000677
678 // In full screen we report all screens that are fully covered.
679
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000680 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
681 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000682
Pierre Ossmanaae38912012-07-13 11:22:55 +0000683 // If we can find a matching screen in the existing set, we use
684 // that, otherwise we create a brand new screen.
685 //
686 // FIXME: We should really track screens better so we can handle
687 // a resized one.
688 //
689 for (i = 0;i < Fl::screen_count();i++) {
690 Fl::screen_xywh(sx, sy, sw, sh, i);
691
Pierre Ossman93d2d922012-07-20 12:32:52 +0000692 // Check that the screen is fully inside the framebuffer
693 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000694 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000695 continue;
696
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000697 // Adjust the coordinates so they are relative to our viewport
698 sx -= viewport_rect.tl.x;
699 sy -= viewport_rect.tl.y;
700
Pierre Ossmanaae38912012-07-13 11:22:55 +0000701 // Look for perfectly matching existing screen...
702 for (iter = cc->cp.screenLayout.begin();
703 iter != cc->cp.screenLayout.end(); ++iter) {
704 if ((iter->dimensions.tl.x == sx) &&
705 (iter->dimensions.tl.y == sy) &&
706 (iter->dimensions.width() == sw) &&
707 (iter->dimensions.height() == sh))
708 break;
709 }
710
711 // Found it?
712 if (iter != cc->cp.screenLayout.end()) {
713 layout.add_screen(*iter);
714 continue;
715 }
716
717 // Need to add a new one, which means we need to find an unused id
718 while (true) {
719 id = rand();
720 for (iter = cc->cp.screenLayout.begin();
721 iter != cc->cp.screenLayout.end(); ++iter) {
722 if (iter->id == id)
723 break;
724 }
725
726 if (iter == cc->cp.screenLayout.end())
727 break;
728 }
729
730 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000731 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000732
733 // If the viewport doesn't match a physical screen, then we might
734 // end up with no screens in the layout. Add a fake one...
735 if (layout.num_screens() == 0)
736 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000737 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000738
739 // Do we actually change anything?
740 if ((width == cc->cp.width) &&
741 (height == cc->cp.height) &&
742 (layout == cc->cp.screenLayout))
743 return;
744
Pierre Ossman9018af42015-01-26 15:15:47 +0100745 char buffer[2048];
746 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
747 cc->cp.width, cc->cp.height, width, height);
748 layout.print(buffer, sizeof(buffer));
749 vlog.debug("%s", buffer);
Pierre Ossmanaae38912012-07-13 11:22:55 +0000750
751 if (!layout.validate(width, height)) {
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200752 vlog.error(_("Invalid screen layout computed for resize request!"));
Pierre Ossmanaae38912012-07-13 11:22:55 +0000753 return;
754 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000755
756 cc->writer()->writeSetDesktopSize(width, height, layout);
757}
758
759
Pierre Ossman6455d852011-06-01 09:33:00 +0000760void DesktopWindow::repositionViewport()
761{
762 int new_x, new_y;
763
764 // Deal with some scrolling corner cases:
765 //
766 // a) If the window is larger then the viewport, center the viewport.
767 // b) If the window is smaller than the viewport, make sure there is
768 // no wasted space on the sides.
769 //
770 // FIXME: Doesn't compensate for scroll widget size properly.
771
772 new_x = viewport->x();
773 new_y = viewport->y();
774
775 if (w() > viewport->w())
776 new_x = (w() - viewport->w()) / 2;
777 else {
778 if (viewport->x() > 0)
779 new_x = 0;
780 else if (w() > (viewport->x() + viewport->w()))
781 new_x = w() - viewport->w();
782 }
783
784 // Same thing for y axis
785 if (h() > viewport->h())
786 new_y = (h() - viewport->h()) / 2;
787 else {
788 if (viewport->y() > 0)
789 new_y = 0;
790 else if (h() > (viewport->y() + viewport->h()))
791 new_y = h() - viewport->h();
792 }
793
794 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
795 viewport->position(new_x, new_y);
796
797 // The scroll widget does not notice when you move around child widgets,
798 // so redraw everything to make sure things update.
799 redraw();
800 }
801}
802
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000803void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
804{
805 exit_vncviewer();
806}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000807
808
809void DesktopWindow::handleOptions(void *data)
810{
811 DesktopWindow *self = (DesktopWindow*)data;
812
Pierre Ossman407a5c32011-05-26 14:48:29 +0000813 if (self->fullscreen_active() && fullscreenSystemKeys)
814 self->grabKeyboard();
815 else
816 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000817
Pierre Ossmanff473402012-07-04 11:27:47 +0000818 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000819 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000820 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000821 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000822}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000823
824void DesktopWindow::handleFullscreenTimeout(void *data)
825{
826 DesktopWindow *self = (DesktopWindow *)data;
827
828 assert(self);
829
830 self->delayedFullscreen = false;
831
832 if (self->delayedDesktopSize) {
833 self->handleDesktopSize();
834 self->delayedDesktopSize = false;
835 }
836}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000837
838void DesktopWindow::handleEdgeScroll(void *data)
839{
Pierre Ossman1d867b62012-09-03 09:25:07 +0000840 DesktopWindow *self = (DesktopWindow *)data;
841
842 int mx, my;
843 int dx, dy;
844
845 assert(self);
846
847 if (!self->fullscreen_active())
848 return;
849
850 mx = Fl::event_x();
851 my = Fl::event_y();
852
853 dx = dy = 0;
854
855 // Clamp mouse position in case it is outside the window
856 if (mx < 0)
857 mx = 0;
858 if (mx > self->w())
859 mx = self->w();
860 if (my < 0)
861 my = 0;
862 if (my > self->h())
863 my = self->h();
864
865 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000866 dx = EDGE_SCROLL_SPEED -
867 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
868 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
869 (mx > self->w() - EDGE_SCROLL_SIZE))
870 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
871 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000872 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000873 dy = EDGE_SCROLL_SPEED -
874 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
875 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
876 (my > self->h() - EDGE_SCROLL_SIZE))
877 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
878 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000879
880 if ((dx == 0) && (dy == 0))
881 return;
882
883 // Make sure we don't move the viewport too much
884 if (self->viewport->x() + dx > 0)
885 dx = -self->viewport->x();
886 if (self->viewport->x() + dx + self->viewport->w() < self->w())
887 dx = self->w() - (self->viewport->x() + self->viewport->w());
888 if (self->viewport->y() + dy > 0)
889 dy = -self->viewport->y();
890 if (self->viewport->y() + dy + self->viewport->h() < self->h())
891 dy = self->h() - (self->viewport->y() + self->viewport->h());
892
893 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
894
895 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000896}