blob: 89398ba9e59bd4d16f43dc44be0b1d07b75bc3e0 [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 Ossmanff473402012-07-04 11:27:47 +000056 : Fl_Window(w, h), cc(cc_), firstUpdate(true)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000057{
Pierre Ossman4ae229f2011-04-15 12:58:31 +000058 Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h);
59 scroll->color(FL_BLACK);
60
61 // Automatically adjust the scroll box to the window
62 resizable(scroll);
63
Pierre Ossmanff473402012-07-04 11:27:47 +000064 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000065
Pierre Ossman4ae229f2011-04-15 12:58:31 +000066 scroll->end();
67
Pierre Ossman5156d5e2011-03-09 09:42:34 +000068 callback(handleClose, this);
69
70 setName(name);
71
Pierre Ossman407a5c32011-05-26 14:48:29 +000072 OptionsDialog::addCallback(handleOptions, this);
73
Pierre Ossmana4f0f182011-06-14 13:36:57 +000074 // Hack. See below...
75 Fl::event_dispatch(&fltkHandle);
76
Pierre Ossman63ca58e2011-05-26 14:59:32 +000077#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanff473402012-07-04 11:27:47 +000078 if (fullScreen)
Pierre Ossmanaae38912012-07-13 11:22:55 +000079 fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +000080 else
Pierre Ossman63ca58e2011-05-26 14:59:32 +000081#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +000082 {
83 // If we are creating a window which is equal to the size on the
84 // screen on X11, many WMs will treat this as a legacy fullscreen
85 // request. This is not what we want. Besides, it doesn't really
86 // make sense to try to create a window which is larger than the
87 // available work space.
88 size(__rfbmin(w, Fl::w()), __rfbmin(h, Fl::h()));
89 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +000090
Pierre Ossman5156d5e2011-03-09 09:42:34 +000091 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +000092
Peter Åstrand49b11572012-08-01 08:09:09 +000093 // Unfortunately, current FLTK does not allow us to set the
94 // maximized property before showing the window. See STR #2083 and
95 // STR #2178
96 if (maximize) {
97 maximizeWindow();
98 }
99
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000100 // The window manager might give us an initial window size that is different
101 // than the one we requested, and in those cases we need to manually adjust
102 // the scroll widget for things to behave sanely.
103 if ((w != this->w()) || (h != this->h()))
104 scroll->size(this->w(), this->h());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000105}
106
107
108DesktopWindow::~DesktopWindow()
109{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000110 // Unregister all timeouts in case they get a change tro trigger
111 // again later when this object is already gone.
112 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000113 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000114
115 OptionsDialog::removeCallback(handleOptions);
116
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000117 // FLTK automatically deletes all child widgets, so we shouldn't touch
118 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000119}
120
121
122void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
123{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000124 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000125}
126
127
128const rfb::PixelFormat &DesktopWindow::getPreferredPF()
129{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000130 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000131}
132
133
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000134void DesktopWindow::setName(const char *name)
135{
136 CharArray windowNameStr;
137 windowNameStr.replaceBuf(new char[256]);
138
Pierre Ossman27820ba2011-09-30 12:54:24 +0000139 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000140
141 copy_label(windowNameStr.buf);
142}
143
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000144
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000145void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
146 rdr::U16* rgbs)
147{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000148 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000149}
150
151
152// Copy the areas of the framebuffer that have been changed (damaged)
153// to the displayed window.
154
155void DesktopWindow::updateWindow()
156{
Pierre Ossmanff473402012-07-04 11:27:47 +0000157 if (firstUpdate) {
158 if (cc->cp.supportsSetDesktopSize)
159 remoteResize();
160 firstUpdate = false;
161 }
162
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000163 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000164}
165
166
Pierre Ossman6455d852011-06-01 09:33:00 +0000167void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
168{
169 if ((new_w == viewport->w()) && (new_h == viewport->h()))
170 return;
171
Pierre Ossman6455d852011-06-01 09:33:00 +0000172 // If we're letting the viewport match the window perfectly, then
173 // keep things that way for the new size, otherwise just keep things
174 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000175#ifdef HAVE_FLTK_FULLSCREEN
176 if (!fullscreen_active()) {
177#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000178 if ((w() == viewport->w()) && (h() == viewport->h()))
179 size(new_w, new_h);
180 else {
181 // Make sure the window isn't too big. We do this manually because
182 // we have to disable the window size restriction (and it isn't
183 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000184 if ((w() > new_w) || (h() > new_h))
185 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000186 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000187#ifdef HAVE_FLTK_FULLSCREEN
188 }
189#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000190
191 viewport->size(new_w, new_h);
192
193 // We might not resize the main window, so we need to manually call this
194 // to make sure the viewport is centered.
195 repositionViewport();
196
Pierre Ossman6455d852011-06-01 09:33:00 +0000197 // repositionViewport() makes sure the scroll widget notices any changes
198 // in position, but it might be just the size that changes so we also
199 // need a poke here as well.
200 redraw();
201}
202
203
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000204void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
205 void* data, void* mask)
206{
207 viewport->setCursor(width, height, hotspot, data, mask);
208}
209
210
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000211void DesktopWindow::resize(int x, int y, int w, int h)
212{
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000213 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000214
Pierre Ossmanff473402012-07-04 11:27:47 +0000215 // Try to get the remote size to match our window size, provided
216 // the following conditions are true:
217 //
218 // a) The user has this feature turned on
219 // b) The server supports it
220 // c) We're not still waiting for a chance to handle DesktopSize
221 //
222 if (not firstUpdate and ::remoteResize and cc->cp.supportsSetDesktopSize) {
223 // We delay updating the remote desktop as we tend to get a flood
224 // of resize events as the user is dragging the window.
225 Fl::remove_timeout(handleResizeTimeout, this);
226 Fl::add_timeout(0.5, handleResizeTimeout, this);
227 }
228
Pierre Ossman6455d852011-06-01 09:33:00 +0000229 // Deal with some scrolling corner cases
230 repositionViewport();
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000231}
232
233
Pierre Ossman407a5c32011-05-26 14:48:29 +0000234int DesktopWindow::handle(int event)
235{
236 switch (event) {
237#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000238 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000239 fullScreen.setParam(fullscreen_active());
240
Pierre Ossman407a5c32011-05-26 14:48:29 +0000241 if (!fullscreenSystemKeys)
242 break;
243
244 if (fullscreen_active())
245 grabKeyboard();
246 else
247 ungrabKeyboard();
248
249 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000250#endif
251 case FL_SHORTCUT:
252 // Sometimes the focus gets out of whack and we fall through to the
253 // shortcut dispatching. Try to make things sane again...
254 if (Fl::focus() == NULL) {
255 take_focus();
256 Fl::handle(FL_KEYDOWN, this);
257 }
258 return 1;
259 }
260
261 return Fl_Window::handle(event);
262}
263
264
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000265int DesktopWindow::fltkHandle(int event, Fl_Window *win)
266{
267 int ret;
268
269 ret = Fl::handle_(event, win);
270
271#ifdef HAVE_FLTK_FULLSCREEN
272 // This is hackish and the result of the dodgy focus handling in FLTK.
273 // The basic problem is that FLTK's view of focus and the system's tend
274 // to differ, and as a result we do not see all the FL_FOCUS events we
275 // need. Fortunately we can grab them here...
276
277 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
278
279 if (dw && fullscreenSystemKeys) {
280 switch (event) {
281 case FL_FOCUS:
282 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
283 // some issues we need to work around:
284 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
285 // b) Gaining focus on the system level causes FLTK to switch
286 // window level on OS X.
287 if (dw->fullscreen_active())
288 dw->grabKeyboard();
289 break;
290
291 case FL_UNFOCUS:
292 // FIXME: We need to relinquish control when the entire window loses
293 // focus as it is very tied to this specific window on some
294 // platforms and we want to be able to open subwindows.
295 dw->ungrabKeyboard();
296 break;
297 }
298 }
299#endif
300
301 return ret;
302}
303
304
Pierre Ossmanaae38912012-07-13 11:22:55 +0000305void DesktopWindow::fullscreen_on()
306{
307#ifdef HAVE_FLTK_FULLSCREEN
308#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
309 if (not fullScreenAllMonitors)
310 fullscreen_screens(-1, -1, -1, -1);
311 else {
312 int top, bottom, left, right;
313 int top_y, bottom_y, left_x, right_x;
314
315 int sx, sy, sw, sh;
316
317 top = bottom = left = right = 0;
318
319 Fl::screen_xywh(sx, sy, sw, sh, 0);
320 top_y = sy;
321 bottom_y = sy + sh;
322 left_x = sx;
323 right_x = sx + sw;
324
325 for (int i = 1;i < Fl::screen_count();i++) {
326 Fl::screen_xywh(sx, sy, sw, sh, i);
327 if (sy < top_y) {
328 top = i;
329 top_y = sy;
330 }
331 if ((sy + sh) > bottom_y) {
332 bottom = i;
333 bottom_y = sy + sh;
334 }
335 if (sx < left_x) {
336 left = i;
337 left_x = sx;
338 }
339 if ((sx + sw) > right_x) {
340 right = i;
341 right_x = sx + sw;
342 }
343 }
344
345 fullscreen_screens(top, bottom, left, right);
346 }
347#endif // HAVE_FLTK_FULLSCREEN_SCREENS
348
349 fullscreen();
350#endif // HAVE_FLTK_FULLSCREEN
351}
352
Pierre Ossman407a5c32011-05-26 14:48:29 +0000353void DesktopWindow::grabKeyboard()
354{
355 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
356 // correct widget regardless of which low level window got the system
357 // event.
358
359 // FIXME: Push this stuff into FLTK.
360
361#if defined(WIN32)
362 int ret;
363
364 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
365 if (ret != 0)
366 vlog.error(_("Failure grabbing keyboard"));
367#elif defined(__APPLE__)
368 int ret;
369
370 ret = cocoa_capture_display(this);
371 if (ret != 0)
372 vlog.error(_("Failure grabbing keyboard"));
373#else
374 int ret;
375
376 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000377 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000378 if (ret) {
379 if (ret == AlreadyGrabbed) {
380 // It seems like we can race with the WM in some cases.
381 // Try again in a bit.
382 if (!Fl::has_timeout(handleGrab, this))
383 Fl::add_timeout(0.500, handleGrab, this);
384 } else {
385 vlog.error(_("Failure grabbing keyboard"));
386 }
387 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000388
389 // We also need to grab the pointer as some WMs like to grab buttons
390 // combined with modifies (e.g. Alt+Button0 in metacity).
391 ret = XGrabPointer(fl_display, fl_xid(this), True,
392 ButtonPressMask|ButtonReleaseMask|
393 ButtonMotionMask|PointerMotionMask,
394 GrabModeAsync, GrabModeAsync,
395 None, None, CurrentTime);
396 if (ret)
397 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000398#endif
399}
400
401
402void DesktopWindow::ungrabKeyboard()
403{
404 Fl::remove_timeout(handleGrab, this);
405
406#if defined(WIN32)
407 win32_disable_lowlevel_keyboard(fl_xid(this));
408#elif defined(__APPLE__)
409 cocoa_release_display(this);
410#else
411 // FLTK has a grab so lets not mess with it
412 if (Fl::grab())
413 return;
414
Pierre Ossman53fd5442011-11-17 10:19:19 +0000415 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000416 XUngrabKeyboard(fl_display, fl_event_time);
417#endif
418}
419
420
421void DesktopWindow::handleGrab(void *data)
422{
423 DesktopWindow *self = (DesktopWindow*)data;
424
425 assert(self);
426
427#ifdef HAVE_FLTK_FULLSCREEN
428 if (!fullscreenSystemKeys)
429 return;
430 if (!self->fullscreen_active())
431 return;
432
433 self->grabKeyboard();
434#endif
435}
436
437
Peter Åstrand49b11572012-08-01 08:09:09 +0000438#define _NET_WM_STATE_ADD 1 /* add/set property */
439void DesktopWindow::maximizeWindow()
440{
441#if defined(WIN32)
442 WINDOWPLACEMENT wp;
443 wp.length = sizeof(WINDOWPLACEMENT);
444 GetWindowPlacement(fl_xid(this), &wp);
445 wp.showCmd = SW_MAXIMIZE;
446 SetWindowPlacement(fl_xid(this), &wp);
447#elif defined(__APPLE__)
448 /* OS X is somewhat strange and does not really have a concept of a
449 maximized window, so we can simply resize the window to the workarea */
450 int X, Y, W, H;
451 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
452 size(W, H);
453#else
454 // X11
455 fl_open_display();
456 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
457 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
458 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
459
460 XEvent e;
461 e.xany.type = ClientMessage;
462 e.xany.window = fl_xid(this);
463 e.xclient.message_type = net_wm_state;
464 e.xclient.format = 32;
465 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
466 e.xclient.data.l[1] = net_wm_state_maximized_vert;
467 e.xclient.data.l[2] = net_wm_state_maximized_horz;
468 e.xclient.data.l[3] = 0;
469 e.xclient.data.l[4] = 0;
470 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
471#endif
472}
473
474
Pierre Ossmanff473402012-07-04 11:27:47 +0000475void DesktopWindow::handleResizeTimeout(void *data)
476{
477 DesktopWindow *self = (DesktopWindow *)data;
478
479 assert(self);
480
481 self->remoteResize();
482}
483
484
485void DesktopWindow::remoteResize()
486{
487 int width, height;
488 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000489 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000490
491 if (firstUpdate) {
492 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
493 return;
494 } else {
495 width = w();
496 height = h();
497 }
498
Pierre Ossmanaae38912012-07-13 11:22:55 +0000499#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000500 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000501#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000502 // In windowed mode (or the framebuffer is so large that we need
503 // to scroll) we just report a single virtual screen that covers
504 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000505
Pierre Ossmanaae38912012-07-13 11:22:55 +0000506 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000507
Pierre Ossmanaae38912012-07-13 11:22:55 +0000508 // Not sure why we have no screens, but adding a new one should be
509 // safe as there is nothing to conflict with...
510 if (layout.num_screens() == 0)
511 layout.add_screen(rfb::Screen());
512 else if (layout.num_screens() != 1) {
513 // More than one screen. Remove all but the first (which we
514 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000515
Pierre Ossmanaae38912012-07-13 11:22:55 +0000516 while (true) {
517 iter = layout.begin();
518 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000519
Pierre Ossmanaae38912012-07-13 11:22:55 +0000520 if (iter == layout.end())
521 break;
522
523 layout.remove_screen(iter->id);
524 }
525 }
526
527 // Resize the remaining single screen to the complete framebuffer
528 layout.begin()->dimensions.tl.x = 0;
529 layout.begin()->dimensions.tl.y = 0;
530 layout.begin()->dimensions.br.x = width;
531 layout.begin()->dimensions.br.y = height;
532#ifdef HAVE_FLTK_FULLSCREEN
533 } else {
534 int i;
535 rdr::U32 id;
536 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000537 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000538
539 // In full screen we report all screens that are fully covered.
540
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000541 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
542 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000543
Pierre Ossmanaae38912012-07-13 11:22:55 +0000544 // If we can find a matching screen in the existing set, we use
545 // that, otherwise we create a brand new screen.
546 //
547 // FIXME: We should really track screens better so we can handle
548 // a resized one.
549 //
550 for (i = 0;i < Fl::screen_count();i++) {
551 Fl::screen_xywh(sx, sy, sw, sh, i);
552
Pierre Ossman93d2d922012-07-20 12:32:52 +0000553 // Check that the screen is fully inside the framebuffer
554 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000555 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000556 continue;
557
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000558 // Adjust the coordinates so they are relative to our viewport
559 sx -= viewport_rect.tl.x;
560 sy -= viewport_rect.tl.y;
561
Pierre Ossmanaae38912012-07-13 11:22:55 +0000562 // Look for perfectly matching existing screen...
563 for (iter = cc->cp.screenLayout.begin();
564 iter != cc->cp.screenLayout.end(); ++iter) {
565 if ((iter->dimensions.tl.x == sx) &&
566 (iter->dimensions.tl.y == sy) &&
567 (iter->dimensions.width() == sw) &&
568 (iter->dimensions.height() == sh))
569 break;
570 }
571
572 // Found it?
573 if (iter != cc->cp.screenLayout.end()) {
574 layout.add_screen(*iter);
575 continue;
576 }
577
578 // Need to add a new one, which means we need to find an unused id
579 while (true) {
580 id = rand();
581 for (iter = cc->cp.screenLayout.begin();
582 iter != cc->cp.screenLayout.end(); ++iter) {
583 if (iter->id == id)
584 break;
585 }
586
587 if (iter == cc->cp.screenLayout.end())
588 break;
589 }
590
591 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000592 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000593
594 // If the viewport doesn't match a physical screen, then we might
595 // end up with no screens in the layout. Add a fake one...
596 if (layout.num_screens() == 0)
597 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000598 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000599#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000600
601 // Do we actually change anything?
602 if ((width == cc->cp.width) &&
603 (height == cc->cp.height) &&
604 (layout == cc->cp.screenLayout))
605 return;
606
Pierre Ossmanaae38912012-07-13 11:22:55 +0000607 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
608 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
609
610 if (!layout.validate(width, height)) {
611 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000612 return;
613 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000614
615 cc->writer()->writeSetDesktopSize(width, height, layout);
616}
617
618
Pierre Ossman6455d852011-06-01 09:33:00 +0000619void DesktopWindow::repositionViewport()
620{
621 int new_x, new_y;
622
623 // Deal with some scrolling corner cases:
624 //
625 // a) If the window is larger then the viewport, center the viewport.
626 // b) If the window is smaller than the viewport, make sure there is
627 // no wasted space on the sides.
628 //
629 // FIXME: Doesn't compensate for scroll widget size properly.
630
631 new_x = viewport->x();
632 new_y = viewport->y();
633
634 if (w() > viewport->w())
635 new_x = (w() - viewport->w()) / 2;
636 else {
637 if (viewport->x() > 0)
638 new_x = 0;
639 else if (w() > (viewport->x() + viewport->w()))
640 new_x = w() - viewport->w();
641 }
642
643 // Same thing for y axis
644 if (h() > viewport->h())
645 new_y = (h() - viewport->h()) / 2;
646 else {
647 if (viewport->y() > 0)
648 new_y = 0;
649 else if (h() > (viewport->y() + viewport->h()))
650 new_y = h() - viewport->h();
651 }
652
653 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
654 viewport->position(new_x, new_y);
655
656 // The scroll widget does not notice when you move around child widgets,
657 // so redraw everything to make sure things update.
658 redraw();
659 }
660}
661
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000662void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
663{
664 exit_vncviewer();
665}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000666
667
668void DesktopWindow::handleOptions(void *data)
669{
670 DesktopWindow *self = (DesktopWindow*)data;
671
672#ifdef HAVE_FLTK_FULLSCREEN
673 if (self->fullscreen_active() && fullscreenSystemKeys)
674 self->grabKeyboard();
675 else
676 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000677
Pierre Ossmanff473402012-07-04 11:27:47 +0000678 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000679 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000680 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000681 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000682#endif
683}