blob: 3e9b57e6a41c2b3689090cd304f21bf4147ae216 [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 Ossman947b48d2014-01-27 16:52:35 +010037#include "Viewport.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000038
Pierre Ossman947b48d2014-01-27 16:52:35 +010039#include <FL/Fl.H>
DRCb65bb932011-06-24 03:17:00 +000040#include <FL/Fl_Scroll.H>
41#include <FL/x.H>
42
Pierre Ossman407a5c32011-05-26 14:48:29 +000043#ifdef WIN32
44#include "win32.h"
45#endif
46
47#ifdef __APPLE__
48#include "cocoa.h"
49#endif
Pierre Ossman89f868a2011-04-11 11:59:31 +000050
Pierre Ossman1d867b62012-09-03 09:25:07 +000051#define EDGE_SCROLL_SIZE 32
52#define EDGE_SCROLL_SPEED 20
53
Pierre Ossman5156d5e2011-03-09 09:42:34 +000054using namespace rfb;
55
Pierre Ossman5156d5e2011-03-09 09:42:34 +000056static rfb::LogWriter vlog("DesktopWindow");
57
58DesktopWindow::DesktopWindow(int w, int h, const char *name,
59 const rfb::PixelFormat& serverPF,
60 CConn* cc_)
Pierre Ossman4c4b66f2012-08-23 14:53:36 +000061 : Fl_Window(w, h), cc(cc_), firstUpdate(true),
62 delayedFullscreen(false), delayedDesktopSize(false)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000063{
Pierre Ossman1d867b62012-09-03 09:25:07 +000064 scroll = new Fl_Scroll(0, 0, w, h);
Pierre Ossman4ae229f2011-04-15 12:58:31 +000065 scroll->color(FL_BLACK);
66
67 // Automatically adjust the scroll box to the window
68 resizable(scroll);
69
Pierre Ossmanff473402012-07-04 11:27:47 +000070 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000071
Pierre Ossman4ae229f2011-04-15 12:58:31 +000072 scroll->end();
73
Pierre Ossman5156d5e2011-03-09 09:42:34 +000074 callback(handleClose, this);
75
76 setName(name);
77
Pierre Ossman407a5c32011-05-26 14:48:29 +000078 OptionsDialog::addCallback(handleOptions, this);
79
Pierre Ossmana4f0f182011-06-14 13:36:57 +000080 // Hack. See below...
81 Fl::event_dispatch(&fltkHandle);
82
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +000083 // Support for -geometry option. Note that although we do support
84 // negative coordinates, we do not support -XOFF-YOFF (ie
85 // coordinates relative to the right edge / bottom edge) at this
86 // time.
87 int geom_x = 0, geom_y = 0;
88 if (geometry.hasBeenSet()) {
89 int matched;
90 matched = sscanf(geometry.getValueStr(), "+%d+%d", &geom_x, &geom_y);
91 if (matched == 2) {
92 force_position(1);
93 } else {
94 int geom_w, geom_h;
95 matched = sscanf(geometry.getValueStr(), "%dx%d+%d+%d", &geom_w, &geom_h, &geom_x, &geom_y);
96 switch (matched) {
97 case 4:
Pierre Ossman9d267c52012-10-02 14:30:22 +000098 force_position(1);
99 /* fall through */
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000100 case 2:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000101 w = geom_w;
102 h = geom_h;
103 break;
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000104 default:
Pierre Ossman9d267c52012-10-02 14:30:22 +0000105 geom_x = geom_y = 0;
106 vlog.error("Invalid geometry specified!");
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000107 }
108 }
109 }
110
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000111#ifdef __APPLE__
112 // On OS X we can do the maximize thing properly before the
113 // window is showned. Other platforms handled further down...
114 if (maximize) {
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000115#ifdef HAVE_FLTK_WORK_AREA
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000116 int dummy;
117 Fl::screen_work_area(dummy, dummy, w, h, geom_x, geom_y);
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000118#else
119 w = Fl::w();
120 h = Fl::h();
121#endif
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000122 }
123#endif
124
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000125 if (force_position()) {
126 resize(geom_x, geom_y, w, h);
127 } else {
128 size(w, h);
129 }
130
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000131#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000132 if (fullScreen) {
133 // Hack: Window managers seem to be rather crappy at respecting
134 // fullscreen hints on initial windows. So on X11 we'll have to
135 // wait until after we've been mapped.
136#if defined(WIN32) || defined(__APPLE__)
Pierre Ossmanaae38912012-07-13 11:22:55 +0000137 fullscreen_on();
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000138#else
139 delayedFullscreen = true;
140#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +0000141 }
Peter Åstrandc6cdc1f2012-08-29 07:14:31 +0000142#endif
Pierre Ossman63ca58e2011-05-26 14:59:32 +0000143
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000144 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000145
Pierre Ossmancb68c192012-10-17 07:59:20 +0000146 // Full screen events are not sent out for a hidden window,
147 // so send a fake one here to set up things properly.
148#ifdef HAVE_FLTK_FULLSCREEN
149 if (fullscreen_active())
150 handle(FL_FULLSCREEN);
151#endif
152
Peter Åstrand49b11572012-08-01 08:09:09 +0000153 // Unfortunately, current FLTK does not allow us to set the
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000154 // maximized property on Windows and X11 before showing the window.
155 // See STR #2083 and STR #2178
156#ifndef __APPLE__
Peter Åstrand49b11572012-08-01 08:09:09 +0000157 if (maximize) {
158 maximizeWindow();
159 }
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000160#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000161
Pierre Ossman3f6c4d02011-04-15 14:09:09 +0000162 // The window manager might give us an initial window size that is different
163 // than the one we requested, and in those cases we need to manually adjust
164 // the scroll widget for things to behave sanely.
165 if ((w != this->w()) || (h != this->h()))
166 scroll->size(this->w(), this->h());
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000167
168#ifdef HAVE_FLTK_FULLSCREEN
169 if (delayedFullscreen) {
170 // Hack: Fullscreen requests may be ignored, so we need a timeout for
171 // when we should stop waiting. We also really need to wait for the
172 // resize, which can come after the fullscreen event.
173 Fl::add_timeout(0.5, handleFullscreenTimeout, this);
174 fullscreen_on();
175 }
176#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000177}
178
179
180DesktopWindow::~DesktopWindow()
181{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000182 // Unregister all timeouts in case they get a change tro trigger
183 // again later when this object is already gone.
184 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000185 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000186 Fl::remove_timeout(handleFullscreenTimeout, this);
Pierre Ossman1d867b62012-09-03 09:25:07 +0000187 Fl::remove_timeout(handleEdgeScroll, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000188
189 OptionsDialog::removeCallback(handleOptions);
190
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000191 // FLTK automatically deletes all child widgets, so we shouldn't touch
192 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000193}
194
195
196void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
197{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000198 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000199}
200
201
202const rfb::PixelFormat &DesktopWindow::getPreferredPF()
203{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000204 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000205}
206
207
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000208void DesktopWindow::setName(const char *name)
209{
210 CharArray windowNameStr;
211 windowNameStr.replaceBuf(new char[256]);
212
Pierre Ossman27820ba2011-09-30 12:54:24 +0000213 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000214
215 copy_label(windowNameStr.buf);
216}
217
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000218
Pierre Ossman947b48d2014-01-27 16:52:35 +0100219void DesktopWindow::fillRect(const rfb::Rect& r, rfb::Pixel pix) {
220 viewport->fillRect(r, pix);
221}
222
223void DesktopWindow::imageRect(const rfb::Rect& r, void* pixels) {
224 viewport->imageRect(r, pixels);
225}
226
227void DesktopWindow::copyRect(const rfb::Rect& r, int srcX, int srcY) {
228 viewport->copyRect(r, srcX, srcY);
229}
230
231rdr::U8* DesktopWindow::getBufferRW(const rfb::Rect& r, int* stride) {
232 return viewport->getBufferRW(r, stride);
233}
234
235void DesktopWindow::damageRect(const rfb::Rect& r) {
236 viewport->damageRect(r);
237}
238
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000239
240// Copy the areas of the framebuffer that have been changed (damaged)
241// to the displayed window.
242
243void DesktopWindow::updateWindow()
244{
Pierre Ossmanff473402012-07-04 11:27:47 +0000245 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000246 if (cc->cp.supportsSetDesktopSize) {
247 // Hack: Wait until we're in the proper mode and position until
248 // resizing things, otherwise we might send the wrong thing.
249 if (delayedFullscreen)
250 delayedDesktopSize = true;
251 else
252 handleDesktopSize();
253 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000254 firstUpdate = false;
255 }
256
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000257 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000258}
259
260
Pierre Ossman6455d852011-06-01 09:33:00 +0000261void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
262{
263 if ((new_w == viewport->w()) && (new_h == viewport->h()))
264 return;
265
Pierre Ossman6455d852011-06-01 09:33:00 +0000266 // If we're letting the viewport match the window perfectly, then
267 // keep things that way for the new size, otherwise just keep things
268 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000269#ifdef HAVE_FLTK_FULLSCREEN
270 if (!fullscreen_active()) {
271#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000272 if ((w() == viewport->w()) && (h() == viewport->h()))
273 size(new_w, new_h);
274 else {
275 // Make sure the window isn't too big. We do this manually because
276 // we have to disable the window size restriction (and it isn't
277 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000278 if ((w() > new_w) || (h() > new_h))
279 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000280 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000281#ifdef HAVE_FLTK_FULLSCREEN
282 }
283#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000284
285 viewport->size(new_w, new_h);
286
287 // We might not resize the main window, so we need to manually call this
288 // to make sure the viewport is centered.
289 repositionViewport();
290
Pierre Ossman6455d852011-06-01 09:33:00 +0000291 // repositionViewport() makes sure the scroll widget notices any changes
292 // in position, but it might be just the size that changes so we also
293 // need a poke here as well.
294 redraw();
295}
296
297
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000298void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
299 void* data, void* mask)
300{
301 viewport->setCursor(width, height, hotspot, data, mask);
302}
303
304
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000305void DesktopWindow::resize(int x, int y, int w, int h)
306{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000307 bool resizing;
308
Pierre Ossman0e593462012-09-03 09:43:23 +0000309#if ! (defined(WIN32) || defined(__APPLE__))
310 // X11 window managers will treat a resize to cover the entire
311 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman0e593462012-09-03 09:43:23 +0000312#ifdef HAVE_FLTK_FULLSCREEN
313 if (!fullscreen_active())
314#endif
315 {
Pierre Ossman1f884e02012-10-30 10:26:23 +0000316 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000317
Pierre Ossman1f884e02012-10-30 10:26:23 +0000318 // If there is no X11 window, then this must be a resize request,
319 // not a notification from the X server.
320 if (!shown())
321 resize_req = true;
322 else {
323 // Otherwise we need to get the real window coordinates to tell
324 // the difference
325 XWindowAttributes actual;
326 Window cr;
327 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000328
Pierre Ossman1f884e02012-10-30 10:26:23 +0000329 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
330 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
331 0, 0, &wx, &wy, &cr);
332
333 // Actual resize request?
334 if ((wx != x) || (wy != y) ||
335 (actual.width != w) || (actual.height != h))
336 resize_req = true;
337 else
338 resize_req = false;
339 }
340
341 if (resize_req) {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000342 for (int i = 0;i < Fl::screen_count();i++) {
343 int sx, sy, sw, sh;
344
345 Fl::screen_xywh(sx, sy, sw, sh, i);
346
347 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
348 vlog.info("Adjusting window size to avoid accidental full screen request");
349 // Assume a panel of some form and adjust the height
350 y += 20;
351 h -= 40;
352 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000353 }
354 }
355 }
356#endif
357
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000358 if ((this->w() != w) || (this->h() != h))
359 resizing = true;
360 else
361 resizing = false;
362
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000363 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000364
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000365 if (resizing) {
366 // Try to get the remote size to match our window size, provided
367 // the following conditions are true:
368 //
369 // a) The user has this feature turned on
370 // b) The server supports it
371 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000372 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000373 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000374 if (not firstUpdate and not delayedFullscreen and
375 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000376 // We delay updating the remote desktop as we tend to get a flood
377 // of resize events as the user is dragging the window.
378 Fl::remove_timeout(handleResizeTimeout, this);
379 Fl::add_timeout(0.5, handleResizeTimeout, this);
380 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000381
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000382 // Deal with some scrolling corner cases
383 repositionViewport();
384 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000385}
386
387
Pierre Ossman407a5c32011-05-26 14:48:29 +0000388int DesktopWindow::handle(int event)
389{
390 switch (event) {
391#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000392 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000393 fullScreen.setParam(fullscreen_active());
394
Pierre Ossman1d867b62012-09-03 09:25:07 +0000395 if (fullscreen_active())
396 scroll->type(0);
397 else
398 scroll->type(Fl_Scroll::BOTH);
399
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000400 // The scroll widget isn't clever enough to actually redraw the
401 // scroll bars when they are added/removed, so we need to give
402 // it a push.
403 scroll->redraw();
404
Pierre Ossman407a5c32011-05-26 14:48:29 +0000405 if (!fullscreenSystemKeys)
406 break;
407
408 if (fullscreen_active())
409 grabKeyboard();
410 else
411 ungrabKeyboard();
412
413 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000414
415 case FL_ENTER:
416 case FL_LEAVE:
417 case FL_DRAG:
418 case FL_MOVE:
419 if (fullscreen_active()) {
420 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
421 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
422 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
423 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
424 if (!Fl::has_timeout(handleEdgeScroll, this))
425 Fl::add_timeout(0.1, handleEdgeScroll, this);
426 }
427 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000428 // Continue processing so that the viewport also gets mouse events
429 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000430#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000431
Pierre Ossman407a5c32011-05-26 14:48:29 +0000432 case FL_SHORTCUT:
433 // Sometimes the focus gets out of whack and we fall through to the
434 // shortcut dispatching. Try to make things sane again...
435 if (Fl::focus() == NULL) {
436 take_focus();
437 Fl::handle(FL_KEYDOWN, this);
438 }
439 return 1;
440 }
441
442 return Fl_Window::handle(event);
443}
444
445
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000446int DesktopWindow::fltkHandle(int event, Fl_Window *win)
447{
448 int ret;
449
450 ret = Fl::handle_(event, win);
451
452#ifdef HAVE_FLTK_FULLSCREEN
453 // This is hackish and the result of the dodgy focus handling in FLTK.
454 // The basic problem is that FLTK's view of focus and the system's tend
455 // to differ, and as a result we do not see all the FL_FOCUS events we
456 // need. Fortunately we can grab them here...
457
458 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
459
460 if (dw && fullscreenSystemKeys) {
461 switch (event) {
462 case FL_FOCUS:
463 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
464 // some issues we need to work around:
465 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
466 // b) Gaining focus on the system level causes FLTK to switch
467 // window level on OS X.
468 if (dw->fullscreen_active())
469 dw->grabKeyboard();
470 break;
471
Pierre Ossmanb1369802012-10-17 07:59:36 +0000472 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000473 // FIXME: We need to relinquish control when the entire window loses
474 // focus as it is very tied to this specific window on some
475 // platforms and we want to be able to open subwindows.
476 dw->ungrabKeyboard();
477 break;
478 }
479 }
480#endif
481
482 return ret;
483}
484
485
Pierre Ossmanaae38912012-07-13 11:22:55 +0000486void DesktopWindow::fullscreen_on()
487{
488#ifdef HAVE_FLTK_FULLSCREEN
489#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
490 if (not fullScreenAllMonitors)
491 fullscreen_screens(-1, -1, -1, -1);
492 else {
493 int top, bottom, left, right;
494 int top_y, bottom_y, left_x, right_x;
495
496 int sx, sy, sw, sh;
497
498 top = bottom = left = right = 0;
499
500 Fl::screen_xywh(sx, sy, sw, sh, 0);
501 top_y = sy;
502 bottom_y = sy + sh;
503 left_x = sx;
504 right_x = sx + sw;
505
506 for (int i = 1;i < Fl::screen_count();i++) {
507 Fl::screen_xywh(sx, sy, sw, sh, i);
508 if (sy < top_y) {
509 top = i;
510 top_y = sy;
511 }
512 if ((sy + sh) > bottom_y) {
513 bottom = i;
514 bottom_y = sy + sh;
515 }
516 if (sx < left_x) {
517 left = i;
518 left_x = sx;
519 }
520 if ((sx + sw) > right_x) {
521 right = i;
522 right_x = sx + sw;
523 }
524 }
525
526 fullscreen_screens(top, bottom, left, right);
527 }
528#endif // HAVE_FLTK_FULLSCREEN_SCREENS
529
530 fullscreen();
531#endif // HAVE_FLTK_FULLSCREEN
532}
533
Pierre Ossman407a5c32011-05-26 14:48:29 +0000534void DesktopWindow::grabKeyboard()
535{
536 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
537 // correct widget regardless of which low level window got the system
538 // event.
539
540 // FIXME: Push this stuff into FLTK.
541
542#if defined(WIN32)
543 int ret;
544
545 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
546 if (ret != 0)
547 vlog.error(_("Failure grabbing keyboard"));
548#elif defined(__APPLE__)
549 int ret;
550
Pierre Ossman3d759112012-08-27 14:40:51 +0000551 ret = cocoa_capture_display(this,
552#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
553 fullScreenAllMonitors
554#else
555 false
556#endif
557 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000558 if (ret != 0)
559 vlog.error(_("Failure grabbing keyboard"));
560#else
561 int ret;
562
563 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000564 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000565 if (ret) {
566 if (ret == AlreadyGrabbed) {
567 // It seems like we can race with the WM in some cases.
568 // Try again in a bit.
569 if (!Fl::has_timeout(handleGrab, this))
570 Fl::add_timeout(0.500, handleGrab, this);
571 } else {
572 vlog.error(_("Failure grabbing keyboard"));
573 }
574 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000575
576 // We also need to grab the pointer as some WMs like to grab buttons
577 // combined with modifies (e.g. Alt+Button0 in metacity).
578 ret = XGrabPointer(fl_display, fl_xid(this), True,
579 ButtonPressMask|ButtonReleaseMask|
580 ButtonMotionMask|PointerMotionMask,
581 GrabModeAsync, GrabModeAsync,
582 None, None, CurrentTime);
583 if (ret)
584 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000585#endif
586}
587
588
589void DesktopWindow::ungrabKeyboard()
590{
591 Fl::remove_timeout(handleGrab, this);
592
593#if defined(WIN32)
594 win32_disable_lowlevel_keyboard(fl_xid(this));
595#elif defined(__APPLE__)
596 cocoa_release_display(this);
597#else
598 // FLTK has a grab so lets not mess with it
599 if (Fl::grab())
600 return;
601
Pierre Ossman53fd5442011-11-17 10:19:19 +0000602 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000603 XUngrabKeyboard(fl_display, fl_event_time);
604#endif
605}
606
607
608void DesktopWindow::handleGrab(void *data)
609{
610 DesktopWindow *self = (DesktopWindow*)data;
611
612 assert(self);
613
614#ifdef HAVE_FLTK_FULLSCREEN
615 if (!fullscreenSystemKeys)
616 return;
617 if (!self->fullscreen_active())
618 return;
619
620 self->grabKeyboard();
621#endif
622}
623
624
Peter Åstrand49b11572012-08-01 08:09:09 +0000625#define _NET_WM_STATE_ADD 1 /* add/set property */
626void DesktopWindow::maximizeWindow()
627{
628#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000629 // We cannot use ShowWindow() in full screen mode as it will
630 // resize things implicitly. Fortunately modifying the style
631 // directly results in a maximized state once we leave full screen.
632#ifdef HAVE_FLTK_FULLSCREEN
633 if (fullscreen_active()) {
634 WINDOWINFO wi;
635 wi.cbSize = sizeof(WINDOWINFO);
636 GetWindowInfo(fl_xid(this), &wi);
637 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
638 } else
639#endif
640 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000641#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000642 // OS X is somewhat strange and does not really have a concept of a
643 // maximized window, so we can simply resize the window to the workarea.
644 // Note that we shouldn't do this whilst in full screen as that will
645 // incorrectly adjust things.
646#ifdef HAVE_FLTK_FULLSCREEN
647 if (fullscreen_active())
648 return;
649#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000650 int X, Y, W, H;
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000651#ifdef HAVE_FLTK_WORK_AREA
Peter Åstrand49b11572012-08-01 08:09:09 +0000652 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000653#else
654 W = Fl::w();
655 H = Fl::h();
656#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000657 size(W, H);
658#else
659 // X11
660 fl_open_display();
661 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
662 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
663 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
664
665 XEvent e;
666 e.xany.type = ClientMessage;
667 e.xany.window = fl_xid(this);
668 e.xclient.message_type = net_wm_state;
669 e.xclient.format = 32;
670 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
671 e.xclient.data.l[1] = net_wm_state_maximized_vert;
672 e.xclient.data.l[2] = net_wm_state_maximized_horz;
673 e.xclient.data.l[3] = 0;
674 e.xclient.data.l[4] = 0;
675 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
676#endif
677}
678
679
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000680void DesktopWindow::handleDesktopSize()
681{
682 int width, height;
683
684 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
685 return;
686
687 remoteResize(width, height);
688}
689
690
Pierre Ossmanff473402012-07-04 11:27:47 +0000691void DesktopWindow::handleResizeTimeout(void *data)
692{
693 DesktopWindow *self = (DesktopWindow *)data;
694
695 assert(self);
696
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000697 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000698}
699
700
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000701void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000702{
Pierre Ossmanff473402012-07-04 11:27:47 +0000703 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000704 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000705
Pierre Ossmanaae38912012-07-13 11:22:55 +0000706#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000707 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000708#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000709 // In windowed mode (or the framebuffer is so large that we need
710 // to scroll) we just report a single virtual screen that covers
711 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000712
Pierre Ossmanaae38912012-07-13 11:22:55 +0000713 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000714
Pierre Ossmanaae38912012-07-13 11:22:55 +0000715 // Not sure why we have no screens, but adding a new one should be
716 // safe as there is nothing to conflict with...
717 if (layout.num_screens() == 0)
718 layout.add_screen(rfb::Screen());
719 else if (layout.num_screens() != 1) {
720 // More than one screen. Remove all but the first (which we
721 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000722
Pierre Ossmanaae38912012-07-13 11:22:55 +0000723 while (true) {
724 iter = layout.begin();
725 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000726
Pierre Ossmanaae38912012-07-13 11:22:55 +0000727 if (iter == layout.end())
728 break;
729
730 layout.remove_screen(iter->id);
731 }
732 }
733
734 // Resize the remaining single screen to the complete framebuffer
735 layout.begin()->dimensions.tl.x = 0;
736 layout.begin()->dimensions.tl.y = 0;
737 layout.begin()->dimensions.br.x = width;
738 layout.begin()->dimensions.br.y = height;
739#ifdef HAVE_FLTK_FULLSCREEN
740 } else {
741 int i;
742 rdr::U32 id;
743 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000744 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000745
746 // In full screen we report all screens that are fully covered.
747
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000748 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
749 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000750
Pierre Ossmanaae38912012-07-13 11:22:55 +0000751 // If we can find a matching screen in the existing set, we use
752 // that, otherwise we create a brand new screen.
753 //
754 // FIXME: We should really track screens better so we can handle
755 // a resized one.
756 //
757 for (i = 0;i < Fl::screen_count();i++) {
758 Fl::screen_xywh(sx, sy, sw, sh, i);
759
Pierre Ossman93d2d922012-07-20 12:32:52 +0000760 // Check that the screen is fully inside the framebuffer
761 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000762 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000763 continue;
764
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000765 // Adjust the coordinates so they are relative to our viewport
766 sx -= viewport_rect.tl.x;
767 sy -= viewport_rect.tl.y;
768
Pierre Ossmanaae38912012-07-13 11:22:55 +0000769 // Look for perfectly matching existing screen...
770 for (iter = cc->cp.screenLayout.begin();
771 iter != cc->cp.screenLayout.end(); ++iter) {
772 if ((iter->dimensions.tl.x == sx) &&
773 (iter->dimensions.tl.y == sy) &&
774 (iter->dimensions.width() == sw) &&
775 (iter->dimensions.height() == sh))
776 break;
777 }
778
779 // Found it?
780 if (iter != cc->cp.screenLayout.end()) {
781 layout.add_screen(*iter);
782 continue;
783 }
784
785 // Need to add a new one, which means we need to find an unused id
786 while (true) {
787 id = rand();
788 for (iter = cc->cp.screenLayout.begin();
789 iter != cc->cp.screenLayout.end(); ++iter) {
790 if (iter->id == id)
791 break;
792 }
793
794 if (iter == cc->cp.screenLayout.end())
795 break;
796 }
797
798 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000799 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000800
801 // If the viewport doesn't match a physical screen, then we might
802 // end up with no screens in the layout. Add a fake one...
803 if (layout.num_screens() == 0)
804 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000805 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000806#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000807
808 // Do we actually change anything?
809 if ((width == cc->cp.width) &&
810 (height == cc->cp.height) &&
811 (layout == cc->cp.screenLayout))
812 return;
813
Pierre Ossmanaae38912012-07-13 11:22:55 +0000814 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
815 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
816
817 if (!layout.validate(width, height)) {
818 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000819 return;
820 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000821
822 cc->writer()->writeSetDesktopSize(width, height, layout);
823}
824
825
Pierre Ossman6455d852011-06-01 09:33:00 +0000826void DesktopWindow::repositionViewport()
827{
828 int new_x, new_y;
829
830 // Deal with some scrolling corner cases:
831 //
832 // a) If the window is larger then the viewport, center the viewport.
833 // b) If the window is smaller than the viewport, make sure there is
834 // no wasted space on the sides.
835 //
836 // FIXME: Doesn't compensate for scroll widget size properly.
837
838 new_x = viewport->x();
839 new_y = viewport->y();
840
841 if (w() > viewport->w())
842 new_x = (w() - viewport->w()) / 2;
843 else {
844 if (viewport->x() > 0)
845 new_x = 0;
846 else if (w() > (viewport->x() + viewport->w()))
847 new_x = w() - viewport->w();
848 }
849
850 // Same thing for y axis
851 if (h() > viewport->h())
852 new_y = (h() - viewport->h()) / 2;
853 else {
854 if (viewport->y() > 0)
855 new_y = 0;
856 else if (h() > (viewport->y() + viewport->h()))
857 new_y = h() - viewport->h();
858 }
859
860 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
861 viewport->position(new_x, new_y);
862
863 // The scroll widget does not notice when you move around child widgets,
864 // so redraw everything to make sure things update.
865 redraw();
866 }
867}
868
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000869void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
870{
871 exit_vncviewer();
872}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000873
874
875void DesktopWindow::handleOptions(void *data)
876{
877 DesktopWindow *self = (DesktopWindow*)data;
878
879#ifdef HAVE_FLTK_FULLSCREEN
880 if (self->fullscreen_active() && fullscreenSystemKeys)
881 self->grabKeyboard();
882 else
883 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000884
Pierre Ossmanff473402012-07-04 11:27:47 +0000885 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000886 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000887 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000888 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000889#endif
890}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000891
892void DesktopWindow::handleFullscreenTimeout(void *data)
893{
894 DesktopWindow *self = (DesktopWindow *)data;
895
896 assert(self);
897
898 self->delayedFullscreen = false;
899
900 if (self->delayedDesktopSize) {
901 self->handleDesktopSize();
902 self->delayedDesktopSize = false;
903 }
904}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000905
906void DesktopWindow::handleEdgeScroll(void *data)
907{
908#ifdef HAVE_FLTK_FULLSCREEN
909 DesktopWindow *self = (DesktopWindow *)data;
910
911 int mx, my;
912 int dx, dy;
913
914 assert(self);
915
916 if (!self->fullscreen_active())
917 return;
918
919 mx = Fl::event_x();
920 my = Fl::event_y();
921
922 dx = dy = 0;
923
924 // Clamp mouse position in case it is outside the window
925 if (mx < 0)
926 mx = 0;
927 if (mx > self->w())
928 mx = self->w();
929 if (my < 0)
930 my = 0;
931 if (my > self->h())
932 my = self->h();
933
934 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000935 dx = EDGE_SCROLL_SPEED -
936 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
937 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
938 (mx > self->w() - EDGE_SCROLL_SIZE))
939 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
940 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000941 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000942 dy = EDGE_SCROLL_SPEED -
943 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
944 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
945 (my > self->h() - EDGE_SCROLL_SIZE))
946 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
947 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000948
949 if ((dx == 0) && (dy == 0))
950 return;
951
952 // Make sure we don't move the viewport too much
953 if (self->viewport->x() + dx > 0)
954 dx = -self->viewport->x();
955 if (self->viewport->x() + dx + self->viewport->w() < self->w())
956 dx = self->w() - (self->viewport->x() + self->viewport->w());
957 if (self->viewport->y() + dy > 0)
958 dy = -self->viewport->y();
959 if (self->viewport->y() + dy + self->viewport->h() < self->h())
960 dy = self->h() - (self->viewport->y() + self->viewport->h());
961
962 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
963
964 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
965#endif
966}