blob: 0894275a7b62ab864e91bebb254ab4572cd79620 [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 Ossman9f32e3c2012-08-23 14:40:52 +0000213 bool resizing;
214
215 if ((this->w() != w) || (this->h() != h))
216 resizing = true;
217 else
218 resizing = false;
219
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000220 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000221
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000222 if (resizing) {
223 // Try to get the remote size to match our window size, provided
224 // the following conditions are true:
225 //
226 // a) The user has this feature turned on
227 // b) The server supports it
228 // c) We're not still waiting for a chance to handle DesktopSize
229 //
230 if (not firstUpdate and ::remoteResize and cc->cp.supportsSetDesktopSize) {
231 // We delay updating the remote desktop as we tend to get a flood
232 // of resize events as the user is dragging the window.
233 Fl::remove_timeout(handleResizeTimeout, this);
234 Fl::add_timeout(0.5, handleResizeTimeout, this);
235 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000236
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000237 // Deal with some scrolling corner cases
238 repositionViewport();
239 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000240}
241
242
Pierre Ossman407a5c32011-05-26 14:48:29 +0000243int DesktopWindow::handle(int event)
244{
245 switch (event) {
246#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000247 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000248 fullScreen.setParam(fullscreen_active());
249
Pierre Ossman407a5c32011-05-26 14:48:29 +0000250 if (!fullscreenSystemKeys)
251 break;
252
253 if (fullscreen_active())
254 grabKeyboard();
255 else
256 ungrabKeyboard();
257
258 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000259#endif
260 case FL_SHORTCUT:
261 // Sometimes the focus gets out of whack and we fall through to the
262 // shortcut dispatching. Try to make things sane again...
263 if (Fl::focus() == NULL) {
264 take_focus();
265 Fl::handle(FL_KEYDOWN, this);
266 }
267 return 1;
268 }
269
270 return Fl_Window::handle(event);
271}
272
273
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000274int DesktopWindow::fltkHandle(int event, Fl_Window *win)
275{
276 int ret;
277
278 ret = Fl::handle_(event, win);
279
280#ifdef HAVE_FLTK_FULLSCREEN
281 // This is hackish and the result of the dodgy focus handling in FLTK.
282 // The basic problem is that FLTK's view of focus and the system's tend
283 // to differ, and as a result we do not see all the FL_FOCUS events we
284 // need. Fortunately we can grab them here...
285
286 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
287
288 if (dw && fullscreenSystemKeys) {
289 switch (event) {
290 case FL_FOCUS:
291 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
292 // some issues we need to work around:
293 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
294 // b) Gaining focus on the system level causes FLTK to switch
295 // window level on OS X.
296 if (dw->fullscreen_active())
297 dw->grabKeyboard();
298 break;
299
300 case FL_UNFOCUS:
301 // FIXME: We need to relinquish control when the entire window loses
302 // focus as it is very tied to this specific window on some
303 // platforms and we want to be able to open subwindows.
304 dw->ungrabKeyboard();
305 break;
306 }
307 }
308#endif
309
310 return ret;
311}
312
313
Pierre Ossmanaae38912012-07-13 11:22:55 +0000314void DesktopWindow::fullscreen_on()
315{
316#ifdef HAVE_FLTK_FULLSCREEN
317#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
318 if (not fullScreenAllMonitors)
319 fullscreen_screens(-1, -1, -1, -1);
320 else {
321 int top, bottom, left, right;
322 int top_y, bottom_y, left_x, right_x;
323
324 int sx, sy, sw, sh;
325
326 top = bottom = left = right = 0;
327
328 Fl::screen_xywh(sx, sy, sw, sh, 0);
329 top_y = sy;
330 bottom_y = sy + sh;
331 left_x = sx;
332 right_x = sx + sw;
333
334 for (int i = 1;i < Fl::screen_count();i++) {
335 Fl::screen_xywh(sx, sy, sw, sh, i);
336 if (sy < top_y) {
337 top = i;
338 top_y = sy;
339 }
340 if ((sy + sh) > bottom_y) {
341 bottom = i;
342 bottom_y = sy + sh;
343 }
344 if (sx < left_x) {
345 left = i;
346 left_x = sx;
347 }
348 if ((sx + sw) > right_x) {
349 right = i;
350 right_x = sx + sw;
351 }
352 }
353
354 fullscreen_screens(top, bottom, left, right);
355 }
356#endif // HAVE_FLTK_FULLSCREEN_SCREENS
357
358 fullscreen();
359#endif // HAVE_FLTK_FULLSCREEN
360}
361
Pierre Ossman407a5c32011-05-26 14:48:29 +0000362void DesktopWindow::grabKeyboard()
363{
364 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
365 // correct widget regardless of which low level window got the system
366 // event.
367
368 // FIXME: Push this stuff into FLTK.
369
370#if defined(WIN32)
371 int ret;
372
373 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
374 if (ret != 0)
375 vlog.error(_("Failure grabbing keyboard"));
376#elif defined(__APPLE__)
377 int ret;
378
379 ret = cocoa_capture_display(this);
380 if (ret != 0)
381 vlog.error(_("Failure grabbing keyboard"));
382#else
383 int ret;
384
385 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000386 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000387 if (ret) {
388 if (ret == AlreadyGrabbed) {
389 // It seems like we can race with the WM in some cases.
390 // Try again in a bit.
391 if (!Fl::has_timeout(handleGrab, this))
392 Fl::add_timeout(0.500, handleGrab, this);
393 } else {
394 vlog.error(_("Failure grabbing keyboard"));
395 }
396 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000397
398 // We also need to grab the pointer as some WMs like to grab buttons
399 // combined with modifies (e.g. Alt+Button0 in metacity).
400 ret = XGrabPointer(fl_display, fl_xid(this), True,
401 ButtonPressMask|ButtonReleaseMask|
402 ButtonMotionMask|PointerMotionMask,
403 GrabModeAsync, GrabModeAsync,
404 None, None, CurrentTime);
405 if (ret)
406 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000407#endif
408}
409
410
411void DesktopWindow::ungrabKeyboard()
412{
413 Fl::remove_timeout(handleGrab, this);
414
415#if defined(WIN32)
416 win32_disable_lowlevel_keyboard(fl_xid(this));
417#elif defined(__APPLE__)
418 cocoa_release_display(this);
419#else
420 // FLTK has a grab so lets not mess with it
421 if (Fl::grab())
422 return;
423
Pierre Ossman53fd5442011-11-17 10:19:19 +0000424 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000425 XUngrabKeyboard(fl_display, fl_event_time);
426#endif
427}
428
429
430void DesktopWindow::handleGrab(void *data)
431{
432 DesktopWindow *self = (DesktopWindow*)data;
433
434 assert(self);
435
436#ifdef HAVE_FLTK_FULLSCREEN
437 if (!fullscreenSystemKeys)
438 return;
439 if (!self->fullscreen_active())
440 return;
441
442 self->grabKeyboard();
443#endif
444}
445
446
Peter Åstrand49b11572012-08-01 08:09:09 +0000447#define _NET_WM_STATE_ADD 1 /* add/set property */
448void DesktopWindow::maximizeWindow()
449{
450#if defined(WIN32)
451 WINDOWPLACEMENT wp;
452 wp.length = sizeof(WINDOWPLACEMENT);
453 GetWindowPlacement(fl_xid(this), &wp);
454 wp.showCmd = SW_MAXIMIZE;
455 SetWindowPlacement(fl_xid(this), &wp);
456#elif defined(__APPLE__)
457 /* OS X is somewhat strange and does not really have a concept of a
458 maximized window, so we can simply resize the window to the workarea */
459 int X, Y, W, H;
460 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
461 size(W, H);
462#else
463 // X11
464 fl_open_display();
465 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
466 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
467 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
468
469 XEvent e;
470 e.xany.type = ClientMessage;
471 e.xany.window = fl_xid(this);
472 e.xclient.message_type = net_wm_state;
473 e.xclient.format = 32;
474 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
475 e.xclient.data.l[1] = net_wm_state_maximized_vert;
476 e.xclient.data.l[2] = net_wm_state_maximized_horz;
477 e.xclient.data.l[3] = 0;
478 e.xclient.data.l[4] = 0;
479 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
480#endif
481}
482
483
Pierre Ossmanff473402012-07-04 11:27:47 +0000484void DesktopWindow::handleResizeTimeout(void *data)
485{
486 DesktopWindow *self = (DesktopWindow *)data;
487
488 assert(self);
489
490 self->remoteResize();
491}
492
493
494void DesktopWindow::remoteResize()
495{
496 int width, height;
497 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000498 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000499
500 if (firstUpdate) {
501 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
502 return;
503 } else {
504 width = w();
505 height = h();
506 }
507
Pierre Ossmanaae38912012-07-13 11:22:55 +0000508#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000509 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000510#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000511 // In windowed mode (or the framebuffer is so large that we need
512 // to scroll) we just report a single virtual screen that covers
513 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000514
Pierre Ossmanaae38912012-07-13 11:22:55 +0000515 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000516
Pierre Ossmanaae38912012-07-13 11:22:55 +0000517 // Not sure why we have no screens, but adding a new one should be
518 // safe as there is nothing to conflict with...
519 if (layout.num_screens() == 0)
520 layout.add_screen(rfb::Screen());
521 else if (layout.num_screens() != 1) {
522 // More than one screen. Remove all but the first (which we
523 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000524
Pierre Ossmanaae38912012-07-13 11:22:55 +0000525 while (true) {
526 iter = layout.begin();
527 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000528
Pierre Ossmanaae38912012-07-13 11:22:55 +0000529 if (iter == layout.end())
530 break;
531
532 layout.remove_screen(iter->id);
533 }
534 }
535
536 // Resize the remaining single screen to the complete framebuffer
537 layout.begin()->dimensions.tl.x = 0;
538 layout.begin()->dimensions.tl.y = 0;
539 layout.begin()->dimensions.br.x = width;
540 layout.begin()->dimensions.br.y = height;
541#ifdef HAVE_FLTK_FULLSCREEN
542 } else {
543 int i;
544 rdr::U32 id;
545 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000546 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000547
548 // In full screen we report all screens that are fully covered.
549
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000550 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
551 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000552
Pierre Ossmanaae38912012-07-13 11:22:55 +0000553 // If we can find a matching screen in the existing set, we use
554 // that, otherwise we create a brand new screen.
555 //
556 // FIXME: We should really track screens better so we can handle
557 // a resized one.
558 //
559 for (i = 0;i < Fl::screen_count();i++) {
560 Fl::screen_xywh(sx, sy, sw, sh, i);
561
Pierre Ossman93d2d922012-07-20 12:32:52 +0000562 // Check that the screen is fully inside the framebuffer
563 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000564 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000565 continue;
566
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000567 // Adjust the coordinates so they are relative to our viewport
568 sx -= viewport_rect.tl.x;
569 sy -= viewport_rect.tl.y;
570
Pierre Ossmanaae38912012-07-13 11:22:55 +0000571 // Look for perfectly matching existing screen...
572 for (iter = cc->cp.screenLayout.begin();
573 iter != cc->cp.screenLayout.end(); ++iter) {
574 if ((iter->dimensions.tl.x == sx) &&
575 (iter->dimensions.tl.y == sy) &&
576 (iter->dimensions.width() == sw) &&
577 (iter->dimensions.height() == sh))
578 break;
579 }
580
581 // Found it?
582 if (iter != cc->cp.screenLayout.end()) {
583 layout.add_screen(*iter);
584 continue;
585 }
586
587 // Need to add a new one, which means we need to find an unused id
588 while (true) {
589 id = rand();
590 for (iter = cc->cp.screenLayout.begin();
591 iter != cc->cp.screenLayout.end(); ++iter) {
592 if (iter->id == id)
593 break;
594 }
595
596 if (iter == cc->cp.screenLayout.end())
597 break;
598 }
599
600 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000601 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000602
603 // If the viewport doesn't match a physical screen, then we might
604 // end up with no screens in the layout. Add a fake one...
605 if (layout.num_screens() == 0)
606 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000607 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000608#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000609
610 // Do we actually change anything?
611 if ((width == cc->cp.width) &&
612 (height == cc->cp.height) &&
613 (layout == cc->cp.screenLayout))
614 return;
615
Pierre Ossmanaae38912012-07-13 11:22:55 +0000616 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
617 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
618
619 if (!layout.validate(width, height)) {
620 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000621 return;
622 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000623
624 cc->writer()->writeSetDesktopSize(width, height, layout);
625}
626
627
Pierre Ossman6455d852011-06-01 09:33:00 +0000628void DesktopWindow::repositionViewport()
629{
630 int new_x, new_y;
631
632 // Deal with some scrolling corner cases:
633 //
634 // a) If the window is larger then the viewport, center the viewport.
635 // b) If the window is smaller than the viewport, make sure there is
636 // no wasted space on the sides.
637 //
638 // FIXME: Doesn't compensate for scroll widget size properly.
639
640 new_x = viewport->x();
641 new_y = viewport->y();
642
643 if (w() > viewport->w())
644 new_x = (w() - viewport->w()) / 2;
645 else {
646 if (viewport->x() > 0)
647 new_x = 0;
648 else if (w() > (viewport->x() + viewport->w()))
649 new_x = w() - viewport->w();
650 }
651
652 // Same thing for y axis
653 if (h() > viewport->h())
654 new_y = (h() - viewport->h()) / 2;
655 else {
656 if (viewport->y() > 0)
657 new_y = 0;
658 else if (h() > (viewport->y() + viewport->h()))
659 new_y = h() - viewport->h();
660 }
661
662 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
663 viewport->position(new_x, new_y);
664
665 // The scroll widget does not notice when you move around child widgets,
666 // so redraw everything to make sure things update.
667 redraw();
668 }
669}
670
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000671void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
672{
673 exit_vncviewer();
674}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000675
676
677void DesktopWindow::handleOptions(void *data)
678{
679 DesktopWindow *self = (DesktopWindow*)data;
680
681#ifdef HAVE_FLTK_FULLSCREEN
682 if (self->fullscreen_active() && fullscreenSystemKeys)
683 self->grabKeyboard();
684 else
685 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000686
Pierre Ossmanff473402012-07-04 11:27:47 +0000687 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000688 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000689 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000690 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000691#endif
692}