blob: ed62de0f43710173ac393f85678d55cfb277147b [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:
Pierre Ossman9d267c52012-10-02 14:30:22 +000096 force_position(1);
97 /* fall through */
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000098 case 2:
Pierre Ossman9d267c52012-10-02 14:30:22 +000099 w = geom_w;
100 h = geom_h;
101 break;
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000102 default:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000103 geom_x = geom_y = 0;
104 vlog.error("Invalid geometry specified!");
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000105 }
106 }
107 }
108
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000109#ifdef __APPLE__
110 // On OS X we can do the maximize thing properly before the
111 // window is showned. Other platforms handled further down...
112 if (maximize) {
113 int dummy;
114 Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
115 }
116#endif
117
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000118 if (force_position()) {
119 resize(geom_x, geom_y, w, h);
120 } else {
121 size(w, h);
122 }
123
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000124#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000125 if (fullScreen) {
126 // Hack: Window managers seem to be rather crappy at respecting
127 // fullscreen hints on initial windows. So on X11 we'll have to
128 // wait until after we've been mapped.
129#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000130 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000131#else
132 delayedFullscreen = true;
133#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000134 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000135#endif
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000136
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000137 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000138
Peter Åstrand49b11572012-08-01 08:09:09 +0000139 // Unfortunately, current FLTK does not allow us to set the
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000140 // maximized property on Windows and X11 before showing the window.
141 // See STR #2083 and STR #2178
142#ifndef __APPLE__
Peter Åstrand49b11572012-08-01 08:09:09 +0000143 if (maximize) {
144 maximizeWindow();
145 }
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000146#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000147
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000148 // The window manager might give us an initial window size that is different
149 // than the one we requested, and in those cases we need to manually adjust
150 // the scroll widget for things to behave sanely.
151 if ((w != this->w()) || (h != this->h()))
152 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000153
154#ifdef HAVE_FLTK_FULLSCREEN
155 if (delayedFullscreen) {
156 // Hack: Fullscreen requests may be ignored, so we need a timeout for
157 // when we should stop waiting. We also really need to wait for the
158 // resize, which can come after the fullscreen event.
159 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
160 fullscreen_on();
161 }
162#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000163}
164
165
166DesktopWindow::~DesktopWindow()
167{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000168 // Unregister all timeouts in case they get a change tro trigger
169 // again later when this object is already gone.
170 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000171 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000172 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000173 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000174
175 OptionsDialog::removeCallback(handleOptions);
176
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000177 // FLTK automatically deletes all child widgets, so we shouldn't touch
178 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000179}
180
181
182void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
183{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000184 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000185}
186
187
188const rfb::PixelFormat &DesktopWindow::getPreferredPF()
189{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000190 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000191}
192
193
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000194void DesktopWindow::setName(const char *name)
195{
196 CharArray windowNameStr;
197 windowNameStr.replaceBuf(new char[256]);
198
Pierre Ossman27820ba2011-09-30 12:54:24 +0000199 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000200
201 copy_label(windowNameStr.buf);
202}
203
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000204
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000205void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
206 rdr::U16* rgbs)
207{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000208 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000209}
210
211
212// Copy the areas of the framebuffer that have been changed (damaged)
213// to the displayed window.
214
215void DesktopWindow::updateWindow()
216{
Pierre Ossmanff473402012-07-04 11:27:47 +0000217 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000218 if (cc->cp.supportsSetDesktopSize) {
219 // Hack: Wait until we're in the proper mode and position until
220 // resizing things, otherwise we might send the wrong thing.
221 if (delayedFullscreen)
222 delayedDesktopSize = true;
223 else
224 handleDesktopSize();
225 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000226 firstUpdate = false;
227 }
228
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000229 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000230}
231
232
Pierre Ossman6455d852011-06-01 09:33:00 +0000233void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
234{
235 if ((new_w == viewport->w()) && (new_h == viewport->h()))
236 return;
237
Pierre Ossman6455d852011-06-01 09:33:00 +0000238 // If we're letting the viewport match the window perfectly, then
239 // keep things that way for the new size, otherwise just keep things
240 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000241#ifdef HAVE_FLTK_FULLSCREEN
242 if (!fullscreen_active()) {
243#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000244 if ((w() == viewport->w()) && (h() == viewport->h()))
245 size(new_w, new_h);
246 else {
247 // Make sure the window isn't too big. We do this manually because
248 // we have to disable the window size restriction (and it isn't
249 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000250 if ((w() > new_w) || (h() > new_h))
251 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000252 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000253#ifdef HAVE_FLTK_FULLSCREEN
254 }
255#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000256
257 viewport->size(new_w, new_h);
258
259 // We might not resize the main window, so we need to manually call this
260 // to make sure the viewport is centered.
261 repositionViewport();
262
Pierre Ossman6455d852011-06-01 09:33:00 +0000263 // repositionViewport() makes sure the scroll widget notices any changes
264 // in position, but it might be just the size that changes so we also
265 // need a poke here as well.
266 redraw();
267}
268
269
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000270void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
271 void* data, void* mask)
272{
273 viewport->setCursor(width, height, hotspot, data, mask);
274}
275
276
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000277void DesktopWindow::resize(int x, int y, int w, int h)
278{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000279 bool resizing;
280
Pierre Ossman0e593462012-09-03 09:43:23 +0000281#if ! (defined(WIN32) || defined(__APPLE__))
282 // X11 window managers will treat a resize to cover the entire
283 // monitor as a request to go full screen. Make sure we avoid this.
284 //
285 // FIXME: If this is an external event then this code will get
286 // FLTK into a confused state about the window's position/size.
287 // Unfortunately FLTK doesn't offer an easy way to tell
288 // what kind of resize it is. Let's hope this corner case
289 // isn't too common...
290#ifdef HAVE_FLTK_FULLSCREEN
291 if (!fullscreen_active())
292#endif
293 {
294 for (int i = 0;i < Fl::screen_count();i++) {
295 int sx, sy, sw, sh;
296
297 Fl::screen_xywh(sx, sy, sw, sh, i);
298
299 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
300 vlog.info("Adjusting window size to avoid accidental full screen request");
301 // Assume a panel of some form and adjust the height
302 y += 20;
303 h -= 40;
304 }
305 }
306 }
307#endif
308
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000309 if ((this->w() != w) || (this->h() != h))
310 resizing = true;
311 else
312 resizing = false;
313
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000314 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000315
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000316 if (resizing) {
317 // Try to get the remote size to match our window size, provided
318 // the following conditions are true:
319 //
320 // a) The user has this feature turned on
321 // b) The server supports it
322 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000323 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000324 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000325 if (not firstUpdate and not delayedFullscreen and
326 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000327 // We delay updating the remote desktop as we tend to get a flood
328 // of resize events as the user is dragging the window.
329 Fl::remove_timeout(handleResizeTimeout, this);
330 Fl::add_timeout(0.5, handleResizeTimeout, this);
331 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000332
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000333 // Deal with some scrolling corner cases
334 repositionViewport();
335 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000336}
337
338
Pierre Ossman407a5c32011-05-26 14:48:29 +0000339int DesktopWindow::handle(int event)
340{
341 switch (event) {
342#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000343 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000344 fullScreen.setParam(fullscreen_active());
345
Pierre Ossman1d867b62012-09-03 09:25:07 +0000346 if (fullscreen_active())
347 scroll->type(0);
348 else
349 scroll->type(Fl_Scroll::BOTH);
350
Pierre Ossman407a5c32011-05-26 14:48:29 +0000351 if (!fullscreenSystemKeys)
352 break;
353
354 if (fullscreen_active())
355 grabKeyboard();
356 else
357 ungrabKeyboard();
358
359 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000360
361 case FL_ENTER:
362 case FL_LEAVE:
363 case FL_DRAG:
364 case FL_MOVE:
365 if (fullscreen_active()) {
366 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
367 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
368 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
369 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
370 if (!Fl::has_timeout(handleEdgeScroll, this))
371 Fl::add_timeout(0.1, handleEdgeScroll, this);
372 }
373 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000374 // Continue processing so that the viewport also gets mouse events
375 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000376#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000377
Pierre Ossman407a5c32011-05-26 14:48:29 +0000378 case FL_SHORTCUT:
379 // Sometimes the focus gets out of whack and we fall through to the
380 // shortcut dispatching. Try to make things sane again...
381 if (Fl::focus() == NULL) {
382 take_focus();
383 Fl::handle(FL_KEYDOWN, this);
384 }
385 return 1;
386 }
387
388 return Fl_Window::handle(event);
389}
390
391
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000392int DesktopWindow::fltkHandle(int event, Fl_Window *win)
393{
394 int ret;
395
396 ret = Fl::handle_(event, win);
397
398#ifdef HAVE_FLTK_FULLSCREEN
399 // This is hackish and the result of the dodgy focus handling in FLTK.
400 // The basic problem is that FLTK's view of focus and the system's tend
401 // to differ, and as a result we do not see all the FL_FOCUS events we
402 // need. Fortunately we can grab them here...
403
404 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
405
406 if (dw && fullscreenSystemKeys) {
407 switch (event) {
408 case FL_FOCUS:
409 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
410 // some issues we need to work around:
411 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
412 // b) Gaining focus on the system level causes FLTK to switch
413 // window level on OS X.
414 if (dw->fullscreen_active())
415 dw->grabKeyboard();
416 break;
417
418 case FL_UNFOCUS:
419 // FIXME: We need to relinquish control when the entire window loses
420 // focus as it is very tied to this specific window on some
421 // platforms and we want to be able to open subwindows.
422 dw->ungrabKeyboard();
423 break;
424 }
425 }
426#endif
427
428 return ret;
429}
430
431
Pierre Ossmanaae38912012-07-13 11:22:55 +0000432void DesktopWindow::fullscreen_on()
433{
434#ifdef HAVE_FLTK_FULLSCREEN
435#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
436 if (not fullScreenAllMonitors)
437 fullscreen_screens(-1, -1, -1, -1);
438 else {
439 int top, bottom, left, right;
440 int top_y, bottom_y, left_x, right_x;
441
442 int sx, sy, sw, sh;
443
444 top = bottom = left = right = 0;
445
446 Fl::screen_xywh(sx, sy, sw, sh, 0);
447 top_y = sy;
448 bottom_y = sy + sh;
449 left_x = sx;
450 right_x = sx + sw;
451
452 for (int i = 1;i < Fl::screen_count();i++) {
453 Fl::screen_xywh(sx, sy, sw, sh, i);
454 if (sy < top_y) {
455 top = i;
456 top_y = sy;
457 }
458 if ((sy + sh) > bottom_y) {
459 bottom = i;
460 bottom_y = sy + sh;
461 }
462 if (sx < left_x) {
463 left = i;
464 left_x = sx;
465 }
466 if ((sx + sw) > right_x) {
467 right = i;
468 right_x = sx + sw;
469 }
470 }
471
472 fullscreen_screens(top, bottom, left, right);
473 }
474#endif // HAVE_FLTK_FULLSCREEN_SCREENS
475
476 fullscreen();
477#endif // HAVE_FLTK_FULLSCREEN
478}
479
Pierre Ossman407a5c32011-05-26 14:48:29 +0000480void DesktopWindow::grabKeyboard()
481{
482 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
483 // correct widget regardless of which low level window got the system
484 // event.
485
486 // FIXME: Push this stuff into FLTK.
487
488#if defined(WIN32)
489 int ret;
490
491 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
492 if (ret != 0)
493 vlog.error(_("Failure grabbing keyboard"));
494#elif defined(__APPLE__)
495 int ret;
496
Pierre Ossman3d759112012-08-27 14:40:51 +0000497 ret = cocoa_capture_display(this,
498#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
499 fullScreenAllMonitors
500#else
501 false
502#endif
503 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000504 if (ret != 0)
505 vlog.error(_("Failure grabbing keyboard"));
506#else
507 int ret;
508
509 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000510 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000511 if (ret) {
512 if (ret == AlreadyGrabbed) {
513 // It seems like we can race with the WM in some cases.
514 // Try again in a bit.
515 if (!Fl::has_timeout(handleGrab, this))
516 Fl::add_timeout(0.500, handleGrab, this);
517 } else {
518 vlog.error(_("Failure grabbing keyboard"));
519 }
520 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000521
522 // We also need to grab the pointer as some WMs like to grab buttons
523 // combined with modifies (e.g. Alt+Button0 in metacity).
524 ret = XGrabPointer(fl_display, fl_xid(this), True,
525 ButtonPressMask|ButtonReleaseMask|
526 ButtonMotionMask|PointerMotionMask,
527 GrabModeAsync, GrabModeAsync,
528 None, None, CurrentTime);
529 if (ret)
530 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000531#endif
532}
533
534
535void DesktopWindow::ungrabKeyboard()
536{
537 Fl::remove_timeout(handleGrab, this);
538
539#if defined(WIN32)
540 win32_disable_lowlevel_keyboard(fl_xid(this));
541#elif defined(__APPLE__)
542 cocoa_release_display(this);
543#else
544 // FLTK has a grab so lets not mess with it
545 if (Fl::grab())
546 return;
547
Pierre Ossman53fd5442011-11-17 10:19:19 +0000548 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000549 XUngrabKeyboard(fl_display, fl_event_time);
550#endif
551}
552
553
554void DesktopWindow::handleGrab(void *data)
555{
556 DesktopWindow *self = (DesktopWindow*)data;
557
558 assert(self);
559
560#ifdef HAVE_FLTK_FULLSCREEN
561 if (!fullscreenSystemKeys)
562 return;
563 if (!self->fullscreen_active())
564 return;
565
566 self->grabKeyboard();
567#endif
568}
569
570
Peter Åstrand49b11572012-08-01 08:09:09 +0000571#define _NET_WM_STATE_ADD 1 /* add/set property */
572void DesktopWindow::maximizeWindow()
573{
574#if defined(WIN32)
575 WINDOWPLACEMENT wp;
576 wp.length = sizeof(WINDOWPLACEMENT);
577 GetWindowPlacement(fl_xid(this), &wp);
578 wp.showCmd = SW_MAXIMIZE;
579 SetWindowPlacement(fl_xid(this), &wp);
580#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000581 // OS X is somewhat strange and does not really have a concept of a
582 // maximized window, so we can simply resize the window to the workarea.
583 // Note that we shouldn't do this whilst in full screen as that will
584 // incorrectly adjust things.
585#ifdef HAVE_FLTK_FULLSCREEN
586 if (fullscreen_active())
587 return;
588#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000589 int X, Y, W, H;
590 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
591 size(W, H);
592#else
593 // X11
594 fl_open_display();
595 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
596 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
597 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
598
599 XEvent e;
600 e.xany.type = ClientMessage;
601 e.xany.window = fl_xid(this);
602 e.xclient.message_type = net_wm_state;
603 e.xclient.format = 32;
604 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
605 e.xclient.data.l[1] = net_wm_state_maximized_vert;
606 e.xclient.data.l[2] = net_wm_state_maximized_horz;
607 e.xclient.data.l[3] = 0;
608 e.xclient.data.l[4] = 0;
609 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
610#endif
611}
612
613
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000614void DesktopWindow::handleDesktopSize()
615{
616 int width, height;
617
618 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
619 return;
620
621 remoteResize(width, height);
622}
623
624
Pierre Ossmanff473402012-07-04 11:27:47 +0000625void DesktopWindow::handleResizeTimeout(void *data)
626{
627 DesktopWindow *self = (DesktopWindow *)data;
628
629 assert(self);
630
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000631 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000632}
633
634
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000635void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000636{
Pierre Ossmanff473402012-07-04 11:27:47 +0000637 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000638 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000639
Pierre Ossmanaae38912012-07-13 11:22:55 +0000640#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000641 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000642#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000643 // In windowed mode (or the framebuffer is so large that we need
644 // to scroll) we just report a single virtual screen that covers
645 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000646
Pierre Ossmanaae38912012-07-13 11:22:55 +0000647 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000648
Pierre Ossmanaae38912012-07-13 11:22:55 +0000649 // Not sure why we have no screens, but adding a new one should be
650 // safe as there is nothing to conflict with...
651 if (layout.num_screens() == 0)
652 layout.add_screen(rfb::Screen());
653 else if (layout.num_screens() != 1) {
654 // More than one screen. Remove all but the first (which we
655 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000656
Pierre Ossmanaae38912012-07-13 11:22:55 +0000657 while (true) {
658 iter = layout.begin();
659 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000660
Pierre Ossmanaae38912012-07-13 11:22:55 +0000661 if (iter == layout.end())
662 break;
663
664 layout.remove_screen(iter->id);
665 }
666 }
667
668 // Resize the remaining single screen to the complete framebuffer
669 layout.begin()->dimensions.tl.x = 0;
670 layout.begin()->dimensions.tl.y = 0;
671 layout.begin()->dimensions.br.x = width;
672 layout.begin()->dimensions.br.y = height;
673#ifdef HAVE_FLTK_FULLSCREEN
674 } else {
675 int i;
676 rdr::U32 id;
677 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000678 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000679
680 // In full screen we report all screens that are fully covered.
681
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000682 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
683 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000684
Pierre Ossmanaae38912012-07-13 11:22:55 +0000685 // If we can find a matching screen in the existing set, we use
686 // that, otherwise we create a brand new screen.
687 //
688 // FIXME: We should really track screens better so we can handle
689 // a resized one.
690 //
691 for (i = 0;i < Fl::screen_count();i++) {
692 Fl::screen_xywh(sx, sy, sw, sh, i);
693
Pierre Ossman93d2d922012-07-20 12:32:52 +0000694 // Check that the screen is fully inside the framebuffer
695 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000696 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000697 continue;
698
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000699 // Adjust the coordinates so they are relative to our viewport
700 sx -= viewport_rect.tl.x;
701 sy -= viewport_rect.tl.y;
702
Pierre Ossmanaae38912012-07-13 11:22:55 +0000703 // Look for perfectly matching existing screen...
704 for (iter = cc->cp.screenLayout.begin();
705 iter != cc->cp.screenLayout.end(); ++iter) {
706 if ((iter->dimensions.tl.x == sx) &&
707 (iter->dimensions.tl.y == sy) &&
708 (iter->dimensions.width() == sw) &&
709 (iter->dimensions.height() == sh))
710 break;
711 }
712
713 // Found it?
714 if (iter != cc->cp.screenLayout.end()) {
715 layout.add_screen(*iter);
716 continue;
717 }
718
719 // Need to add a new one, which means we need to find an unused id
720 while (true) {
721 id = rand();
722 for (iter = cc->cp.screenLayout.begin();
723 iter != cc->cp.screenLayout.end(); ++iter) {
724 if (iter->id == id)
725 break;
726 }
727
728 if (iter == cc->cp.screenLayout.end())
729 break;
730 }
731
732 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000733 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000734
735 // If the viewport doesn't match a physical screen, then we might
736 // end up with no screens in the layout. Add a fake one...
737 if (layout.num_screens() == 0)
738 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000739 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000740#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000741
742 // Do we actually change anything?
743 if ((width == cc->cp.width) &&
744 (height == cc->cp.height) &&
745 (layout == cc->cp.screenLayout))
746 return;
747
Pierre Ossmanaae38912012-07-13 11:22:55 +0000748 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
749 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
750
751 if (!layout.validate(width, height)) {
752 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000753 return;
754 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000755
756 cc->writer()->writeSetDesktopSize(width, height, layout);
757}
758
759
Pierre Ossman6455d852011-06-01 09:33:00 +0000760void DesktopWindow::repositionViewport()
761{
762 int new_x, new_y;
763
764 // Deal with some scrolling corner cases:
765 //
766 // a) If the window is larger then the viewport, center the viewport.
767 // b) If the window is smaller than the viewport, make sure there is
768 // no wasted space on the sides.
769 //
770 // FIXME: Doesn't compensate for scroll widget size properly.
771
772 new_x = viewport->x();
773 new_y = viewport->y();
774
775 if (w() > viewport->w())
776 new_x = (w() - viewport->w()) / 2;
777 else {
778 if (viewport->x() > 0)
779 new_x = 0;
780 else if (w() > (viewport->x() + viewport->w()))
781 new_x = w() - viewport->w();
782 }
783
784 // Same thing for y axis
785 if (h() > viewport->h())
786 new_y = (h() - viewport->h()) / 2;
787 else {
788 if (viewport->y() > 0)
789 new_y = 0;
790 else if (h() > (viewport->y() + viewport->h()))
791 new_y = h() - viewport->h();
792 }
793
794 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
795 viewport->position(new_x, new_y);
796
797 // The scroll widget does not notice when you move around child widgets,
798 // so redraw everything to make sure things update.
799 redraw();
800 }
801}
802
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000803void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
804{
805 exit_vncviewer();
806}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000807
808
809void DesktopWindow::handleOptions(void *data)
810{
811 DesktopWindow *self = (DesktopWindow*)data;
812
813#ifdef HAVE_FLTK_FULLSCREEN
814 if (self->fullscreen_active() && fullscreenSystemKeys)
815 self->grabKeyboard();
816 else
817 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000818
Pierre Ossmanff473402012-07-04 11:27:47 +0000819 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000820 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000821 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000822 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000823#endif
824}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000825
826void DesktopWindow::handleFullscreenTimeout(void *data)
827{
828 DesktopWindow *self = (DesktopWindow *)data;
829
830 assert(self);
831
832 self->delayedFullscreen = false;
833
834 if (self->delayedDesktopSize) {
835 self->handleDesktopSize();
836 self->delayedDesktopSize = false;
837 }
838}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000839
840void DesktopWindow::handleEdgeScroll(void *data)
841{
842#ifdef HAVE_FLTK_FULLSCREEN
843 DesktopWindow *self = (DesktopWindow *)data;
844
845 int mx, my;
846 int dx, dy;
847
848 assert(self);
849
850 if (!self->fullscreen_active())
851 return;
852
853 mx = Fl::event_x();
854 my = Fl::event_y();
855
856 dx = dy = 0;
857
858 // Clamp mouse position in case it is outside the window
859 if (mx < 0)
860 mx = 0;
861 if (mx > self->w())
862 mx = self->w();
863 if (my < 0)
864 my = 0;
865 if (my > self->h())
866 my = self->h();
867
868 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000869 dx = EDGE_SCROLL_SPEED -
870 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
871 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
872 (mx > self->w() - EDGE_SCROLL_SIZE))
873 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
874 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000875 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000876 dy = EDGE_SCROLL_SPEED -
877 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
878 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
879 (my > self->h() - EDGE_SCROLL_SIZE))
880 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
881 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000882
883 if ((dx == 0) && (dy == 0))
884 return;
885
886 // Make sure we don't move the viewport too much
887 if (self->viewport->x() + dx > 0)
888 dx = -self->viewport->x();
889 if (self->viewport->x() + dx + self->viewport->w() < self->w())
890 dx = self->w() - (self->viewport->x() + self->viewport->w());
891 if (self->viewport->y() + dy > 0)
892 dy = -self->viewport->y();
893 if (self->viewport->y() + dy + self->viewport->h() < self->h())
894 dy = self->h() - (self->viewport->y() + self->viewport->h());
895
896 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
897
898 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
899#endif
900}