blob: 5cec60b99198159395352f78585052727a3b13a5 [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) {
113 int dummy;
114 Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
115 }
116#endif
117
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000118 if (force_position()) {
119 resize(geom_x, geom_y, w, h);
120 } else {
121 size(w, h);
122 }
123
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000124#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000125 if (fullScreen) {
126 // Hack: Window managers seem to be rather crappy at respecting
127 // fullscreen hints on initial windows. So on X11 we'll have to
128 // wait until after we've been mapped.
129#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000130 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000131#else
132 delayedFullscreen = true;
133#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000134 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000135#endif
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.
141#ifdef HAVE_FLTK_FULLSCREEN
142 if (fullscreen_active())
143 handle(FL_FULLSCREEN);
144#endif
145
Peter Åstrand49b11572012-08-01 08:09:09 +0000146 // Unfortunately, current FLTK does not allow us to set the
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000147 // maximized property on Windows and X11 before showing the window.
148 // See STR #2083 and STR #2178
149#ifndef __APPLE__
Peter Åstrand49b11572012-08-01 08:09:09 +0000150 if (maximize) {
151 maximizeWindow();
152 }
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000153#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000154
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000155 // The window manager might give us an initial window size that is different
156 // than the one we requested, and in those cases we need to manually adjust
157 // the scroll widget for things to behave sanely.
158 if ((w != this->w()) || (h != this->h()))
159 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000160
161#ifdef HAVE_FLTK_FULLSCREEN
162 if (delayedFullscreen) {
163 // Hack: Fullscreen requests may be ignored, so we need a timeout for
164 // when we should stop waiting. We also really need to wait for the
165 // resize, which can come after the fullscreen event.
166 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
167 fullscreen_on();
168 }
169#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000170}
171
172
173DesktopWindow::~DesktopWindow()
174{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000175 // Unregister all timeouts in case they get a change tro trigger
176 // again later when this object is already gone.
177 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000178 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000179 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000180 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000181
182 OptionsDialog::removeCallback(handleOptions);
183
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000184 // FLTK automatically deletes all child widgets, so we shouldn't touch
185 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000186}
187
188
189void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
190{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000191 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000192}
193
194
195const rfb::PixelFormat &DesktopWindow::getPreferredPF()
196{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000197 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000198}
199
200
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000201void DesktopWindow::setName(const char *name)
202{
203 CharArray windowNameStr;
204 windowNameStr.replaceBuf(new char[256]);
205
Pierre Ossman27820ba2011-09-30 12:54:24 +0000206 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000207
208 copy_label(windowNameStr.buf);
209}
210
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000211
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000212void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
213 rdr::U16* rgbs)
214{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000215 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000216}
217
218
219// Copy the areas of the framebuffer that have been changed (damaged)
220// to the displayed window.
221
222void DesktopWindow::updateWindow()
223{
Pierre Ossmanff473402012-07-04 11:27:47 +0000224 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000225 if (cc->cp.supportsSetDesktopSize) {
226 // Hack: Wait until we're in the proper mode and position until
227 // resizing things, otherwise we might send the wrong thing.
228 if (delayedFullscreen)
229 delayedDesktopSize = true;
230 else
231 handleDesktopSize();
232 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000233 firstUpdate = false;
234 }
235
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000236 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000237}
238
239
Pierre Ossman6455d852011-06-01 09:33:00 +0000240void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
241{
242 if ((new_w == viewport->w()) && (new_h == viewport->h()))
243 return;
244
Pierre Ossman6455d852011-06-01 09:33:00 +0000245 // If we're letting the viewport match the window perfectly, then
246 // keep things that way for the new size, otherwise just keep things
247 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000248#ifdef HAVE_FLTK_FULLSCREEN
249 if (!fullscreen_active()) {
250#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000251 if ((w() == viewport->w()) && (h() == viewport->h()))
252 size(new_w, new_h);
253 else {
254 // Make sure the window isn't too big. We do this manually because
255 // we have to disable the window size restriction (and it isn't
256 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000257 if ((w() > new_w) || (h() > new_h))
258 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000259 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000260#ifdef HAVE_FLTK_FULLSCREEN
261 }
262#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000263
264 viewport->size(new_w, new_h);
265
266 // We might not resize the main window, so we need to manually call this
267 // to make sure the viewport is centered.
268 repositionViewport();
269
Pierre Ossman6455d852011-06-01 09:33:00 +0000270 // repositionViewport() makes sure the scroll widget notices any changes
271 // in position, but it might be just the size that changes so we also
272 // need a poke here as well.
273 redraw();
274}
275
276
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000277void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
278 void* data, void* mask)
279{
280 viewport->setCursor(width, height, hotspot, data, mask);
281}
282
283
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000284void DesktopWindow::resize(int x, int y, int w, int h)
285{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000286 bool resizing;
287
Pierre Ossman0e593462012-09-03 09:43:23 +0000288#if ! (defined(WIN32) || defined(__APPLE__))
289 // X11 window managers will treat a resize to cover the entire
290 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman0e593462012-09-03 09:43:23 +0000291#ifdef HAVE_FLTK_FULLSCREEN
292 if (!fullscreen_active())
293#endif
294 {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000295 // Get the real window coordinates, so we can determine if
296 // this is a request to resize, or a notification of a resize
297 // from the X server.
298 XWindowAttributes actual;
299 Window cr;
300 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000301
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000302 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
303 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
304 0, 0, &wx, &wy, &cr);
Pierre Ossman0e593462012-09-03 09:43:23 +0000305
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000306 // Actual resize request?
307 if ((wx != x) || (wy != y) ||
308 (actual.width != w) || (actual.height != h)) {
309 for (int i = 0;i < Fl::screen_count();i++) {
310 int sx, sy, sw, sh;
311
312 Fl::screen_xywh(sx, sy, sw, sh, i);
313
314 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
315 vlog.info("Adjusting window size to avoid accidental full screen request");
316 // Assume a panel of some form and adjust the height
317 y += 20;
318 h -= 40;
319 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000320 }
321 }
322 }
323#endif
324
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000325 if ((this->w() != w) || (this->h() != h))
326 resizing = true;
327 else
328 resizing = false;
329
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000330 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000331
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000332 if (resizing) {
333 // Try to get the remote size to match our window size, provided
334 // the following conditions are true:
335 //
336 // a) The user has this feature turned on
337 // b) The server supports it
338 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000339 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000340 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000341 if (not firstUpdate and not delayedFullscreen and
342 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000343 // We delay updating the remote desktop as we tend to get a flood
344 // of resize events as the user is dragging the window.
345 Fl::remove_timeout(handleResizeTimeout, this);
346 Fl::add_timeout(0.5, handleResizeTimeout, this);
347 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000348
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000349 // Deal with some scrolling corner cases
350 repositionViewport();
351 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000352}
353
354
Pierre Ossman407a5c32011-05-26 14:48:29 +0000355int DesktopWindow::handle(int event)
356{
357 switch (event) {
358#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000359 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000360 fullScreen.setParam(fullscreen_active());
361
Pierre Ossman1d867b62012-09-03 09:25:07 +0000362 if (fullscreen_active())
363 scroll->type(0);
364 else
365 scroll->type(Fl_Scroll::BOTH);
366
Pierre Ossman407a5c32011-05-26 14:48:29 +0000367 if (!fullscreenSystemKeys)
368 break;
369
370 if (fullscreen_active())
371 grabKeyboard();
372 else
373 ungrabKeyboard();
374
375 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000376
377 case FL_ENTER:
378 case FL_LEAVE:
379 case FL_DRAG:
380 case FL_MOVE:
381 if (fullscreen_active()) {
382 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
383 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
384 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
385 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
386 if (!Fl::has_timeout(handleEdgeScroll, this))
387 Fl::add_timeout(0.1, handleEdgeScroll, this);
388 }
389 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000390 // Continue processing so that the viewport also gets mouse events
391 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000392#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000393
Pierre Ossman407a5c32011-05-26 14:48:29 +0000394 case FL_SHORTCUT:
395 // Sometimes the focus gets out of whack and we fall through to the
396 // shortcut dispatching. Try to make things sane again...
397 if (Fl::focus() == NULL) {
398 take_focus();
399 Fl::handle(FL_KEYDOWN, this);
400 }
401 return 1;
402 }
403
404 return Fl_Window::handle(event);
405}
406
407
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000408int DesktopWindow::fltkHandle(int event, Fl_Window *win)
409{
410 int ret;
411
412 ret = Fl::handle_(event, win);
413
414#ifdef HAVE_FLTK_FULLSCREEN
415 // This is hackish and the result of the dodgy focus handling in FLTK.
416 // The basic problem is that FLTK's view of focus and the system's tend
417 // to differ, and as a result we do not see all the FL_FOCUS events we
418 // need. Fortunately we can grab them here...
419
420 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
421
422 if (dw && fullscreenSystemKeys) {
423 switch (event) {
424 case FL_FOCUS:
425 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
426 // some issues we need to work around:
427 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
428 // b) Gaining focus on the system level causes FLTK to switch
429 // window level on OS X.
430 if (dw->fullscreen_active())
431 dw->grabKeyboard();
432 break;
433
434 case FL_UNFOCUS:
435 // FIXME: We need to relinquish control when the entire window loses
436 // focus as it is very tied to this specific window on some
437 // platforms and we want to be able to open subwindows.
438 dw->ungrabKeyboard();
439 break;
440 }
441 }
442#endif
443
444 return ret;
445}
446
447
Pierre Ossmanaae38912012-07-13 11:22:55 +0000448void DesktopWindow::fullscreen_on()
449{
450#ifdef HAVE_FLTK_FULLSCREEN
451#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
452 if (not fullScreenAllMonitors)
453 fullscreen_screens(-1, -1, -1, -1);
454 else {
455 int top, bottom, left, right;
456 int top_y, bottom_y, left_x, right_x;
457
458 int sx, sy, sw, sh;
459
460 top = bottom = left = right = 0;
461
462 Fl::screen_xywh(sx, sy, sw, sh, 0);
463 top_y = sy;
464 bottom_y = sy + sh;
465 left_x = sx;
466 right_x = sx + sw;
467
468 for (int i = 1;i < Fl::screen_count();i++) {
469 Fl::screen_xywh(sx, sy, sw, sh, i);
470 if (sy < top_y) {
471 top = i;
472 top_y = sy;
473 }
474 if ((sy + sh) > bottom_y) {
475 bottom = i;
476 bottom_y = sy + sh;
477 }
478 if (sx < left_x) {
479 left = i;
480 left_x = sx;
481 }
482 if ((sx + sw) > right_x) {
483 right = i;
484 right_x = sx + sw;
485 }
486 }
487
488 fullscreen_screens(top, bottom, left, right);
489 }
490#endif // HAVE_FLTK_FULLSCREEN_SCREENS
491
492 fullscreen();
493#endif // HAVE_FLTK_FULLSCREEN
494}
495
Pierre Ossman407a5c32011-05-26 14:48:29 +0000496void DesktopWindow::grabKeyboard()
497{
498 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
499 // correct widget regardless of which low level window got the system
500 // event.
501
502 // FIXME: Push this stuff into FLTK.
503
504#if defined(WIN32)
505 int ret;
506
507 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
508 if (ret != 0)
509 vlog.error(_("Failure grabbing keyboard"));
510#elif defined(__APPLE__)
511 int ret;
512
Pierre Ossman3d759112012-08-27 14:40:51 +0000513 ret = cocoa_capture_display(this,
514#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
515 fullScreenAllMonitors
516#else
517 false
518#endif
519 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000520 if (ret != 0)
521 vlog.error(_("Failure grabbing keyboard"));
522#else
523 int ret;
524
525 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000526 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000527 if (ret) {
528 if (ret == AlreadyGrabbed) {
529 // It seems like we can race with the WM in some cases.
530 // Try again in a bit.
531 if (!Fl::has_timeout(handleGrab, this))
532 Fl::add_timeout(0.500, handleGrab, this);
533 } else {
534 vlog.error(_("Failure grabbing keyboard"));
535 }
536 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000537
538 // We also need to grab the pointer as some WMs like to grab buttons
539 // combined with modifies (e.g. Alt+Button0 in metacity).
540 ret = XGrabPointer(fl_display, fl_xid(this), True,
541 ButtonPressMask|ButtonReleaseMask|
542 ButtonMotionMask|PointerMotionMask,
543 GrabModeAsync, GrabModeAsync,
544 None, None, CurrentTime);
545 if (ret)
546 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000547#endif
548}
549
550
551void DesktopWindow::ungrabKeyboard()
552{
553 Fl::remove_timeout(handleGrab, this);
554
555#if defined(WIN32)
556 win32_disable_lowlevel_keyboard(fl_xid(this));
557#elif defined(__APPLE__)
558 cocoa_release_display(this);
559#else
560 // FLTK has a grab so lets not mess with it
561 if (Fl::grab())
562 return;
563
Pierre Ossman53fd5442011-11-17 10:19:19 +0000564 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000565 XUngrabKeyboard(fl_display, fl_event_time);
566#endif
567}
568
569
570void DesktopWindow::handleGrab(void *data)
571{
572 DesktopWindow *self = (DesktopWindow*)data;
573
574 assert(self);
575
576#ifdef HAVE_FLTK_FULLSCREEN
577 if (!fullscreenSystemKeys)
578 return;
579 if (!self->fullscreen_active())
580 return;
581
582 self->grabKeyboard();
583#endif
584}
585
586
Peter Åstrand49b11572012-08-01 08:09:09 +0000587#define _NET_WM_STATE_ADD 1 /* add/set property */
588void DesktopWindow::maximizeWindow()
589{
590#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000591 // We cannot use ShowWindow() in full screen mode as it will
592 // resize things implicitly. Fortunately modifying the style
593 // directly results in a maximized state once we leave full screen.
594#ifdef HAVE_FLTK_FULLSCREEN
595 if (fullscreen_active()) {
596 WINDOWINFO wi;
597 wi.cbSize = sizeof(WINDOWINFO);
598 GetWindowInfo(fl_xid(this), &wi);
599 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
600 } else
601#endif
602 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000603#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000604 // OS X is somewhat strange and does not really have a concept of a
605 // maximized window, so we can simply resize the window to the workarea.
606 // Note that we shouldn't do this whilst in full screen as that will
607 // incorrectly adjust things.
608#ifdef HAVE_FLTK_FULLSCREEN
609 if (fullscreen_active())
610 return;
611#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000612 int X, Y, W, H;
613 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
614 size(W, H);
615#else
616 // X11
617 fl_open_display();
618 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
619 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
620 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
621
622 XEvent e;
623 e.xany.type = ClientMessage;
624 e.xany.window = fl_xid(this);
625 e.xclient.message_type = net_wm_state;
626 e.xclient.format = 32;
627 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
628 e.xclient.data.l[1] = net_wm_state_maximized_vert;
629 e.xclient.data.l[2] = net_wm_state_maximized_horz;
630 e.xclient.data.l[3] = 0;
631 e.xclient.data.l[4] = 0;
632 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
633#endif
634}
635
636
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000637void DesktopWindow::handleDesktopSize()
638{
639 int width, height;
640
641 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
642 return;
643
644 remoteResize(width, height);
645}
646
647
Pierre Ossmanff473402012-07-04 11:27:47 +0000648void DesktopWindow::handleResizeTimeout(void *data)
649{
650 DesktopWindow *self = (DesktopWindow *)data;
651
652 assert(self);
653
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000654 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000655}
656
657
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000658void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000659{
Pierre Ossmanff473402012-07-04 11:27:47 +0000660 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000661 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000662
Pierre Ossmanaae38912012-07-13 11:22:55 +0000663#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000664 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000665#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000666 // In windowed mode (or the framebuffer is so large that we need
667 // to scroll) we just report a single virtual screen that covers
668 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000669
Pierre Ossmanaae38912012-07-13 11:22:55 +0000670 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000671
Pierre Ossmanaae38912012-07-13 11:22:55 +0000672 // Not sure why we have no screens, but adding a new one should be
673 // safe as there is nothing to conflict with...
674 if (layout.num_screens() == 0)
675 layout.add_screen(rfb::Screen());
676 else if (layout.num_screens() != 1) {
677 // More than one screen. Remove all but the first (which we
678 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000679
Pierre Ossmanaae38912012-07-13 11:22:55 +0000680 while (true) {
681 iter = layout.begin();
682 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000683
Pierre Ossmanaae38912012-07-13 11:22:55 +0000684 if (iter == layout.end())
685 break;
686
687 layout.remove_screen(iter->id);
688 }
689 }
690
691 // Resize the remaining single screen to the complete framebuffer
692 layout.begin()->dimensions.tl.x = 0;
693 layout.begin()->dimensions.tl.y = 0;
694 layout.begin()->dimensions.br.x = width;
695 layout.begin()->dimensions.br.y = height;
696#ifdef HAVE_FLTK_FULLSCREEN
697 } else {
698 int i;
699 rdr::U32 id;
700 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000701 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000702
703 // In full screen we report all screens that are fully covered.
704
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000705 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
706 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000707
Pierre Ossmanaae38912012-07-13 11:22:55 +0000708 // If we can find a matching screen in the existing set, we use
709 // that, otherwise we create a brand new screen.
710 //
711 // FIXME: We should really track screens better so we can handle
712 // a resized one.
713 //
714 for (i = 0;i < Fl::screen_count();i++) {
715 Fl::screen_xywh(sx, sy, sw, sh, i);
716
Pierre Ossman93d2d922012-07-20 12:32:52 +0000717 // Check that the screen is fully inside the framebuffer
718 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000719 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000720 continue;
721
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000722 // Adjust the coordinates so they are relative to our viewport
723 sx -= viewport_rect.tl.x;
724 sy -= viewport_rect.tl.y;
725
Pierre Ossmanaae38912012-07-13 11:22:55 +0000726 // Look for perfectly matching existing screen...
727 for (iter = cc->cp.screenLayout.begin();
728 iter != cc->cp.screenLayout.end(); ++iter) {
729 if ((iter->dimensions.tl.x == sx) &&
730 (iter->dimensions.tl.y == sy) &&
731 (iter->dimensions.width() == sw) &&
732 (iter->dimensions.height() == sh))
733 break;
734 }
735
736 // Found it?
737 if (iter != cc->cp.screenLayout.end()) {
738 layout.add_screen(*iter);
739 continue;
740 }
741
742 // Need to add a new one, which means we need to find an unused id
743 while (true) {
744 id = rand();
745 for (iter = cc->cp.screenLayout.begin();
746 iter != cc->cp.screenLayout.end(); ++iter) {
747 if (iter->id == id)
748 break;
749 }
750
751 if (iter == cc->cp.screenLayout.end())
752 break;
753 }
754
755 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000756 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000757
758 // If the viewport doesn't match a physical screen, then we might
759 // end up with no screens in the layout. Add a fake one...
760 if (layout.num_screens() == 0)
761 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000762 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000763#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000764
765 // Do we actually change anything?
766 if ((width == cc->cp.width) &&
767 (height == cc->cp.height) &&
768 (layout == cc->cp.screenLayout))
769 return;
770
Pierre Ossmanaae38912012-07-13 11:22:55 +0000771 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
772 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
773
774 if (!layout.validate(width, height)) {
775 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000776 return;
777 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000778
779 cc->writer()->writeSetDesktopSize(width, height, layout);
780}
781
782
Pierre Ossman6455d852011-06-01 09:33:00 +0000783void DesktopWindow::repositionViewport()
784{
785 int new_x, new_y;
786
787 // Deal with some scrolling corner cases:
788 //
789 // a) If the window is larger then the viewport, center the viewport.
790 // b) If the window is smaller than the viewport, make sure there is
791 // no wasted space on the sides.
792 //
793 // FIXME: Doesn't compensate for scroll widget size properly.
794
795 new_x = viewport->x();
796 new_y = viewport->y();
797
798 if (w() > viewport->w())
799 new_x = (w() - viewport->w()) / 2;
800 else {
801 if (viewport->x() > 0)
802 new_x = 0;
803 else if (w() > (viewport->x() + viewport->w()))
804 new_x = w() - viewport->w();
805 }
806
807 // Same thing for y axis
808 if (h() > viewport->h())
809 new_y = (h() - viewport->h()) / 2;
810 else {
811 if (viewport->y() > 0)
812 new_y = 0;
813 else if (h() > (viewport->y() + viewport->h()))
814 new_y = h() - viewport->h();
815 }
816
817 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
818 viewport->position(new_x, new_y);
819
820 // The scroll widget does not notice when you move around child widgets,
821 // so redraw everything to make sure things update.
822 redraw();
823 }
824}
825
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000826void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
827{
828 exit_vncviewer();
829}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000830
831
832void DesktopWindow::handleOptions(void *data)
833{
834 DesktopWindow *self = (DesktopWindow*)data;
835
836#ifdef HAVE_FLTK_FULLSCREEN
837 if (self->fullscreen_active() && fullscreenSystemKeys)
838 self->grabKeyboard();
839 else
840 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000841
Pierre Ossmanff473402012-07-04 11:27:47 +0000842 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000843 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000844 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000845 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000846#endif
847}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000848
849void DesktopWindow::handleFullscreenTimeout(void *data)
850{
851 DesktopWindow *self = (DesktopWindow *)data;
852
853 assert(self);
854
855 self->delayedFullscreen = false;
856
857 if (self->delayedDesktopSize) {
858 self->handleDesktopSize();
859 self->delayedDesktopSize = false;
860 }
861}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000862
863void DesktopWindow::handleEdgeScroll(void *data)
864{
865#ifdef HAVE_FLTK_FULLSCREEN
866 DesktopWindow *self = (DesktopWindow *)data;
867
868 int mx, my;
869 int dx, dy;
870
871 assert(self);
872
873 if (!self->fullscreen_active())
874 return;
875
876 mx = Fl::event_x();
877 my = Fl::event_y();
878
879 dx = dy = 0;
880
881 // Clamp mouse position in case it is outside the window
882 if (mx < 0)
883 mx = 0;
884 if (mx > self->w())
885 mx = self->w();
886 if (my < 0)
887 my = 0;
888 if (my > self->h())
889 my = self->h();
890
891 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000892 dx = EDGE_SCROLL_SPEED -
893 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
894 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
895 (mx > self->w() - EDGE_SCROLL_SIZE))
896 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
897 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000898 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000899 dy = EDGE_SCROLL_SPEED -
900 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
901 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
902 (my > self->h() - EDGE_SCROLL_SIZE))
903 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
904 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000905
906 if ((dx == 0) && (dy == 0))
907 return;
908
909 // Make sure we don't move the viewport too much
910 if (self->viewport->x() + dx > 0)
911 dx = -self->viewport->x();
912 if (self->viewport->x() + dx + self->viewport->w() < self->w())
913 dx = self->w() - (self->viewport->x() + self->viewport->w());
914 if (self->viewport->y() + dy > 0)
915 dy = -self->viewport->y();
916 if (self->viewport->y() + dy + self->viewport->h() < self->h())
917 dy = self->h() - (self->viewport->y() + self->viewport->h());
918
919 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
920
921 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
922#endif
923}