blob: 486daf1deda75981184fb39c202ced0ac547783e [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.
Pierre Ossman0e593462012-09-03 09:43:23 +0000284#ifdef HAVE_FLTK_FULLSCREEN
285 if (!fullscreen_active())
286#endif
287 {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000288 // Get the real window coordinates, so we can determine if
289 // this is a request to resize, or a notification of a resize
290 // from the X server.
291 XWindowAttributes actual;
292 Window cr;
293 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000294
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000295 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
296 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
297 0, 0, &wx, &wy, &cr);
Pierre Ossman0e593462012-09-03 09:43:23 +0000298
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000299 // Actual resize request?
300 if ((wx != x) || (wy != y) ||
301 (actual.width != w) || (actual.height != h)) {
302 for (int i = 0;i < Fl::screen_count();i++) {
303 int sx, sy, sw, sh;
304
305 Fl::screen_xywh(sx, sy, sw, sh, i);
306
307 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
308 vlog.info("Adjusting window size to avoid accidental full screen request");
309 // Assume a panel of some form and adjust the height
310 y += 20;
311 h -= 40;
312 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000313 }
314 }
315 }
316#endif
317
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000318 if ((this->w() != w) || (this->h() != h))
319 resizing = true;
320 else
321 resizing = false;
322
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000323 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000324
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000325 if (resizing) {
326 // Try to get the remote size to match our window size, provided
327 // the following conditions are true:
328 //
329 // a) The user has this feature turned on
330 // b) The server supports it
331 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000332 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000333 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000334 if (not firstUpdate and not delayedFullscreen and
335 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000336 // We delay updating the remote desktop as we tend to get a flood
337 // of resize events as the user is dragging the window.
338 Fl::remove_timeout(handleResizeTimeout, this);
339 Fl::add_timeout(0.5, handleResizeTimeout, this);
340 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000341
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000342 // Deal with some scrolling corner cases
343 repositionViewport();
344 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000345}
346
347
Pierre Ossman407a5c32011-05-26 14:48:29 +0000348int DesktopWindow::handle(int event)
349{
350 switch (event) {
351#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000352 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000353 fullScreen.setParam(fullscreen_active());
354
Pierre Ossman1d867b62012-09-03 09:25:07 +0000355 if (fullscreen_active())
356 scroll->type(0);
357 else
358 scroll->type(Fl_Scroll::BOTH);
359
Pierre Ossman407a5c32011-05-26 14:48:29 +0000360 if (!fullscreenSystemKeys)
361 break;
362
363 if (fullscreen_active())
364 grabKeyboard();
365 else
366 ungrabKeyboard();
367
368 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000369
370 case FL_ENTER:
371 case FL_LEAVE:
372 case FL_DRAG:
373 case FL_MOVE:
374 if (fullscreen_active()) {
375 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
376 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
377 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
378 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
379 if (!Fl::has_timeout(handleEdgeScroll, this))
380 Fl::add_timeout(0.1, handleEdgeScroll, this);
381 }
382 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000383 // Continue processing so that the viewport also gets mouse events
384 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000385#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000386
Pierre Ossman407a5c32011-05-26 14:48:29 +0000387 case FL_SHORTCUT:
388 // Sometimes the focus gets out of whack and we fall through to the
389 // shortcut dispatching. Try to make things sane again...
390 if (Fl::focus() == NULL) {
391 take_focus();
392 Fl::handle(FL_KEYDOWN, this);
393 }
394 return 1;
395 }
396
397 return Fl_Window::handle(event);
398}
399
400
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000401int DesktopWindow::fltkHandle(int event, Fl_Window *win)
402{
403 int ret;
404
405 ret = Fl::handle_(event, win);
406
407#ifdef HAVE_FLTK_FULLSCREEN
408 // This is hackish and the result of the dodgy focus handling in FLTK.
409 // The basic problem is that FLTK's view of focus and the system's tend
410 // to differ, and as a result we do not see all the FL_FOCUS events we
411 // need. Fortunately we can grab them here...
412
413 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
414
415 if (dw && fullscreenSystemKeys) {
416 switch (event) {
417 case FL_FOCUS:
418 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
419 // some issues we need to work around:
420 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
421 // b) Gaining focus on the system level causes FLTK to switch
422 // window level on OS X.
423 if (dw->fullscreen_active())
424 dw->grabKeyboard();
425 break;
426
427 case FL_UNFOCUS:
428 // FIXME: We need to relinquish control when the entire window loses
429 // focus as it is very tied to this specific window on some
430 // platforms and we want to be able to open subwindows.
431 dw->ungrabKeyboard();
432 break;
433 }
434 }
435#endif
436
437 return ret;
438}
439
440
Pierre Ossmanaae38912012-07-13 11:22:55 +0000441void DesktopWindow::fullscreen_on()
442{
443#ifdef HAVE_FLTK_FULLSCREEN
444#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
445 if (not fullScreenAllMonitors)
446 fullscreen_screens(-1, -1, -1, -1);
447 else {
448 int top, bottom, left, right;
449 int top_y, bottom_y, left_x, right_x;
450
451 int sx, sy, sw, sh;
452
453 top = bottom = left = right = 0;
454
455 Fl::screen_xywh(sx, sy, sw, sh, 0);
456 top_y = sy;
457 bottom_y = sy + sh;
458 left_x = sx;
459 right_x = sx + sw;
460
461 for (int i = 1;i < Fl::screen_count();i++) {
462 Fl::screen_xywh(sx, sy, sw, sh, i);
463 if (sy < top_y) {
464 top = i;
465 top_y = sy;
466 }
467 if ((sy + sh) > bottom_y) {
468 bottom = i;
469 bottom_y = sy + sh;
470 }
471 if (sx < left_x) {
472 left = i;
473 left_x = sx;
474 }
475 if ((sx + sw) > right_x) {
476 right = i;
477 right_x = sx + sw;
478 }
479 }
480
481 fullscreen_screens(top, bottom, left, right);
482 }
483#endif // HAVE_FLTK_FULLSCREEN_SCREENS
484
485 fullscreen();
486#endif // HAVE_FLTK_FULLSCREEN
487}
488
Pierre Ossman407a5c32011-05-26 14:48:29 +0000489void DesktopWindow::grabKeyboard()
490{
491 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
492 // correct widget regardless of which low level window got the system
493 // event.
494
495 // FIXME: Push this stuff into FLTK.
496
497#if defined(WIN32)
498 int ret;
499
500 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
501 if (ret != 0)
502 vlog.error(_("Failure grabbing keyboard"));
503#elif defined(__APPLE__)
504 int ret;
505
Pierre Ossman3d759112012-08-27 14:40:51 +0000506 ret = cocoa_capture_display(this,
507#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
508 fullScreenAllMonitors
509#else
510 false
511#endif
512 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000513 if (ret != 0)
514 vlog.error(_("Failure grabbing keyboard"));
515#else
516 int ret;
517
518 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000519 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000520 if (ret) {
521 if (ret == AlreadyGrabbed) {
522 // It seems like we can race with the WM in some cases.
523 // Try again in a bit.
524 if (!Fl::has_timeout(handleGrab, this))
525 Fl::add_timeout(0.500, handleGrab, this);
526 } else {
527 vlog.error(_("Failure grabbing keyboard"));
528 }
529 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000530
531 // We also need to grab the pointer as some WMs like to grab buttons
532 // combined with modifies (e.g. Alt+Button0 in metacity).
533 ret = XGrabPointer(fl_display, fl_xid(this), True,
534 ButtonPressMask|ButtonReleaseMask|
535 ButtonMotionMask|PointerMotionMask,
536 GrabModeAsync, GrabModeAsync,
537 None, None, CurrentTime);
538 if (ret)
539 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000540#endif
541}
542
543
544void DesktopWindow::ungrabKeyboard()
545{
546 Fl::remove_timeout(handleGrab, this);
547
548#if defined(WIN32)
549 win32_disable_lowlevel_keyboard(fl_xid(this));
550#elif defined(__APPLE__)
551 cocoa_release_display(this);
552#else
553 // FLTK has a grab so lets not mess with it
554 if (Fl::grab())
555 return;
556
Pierre Ossman53fd5442011-11-17 10:19:19 +0000557 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000558 XUngrabKeyboard(fl_display, fl_event_time);
559#endif
560}
561
562
563void DesktopWindow::handleGrab(void *data)
564{
565 DesktopWindow *self = (DesktopWindow*)data;
566
567 assert(self);
568
569#ifdef HAVE_FLTK_FULLSCREEN
570 if (!fullscreenSystemKeys)
571 return;
572 if (!self->fullscreen_active())
573 return;
574
575 self->grabKeyboard();
576#endif
577}
578
579
Peter Åstrand49b11572012-08-01 08:09:09 +0000580#define _NET_WM_STATE_ADD 1 /* add/set property */
581void DesktopWindow::maximizeWindow()
582{
583#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000584 // We cannot use ShowWindow() in full screen mode as it will
585 // resize things implicitly. Fortunately modifying the style
586 // directly results in a maximized state once we leave full screen.
587#ifdef HAVE_FLTK_FULLSCREEN
588 if (fullscreen_active()) {
589 WINDOWINFO wi;
590 wi.cbSize = sizeof(WINDOWINFO);
591 GetWindowInfo(fl_xid(this), &wi);
592 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
593 } else
594#endif
595 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000596#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000597 // OS X is somewhat strange and does not really have a concept of a
598 // maximized window, so we can simply resize the window to the workarea.
599 // Note that we shouldn't do this whilst in full screen as that will
600 // incorrectly adjust things.
601#ifdef HAVE_FLTK_FULLSCREEN
602 if (fullscreen_active())
603 return;
604#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000605 int X, Y, W, H;
606 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
607 size(W, H);
608#else
609 // X11
610 fl_open_display();
611 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
612 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
613 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
614
615 XEvent e;
616 e.xany.type = ClientMessage;
617 e.xany.window = fl_xid(this);
618 e.xclient.message_type = net_wm_state;
619 e.xclient.format = 32;
620 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
621 e.xclient.data.l[1] = net_wm_state_maximized_vert;
622 e.xclient.data.l[2] = net_wm_state_maximized_horz;
623 e.xclient.data.l[3] = 0;
624 e.xclient.data.l[4] = 0;
625 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
626#endif
627}
628
629
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000630void DesktopWindow::handleDesktopSize()
631{
632 int width, height;
633
634 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
635 return;
636
637 remoteResize(width, height);
638}
639
640
Pierre Ossmanff473402012-07-04 11:27:47 +0000641void DesktopWindow::handleResizeTimeout(void *data)
642{
643 DesktopWindow *self = (DesktopWindow *)data;
644
645 assert(self);
646
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000647 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000648}
649
650
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000651void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000652{
Pierre Ossmanff473402012-07-04 11:27:47 +0000653 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000654 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000655
Pierre Ossmanaae38912012-07-13 11:22:55 +0000656#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000657 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000658#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000659 // In windowed mode (or the framebuffer is so large that we need
660 // to scroll) we just report a single virtual screen that covers
661 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000662
Pierre Ossmanaae38912012-07-13 11:22:55 +0000663 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000664
Pierre Ossmanaae38912012-07-13 11:22:55 +0000665 // Not sure why we have no screens, but adding a new one should be
666 // safe as there is nothing to conflict with...
667 if (layout.num_screens() == 0)
668 layout.add_screen(rfb::Screen());
669 else if (layout.num_screens() != 1) {
670 // More than one screen. Remove all but the first (which we
671 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000672
Pierre Ossmanaae38912012-07-13 11:22:55 +0000673 while (true) {
674 iter = layout.begin();
675 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000676
Pierre Ossmanaae38912012-07-13 11:22:55 +0000677 if (iter == layout.end())
678 break;
679
680 layout.remove_screen(iter->id);
681 }
682 }
683
684 // Resize the remaining single screen to the complete framebuffer
685 layout.begin()->dimensions.tl.x = 0;
686 layout.begin()->dimensions.tl.y = 0;
687 layout.begin()->dimensions.br.x = width;
688 layout.begin()->dimensions.br.y = height;
689#ifdef HAVE_FLTK_FULLSCREEN
690 } else {
691 int i;
692 rdr::U32 id;
693 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000694 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000695
696 // In full screen we report all screens that are fully covered.
697
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000698 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
699 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000700
Pierre Ossmanaae38912012-07-13 11:22:55 +0000701 // If we can find a matching screen in the existing set, we use
702 // that, otherwise we create a brand new screen.
703 //
704 // FIXME: We should really track screens better so we can handle
705 // a resized one.
706 //
707 for (i = 0;i < Fl::screen_count();i++) {
708 Fl::screen_xywh(sx, sy, sw, sh, i);
709
Pierre Ossman93d2d922012-07-20 12:32:52 +0000710 // Check that the screen is fully inside the framebuffer
711 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000712 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000713 continue;
714
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000715 // Adjust the coordinates so they are relative to our viewport
716 sx -= viewport_rect.tl.x;
717 sy -= viewport_rect.tl.y;
718
Pierre Ossmanaae38912012-07-13 11:22:55 +0000719 // Look for perfectly matching existing screen...
720 for (iter = cc->cp.screenLayout.begin();
721 iter != cc->cp.screenLayout.end(); ++iter) {
722 if ((iter->dimensions.tl.x == sx) &&
723 (iter->dimensions.tl.y == sy) &&
724 (iter->dimensions.width() == sw) &&
725 (iter->dimensions.height() == sh))
726 break;
727 }
728
729 // Found it?
730 if (iter != cc->cp.screenLayout.end()) {
731 layout.add_screen(*iter);
732 continue;
733 }
734
735 // Need to add a new one, which means we need to find an unused id
736 while (true) {
737 id = rand();
738 for (iter = cc->cp.screenLayout.begin();
739 iter != cc->cp.screenLayout.end(); ++iter) {
740 if (iter->id == id)
741 break;
742 }
743
744 if (iter == cc->cp.screenLayout.end())
745 break;
746 }
747
748 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000749 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000750
751 // If the viewport doesn't match a physical screen, then we might
752 // end up with no screens in the layout. Add a fake one...
753 if (layout.num_screens() == 0)
754 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000755 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000756#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000757
758 // Do we actually change anything?
759 if ((width == cc->cp.width) &&
760 (height == cc->cp.height) &&
761 (layout == cc->cp.screenLayout))
762 return;
763
Pierre Ossmanaae38912012-07-13 11:22:55 +0000764 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
765 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
766
767 if (!layout.validate(width, height)) {
768 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000769 return;
770 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000771
772 cc->writer()->writeSetDesktopSize(width, height, layout);
773}
774
775
Pierre Ossman6455d852011-06-01 09:33:00 +0000776void DesktopWindow::repositionViewport()
777{
778 int new_x, new_y;
779
780 // Deal with some scrolling corner cases:
781 //
782 // a) If the window is larger then the viewport, center the viewport.
783 // b) If the window is smaller than the viewport, make sure there is
784 // no wasted space on the sides.
785 //
786 // FIXME: Doesn't compensate for scroll widget size properly.
787
788 new_x = viewport->x();
789 new_y = viewport->y();
790
791 if (w() > viewport->w())
792 new_x = (w() - viewport->w()) / 2;
793 else {
794 if (viewport->x() > 0)
795 new_x = 0;
796 else if (w() > (viewport->x() + viewport->w()))
797 new_x = w() - viewport->w();
798 }
799
800 // Same thing for y axis
801 if (h() > viewport->h())
802 new_y = (h() - viewport->h()) / 2;
803 else {
804 if (viewport->y() > 0)
805 new_y = 0;
806 else if (h() > (viewport->y() + viewport->h()))
807 new_y = h() - viewport->h();
808 }
809
810 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
811 viewport->position(new_x, new_y);
812
813 // The scroll widget does not notice when you move around child widgets,
814 // so redraw everything to make sure things update.
815 redraw();
816 }
817}
818
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000819void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
820{
821 exit_vncviewer();
822}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000823
824
825void DesktopWindow::handleOptions(void *data)
826{
827 DesktopWindow *self = (DesktopWindow*)data;
828
829#ifdef HAVE_FLTK_FULLSCREEN
830 if (self->fullscreen_active() && fullscreenSystemKeys)
831 self->grabKeyboard();
832 else
833 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000834
Pierre Ossmanff473402012-07-04 11:27:47 +0000835 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000836 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000837 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000838 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000839#endif
840}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000841
842void DesktopWindow::handleFullscreenTimeout(void *data)
843{
844 DesktopWindow *self = (DesktopWindow *)data;
845
846 assert(self);
847
848 self->delayedFullscreen = false;
849
850 if (self->delayedDesktopSize) {
851 self->handleDesktopSize();
852 self->delayedDesktopSize = false;
853 }
854}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000855
856void DesktopWindow::handleEdgeScroll(void *data)
857{
858#ifdef HAVE_FLTK_FULLSCREEN
859 DesktopWindow *self = (DesktopWindow *)data;
860
861 int mx, my;
862 int dx, dy;
863
864 assert(self);
865
866 if (!self->fullscreen_active())
867 return;
868
869 mx = Fl::event_x();
870 my = Fl::event_y();
871
872 dx = dy = 0;
873
874 // Clamp mouse position in case it is outside the window
875 if (mx < 0)
876 mx = 0;
877 if (mx > self->w())
878 mx = self->w();
879 if (my < 0)
880 my = 0;
881 if (my > self->h())
882 my = self->h();
883
884 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000885 dx = EDGE_SCROLL_SPEED -
886 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
887 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
888 (mx > self->w() - EDGE_SCROLL_SIZE))
889 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
890 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000891 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000892 dy = EDGE_SCROLL_SPEED -
893 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
894 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
895 (my > self->h() - EDGE_SCROLL_SIZE))
896 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
897 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000898
899 if ((dx == 0) && (dy == 0))
900 return;
901
902 // Make sure we don't move the viewport too much
903 if (self->viewport->x() + dx > 0)
904 dx = -self->viewport->x();
905 if (self->viewport->x() + dx + self->viewport->w() < self->w())
906 dx = self->w() - (self->viewport->x() + self->viewport->w());
907 if (self->viewport->y() + dy > 0)
908 dy = -self->viewport->y();
909 if (self->viewport->y() + dy + self->viewport->h() < self->h())
910 dy = self->h() - (self->viewport->y() + self->viewport->h());
911
912 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
913
914 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
915#endif
916}