blob: d6c31e1b0610143ab420678370f739562c79e818 [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
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000078 // Support for -geometry option. Note that although we do support
79 // negative coordinates, we do not support -XOFF-YOFF (ie
80 // coordinates relative to the right edge / bottom edge) at this
81 // time.
82 int geom_x = 0, geom_y = 0;
83 if (geometry.hasBeenSet()) {
84 int matched;
85 matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
86 if (matched == 2) {
87 force_position(1);
88 } else {
89 int geom_w, geom_h;
90 matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
91 switch (matched) {
92 case 4:
93 force_position(1);
94 /* fall through */
95 case 2:
96 w = geom_w;
97 h = geom_h;
98 break;
99 default:
100 geom_x = geom_y = 0;
101 vlog.error("Invalid geometry specified!");
102 }
103 }
104 }
105
106 // If we are creating a window which is equal to the size on the
107 // screen on X11, many WMs will treat this as a legacy fullscreen
108 // request. This is not what we want. Besides, it doesn't really
109 // make sense to try to create a window which is larger than the
110 // available work space.
111 w = __rfbmin(w, Fl::w());
112 h = __rfbmin(h, Fl::h());
113
114 if (force_position()) {
115 resize(geom_x, geom_y, w, h);
116 } else {
117 size(w, h);
118 }
119
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000120#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000121 if (fullScreen) {
122 // Hack: Window managers seem to be rather crappy at respecting
123 // fullscreen hints on initial windows. So on X11 we'll have to
124 // wait until after we've been mapped.
125#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000126 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000127#else
128 delayedFullscreen = true;
129#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000130 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000131#endif
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000132
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000133 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000134
Peter Åstrand49b11572012-08-01 08:09:09 +0000135 // Unfortunately, current FLTK does not allow us to set the
136 // maximized property before showing the window. See STR #2083 and
137 // STR #2178
138 if (maximize) {
139 maximizeWindow();
140 }
141
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000142 // The window manager might give us an initial window size that is different
143 // than the one we requested, and in those cases we need to manually adjust
144 // the scroll widget for things to behave sanely.
145 if ((w != this->w()) || (h != this->h()))
146 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000147
148#ifdef HAVE_FLTK_FULLSCREEN
149 if (delayedFullscreen) {
150 // Hack: Fullscreen requests may be ignored, so we need a timeout for
151 // when we should stop waiting. We also really need to wait for the
152 // resize, which can come after the fullscreen event.
153 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
154 fullscreen_on();
155 }
156#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000157}
158
159
160DesktopWindow::~DesktopWindow()
161{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000162 // Unregister all timeouts in case they get a change tro trigger
163 // again later when this object is already gone.
164 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000165 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000166 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000167
168 OptionsDialog::removeCallback(handleOptions);
169
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000170 // FLTK automatically deletes all child widgets, so we shouldn't touch
171 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000172}
173
174
175void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
176{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000177 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000178}
179
180
181const rfb::PixelFormat &DesktopWindow::getPreferredPF()
182{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000183 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000184}
185
186
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000187void DesktopWindow::setName(const char *name)
188{
189 CharArray windowNameStr;
190 windowNameStr.replaceBuf(new char[256]);
191
Pierre Ossman27820ba2011-09-30 12:54:24 +0000192 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000193
194 copy_label(windowNameStr.buf);
195}
196
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000197
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000198void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
199 rdr::U16* rgbs)
200{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000201 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000202}
203
204
205// Copy the areas of the framebuffer that have been changed (damaged)
206// to the displayed window.
207
208void DesktopWindow::updateWindow()
209{
Pierre Ossmanff473402012-07-04 11:27:47 +0000210 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000211 if (cc->cp.supportsSetDesktopSize) {
212 // Hack: Wait until we're in the proper mode and position until
213 // resizing things, otherwise we might send the wrong thing.
214 if (delayedFullscreen)
215 delayedDesktopSize = true;
216 else
217 handleDesktopSize();
218 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000219 firstUpdate = false;
220 }
221
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000222 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000223}
224
225
Pierre Ossman6455d852011-06-01 09:33:00 +0000226void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
227{
228 if ((new_w == viewport->w()) && (new_h == viewport->h()))
229 return;
230
Pierre Ossman6455d852011-06-01 09:33:00 +0000231 // If we're letting the viewport match the window perfectly, then
232 // keep things that way for the new size, otherwise just keep things
233 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000234#ifdef HAVE_FLTK_FULLSCREEN
235 if (!fullscreen_active()) {
236#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000237 if ((w() == viewport->w()) && (h() == viewport->h()))
238 size(new_w, new_h);
239 else {
240 // Make sure the window isn't too big. We do this manually because
241 // we have to disable the window size restriction (and it isn't
242 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000243 if ((w() > new_w) || (h() > new_h))
244 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000245 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000246#ifdef HAVE_FLTK_FULLSCREEN
247 }
248#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000249
250 viewport->size(new_w, new_h);
251
252 // We might not resize the main window, so we need to manually call this
253 // to make sure the viewport is centered.
254 repositionViewport();
255
Pierre Ossman6455d852011-06-01 09:33:00 +0000256 // repositionViewport() makes sure the scroll widget notices any changes
257 // in position, but it might be just the size that changes so we also
258 // need a poke here as well.
259 redraw();
260}
261
262
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000263void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
264 void* data, void* mask)
265{
266 viewport->setCursor(width, height, hotspot, data, mask);
267}
268
269
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000270void DesktopWindow::resize(int x, int y, int w, int h)
271{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000272 bool resizing;
273
274 if ((this->w() != w) || (this->h() != h))
275 resizing = true;
276 else
277 resizing = false;
278
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000279 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000280
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000281 if (resizing) {
282 // Try to get the remote size to match our window size, provided
283 // the following conditions are true:
284 //
285 // a) The user has this feature turned on
286 // b) The server supports it
287 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000288 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000289 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000290 if (not firstUpdate and not delayedFullscreen and
291 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000292 // We delay updating the remote desktop as we tend to get a flood
293 // of resize events as the user is dragging the window.
294 Fl::remove_timeout(handleResizeTimeout, this);
295 Fl::add_timeout(0.5, handleResizeTimeout, this);
296 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000297
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000298 // Deal with some scrolling corner cases
299 repositionViewport();
300 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000301}
302
303
Pierre Ossman407a5c32011-05-26 14:48:29 +0000304int DesktopWindow::handle(int event)
305{
306 switch (event) {
307#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000308 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000309 fullScreen.setParam(fullscreen_active());
310
Pierre Ossman407a5c32011-05-26 14:48:29 +0000311 if (!fullscreenSystemKeys)
312 break;
313
314 if (fullscreen_active())
315 grabKeyboard();
316 else
317 ungrabKeyboard();
318
319 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000320#endif
321 case FL_SHORTCUT:
322 // Sometimes the focus gets out of whack and we fall through to the
323 // shortcut dispatching. Try to make things sane again...
324 if (Fl::focus() == NULL) {
325 take_focus();
326 Fl::handle(FL_KEYDOWN, this);
327 }
328 return 1;
329 }
330
331 return Fl_Window::handle(event);
332}
333
334
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000335int DesktopWindow::fltkHandle(int event, Fl_Window *win)
336{
337 int ret;
338
339 ret = Fl::handle_(event, win);
340
341#ifdef HAVE_FLTK_FULLSCREEN
342 // This is hackish and the result of the dodgy focus handling in FLTK.
343 // The basic problem is that FLTK's view of focus and the system's tend
344 // to differ, and as a result we do not see all the FL_FOCUS events we
345 // need. Fortunately we can grab them here...
346
347 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
348
349 if (dw && fullscreenSystemKeys) {
350 switch (event) {
351 case FL_FOCUS:
352 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
353 // some issues we need to work around:
354 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
355 // b) Gaining focus on the system level causes FLTK to switch
356 // window level on OS X.
357 if (dw->fullscreen_active())
358 dw->grabKeyboard();
359 break;
360
361 case FL_UNFOCUS:
362 // FIXME: We need to relinquish control when the entire window loses
363 // focus as it is very tied to this specific window on some
364 // platforms and we want to be able to open subwindows.
365 dw->ungrabKeyboard();
366 break;
367 }
368 }
369#endif
370
371 return ret;
372}
373
374
Pierre Ossmanaae38912012-07-13 11:22:55 +0000375void DesktopWindow::fullscreen_on()
376{
377#ifdef HAVE_FLTK_FULLSCREEN
378#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
379 if (not fullScreenAllMonitors)
380 fullscreen_screens(-1, -1, -1, -1);
381 else {
382 int top, bottom, left, right;
383 int top_y, bottom_y, left_x, right_x;
384
385 int sx, sy, sw, sh;
386
387 top = bottom = left = right = 0;
388
389 Fl::screen_xywh(sx, sy, sw, sh, 0);
390 top_y = sy;
391 bottom_y = sy + sh;
392 left_x = sx;
393 right_x = sx + sw;
394
395 for (int i = 1;i < Fl::screen_count();i++) {
396 Fl::screen_xywh(sx, sy, sw, sh, i);
397 if (sy < top_y) {
398 top = i;
399 top_y = sy;
400 }
401 if ((sy + sh) > bottom_y) {
402 bottom = i;
403 bottom_y = sy + sh;
404 }
405 if (sx < left_x) {
406 left = i;
407 left_x = sx;
408 }
409 if ((sx + sw) > right_x) {
410 right = i;
411 right_x = sx + sw;
412 }
413 }
414
415 fullscreen_screens(top, bottom, left, right);
416 }
417#endif // HAVE_FLTK_FULLSCREEN_SCREENS
418
419 fullscreen();
420#endif // HAVE_FLTK_FULLSCREEN
421}
422
Pierre Ossman407a5c32011-05-26 14:48:29 +0000423void DesktopWindow::grabKeyboard()
424{
425 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
426 // correct widget regardless of which low level window got the system
427 // event.
428
429 // FIXME: Push this stuff into FLTK.
430
431#if defined(WIN32)
432 int ret;
433
434 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
435 if (ret != 0)
436 vlog.error(_("Failure grabbing keyboard"));
437#elif defined(__APPLE__)
438 int ret;
439
Pierre Ossman3d759112012-08-27 14:40:51 +0000440 ret = cocoa_capture_display(this,
441#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
442 fullScreenAllMonitors
443#else
444 false
445#endif
446 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000447 if (ret != 0)
448 vlog.error(_("Failure grabbing keyboard"));
449#else
450 int ret;
451
452 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000453 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000454 if (ret) {
455 if (ret == AlreadyGrabbed) {
456 // It seems like we can race with the WM in some cases.
457 // Try again in a bit.
458 if (!Fl::has_timeout(handleGrab, this))
459 Fl::add_timeout(0.500, handleGrab, this);
460 } else {
461 vlog.error(_("Failure grabbing keyboard"));
462 }
463 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000464
465 // We also need to grab the pointer as some WMs like to grab buttons
466 // combined with modifies (e.g. Alt+Button0 in metacity).
467 ret = XGrabPointer(fl_display, fl_xid(this), True,
468 ButtonPressMask|ButtonReleaseMask|
469 ButtonMotionMask|PointerMotionMask,
470 GrabModeAsync, GrabModeAsync,
471 None, None, CurrentTime);
472 if (ret)
473 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000474#endif
475}
476
477
478void DesktopWindow::ungrabKeyboard()
479{
480 Fl::remove_timeout(handleGrab, this);
481
482#if defined(WIN32)
483 win32_disable_lowlevel_keyboard(fl_xid(this));
484#elif defined(__APPLE__)
485 cocoa_release_display(this);
486#else
487 // FLTK has a grab so lets not mess with it
488 if (Fl::grab())
489 return;
490
Pierre Ossman53fd5442011-11-17 10:19:19 +0000491 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000492 XUngrabKeyboard(fl_display, fl_event_time);
493#endif
494}
495
496
497void DesktopWindow::handleGrab(void *data)
498{
499 DesktopWindow *self = (DesktopWindow*)data;
500
501 assert(self);
502
503#ifdef HAVE_FLTK_FULLSCREEN
504 if (!fullscreenSystemKeys)
505 return;
506 if (!self->fullscreen_active())
507 return;
508
509 self->grabKeyboard();
510#endif
511}
512
513
Peter Åstrand49b11572012-08-01 08:09:09 +0000514#define _NET_WM_STATE_ADD 1 /* add/set property */
515void DesktopWindow::maximizeWindow()
516{
517#if defined(WIN32)
518 WINDOWPLACEMENT wp;
519 wp.length = sizeof(WINDOWPLACEMENT);
520 GetWindowPlacement(fl_xid(this), &wp);
521 wp.showCmd = SW_MAXIMIZE;
522 SetWindowPlacement(fl_xid(this), &wp);
523#elif defined(__APPLE__)
524 /* OS X is somewhat strange and does not really have a concept of a
525 maximized window, so we can simply resize the window to the workarea */
526 int X, Y, W, H;
527 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
528 size(W, H);
529#else
530 // X11
531 fl_open_display();
532 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
533 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
534 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
535
536 XEvent e;
537 e.xany.type = ClientMessage;
538 e.xany.window = fl_xid(this);
539 e.xclient.message_type = net_wm_state;
540 e.xclient.format = 32;
541 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
542 e.xclient.data.l[1] = net_wm_state_maximized_vert;
543 e.xclient.data.l[2] = net_wm_state_maximized_horz;
544 e.xclient.data.l[3] = 0;
545 e.xclient.data.l[4] = 0;
546 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
547#endif
548}
549
550
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000551void DesktopWindow::handleDesktopSize()
552{
553 int width, height;
554
555 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
556 return;
557
558 remoteResize(width, height);
559}
560
561
Pierre Ossmanff473402012-07-04 11:27:47 +0000562void DesktopWindow::handleResizeTimeout(void *data)
563{
564 DesktopWindow *self = (DesktopWindow *)data;
565
566 assert(self);
567
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000568 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000569}
570
571
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000572void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000573{
Pierre Ossmanff473402012-07-04 11:27:47 +0000574 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000575 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000576
Pierre Ossmanaae38912012-07-13 11:22:55 +0000577#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000578 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000579#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000580 // In windowed mode (or the framebuffer is so large that we need
581 // to scroll) we just report a single virtual screen that covers
582 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000583
Pierre Ossmanaae38912012-07-13 11:22:55 +0000584 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000585
Pierre Ossmanaae38912012-07-13 11:22:55 +0000586 // Not sure why we have no screens, but adding a new one should be
587 // safe as there is nothing to conflict with...
588 if (layout.num_screens() == 0)
589 layout.add_screen(rfb::Screen());
590 else if (layout.num_screens() != 1) {
591 // More than one screen. Remove all but the first (which we
592 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000593
Pierre Ossmanaae38912012-07-13 11:22:55 +0000594 while (true) {
595 iter = layout.begin();
596 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000597
Pierre Ossmanaae38912012-07-13 11:22:55 +0000598 if (iter == layout.end())
599 break;
600
601 layout.remove_screen(iter->id);
602 }
603 }
604
605 // Resize the remaining single screen to the complete framebuffer
606 layout.begin()->dimensions.tl.x = 0;
607 layout.begin()->dimensions.tl.y = 0;
608 layout.begin()->dimensions.br.x = width;
609 layout.begin()->dimensions.br.y = height;
610#ifdef HAVE_FLTK_FULLSCREEN
611 } else {
612 int i;
613 rdr::U32 id;
614 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000615 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000616
617 // In full screen we report all screens that are fully covered.
618
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000619 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
620 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000621
Pierre Ossmanaae38912012-07-13 11:22:55 +0000622 // If we can find a matching screen in the existing set, we use
623 // that, otherwise we create a brand new screen.
624 //
625 // FIXME: We should really track screens better so we can handle
626 // a resized one.
627 //
628 for (i = 0;i < Fl::screen_count();i++) {
629 Fl::screen_xywh(sx, sy, sw, sh, i);
630
Pierre Ossman93d2d922012-07-20 12:32:52 +0000631 // Check that the screen is fully inside the framebuffer
632 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000633 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000634 continue;
635
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000636 // Adjust the coordinates so they are relative to our viewport
637 sx -= viewport_rect.tl.x;
638 sy -= viewport_rect.tl.y;
639
Pierre Ossmanaae38912012-07-13 11:22:55 +0000640 // Look for perfectly matching existing screen...
641 for (iter = cc->cp.screenLayout.begin();
642 iter != cc->cp.screenLayout.end(); ++iter) {
643 if ((iter->dimensions.tl.x == sx) &&
644 (iter->dimensions.tl.y == sy) &&
645 (iter->dimensions.width() == sw) &&
646 (iter->dimensions.height() == sh))
647 break;
648 }
649
650 // Found it?
651 if (iter != cc->cp.screenLayout.end()) {
652 layout.add_screen(*iter);
653 continue;
654 }
655
656 // Need to add a new one, which means we need to find an unused id
657 while (true) {
658 id = rand();
659 for (iter = cc->cp.screenLayout.begin();
660 iter != cc->cp.screenLayout.end(); ++iter) {
661 if (iter->id == id)
662 break;
663 }
664
665 if (iter == cc->cp.screenLayout.end())
666 break;
667 }
668
669 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000670 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000671
672 // If the viewport doesn't match a physical screen, then we might
673 // end up with no screens in the layout. Add a fake one...
674 if (layout.num_screens() == 0)
675 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000676 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000677#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000678
679 // Do we actually change anything?
680 if ((width == cc->cp.width) &&
681 (height == cc->cp.height) &&
682 (layout == cc->cp.screenLayout))
683 return;
684
Pierre Ossmanaae38912012-07-13 11:22:55 +0000685 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
686 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
687
688 if (!layout.validate(width, height)) {
689 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000690 return;
691 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000692
693 cc->writer()->writeSetDesktopSize(width, height, layout);
694}
695
696
Pierre Ossman6455d852011-06-01 09:33:00 +0000697void DesktopWindow::repositionViewport()
698{
699 int new_x, new_y;
700
701 // Deal with some scrolling corner cases:
702 //
703 // a) If the window is larger then the viewport, center the viewport.
704 // b) If the window is smaller than the viewport, make sure there is
705 // no wasted space on the sides.
706 //
707 // FIXME: Doesn't compensate for scroll widget size properly.
708
709 new_x = viewport->x();
710 new_y = viewport->y();
711
712 if (w() > viewport->w())
713 new_x = (w() - viewport->w()) / 2;
714 else {
715 if (viewport->x() > 0)
716 new_x = 0;
717 else if (w() > (viewport->x() + viewport->w()))
718 new_x = w() - viewport->w();
719 }
720
721 // Same thing for y axis
722 if (h() > viewport->h())
723 new_y = (h() - viewport->h()) / 2;
724 else {
725 if (viewport->y() > 0)
726 new_y = 0;
727 else if (h() > (viewport->y() + viewport->h()))
728 new_y = h() - viewport->h();
729 }
730
731 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
732 viewport->position(new_x, new_y);
733
734 // The scroll widget does not notice when you move around child widgets,
735 // so redraw everything to make sure things update.
736 redraw();
737 }
738}
739
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000740void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
741{
742 exit_vncviewer();
743}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000744
745
746void DesktopWindow::handleOptions(void *data)
747{
748 DesktopWindow *self = (DesktopWindow*)data;
749
750#ifdef HAVE_FLTK_FULLSCREEN
751 if (self->fullscreen_active() && fullscreenSystemKeys)
752 self->grabKeyboard();
753 else
754 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000755
Pierre Ossmanff473402012-07-04 11:27:47 +0000756 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000757 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000758 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000759 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000760#endif
761}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000762
763void DesktopWindow::handleFullscreenTimeout(void *data)
764{
765 DesktopWindow *self = (DesktopWindow *)data;
766
767 assert(self);
768
769 self->delayedFullscreen = false;
770
771 if (self->delayedDesktopSize) {
772 self->handleDesktopSize();
773 self->delayedDesktopSize = false;
774 }
775}