blob: ca33ebeb845ad44b5f1a1870091096eaf48ebbd6 [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
Peter Åstrandc359f362011-08-23 12:04:46 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Pierre Ossman5156d5e2011-03-09 09:42:34 +000024#include <assert.h>
25#include <stdio.h>
26#include <string.h>
27
Pierre Ossman5156d5e2011-03-09 09:42:34 +000028#include <rfb/LogWriter.h>
Pierre Ossmanff473402012-07-04 11:27:47 +000029#include <rfb/CMsgWriter.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000030
31#include "DesktopWindow.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000032#include "OptionsDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000033#include "i18n.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000034#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000035#include "vncviewer.h"
Pierre Ossmanff473402012-07-04 11:27:47 +000036#include "CConn.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000037
DRCb65bb932011-06-24 03:17:00 +000038#include <FL/Fl_Scroll.H>
39#include <FL/x.H>
40
Pierre Ossman407a5c32011-05-26 14:48:29 +000041#ifdef WIN32
42#include "win32.h"
43#endif
44
45#ifdef __APPLE__
46#include "cocoa.h"
47#endif
Pierre Ossman89f868a2011-04-11 11:59:31 +000048
Pierre Ossman5156d5e2011-03-09 09:42:34 +000049using namespace rfb;
50
Pierre Ossman5156d5e2011-03-09 09:42:34 +000051static rfb::LogWriter vlog("DesktopWindow");
52
53DesktopWindow::DesktopWindow(int w, int h, const char *name,
54 const rfb::PixelFormat& serverPF,
55 CConn* cc_)
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000056 : Fl_Window(w, h), cc(cc_), firstUpdate(true),
57 delayedFullscreen(false), delayedDesktopSize(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000058{
Pierre Ossman4ae229f2011-04-15 12:58:31 +000059 Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h);
60 scroll->color(FL_BLACK);
61
62 // Automatically adjust the scroll box to the window
63 resizable(scroll);
64
Pierre Ossmanff473402012-07-04 11:27:47 +000065 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000066
Pierre Ossman4ae229f2011-04-15 12:58:31 +000067 scroll->end();
68
Pierre Ossman5156d5e2011-03-09 09:42:34 +000069 callback(handleClose, this);
70
71 setName(name);
72
Pierre Ossman407a5c32011-05-26 14:48:29 +000073 OptionsDialog::addCallback(handleOptions, this);
74
Pierre Ossmana4f0f182011-06-14 13:36:57 +000075 // Hack. See below...
76 Fl::event_dispatch(&fltkHandle);
77
Pierre Ossman63ca58e2011-05-26 14:59:32 +000078#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000079 if (fullScreen) {
80 // Hack: Window managers seem to be rather crappy at respecting
81 // fullscreen hints on initial windows. So on X11 we'll have to
82 // wait until after we've been mapped.
83#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +000084 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000085#else
86 delayedFullscreen = true;
87#endif
88 } else
Pierre Ossman63ca58e2011-05-26 14:59:32 +000089#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +000090 {
Peter Åstrandb182a9e2012-08-27 07:28:08 +000091
Peter Åstranda21a0cf2012-08-27 08:11:20 +000092 // Support for -geometry option. Note that although we do support
93 // negative coordinates, we do not support -XOFF-YOFF (ie
94 // coordinates relative to the right edge / bottom edge) at this
95 // time.
Peter Åstrandb182a9e2012-08-27 07:28:08 +000096 int geom_x = 0, geom_y = 0;
97 if (geometry.hasBeenSet()) {
98 int matched;
99 matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
100 if (matched == 2) {
101 force_position(1);
102 } else {
103 int geom_w, geom_h;
104 matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
105 switch (matched) {
106 case 4:
107 force_position(1);
108 /* fall through */
109 case 2:
110 w = geom_w;
111 h = geom_h;
Peter Åstrand63d75cc2012-08-27 08:02:36 +0000112 break;
Peter Åstrandb182a9e2012-08-27 07:28:08 +0000113 default:
Peter Åstrand63d75cc2012-08-27 08:02:36 +0000114 geom_x = geom_y = 0;
Peter Åstrandb182a9e2012-08-27 07:28:08 +0000115 vlog.error("Invalid geometry specified!");
116 }
117 }
118 }
119
Peter Åstrandd62482e2011-08-02 08:33:27 +0000120 // If we are creating a window which is equal to the size on the
121 // screen on X11, many WMs will treat this as a legacy fullscreen
122 // request. This is not what we want. Besides, it doesn't really
123 // make sense to try to create a window which is larger than the
124 // available work space.
Peter Åstrandb182a9e2012-08-27 07:28:08 +0000125 w = __rfbmin(w, Fl::w());
126 h = __rfbmin(h, Fl::h());
127
128 if (force_position()) {
129 resize(geom_x, geom_y, w, h);
130 } else {
131 size(w, h);
132 }
Peter Åstrandd62482e2011-08-02 08:33:27 +0000133 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000134
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000135 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000136
Peter Åstrand49b11572012-08-01 08:09:09 +0000137 // Unfortunately, current FLTK does not allow us to set the
138 // maximized property before showing the window. See STR #2083 and
139 // STR #2178
140 if (maximize) {
141 maximizeWindow();
142 }
143
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000144 // The window manager might give us an initial window size that is different
145 // than the one we requested, and in those cases we need to manually adjust
146 // the scroll widget for things to behave sanely.
147 if ((w != this->w()) || (h != this->h()))
148 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000149
150#ifdef HAVE_FLTK_FULLSCREEN
151 if (delayedFullscreen) {
152 // Hack: Fullscreen requests may be ignored, so we need a timeout for
153 // when we should stop waiting. We also really need to wait for the
154 // resize, which can come after the fullscreen event.
155 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
156 fullscreen_on();
157 }
158#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000159}
160
161
162DesktopWindow::~DesktopWindow()
163{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000164 // Unregister all timeouts in case they get a change tro trigger
165 // again later when this object is already gone.
166 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000167 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000168 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000169
170 OptionsDialog::removeCallback(handleOptions);
171
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000172 // FLTK automatically deletes all child widgets, so we shouldn't touch
173 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000174}
175
176
177void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
178{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000179 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000180}
181
182
183const rfb::PixelFormat &DesktopWindow::getPreferredPF()
184{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000185 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000186}
187
188
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000189void DesktopWindow::setName(const char *name)
190{
191 CharArray windowNameStr;
192 windowNameStr.replaceBuf(new char[256]);
193
Pierre Ossman27820ba2011-09-30 12:54:24 +0000194 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000195
196 copy_label(windowNameStr.buf);
197}
198
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000199
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000200void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
201 rdr::U16* rgbs)
202{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000203 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000204}
205
206
207// Copy the areas of the framebuffer that have been changed (damaged)
208// to the displayed window.
209
210void DesktopWindow::updateWindow()
211{
Pierre Ossmanff473402012-07-04 11:27:47 +0000212 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000213 if (cc->cp.supportsSetDesktopSize) {
214 // Hack: Wait until we're in the proper mode and position until
215 // resizing things, otherwise we might send the wrong thing.
216 if (delayedFullscreen)
217 delayedDesktopSize = true;
218 else
219 handleDesktopSize();
220 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000221 firstUpdate = false;
222 }
223
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000224 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000225}
226
227
Pierre Ossman6455d852011-06-01 09:33:00 +0000228void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
229{
230 if ((new_w == viewport->w()) && (new_h == viewport->h()))
231 return;
232
Pierre Ossman6455d852011-06-01 09:33:00 +0000233 // If we're letting the viewport match the window perfectly, then
234 // keep things that way for the new size, otherwise just keep things
235 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000236#ifdef HAVE_FLTK_FULLSCREEN
237 if (!fullscreen_active()) {
238#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000239 if ((w() == viewport->w()) && (h() == viewport->h()))
240 size(new_w, new_h);
241 else {
242 // Make sure the window isn't too big. We do this manually because
243 // we have to disable the window size restriction (and it isn't
244 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000245 if ((w() > new_w) || (h() > new_h))
246 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000247 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000248#ifdef HAVE_FLTK_FULLSCREEN
249 }
250#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000251
252 viewport->size(new_w, new_h);
253
254 // We might not resize the main window, so we need to manually call this
255 // to make sure the viewport is centered.
256 repositionViewport();
257
Pierre Ossman6455d852011-06-01 09:33:00 +0000258 // repositionViewport() makes sure the scroll widget notices any changes
259 // in position, but it might be just the size that changes so we also
260 // need a poke here as well.
261 redraw();
262}
263
264
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000265void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
266 void* data, void* mask)
267{
268 viewport->setCursor(width, height, hotspot, data, mask);
269}
270
271
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000272void DesktopWindow::resize(int x, int y, int w, int h)
273{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000274 bool resizing;
275
276 if ((this->w() != w) || (this->h() != h))
277 resizing = true;
278 else
279 resizing = false;
280
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000281 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000282
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000283 if (resizing) {
284 // Try to get the remote size to match our window size, provided
285 // the following conditions are true:
286 //
287 // a) The user has this feature turned on
288 // b) The server supports it
289 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000290 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000291 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000292 if (not firstUpdate and not delayedFullscreen and
293 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000294 // We delay updating the remote desktop as we tend to get a flood
295 // of resize events as the user is dragging the window.
296 Fl::remove_timeout(handleResizeTimeout, this);
297 Fl::add_timeout(0.5, handleResizeTimeout, this);
298 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000299
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000300 // Deal with some scrolling corner cases
301 repositionViewport();
302 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000303}
304
305
Pierre Ossman407a5c32011-05-26 14:48:29 +0000306int DesktopWindow::handle(int event)
307{
308 switch (event) {
309#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000310 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000311 fullScreen.setParam(fullscreen_active());
312
Pierre Ossman407a5c32011-05-26 14:48:29 +0000313 if (!fullscreenSystemKeys)
314 break;
315
316 if (fullscreen_active())
317 grabKeyboard();
318 else
319 ungrabKeyboard();
320
321 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000322#endif
323 case FL_SHORTCUT:
324 // Sometimes the focus gets out of whack and we fall through to the
325 // shortcut dispatching. Try to make things sane again...
326 if (Fl::focus() == NULL) {
327 take_focus();
328 Fl::handle(FL_KEYDOWN, this);
329 }
330 return 1;
331 }
332
333 return Fl_Window::handle(event);
334}
335
336
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000337int DesktopWindow::fltkHandle(int event, Fl_Window *win)
338{
339 int ret;
340
341 ret = Fl::handle_(event, win);
342
343#ifdef HAVE_FLTK_FULLSCREEN
344 // This is hackish and the result of the dodgy focus handling in FLTK.
345 // The basic problem is that FLTK's view of focus and the system's tend
346 // to differ, and as a result we do not see all the FL_FOCUS events we
347 // need. Fortunately we can grab them here...
348
349 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
350
351 if (dw && fullscreenSystemKeys) {
352 switch (event) {
353 case FL_FOCUS:
354 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
355 // some issues we need to work around:
356 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
357 // b) Gaining focus on the system level causes FLTK to switch
358 // window level on OS X.
359 if (dw->fullscreen_active())
360 dw->grabKeyboard();
361 break;
362
363 case FL_UNFOCUS:
364 // FIXME: We need to relinquish control when the entire window loses
365 // focus as it is very tied to this specific window on some
366 // platforms and we want to be able to open subwindows.
367 dw->ungrabKeyboard();
368 break;
369 }
370 }
371#endif
372
373 return ret;
374}
375
376
Pierre Ossmanaae38912012-07-13 11:22:55 +0000377void DesktopWindow::fullscreen_on()
378{
379#ifdef HAVE_FLTK_FULLSCREEN
380#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
381 if (not fullScreenAllMonitors)
382 fullscreen_screens(-1, -1, -1, -1);
383 else {
384 int top, bottom, left, right;
385 int top_y, bottom_y, left_x, right_x;
386
387 int sx, sy, sw, sh;
388
389 top = bottom = left = right = 0;
390
391 Fl::screen_xywh(sx, sy, sw, sh, 0);
392 top_y = sy;
393 bottom_y = sy + sh;
394 left_x = sx;
395 right_x = sx + sw;
396
397 for (int i = 1;i < Fl::screen_count();i++) {
398 Fl::screen_xywh(sx, sy, sw, sh, i);
399 if (sy < top_y) {
400 top = i;
401 top_y = sy;
402 }
403 if ((sy + sh) > bottom_y) {
404 bottom = i;
405 bottom_y = sy + sh;
406 }
407 if (sx < left_x) {
408 left = i;
409 left_x = sx;
410 }
411 if ((sx + sw) > right_x) {
412 right = i;
413 right_x = sx + sw;
414 }
415 }
416
417 fullscreen_screens(top, bottom, left, right);
418 }
419#endif // HAVE_FLTK_FULLSCREEN_SCREENS
420
421 fullscreen();
422#endif // HAVE_FLTK_FULLSCREEN
423}
424
Pierre Ossman407a5c32011-05-26 14:48:29 +0000425void DesktopWindow::grabKeyboard()
426{
427 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
428 // correct widget regardless of which low level window got the system
429 // event.
430
431 // FIXME: Push this stuff into FLTK.
432
433#if defined(WIN32)
434 int ret;
435
436 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
437 if (ret != 0)
438 vlog.error(_("Failure grabbing keyboard"));
439#elif defined(__APPLE__)
440 int ret;
441
Pierre Ossman3d759112012-08-27 14:40:51 +0000442 ret = cocoa_capture_display(this,
443#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
444 fullScreenAllMonitors
445#else
446 false
447#endif
448 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000449 if (ret != 0)
450 vlog.error(_("Failure grabbing keyboard"));
451#else
452 int ret;
453
454 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000455 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000456 if (ret) {
457 if (ret == AlreadyGrabbed) {
458 // It seems like we can race with the WM in some cases.
459 // Try again in a bit.
460 if (!Fl::has_timeout(handleGrab, this))
461 Fl::add_timeout(0.500, handleGrab, this);
462 } else {
463 vlog.error(_("Failure grabbing keyboard"));
464 }
465 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000466
467 // We also need to grab the pointer as some WMs like to grab buttons
468 // combined with modifies (e.g. Alt+Button0 in metacity).
469 ret = XGrabPointer(fl_display, fl_xid(this), True,
470 ButtonPressMask|ButtonReleaseMask|
471 ButtonMotionMask|PointerMotionMask,
472 GrabModeAsync, GrabModeAsync,
473 None, None, CurrentTime);
474 if (ret)
475 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000476#endif
477}
478
479
480void DesktopWindow::ungrabKeyboard()
481{
482 Fl::remove_timeout(handleGrab, this);
483
484#if defined(WIN32)
485 win32_disable_lowlevel_keyboard(fl_xid(this));
486#elif defined(__APPLE__)
487 cocoa_release_display(this);
488#else
489 // FLTK has a grab so lets not mess with it
490 if (Fl::grab())
491 return;
492
Pierre Ossman53fd5442011-11-17 10:19:19 +0000493 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000494 XUngrabKeyboard(fl_display, fl_event_time);
495#endif
496}
497
498
499void DesktopWindow::handleGrab(void *data)
500{
501 DesktopWindow *self = (DesktopWindow*)data;
502
503 assert(self);
504
505#ifdef HAVE_FLTK_FULLSCREEN
506 if (!fullscreenSystemKeys)
507 return;
508 if (!self->fullscreen_active())
509 return;
510
511 self->grabKeyboard();
512#endif
513}
514
515
Peter Åstrand49b11572012-08-01 08:09:09 +0000516#define _NET_WM_STATE_ADD 1 /* add/set property */
517void DesktopWindow::maximizeWindow()
518{
519#if defined(WIN32)
520 WINDOWPLACEMENT wp;
521 wp.length = sizeof(WINDOWPLACEMENT);
522 GetWindowPlacement(fl_xid(this), &wp);
523 wp.showCmd = SW_MAXIMIZE;
524 SetWindowPlacement(fl_xid(this), &wp);
525#elif defined(__APPLE__)
526 /* OS X is somewhat strange and does not really have a concept of a
527 maximized window, so we can simply resize the window to the workarea */
528 int X, Y, W, H;
529 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
530 size(W, H);
531#else
532 // X11
533 fl_open_display();
534 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
535 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
536 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
537
538 XEvent e;
539 e.xany.type = ClientMessage;
540 e.xany.window = fl_xid(this);
541 e.xclient.message_type = net_wm_state;
542 e.xclient.format = 32;
543 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
544 e.xclient.data.l[1] = net_wm_state_maximized_vert;
545 e.xclient.data.l[2] = net_wm_state_maximized_horz;
546 e.xclient.data.l[3] = 0;
547 e.xclient.data.l[4] = 0;
548 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
549#endif
550}
551
552
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000553void DesktopWindow::handleDesktopSize()
554{
555 int width, height;
556
557 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
558 return;
559
560 remoteResize(width, height);
561}
562
563
Pierre Ossmanff473402012-07-04 11:27:47 +0000564void DesktopWindow::handleResizeTimeout(void *data)
565{
566 DesktopWindow *self = (DesktopWindow *)data;
567
568 assert(self);
569
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000570 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000571}
572
573
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000574void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000575{
Pierre Ossmanff473402012-07-04 11:27:47 +0000576 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000577 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000578
Pierre Ossmanaae38912012-07-13 11:22:55 +0000579#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000580 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000581#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000582 // In windowed mode (or the framebuffer is so large that we need
583 // to scroll) we just report a single virtual screen that covers
584 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000585
Pierre Ossmanaae38912012-07-13 11:22:55 +0000586 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000587
Pierre Ossmanaae38912012-07-13 11:22:55 +0000588 // Not sure why we have no screens, but adding a new one should be
589 // safe as there is nothing to conflict with...
590 if (layout.num_screens() == 0)
591 layout.add_screen(rfb::Screen());
592 else if (layout.num_screens() != 1) {
593 // More than one screen. Remove all but the first (which we
594 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000595
Pierre Ossmanaae38912012-07-13 11:22:55 +0000596 while (true) {
597 iter = layout.begin();
598 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000599
Pierre Ossmanaae38912012-07-13 11:22:55 +0000600 if (iter == layout.end())
601 break;
602
603 layout.remove_screen(iter->id);
604 }
605 }
606
607 // Resize the remaining single screen to the complete framebuffer
608 layout.begin()->dimensions.tl.x = 0;
609 layout.begin()->dimensions.tl.y = 0;
610 layout.begin()->dimensions.br.x = width;
611 layout.begin()->dimensions.br.y = height;
612#ifdef HAVE_FLTK_FULLSCREEN
613 } else {
614 int i;
615 rdr::U32 id;
616 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000617 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000618
619 // In full screen we report all screens that are fully covered.
620
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000621 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
622 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000623
Pierre Ossmanaae38912012-07-13 11:22:55 +0000624 // If we can find a matching screen in the existing set, we use
625 // that, otherwise we create a brand new screen.
626 //
627 // FIXME: We should really track screens better so we can handle
628 // a resized one.
629 //
630 for (i = 0;i < Fl::screen_count();i++) {
631 Fl::screen_xywh(sx, sy, sw, sh, i);
632
Pierre Ossman93d2d922012-07-20 12:32:52 +0000633 // Check that the screen is fully inside the framebuffer
634 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000635 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000636 continue;
637
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000638 // Adjust the coordinates so they are relative to our viewport
639 sx -= viewport_rect.tl.x;
640 sy -= viewport_rect.tl.y;
641
Pierre Ossmanaae38912012-07-13 11:22:55 +0000642 // Look for perfectly matching existing screen...
643 for (iter = cc->cp.screenLayout.begin();
644 iter != cc->cp.screenLayout.end(); ++iter) {
645 if ((iter->dimensions.tl.x == sx) &&
646 (iter->dimensions.tl.y == sy) &&
647 (iter->dimensions.width() == sw) &&
648 (iter->dimensions.height() == sh))
649 break;
650 }
651
652 // Found it?
653 if (iter != cc->cp.screenLayout.end()) {
654 layout.add_screen(*iter);
655 continue;
656 }
657
658 // Need to add a new one, which means we need to find an unused id
659 while (true) {
660 id = rand();
661 for (iter = cc->cp.screenLayout.begin();
662 iter != cc->cp.screenLayout.end(); ++iter) {
663 if (iter->id == id)
664 break;
665 }
666
667 if (iter == cc->cp.screenLayout.end())
668 break;
669 }
670
671 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000672 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000673
674 // If the viewport doesn't match a physical screen, then we might
675 // end up with no screens in the layout. Add a fake one...
676 if (layout.num_screens() == 0)
677 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000678 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000679#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000680
681 // Do we actually change anything?
682 if ((width == cc->cp.width) &&
683 (height == cc->cp.height) &&
684 (layout == cc->cp.screenLayout))
685 return;
686
Pierre Ossmanaae38912012-07-13 11:22:55 +0000687 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
688 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
689
690 if (!layout.validate(width, height)) {
691 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000692 return;
693 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000694
695 cc->writer()->writeSetDesktopSize(width, height, layout);
696}
697
698
Pierre Ossman6455d852011-06-01 09:33:00 +0000699void DesktopWindow::repositionViewport()
700{
701 int new_x, new_y;
702
703 // Deal with some scrolling corner cases:
704 //
705 // a) If the window is larger then the viewport, center the viewport.
706 // b) If the window is smaller than the viewport, make sure there is
707 // no wasted space on the sides.
708 //
709 // FIXME: Doesn't compensate for scroll widget size properly.
710
711 new_x = viewport->x();
712 new_y = viewport->y();
713
714 if (w() > viewport->w())
715 new_x = (w() - viewport->w()) / 2;
716 else {
717 if (viewport->x() > 0)
718 new_x = 0;
719 else if (w() > (viewport->x() + viewport->w()))
720 new_x = w() - viewport->w();
721 }
722
723 // Same thing for y axis
724 if (h() > viewport->h())
725 new_y = (h() - viewport->h()) / 2;
726 else {
727 if (viewport->y() > 0)
728 new_y = 0;
729 else if (h() > (viewport->y() + viewport->h()))
730 new_y = h() - viewport->h();
731 }
732
733 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
734 viewport->position(new_x, new_y);
735
736 // The scroll widget does not notice when you move around child widgets,
737 // so redraw everything to make sure things update.
738 redraw();
739 }
740}
741
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000742void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
743{
744 exit_vncviewer();
745}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000746
747
748void DesktopWindow::handleOptions(void *data)
749{
750 DesktopWindow *self = (DesktopWindow*)data;
751
752#ifdef HAVE_FLTK_FULLSCREEN
753 if (self->fullscreen_active() && fullscreenSystemKeys)
754 self->grabKeyboard();
755 else
756 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000757
Pierre Ossmanff473402012-07-04 11:27:47 +0000758 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000759 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000760 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000761 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000762#endif
763}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000764
765void DesktopWindow::handleFullscreenTimeout(void *data)
766{
767 DesktopWindow *self = (DesktopWindow *)data;
768
769 assert(self);
770
771 self->delayedFullscreen = false;
772
773 if (self->delayedDesktopSize) {
774 self->handleDesktopSize();
775 self->delayedDesktopSize = false;
776 }
777}