blob: 463adf365a7bce13d3d88607d8fd7d9e425c5247 [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 {
91 // If we are creating a window which is equal to the size on the
92 // screen on X11, many WMs will treat this as a legacy fullscreen
93 // request. This is not what we want. Besides, it doesn't really
94 // make sense to try to create a window which is larger than the
95 // available work space.
96 size(__rfbmin(w, Fl::w()), __rfbmin(h, Fl::h()));
97 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +000098
Pierre Ossman5156d5e2011-03-09 09:42:34 +000099 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000100
Peter Åstrand49b11572012-08-01 08:09:09 +0000101 // Unfortunately, current FLTK does not allow us to set the
102 // maximized property before showing the window. See STR #2083 and
103 // STR #2178
104 if (maximize) {
105 maximizeWindow();
106 }
107
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000108 // The window manager might give us an initial window size that is different
109 // than the one we requested, and in those cases we need to manually adjust
110 // the scroll widget for things to behave sanely.
111 if ((w != this->w()) || (h != this->h()))
112 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000113
114#ifdef HAVE_FLTK_FULLSCREEN
115 if (delayedFullscreen) {
116 // Hack: Fullscreen requests may be ignored, so we need a timeout for
117 // when we should stop waiting. We also really need to wait for the
118 // resize, which can come after the fullscreen event.
119 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
120 fullscreen_on();
121 }
122#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000123}
124
125
126DesktopWindow::~DesktopWindow()
127{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000128 // Unregister all timeouts in case they get a change tro trigger
129 // again later when this object is already gone.
130 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000131 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000132 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000133
134 OptionsDialog::removeCallback(handleOptions);
135
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000136 // FLTK automatically deletes all child widgets, so we shouldn't touch
137 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000138}
139
140
141void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
142{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000143 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000144}
145
146
147const rfb::PixelFormat &DesktopWindow::getPreferredPF()
148{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000149 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000150}
151
152
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000153void DesktopWindow::setName(const char *name)
154{
155 CharArray windowNameStr;
156 windowNameStr.replaceBuf(new char[256]);
157
Pierre Ossman27820ba2011-09-30 12:54:24 +0000158 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000159
160 copy_label(windowNameStr.buf);
161}
162
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000163
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000164void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
165 rdr::U16* rgbs)
166{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000167 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000168}
169
170
171// Copy the areas of the framebuffer that have been changed (damaged)
172// to the displayed window.
173
174void DesktopWindow::updateWindow()
175{
Pierre Ossmanff473402012-07-04 11:27:47 +0000176 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000177 if (cc->cp.supportsSetDesktopSize) {
178 // Hack: Wait until we're in the proper mode and position until
179 // resizing things, otherwise we might send the wrong thing.
180 if (delayedFullscreen)
181 delayedDesktopSize = true;
182 else
183 handleDesktopSize();
184 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000185 firstUpdate = false;
186 }
187
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000188 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000189}
190
191
Pierre Ossman6455d852011-06-01 09:33:00 +0000192void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
193{
194 if ((new_w == viewport->w()) && (new_h == viewport->h()))
195 return;
196
Pierre Ossman6455d852011-06-01 09:33:00 +0000197 // If we're letting the viewport match the window perfectly, then
198 // keep things that way for the new size, otherwise just keep things
199 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000200#ifdef HAVE_FLTK_FULLSCREEN
201 if (!fullscreen_active()) {
202#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000203 if ((w() == viewport->w()) && (h() == viewport->h()))
204 size(new_w, new_h);
205 else {
206 // Make sure the window isn't too big. We do this manually because
207 // we have to disable the window size restriction (and it isn't
208 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000209 if ((w() > new_w) || (h() > new_h))
210 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000211 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000212#ifdef HAVE_FLTK_FULLSCREEN
213 }
214#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000215
216 viewport->size(new_w, new_h);
217
218 // We might not resize the main window, so we need to manually call this
219 // to make sure the viewport is centered.
220 repositionViewport();
221
Pierre Ossman6455d852011-06-01 09:33:00 +0000222 // repositionViewport() makes sure the scroll widget notices any changes
223 // in position, but it might be just the size that changes so we also
224 // need a poke here as well.
225 redraw();
226}
227
228
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000229void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
230 void* data, void* mask)
231{
232 viewport->setCursor(width, height, hotspot, data, mask);
233}
234
235
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000236void DesktopWindow::resize(int x, int y, int w, int h)
237{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000238 bool resizing;
239
240 if ((this->w() != w) || (this->h() != h))
241 resizing = true;
242 else
243 resizing = false;
244
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000245 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000246
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000247 if (resizing) {
248 // Try to get the remote size to match our window size, provided
249 // the following conditions are true:
250 //
251 // a) The user has this feature turned on
252 // b) The server supports it
253 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000254 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000255 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000256 if (not firstUpdate and not delayedFullscreen and
257 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000258 // We delay updating the remote desktop as we tend to get a flood
259 // of resize events as the user is dragging the window.
260 Fl::remove_timeout(handleResizeTimeout, this);
261 Fl::add_timeout(0.5, handleResizeTimeout, this);
262 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000263
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000264 // Deal with some scrolling corner cases
265 repositionViewport();
266 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000267}
268
269
Pierre Ossman407a5c32011-05-26 14:48:29 +0000270int DesktopWindow::handle(int event)
271{
272 switch (event) {
273#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000274 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000275 fullScreen.setParam(fullscreen_active());
276
Pierre Ossman407a5c32011-05-26 14:48:29 +0000277 if (!fullscreenSystemKeys)
278 break;
279
280 if (fullscreen_active())
281 grabKeyboard();
282 else
283 ungrabKeyboard();
284
285 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000286#endif
287 case FL_SHORTCUT:
288 // Sometimes the focus gets out of whack and we fall through to the
289 // shortcut dispatching. Try to make things sane again...
290 if (Fl::focus() == NULL) {
291 take_focus();
292 Fl::handle(FL_KEYDOWN, this);
293 }
294 return 1;
295 }
296
297 return Fl_Window::handle(event);
298}
299
300
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000301int DesktopWindow::fltkHandle(int event, Fl_Window *win)
302{
303 int ret;
304
305 ret = Fl::handle_(event, win);
306
307#ifdef HAVE_FLTK_FULLSCREEN
308 // This is hackish and the result of the dodgy focus handling in FLTK.
309 // The basic problem is that FLTK's view of focus and the system's tend
310 // to differ, and as a result we do not see all the FL_FOCUS events we
311 // need. Fortunately we can grab them here...
312
313 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
314
315 if (dw && fullscreenSystemKeys) {
316 switch (event) {
317 case FL_FOCUS:
318 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
319 // some issues we need to work around:
320 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
321 // b) Gaining focus on the system level causes FLTK to switch
322 // window level on OS X.
323 if (dw->fullscreen_active())
324 dw->grabKeyboard();
325 break;
326
327 case FL_UNFOCUS:
328 // FIXME: We need to relinquish control when the entire window loses
329 // focus as it is very tied to this specific window on some
330 // platforms and we want to be able to open subwindows.
331 dw->ungrabKeyboard();
332 break;
333 }
334 }
335#endif
336
337 return ret;
338}
339
340
Pierre Ossmanaae38912012-07-13 11:22:55 +0000341void DesktopWindow::fullscreen_on()
342{
343#ifdef HAVE_FLTK_FULLSCREEN
344#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
345 if (not fullScreenAllMonitors)
346 fullscreen_screens(-1, -1, -1, -1);
347 else {
348 int top, bottom, left, right;
349 int top_y, bottom_y, left_x, right_x;
350
351 int sx, sy, sw, sh;
352
353 top = bottom = left = right = 0;
354
355 Fl::screen_xywh(sx, sy, sw, sh, 0);
356 top_y = sy;
357 bottom_y = sy + sh;
358 left_x = sx;
359 right_x = sx + sw;
360
361 for (int i = 1;i < Fl::screen_count();i++) {
362 Fl::screen_xywh(sx, sy, sw, sh, i);
363 if (sy < top_y) {
364 top = i;
365 top_y = sy;
366 }
367 if ((sy + sh) > bottom_y) {
368 bottom = i;
369 bottom_y = sy + sh;
370 }
371 if (sx < left_x) {
372 left = i;
373 left_x = sx;
374 }
375 if ((sx + sw) > right_x) {
376 right = i;
377 right_x = sx + sw;
378 }
379 }
380
381 fullscreen_screens(top, bottom, left, right);
382 }
383#endif // HAVE_FLTK_FULLSCREEN_SCREENS
384
385 fullscreen();
386#endif // HAVE_FLTK_FULLSCREEN
387}
388
Pierre Ossman407a5c32011-05-26 14:48:29 +0000389void DesktopWindow::grabKeyboard()
390{
391 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
392 // correct widget regardless of which low level window got the system
393 // event.
394
395 // FIXME: Push this stuff into FLTK.
396
397#if defined(WIN32)
398 int ret;
399
400 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
401 if (ret != 0)
402 vlog.error(_("Failure grabbing keyboard"));
403#elif defined(__APPLE__)
404 int ret;
405
406 ret = cocoa_capture_display(this);
407 if (ret != 0)
408 vlog.error(_("Failure grabbing keyboard"));
409#else
410 int ret;
411
412 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000413 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000414 if (ret) {
415 if (ret == AlreadyGrabbed) {
416 // It seems like we can race with the WM in some cases.
417 // Try again in a bit.
418 if (!Fl::has_timeout(handleGrab, this))
419 Fl::add_timeout(0.500, handleGrab, this);
420 } else {
421 vlog.error(_("Failure grabbing keyboard"));
422 }
423 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000424
425 // We also need to grab the pointer as some WMs like to grab buttons
426 // combined with modifies (e.g. Alt+Button0 in metacity).
427 ret = XGrabPointer(fl_display, fl_xid(this), True,
428 ButtonPressMask|ButtonReleaseMask|
429 ButtonMotionMask|PointerMotionMask,
430 GrabModeAsync, GrabModeAsync,
431 None, None, CurrentTime);
432 if (ret)
433 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000434#endif
435}
436
437
438void DesktopWindow::ungrabKeyboard()
439{
440 Fl::remove_timeout(handleGrab, this);
441
442#if defined(WIN32)
443 win32_disable_lowlevel_keyboard(fl_xid(this));
444#elif defined(__APPLE__)
445 cocoa_release_display(this);
446#else
447 // FLTK has a grab so lets not mess with it
448 if (Fl::grab())
449 return;
450
Pierre Ossman53fd5442011-11-17 10:19:19 +0000451 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000452 XUngrabKeyboard(fl_display, fl_event_time);
453#endif
454}
455
456
457void DesktopWindow::handleGrab(void *data)
458{
459 DesktopWindow *self = (DesktopWindow*)data;
460
461 assert(self);
462
463#ifdef HAVE_FLTK_FULLSCREEN
464 if (!fullscreenSystemKeys)
465 return;
466 if (!self->fullscreen_active())
467 return;
468
469 self->grabKeyboard();
470#endif
471}
472
473
Peter Åstrand49b11572012-08-01 08:09:09 +0000474#define _NET_WM_STATE_ADD 1 /* add/set property */
475void DesktopWindow::maximizeWindow()
476{
477#if defined(WIN32)
478 WINDOWPLACEMENT wp;
479 wp.length = sizeof(WINDOWPLACEMENT);
480 GetWindowPlacement(fl_xid(this), &wp);
481 wp.showCmd = SW_MAXIMIZE;
482 SetWindowPlacement(fl_xid(this), &wp);
483#elif defined(__APPLE__)
484 /* OS X is somewhat strange and does not really have a concept of a
485 maximized window, so we can simply resize the window to the workarea */
486 int X, Y, W, H;
487 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
488 size(W, H);
489#else
490 // X11
491 fl_open_display();
492 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
493 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
494 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
495
496 XEvent e;
497 e.xany.type = ClientMessage;
498 e.xany.window = fl_xid(this);
499 e.xclient.message_type = net_wm_state;
500 e.xclient.format = 32;
501 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
502 e.xclient.data.l[1] = net_wm_state_maximized_vert;
503 e.xclient.data.l[2] = net_wm_state_maximized_horz;
504 e.xclient.data.l[3] = 0;
505 e.xclient.data.l[4] = 0;
506 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
507#endif
508}
509
510
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000511void DesktopWindow::handleDesktopSize()
512{
513 int width, height;
514
515 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
516 return;
517
518 remoteResize(width, height);
519}
520
521
Pierre Ossmanff473402012-07-04 11:27:47 +0000522void DesktopWindow::handleResizeTimeout(void *data)
523{
524 DesktopWindow *self = (DesktopWindow *)data;
525
526 assert(self);
527
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000528 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000529}
530
531
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000532void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000533{
Pierre Ossmanff473402012-07-04 11:27:47 +0000534 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000535 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000536
Pierre Ossmanaae38912012-07-13 11:22:55 +0000537#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000538 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000539#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000540 // In windowed mode (or the framebuffer is so large that we need
541 // to scroll) we just report a single virtual screen that covers
542 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000543
Pierre Ossmanaae38912012-07-13 11:22:55 +0000544 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000545
Pierre Ossmanaae38912012-07-13 11:22:55 +0000546 // Not sure why we have no screens, but adding a new one should be
547 // safe as there is nothing to conflict with...
548 if (layout.num_screens() == 0)
549 layout.add_screen(rfb::Screen());
550 else if (layout.num_screens() != 1) {
551 // More than one screen. Remove all but the first (which we
552 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000553
Pierre Ossmanaae38912012-07-13 11:22:55 +0000554 while (true) {
555 iter = layout.begin();
556 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000557
Pierre Ossmanaae38912012-07-13 11:22:55 +0000558 if (iter == layout.end())
559 break;
560
561 layout.remove_screen(iter->id);
562 }
563 }
564
565 // Resize the remaining single screen to the complete framebuffer
566 layout.begin()->dimensions.tl.x = 0;
567 layout.begin()->dimensions.tl.y = 0;
568 layout.begin()->dimensions.br.x = width;
569 layout.begin()->dimensions.br.y = height;
570#ifdef HAVE_FLTK_FULLSCREEN
571 } else {
572 int i;
573 rdr::U32 id;
574 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000575 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000576
577 // In full screen we report all screens that are fully covered.
578
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000579 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
580 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000581
Pierre Ossmanaae38912012-07-13 11:22:55 +0000582 // If we can find a matching screen in the existing set, we use
583 // that, otherwise we create a brand new screen.
584 //
585 // FIXME: We should really track screens better so we can handle
586 // a resized one.
587 //
588 for (i = 0;i < Fl::screen_count();i++) {
589 Fl::screen_xywh(sx, sy, sw, sh, i);
590
Pierre Ossman93d2d922012-07-20 12:32:52 +0000591 // Check that the screen is fully inside the framebuffer
592 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000593 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000594 continue;
595
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000596 // Adjust the coordinates so they are relative to our viewport
597 sx -= viewport_rect.tl.x;
598 sy -= viewport_rect.tl.y;
599
Pierre Ossmanaae38912012-07-13 11:22:55 +0000600 // Look for perfectly matching existing screen...
601 for (iter = cc->cp.screenLayout.begin();
602 iter != cc->cp.screenLayout.end(); ++iter) {
603 if ((iter->dimensions.tl.x == sx) &&
604 (iter->dimensions.tl.y == sy) &&
605 (iter->dimensions.width() == sw) &&
606 (iter->dimensions.height() == sh))
607 break;
608 }
609
610 // Found it?
611 if (iter != cc->cp.screenLayout.end()) {
612 layout.add_screen(*iter);
613 continue;
614 }
615
616 // Need to add a new one, which means we need to find an unused id
617 while (true) {
618 id = rand();
619 for (iter = cc->cp.screenLayout.begin();
620 iter != cc->cp.screenLayout.end(); ++iter) {
621 if (iter->id == id)
622 break;
623 }
624
625 if (iter == cc->cp.screenLayout.end())
626 break;
627 }
628
629 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000630 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000631
632 // If the viewport doesn't match a physical screen, then we might
633 // end up with no screens in the layout. Add a fake one...
634 if (layout.num_screens() == 0)
635 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000636 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000637#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000638
639 // Do we actually change anything?
640 if ((width == cc->cp.width) &&
641 (height == cc->cp.height) &&
642 (layout == cc->cp.screenLayout))
643 return;
644
Pierre Ossmanaae38912012-07-13 11:22:55 +0000645 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
646 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
647
648 if (!layout.validate(width, height)) {
649 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000650 return;
651 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000652
653 cc->writer()->writeSetDesktopSize(width, height, layout);
654}
655
656
Pierre Ossman6455d852011-06-01 09:33:00 +0000657void DesktopWindow::repositionViewport()
658{
659 int new_x, new_y;
660
661 // Deal with some scrolling corner cases:
662 //
663 // a) If the window is larger then the viewport, center the viewport.
664 // b) If the window is smaller than the viewport, make sure there is
665 // no wasted space on the sides.
666 //
667 // FIXME: Doesn't compensate for scroll widget size properly.
668
669 new_x = viewport->x();
670 new_y = viewport->y();
671
672 if (w() > viewport->w())
673 new_x = (w() - viewport->w()) / 2;
674 else {
675 if (viewport->x() > 0)
676 new_x = 0;
677 else if (w() > (viewport->x() + viewport->w()))
678 new_x = w() - viewport->w();
679 }
680
681 // Same thing for y axis
682 if (h() > viewport->h())
683 new_y = (h() - viewport->h()) / 2;
684 else {
685 if (viewport->y() > 0)
686 new_y = 0;
687 else if (h() > (viewport->y() + viewport->h()))
688 new_y = h() - viewport->h();
689 }
690
691 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
692 viewport->position(new_x, new_y);
693
694 // The scroll widget does not notice when you move around child widgets,
695 // so redraw everything to make sure things update.
696 redraw();
697 }
698}
699
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000700void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
701{
702 exit_vncviewer();
703}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000704
705
706void DesktopWindow::handleOptions(void *data)
707{
708 DesktopWindow *self = (DesktopWindow*)data;
709
710#ifdef HAVE_FLTK_FULLSCREEN
711 if (self->fullscreen_active() && fullscreenSystemKeys)
712 self->grabKeyboard();
713 else
714 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000715
Pierre Ossmanff473402012-07-04 11:27:47 +0000716 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000717 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000718 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000719 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000720#endif
721}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000722
723void DesktopWindow::handleFullscreenTimeout(void *data)
724{
725 DesktopWindow *self = (DesktopWindow *)data;
726
727 assert(self);
728
729 self->delayedFullscreen = false;
730
731 if (self->delayedDesktopSize) {
732 self->handleDesktopSize();
733 self->delayedDesktopSize = false;
734 }
735}