blob: ef5ec005c027ab13e67ed2e78b516c8246e81fcd [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 Ossman1d867b62012-09-03 09:25:07 +000049#define EDGE_SCROLL_SIZE 32
50#define EDGE_SCROLL_SPEED 20
51
Pierre Ossman5156d5e2011-03-09 09:42:34 +000052using namespace rfb;
53
Pierre Ossman5156d5e2011-03-09 09:42:34 +000054static rfb::LogWriter vlog("DesktopWindow");
55
56DesktopWindow::DesktopWindow(int w, int h, const char *name,
57 const rfb::PixelFormat& serverPF,
58 CConn* cc_)
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000059 : Fl_Window(w, h), cc(cc_), firstUpdate(true),
60 delayedFullscreen(false), delayedDesktopSize(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000061{
Pierre Ossman1d867b62012-09-03 09:25:07 +000062 scroll = new Fl_Scroll(0, 0, w, h);
Pierre Ossman4ae229f2011-04-15 12:58:31 +000063 scroll->color(FL_BLACK);
64
65 // Automatically adjust the scroll box to the window
66 resizable(scroll);
67
Pierre Ossmanff473402012-07-04 11:27:47 +000068 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000069
Pierre Ossman4ae229f2011-04-15 12:58:31 +000070 scroll->end();
71
Pierre Ossman5156d5e2011-03-09 09:42:34 +000072 callback(handleClose, this);
73
74 setName(name);
75
Pierre Ossman407a5c32011-05-26 14:48:29 +000076 OptionsDialog::addCallback(handleOptions, this);
77
Pierre Ossmana4f0f182011-06-14 13:36:57 +000078 // Hack. See below...
79 Fl::event_dispatch(&fltkHandle);
80
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000081 // Support for -geometry option. Note that although we do support
82 // negative coordinates, we do not support -XOFF-YOFF (ie
83 // coordinates relative to the right edge / bottom edge) at this
84 // time.
85 int geom_x = 0, geom_y = 0;
86 if (geometry.hasBeenSet()) {
87 int matched;
88 matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
89 if (matched == 2) {
90 force_position(1);
91 } else {
92 int geom_w, geom_h;
93 matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
94 switch (matched) {
95 case 4:
96 force_position(1);
97 /* fall through */
98 case 2:
99 w = geom_w;
100 h = geom_h;
101 break;
102 default:
103 geom_x = geom_y = 0;
104 vlog.error("Invalid geometry specified!");
105 }
106 }
107 }
108
109 // If we are creating a window which is equal to the size on the
110 // screen on X11, many WMs will treat this as a legacy fullscreen
111 // request. This is not what we want. Besides, it doesn't really
112 // make sense to try to create a window which is larger than the
113 // available work space.
114 w = __rfbmin(w, Fl::w());
115 h = __rfbmin(h, Fl::h());
116
117 if (force_position()) {
118 resize(geom_x, geom_y, w, h);
119 } else {
120 size(w, h);
121 }
122
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000123#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000124 if (fullScreen) {
125 // Hack: Window managers seem to be rather crappy at respecting
126 // fullscreen hints on initial windows. So on X11 we'll have to
127 // wait until after we've been mapped.
128#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000129 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000130#else
131 delayedFullscreen = true;
132#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000133 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000134#endif
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000135
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000136 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000137
Peter Åstrand49b11572012-08-01 08:09:09 +0000138 // Unfortunately, current FLTK does not allow us to set the
139 // maximized property before showing the window. See STR #2083 and
140 // STR #2178
141 if (maximize) {
142 maximizeWindow();
143 }
144
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000145 // The window manager might give us an initial window size that is different
146 // than the one we requested, and in those cases we need to manually adjust
147 // the scroll widget for things to behave sanely.
148 if ((w != this->w()) || (h != this->h()))
149 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000150
151#ifdef HAVE_FLTK_FULLSCREEN
152 if (delayedFullscreen) {
153 // Hack: Fullscreen requests may be ignored, so we need a timeout for
154 // when we should stop waiting. We also really need to wait for the
155 // resize, which can come after the fullscreen event.
156 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
157 fullscreen_on();
158 }
159#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000160}
161
162
163DesktopWindow::~DesktopWindow()
164{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000165 // Unregister all timeouts in case they get a change tro trigger
166 // again later when this object is already gone.
167 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000168 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000169 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000170 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000171
172 OptionsDialog::removeCallback(handleOptions);
173
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000174 // FLTK automatically deletes all child widgets, so we shouldn't touch
175 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000176}
177
178
179void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
180{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000181 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000182}
183
184
185const rfb::PixelFormat &DesktopWindow::getPreferredPF()
186{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000187 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000188}
189
190
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000191void DesktopWindow::setName(const char *name)
192{
193 CharArray windowNameStr;
194 windowNameStr.replaceBuf(new char[256]);
195
Pierre Ossman27820ba2011-09-30 12:54:24 +0000196 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000197
198 copy_label(windowNameStr.buf);
199}
200
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000201
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000202void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
203 rdr::U16* rgbs)
204{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000205 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000206}
207
208
209// Copy the areas of the framebuffer that have been changed (damaged)
210// to the displayed window.
211
212void DesktopWindow::updateWindow()
213{
Pierre Ossmanff473402012-07-04 11:27:47 +0000214 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000215 if (cc->cp.supportsSetDesktopSize) {
216 // Hack: Wait until we're in the proper mode and position until
217 // resizing things, otherwise we might send the wrong thing.
218 if (delayedFullscreen)
219 delayedDesktopSize = true;
220 else
221 handleDesktopSize();
222 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000223 firstUpdate = false;
224 }
225
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000226 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000227}
228
229
Pierre Ossman6455d852011-06-01 09:33:00 +0000230void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
231{
232 if ((new_w == viewport->w()) && (new_h == viewport->h()))
233 return;
234
Pierre Ossman6455d852011-06-01 09:33:00 +0000235 // If we're letting the viewport match the window perfectly, then
236 // keep things that way for the new size, otherwise just keep things
237 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000238#ifdef HAVE_FLTK_FULLSCREEN
239 if (!fullscreen_active()) {
240#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000241 if ((w() == viewport->w()) && (h() == viewport->h()))
242 size(new_w, new_h);
243 else {
244 // Make sure the window isn't too big. We do this manually because
245 // we have to disable the window size restriction (and it isn't
246 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000247 if ((w() > new_w) || (h() > new_h))
248 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000249 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000250#ifdef HAVE_FLTK_FULLSCREEN
251 }
252#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000253
254 viewport->size(new_w, new_h);
255
256 // We might not resize the main window, so we need to manually call this
257 // to make sure the viewport is centered.
258 repositionViewport();
259
Pierre Ossman6455d852011-06-01 09:33:00 +0000260 // repositionViewport() makes sure the scroll widget notices any changes
261 // in position, but it might be just the size that changes so we also
262 // need a poke here as well.
263 redraw();
264}
265
266
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000267void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
268 void* data, void* mask)
269{
270 viewport->setCursor(width, height, hotspot, data, mask);
271}
272
273
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000274void DesktopWindow::resize(int x, int y, int w, int h)
275{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000276 bool resizing;
277
278 if ((this->w() != w) || (this->h() != h))
279 resizing = true;
280 else
281 resizing = false;
282
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000283 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000284
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000285 if (resizing) {
286 // Try to get the remote size to match our window size, provided
287 // the following conditions are true:
288 //
289 // a) The user has this feature turned on
290 // b) The server supports it
291 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000292 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000293 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000294 if (not firstUpdate and not delayedFullscreen and
295 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000296 // We delay updating the remote desktop as we tend to get a flood
297 // of resize events as the user is dragging the window.
298 Fl::remove_timeout(handleResizeTimeout, this);
299 Fl::add_timeout(0.5, handleResizeTimeout, this);
300 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000301
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000302 // Deal with some scrolling corner cases
303 repositionViewport();
304 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000305}
306
307
Pierre Ossman407a5c32011-05-26 14:48:29 +0000308int DesktopWindow::handle(int event)
309{
310 switch (event) {
311#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000312 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000313 fullScreen.setParam(fullscreen_active());
314
Pierre Ossman1d867b62012-09-03 09:25:07 +0000315 if (fullscreen_active())
316 scroll->type(0);
317 else
318 scroll->type(Fl_Scroll::BOTH);
319
Pierre Ossman407a5c32011-05-26 14:48:29 +0000320 if (!fullscreenSystemKeys)
321 break;
322
323 if (fullscreen_active())
324 grabKeyboard();
325 else
326 ungrabKeyboard();
327
328 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000329
330 case FL_ENTER:
331 case FL_LEAVE:
332 case FL_DRAG:
333 case FL_MOVE:
334 if (fullscreen_active()) {
335 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
336 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
337 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
338 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
339 if (!Fl::has_timeout(handleEdgeScroll, this))
340 Fl::add_timeout(0.1, handleEdgeScroll, this);
341 }
342 }
343 return 1;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000344#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000345
Pierre Ossman407a5c32011-05-26 14:48:29 +0000346 case FL_SHORTCUT:
347 // Sometimes the focus gets out of whack and we fall through to the
348 // shortcut dispatching. Try to make things sane again...
349 if (Fl::focus() == NULL) {
350 take_focus();
351 Fl::handle(FL_KEYDOWN, this);
352 }
353 return 1;
354 }
355
356 return Fl_Window::handle(event);
357}
358
359
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000360int DesktopWindow::fltkHandle(int event, Fl_Window *win)
361{
362 int ret;
363
364 ret = Fl::handle_(event, win);
365
366#ifdef HAVE_FLTK_FULLSCREEN
367 // This is hackish and the result of the dodgy focus handling in FLTK.
368 // The basic problem is that FLTK's view of focus and the system's tend
369 // to differ, and as a result we do not see all the FL_FOCUS events we
370 // need. Fortunately we can grab them here...
371
372 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
373
374 if (dw && fullscreenSystemKeys) {
375 switch (event) {
376 case FL_FOCUS:
377 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
378 // some issues we need to work around:
379 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
380 // b) Gaining focus on the system level causes FLTK to switch
381 // window level on OS X.
382 if (dw->fullscreen_active())
383 dw->grabKeyboard();
384 break;
385
386 case FL_UNFOCUS:
387 // FIXME: We need to relinquish control when the entire window loses
388 // focus as it is very tied to this specific window on some
389 // platforms and we want to be able to open subwindows.
390 dw->ungrabKeyboard();
391 break;
392 }
393 }
394#endif
395
396 return ret;
397}
398
399
Pierre Ossmanaae38912012-07-13 11:22:55 +0000400void DesktopWindow::fullscreen_on()
401{
402#ifdef HAVE_FLTK_FULLSCREEN
403#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
404 if (not fullScreenAllMonitors)
405 fullscreen_screens(-1, -1, -1, -1);
406 else {
407 int top, bottom, left, right;
408 int top_y, bottom_y, left_x, right_x;
409
410 int sx, sy, sw, sh;
411
412 top = bottom = left = right = 0;
413
414 Fl::screen_xywh(sx, sy, sw, sh, 0);
415 top_y = sy;
416 bottom_y = sy + sh;
417 left_x = sx;
418 right_x = sx + sw;
419
420 for (int i = 1;i < Fl::screen_count();i++) {
421 Fl::screen_xywh(sx, sy, sw, sh, i);
422 if (sy < top_y) {
423 top = i;
424 top_y = sy;
425 }
426 if ((sy + sh) > bottom_y) {
427 bottom = i;
428 bottom_y = sy + sh;
429 }
430 if (sx < left_x) {
431 left = i;
432 left_x = sx;
433 }
434 if ((sx + sw) > right_x) {
435 right = i;
436 right_x = sx + sw;
437 }
438 }
439
440 fullscreen_screens(top, bottom, left, right);
441 }
442#endif // HAVE_FLTK_FULLSCREEN_SCREENS
443
444 fullscreen();
445#endif // HAVE_FLTK_FULLSCREEN
446}
447
Pierre Ossman407a5c32011-05-26 14:48:29 +0000448void DesktopWindow::grabKeyboard()
449{
450 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
451 // correct widget regardless of which low level window got the system
452 // event.
453
454 // FIXME: Push this stuff into FLTK.
455
456#if defined(WIN32)
457 int ret;
458
459 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
460 if (ret != 0)
461 vlog.error(_("Failure grabbing keyboard"));
462#elif defined(__APPLE__)
463 int ret;
464
Pierre Ossman3d759112012-08-27 14:40:51 +0000465 ret = cocoa_capture_display(this,
466#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
467 fullScreenAllMonitors
468#else
469 false
470#endif
471 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000472 if (ret != 0)
473 vlog.error(_("Failure grabbing keyboard"));
474#else
475 int ret;
476
477 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000478 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000479 if (ret) {
480 if (ret == AlreadyGrabbed) {
481 // It seems like we can race with the WM in some cases.
482 // Try again in a bit.
483 if (!Fl::has_timeout(handleGrab, this))
484 Fl::add_timeout(0.500, handleGrab, this);
485 } else {
486 vlog.error(_("Failure grabbing keyboard"));
487 }
488 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000489
490 // We also need to grab the pointer as some WMs like to grab buttons
491 // combined with modifies (e.g. Alt+Button0 in metacity).
492 ret = XGrabPointer(fl_display, fl_xid(this), True,
493 ButtonPressMask|ButtonReleaseMask|
494 ButtonMotionMask|PointerMotionMask,
495 GrabModeAsync, GrabModeAsync,
496 None, None, CurrentTime);
497 if (ret)
498 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000499#endif
500}
501
502
503void DesktopWindow::ungrabKeyboard()
504{
505 Fl::remove_timeout(handleGrab, this);
506
507#if defined(WIN32)
508 win32_disable_lowlevel_keyboard(fl_xid(this));
509#elif defined(__APPLE__)
510 cocoa_release_display(this);
511#else
512 // FLTK has a grab so lets not mess with it
513 if (Fl::grab())
514 return;
515
Pierre Ossman53fd5442011-11-17 10:19:19 +0000516 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000517 XUngrabKeyboard(fl_display, fl_event_time);
518#endif
519}
520
521
522void DesktopWindow::handleGrab(void *data)
523{
524 DesktopWindow *self = (DesktopWindow*)data;
525
526 assert(self);
527
528#ifdef HAVE_FLTK_FULLSCREEN
529 if (!fullscreenSystemKeys)
530 return;
531 if (!self->fullscreen_active())
532 return;
533
534 self->grabKeyboard();
535#endif
536}
537
538
Peter Åstrand49b11572012-08-01 08:09:09 +0000539#define _NET_WM_STATE_ADD 1 /* add/set property */
540void DesktopWindow::maximizeWindow()
541{
542#if defined(WIN32)
543 WINDOWPLACEMENT wp;
544 wp.length = sizeof(WINDOWPLACEMENT);
545 GetWindowPlacement(fl_xid(this), &wp);
546 wp.showCmd = SW_MAXIMIZE;
547 SetWindowPlacement(fl_xid(this), &wp);
548#elif defined(__APPLE__)
549 /* OS X is somewhat strange and does not really have a concept of a
550 maximized window, so we can simply resize the window to the workarea */
551 int X, Y, W, H;
552 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
553 size(W, H);
554#else
555 // X11
556 fl_open_display();
557 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
558 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
559 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
560
561 XEvent e;
562 e.xany.type = ClientMessage;
563 e.xany.window = fl_xid(this);
564 e.xclient.message_type = net_wm_state;
565 e.xclient.format = 32;
566 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
567 e.xclient.data.l[1] = net_wm_state_maximized_vert;
568 e.xclient.data.l[2] = net_wm_state_maximized_horz;
569 e.xclient.data.l[3] = 0;
570 e.xclient.data.l[4] = 0;
571 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
572#endif
573}
574
575
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000576void DesktopWindow::handleDesktopSize()
577{
578 int width, height;
579
580 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
581 return;
582
583 remoteResize(width, height);
584}
585
586
Pierre Ossmanff473402012-07-04 11:27:47 +0000587void DesktopWindow::handleResizeTimeout(void *data)
588{
589 DesktopWindow *self = (DesktopWindow *)data;
590
591 assert(self);
592
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000593 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000594}
595
596
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000597void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000598{
Pierre Ossmanff473402012-07-04 11:27:47 +0000599 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000600 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000601
Pierre Ossmanaae38912012-07-13 11:22:55 +0000602#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000603 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000604#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000605 // In windowed mode (or the framebuffer is so large that we need
606 // to scroll) we just report a single virtual screen that covers
607 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000608
Pierre Ossmanaae38912012-07-13 11:22:55 +0000609 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000610
Pierre Ossmanaae38912012-07-13 11:22:55 +0000611 // Not sure why we have no screens, but adding a new one should be
612 // safe as there is nothing to conflict with...
613 if (layout.num_screens() == 0)
614 layout.add_screen(rfb::Screen());
615 else if (layout.num_screens() != 1) {
616 // More than one screen. Remove all but the first (which we
617 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000618
Pierre Ossmanaae38912012-07-13 11:22:55 +0000619 while (true) {
620 iter = layout.begin();
621 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000622
Pierre Ossmanaae38912012-07-13 11:22:55 +0000623 if (iter == layout.end())
624 break;
625
626 layout.remove_screen(iter->id);
627 }
628 }
629
630 // Resize the remaining single screen to the complete framebuffer
631 layout.begin()->dimensions.tl.x = 0;
632 layout.begin()->dimensions.tl.y = 0;
633 layout.begin()->dimensions.br.x = width;
634 layout.begin()->dimensions.br.y = height;
635#ifdef HAVE_FLTK_FULLSCREEN
636 } else {
637 int i;
638 rdr::U32 id;
639 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000640 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000641
642 // In full screen we report all screens that are fully covered.
643
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000644 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
645 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000646
Pierre Ossmanaae38912012-07-13 11:22:55 +0000647 // If we can find a matching screen in the existing set, we use
648 // that, otherwise we create a brand new screen.
649 //
650 // FIXME: We should really track screens better so we can handle
651 // a resized one.
652 //
653 for (i = 0;i < Fl::screen_count();i++) {
654 Fl::screen_xywh(sx, sy, sw, sh, i);
655
Pierre Ossman93d2d922012-07-20 12:32:52 +0000656 // Check that the screen is fully inside the framebuffer
657 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000658 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000659 continue;
660
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000661 // Adjust the coordinates so they are relative to our viewport
662 sx -= viewport_rect.tl.x;
663 sy -= viewport_rect.tl.y;
664
Pierre Ossmanaae38912012-07-13 11:22:55 +0000665 // Look for perfectly matching existing screen...
666 for (iter = cc->cp.screenLayout.begin();
667 iter != cc->cp.screenLayout.end(); ++iter) {
668 if ((iter->dimensions.tl.x == sx) &&
669 (iter->dimensions.tl.y == sy) &&
670 (iter->dimensions.width() == sw) &&
671 (iter->dimensions.height() == sh))
672 break;
673 }
674
675 // Found it?
676 if (iter != cc->cp.screenLayout.end()) {
677 layout.add_screen(*iter);
678 continue;
679 }
680
681 // Need to add a new one, which means we need to find an unused id
682 while (true) {
683 id = rand();
684 for (iter = cc->cp.screenLayout.begin();
685 iter != cc->cp.screenLayout.end(); ++iter) {
686 if (iter->id == id)
687 break;
688 }
689
690 if (iter == cc->cp.screenLayout.end())
691 break;
692 }
693
694 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000695 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000696
697 // If the viewport doesn't match a physical screen, then we might
698 // end up with no screens in the layout. Add a fake one...
699 if (layout.num_screens() == 0)
700 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000701 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000702#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000703
704 // Do we actually change anything?
705 if ((width == cc->cp.width) &&
706 (height == cc->cp.height) &&
707 (layout == cc->cp.screenLayout))
708 return;
709
Pierre Ossmanaae38912012-07-13 11:22:55 +0000710 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
711 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
712
713 if (!layout.validate(width, height)) {
714 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000715 return;
716 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000717
718 cc->writer()->writeSetDesktopSize(width, height, layout);
719}
720
721
Pierre Ossman6455d852011-06-01 09:33:00 +0000722void DesktopWindow::repositionViewport()
723{
724 int new_x, new_y;
725
726 // Deal with some scrolling corner cases:
727 //
728 // a) If the window is larger then the viewport, center the viewport.
729 // b) If the window is smaller than the viewport, make sure there is
730 // no wasted space on the sides.
731 //
732 // FIXME: Doesn't compensate for scroll widget size properly.
733
734 new_x = viewport->x();
735 new_y = viewport->y();
736
737 if (w() > viewport->w())
738 new_x = (w() - viewport->w()) / 2;
739 else {
740 if (viewport->x() > 0)
741 new_x = 0;
742 else if (w() > (viewport->x() + viewport->w()))
743 new_x = w() - viewport->w();
744 }
745
746 // Same thing for y axis
747 if (h() > viewport->h())
748 new_y = (h() - viewport->h()) / 2;
749 else {
750 if (viewport->y() > 0)
751 new_y = 0;
752 else if (h() > (viewport->y() + viewport->h()))
753 new_y = h() - viewport->h();
754 }
755
756 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
757 viewport->position(new_x, new_y);
758
759 // The scroll widget does not notice when you move around child widgets,
760 // so redraw everything to make sure things update.
761 redraw();
762 }
763}
764
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000765void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
766{
767 exit_vncviewer();
768}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000769
770
771void DesktopWindow::handleOptions(void *data)
772{
773 DesktopWindow *self = (DesktopWindow*)data;
774
775#ifdef HAVE_FLTK_FULLSCREEN
776 if (self->fullscreen_active() && fullscreenSystemKeys)
777 self->grabKeyboard();
778 else
779 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000780
Pierre Ossmanff473402012-07-04 11:27:47 +0000781 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000782 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000783 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000784 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000785#endif
786}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000787
788void DesktopWindow::handleFullscreenTimeout(void *data)
789{
790 DesktopWindow *self = (DesktopWindow *)data;
791
792 assert(self);
793
794 self->delayedFullscreen = false;
795
796 if (self->delayedDesktopSize) {
797 self->handleDesktopSize();
798 self->delayedDesktopSize = false;
799 }
800}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000801
802void DesktopWindow::handleEdgeScroll(void *data)
803{
804#ifdef HAVE_FLTK_FULLSCREEN
805 DesktopWindow *self = (DesktopWindow *)data;
806
807 int mx, my;
808 int dx, dy;
809
810 assert(self);
811
812 if (!self->fullscreen_active())
813 return;
814
815 mx = Fl::event_x();
816 my = Fl::event_y();
817
818 dx = dy = 0;
819
820 // Clamp mouse position in case it is outside the window
821 if (mx < 0)
822 mx = 0;
823 if (mx > self->w())
824 mx = self->w();
825 if (my < 0)
826 my = 0;
827 if (my > self->h())
828 my = self->h();
829
830 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
831 dx = EDGE_SCROLL_SPEED - EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
832 if ((self->viewport->x() + self->viewport->w() > self->w()) && (mx > self->w() - EDGE_SCROLL_SIZE))
833 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE - EDGE_SCROLL_SPEED;
834 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
835 dy = EDGE_SCROLL_SPEED - EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
836 if ((self->viewport->y() + self->viewport->h() > self->h()) && (my > self->h() - EDGE_SCROLL_SIZE))
837 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE - EDGE_SCROLL_SPEED;
838
839 if ((dx == 0) && (dy == 0))
840 return;
841
842 // Make sure we don't move the viewport too much
843 if (self->viewport->x() + dx > 0)
844 dx = -self->viewport->x();
845 if (self->viewport->x() + dx + self->viewport->w() < self->w())
846 dx = self->w() - (self->viewport->x() + self->viewport->w());
847 if (self->viewport->y() + dy > 0)
848 dy = -self->viewport->y();
849 if (self->viewport->y() + dy + self->viewport->h() < self->h())
850 dy = self->h() - (self->viewport->y() + self->viewport->h());
851
852 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
853
854 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
855#endif
856}