blob: 2a2f8734db56970e22419225140cd8f4635f5ab8 [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 Ossman5156d5e2011-03-09 09:42:34 +0000219void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
220 rdr::U16* rgbs)
221{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000222 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000223}
224
Pierre Ossman947b48d2014-01-27 16:52:35 +0100225void DesktopWindow::fillRect(const rfb::Rect& r, rfb::Pixel pix) {
226 viewport->fillRect(r, pix);
227}
228
229void DesktopWindow::imageRect(const rfb::Rect& r, void* pixels) {
230 viewport->imageRect(r, pixels);
231}
232
233void DesktopWindow::copyRect(const rfb::Rect& r, int srcX, int srcY) {
234 viewport->copyRect(r, srcX, srcY);
235}
236
237rdr::U8* DesktopWindow::getBufferRW(const rfb::Rect& r, int* stride) {
238 return viewport->getBufferRW(r, stride);
239}
240
241void DesktopWindow::damageRect(const rfb::Rect& r) {
242 viewport->damageRect(r);
243}
244
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000245
246// Copy the areas of the framebuffer that have been changed (damaged)
247// to the displayed window.
248
249void DesktopWindow::updateWindow()
250{
Pierre Ossmanff473402012-07-04 11:27:47 +0000251 if (firstUpdate) {
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000252 if (cc->cp.supportsSetDesktopSize) {
253 // Hack: Wait until we're in the proper mode and position until
254 // resizing things, otherwise we might send the wrong thing.
255 if (delayedFullscreen)
256 delayedDesktopSize = true;
257 else
258 handleDesktopSize();
259 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000260 firstUpdate = false;
261 }
262
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000263 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000264}
265
266
Pierre Ossman6455d852011-06-01 09:33:00 +0000267void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
268{
269 if ((new_w == viewport->w()) && (new_h == viewport->h()))
270 return;
271
Pierre Ossman6455d852011-06-01 09:33:00 +0000272 // If we're letting the viewport match the window perfectly, then
273 // keep things that way for the new size, otherwise just keep things
274 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000275#ifdef HAVE_FLTK_FULLSCREEN
276 if (!fullscreen_active()) {
277#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000278 if ((w() == viewport->w()) && (h() == viewport->h()))
279 size(new_w, new_h);
280 else {
281 // Make sure the window isn't too big. We do this manually because
282 // we have to disable the window size restriction (and it isn't
283 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000284 if ((w() > new_w) || (h() > new_h))
285 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000286 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000287#ifdef HAVE_FLTK_FULLSCREEN
288 }
289#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000290
291 viewport->size(new_w, new_h);
292
293 // We might not resize the main window, so we need to manually call this
294 // to make sure the viewport is centered.
295 repositionViewport();
296
Pierre Ossman6455d852011-06-01 09:33:00 +0000297 // repositionViewport() makes sure the scroll widget notices any changes
298 // in position, but it might be just the size that changes so we also
299 // need a poke here as well.
300 redraw();
301}
302
303
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000304void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
305 void* data, void* mask)
306{
307 viewport->setCursor(width, height, hotspot, data, mask);
308}
309
310
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000311void DesktopWindow::resize(int x, int y, int w, int h)
312{
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000313 bool resizing;
314
Pierre Ossman0e593462012-09-03 09:43:23 +0000315#if ! (defined(WIN32) || defined(__APPLE__))
316 // X11 window managers will treat a resize to cover the entire
317 // monitor as a request to go full screen. Make sure we avoid this.
Pierre Ossman0e593462012-09-03 09:43:23 +0000318#ifdef HAVE_FLTK_FULLSCREEN
319 if (!fullscreen_active())
320#endif
321 {
Pierre Ossman1f884e02012-10-30 10:26:23 +0000322 bool resize_req;
Pierre Ossman0e593462012-09-03 09:43:23 +0000323
Pierre Ossman1f884e02012-10-30 10:26:23 +0000324 // If there is no X11 window, then this must be a resize request,
325 // not a notification from the X server.
326 if (!shown())
327 resize_req = true;
328 else {
329 // Otherwise we need to get the real window coordinates to tell
330 // the difference
331 XWindowAttributes actual;
332 Window cr;
333 int wx, wy;
Pierre Ossman0e593462012-09-03 09:43:23 +0000334
Pierre Ossman1f884e02012-10-30 10:26:23 +0000335 XGetWindowAttributes(fl_display, fl_xid(this), &actual);
336 XTranslateCoordinates(fl_display, fl_xid(this), actual.root,
337 0, 0, &wx, &wy, &cr);
338
339 // Actual resize request?
340 if ((wx != x) || (wy != y) ||
341 (actual.width != w) || (actual.height != h))
342 resize_req = true;
343 else
344 resize_req = false;
345 }
346
347 if (resize_req) {
Pierre Ossman9e0e7542012-10-03 12:21:54 +0000348 for (int i = 0;i < Fl::screen_count();i++) {
349 int sx, sy, sw, sh;
350
351 Fl::screen_xywh(sx, sy, sw, sh, i);
352
353 if ((sx == x) && (sy == y) && (sw == w) && (sh == h)) {
354 vlog.info("Adjusting window size to avoid accidental full screen request");
355 // Assume a panel of some form and adjust the height
356 y += 20;
357 h -= 40;
358 }
Pierre Ossman0e593462012-09-03 09:43:23 +0000359 }
360 }
361 }
362#endif
363
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000364 if ((this->w() != w) || (this->h() != h))
365 resizing = true;
366 else
367 resizing = false;
368
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000369 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000370
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000371 if (resizing) {
372 // Try to get the remote size to match our window size, provided
373 // the following conditions are true:
374 //
375 // a) The user has this feature turned on
376 // b) The server supports it
377 // c) We're not still waiting for a chance to handle DesktopSize
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000378 // d) We're not still waiting for startup fullscreen to kick in
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000379 //
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000380 if (not firstUpdate and not delayedFullscreen and
381 ::remoteResize and cc->cp.supportsSetDesktopSize) {
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000382 // We delay updating the remote desktop as we tend to get a flood
383 // of resize events as the user is dragging the window.
384 Fl::remove_timeout(handleResizeTimeout, this);
385 Fl::add_timeout(0.5, handleResizeTimeout, this);
386 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000387
Pierre Ossman9f32e3c2012-08-23 14:40:52 +0000388 // Deal with some scrolling corner cases
389 repositionViewport();
390 }
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000391}
392
393
Pierre Ossman407a5c32011-05-26 14:48:29 +0000394int DesktopWindow::handle(int event)
395{
396 switch (event) {
397#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000398 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000399 fullScreen.setParam(fullscreen_active());
400
Pierre Ossman1d867b62012-09-03 09:25:07 +0000401 if (fullscreen_active())
402 scroll->type(0);
403 else
404 scroll->type(Fl_Scroll::BOTH);
405
Pierre Ossman2f3a04e2012-10-24 12:15:19 +0000406 // The scroll widget isn't clever enough to actually redraw the
407 // scroll bars when they are added/removed, so we need to give
408 // it a push.
409 scroll->redraw();
410
Pierre Ossman407a5c32011-05-26 14:48:29 +0000411 if (!fullscreenSystemKeys)
412 break;
413
414 if (fullscreen_active())
415 grabKeyboard();
416 else
417 ungrabKeyboard();
418
419 break;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000420
421 case FL_ENTER:
422 case FL_LEAVE:
423 case FL_DRAG:
424 case FL_MOVE:
425 if (fullscreen_active()) {
426 if (((viewport->x() < 0) && (Fl::event_x() < EDGE_SCROLL_SIZE)) ||
427 ((viewport->x() + viewport->w() > w()) && (Fl::event_x() > w() - EDGE_SCROLL_SIZE)) ||
428 ((viewport->y() < 0) && (Fl::event_y() < EDGE_SCROLL_SIZE)) ||
429 ((viewport->y() + viewport->h() > h()) && (Fl::event_y() > h() - EDGE_SCROLL_SIZE))) {
430 if (!Fl::has_timeout(handleEdgeScroll, this))
431 Fl::add_timeout(0.1, handleEdgeScroll, this);
432 }
433 }
Pierre Ossmanb2e712b2012-09-10 11:46:08 +0000434 // Continue processing so that the viewport also gets mouse events
435 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000436#endif
Pierre Ossman1d867b62012-09-03 09:25:07 +0000437
Pierre Ossman407a5c32011-05-26 14:48:29 +0000438 case FL_SHORTCUT:
439 // Sometimes the focus gets out of whack and we fall through to the
440 // shortcut dispatching. Try to make things sane again...
441 if (Fl::focus() == NULL) {
442 take_focus();
443 Fl::handle(FL_KEYDOWN, this);
444 }
445 return 1;
446 }
447
448 return Fl_Window::handle(event);
449}
450
451
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000452int DesktopWindow::fltkHandle(int event, Fl_Window *win)
453{
454 int ret;
455
456 ret = Fl::handle_(event, win);
457
458#ifdef HAVE_FLTK_FULLSCREEN
459 // This is hackish and the result of the dodgy focus handling in FLTK.
460 // The basic problem is that FLTK's view of focus and the system's tend
461 // to differ, and as a result we do not see all the FL_FOCUS events we
462 // need. Fortunately we can grab them here...
463
464 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
465
466 if (dw && fullscreenSystemKeys) {
467 switch (event) {
468 case FL_FOCUS:
469 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
470 // some issues we need to work around:
471 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
472 // b) Gaining focus on the system level causes FLTK to switch
473 // window level on OS X.
474 if (dw->fullscreen_active())
475 dw->grabKeyboard();
476 break;
477
Pierre Ossmanb1369802012-10-17 07:59:36 +0000478 case FL_UNFOCUS:
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000479 // FIXME: We need to relinquish control when the entire window loses
480 // focus as it is very tied to this specific window on some
481 // platforms and we want to be able to open subwindows.
482 dw->ungrabKeyboard();
483 break;
484 }
485 }
486#endif
487
488 return ret;
489}
490
491
Pierre Ossmanaae38912012-07-13 11:22:55 +0000492void DesktopWindow::fullscreen_on()
493{
494#ifdef HAVE_FLTK_FULLSCREEN
495#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
496 if (not fullScreenAllMonitors)
497 fullscreen_screens(-1, -1, -1, -1);
498 else {
499 int top, bottom, left, right;
500 int top_y, bottom_y, left_x, right_x;
501
502 int sx, sy, sw, sh;
503
504 top = bottom = left = right = 0;
505
506 Fl::screen_xywh(sx, sy, sw, sh, 0);
507 top_y = sy;
508 bottom_y = sy + sh;
509 left_x = sx;
510 right_x = sx + sw;
511
512 for (int i = 1;i < Fl::screen_count();i++) {
513 Fl::screen_xywh(sx, sy, sw, sh, i);
514 if (sy < top_y) {
515 top = i;
516 top_y = sy;
517 }
518 if ((sy + sh) > bottom_y) {
519 bottom = i;
520 bottom_y = sy + sh;
521 }
522 if (sx < left_x) {
523 left = i;
524 left_x = sx;
525 }
526 if ((sx + sw) > right_x) {
527 right = i;
528 right_x = sx + sw;
529 }
530 }
531
532 fullscreen_screens(top, bottom, left, right);
533 }
534#endif // HAVE_FLTK_FULLSCREEN_SCREENS
535
536 fullscreen();
537#endif // HAVE_FLTK_FULLSCREEN
538}
539
Pierre Ossman407a5c32011-05-26 14:48:29 +0000540void DesktopWindow::grabKeyboard()
541{
542 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
543 // correct widget regardless of which low level window got the system
544 // event.
545
546 // FIXME: Push this stuff into FLTK.
547
548#if defined(WIN32)
549 int ret;
550
551 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
552 if (ret != 0)
553 vlog.error(_("Failure grabbing keyboard"));
554#elif defined(__APPLE__)
555 int ret;
556
Pierre Ossman3d759112012-08-27 14:40:51 +0000557 ret = cocoa_capture_display(this,
558#ifdef HAVE_FLTK_FULLSCREEN_SCREENS
559 fullScreenAllMonitors
560#else
561 false
562#endif
563 );
Pierre Ossman407a5c32011-05-26 14:48:29 +0000564 if (ret != 0)
565 vlog.error(_("Failure grabbing keyboard"));
566#else
567 int ret;
568
569 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000570 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000571 if (ret) {
572 if (ret == AlreadyGrabbed) {
573 // It seems like we can race with the WM in some cases.
574 // Try again in a bit.
575 if (!Fl::has_timeout(handleGrab, this))
576 Fl::add_timeout(0.500, handleGrab, this);
577 } else {
578 vlog.error(_("Failure grabbing keyboard"));
579 }
580 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000581
582 // We also need to grab the pointer as some WMs like to grab buttons
583 // combined with modifies (e.g. Alt+Button0 in metacity).
584 ret = XGrabPointer(fl_display, fl_xid(this), True,
585 ButtonPressMask|ButtonReleaseMask|
586 ButtonMotionMask|PointerMotionMask,
587 GrabModeAsync, GrabModeAsync,
588 None, None, CurrentTime);
589 if (ret)
590 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000591#endif
592}
593
594
595void DesktopWindow::ungrabKeyboard()
596{
597 Fl::remove_timeout(handleGrab, this);
598
599#if defined(WIN32)
600 win32_disable_lowlevel_keyboard(fl_xid(this));
601#elif defined(__APPLE__)
602 cocoa_release_display(this);
603#else
604 // FLTK has a grab so lets not mess with it
605 if (Fl::grab())
606 return;
607
Pierre Ossman53fd5442011-11-17 10:19:19 +0000608 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000609 XUngrabKeyboard(fl_display, fl_event_time);
610#endif
611}
612
613
614void DesktopWindow::handleGrab(void *data)
615{
616 DesktopWindow *self = (DesktopWindow*)data;
617
618 assert(self);
619
620#ifdef HAVE_FLTK_FULLSCREEN
621 if (!fullscreenSystemKeys)
622 return;
623 if (!self->fullscreen_active())
624 return;
625
626 self->grabKeyboard();
627#endif
628}
629
630
Peter Åstrand49b11572012-08-01 08:09:09 +0000631#define _NET_WM_STATE_ADD 1 /* add/set property */
632void DesktopWindow::maximizeWindow()
633{
634#if defined(WIN32)
Pierre Ossman897067b2012-10-11 09:17:19 +0000635 // We cannot use ShowWindow() in full screen mode as it will
636 // resize things implicitly. Fortunately modifying the style
637 // directly results in a maximized state once we leave full screen.
638#ifdef HAVE_FLTK_FULLSCREEN
639 if (fullscreen_active()) {
640 WINDOWINFO wi;
641 wi.cbSize = sizeof(WINDOWINFO);
642 GetWindowInfo(fl_xid(this), &wi);
643 SetWindowLongPtr(fl_xid(this), GWL_STYLE, wi.dwStyle | WS_MAXIMIZE);
644 } else
645#endif
646 ShowWindow(fl_xid(this), SW_MAXIMIZE);
Peter Åstrand49b11572012-08-01 08:09:09 +0000647#elif defined(__APPLE__)
Pierre Ossman002cc5d2012-10-02 14:45:10 +0000648 // OS X is somewhat strange and does not really have a concept of a
649 // maximized window, so we can simply resize the window to the workarea.
650 // Note that we shouldn't do this whilst in full screen as that will
651 // incorrectly adjust things.
652#ifdef HAVE_FLTK_FULLSCREEN
653 if (fullscreen_active())
654 return;
655#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000656 int X, Y, W, H;
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000657#ifdef HAVE_FLTK_WORK_AREA
Peter Åstrand49b11572012-08-01 08:09:09 +0000658 Fl::screen_work_area(X, Y, W, H, this->x(), this->y());
Pierre Ossman208b2ea2012-10-24 08:28:18 +0000659#else
660 W = Fl::w();
661 H = Fl::h();
662#endif
Peter Åstrand49b11572012-08-01 08:09:09 +0000663 size(W, H);
664#else
665 // X11
666 fl_open_display();
667 Atom net_wm_state = XInternAtom (fl_display, "_NET_WM_STATE", 0);
668 Atom net_wm_state_maximized_vert = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_VERT", 0);
669 Atom net_wm_state_maximized_horz = XInternAtom (fl_display, "_NET_WM_STATE_MAXIMIZED_HORZ", 0);
670
671 XEvent e;
672 e.xany.type = ClientMessage;
673 e.xany.window = fl_xid(this);
674 e.xclient.message_type = net_wm_state;
675 e.xclient.format = 32;
676 e.xclient.data.l[0] = _NET_WM_STATE_ADD;
677 e.xclient.data.l[1] = net_wm_state_maximized_vert;
678 e.xclient.data.l[2] = net_wm_state_maximized_horz;
679 e.xclient.data.l[3] = 0;
680 e.xclient.data.l[4] = 0;
681 XSendEvent(fl_display, RootWindow(fl_display, fl_screen), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
682#endif
683}
684
685
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000686void DesktopWindow::handleDesktopSize()
687{
688 int width, height;
689
690 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
691 return;
692
693 remoteResize(width, height);
694}
695
696
Pierre Ossmanff473402012-07-04 11:27:47 +0000697void DesktopWindow::handleResizeTimeout(void *data)
698{
699 DesktopWindow *self = (DesktopWindow *)data;
700
701 assert(self);
702
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000703 self->remoteResize(self->w(), self->h());
Pierre Ossmanff473402012-07-04 11:27:47 +0000704}
705
706
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000707void DesktopWindow::remoteResize(int width, int height)
Pierre Ossmanff473402012-07-04 11:27:47 +0000708{
Pierre Ossmanff473402012-07-04 11:27:47 +0000709 ScreenSet layout;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000710 ScreenSet::iterator iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000711
Pierre Ossmanaae38912012-07-13 11:22:55 +0000712#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000713 if (!fullscreen_active() || (width > w()) || (height > h())) {
Pierre Ossmanaae38912012-07-13 11:22:55 +0000714#endif
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000715 // In windowed mode (or the framebuffer is so large that we need
716 // to scroll) we just report a single virtual screen that covers
717 // the entire framebuffer.
Pierre Ossmanff473402012-07-04 11:27:47 +0000718
Pierre Ossmanaae38912012-07-13 11:22:55 +0000719 layout = cc->cp.screenLayout;
Pierre Ossmanff473402012-07-04 11:27:47 +0000720
Pierre Ossmanaae38912012-07-13 11:22:55 +0000721 // Not sure why we have no screens, but adding a new one should be
722 // safe as there is nothing to conflict with...
723 if (layout.num_screens() == 0)
724 layout.add_screen(rfb::Screen());
725 else if (layout.num_screens() != 1) {
726 // More than one screen. Remove all but the first (which we
727 // assume is the "primary").
Pierre Ossmanff473402012-07-04 11:27:47 +0000728
Pierre Ossmanaae38912012-07-13 11:22:55 +0000729 while (true) {
730 iter = layout.begin();
731 ++iter;
Pierre Ossmanff473402012-07-04 11:27:47 +0000732
Pierre Ossmanaae38912012-07-13 11:22:55 +0000733 if (iter == layout.end())
734 break;
735
736 layout.remove_screen(iter->id);
737 }
738 }
739
740 // Resize the remaining single screen to the complete framebuffer
741 layout.begin()->dimensions.tl.x = 0;
742 layout.begin()->dimensions.tl.y = 0;
743 layout.begin()->dimensions.br.x = width;
744 layout.begin()->dimensions.br.y = height;
745#ifdef HAVE_FLTK_FULLSCREEN
746 } else {
747 int i;
748 rdr::U32 id;
749 int sx, sy, sw, sh;
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000750 Rect viewport_rect, screen_rect;
Pierre Ossmanaae38912012-07-13 11:22:55 +0000751
752 // In full screen we report all screens that are fully covered.
753
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000754 viewport_rect.setXYWH(x() + (w() - width)/2, y() + (h() - height)/2,
755 width, height);
Pierre Ossman93d2d922012-07-20 12:32:52 +0000756
Pierre Ossmanaae38912012-07-13 11:22:55 +0000757 // If we can find a matching screen in the existing set, we use
758 // that, otherwise we create a brand new screen.
759 //
760 // FIXME: We should really track screens better so we can handle
761 // a resized one.
762 //
763 for (i = 0;i < Fl::screen_count();i++) {
764 Fl::screen_xywh(sx, sy, sw, sh, i);
765
Pierre Ossman93d2d922012-07-20 12:32:52 +0000766 // Check that the screen is fully inside the framebuffer
767 screen_rect.setXYWH(sx, sy, sw, sh);
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000768 if (!screen_rect.enclosed_by(viewport_rect))
Pierre Ossman93d2d922012-07-20 12:32:52 +0000769 continue;
770
Pierre Ossmanf44f6c02012-07-20 12:39:27 +0000771 // Adjust the coordinates so they are relative to our viewport
772 sx -= viewport_rect.tl.x;
773 sy -= viewport_rect.tl.y;
774
Pierre Ossmanaae38912012-07-13 11:22:55 +0000775 // Look for perfectly matching existing screen...
776 for (iter = cc->cp.screenLayout.begin();
777 iter != cc->cp.screenLayout.end(); ++iter) {
778 if ((iter->dimensions.tl.x == sx) &&
779 (iter->dimensions.tl.y == sy) &&
780 (iter->dimensions.width() == sw) &&
781 (iter->dimensions.height() == sh))
782 break;
783 }
784
785 // Found it?
786 if (iter != cc->cp.screenLayout.end()) {
787 layout.add_screen(*iter);
788 continue;
789 }
790
791 // Need to add a new one, which means we need to find an unused id
792 while (true) {
793 id = rand();
794 for (iter = cc->cp.screenLayout.begin();
795 iter != cc->cp.screenLayout.end(); ++iter) {
796 if (iter->id == id)
797 break;
798 }
799
800 if (iter == cc->cp.screenLayout.end())
801 break;
802 }
803
804 layout.add_screen(rfb::Screen(id, sx, sy, sw, sh, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000805 }
Pierre Ossman72b4adf2012-07-20 14:11:26 +0000806
807 // If the viewport doesn't match a physical screen, then we might
808 // end up with no screens in the layout. Add a fake one...
809 if (layout.num_screens() == 0)
810 layout.add_screen(rfb::Screen(0, 0, 0, width, height, 0));
Pierre Ossmanff473402012-07-04 11:27:47 +0000811 }
Pierre Ossmanaae38912012-07-13 11:22:55 +0000812#endif
Pierre Ossmanff473402012-07-04 11:27:47 +0000813
814 // Do we actually change anything?
815 if ((width == cc->cp.width) &&
816 (height == cc->cp.height) &&
817 (layout == cc->cp.screenLayout))
818 return;
819
Pierre Ossmanaae38912012-07-13 11:22:55 +0000820 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d (%d screens)",
821 cc->cp.width, cc->cp.height, width, height, layout.num_screens());
822
823 if (!layout.validate(width, height)) {
824 vlog.error("Invalid screen layout computed for resize request!");
Pierre Ossmanaae38912012-07-13 11:22:55 +0000825 return;
826 }
Pierre Ossmanff473402012-07-04 11:27:47 +0000827
828 cc->writer()->writeSetDesktopSize(width, height, layout);
829}
830
831
Pierre Ossman6455d852011-06-01 09:33:00 +0000832void DesktopWindow::repositionViewport()
833{
834 int new_x, new_y;
835
836 // Deal with some scrolling corner cases:
837 //
838 // a) If the window is larger then the viewport, center the viewport.
839 // b) If the window is smaller than the viewport, make sure there is
840 // no wasted space on the sides.
841 //
842 // FIXME: Doesn't compensate for scroll widget size properly.
843
844 new_x = viewport->x();
845 new_y = viewport->y();
846
847 if (w() > viewport->w())
848 new_x = (w() - viewport->w()) / 2;
849 else {
850 if (viewport->x() > 0)
851 new_x = 0;
852 else if (w() > (viewport->x() + viewport->w()))
853 new_x = w() - viewport->w();
854 }
855
856 // Same thing for y axis
857 if (h() > viewport->h())
858 new_y = (h() - viewport->h()) / 2;
859 else {
860 if (viewport->y() > 0)
861 new_y = 0;
862 else if (h() > (viewport->y() + viewport->h()))
863 new_y = h() - viewport->h();
864 }
865
866 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
867 viewport->position(new_x, new_y);
868
869 // The scroll widget does not notice when you move around child widgets,
870 // so redraw everything to make sure things update.
871 redraw();
872 }
873}
874
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000875void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
876{
877 exit_vncviewer();
878}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000879
880
881void DesktopWindow::handleOptions(void *data)
882{
883 DesktopWindow *self = (DesktopWindow*)data;
884
885#ifdef HAVE_FLTK_FULLSCREEN
886 if (self->fullscreen_active() && fullscreenSystemKeys)
887 self->grabKeyboard();
888 else
889 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000890
Pierre Ossmanff473402012-07-04 11:27:47 +0000891 if (fullScreen && !self->fullscreen_active())
Pierre Ossmanaae38912012-07-13 11:22:55 +0000892 self->fullscreen_on();
Pierre Ossmanff473402012-07-04 11:27:47 +0000893 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000894 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000895#endif
896}
Pierre Ossman4c4b66f2012-08-23 14:53:36 +0000897
898void DesktopWindow::handleFullscreenTimeout(void *data)
899{
900 DesktopWindow *self = (DesktopWindow *)data;
901
902 assert(self);
903
904 self->delayedFullscreen = false;
905
906 if (self->delayedDesktopSize) {
907 self->handleDesktopSize();
908 self->delayedDesktopSize = false;
909 }
910}
Pierre Ossman1d867b62012-09-03 09:25:07 +0000911
912void DesktopWindow::handleEdgeScroll(void *data)
913{
914#ifdef HAVE_FLTK_FULLSCREEN
915 DesktopWindow *self = (DesktopWindow *)data;
916
917 int mx, my;
918 int dx, dy;
919
920 assert(self);
921
922 if (!self->fullscreen_active())
923 return;
924
925 mx = Fl::event_x();
926 my = Fl::event_y();
927
928 dx = dy = 0;
929
930 // Clamp mouse position in case it is outside the window
931 if (mx < 0)
932 mx = 0;
933 if (mx > self->w())
934 mx = self->w();
935 if (my < 0)
936 my = 0;
937 if (my > self->h())
938 my = self->h();
939
940 if ((self->viewport->x() < 0) && (mx < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000941 dx = EDGE_SCROLL_SPEED -
942 EDGE_SCROLL_SPEED * mx / EDGE_SCROLL_SIZE;
943 if ((self->viewport->x() + self->viewport->w() > self->w()) &&
944 (mx > self->w() - EDGE_SCROLL_SIZE))
945 dx = EDGE_SCROLL_SPEED * (self->w() - mx) / EDGE_SCROLL_SIZE -
946 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000947 if ((self->viewport->y() < 0) && (my < EDGE_SCROLL_SIZE))
Pierre Ossmandd138442012-09-03 09:45:40 +0000948 dy = EDGE_SCROLL_SPEED -
949 EDGE_SCROLL_SPEED * my / EDGE_SCROLL_SIZE;
950 if ((self->viewport->y() + self->viewport->h() > self->h()) &&
951 (my > self->h() - EDGE_SCROLL_SIZE))
952 dy = EDGE_SCROLL_SPEED * (self->h() - my) / EDGE_SCROLL_SIZE -
953 EDGE_SCROLL_SPEED;
Pierre Ossman1d867b62012-09-03 09:25:07 +0000954
955 if ((dx == 0) && (dy == 0))
956 return;
957
958 // Make sure we don't move the viewport too much
959 if (self->viewport->x() + dx > 0)
960 dx = -self->viewport->x();
961 if (self->viewport->x() + dx + self->viewport->w() < self->w())
962 dx = self->w() - (self->viewport->x() + self->viewport->w());
963 if (self->viewport->y() + dy > 0)
964 dy = -self->viewport->y();
965 if (self->viewport->y() + dy + self->viewport->h() < self->h())
966 dy = self->h() - (self->viewport->y() + self->viewport->h());
967
968 self->scroll->scroll_to(self->scroll->xposition() - dx, self->scroll->yposition() - dy);
969
970 Fl::repeat_timeout(0.1, handleEdgeScroll, data);
971#endif
972}