blob: 40d08ae2d71b420aebff6a6b92ae538c85be0c9d [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 Ossman1f884e02012-10-30 10:26:23 +0000300 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000301
Pierre Ossman1f884e02012-10-30 10:26:23 +0000302 // If there is no X11 window, then this must be a resize request,
303 // not a notification from the X server.
304 if (!shown())
305 resize_req = true;
306 else {
307 // Otherwise we need to get the real window coordinates to tell
308 // the difference
309 XWindowAttributes actual;
310 Window cr;
311 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000312
Pierre Ossman1f884e02012-10-30 10:26:23 +0000313 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
314 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
315 0, 0, &wx, &wy, &cr);
316
317 // Actual resize request?
318 if ((wx != x) || (wy != y) ||
319 (actual.width != w) || (actual.height != h))
320 resize_req = true;
321 else
322 resize_req = false;
323 }
324
325 if (resize_req) {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000326 for (int i = 0;i < Fl::screen_count();i++) {
327 int sx, sy, sw, sh;
328
329 Fl::screen_xywh(sx, sy, sw, sh, i);
330
331 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
332 vlog.info("Adjusting window size to avoid accidental full screen request");
333 // Assume a panel of some form and adjust the height
334 y += 20;
335 h -= 40;
336 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000337 }
338 }
339 }
340#endif
341
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000342 if ((this->w() != w) || (this->h() != h))
343 resizing = true;
344 else
345 resizing = false;
346
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000347 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000348
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000349 if (resizing) {
350 // Try to get the remote size to match our window size, provided
351 // the following conditions are true:
352 //
353 // a) The user has this feature turned on
354 // b) The server supports it
355 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000356 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000357 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000358 if (not firstUpdate and not delayedFullscreen and
359 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000360 // We delay updating the remote desktop as we tend to get a flood
361 // of resize events as the user is dragging the window.
362 Fl::remove_timeout(handleResizeTimeout, this);
363 Fl::add_timeout(0.5, handleResizeTimeout, this);
364 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000365
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000366 // Deal with some scrolling corner cases
367 repositionViewport();
368 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000369}
370
371
Pierre Ossman407a5c32011-05-26 14:48:29 +0000372int DesktopWindow::handle(int event)
373{
374 switch (event) {
375#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000376 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000377 fullScreen.setParam(fullscreen_active());
378
Pierre Ossman1d867b62012-09-03 09:25:07 +0000379 if (fullscreen_active())
380 scroll->type(0);
381 else
382 scroll->type(Fl_Scroll::BOTH);
383
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000384 // The scroll widget isn't clever enough to actually redraw the
385 // scroll bars when they are added/removed, so we need to give
386 // it a push.
387 scroll->redraw();
388
Pierre Ossman407a5c32011-05-26 14:48:29 +0000389 if (!fullscreenSystemKeys)
390 break;
391
392 if (fullscreen_active())
393 grabKeyboard();
394 else
395 ungrabKeyboard();
396
397 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000398
399 case FL_ENTER:
400 case FL_LEAVE:
401 case FL_DRAG:
402 case FL_MOVE:
403 if (fullscreen_active()) {
404 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
405 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
406 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
407 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
408 if (!Fl::has_timeout(handleEdgeScroll, this))
409 Fl::add_timeout(0.1, handleEdgeScroll, this);
410 }
411 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000412 // Continue processing so that the viewport also gets mouse events
413 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000414#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000415
Pierre Ossman407a5c32011-05-26 14:48:29 +0000416 case FL_SHORTCUT:
417 // Sometimes the focus gets out of whack and we fall through to the
418 // shortcut dispatching. Try to make things sane again...
419 if (Fl::focus() == NULL) {
420 take_focus();
421 Fl::handle(FL_KEYDOWN, this);
422 }
423 return 1;
424 }
425
426 return Fl_Window::handle(event);
427}
428
429
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000430int DesktopWindow::fltkHandle(int event, Fl_Window *win)
431{
432 int ret;
433
434 ret = Fl::handle_(event, win);
435
436#ifdef HAVE_FLTK_FULLSCREEN
437 // This is hackish and the result of the dodgy focus handling in FLTK.
438 // The basic problem is that FLTK's view of focus and the system's tend
439 // to differ, and as a result we do not see all the FL_FOCUS events we
440 // need. Fortunately we can grab them here...
441
442 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
443
444 if (dw && fullscreenSystemKeys) {
445 switch (event) {
446 case FL_FOCUS:
447 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
448 // some issues we need to work around:
449 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
450 // b) Gaining focus on the system level causes FLTK to switch
451 // window level on OS X.
452 if (dw->fullscreen_active())
453 dw->grabKeyboard();
454 break;
455
Pierre Ossmanb1369802012-10-17 07:59:36 +0000456 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000457 // FIXME: We need to relinquish control when the entire window loses
458 // focus as it is very tied to this specific window on some
459 // platforms and we want to be able to open subwindows.
460 dw->ungrabKeyboard();
461 break;
462 }
463 }
464#endif
465
466 return ret;
467}
468
469
Pierre Ossmanaae38912012-07-13 11:22:55 +0000470void DesktopWindow::fullscreen_on()
471{
472#ifdef HAVE_FLTK_FULLSCREEN
473#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
474 if (not fullScreenAllMonitors)
475 fullscreen_screens(-1, -1, -1, -1);
476 else {
477 int top, bottom, left, right;
478 int top_y, bottom_y, left_x, right_x;
479
480 int sx, sy, sw, sh;
481
482 top = bottom = left = right = 0;
483
484 Fl::screen_xywh(sx, sy, sw, sh, 0);
485 top_y = sy;
486 bottom_y = sy + sh;
487 left_x = sx;
488 right_x = sx + sw;
489
490 for (int i = 1;i < Fl::screen_count();i++) {
491 Fl::screen_xywh(sx, sy, sw, sh, i);
492 if (sy < top_y) {
493 top = i;
494 top_y = sy;
495 }
496 if ((sy + sh) > bottom_y) {
497 bottom = i;
498 bottom_y = sy + sh;
499 }
500 if (sx < left_x) {
501 left = i;
502 left_x = sx;
503 }
504 if ((sx + sw) > right_x) {
505 right = i;
506 right_x = sx + sw;
507 }
508 }
509
510 fullscreen_screens(top, bottom, left, right);
511 }
512#endif // HAVE_FLTK_FULLSCREEN_SCREENS
513
514 fullscreen();
515#endif // HAVE_FLTK_FULLSCREEN
516}
517
Pierre Ossman407a5c32011-05-26 14:48:29 +0000518void DesktopWindow::grabKeyboard()
519{
520 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
521 // correct widget regardless of which low level window got the system
522 // event.
523
524 // FIXME: Push this stuff into FLTK.
525
526#if defined(WIN32)
527 int ret;
528
529 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
530 if (ret != 0)
531 vlog.error(_("Failure grabbing keyboard"));
532#elif defined(__APPLE__)
533 int ret;
534
Pierre Ossman3d759112012-08-27 14:40:51 +0000535 ret = cocoa_capture_display(this,
536#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
537 fullScreenAllMonitors
538#else
539 false
540#endif
541 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000542 if (ret != 0)
543 vlog.error(_("Failure grabbing keyboard"));
544#else
545 int ret;
546
547 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000548 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000549 if (ret) {
550 if (ret == AlreadyGrabbed) {
551 // It seems like we can race with the WM in some cases.
552 // Try again in a bit.
553 if (!Fl::has_timeout(handleGrab, this))
554 Fl::add_timeout(0.500, handleGrab, this);
555 } else {
556 vlog.error(_("Failure grabbing keyboard"));
557 }
558 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000559
560 // We also need to grab the pointer as some WMs like to grab buttons
561 // combined with modifies (e.g. Alt+Button0 in metacity).
562 ret = XGrabPointer(fl_display, fl_xid(this), True,
563 ButtonPressMask|ButtonReleaseMask|
564 ButtonMotionMask|PointerMotionMask,
565 GrabModeAsync, GrabModeAsync,
566 None, None, CurrentTime);
567 if (ret)
568 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000569#endif
570}
571
572
573void DesktopWindow::ungrabKeyboard()
574{
575 Fl::remove_timeout(handleGrab, this);
576
577#if defined(WIN32)
578 win32_disable_lowlevel_keyboard(fl_xid(this));
579#elif defined(__APPLE__)
580 cocoa_release_display(this);
581#else
582 // FLTK has a grab so lets not mess with it
583 if (Fl::grab())
584 return;
585
Pierre Ossman53fd5442011-11-17 10:19:19 +0000586 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000587 XUngrabKeyboard(fl_display, fl_event_time);
588#endif
589}
590
591
592void DesktopWindow::handleGrab(void *data)
593{
594 DesktopWindow *self = (DesktopWindow*)data;
595
596 assert(self);
597
598#ifdef HAVE_FLTK_FULLSCREEN
599 if (!fullscreenSystemKeys)
600 return;
601 if (!self->fullscreen_active())
602 return;
603
604 self->grabKeyboard();
605#endif
606}
607
608
Peter Åstrand49b11572012-08-01 08:09:09 +0000609#define _NET_WM_STATE_ADD 1 /* add/set property */
610void DesktopWindow::maximizeWindow()
611{
612#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000613 // We cannot use ShowWindow() in full screen mode as it will
614 // resize things implicitly. Fortunately modifying the style
615 // directly results in a maximized state once we leave full screen.
616#ifdef HAVE_FLTK_FULLSCREEN
617 if (fullscreen_active()) {
618 WINDOWINFO wi;
619 wi.cbSize = sizeof(WINDOWINFO);
620 GetWindowInfo(fl_xid(this), &wi);
621 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
622 } else
623#endif
624 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000625#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000626 // OS X is somewhat strange and does not really have a concept of a
627 // maximized window, so we can simply resize the window to the workarea.
628 // Note that we shouldn't do this whilst in full screen as that will
629 // incorrectly adjust things.
630#ifdef HAVE_FLTK_FULLSCREEN
631 if (fullscreen_active())
632 return;
633#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000634 int X, Y, W, H;
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000635#ifdef HAVE_FLTK_WORK_AREA
Peter Åstrand49b11572012-08-01 08:09:09 +0000636 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000637#else
638 W = Fl::w();
639 H = Fl::h();
640#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000641 size(W, H);
642#else
643 // X11
644 fl_open_display();
645 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
646 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
647 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
648
649 XEvent e;
650 e.xany.type = ClientMessage;
651 e.xany.window = fl_xid(this);
652 e.xclient.message_type = net_wm_state;
653 e.xclient.format = 32;
654 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
655 e.xclient.data.l[1] = net_wm_state_maximized_vert;
656 e.xclient.data.l[2] = net_wm_state_maximized_horz;
657 e.xclient.data.l[3] = 0;
658 e.xclient.data.l[4] = 0;
659 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
660#endif
661}
662
663
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000664void DesktopWindow::handleDesktopSize()
665{
666 int width, height;
667
668 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
669 return;
670
671 remoteResize(width, height);
672}
673
674
Pierre Ossmanff473402012-07-04 11:27:47 +0000675void DesktopWindow::handleResizeTimeout(void *data)
676{
677 DesktopWindow *self = (DesktopWindow *)data;
678
679 assert(self);
680
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000681 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000682}
683
684
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000685void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000686{
Pierre Ossmanff473402012-07-04 11:27:47 +0000687 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000688 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000689
Pierre Ossmanaae38912012-07-13 11:22:55 +0000690#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000691 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000692#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000693 // In windowed mode (or the framebuffer is so large that we need
694 // to scroll) we just report a single virtual screen that covers
695 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000696
Pierre Ossmanaae38912012-07-13 11:22:55 +0000697 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000698
Pierre Ossmanaae38912012-07-13 11:22:55 +0000699 // Not sure why we have no screens, but adding a new one should be
700 // safe as there is nothing to conflict with...
701 if (layout.num_screens() == 0)
702 layout.add_screen(rfb::Screen());
703 else if (layout.num_screens() != 1) {
704 // More than one screen. Remove all but the first (which we
705 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000706
Pierre Ossmanaae38912012-07-13 11:22:55 +0000707 while (true) {
708 iter = layout.begin();
709 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000710
Pierre Ossmanaae38912012-07-13 11:22:55 +0000711 if (iter == layout.end())
712 break;
713
714 layout.remove_screen(iter->id);
715 }
716 }
717
718 // Resize the remaining single screen to the complete framebuffer
719 layout.begin()->dimensions.tl.x = 0;
720 layout.begin()->dimensions.tl.y = 0;
721 layout.begin()->dimensions.br.x = width;
722 layout.begin()->dimensions.br.y = height;
723#ifdef HAVE_FLTK_FULLSCREEN
724 } else {
725 int i;
726 rdr::U32 id;
727 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000728 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000729
730 // In full screen we report all screens that are fully covered.
731
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000732 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
733 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000734
Pierre Ossmanaae38912012-07-13 11:22:55 +0000735 // If we can find a matching screen in the existing set, we use
736 // that, otherwise we create a brand new screen.
737 //
738 // FIXME: We should really track screens better so we can handle
739 // a resized one.
740 //
741 for (i = 0;i < Fl::screen_count();i++) {
742 Fl::screen_xywh(sx, sy, sw, sh, i);
743
Pierre Ossman93d2d922012-07-20 12:32:52 +0000744 // Check that the screen is fully inside the framebuffer
745 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000746 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000747 continue;
748
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000749 // Adjust the coordinates so they are relative to our viewport
750 sx -= viewport_rect.tl.x;
751 sy -= viewport_rect.tl.y;
752
Pierre Ossmanaae38912012-07-13 11:22:55 +0000753 // Look for perfectly matching existing screen...
754 for (iter = cc->cp.screenLayout.begin();
755 iter != cc->cp.screenLayout.end(); ++iter) {
756 if ((iter->dimensions.tl.x == sx) &&
757 (iter->dimensions.tl.y == sy) &&
758 (iter->dimensions.width() == sw) &&
759 (iter->dimensions.height() == sh))
760 break;
761 }
762
763 // Found it?
764 if (iter != cc->cp.screenLayout.end()) {
765 layout.add_screen(*iter);
766 continue;
767 }
768
769 // Need to add a new one, which means we need to find an unused id
770 while (true) {
771 id = rand();
772 for (iter = cc->cp.screenLayout.begin();
773 iter != cc->cp.screenLayout.end(); ++iter) {
774 if (iter->id == id)
775 break;
776 }
777
778 if (iter == cc->cp.screenLayout.end())
779 break;
780 }
781
782 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000783 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000784
785 // If the viewport doesn't match a physical screen, then we might
786 // end up with no screens in the layout. Add a fake one...
787 if (layout.num_screens() == 0)
788 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000789 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000790#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000791
792 // Do we actually change anything?
793 if ((width == cc->cp.width) &&
794 (height == cc->cp.height) &&
795 (layout == cc->cp.screenLayout))
796 return;
797
Pierre Ossmanaae38912012-07-13 11:22:55 +0000798 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
799 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
800
801 if (!layout.validate(width, height)) {
802 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000803 return;
804 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000805
806 cc->writer()->writeSetDesktopSize(width, height, layout);
807}
808
809
Pierre Ossman6455d852011-06-01 09:33:00 +0000810void DesktopWindow::repositionViewport()
811{
812 int new_x, new_y;
813
814 // Deal with some scrolling corner cases:
815 //
816 // a) If the window is larger then the viewport, center the viewport.
817 // b) If the window is smaller than the viewport, make sure there is
818 // no wasted space on the sides.
819 //
820 // FIXME: Doesn't compensate for scroll widget size properly.
821
822 new_x = viewport->x();
823 new_y = viewport->y();
824
825 if (w() > viewport->w())
826 new_x = (w() - viewport->w()) / 2;
827 else {
828 if (viewport->x() > 0)
829 new_x = 0;
830 else if (w() > (viewport->x() + viewport->w()))
831 new_x = w() - viewport->w();
832 }
833
834 // Same thing for y axis
835 if (h() > viewport->h())
836 new_y = (h() - viewport->h()) / 2;
837 else {
838 if (viewport->y() > 0)
839 new_y = 0;
840 else if (h() > (viewport->y() + viewport->h()))
841 new_y = h() - viewport->h();
842 }
843
844 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
845 viewport->position(new_x, new_y);
846
847 // The scroll widget does not notice when you move around child widgets,
848 // so redraw everything to make sure things update.
849 redraw();
850 }
851}
852
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000853void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
854{
855 exit_vncviewer();
856}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000857
858
859void DesktopWindow::handleOptions(void *data)
860{
861 DesktopWindow *self = (DesktopWindow*)data;
862
863#ifdef HAVE_FLTK_FULLSCREEN
864 if (self->fullscreen_active() && fullscreenSystemKeys)
865 self->grabKeyboard();
866 else
867 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000868
Pierre Ossmanff473402012-07-04 11:27:47 +0000869 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000870 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000871 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000872 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000873#endif
874}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000875
876void DesktopWindow::handleFullscreenTimeout(void *data)
877{
878 DesktopWindow *self = (DesktopWindow *)data;
879
880 assert(self);
881
882 self->delayedFullscreen = false;
883
884 if (self->delayedDesktopSize) {
885 self->handleDesktopSize();
886 self->delayedDesktopSize = false;
887 }
888}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000889
890void DesktopWindow::handleEdgeScroll(void *data)
891{
892#ifdef HAVE_FLTK_FULLSCREEN
893 DesktopWindow *self = (DesktopWindow *)data;
894
895 int mx, my;
896 int dx, dy;
897
898 assert(self);
899
900 if (!self->fullscreen_active())
901 return;
902
903 mx = Fl::event_x();
904 my = Fl::event_y();
905
906 dx = dy = 0;
907
908 // Clamp mouse position in case it is outside the window
909 if (mx < 0)
910 mx = 0;
911 if (mx > self->w())
912 mx = self->w();
913 if (my < 0)
914 my = 0;
915 if (my > self->h())
916 my = self->h();
917
918 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000919 dx = EDGE_SCROLL_SPEED -
920 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
921 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
922 (mx > self->w() - EDGE_SCROLL_SIZE))
923 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
924 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000925 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000926 dy = EDGE_SCROLL_SPEED -
927 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
928 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
929 (my > self->h() - EDGE_SCROLL_SIZE))
930 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
931 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000932
933 if ((dx == 0) && (dy == 0))
934 return;
935
936 // Make sure we don't move the viewport too much
937 if (self->viewport->x() + dx > 0)
938 dx = -self->viewport->x();
939 if (self->viewport->x() + dx + self->viewport->w() < self->w())
940 dx = self->w() - (self->viewport->x() + self->viewport->w());
941 if (self->viewport->y() + dy > 0)
942 dy = -self->viewport->y();
943 if (self->viewport->y() + dy + self->viewport->h() < self->h())
944 dy = self->h() - (self->viewport->y() + self->viewport->h());
945
946 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
947
948 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
949#endif
950}