blob: d2975dc04ad2b6ec2bc58717853525395f6036ca [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>
29
30#include "DesktopWindow.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000031#include "OptionsDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000032#include "i18n.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000033#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000034#include "vncviewer.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000035
DRCb65bb932011-06-24 03:17:00 +000036#include <FL/Fl_Scroll.H>
37#include <FL/x.H>
38
Pierre Ossman407a5c32011-05-26 14:48:29 +000039#ifdef WIN32
40#include "win32.h"
41#endif
42
43#ifdef __APPLE__
44#include "cocoa.h"
45#endif
Pierre Ossman89f868a2011-04-11 11:59:31 +000046
Pierre Ossman5156d5e2011-03-09 09:42:34 +000047using namespace rfb;
48
Pierre Ossman5156d5e2011-03-09 09:42:34 +000049static rfb::LogWriter vlog("DesktopWindow");
50
51DesktopWindow::DesktopWindow(int w, int h, const char *name,
52 const rfb::PixelFormat& serverPF,
53 CConn* cc_)
Pierre Ossmand50b3d12011-04-15 07:46:56 +000054 : Fl_Window(w, h)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000055{
Pierre Ossman4ae229f2011-04-15 12:58:31 +000056 // Allow resize
Pierre Ossman343e2e02011-04-15 13:00:12 +000057 size_range(100, 100, w, h);
Pierre Ossman4ae229f2011-04-15 12:58:31 +000058
59 Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h);
60 scroll->color(FL_BLACK);
61
62 // Automatically adjust the scroll box to the window
63 resizable(scroll);
64
Pierre Ossmand50b3d12011-04-15 07:46:56 +000065 viewport = new Viewport(w, h, serverPF, cc_);
66
Pierre Ossman4ae229f2011-04-15 12:58:31 +000067 scroll->end();
68
Pierre Ossman5156d5e2011-03-09 09:42:34 +000069 callback(handleClose, this);
70
71 setName(name);
72
Pierre Ossman407a5c32011-05-26 14:48:29 +000073 OptionsDialog::addCallback(handleOptions, this);
74
Pierre Ossmana4f0f182011-06-14 13:36:57 +000075 // Hack. See below...
76 Fl::event_dispatch(&fltkHandle);
77
Pierre Ossman63ca58e2011-05-26 14:59:32 +000078#ifdef HAVE_FLTK_FULLSCREEN
Peter Åstrand17067b12011-07-18 07:45:14 +000079 if (fullScreen) {
80 // See comment in DesktopWindow::handleOptions
81 size_range(100, 100, 0, 0);
Pierre Ossman63ca58e2011-05-26 14:59:32 +000082 fullscreen();
Peter Åstrandd62482e2011-08-02 08:33:27 +000083 } else
Pierre Ossman63ca58e2011-05-26 14:59:32 +000084#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +000085 {
86 // If we are creating a window which is equal to the size on the
87 // screen on X11, many WMs will treat this as a legacy fullscreen
88 // request. This is not what we want. Besides, it doesn't really
89 // make sense to try to create a window which is larger than the
90 // available work space.
91 size(__rfbmin(w, Fl::w()), __rfbmin(h, Fl::h()));
92 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +000093
Pierre Ossman5156d5e2011-03-09 09:42:34 +000094 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +000095
96 // The window manager might give us an initial window size that is different
97 // than the one we requested, and in those cases we need to manually adjust
98 // the scroll widget for things to behave sanely.
99 if ((w != this->w()) || (h != this->h()))
100 scroll->size(this->w(), this->h());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000101}
102
103
104DesktopWindow::~DesktopWindow()
105{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000106 // Unregister all timeouts in case they get a change tro trigger
107 // again later when this object is already gone.
108 Fl::remove_timeout(handleGrab, this);
109
110 OptionsDialog::removeCallback(handleOptions);
111
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000112 // FLTK automatically deletes all child widgets, so we shouldn't touch
113 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000114}
115
116
117void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
118{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000119 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000120}
121
122
123const rfb::PixelFormat &DesktopWindow::getPreferredPF()
124{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000125 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000126}
127
128
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000129void DesktopWindow::setName(const char *name)
130{
131 CharArray windowNameStr;
132 windowNameStr.replaceBuf(new char[256]);
133
Pierre Ossman27820ba2011-09-30 12:54:24 +0000134 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000135
136 copy_label(windowNameStr.buf);
137}
138
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000139
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000140void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
141 rdr::U16* rgbs)
142{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000143 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000144}
145
146
147// Copy the areas of the framebuffer that have been changed (damaged)
148// to the displayed window.
149
150void DesktopWindow::updateWindow()
151{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000152 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000153}
154
155
Pierre Ossman6455d852011-06-01 09:33:00 +0000156void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
157{
158 if ((new_w == viewport->w()) && (new_h == viewport->h()))
159 return;
160
161 // Turn off size limitations for a bit while we juggle things around
162 size_range(100, 100, 0, 0);
163
164 // If we're letting the viewport match the window perfectly, then
165 // keep things that way for the new size, otherwise just keep things
166 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000167#ifdef HAVE_FLTK_FULLSCREEN
168 if (!fullscreen_active()) {
169#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000170 if ((w() == viewport->w()) && (h() == viewport->h()))
171 size(new_w, new_h);
172 else {
173 // Make sure the window isn't too big. We do this manually because
174 // we have to disable the window size restriction (and it isn't
175 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000176 if ((w() > new_w) || (h() > new_h))
177 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000178 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000179#ifdef HAVE_FLTK_FULLSCREEN
180 }
181#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000182
183 viewport->size(new_w, new_h);
184
185 // We might not resize the main window, so we need to manually call this
186 // to make sure the viewport is centered.
187 repositionViewport();
188
189 // Update allowed resize range
Peter Åstrandadf5e252011-07-19 09:28:39 +0000190#ifdef HAVE_FLTK_FULLSCREEN
191 if (!fullscreen_active())
192#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000193 size_range(100, 100, new_w, new_h);
194
195 // repositionViewport() makes sure the scroll widget notices any changes
196 // in position, but it might be just the size that changes so we also
197 // need a poke here as well.
198 redraw();
199}
200
201
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000202void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
203 void* data, void* mask)
204{
205 viewport->setCursor(width, height, hotspot, data, mask);
206}
207
208
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000209void DesktopWindow::resize(int x, int y, int w, int h)
210{
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000211 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000212
213 // Deal with some scrolling corner cases
214 repositionViewport();
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000215}
216
217
Pierre Ossman407a5c32011-05-26 14:48:29 +0000218int DesktopWindow::handle(int event)
219{
220 switch (event) {
221#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000222 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000223 fullScreen.setParam(fullscreen_active());
224
225 if (!fullscreen_active()) {
226 size_range(100, 100, viewport->w(), viewport->h());
227 size(viewport->w(), viewport->h());
228 } else {
229 // We need to turn off the size limitations for proper
230 // fullscreen support, but in case fullscreen is activated via
231 // the WM, this is a bit of a problem. In practice, it seems to
232 // work to change the size limits after we have recieved the
233 // FL_FULLSCREEN event, at least with my Metacity.
234 size_range(100, 100, 0, 0);
Peter Åstrand17067b12011-07-18 07:45:14 +0000235 }
Pierre Ossman1870ab12011-05-26 14:53:49 +0000236
Pierre Ossman407a5c32011-05-26 14:48:29 +0000237 if (!fullscreenSystemKeys)
238 break;
239
240 if (fullscreen_active())
241 grabKeyboard();
242 else
243 ungrabKeyboard();
244
245 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000246#endif
247 case FL_SHORTCUT:
248 // Sometimes the focus gets out of whack and we fall through to the
249 // shortcut dispatching. Try to make things sane again...
250 if (Fl::focus() == NULL) {
251 take_focus();
252 Fl::handle(FL_KEYDOWN, this);
253 }
254 return 1;
255 }
256
257 return Fl_Window::handle(event);
258}
259
260
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000261int DesktopWindow::fltkHandle(int event, Fl_Window *win)
262{
263 int ret;
264
265 ret = Fl::handle_(event, win);
266
267#ifdef HAVE_FLTK_FULLSCREEN
268 // This is hackish and the result of the dodgy focus handling in FLTK.
269 // The basic problem is that FLTK's view of focus and the system's tend
270 // to differ, and as a result we do not see all the FL_FOCUS events we
271 // need. Fortunately we can grab them here...
272
273 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
274
275 if (dw && fullscreenSystemKeys) {
276 switch (event) {
277 case FL_FOCUS:
278 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
279 // some issues we need to work around:
280 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
281 // b) Gaining focus on the system level causes FLTK to switch
282 // window level on OS X.
283 if (dw->fullscreen_active())
284 dw->grabKeyboard();
285 break;
286
287 case FL_UNFOCUS:
288 // FIXME: We need to relinquish control when the entire window loses
289 // focus as it is very tied to this specific window on some
290 // platforms and we want to be able to open subwindows.
291 dw->ungrabKeyboard();
292 break;
293 }
294 }
295#endif
296
297 return ret;
298}
299
300
Pierre Ossman407a5c32011-05-26 14:48:29 +0000301void DesktopWindow::grabKeyboard()
302{
303 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
304 // correct widget regardless of which low level window got the system
305 // event.
306
307 // FIXME: Push this stuff into FLTK.
308
309#if defined(WIN32)
310 int ret;
311
312 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
313 if (ret != 0)
314 vlog.error(_("Failure grabbing keyboard"));
315#elif defined(__APPLE__)
316 int ret;
317
318 ret = cocoa_capture_display(this);
319 if (ret != 0)
320 vlog.error(_("Failure grabbing keyboard"));
321#else
322 int ret;
323
324 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000325 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000326 if (ret) {
327 if (ret == AlreadyGrabbed) {
328 // It seems like we can race with the WM in some cases.
329 // Try again in a bit.
330 if (!Fl::has_timeout(handleGrab, this))
331 Fl::add_timeout(0.500, handleGrab, this);
332 } else {
333 vlog.error(_("Failure grabbing keyboard"));
334 }
335 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000336
337 // We also need to grab the pointer as some WMs like to grab buttons
338 // combined with modifies (e.g. Alt+Button0 in metacity).
339 ret = XGrabPointer(fl_display, fl_xid(this), True,
340 ButtonPressMask|ButtonReleaseMask|
341 ButtonMotionMask|PointerMotionMask,
342 GrabModeAsync, GrabModeAsync,
343 None, None, CurrentTime);
344 if (ret)
345 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000346#endif
347}
348
349
350void DesktopWindow::ungrabKeyboard()
351{
352 Fl::remove_timeout(handleGrab, this);
353
354#if defined(WIN32)
355 win32_disable_lowlevel_keyboard(fl_xid(this));
356#elif defined(__APPLE__)
357 cocoa_release_display(this);
358#else
359 // FLTK has a grab so lets not mess with it
360 if (Fl::grab())
361 return;
362
Pierre Ossman53fd5442011-11-17 10:19:19 +0000363 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000364 XUngrabKeyboard(fl_display, fl_event_time);
365#endif
366}
367
368
369void DesktopWindow::handleGrab(void *data)
370{
371 DesktopWindow *self = (DesktopWindow*)data;
372
373 assert(self);
374
375#ifdef HAVE_FLTK_FULLSCREEN
376 if (!fullscreenSystemKeys)
377 return;
378 if (!self->fullscreen_active())
379 return;
380
381 self->grabKeyboard();
382#endif
383}
384
385
Pierre Ossman6455d852011-06-01 09:33:00 +0000386void DesktopWindow::repositionViewport()
387{
388 int new_x, new_y;
389
390 // Deal with some scrolling corner cases:
391 //
392 // a) If the window is larger then the viewport, center the viewport.
393 // b) If the window is smaller than the viewport, make sure there is
394 // no wasted space on the sides.
395 //
396 // FIXME: Doesn't compensate for scroll widget size properly.
397
398 new_x = viewport->x();
399 new_y = viewport->y();
400
401 if (w() > viewport->w())
402 new_x = (w() - viewport->w()) / 2;
403 else {
404 if (viewport->x() > 0)
405 new_x = 0;
406 else if (w() > (viewport->x() + viewport->w()))
407 new_x = w() - viewport->w();
408 }
409
410 // Same thing for y axis
411 if (h() > viewport->h())
412 new_y = (h() - viewport->h()) / 2;
413 else {
414 if (viewport->y() > 0)
415 new_y = 0;
416 else if (h() > (viewport->y() + viewport->h()))
417 new_y = h() - viewport->h();
418 }
419
420 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
421 viewport->position(new_x, new_y);
422
423 // The scroll widget does not notice when you move around child widgets,
424 // so redraw everything to make sure things update.
425 redraw();
426 }
427}
428
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000429void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
430{
431 exit_vncviewer();
432}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000433
434
435void DesktopWindow::handleOptions(void *data)
436{
437 DesktopWindow *self = (DesktopWindow*)data;
438
439#ifdef HAVE_FLTK_FULLSCREEN
440 if (self->fullscreen_active() && fullscreenSystemKeys)
441 self->grabKeyboard();
442 else
443 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000444
Peter Åstrand17067b12011-07-18 07:45:14 +0000445 if (fullScreen && !self->fullscreen_active()) {
446 // Some WMs (Metacity) apparently requires that the size limits
447 // are removed before fullscreen
448 self->size_range(100, 100, 0, 0);
Pierre Ossman91911642011-05-26 14:57:51 +0000449 self->fullscreen();
Peter Åstrand17067b12011-07-18 07:45:14 +0000450 } else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000451 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000452#endif
453}