blob: f35457d7c1fd7d495c2ca5598a047bba2921b563 [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
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000109 if (force_position()) {
110 resize(geom_x, geom_y, w, h);
111 } else {
112 size(w, h);
113 }
114
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000115#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000116 if (fullScreen) {
117 // Hack: Window managers seem to be rather crappy at respecting
118 // fullscreen hints on initial windows. So on X11 we'll have to
119 // wait until after we've been mapped.
120#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000121 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000122#else
123 delayedFullscreen = true;
124#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000125 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000126#endif
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000127
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000128 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000129
Peter Åstrand49b11572012-08-01 08:09:09 +0000130 // Unfortunately, current FLTK does not allow us to set the
131 // maximized property before showing the window. See STR #2083 and
132 // STR #2178
133 if (maximize) {
134 maximizeWindow();
135 }
136
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000137 // The window manager might give us an initial window size that is different
138 // than the one we requested, and in those cases we need to manually adjust
139 // the scroll widget for things to behave sanely.
140 if ((w != this->w()) || (h != this->h()))
141 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000142
143#ifdef HAVE_FLTK_FULLSCREEN
144 if (delayedFullscreen) {
145 // Hack: Fullscreen requests may be ignored, so we need a timeout for
146 // when we should stop waiting. We also really need to wait for the
147 // resize, which can come after the fullscreen event.
148 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
149 fullscreen_on();
150 }
151#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000152}
153
154
155DesktopWindow::~DesktopWindow()
156{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000157 // Unregister all timeouts in case they get a change tro trigger
158 // again later when this object is already gone.
159 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000160 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000161 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000162 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000163
164 OptionsDialog::removeCallback(handleOptions);
165
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000166 // FLTK automatically deletes all child widgets, so we shouldn't touch
167 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000168}
169
170
171void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
172{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000173 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000174}
175
176
177const rfb::PixelFormat &DesktopWindow::getPreferredPF()
178{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000179 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000180}
181
182
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000183void DesktopWindow::setName(const char *name)
184{
185 CharArray windowNameStr;
186 windowNameStr.replaceBuf(new char[256]);
187
Pierre Ossman27820ba2011-09-30 12:54:24 +0000188 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000189
190 copy_label(windowNameStr.buf);
191}
192
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000193
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000194void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
195 rdr::U16* rgbs)
196{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000197 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000198}
199
200
201// Copy the areas of the framebuffer that have been changed (damaged)
202// to the displayed window.
203
204void DesktopWindow::updateWindow()
205{
Pierre Ossmanff473402012-07-04 11:27:47 +0000206 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000207 if (cc->cp.supportsSetDesktopSize) {
208 // Hack: Wait until we're in the proper mode and position until
209 // resizing things, otherwise we might send the wrong thing.
210 if (delayedFullscreen)
211 delayedDesktopSize = true;
212 else
213 handleDesktopSize();
214 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000215 firstUpdate = false;
216 }
217
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000218 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000219}
220
221
Pierre Ossman6455d852011-06-01 09:33:00 +0000222void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
223{
224 if ((new_w == viewport->w()) && (new_h == viewport->h()))
225 return;
226
Pierre Ossman6455d852011-06-01 09:33:00 +0000227 // If we're letting the viewport match the window perfectly, then
228 // keep things that way for the new size, otherwise just keep things
229 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000230#ifdef HAVE_FLTK_FULLSCREEN
231 if (!fullscreen_active()) {
232#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000233 if ((w() == viewport->w()) && (h() == viewport->h()))
234 size(new_w, new_h);
235 else {
236 // Make sure the window isn't too big. We do this manually because
237 // we have to disable the window size restriction (and it isn't
238 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000239 if ((w() > new_w) || (h() > new_h))
240 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000241 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000242#ifdef HAVE_FLTK_FULLSCREEN
243 }
244#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000245
246 viewport->size(new_w, new_h);
247
248 // We might not resize the main window, so we need to manually call this
249 // to make sure the viewport is centered.
250 repositionViewport();
251
Pierre Ossman6455d852011-06-01 09:33:00 +0000252 // repositionViewport() makes sure the scroll widget notices any changes
253 // in position, but it might be just the size that changes so we also
254 // need a poke here as well.
255 redraw();
256}
257
258
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000259void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
260 void* data, void* mask)
261{
262 viewport->setCursor(width, height, hotspot, data, mask);
263}
264
265
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000266void DesktopWindow::resize(int x, int y, int w, int h)
267{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000268 bool resizing;
269
Pierre Ossman0e593462012-09-03 09:43:23 +0000270#if ! (defined(WIN32) || defined(__APPLE__))
271 // X11 window managers will treat a resize to cover the entire
272 // monitor as a request to go full screen. Make sure we avoid this.
273 //
274 // FIXME: If this is an external event then this code will get
275 // FLTK into a confused state about the window's position/size.
276 // Unfortunately FLTK doesn't offer an easy way to tell
277 // what kind of resize it is. Let's hope this corner case
278 // isn't too common...
279#ifdef HAVE_FLTK_FULLSCREEN
280 if (!fullscreen_active())
281#endif
282 {
283 for (int i = 0;i < Fl::screen_count();i++) {
284 int sx, sy, sw, sh;
285
286 Fl::screen_xywh(sx, sy, sw, sh, i);
287
288 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
289 vlog.info("Adjusting window size to avoid accidental full screen request");
290 // Assume a panel of some form and adjust the height
291 y += 20;
292 h -= 40;
293 }
294 }
295 }
296#endif
297
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000298 if ((this->w() != w) || (this->h() != h))
299 resizing = true;
300 else
301 resizing = false;
302
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000303 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000304
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000305 if (resizing) {
306 // Try to get the remote size to match our window size, provided
307 // the following conditions are true:
308 //
309 // a) The user has this feature turned on
310 // b) The server supports it
311 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000312 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000313 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000314 if (not firstUpdate and not delayedFullscreen and
315 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000316 // We delay updating the remote desktop as we tend to get a flood
317 // of resize events as the user is dragging the window.
318 Fl::remove_timeout(handleResizeTimeout, this);
319 Fl::add_timeout(0.5, handleResizeTimeout, this);
320 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000321
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000322 // Deal with some scrolling corner cases
323 repositionViewport();
324 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000325}
326
327
Pierre Ossman407a5c32011-05-26 14:48:29 +0000328int DesktopWindow::handle(int event)
329{
330 switch (event) {
331#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000332 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000333 fullScreen.setParam(fullscreen_active());
334
Pierre Ossman1d867b62012-09-03 09:25:07 +0000335 if (fullscreen_active())
336 scroll->type(0);
337 else
338 scroll->type(Fl_Scroll::BOTH);
339
Pierre Ossman407a5c32011-05-26 14:48:29 +0000340 if (!fullscreenSystemKeys)
341 break;
342
343 if (fullscreen_active())
344 grabKeyboard();
345 else
346 ungrabKeyboard();
347
348 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000349
350 case FL_ENTER:
351 case FL_LEAVE:
352 case FL_DRAG:
353 case FL_MOVE:
354 if (fullscreen_active()) {
355 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
356 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
357 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
358 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
359 if (!Fl::has_timeout(handleEdgeScroll, this))
360 Fl::add_timeout(0.1, handleEdgeScroll, this);
361 }
362 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000363 // Continue processing so that the viewport also gets mouse events
364 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000365#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000366
Pierre Ossman407a5c32011-05-26 14:48:29 +0000367 case FL_SHORTCUT:
368 // Sometimes the focus gets out of whack and we fall through to the
369 // shortcut dispatching. Try to make things sane again...
370 if (Fl::focus() == NULL) {
371 take_focus();
372 Fl::handle(FL_KEYDOWN, this);
373 }
374 return 1;
375 }
376
377 return Fl_Window::handle(event);
378}
379
380
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000381int DesktopWindow::fltkHandle(int event, Fl_Window *win)
382{
383 int ret;
384
385 ret = Fl::handle_(event, win);
386
387#ifdef HAVE_FLTK_FULLSCREEN
388 // This is hackish and the result of the dodgy focus handling in FLTK.
389 // The basic problem is that FLTK's view of focus and the system's tend
390 // to differ, and as a result we do not see all the FL_FOCUS events we
391 // need. Fortunately we can grab them here...
392
393 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
394
395 if (dw && fullscreenSystemKeys) {
396 switch (event) {
397 case FL_FOCUS:
398 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
399 // some issues we need to work around:
400 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
401 // b) Gaining focus on the system level causes FLTK to switch
402 // window level on OS X.
403 if (dw->fullscreen_active())
404 dw->grabKeyboard();
405 break;
406
407 case FL_UNFOCUS:
408 // FIXME: We need to relinquish control when the entire window loses
409 // focus as it is very tied to this specific window on some
410 // platforms and we want to be able to open subwindows.
411 dw->ungrabKeyboard();
412 break;
413 }
414 }
415#endif
416
417 return ret;
418}
419
420
Pierre Ossmanaae38912012-07-13 11:22:55 +0000421void DesktopWindow::fullscreen_on()
422{
423#ifdef HAVE_FLTK_FULLSCREEN
424#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
425 if (not fullScreenAllMonitors)
426 fullscreen_screens(-1, -1, -1, -1);
427 else {
428 int top, bottom, left, right;
429 int top_y, bottom_y, left_x, right_x;
430
431 int sx, sy, sw, sh;
432
433 top = bottom = left = right = 0;
434
435 Fl::screen_xywh(sx, sy, sw, sh, 0);
436 top_y = sy;
437 bottom_y = sy + sh;
438 left_x = sx;
439 right_x = sx + sw;
440
441 for (int i = 1;i < Fl::screen_count();i++) {
442 Fl::screen_xywh(sx, sy, sw, sh, i);
443 if (sy < top_y) {
444 top = i;
445 top_y = sy;
446 }
447 if ((sy + sh) > bottom_y) {
448 bottom = i;
449 bottom_y = sy + sh;
450 }
451 if (sx < left_x) {
452 left = i;
453 left_x = sx;
454 }
455 if ((sx + sw) > right_x) {
456 right = i;
457 right_x = sx + sw;
458 }
459 }
460
461 fullscreen_screens(top, bottom, left, right);
462 }
463#endif // HAVE_FLTK_FULLSCREEN_SCREENS
464
465 fullscreen();
466#endif // HAVE_FLTK_FULLSCREEN
467}
468
Pierre Ossman407a5c32011-05-26 14:48:29 +0000469void DesktopWindow::grabKeyboard()
470{
471 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
472 // correct widget regardless of which low level window got the system
473 // event.
474
475 // FIXME: Push this stuff into FLTK.
476
477#if defined(WIN32)
478 int ret;
479
480 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
481 if (ret != 0)
482 vlog.error(_("Failure grabbing keyboard"));
483#elif defined(__APPLE__)
484 int ret;
485
Pierre Ossman3d759112012-08-27 14:40:51 +0000486 ret = cocoa_capture_display(this,
487#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
488 fullScreenAllMonitors
489#else
490 false
491#endif
492 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000493 if (ret != 0)
494 vlog.error(_("Failure grabbing keyboard"));
495#else
496 int ret;
497
498 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000499 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000500 if (ret) {
501 if (ret == AlreadyGrabbed) {
502 // It seems like we can race with the WM in some cases.
503 // Try again in a bit.
504 if (!Fl::has_timeout(handleGrab, this))
505 Fl::add_timeout(0.500, handleGrab, this);
506 } else {
507 vlog.error(_("Failure grabbing keyboard"));
508 }
509 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000510
511 // We also need to grab the pointer as some WMs like to grab buttons
512 // combined with modifies (e.g. Alt+Button0 in metacity).
513 ret = XGrabPointer(fl_display, fl_xid(this), True,
514 ButtonPressMask|ButtonReleaseMask|
515 ButtonMotionMask|PointerMotionMask,
516 GrabModeAsync, GrabModeAsync,
517 None, None, CurrentTime);
518 if (ret)
519 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000520#endif
521}
522
523
524void DesktopWindow::ungrabKeyboard()
525{
526 Fl::remove_timeout(handleGrab, this);
527
528#if defined(WIN32)
529 win32_disable_lowlevel_keyboard(fl_xid(this));
530#elif defined(__APPLE__)
531 cocoa_release_display(this);
532#else
533 // FLTK has a grab so lets not mess with it
534 if (Fl::grab())
535 return;
536
Pierre Ossman53fd5442011-11-17 10:19:19 +0000537 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000538 XUngrabKeyboard(fl_display, fl_event_time);
539#endif
540}
541
542
543void DesktopWindow::handleGrab(void *data)
544{
545 DesktopWindow *self = (DesktopWindow*)data;
546
547 assert(self);
548
549#ifdef HAVE_FLTK_FULLSCREEN
550 if (!fullscreenSystemKeys)
551 return;
552 if (!self->fullscreen_active())
553 return;
554
555 self->grabKeyboard();
556#endif
557}
558
559
Peter Åstrand49b11572012-08-01 08:09:09 +0000560#define _NET_WM_STATE_ADD 1 /* add/set property */
561void DesktopWindow::maximizeWindow()
562{
563#if defined(WIN32)
564 WINDOWPLACEMENT wp;
565 wp.length = sizeof(WINDOWPLACEMENT);
566 GetWindowPlacement(fl_xid(this), &wp);
567 wp.showCmd = SW_MAXIMIZE;
568 SetWindowPlacement(fl_xid(this), &wp);
569#elif defined(__APPLE__)
570 /* OS X is somewhat strange and does not really have a concept of a
571 maximized window, so we can simply resize the window to the workarea */
572 int X, Y, W, H;
573 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
574 size(W, H);
575#else
576 // X11
577 fl_open_display();
578 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
579 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
580 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
581
582 XEvent e;
583 e.xany.type = ClientMessage;
584 e.xany.window = fl_xid(this);
585 e.xclient.message_type = net_wm_state;
586 e.xclient.format = 32;
587 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
588 e.xclient.data.l[1] = net_wm_state_maximized_vert;
589 e.xclient.data.l[2] = net_wm_state_maximized_horz;
590 e.xclient.data.l[3] = 0;
591 e.xclient.data.l[4] = 0;
592 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
593#endif
594}
595
596
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000597void DesktopWindow::handleDesktopSize()
598{
599 int width, height;
600
601 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
602 return;
603
604 remoteResize(width, height);
605}
606
607
Pierre Ossmanff473402012-07-04 11:27:47 +0000608void DesktopWindow::handleResizeTimeout(void *data)
609{
610 DesktopWindow *self = (DesktopWindow *)data;
611
612 assert(self);
613
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000614 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000615}
616
617
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000618void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000619{
Pierre Ossmanff473402012-07-04 11:27:47 +0000620 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000621 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000622
Pierre Ossmanaae38912012-07-13 11:22:55 +0000623#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000624 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000625#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000626 // In windowed mode (or the framebuffer is so large that we need
627 // to scroll) we just report a single virtual screen that covers
628 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000629
Pierre Ossmanaae38912012-07-13 11:22:55 +0000630 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000631
Pierre Ossmanaae38912012-07-13 11:22:55 +0000632 // Not sure why we have no screens, but adding a new one should be
633 // safe as there is nothing to conflict with...
634 if (layout.num_screens() == 0)
635 layout.add_screen(rfb::Screen());
636 else if (layout.num_screens() != 1) {
637 // More than one screen. Remove all but the first (which we
638 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000639
Pierre Ossmanaae38912012-07-13 11:22:55 +0000640 while (true) {
641 iter = layout.begin();
642 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000643
Pierre Ossmanaae38912012-07-13 11:22:55 +0000644 if (iter == layout.end())
645 break;
646
647 layout.remove_screen(iter->id);
648 }
649 }
650
651 // Resize the remaining single screen to the complete framebuffer
652 layout.begin()->dimensions.tl.x = 0;
653 layout.begin()->dimensions.tl.y = 0;
654 layout.begin()->dimensions.br.x = width;
655 layout.begin()->dimensions.br.y = height;
656#ifdef HAVE_FLTK_FULLSCREEN
657 } else {
658 int i;
659 rdr::U32 id;
660 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000661 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000662
663 // In full screen we report all screens that are fully covered.
664
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000665 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
666 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000667
Pierre Ossmanaae38912012-07-13 11:22:55 +0000668 // If we can find a matching screen in the existing set, we use
669 // that, otherwise we create a brand new screen.
670 //
671 // FIXME: We should really track screens better so we can handle
672 // a resized one.
673 //
674 for (i = 0;i < Fl::screen_count();i++) {
675 Fl::screen_xywh(sx, sy, sw, sh, i);
676
Pierre Ossman93d2d922012-07-20 12:32:52 +0000677 // Check that the screen is fully inside the framebuffer
678 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000679 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000680 continue;
681
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000682 // Adjust the coordinates so they are relative to our viewport
683 sx -= viewport_rect.tl.x;
684 sy -= viewport_rect.tl.y;
685
Pierre Ossmanaae38912012-07-13 11:22:55 +0000686 // Look for perfectly matching existing screen...
687 for (iter = cc->cp.screenLayout.begin();
688 iter != cc->cp.screenLayout.end(); ++iter) {
689 if ((iter->dimensions.tl.x == sx) &&
690 (iter->dimensions.tl.y == sy) &&
691 (iter->dimensions.width() == sw) &&
692 (iter->dimensions.height() == sh))
693 break;
694 }
695
696 // Found it?
697 if (iter != cc->cp.screenLayout.end()) {
698 layout.add_screen(*iter);
699 continue;
700 }
701
702 // Need to add a new one, which means we need to find an unused id
703 while (true) {
704 id = rand();
705 for (iter = cc->cp.screenLayout.begin();
706 iter != cc->cp.screenLayout.end(); ++iter) {
707 if (iter->id == id)
708 break;
709 }
710
711 if (iter == cc->cp.screenLayout.end())
712 break;
713 }
714
715 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000716 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000717
718 // If the viewport doesn't match a physical screen, then we might
719 // end up with no screens in the layout. Add a fake one...
720 if (layout.num_screens() == 0)
721 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000722 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000723#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000724
725 // Do we actually change anything?
726 if ((width == cc->cp.width) &&
727 (height == cc->cp.height) &&
728 (layout == cc->cp.screenLayout))
729 return;
730
Pierre Ossmanaae38912012-07-13 11:22:55 +0000731 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
732 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
733
734 if (!layout.validate(width, height)) {
735 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000736 return;
737 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000738
739 cc->writer()->writeSetDesktopSize(width, height, layout);
740}
741
742
Pierre Ossman6455d852011-06-01 09:33:00 +0000743void DesktopWindow::repositionViewport()
744{
745 int new_x, new_y;
746
747 // Deal with some scrolling corner cases:
748 //
749 // a) If the window is larger then the viewport, center the viewport.
750 // b) If the window is smaller than the viewport, make sure there is
751 // no wasted space on the sides.
752 //
753 // FIXME: Doesn't compensate for scroll widget size properly.
754
755 new_x = viewport->x();
756 new_y = viewport->y();
757
758 if (w() > viewport->w())
759 new_x = (w() - viewport->w()) / 2;
760 else {
761 if (viewport->x() > 0)
762 new_x = 0;
763 else if (w() > (viewport->x() + viewport->w()))
764 new_x = w() - viewport->w();
765 }
766
767 // Same thing for y axis
768 if (h() > viewport->h())
769 new_y = (h() - viewport->h()) / 2;
770 else {
771 if (viewport->y() > 0)
772 new_y = 0;
773 else if (h() > (viewport->y() + viewport->h()))
774 new_y = h() - viewport->h();
775 }
776
777 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
778 viewport->position(new_x, new_y);
779
780 // The scroll widget does not notice when you move around child widgets,
781 // so redraw everything to make sure things update.
782 redraw();
783 }
784}
785
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000786void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
787{
788 exit_vncviewer();
789}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000790
791
792void DesktopWindow::handleOptions(void *data)
793{
794 DesktopWindow *self = (DesktopWindow*)data;
795
796#ifdef HAVE_FLTK_FULLSCREEN
797 if (self->fullscreen_active() && fullscreenSystemKeys)
798 self->grabKeyboard();
799 else
800 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000801
Pierre Ossmanff473402012-07-04 11:27:47 +0000802 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000803 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000804 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000805 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000806#endif
807}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000808
809void DesktopWindow::handleFullscreenTimeout(void *data)
810{
811 DesktopWindow *self = (DesktopWindow *)data;
812
813 assert(self);
814
815 self->delayedFullscreen = false;
816
817 if (self->delayedDesktopSize) {
818 self->handleDesktopSize();
819 self->delayedDesktopSize = false;
820 }
821}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000822
823void DesktopWindow::handleEdgeScroll(void *data)
824{
825#ifdef HAVE_FLTK_FULLSCREEN
826 DesktopWindow *self = (DesktopWindow *)data;
827
828 int mx, my;
829 int dx, dy;
830
831 assert(self);
832
833 if (!self->fullscreen_active())
834 return;
835
836 mx = Fl::event_x();
837 my = Fl::event_y();
838
839 dx = dy = 0;
840
841 // Clamp mouse position in case it is outside the window
842 if (mx < 0)
843 mx = 0;
844 if (mx > self->w())
845 mx = self->w();
846 if (my < 0)
847 my = 0;
848 if (my > self->h())
849 my = self->h();
850
851 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000852 dx = EDGE_SCROLL_SPEED -
853 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
854 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
855 (mx > self->w() - EDGE_SCROLL_SIZE))
856 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
857 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000858 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000859 dy = EDGE_SCROLL_SPEED -
860 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
861 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
862 (my > self->h() - EDGE_SCROLL_SIZE))
863 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
864 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000865
866 if ((dx == 0) && (dy == 0))
867 return;
868
869 // Make sure we don't move the viewport too much
870 if (self->viewport->x() + dx > 0)
871 dx = -self->viewport->x();
872 if (self->viewport->x() + dx + self->viewport->w() < self->w())
873 dx = self->w() - (self->viewport->x() + self->viewport->w());
874 if (self->viewport->y() + dy > 0)
875 dy = -self->viewport->y();
876 if (self->viewport->y() + dy + self->viewport->h() < self->h())
877 dy = self->h() - (self->viewport->y() + self->viewport->h());
878
879 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
880
881 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
882#endif
883}