blob: 122a54dafef07161c914a12268a3dd2b23298059 [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 Ossman5156d5e2011-03-09 09:42:34 +000049using namespace rfb;
50
Pierre Ossman5156d5e2011-03-09 09:42:34 +000051static rfb::LogWriter vlog("DesktopWindow");
52
53DesktopWindow::DesktopWindow(int w, int h, const char *name,
54 const rfb::PixelFormat& serverPF,
55 CConn* cc_)
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000056 : Fl_Window(w, h), cc(cc_), firstUpdate(true),
57 delayedFullscreen(false), delayedDesktopSize(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000058{
Pierre Ossman4ae229f2011-04-15 12:58:31 +000059 Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h);
60 scroll->color(FL_BLACK);
61
62 // Automatically adjust the scroll box to the window
63 resizable(scroll);
64
Pierre Ossmanff473402012-07-04 11:27:47 +000065 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000066
Pierre Ossman4ae229f2011-04-15 12:58:31 +000067 scroll->end();
68
Pierre Ossman5156d5e2011-03-09 09:42:34 +000069 callback(handleClose, this);
70
71 setName(name);
72
Pierre Ossman407a5c32011-05-26 14:48:29 +000073 OptionsDialog::addCallback(handleOptions, this);
74
Pierre Ossmana4f0f182011-06-14 13:36:57 +000075 // Hack. See below...
76 Fl::event_dispatch(&fltkHandle);
77
Pierre Ossman63ca58e2011-05-26 14:59:32 +000078#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000079 if (fullScreen) {
80 // Hack: Window managers seem to be rather crappy at respecting
81 // fullscreen hints on initial windows. So on X11 we'll have to
82 // wait until after we've been mapped.
83#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +000084 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000085#else
86 delayedFullscreen = true;
87#endif
88 } else
Pierre Ossman63ca58e2011-05-26 14:59:32 +000089#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +000090 {
Peter Åstrandb182a9e2012-08-27 07:28:08 +000091
92 int geom_x = 0, geom_y = 0;
93 if (geometry.hasBeenSet()) {
94 int matched;
95 matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
96 if (matched == 2) {
97 force_position(1);
98 } else {
99 int geom_w, geom_h;
100 matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
101 switch (matched) {
102 case 4:
103 force_position(1);
104 /* fall through */
105 case 2:
106 w = geom_w;
107 h = geom_h;
Peter Åstrand63d75cc2012-08-27 08:02:36 +0000108 break;
Peter Åstrandb182a9e2012-08-27 07:28:08 +0000109 default:
Peter Åstrand63d75cc2012-08-27 08:02:36 +0000110 geom_x = geom_y = 0;
Peter Åstrandb182a9e2012-08-27 07:28:08 +0000111 vlog.error("Invalid geometry specified!");
112 }
113 }
114 }
115
Peter Åstrandd62482e2011-08-02 08:33:27 +0000116 // If we are creating a window which is equal to the size on the
117 // screen on X11, many WMs will treat this as a legacy fullscreen
118 // request. This is not what we want. Besides, it doesn't really
119 // make sense to try to create a window which is larger than the
120 // available work space.
Peter Åstrandb182a9e2012-08-27 07:28:08 +0000121 w = __rfbmin(w, Fl::w());
122 h = __rfbmin(h, Fl::h());
123
124 if (force_position()) {
125 resize(geom_x, geom_y, w, h);
126 } else {
127 size(w, h);
128 }
Peter Åstrandd62482e2011-08-02 08:33:27 +0000129 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000130
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000131 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000132
Peter Åstrand49b11572012-08-01 08:09:09 +0000133 // Unfortunately, current FLTK does not allow us to set the
134 // maximized property before showing the window. See STR #2083 and
135 // STR #2178
136 if (maximize) {
137 maximizeWindow();
138 }
139
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000140 // The window manager might give us an initial window size that is different
141 // than the one we requested, and in those cases we need to manually adjust
142 // the scroll widget for things to behave sanely.
143 if ((w != this->w()) || (h != this->h()))
144 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000145
146#ifdef HAVE_FLTK_FULLSCREEN
147 if (delayedFullscreen) {
148 // Hack: Fullscreen requests may be ignored, so we need a timeout for
149 // when we should stop waiting. We also really need to wait for the
150 // resize, which can come after the fullscreen event.
151 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
152 fullscreen_on();
153 }
154#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000155}
156
157
158DesktopWindow::~DesktopWindow()
159{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000160 // Unregister all timeouts in case they get a change tro trigger
161 // again later when this object is already gone.
162 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000163 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000164 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000165
166 OptionsDialog::removeCallback(handleOptions);
167
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000168 // FLTK automatically deletes all child widgets, so we shouldn't touch
169 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000170}
171
172
173void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
174{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000175 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000176}
177
178
179const rfb::PixelFormat &DesktopWindow::getPreferredPF()
180{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000181 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000182}
183
184
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000185void DesktopWindow::setName(const char *name)
186{
187 CharArray windowNameStr;
188 windowNameStr.replaceBuf(new char[256]);
189
Pierre Ossman27820ba2011-09-30 12:54:24 +0000190 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000191
192 copy_label(windowNameStr.buf);
193}
194
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000195
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000196void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
197 rdr::U16* rgbs)
198{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000199 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000200}
201
202
203// Copy the areas of the framebuffer that have been changed (damaged)
204// to the displayed window.
205
206void DesktopWindow::updateWindow()
207{
Pierre Ossmanff473402012-07-04 11:27:47 +0000208 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000209 if (cc->cp.supportsSetDesktopSize) {
210 // Hack: Wait until we're in the proper mode and position until
211 // resizing things, otherwise we might send the wrong thing.
212 if (delayedFullscreen)
213 delayedDesktopSize = true;
214 else
215 handleDesktopSize();
216 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000217 firstUpdate = false;
218 }
219
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000220 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000221}
222
223
Pierre Ossman6455d852011-06-01 09:33:00 +0000224void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
225{
226 if ((new_w == viewport->w()) && (new_h == viewport->h()))
227 return;
228
Pierre Ossman6455d852011-06-01 09:33:00 +0000229 // If we're letting the viewport match the window perfectly, then
230 // keep things that way for the new size, otherwise just keep things
231 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000232#ifdef HAVE_FLTK_FULLSCREEN
233 if (!fullscreen_active()) {
234#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000235 if ((w() == viewport->w()) && (h() == viewport->h()))
236 size(new_w, new_h);
237 else {
238 // Make sure the window isn't too big. We do this manually because
239 // we have to disable the window size restriction (and it isn't
240 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000241 if ((w() > new_w) || (h() > new_h))
242 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000243 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000244#ifdef HAVE_FLTK_FULLSCREEN
245 }
246#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000247
248 viewport->size(new_w, new_h);
249
250 // We might not resize the main window, so we need to manually call this
251 // to make sure the viewport is centered.
252 repositionViewport();
253
Pierre Ossman6455d852011-06-01 09:33:00 +0000254 // repositionViewport() makes sure the scroll widget notices any changes
255 // in position, but it might be just the size that changes so we also
256 // need a poke here as well.
257 redraw();
258}
259
260
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000261void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
262 void* data, void* mask)
263{
264 viewport->setCursor(width, height, hotspot, data, mask);
265}
266
267
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000268void DesktopWindow::resize(int x, int y, int w, int h)
269{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000270 bool resizing;
271
272 if ((this->w() != w) || (this->h() != h))
273 resizing = true;
274 else
275 resizing = false;
276
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000277 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000278
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000279 if (resizing) {
280 // Try to get the remote size to match our window size, provided
281 // the following conditions are true:
282 //
283 // a) The user has this feature turned on
284 // b) The server supports it
285 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000286 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000287 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000288 if (not firstUpdate and not delayedFullscreen and
289 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000290 // We delay updating the remote desktop as we tend to get a flood
291 // of resize events as the user is dragging the window.
292 Fl::remove_timeout(handleResizeTimeout, this);
293 Fl::add_timeout(0.5, handleResizeTimeout, this);
294 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000295
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000296 // Deal with some scrolling corner cases
297 repositionViewport();
298 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000299}
300
301
Pierre Ossman407a5c32011-05-26 14:48:29 +0000302int DesktopWindow::handle(int event)
303{
304 switch (event) {
305#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000306 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000307 fullScreen.setParam(fullscreen_active());
308
Pierre Ossman407a5c32011-05-26 14:48:29 +0000309 if (!fullscreenSystemKeys)
310 break;
311
312 if (fullscreen_active())
313 grabKeyboard();
314 else
315 ungrabKeyboard();
316
317 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000318#endif
319 case FL_SHORTCUT:
320 // Sometimes the focus gets out of whack and we fall through to the
321 // shortcut dispatching. Try to make things sane again...
322 if (Fl::focus() == NULL) {
323 take_focus();
324 Fl::handle(FL_KEYDOWN, this);
325 }
326 return 1;
327 }
328
329 return Fl_Window::handle(event);
330}
331
332
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000333int DesktopWindow::fltkHandle(int event, Fl_Window *win)
334{
335 int ret;
336
337 ret = Fl::handle_(event, win);
338
339#ifdef HAVE_FLTK_FULLSCREEN
340 // This is hackish and the result of the dodgy focus handling in FLTK.
341 // The basic problem is that FLTK's view of focus and the system's tend
342 // to differ, and as a result we do not see all the FL_FOCUS events we
343 // need. Fortunately we can grab them here...
344
345 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
346
347 if (dw && fullscreenSystemKeys) {
348 switch (event) {
349 case FL_FOCUS:
350 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
351 // some issues we need to work around:
352 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
353 // b) Gaining focus on the system level causes FLTK to switch
354 // window level on OS X.
355 if (dw->fullscreen_active())
356 dw->grabKeyboard();
357 break;
358
359 case FL_UNFOCUS:
360 // FIXME: We need to relinquish control when the entire window loses
361 // focus as it is very tied to this specific window on some
362 // platforms and we want to be able to open subwindows.
363 dw->ungrabKeyboard();
364 break;
365 }
366 }
367#endif
368
369 return ret;
370}
371
372
Pierre Ossmanaae38912012-07-13 11:22:55 +0000373void DesktopWindow::fullscreen_on()
374{
375#ifdef HAVE_FLTK_FULLSCREEN
376#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
377 if (not fullScreenAllMonitors)
378 fullscreen_screens(-1, -1, -1, -1);
379 else {
380 int top, bottom, left, right;
381 int top_y, bottom_y, left_x, right_x;
382
383 int sx, sy, sw, sh;
384
385 top = bottom = left = right = 0;
386
387 Fl::screen_xywh(sx, sy, sw, sh, 0);
388 top_y = sy;
389 bottom_y = sy + sh;
390 left_x = sx;
391 right_x = sx + sw;
392
393 for (int i = 1;i < Fl::screen_count();i++) {
394 Fl::screen_xywh(sx, sy, sw, sh, i);
395 if (sy < top_y) {
396 top = i;
397 top_y = sy;
398 }
399 if ((sy + sh) > bottom_y) {
400 bottom = i;
401 bottom_y = sy + sh;
402 }
403 if (sx < left_x) {
404 left = i;
405 left_x = sx;
406 }
407 if ((sx + sw) > right_x) {
408 right = i;
409 right_x = sx + sw;
410 }
411 }
412
413 fullscreen_screens(top, bottom, left, right);
414 }
415#endif // HAVE_FLTK_FULLSCREEN_SCREENS
416
417 fullscreen();
418#endif // HAVE_FLTK_FULLSCREEN
419}
420
Pierre Ossman407a5c32011-05-26 14:48:29 +0000421void DesktopWindow::grabKeyboard()
422{
423 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
424 // correct widget regardless of which low level window got the system
425 // event.
426
427 // FIXME: Push this stuff into FLTK.
428
429#if defined(WIN32)
430 int ret;
431
432 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
433 if (ret != 0)
434 vlog.error(_("Failure grabbing keyboard"));
435#elif defined(__APPLE__)
436 int ret;
437
438 ret = cocoa_capture_display(this);
439 if (ret != 0)
440 vlog.error(_("Failure grabbing keyboard"));
441#else
442 int ret;
443
444 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000445 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000446 if (ret) {
447 if (ret == AlreadyGrabbed) {
448 // It seems like we can race with the WM in some cases.
449 // Try again in a bit.
450 if (!Fl::has_timeout(handleGrab, this))
451 Fl::add_timeout(0.500, handleGrab, this);
452 } else {
453 vlog.error(_("Failure grabbing keyboard"));
454 }
455 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000456
457 // We also need to grab the pointer as some WMs like to grab buttons
458 // combined with modifies (e.g. Alt+Button0 in metacity).
459 ret = XGrabPointer(fl_display, fl_xid(this), True,
460 ButtonPressMask|ButtonReleaseMask|
461 ButtonMotionMask|PointerMotionMask,
462 GrabModeAsync, GrabModeAsync,
463 None, None, CurrentTime);
464 if (ret)
465 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000466#endif
467}
468
469
470void DesktopWindow::ungrabKeyboard()
471{
472 Fl::remove_timeout(handleGrab, this);
473
474#if defined(WIN32)
475 win32_disable_lowlevel_keyboard(fl_xid(this));
476#elif defined(__APPLE__)
477 cocoa_release_display(this);
478#else
479 // FLTK has a grab so lets not mess with it
480 if (Fl::grab())
481 return;
482
Pierre Ossman53fd5442011-11-17 10:19:19 +0000483 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000484 XUngrabKeyboard(fl_display, fl_event_time);
485#endif
486}
487
488
489void DesktopWindow::handleGrab(void *data)
490{
491 DesktopWindow *self = (DesktopWindow*)data;
492
493 assert(self);
494
495#ifdef HAVE_FLTK_FULLSCREEN
496 if (!fullscreenSystemKeys)
497 return;
498 if (!self->fullscreen_active())
499 return;
500
501 self->grabKeyboard();
502#endif
503}
504
505
Peter Åstrand49b11572012-08-01 08:09:09 +0000506#define _NET_WM_STATE_ADD 1 /* add/set property */
507void DesktopWindow::maximizeWindow()
508{
509#if defined(WIN32)
510 WINDOWPLACEMENT wp;
511 wp.length = sizeof(WINDOWPLACEMENT);
512 GetWindowPlacement(fl_xid(this), &wp);
513 wp.showCmd = SW_MAXIMIZE;
514 SetWindowPlacement(fl_xid(this), &wp);
515#elif defined(__APPLE__)
516 /* OS X is somewhat strange and does not really have a concept of a
517 maximized window, so we can simply resize the window to the workarea */
518 int X, Y, W, H;
519 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
520 size(W, H);
521#else
522 // X11
523 fl_open_display();
524 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
525 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
526 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
527
528 XEvent e;
529 e.xany.type = ClientMessage;
530 e.xany.window = fl_xid(this);
531 e.xclient.message_type = net_wm_state;
532 e.xclient.format = 32;
533 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
534 e.xclient.data.l[1] = net_wm_state_maximized_vert;
535 e.xclient.data.l[2] = net_wm_state_maximized_horz;
536 e.xclient.data.l[3] = 0;
537 e.xclient.data.l[4] = 0;
538 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
539#endif
540}
541
542
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000543void DesktopWindow::handleDesktopSize()
544{
545 int width, height;
546
547 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
548 return;
549
550 remoteResize(width, height);
551}
552
553
Pierre Ossmanff473402012-07-04 11:27:47 +0000554void DesktopWindow::handleResizeTimeout(void *data)
555{
556 DesktopWindow *self = (DesktopWindow *)data;
557
558 assert(self);
559
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000560 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000561}
562
563
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000564void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000565{
Pierre Ossmanff473402012-07-04 11:27:47 +0000566 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000567 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000568
Pierre Ossmanaae38912012-07-13 11:22:55 +0000569#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000570 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000571#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000572 // In windowed mode (or the framebuffer is so large that we need
573 // to scroll) we just report a single virtual screen that covers
574 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000575
Pierre Ossmanaae38912012-07-13 11:22:55 +0000576 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000577
Pierre Ossmanaae38912012-07-13 11:22:55 +0000578 // Not sure why we have no screens, but adding a new one should be
579 // safe as there is nothing to conflict with...
580 if (layout.num_screens() == 0)
581 layout.add_screen(rfb::Screen());
582 else if (layout.num_screens() != 1) {
583 // More than one screen. Remove all but the first (which we
584 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000585
Pierre Ossmanaae38912012-07-13 11:22:55 +0000586 while (true) {
587 iter = layout.begin();
588 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000589
Pierre Ossmanaae38912012-07-13 11:22:55 +0000590 if (iter == layout.end())
591 break;
592
593 layout.remove_screen(iter->id);
594 }
595 }
596
597 // Resize the remaining single screen to the complete framebuffer
598 layout.begin()->dimensions.tl.x = 0;
599 layout.begin()->dimensions.tl.y = 0;
600 layout.begin()->dimensions.br.x = width;
601 layout.begin()->dimensions.br.y = height;
602#ifdef HAVE_FLTK_FULLSCREEN
603 } else {
604 int i;
605 rdr::U32 id;
606 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000607 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000608
609 // In full screen we report all screens that are fully covered.
610
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000611 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
612 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000613
Pierre Ossmanaae38912012-07-13 11:22:55 +0000614 // If we can find a matching screen in the existing set, we use
615 // that, otherwise we create a brand new screen.
616 //
617 // FIXME: We should really track screens better so we can handle
618 // a resized one.
619 //
620 for (i = 0;i < Fl::screen_count();i++) {
621 Fl::screen_xywh(sx, sy, sw, sh, i);
622
Pierre Ossman93d2d922012-07-20 12:32:52 +0000623 // Check that the screen is fully inside the framebuffer
624 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000625 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000626 continue;
627
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000628 // Adjust the coordinates so they are relative to our viewport
629 sx -= viewport_rect.tl.x;
630 sy -= viewport_rect.tl.y;
631
Pierre Ossmanaae38912012-07-13 11:22:55 +0000632 // Look for perfectly matching existing screen...
633 for (iter = cc->cp.screenLayout.begin();
634 iter != cc->cp.screenLayout.end(); ++iter) {
635 if ((iter->dimensions.tl.x == sx) &&
636 (iter->dimensions.tl.y == sy) &&
637 (iter->dimensions.width() == sw) &&
638 (iter->dimensions.height() == sh))
639 break;
640 }
641
642 // Found it?
643 if (iter != cc->cp.screenLayout.end()) {
644 layout.add_screen(*iter);
645 continue;
646 }
647
648 // Need to add a new one, which means we need to find an unused id
649 while (true) {
650 id = rand();
651 for (iter = cc->cp.screenLayout.begin();
652 iter != cc->cp.screenLayout.end(); ++iter) {
653 if (iter->id == id)
654 break;
655 }
656
657 if (iter == cc->cp.screenLayout.end())
658 break;
659 }
660
661 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000662 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000663
664 // If the viewport doesn't match a physical screen, then we might
665 // end up with no screens in the layout. Add a fake one...
666 if (layout.num_screens() == 0)
667 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000668 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000669#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000670
671 // Do we actually change anything?
672 if ((width == cc->cp.width) &&
673 (height == cc->cp.height) &&
674 (layout == cc->cp.screenLayout))
675 return;
676
Pierre Ossmanaae38912012-07-13 11:22:55 +0000677 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
678 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
679
680 if (!layout.validate(width, height)) {
681 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000682 return;
683 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000684
685 cc->writer()->writeSetDesktopSize(width, height, layout);
686}
687
688
Pierre Ossman6455d852011-06-01 09:33:00 +0000689void DesktopWindow::repositionViewport()
690{
691 int new_x, new_y;
692
693 // Deal with some scrolling corner cases:
694 //
695 // a) If the window is larger then the viewport, center the viewport.
696 // b) If the window is smaller than the viewport, make sure there is
697 // no wasted space on the sides.
698 //
699 // FIXME: Doesn't compensate for scroll widget size properly.
700
701 new_x = viewport->x();
702 new_y = viewport->y();
703
704 if (w() > viewport->w())
705 new_x = (w() - viewport->w()) / 2;
706 else {
707 if (viewport->x() > 0)
708 new_x = 0;
709 else if (w() > (viewport->x() + viewport->w()))
710 new_x = w() - viewport->w();
711 }
712
713 // Same thing for y axis
714 if (h() > viewport->h())
715 new_y = (h() - viewport->h()) / 2;
716 else {
717 if (viewport->y() > 0)
718 new_y = 0;
719 else if (h() > (viewport->y() + viewport->h()))
720 new_y = h() - viewport->h();
721 }
722
723 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
724 viewport->position(new_x, new_y);
725
726 // The scroll widget does not notice when you move around child widgets,
727 // so redraw everything to make sure things update.
728 redraw();
729 }
730}
731
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000732void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
733{
734 exit_vncviewer();
735}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000736
737
738void DesktopWindow::handleOptions(void *data)
739{
740 DesktopWindow *self = (DesktopWindow*)data;
741
742#ifdef HAVE_FLTK_FULLSCREEN
743 if (self->fullscreen_active() && fullscreenSystemKeys)
744 self->grabKeyboard();
745 else
746 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000747
Pierre Ossmanff473402012-07-04 11:27:47 +0000748 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000749 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000750 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000751 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000752#endif
753}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000754
755void DesktopWindow::handleFullscreenTimeout(void *data)
756{
757 DesktopWindow *self = (DesktopWindow *)data;
758
759 assert(self);
760
761 self->delayedFullscreen = false;
762
763 if (self->delayedDesktopSize) {
764 self->handleDesktopSize();
765 self->delayedDesktopSize = false;
766 }
767}