blob: 04addba2ac698ad9497a749d54321a81f00f952a [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:
96 force_position(1);
97 /* fall through */
98 case 2:
99 w = geom_w;
100 h = geom_h;
101 break;
102 default:
103 geom_x = geom_y = 0;
104 vlog.error("Invalid geometry specified!");
105 }
106 }
107 }
108
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000109 if (force_position()) {
110 resize(geom_x, geom_y, w, h);
111 } else {
112 size(w, h);
113 }
114
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000115#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000116 if (fullScreen) {
117 // Hack: Window managers seem to be rather crappy at respecting
118 // fullscreen hints on initial windows. So on X11 we'll have to
119 // wait until after we've been mapped.
120#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000121 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000122#else
123 delayedFullscreen = true;
124#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000125 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000126#endif
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000127
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000128 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000129
Peter Åstrand49b11572012-08-01 08:09:09 +0000130 // Unfortunately, current FLTK does not allow us to set the
131 // maximized property before showing the window. See STR #2083 and
132 // STR #2178
133 if (maximize) {
134 maximizeWindow();
135 }
136
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000137 // The window manager might give us an initial window size that is different
138 // than the one we requested, and in those cases we need to manually adjust
139 // the scroll widget for things to behave sanely.
140 if ((w != this->w()) || (h != this->h()))
141 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000142
143#ifdef HAVE_FLTK_FULLSCREEN
144 if (delayedFullscreen) {
145 // Hack: Fullscreen requests may be ignored, so we need a timeout for
146 // when we should stop waiting. We also really need to wait for the
147 // resize, which can come after the fullscreen event.
148 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
149 fullscreen_on();
150 }
151#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000152}
153
154
155DesktopWindow::~DesktopWindow()
156{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000157 // Unregister all timeouts in case they get a change tro trigger
158 // again later when this object is already gone.
159 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000160 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000161 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000162 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000163
164 OptionsDialog::removeCallback(handleOptions);
165
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000166 // FLTK automatically deletes all child widgets, so we shouldn't touch
167 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000168}
169
170
171void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
172{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000173 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000174}
175
176
177const rfb::PixelFormat &DesktopWindow::getPreferredPF()
178{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000179 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000180}
181
182
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000183void DesktopWindow::setName(const char *name)
184{
185 CharArray windowNameStr;
186 windowNameStr.replaceBuf(new char[256]);
187
Pierre Ossman27820ba2011-09-30 12:54:24 +0000188 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000189
190 copy_label(windowNameStr.buf);
191}
192
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000193
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000194void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
195 rdr::U16* rgbs)
196{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000197 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000198}
199
200
201// Copy the areas of the framebuffer that have been changed (damaged)
202// to the displayed window.
203
204void DesktopWindow::updateWindow()
205{
Pierre Ossmanff473402012-07-04 11:27:47 +0000206 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000207 if (cc->cp.supportsSetDesktopSize) {
208 // Hack: Wait until we're in the proper mode and position until
209 // resizing things, otherwise we might send the wrong thing.
210 if (delayedFullscreen)
211 delayedDesktopSize = true;
212 else
213 handleDesktopSize();
214 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000215 firstUpdate = false;
216 }
217
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000218 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000219}
220
221
Pierre Ossman6455d852011-06-01 09:33:00 +0000222void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
223{
224 if ((new_w == viewport->w()) && (new_h == viewport->h()))
225 return;
226
Pierre Ossman6455d852011-06-01 09:33:00 +0000227 // If we're letting the viewport match the window perfectly, then
228 // keep things that way for the new size, otherwise just keep things
229 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000230#ifdef HAVE_FLTK_FULLSCREEN
231 if (!fullscreen_active()) {
232#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000233 if ((w() == viewport->w()) && (h() == viewport->h()))
234 size(new_w, new_h);
235 else {
236 // Make sure the window isn't too big. We do this manually because
237 // we have to disable the window size restriction (and it isn't
238 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000239 if ((w() > new_w) || (h() > new_h))
240 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000241 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000242#ifdef HAVE_FLTK_FULLSCREEN
243 }
244#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000245
246 viewport->size(new_w, new_h);
247
248 // We might not resize the main window, so we need to manually call this
249 // to make sure the viewport is centered.
250 repositionViewport();
251
Pierre Ossman6455d852011-06-01 09:33:00 +0000252 // repositionViewport() makes sure the scroll widget notices any changes
253 // in position, but it might be just the size that changes so we also
254 // need a poke here as well.
255 redraw();
256}
257
258
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000259void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
260 void* data, void* mask)
261{
262 viewport->setCursor(width, height, hotspot, data, mask);
263}
264
265
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000266void DesktopWindow::resize(int x, int y, int w, int h)
267{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000268 bool resizing;
269
Pierre Ossman0e593462012-09-03 09:43:23 +0000270#if ! (defined(WIN32) || defined(__APPLE__))
271 // X11 window managers will treat a resize to cover the entire
272 // monitor as a request to go full screen. Make sure we avoid this.
273 //
274 // FIXME: If this is an external event then this code will get
275 // FLTK into a confused state about the window's position/size.
276 // Unfortunately FLTK doesn't offer an easy way to tell
277 // what kind of resize it is. Let's hope this corner case
278 // isn't too common...
279#ifdef HAVE_FLTK_FULLSCREEN
280 if (!fullscreen_active())
281#endif
282 {
283 for (int i = 0;i < Fl::screen_count();i++) {
284 int sx, sy, sw, sh;
285
286 Fl::screen_xywh(sx, sy, sw, sh, i);
287
288 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
289 vlog.info("Adjusting window size to avoid accidental full screen request");
290 // Assume a panel of some form and adjust the height
291 y += 20;
292 h -= 40;
293 }
294 }
295 }
296#endif
297
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000298 if ((this->w() != w) || (this->h() != h))
299 resizing = true;
300 else
301 resizing = false;
302
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000303 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000304
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000305 if (resizing) {
306 // Try to get the remote size to match our window size, provided
307 // the following conditions are true:
308 //
309 // a) The user has this feature turned on
310 // b) The server supports it
311 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000312 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000313 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000314 if (not firstUpdate and not delayedFullscreen and
315 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000316 // We delay updating the remote desktop as we tend to get a flood
317 // of resize events as the user is dragging the window.
318 Fl::remove_timeout(handleResizeTimeout, this);
319 Fl::add_timeout(0.5, handleResizeTimeout, this);
320 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000321
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000322 // Deal with some scrolling corner cases
323 repositionViewport();
324 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000325}
326
327
Pierre Ossman407a5c32011-05-26 14:48:29 +0000328int DesktopWindow::handle(int event)
329{
330 switch (event) {
331#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000332 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000333 fullScreen.setParam(fullscreen_active());
334
Pierre Ossman1d867b62012-09-03 09:25:07 +0000335 if (fullscreen_active())
336 scroll->type(0);
337 else
338 scroll->type(Fl_Scroll::BOTH);
339
Pierre Ossman407a5c32011-05-26 14:48:29 +0000340 if (!fullscreenSystemKeys)
341 break;
342
343 if (fullscreen_active())
344 grabKeyboard();
345 else
346 ungrabKeyboard();
347
348 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000349
350 case FL_ENTER:
351 case FL_LEAVE:
352 case FL_DRAG:
353 case FL_MOVE:
354 if (fullscreen_active()) {
355 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
356 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
357 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
358 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
359 if (!Fl::has_timeout(handleEdgeScroll, this))
360 Fl::add_timeout(0.1, handleEdgeScroll, this);
361 }
362 }
363 return 1;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000364#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000365
Pierre Ossman407a5c32011-05-26 14:48:29 +0000366 case FL_SHORTCUT:
367 // Sometimes the focus gets out of whack and we fall through to the
368 // shortcut dispatching. Try to make things sane again...
369 if (Fl::focus() == NULL) {
370 take_focus();
371 Fl::handle(FL_KEYDOWN, this);
372 }
373 return 1;
374 }
375
376 return Fl_Window::handle(event);
377}
378
379
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000380int DesktopWindow::fltkHandle(int event, Fl_Window *win)
381{
382 int ret;
383
384 ret = Fl::handle_(event, win);
385
386#ifdef HAVE_FLTK_FULLSCREEN
387 // This is hackish and the result of the dodgy focus handling in FLTK.
388 // The basic problem is that FLTK's view of focus and the system's tend
389 // to differ, and as a result we do not see all the FL_FOCUS events we
390 // need. Fortunately we can grab them here...
391
392 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
393
394 if (dw && fullscreenSystemKeys) {
395 switch (event) {
396 case FL_FOCUS:
397 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
398 // some issues we need to work around:
399 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
400 // b) Gaining focus on the system level causes FLTK to switch
401 // window level on OS X.
402 if (dw->fullscreen_active())
403 dw->grabKeyboard();
404 break;
405
406 case FL_UNFOCUS:
407 // FIXME: We need to relinquish control when the entire window loses
408 // focus as it is very tied to this specific window on some
409 // platforms and we want to be able to open subwindows.
410 dw->ungrabKeyboard();
411 break;
412 }
413 }
414#endif
415
416 return ret;
417}
418
419
Pierre Ossmanaae38912012-07-13 11:22:55 +0000420void DesktopWindow::fullscreen_on()
421{
422#ifdef HAVE_FLTK_FULLSCREEN
423#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
424 if (not fullScreenAllMonitors)
425 fullscreen_screens(-1, -1, -1, -1);
426 else {
427 int top, bottom, left, right;
428 int top_y, bottom_y, left_x, right_x;
429
430 int sx, sy, sw, sh;
431
432 top = bottom = left = right = 0;
433
434 Fl::screen_xywh(sx, sy, sw, sh, 0);
435 top_y = sy;
436 bottom_y = sy + sh;
437 left_x = sx;
438 right_x = sx + sw;
439
440 for (int i = 1;i < Fl::screen_count();i++) {
441 Fl::screen_xywh(sx, sy, sw, sh, i);
442 if (sy < top_y) {
443 top = i;
444 top_y = sy;
445 }
446 if ((sy + sh) > bottom_y) {
447 bottom = i;
448 bottom_y = sy + sh;
449 }
450 if (sx < left_x) {
451 left = i;
452 left_x = sx;
453 }
454 if ((sx + sw) > right_x) {
455 right = i;
456 right_x = sx + sw;
457 }
458 }
459
460 fullscreen_screens(top, bottom, left, right);
461 }
462#endif // HAVE_FLTK_FULLSCREEN_SCREENS
463
464 fullscreen();
465#endif // HAVE_FLTK_FULLSCREEN
466}
467
Pierre Ossman407a5c32011-05-26 14:48:29 +0000468void DesktopWindow::grabKeyboard()
469{
470 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
471 // correct widget regardless of which low level window got the system
472 // event.
473
474 // FIXME: Push this stuff into FLTK.
475
476#if defined(WIN32)
477 int ret;
478
479 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
480 if (ret != 0)
481 vlog.error(_("Failure grabbing keyboard"));
482#elif defined(__APPLE__)
483 int ret;
484
Pierre Ossman3d759112012-08-27 14:40:51 +0000485 ret = cocoa_capture_display(this,
486#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
487 fullScreenAllMonitors
488#else
489 false
490#endif
491 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000492 if (ret != 0)
493 vlog.error(_("Failure grabbing keyboard"));
494#else
495 int ret;
496
497 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000498 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000499 if (ret) {
500 if (ret == AlreadyGrabbed) {
501 // It seems like we can race with the WM in some cases.
502 // Try again in a bit.
503 if (!Fl::has_timeout(handleGrab, this))
504 Fl::add_timeout(0.500, handleGrab, this);
505 } else {
506 vlog.error(_("Failure grabbing keyboard"));
507 }
508 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000509
510 // We also need to grab the pointer as some WMs like to grab buttons
511 // combined with modifies (e.g. Alt+Button0 in metacity).
512 ret = XGrabPointer(fl_display, fl_xid(this), True,
513 ButtonPressMask|ButtonReleaseMask|
514 ButtonMotionMask|PointerMotionMask,
515 GrabModeAsync, GrabModeAsync,
516 None, None, CurrentTime);
517 if (ret)
518 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000519#endif
520}
521
522
523void DesktopWindow::ungrabKeyboard()
524{
525 Fl::remove_timeout(handleGrab, this);
526
527#if defined(WIN32)
528 win32_disable_lowlevel_keyboard(fl_xid(this));
529#elif defined(__APPLE__)
530 cocoa_release_display(this);
531#else
532 // FLTK has a grab so lets not mess with it
533 if (Fl::grab())
534 return;
535
Pierre Ossman53fd5442011-11-17 10:19:19 +0000536 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000537 XUngrabKeyboard(fl_display, fl_event_time);
538#endif
539}
540
541
542void DesktopWindow::handleGrab(void *data)
543{
544 DesktopWindow *self = (DesktopWindow*)data;
545
546 assert(self);
547
548#ifdef HAVE_FLTK_FULLSCREEN
549 if (!fullscreenSystemKeys)
550 return;
551 if (!self->fullscreen_active())
552 return;
553
554 self->grabKeyboard();
555#endif
556}
557
558
Peter Åstrand49b11572012-08-01 08:09:09 +0000559#define _NET_WM_STATE_ADD 1 /* add/set property */
560void DesktopWindow::maximizeWindow()
561{
562#if defined(WIN32)
563 WINDOWPLACEMENT wp;
564 wp.length = sizeof(WINDOWPLACEMENT);
565 GetWindowPlacement(fl_xid(this), &wp);
566 wp.showCmd = SW_MAXIMIZE;
567 SetWindowPlacement(fl_xid(this), &wp);
568#elif defined(__APPLE__)
569 /* OS X is somewhat strange and does not really have a concept of a
570 maximized window, so we can simply resize the window to the workarea */
571 int X, Y, W, H;
572 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
573 size(W, H);
574#else
575 // X11
576 fl_open_display();
577 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
578 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
579 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
580
581 XEvent e;
582 e.xany.type = ClientMessage;
583 e.xany.window = fl_xid(this);
584 e.xclient.message_type = net_wm_state;
585 e.xclient.format = 32;
586 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
587 e.xclient.data.l[1] = net_wm_state_maximized_vert;
588 e.xclient.data.l[2] = net_wm_state_maximized_horz;
589 e.xclient.data.l[3] = 0;
590 e.xclient.data.l[4] = 0;
591 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
592#endif
593}
594
595
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000596void DesktopWindow::handleDesktopSize()
597{
598 int width, height;
599
600 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
601 return;
602
603 remoteResize(width, height);
604}
605
606
Pierre Ossmanff473402012-07-04 11:27:47 +0000607void DesktopWindow::handleResizeTimeout(void *data)
608{
609 DesktopWindow *self = (DesktopWindow *)data;
610
611 assert(self);
612
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000613 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000614}
615
616
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000617void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000618{
Pierre Ossmanff473402012-07-04 11:27:47 +0000619 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000620 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000621
Pierre Ossmanaae38912012-07-13 11:22:55 +0000622#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000623 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000624#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000625 // In windowed mode (or the framebuffer is so large that we need
626 // to scroll) we just report a single virtual screen that covers
627 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000628
Pierre Ossmanaae38912012-07-13 11:22:55 +0000629 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000630
Pierre Ossmanaae38912012-07-13 11:22:55 +0000631 // Not sure why we have no screens, but adding a new one should be
632 // safe as there is nothing to conflict with...
633 if (layout.num_screens() == 0)
634 layout.add_screen(rfb::Screen());
635 else if (layout.num_screens() != 1) {
636 // More than one screen. Remove all but the first (which we
637 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000638
Pierre Ossmanaae38912012-07-13 11:22:55 +0000639 while (true) {
640 iter = layout.begin();
641 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000642
Pierre Ossmanaae38912012-07-13 11:22:55 +0000643 if (iter == layout.end())
644 break;
645
646 layout.remove_screen(iter->id);
647 }
648 }
649
650 // Resize the remaining single screen to the complete framebuffer
651 layout.begin()->dimensions.tl.x = 0;
652 layout.begin()->dimensions.tl.y = 0;
653 layout.begin()->dimensions.br.x = width;
654 layout.begin()->dimensions.br.y = height;
655#ifdef HAVE_FLTK_FULLSCREEN
656 } else {
657 int i;
658 rdr::U32 id;
659 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000660 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000661
662 // In full screen we report all screens that are fully covered.
663
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000664 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
665 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000666
Pierre Ossmanaae38912012-07-13 11:22:55 +0000667 // If we can find a matching screen in the existing set, we use
668 // that, otherwise we create a brand new screen.
669 //
670 // FIXME: We should really track screens better so we can handle
671 // a resized one.
672 //
673 for (i = 0;i < Fl::screen_count();i++) {
674 Fl::screen_xywh(sx, sy, sw, sh, i);
675
Pierre Ossman93d2d922012-07-20 12:32:52 +0000676 // Check that the screen is fully inside the framebuffer
677 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000678 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000679 continue;
680
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000681 // Adjust the coordinates so they are relative to our viewport
682 sx -= viewport_rect.tl.x;
683 sy -= viewport_rect.tl.y;
684
Pierre Ossmanaae38912012-07-13 11:22:55 +0000685 // Look for perfectly matching existing screen...
686 for (iter = cc->cp.screenLayout.begin();
687 iter != cc->cp.screenLayout.end(); ++iter) {
688 if ((iter->dimensions.tl.x == sx) &&
689 (iter->dimensions.tl.y == sy) &&
690 (iter->dimensions.width() == sw) &&
691 (iter->dimensions.height() == sh))
692 break;
693 }
694
695 // Found it?
696 if (iter != cc->cp.screenLayout.end()) {
697 layout.add_screen(*iter);
698 continue;
699 }
700
701 // Need to add a new one, which means we need to find an unused id
702 while (true) {
703 id = rand();
704 for (iter = cc->cp.screenLayout.begin();
705 iter != cc->cp.screenLayout.end(); ++iter) {
706 if (iter->id == id)
707 break;
708 }
709
710 if (iter == cc->cp.screenLayout.end())
711 break;
712 }
713
714 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000715 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000716
717 // If the viewport doesn't match a physical screen, then we might
718 // end up with no screens in the layout. Add a fake one...
719 if (layout.num_screens() == 0)
720 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000721 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000722#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000723
724 // Do we actually change anything?
725 if ((width == cc->cp.width) &&
726 (height == cc->cp.height) &&
727 (layout == cc->cp.screenLayout))
728 return;
729
Pierre Ossmanaae38912012-07-13 11:22:55 +0000730 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
731 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
732
733 if (!layout.validate(width, height)) {
734 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000735 return;
736 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000737
738 cc->writer()->writeSetDesktopSize(width, height, layout);
739}
740
741
Pierre Ossman6455d852011-06-01 09:33:00 +0000742void DesktopWindow::repositionViewport()
743{
744 int new_x, new_y;
745
746 // Deal with some scrolling corner cases:
747 //
748 // a) If the window is larger then the viewport, center the viewport.
749 // b) If the window is smaller than the viewport, make sure there is
750 // no wasted space on the sides.
751 //
752 // FIXME: Doesn't compensate for scroll widget size properly.
753
754 new_x = viewport->x();
755 new_y = viewport->y();
756
757 if (w() > viewport->w())
758 new_x = (w() - viewport->w()) / 2;
759 else {
760 if (viewport->x() > 0)
761 new_x = 0;
762 else if (w() > (viewport->x() + viewport->w()))
763 new_x = w() - viewport->w();
764 }
765
766 // Same thing for y axis
767 if (h() > viewport->h())
768 new_y = (h() - viewport->h()) / 2;
769 else {
770 if (viewport->y() > 0)
771 new_y = 0;
772 else if (h() > (viewport->y() + viewport->h()))
773 new_y = h() - viewport->h();
774 }
775
776 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
777 viewport->position(new_x, new_y);
778
779 // The scroll widget does not notice when you move around child widgets,
780 // so redraw everything to make sure things update.
781 redraw();
782 }
783}
784
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000785void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
786{
787 exit_vncviewer();
788}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000789
790
791void DesktopWindow::handleOptions(void *data)
792{
793 DesktopWindow *self = (DesktopWindow*)data;
794
795#ifdef HAVE_FLTK_FULLSCREEN
796 if (self->fullscreen_active() && fullscreenSystemKeys)
797 self->grabKeyboard();
798 else
799 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000800
Pierre Ossmanff473402012-07-04 11:27:47 +0000801 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000802 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000803 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000804 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000805#endif
806}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000807
808void DesktopWindow::handleFullscreenTimeout(void *data)
809{
810 DesktopWindow *self = (DesktopWindow *)data;
811
812 assert(self);
813
814 self->delayedFullscreen = false;
815
816 if (self->delayedDesktopSize) {
817 self->handleDesktopSize();
818 self->delayedDesktopSize = false;
819 }
820}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000821
822void DesktopWindow::handleEdgeScroll(void *data)
823{
824#ifdef HAVE_FLTK_FULLSCREEN
825 DesktopWindow *self = (DesktopWindow *)data;
826
827 int mx, my;
828 int dx, dy;
829
830 assert(self);
831
832 if (!self->fullscreen_active())
833 return;
834
835 mx = Fl::event_x();
836 my = Fl::event_y();
837
838 dx = dy = 0;
839
840 // Clamp mouse position in case it is outside the window
841 if (mx < 0)
842 mx = 0;
843 if (mx > self->w())
844 mx = self->w();
845 if (my < 0)
846 my = 0;
847 if (my > self->h())
848 my = self->h();
849
850 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
851 dx = EDGE_SCROLL_SPEED - EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
852 if ((self->viewport->x() + self->viewport->w() > self->w()) && (mx > self->w() - EDGE_SCROLL_SIZE))
853 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE - EDGE_SCROLL_SPEED;
854 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
855 dy = EDGE_SCROLL_SPEED - EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
856 if ((self->viewport->y() + self->viewport->h() > self->h()) && (my > self->h() - EDGE_SCROLL_SIZE))
857 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE - EDGE_SCROLL_SPEED;
858
859 if ((dx == 0) && (dy == 0))
860 return;
861
862 // Make sure we don't move the viewport too much
863 if (self->viewport->x() + dx > 0)
864 dx = -self->viewport->x();
865 if (self->viewport->x() + dx + self->viewport->w() < self->w())
866 dx = self->w() - (self->viewport->x() + self->viewport->w());
867 if (self->viewport->y() + dy > 0)
868 dy = -self->viewport->y();
869 if (self->viewport->y() + dy + self->viewport->h() < self->h())
870 dy = self->h() - (self->viewport->y() + self->viewport->h());
871
872 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
873
874 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
875#endif
876}