blob: 746f3413b905c07eac42c2e66e939f261d899b31 [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 Ossman407a5c32011-05-26 14:48:29 +000037
DRCb65bb932011-06-24 03:17:00 +000038#include <FL/Fl_Scroll.H>
39#include <FL/x.H>
40
Pierre Ossman407a5c32011-05-26 14:48:29 +000041#ifdef WIN32
42#include "win32.h"
43#endif
44
45#ifdef __APPLE__
46#include "cocoa.h"
47#endif
Pierre Ossman89f868a2011-04-11 11:59:31 +000048
Pierre Ossman1d867b62012-09-03 09:25:07 +000049#define EDGE_SCROLL_SIZE 32
50#define EDGE_SCROLL_SPEED 20
51
Pierre Ossman5156d5e2011-03-09 09:42:34 +000052using namespace rfb;
53
Pierre Ossman5156d5e2011-03-09 09:42:34 +000054static rfb::LogWriter vlog("DesktopWindow");
55
56DesktopWindow::DesktopWindow(int w, int h, const char *name,
57 const rfb::PixelFormat& serverPF,
58 CConn* cc_)
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000059 : Fl_Window(w, h), cc(cc_), firstUpdate(true),
60 delayedFullscreen(false), delayedDesktopSize(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000061{
Pierre Ossman1d867b62012-09-03 09:25:07 +000062 scroll = new Fl_Scroll(0, 0, w, h);
Pierre Ossman4ae229f2011-04-15 12:58:31 +000063 scroll->color(FL_BLACK);
64
65 // Automatically adjust the scroll box to the window
66 resizable(scroll);
67
Pierre Ossmanff473402012-07-04 11:27:47 +000068 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000069
Pierre Ossman4ae229f2011-04-15 12:58:31 +000070 scroll->end();
71
Pierre Ossman5156d5e2011-03-09 09:42:34 +000072 callback(handleClose, this);
73
74 setName(name);
75
Pierre Ossman407a5c32011-05-26 14:48:29 +000076 OptionsDialog::addCallback(handleOptions, this);
77
Pierre Ossmana4f0f182011-06-14 13:36:57 +000078 // Hack. See below...
79 Fl::event_dispatch(&fltkHandle);
80
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000081 // Support for -geometry option. Note that although we do support
82 // negative coordinates, we do not support -XOFF-YOFF (ie
83 // coordinates relative to the right edge / bottom edge) at this
84 // time.
85 int geom_x = 0, geom_y = 0;
86 if (geometry.hasBeenSet()) {
87 int matched;
88 matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
89 if (matched == 2) {
90 force_position(1);
91 } else {
92 int geom_w, geom_h;
93 matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
94 switch (matched) {
95 case 4:
Pierre Ossman9d267c52012-10-02 14:30:22 +000096 force_position(1);
97 /* fall through */
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000098 case 2:
Pierre Ossman9d267c52012-10-02 14:30:22 +000099 w = geom_w;
100 h = geom_h;
101 break;
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000102 default:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000103 geom_x = geom_y = 0;
104 vlog.error("Invalid geometry specified!");
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000105 }
106 }
107 }
108
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000109#ifdef __APPLE__
110 // On OS X we can do the maximize thing properly before the
111 // window is showned. Other platforms handled further down...
112 if (maximize) {
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000113#ifdef HAVE_FLTK_WORK_AREA
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000114 int dummy;
115 Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000116#else
117 w = Fl::w();
118 h = Fl::h();
119#endif
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000120 }
121#endif
122
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000123 if (force_position()) {
124 resize(geom_x, geom_y, w, h);
125 } else {
126 size(w, h);
127 }
128
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000129#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000130 if (fullScreen) {
131 // Hack: Window managers seem to be rather crappy at respecting
132 // fullscreen hints on initial windows. So on X11 we'll have to
133 // wait until after we've been mapped.
134#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000135 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000136#else
137 delayedFullscreen = true;
138#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000139 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000140#endif
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000141
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000142 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000143
Pierre Ossmancb68c192012-10-17 07:59:20 +0000144 // Full screen events are not sent out for a hidden window,
145 // so send a fake one here to set up things properly.
146#ifdef HAVE_FLTK_FULLSCREEN
147 if (fullscreen_active())
148 handle(FL_FULLSCREEN);
149#endif
150
Peter Åstrand49b11572012-08-01 08:09:09 +0000151 // Unfortunately, current FLTK does not allow us to set the
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000152 // maximized property on Windows and X11 before showing the window.
153 // See STR #2083 and STR #2178
154#ifndef __APPLE__
Peter Åstrand49b11572012-08-01 08:09:09 +0000155 if (maximize) {
156 maximizeWindow();
157 }
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000158#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000159
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000160 // The window manager might give us an initial window size that is different
161 // than the one we requested, and in those cases we need to manually adjust
162 // the scroll widget for things to behave sanely.
163 if ((w != this->w()) || (h != this->h()))
164 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000165
166#ifdef HAVE_FLTK_FULLSCREEN
167 if (delayedFullscreen) {
168 // Hack: Fullscreen requests may be ignored, so we need a timeout for
169 // when we should stop waiting. We also really need to wait for the
170 // resize, which can come after the fullscreen event.
171 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
172 fullscreen_on();
173 }
174#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000175}
176
177
178DesktopWindow::~DesktopWindow()
179{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000180 // Unregister all timeouts in case they get a change tro trigger
181 // again later when this object is already gone.
182 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000183 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000184 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000185 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000186
187 OptionsDialog::removeCallback(handleOptions);
188
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000189 // FLTK automatically deletes all child widgets, so we shouldn't touch
190 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000191}
192
193
194void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
195{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000196 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000197}
198
199
200const rfb::PixelFormat &DesktopWindow::getPreferredPF()
201{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000202 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000203}
204
205
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000206void DesktopWindow::setName(const char *name)
207{
208 CharArray windowNameStr;
209 windowNameStr.replaceBuf(new char[256]);
210
Pierre Ossman27820ba2011-09-30 12:54:24 +0000211 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000212
213 copy_label(windowNameStr.buf);
214}
215
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000216
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000217void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
218 rdr::U16* rgbs)
219{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000220 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000221}
222
223
224// Copy the areas of the framebuffer that have been changed (damaged)
225// to the displayed window.
226
227void DesktopWindow::updateWindow()
228{
Pierre Ossmanff473402012-07-04 11:27:47 +0000229 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000230 if (cc->cp.supportsSetDesktopSize) {
231 // Hack: Wait until we're in the proper mode and position until
232 // resizing things, otherwise we might send the wrong thing.
233 if (delayedFullscreen)
234 delayedDesktopSize = true;
235 else
236 handleDesktopSize();
237 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000238 firstUpdate = false;
239 }
240
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000241 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000242}
243
244
Pierre Ossman6455d852011-06-01 09:33:00 +0000245void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
246{
247 if ((new_w == viewport->w()) && (new_h == viewport->h()))
248 return;
249
Pierre Ossman6455d852011-06-01 09:33:00 +0000250 // If we're letting the viewport match the window perfectly, then
251 // keep things that way for the new size, otherwise just keep things
252 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000253#ifdef HAVE_FLTK_FULLSCREEN
254 if (!fullscreen_active()) {
255#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000256 if ((w() == viewport->w()) && (h() == viewport->h()))
257 size(new_w, new_h);
258 else {
259 // Make sure the window isn't too big. We do this manually because
260 // we have to disable the window size restriction (and it isn't
261 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000262 if ((w() > new_w) || (h() > new_h))
263 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000264 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000265#ifdef HAVE_FLTK_FULLSCREEN
266 }
267#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000268
269 viewport->size(new_w, new_h);
270
271 // We might not resize the main window, so we need to manually call this
272 // to make sure the viewport is centered.
273 repositionViewport();
274
Pierre Ossman6455d852011-06-01 09:33:00 +0000275 // repositionViewport() makes sure the scroll widget notices any changes
276 // in position, but it might be just the size that changes so we also
277 // need a poke here as well.
278 redraw();
279}
280
281
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000282void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
283 void* data, void* mask)
284{
285 viewport->setCursor(width, height, hotspot, data, mask);
286}
287
288
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000289void DesktopWindow::resize(int x, int y, int w, int h)
290{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000291 bool resizing;
292
Pierre Ossman0e593462012-09-03 09:43:23 +0000293#if ! (defined(WIN32) || defined(__APPLE__))
294 // X11 window managers will treat a resize to cover the entire
295 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman0e593462012-09-03 09:43:23 +0000296#ifdef HAVE_FLTK_FULLSCREEN
297 if (!fullscreen_active())
298#endif
299 {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000300 // Get the real window coordinates, so we can determine if
301 // this is a request to resize, or a notification of a resize
302 // from the X server.
303 XWindowAttributes actual;
304 Window cr;
305 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000306
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000307 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
308 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
309 0, 0, &wx, &wy, &cr);
Pierre Ossman0e593462012-09-03 09:43:23 +0000310
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000311 // Actual resize request?
312 if ((wx != x) || (wy != y) ||
313 (actual.width != w) || (actual.height != h)) {
314 for (int i = 0;i < Fl::screen_count();i++) {
315 int sx, sy, sw, sh;
316
317 Fl::screen_xywh(sx, sy, sw, sh, i);
318
319 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
320 vlog.info("Adjusting window size to avoid accidental full screen request");
321 // Assume a panel of some form and adjust the height
322 y += 20;
323 h -= 40;
324 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000325 }
326 }
327 }
328#endif
329
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000330 if ((this->w() != w) || (this->h() != h))
331 resizing = true;
332 else
333 resizing = false;
334
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000335 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000336
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000337 if (resizing) {
338 // Try to get the remote size to match our window size, provided
339 // the following conditions are true:
340 //
341 // a) The user has this feature turned on
342 // b) The server supports it
343 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000344 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000345 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000346 if (not firstUpdate and not delayedFullscreen and
347 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000348 // We delay updating the remote desktop as we tend to get a flood
349 // of resize events as the user is dragging the window.
350 Fl::remove_timeout(handleResizeTimeout, this);
351 Fl::add_timeout(0.5, handleResizeTimeout, this);
352 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000353
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000354 // Deal with some scrolling corner cases
355 repositionViewport();
356 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000357}
358
359
Pierre Ossman407a5c32011-05-26 14:48:29 +0000360int DesktopWindow::handle(int event)
361{
362 switch (event) {
363#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000364 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000365 fullScreen.setParam(fullscreen_active());
366
Pierre Ossman1d867b62012-09-03 09:25:07 +0000367 if (fullscreen_active())
368 scroll->type(0);
369 else
370 scroll->type(Fl_Scroll::BOTH);
371
Pierre Ossman407a5c32011-05-26 14:48:29 +0000372 if (!fullscreenSystemKeys)
373 break;
374
375 if (fullscreen_active())
376 grabKeyboard();
377 else
378 ungrabKeyboard();
379
380 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000381
382 case FL_ENTER:
383 case FL_LEAVE:
384 case FL_DRAG:
385 case FL_MOVE:
386 if (fullscreen_active()) {
387 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
388 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
389 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
390 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
391 if (!Fl::has_timeout(handleEdgeScroll, this))
392 Fl::add_timeout(0.1, handleEdgeScroll, this);
393 }
394 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000395 // Continue processing so that the viewport also gets mouse events
396 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000397#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000398
Pierre Ossman407a5c32011-05-26 14:48:29 +0000399 case FL_SHORTCUT:
400 // Sometimes the focus gets out of whack and we fall through to the
401 // shortcut dispatching. Try to make things sane again...
402 if (Fl::focus() == NULL) {
403 take_focus();
404 Fl::handle(FL_KEYDOWN, this);
405 }
406 return 1;
407 }
408
409 return Fl_Window::handle(event);
410}
411
412
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000413int DesktopWindow::fltkHandle(int event, Fl_Window *win)
414{
415 int ret;
416
417 ret = Fl::handle_(event, win);
418
419#ifdef HAVE_FLTK_FULLSCREEN
420 // This is hackish and the result of the dodgy focus handling in FLTK.
421 // The basic problem is that FLTK's view of focus and the system's tend
422 // to differ, and as a result we do not see all the FL_FOCUS events we
423 // need. Fortunately we can grab them here...
424
425 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
426
427 if (dw && fullscreenSystemKeys) {
428 switch (event) {
429 case FL_FOCUS:
430 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
431 // some issues we need to work around:
432 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
433 // b) Gaining focus on the system level causes FLTK to switch
434 // window level on OS X.
435 if (dw->fullscreen_active())
436 dw->grabKeyboard();
437 break;
438
Pierre Ossmanb1369802012-10-17 07:59:36 +0000439 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000440 // FIXME: We need to relinquish control when the entire window loses
441 // focus as it is very tied to this specific window on some
442 // platforms and we want to be able to open subwindows.
443 dw->ungrabKeyboard();
444 break;
445 }
446 }
447#endif
448
449 return ret;
450}
451
452
Pierre Ossmanaae38912012-07-13 11:22:55 +0000453void DesktopWindow::fullscreen_on()
454{
455#ifdef HAVE_FLTK_FULLSCREEN
456#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
457 if (not fullScreenAllMonitors)
458 fullscreen_screens(-1, -1, -1, -1);
459 else {
460 int top, bottom, left, right;
461 int top_y, bottom_y, left_x, right_x;
462
463 int sx, sy, sw, sh;
464
465 top = bottom = left = right = 0;
466
467 Fl::screen_xywh(sx, sy, sw, sh, 0);
468 top_y = sy;
469 bottom_y = sy + sh;
470 left_x = sx;
471 right_x = sx + sw;
472
473 for (int i = 1;i < Fl::screen_count();i++) {
474 Fl::screen_xywh(sx, sy, sw, sh, i);
475 if (sy < top_y) {
476 top = i;
477 top_y = sy;
478 }
479 if ((sy + sh) > bottom_y) {
480 bottom = i;
481 bottom_y = sy + sh;
482 }
483 if (sx < left_x) {
484 left = i;
485 left_x = sx;
486 }
487 if ((sx + sw) > right_x) {
488 right = i;
489 right_x = sx + sw;
490 }
491 }
492
493 fullscreen_screens(top, bottom, left, right);
494 }
495#endif // HAVE_FLTK_FULLSCREEN_SCREENS
496
497 fullscreen();
498#endif // HAVE_FLTK_FULLSCREEN
499}
500
Pierre Ossman407a5c32011-05-26 14:48:29 +0000501void DesktopWindow::grabKeyboard()
502{
503 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
504 // correct widget regardless of which low level window got the system
505 // event.
506
507 // FIXME: Push this stuff into FLTK.
508
509#if defined(WIN32)
510 int ret;
511
512 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
513 if (ret != 0)
514 vlog.error(_("Failure grabbing keyboard"));
515#elif defined(__APPLE__)
516 int ret;
517
Pierre Ossman3d759112012-08-27 14:40:51 +0000518 ret = cocoa_capture_display(this,
519#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
520 fullScreenAllMonitors
521#else
522 false
523#endif
524 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000525 if (ret != 0)
526 vlog.error(_("Failure grabbing keyboard"));
527#else
528 int ret;
529
530 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000531 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000532 if (ret) {
533 if (ret == AlreadyGrabbed) {
534 // It seems like we can race with the WM in some cases.
535 // Try again in a bit.
536 if (!Fl::has_timeout(handleGrab, this))
537 Fl::add_timeout(0.500, handleGrab, this);
538 } else {
539 vlog.error(_("Failure grabbing keyboard"));
540 }
541 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000542
543 // We also need to grab the pointer as some WMs like to grab buttons
544 // combined with modifies (e.g. Alt+Button0 in metacity).
545 ret = XGrabPointer(fl_display, fl_xid(this), True,
546 ButtonPressMask|ButtonReleaseMask|
547 ButtonMotionMask|PointerMotionMask,
548 GrabModeAsync, GrabModeAsync,
549 None, None, CurrentTime);
550 if (ret)
551 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000552#endif
553}
554
555
556void DesktopWindow::ungrabKeyboard()
557{
558 Fl::remove_timeout(handleGrab, this);
559
560#if defined(WIN32)
561 win32_disable_lowlevel_keyboard(fl_xid(this));
562#elif defined(__APPLE__)
563 cocoa_release_display(this);
564#else
565 // FLTK has a grab so lets not mess with it
566 if (Fl::grab())
567 return;
568
Pierre Ossman53fd5442011-11-17 10:19:19 +0000569 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000570 XUngrabKeyboard(fl_display, fl_event_time);
571#endif
572}
573
574
575void DesktopWindow::handleGrab(void *data)
576{
577 DesktopWindow *self = (DesktopWindow*)data;
578
579 assert(self);
580
581#ifdef HAVE_FLTK_FULLSCREEN
582 if (!fullscreenSystemKeys)
583 return;
584 if (!self->fullscreen_active())
585 return;
586
587 self->grabKeyboard();
588#endif
589}
590
591
Peter Åstrand49b11572012-08-01 08:09:09 +0000592#define _NET_WM_STATE_ADD 1 /* add/set property */
593void DesktopWindow::maximizeWindow()
594{
595#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000596 // We cannot use ShowWindow() in full screen mode as it will
597 // resize things implicitly. Fortunately modifying the style
598 // directly results in a maximized state once we leave full screen.
599#ifdef HAVE_FLTK_FULLSCREEN
600 if (fullscreen_active()) {
601 WINDOWINFO wi;
602 wi.cbSize = sizeof(WINDOWINFO);
603 GetWindowInfo(fl_xid(this), &wi);
604 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
605 } else
606#endif
607 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000608#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000609 // OS X is somewhat strange and does not really have a concept of a
610 // maximized window, so we can simply resize the window to the workarea.
611 // Note that we shouldn't do this whilst in full screen as that will
612 // incorrectly adjust things.
613#ifdef HAVE_FLTK_FULLSCREEN
614 if (fullscreen_active())
615 return;
616#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000617 int X, Y, W, H;
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000618#ifdef HAVE_FLTK_WORK_AREA
Peter Åstrand49b11572012-08-01 08:09:09 +0000619 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000620#else
621 W = Fl::w();
622 H = Fl::h();
623#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000624 size(W, H);
625#else
626 // X11
627 fl_open_display();
628 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
629 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
630 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
631
632 XEvent e;
633 e.xany.type = ClientMessage;
634 e.xany.window = fl_xid(this);
635 e.xclient.message_type = net_wm_state;
636 e.xclient.format = 32;
637 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
638 e.xclient.data.l[1] = net_wm_state_maximized_vert;
639 e.xclient.data.l[2] = net_wm_state_maximized_horz;
640 e.xclient.data.l[3] = 0;
641 e.xclient.data.l[4] = 0;
642 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
643#endif
644}
645
646
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000647void DesktopWindow::handleDesktopSize()
648{
649 int width, height;
650
651 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
652 return;
653
654 remoteResize(width, height);
655}
656
657
Pierre Ossmanff473402012-07-04 11:27:47 +0000658void DesktopWindow::handleResizeTimeout(void *data)
659{
660 DesktopWindow *self = (DesktopWindow *)data;
661
662 assert(self);
663
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000664 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000665}
666
667
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000668void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000669{
Pierre Ossmanff473402012-07-04 11:27:47 +0000670 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000671 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000672
Pierre Ossmanaae38912012-07-13 11:22:55 +0000673#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000674 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000675#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000676 // In windowed mode (or the framebuffer is so large that we need
677 // to scroll) we just report a single virtual screen that covers
678 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000679
Pierre Ossmanaae38912012-07-13 11:22:55 +0000680 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000681
Pierre Ossmanaae38912012-07-13 11:22:55 +0000682 // Not sure why we have no screens, but adding a new one should be
683 // safe as there is nothing to conflict with...
684 if (layout.num_screens() == 0)
685 layout.add_screen(rfb::Screen());
686 else if (layout.num_screens() != 1) {
687 // More than one screen. Remove all but the first (which we
688 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000689
Pierre Ossmanaae38912012-07-13 11:22:55 +0000690 while (true) {
691 iter = layout.begin();
692 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000693
Pierre Ossmanaae38912012-07-13 11:22:55 +0000694 if (iter == layout.end())
695 break;
696
697 layout.remove_screen(iter->id);
698 }
699 }
700
701 // Resize the remaining single screen to the complete framebuffer
702 layout.begin()->dimensions.tl.x = 0;
703 layout.begin()->dimensions.tl.y = 0;
704 layout.begin()->dimensions.br.x = width;
705 layout.begin()->dimensions.br.y = height;
706#ifdef HAVE_FLTK_FULLSCREEN
707 } else {
708 int i;
709 rdr::U32 id;
710 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000711 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000712
713 // In full screen we report all screens that are fully covered.
714
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000715 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
716 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000717
Pierre Ossmanaae38912012-07-13 11:22:55 +0000718 // If we can find a matching screen in the existing set, we use
719 // that, otherwise we create a brand new screen.
720 //
721 // FIXME: We should really track screens better so we can handle
722 // a resized one.
723 //
724 for (i = 0;i < Fl::screen_count();i++) {
725 Fl::screen_xywh(sx, sy, sw, sh, i);
726
Pierre Ossman93d2d922012-07-20 12:32:52 +0000727 // Check that the screen is fully inside the framebuffer
728 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000729 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000730 continue;
731
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000732 // Adjust the coordinates so they are relative to our viewport
733 sx -= viewport_rect.tl.x;
734 sy -= viewport_rect.tl.y;
735
Pierre Ossmanaae38912012-07-13 11:22:55 +0000736 // Look for perfectly matching existing screen...
737 for (iter = cc->cp.screenLayout.begin();
738 iter != cc->cp.screenLayout.end(); ++iter) {
739 if ((iter->dimensions.tl.x == sx) &&
740 (iter->dimensions.tl.y == sy) &&
741 (iter->dimensions.width() == sw) &&
742 (iter->dimensions.height() == sh))
743 break;
744 }
745
746 // Found it?
747 if (iter != cc->cp.screenLayout.end()) {
748 layout.add_screen(*iter);
749 continue;
750 }
751
752 // Need to add a new one, which means we need to find an unused id
753 while (true) {
754 id = rand();
755 for (iter = cc->cp.screenLayout.begin();
756 iter != cc->cp.screenLayout.end(); ++iter) {
757 if (iter->id == id)
758 break;
759 }
760
761 if (iter == cc->cp.screenLayout.end())
762 break;
763 }
764
765 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000766 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000767
768 // If the viewport doesn't match a physical screen, then we might
769 // end up with no screens in the layout. Add a fake one...
770 if (layout.num_screens() == 0)
771 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000772 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000773#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000774
775 // Do we actually change anything?
776 if ((width == cc->cp.width) &&
777 (height == cc->cp.height) &&
778 (layout == cc->cp.screenLayout))
779 return;
780
Pierre Ossmanaae38912012-07-13 11:22:55 +0000781 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
782 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
783
784 if (!layout.validate(width, height)) {
785 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000786 return;
787 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000788
789 cc->writer()->writeSetDesktopSize(width, height, layout);
790}
791
792
Pierre Ossman6455d852011-06-01 09:33:00 +0000793void DesktopWindow::repositionViewport()
794{
795 int new_x, new_y;
796
797 // Deal with some scrolling corner cases:
798 //
799 // a) If the window is larger then the viewport, center the viewport.
800 // b) If the window is smaller than the viewport, make sure there is
801 // no wasted space on the sides.
802 //
803 // FIXME: Doesn't compensate for scroll widget size properly.
804
805 new_x = viewport->x();
806 new_y = viewport->y();
807
808 if (w() > viewport->w())
809 new_x = (w() - viewport->w()) / 2;
810 else {
811 if (viewport->x() > 0)
812 new_x = 0;
813 else if (w() > (viewport->x() + viewport->w()))
814 new_x = w() - viewport->w();
815 }
816
817 // Same thing for y axis
818 if (h() > viewport->h())
819 new_y = (h() - viewport->h()) / 2;
820 else {
821 if (viewport->y() > 0)
822 new_y = 0;
823 else if (h() > (viewport->y() + viewport->h()))
824 new_y = h() - viewport->h();
825 }
826
827 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
828 viewport->position(new_x, new_y);
829
830 // The scroll widget does not notice when you move around child widgets,
831 // so redraw everything to make sure things update.
832 redraw();
833 }
834}
835
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000836void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
837{
838 exit_vncviewer();
839}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000840
841
842void DesktopWindow::handleOptions(void *data)
843{
844 DesktopWindow *self = (DesktopWindow*)data;
845
846#ifdef HAVE_FLTK_FULLSCREEN
847 if (self->fullscreen_active() && fullscreenSystemKeys)
848 self->grabKeyboard();
849 else
850 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000851
Pierre Ossmanff473402012-07-04 11:27:47 +0000852 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000853 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000854 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000855 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000856#endif
857}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000858
859void DesktopWindow::handleFullscreenTimeout(void *data)
860{
861 DesktopWindow *self = (DesktopWindow *)data;
862
863 assert(self);
864
865 self->delayedFullscreen = false;
866
867 if (self->delayedDesktopSize) {
868 self->handleDesktopSize();
869 self->delayedDesktopSize = false;
870 }
871}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000872
873void DesktopWindow::handleEdgeScroll(void *data)
874{
875#ifdef HAVE_FLTK_FULLSCREEN
876 DesktopWindow *self = (DesktopWindow *)data;
877
878 int mx, my;
879 int dx, dy;
880
881 assert(self);
882
883 if (!self->fullscreen_active())
884 return;
885
886 mx = Fl::event_x();
887 my = Fl::event_y();
888
889 dx = dy = 0;
890
891 // Clamp mouse position in case it is outside the window
892 if (mx < 0)
893 mx = 0;
894 if (mx > self->w())
895 mx = self->w();
896 if (my < 0)
897 my = 0;
898 if (my > self->h())
899 my = self->h();
900
901 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000902 dx = EDGE_SCROLL_SPEED -
903 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
904 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
905 (mx > self->w() - EDGE_SCROLL_SIZE))
906 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
907 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000908 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000909 dy = EDGE_SCROLL_SPEED -
910 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
911 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
912 (my > self->h() - EDGE_SCROLL_SIZE))
913 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
914 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000915
916 if ((dx == 0) && (dy == 0))
917 return;
918
919 // Make sure we don't move the viewport too much
920 if (self->viewport->x() + dx > 0)
921 dx = -self->viewport->x();
922 if (self->viewport->x() + dx + self->viewport->w() < self->w())
923 dx = self->w() - (self->viewport->x() + self->viewport->w());
924 if (self->viewport->y() + dy > 0)
925 dy = -self->viewport->y();
926 if (self->viewport->y() + dy + self->viewport->h() < self->h())
927 dy = self->h() - (self->viewport->y() + self->viewport->h());
928
929 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
930
931 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
932#endif
933}