blob: 378e43cfef7a38d8afec1255b2e10d3eaab5abe0 [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 Ossman2f3a04e2012-10-24 12:15:19 +0000372 // The scroll widget isn't clever enough to actually redraw the
373 // scroll bars when they are added/removed, so we need to give
374 // it a push.
375 scroll->redraw();
376
Pierre Ossman407a5c32011-05-26 14:48:29 +0000377 if (!fullscreenSystemKeys)
378 break;
379
380 if (fullscreen_active())
381 grabKeyboard();
382 else
383 ungrabKeyboard();
384
385 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000386
387 case FL_ENTER:
388 case FL_LEAVE:
389 case FL_DRAG:
390 case FL_MOVE:
391 if (fullscreen_active()) {
392 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
393 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
394 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
395 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
396 if (!Fl::has_timeout(handleEdgeScroll, this))
397 Fl::add_timeout(0.1, handleEdgeScroll, this);
398 }
399 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000400 // Continue processing so that the viewport also gets mouse events
401 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000402#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000403
Pierre Ossman407a5c32011-05-26 14:48:29 +0000404 case FL_SHORTCUT:
405 // Sometimes the focus gets out of whack and we fall through to the
406 // shortcut dispatching. Try to make things sane again...
407 if (Fl::focus() == NULL) {
408 take_focus();
409 Fl::handle(FL_KEYDOWN, this);
410 }
411 return 1;
412 }
413
414 return Fl_Window::handle(event);
415}
416
417
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000418int DesktopWindow::fltkHandle(int event, Fl_Window *win)
419{
420 int ret;
421
422 ret = Fl::handle_(event, win);
423
424#ifdef HAVE_FLTK_FULLSCREEN
425 // This is hackish and the result of the dodgy focus handling in FLTK.
426 // The basic problem is that FLTK's view of focus and the system's tend
427 // to differ, and as a result we do not see all the FL_FOCUS events we
428 // need. Fortunately we can grab them here...
429
430 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
431
432 if (dw && fullscreenSystemKeys) {
433 switch (event) {
434 case FL_FOCUS:
435 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
436 // some issues we need to work around:
437 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
438 // b) Gaining focus on the system level causes FLTK to switch
439 // window level on OS X.
440 if (dw->fullscreen_active())
441 dw->grabKeyboard();
442 break;
443
Pierre Ossmanb1369802012-10-17 07:59:36 +0000444 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000445 // FIXME: We need to relinquish control when the entire window loses
446 // focus as it is very tied to this specific window on some
447 // platforms and we want to be able to open subwindows.
448 dw->ungrabKeyboard();
449 break;
450 }
451 }
452#endif
453
454 return ret;
455}
456
457
Pierre Ossmanaae38912012-07-13 11:22:55 +0000458void DesktopWindow::fullscreen_on()
459{
460#ifdef HAVE_FLTK_FULLSCREEN
461#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
462 if (not fullScreenAllMonitors)
463 fullscreen_screens(-1, -1, -1, -1);
464 else {
465 int top, bottom, left, right;
466 int top_y, bottom_y, left_x, right_x;
467
468 int sx, sy, sw, sh;
469
470 top = bottom = left = right = 0;
471
472 Fl::screen_xywh(sx, sy, sw, sh, 0);
473 top_y = sy;
474 bottom_y = sy + sh;
475 left_x = sx;
476 right_x = sx + sw;
477
478 for (int i = 1;i < Fl::screen_count();i++) {
479 Fl::screen_xywh(sx, sy, sw, sh, i);
480 if (sy < top_y) {
481 top = i;
482 top_y = sy;
483 }
484 if ((sy + sh) > bottom_y) {
485 bottom = i;
486 bottom_y = sy + sh;
487 }
488 if (sx < left_x) {
489 left = i;
490 left_x = sx;
491 }
492 if ((sx + sw) > right_x) {
493 right = i;
494 right_x = sx + sw;
495 }
496 }
497
498 fullscreen_screens(top, bottom, left, right);
499 }
500#endif // HAVE_FLTK_FULLSCREEN_SCREENS
501
502 fullscreen();
503#endif // HAVE_FLTK_FULLSCREEN
504}
505
Pierre Ossman407a5c32011-05-26 14:48:29 +0000506void DesktopWindow::grabKeyboard()
507{
508 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
509 // correct widget regardless of which low level window got the system
510 // event.
511
512 // FIXME: Push this stuff into FLTK.
513
514#if defined(WIN32)
515 int ret;
516
517 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
518 if (ret != 0)
519 vlog.error(_("Failure grabbing keyboard"));
520#elif defined(__APPLE__)
521 int ret;
522
Pierre Ossman3d759112012-08-27 14:40:51 +0000523 ret = cocoa_capture_display(this,
524#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
525 fullScreenAllMonitors
526#else
527 false
528#endif
529 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000530 if (ret != 0)
531 vlog.error(_("Failure grabbing keyboard"));
532#else
533 int ret;
534
535 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000536 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000537 if (ret) {
538 if (ret == AlreadyGrabbed) {
539 // It seems like we can race with the WM in some cases.
540 // Try again in a bit.
541 if (!Fl::has_timeout(handleGrab, this))
542 Fl::add_timeout(0.500, handleGrab, this);
543 } else {
544 vlog.error(_("Failure grabbing keyboard"));
545 }
546 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000547
548 // We also need to grab the pointer as some WMs like to grab buttons
549 // combined with modifies (e.g. Alt+Button0 in metacity).
550 ret = XGrabPointer(fl_display, fl_xid(this), True,
551 ButtonPressMask|ButtonReleaseMask|
552 ButtonMotionMask|PointerMotionMask,
553 GrabModeAsync, GrabModeAsync,
554 None, None, CurrentTime);
555 if (ret)
556 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000557#endif
558}
559
560
561void DesktopWindow::ungrabKeyboard()
562{
563 Fl::remove_timeout(handleGrab, this);
564
565#if defined(WIN32)
566 win32_disable_lowlevel_keyboard(fl_xid(this));
567#elif defined(__APPLE__)
568 cocoa_release_display(this);
569#else
570 // FLTK has a grab so lets not mess with it
571 if (Fl::grab())
572 return;
573
Pierre Ossman53fd5442011-11-17 10:19:19 +0000574 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000575 XUngrabKeyboard(fl_display, fl_event_time);
576#endif
577}
578
579
580void DesktopWindow::handleGrab(void *data)
581{
582 DesktopWindow *self = (DesktopWindow*)data;
583
584 assert(self);
585
586#ifdef HAVE_FLTK_FULLSCREEN
587 if (!fullscreenSystemKeys)
588 return;
589 if (!self->fullscreen_active())
590 return;
591
592 self->grabKeyboard();
593#endif
594}
595
596
Peter Åstrand49b11572012-08-01 08:09:09 +0000597#define _NET_WM_STATE_ADD 1 /* add/set property */
598void DesktopWindow::maximizeWindow()
599{
600#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000601 // We cannot use ShowWindow() in full screen mode as it will
602 // resize things implicitly. Fortunately modifying the style
603 // directly results in a maximized state once we leave full screen.
604#ifdef HAVE_FLTK_FULLSCREEN
605 if (fullscreen_active()) {
606 WINDOWINFO wi;
607 wi.cbSize = sizeof(WINDOWINFO);
608 GetWindowInfo(fl_xid(this), &wi);
609 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
610 } else
611#endif
612 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000613#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000614 // OS X is somewhat strange and does not really have a concept of a
615 // maximized window, so we can simply resize the window to the workarea.
616 // Note that we shouldn't do this whilst in full screen as that will
617 // incorrectly adjust things.
618#ifdef HAVE_FLTK_FULLSCREEN
619 if (fullscreen_active())
620 return;
621#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000622 int X, Y, W, H;
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000623#ifdef HAVE_FLTK_WORK_AREA
Peter Åstrand49b11572012-08-01 08:09:09 +0000624 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000625#else
626 W = Fl::w();
627 H = Fl::h();
628#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000629 size(W, H);
630#else
631 // X11
632 fl_open_display();
633 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
634 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
635 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
636
637 XEvent e;
638 e.xany.type = ClientMessage;
639 e.xany.window = fl_xid(this);
640 e.xclient.message_type = net_wm_state;
641 e.xclient.format = 32;
642 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
643 e.xclient.data.l[1] = net_wm_state_maximized_vert;
644 e.xclient.data.l[2] = net_wm_state_maximized_horz;
645 e.xclient.data.l[3] = 0;
646 e.xclient.data.l[4] = 0;
647 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
648#endif
649}
650
651
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000652void DesktopWindow::handleDesktopSize()
653{
654 int width, height;
655
656 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
657 return;
658
659 remoteResize(width, height);
660}
661
662
Pierre Ossmanff473402012-07-04 11:27:47 +0000663void DesktopWindow::handleResizeTimeout(void *data)
664{
665 DesktopWindow *self = (DesktopWindow *)data;
666
667 assert(self);
668
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000669 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000670}
671
672
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000673void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000674{
Pierre Ossmanff473402012-07-04 11:27:47 +0000675 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000676 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000677
Pierre Ossmanaae38912012-07-13 11:22:55 +0000678#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000679 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000680#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000681 // In windowed mode (or the framebuffer is so large that we need
682 // to scroll) we just report a single virtual screen that covers
683 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000684
Pierre Ossmanaae38912012-07-13 11:22:55 +0000685 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000686
Pierre Ossmanaae38912012-07-13 11:22:55 +0000687 // Not sure why we have no screens, but adding a new one should be
688 // safe as there is nothing to conflict with...
689 if (layout.num_screens() == 0)
690 layout.add_screen(rfb::Screen());
691 else if (layout.num_screens() != 1) {
692 // More than one screen. Remove all but the first (which we
693 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000694
Pierre Ossmanaae38912012-07-13 11:22:55 +0000695 while (true) {
696 iter = layout.begin();
697 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000698
Pierre Ossmanaae38912012-07-13 11:22:55 +0000699 if (iter == layout.end())
700 break;
701
702 layout.remove_screen(iter->id);
703 }
704 }
705
706 // Resize the remaining single screen to the complete framebuffer
707 layout.begin()->dimensions.tl.x = 0;
708 layout.begin()->dimensions.tl.y = 0;
709 layout.begin()->dimensions.br.x = width;
710 layout.begin()->dimensions.br.y = height;
711#ifdef HAVE_FLTK_FULLSCREEN
712 } else {
713 int i;
714 rdr::U32 id;
715 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000716 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000717
718 // In full screen we report all screens that are fully covered.
719
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000720 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
721 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000722
Pierre Ossmanaae38912012-07-13 11:22:55 +0000723 // If we can find a matching screen in the existing set, we use
724 // that, otherwise we create a brand new screen.
725 //
726 // FIXME: We should really track screens better so we can handle
727 // a resized one.
728 //
729 for (i = 0;i < Fl::screen_count();i++) {
730 Fl::screen_xywh(sx, sy, sw, sh, i);
731
Pierre Ossman93d2d922012-07-20 12:32:52 +0000732 // Check that the screen is fully inside the framebuffer
733 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000734 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000735 continue;
736
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000737 // Adjust the coordinates so they are relative to our viewport
738 sx -= viewport_rect.tl.x;
739 sy -= viewport_rect.tl.y;
740
Pierre Ossmanaae38912012-07-13 11:22:55 +0000741 // Look for perfectly matching existing screen...
742 for (iter = cc->cp.screenLayout.begin();
743 iter != cc->cp.screenLayout.end(); ++iter) {
744 if ((iter->dimensions.tl.x == sx) &&
745 (iter->dimensions.tl.y == sy) &&
746 (iter->dimensions.width() == sw) &&
747 (iter->dimensions.height() == sh))
748 break;
749 }
750
751 // Found it?
752 if (iter != cc->cp.screenLayout.end()) {
753 layout.add_screen(*iter);
754 continue;
755 }
756
757 // Need to add a new one, which means we need to find an unused id
758 while (true) {
759 id = rand();
760 for (iter = cc->cp.screenLayout.begin();
761 iter != cc->cp.screenLayout.end(); ++iter) {
762 if (iter->id == id)
763 break;
764 }
765
766 if (iter == cc->cp.screenLayout.end())
767 break;
768 }
769
770 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000771 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000772
773 // If the viewport doesn't match a physical screen, then we might
774 // end up with no screens in the layout. Add a fake one...
775 if (layout.num_screens() == 0)
776 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000777 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000778#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000779
780 // Do we actually change anything?
781 if ((width == cc->cp.width) &&
782 (height == cc->cp.height) &&
783 (layout == cc->cp.screenLayout))
784 return;
785
Pierre Ossmanaae38912012-07-13 11:22:55 +0000786 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
787 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
788
789 if (!layout.validate(width, height)) {
790 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000791 return;
792 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000793
794 cc->writer()->writeSetDesktopSize(width, height, layout);
795}
796
797
Pierre Ossman6455d852011-06-01 09:33:00 +0000798void DesktopWindow::repositionViewport()
799{
800 int new_x, new_y;
801
802 // Deal with some scrolling corner cases:
803 //
804 // a) If the window is larger then the viewport, center the viewport.
805 // b) If the window is smaller than the viewport, make sure there is
806 // no wasted space on the sides.
807 //
808 // FIXME: Doesn't compensate for scroll widget size properly.
809
810 new_x = viewport->x();
811 new_y = viewport->y();
812
813 if (w() > viewport->w())
814 new_x = (w() - viewport->w()) / 2;
815 else {
816 if (viewport->x() > 0)
817 new_x = 0;
818 else if (w() > (viewport->x() + viewport->w()))
819 new_x = w() - viewport->w();
820 }
821
822 // Same thing for y axis
823 if (h() > viewport->h())
824 new_y = (h() - viewport->h()) / 2;
825 else {
826 if (viewport->y() > 0)
827 new_y = 0;
828 else if (h() > (viewport->y() + viewport->h()))
829 new_y = h() - viewport->h();
830 }
831
832 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
833 viewport->position(new_x, new_y);
834
835 // The scroll widget does not notice when you move around child widgets,
836 // so redraw everything to make sure things update.
837 redraw();
838 }
839}
840
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000841void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
842{
843 exit_vncviewer();
844}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000845
846
847void DesktopWindow::handleOptions(void *data)
848{
849 DesktopWindow *self = (DesktopWindow*)data;
850
851#ifdef HAVE_FLTK_FULLSCREEN
852 if (self->fullscreen_active() && fullscreenSystemKeys)
853 self->grabKeyboard();
854 else
855 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000856
Pierre Ossmanff473402012-07-04 11:27:47 +0000857 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000858 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000859 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000860 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000861#endif
862}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000863
864void DesktopWindow::handleFullscreenTimeout(void *data)
865{
866 DesktopWindow *self = (DesktopWindow *)data;
867
868 assert(self);
869
870 self->delayedFullscreen = false;
871
872 if (self->delayedDesktopSize) {
873 self->handleDesktopSize();
874 self->delayedDesktopSize = false;
875 }
876}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000877
878void DesktopWindow::handleEdgeScroll(void *data)
879{
880#ifdef HAVE_FLTK_FULLSCREEN
881 DesktopWindow *self = (DesktopWindow *)data;
882
883 int mx, my;
884 int dx, dy;
885
886 assert(self);
887
888 if (!self->fullscreen_active())
889 return;
890
891 mx = Fl::event_x();
892 my = Fl::event_y();
893
894 dx = dy = 0;
895
896 // Clamp mouse position in case it is outside the window
897 if (mx < 0)
898 mx = 0;
899 if (mx > self->w())
900 mx = self->w();
901 if (my < 0)
902 my = 0;
903 if (my > self->h())
904 my = self->h();
905
906 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000907 dx = EDGE_SCROLL_SPEED -
908 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
909 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
910 (mx > self->w() - EDGE_SCROLL_SIZE))
911 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
912 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000913 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000914 dy = EDGE_SCROLL_SPEED -
915 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
916 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
917 (my > self->h() - EDGE_SCROLL_SIZE))
918 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
919 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000920
921 if ((dx == 0) && (dy == 0))
922 return;
923
924 // Make sure we don't move the viewport too much
925 if (self->viewport->x() + dx > 0)
926 dx = -self->viewport->x();
927 if (self->viewport->x() + dx + self->viewport->w() < self->w())
928 dx = self->w() - (self->viewport->x() + self->viewport->w());
929 if (self->viewport->y() + dy > 0)
930 dy = -self->viewport->y();
931 if (self->viewport->y() + dy + self->viewport->h() < self->h())
932 dy = self->h() - (self->viewport->y() + self->viewport->h());
933
934 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
935
936 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
937#endif
938}