blob: 41abf6be292235eb4796f12ffa3dc8d6b4d03f5f [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 Ossman6455d852011-06-01 09:33:00 +0000170 if ((w() == viewport->w()) && (h() == viewport->h()))
171 size(new_w, new_h);
172 else {
Pierre Ossman6455d852011-06-01 09:33:00 +0000173 // Make sure the window isn't too big
174 if ((w() > new_w) || (h() > new_h))
175 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman6455d852011-06-01 09:33:00 +0000176 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000177#ifdef HAVE_FLTK_FULLSCREEN
178 }
179#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000180
181 viewport->size(new_w, new_h);
182
183 // We might not resize the main window, so we need to manually call this
184 // to make sure the viewport is centered.
185 repositionViewport();
186
187 // Update allowed resize range
Peter Åstrandadf5e252011-07-19 09:28:39 +0000188#ifdef HAVE_FLTK_FULLSCREEN
189 if (!fullscreen_active())
190#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000191 size_range(100, 100, new_w, new_h);
192
193 // repositionViewport() makes sure the scroll widget notices any changes
194 // in position, but it might be just the size that changes so we also
195 // need a poke here as well.
196 redraw();
197}
198
199
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000200void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
201 void* data, void* mask)
202{
203 viewport->setCursor(width, height, hotspot, data, mask);
204}
205
206
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000207void DesktopWindow::resize(int x, int y, int w, int h)
208{
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000209 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000210
211 // Deal with some scrolling corner cases
212 repositionViewport();
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000213}
214
215
Pierre Ossman407a5c32011-05-26 14:48:29 +0000216int DesktopWindow::handle(int event)
217{
218 switch (event) {
219#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000220 case FL_FULLSCREEN:
Peter Åstrand17067b12011-07-18 07:45:14 +0000221 if (event == FL_FULLSCREEN) {
Pierre Ossman105738c2011-05-26 14:57:25 +0000222 fullScreen.setParam(fullscreen_active());
Peter Åstrand17067b12011-07-18 07:45:14 +0000223 if (!fullscreen_active()) {
224 size_range(100, 100, viewport->w(), viewport->h());
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000225 size(viewport->w(), viewport->h());
Peter Åstrand17067b12011-07-18 07:45:14 +0000226 } else {
227 // We need to turn off the size limitations for proper
228 // fullscreen support, but in case fullscreen is activated via
229 // the WM, this is a bit of a problem. In practice, it seems to
230 // work to change the size limits after we have recieved the
231 // FL_FULLSCREEN event, at least with my Metacity.
232 size_range(100, 100, 0, 0);
233 }
234 }
Pierre Ossman1870ab12011-05-26 14:53:49 +0000235
Pierre Ossman407a5c32011-05-26 14:48:29 +0000236 if (!fullscreenSystemKeys)
237 break;
238
239 if (fullscreen_active())
240 grabKeyboard();
241 else
242 ungrabKeyboard();
243
244 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000245#endif
246 case FL_SHORTCUT:
247 // Sometimes the focus gets out of whack and we fall through to the
248 // shortcut dispatching. Try to make things sane again...
249 if (Fl::focus() == NULL) {
250 take_focus();
251 Fl::handle(FL_KEYDOWN, this);
252 }
253 return 1;
254 }
255
256 return Fl_Window::handle(event);
257}
258
259
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000260int DesktopWindow::fltkHandle(int event, Fl_Window *win)
261{
262 int ret;
263
264 ret = Fl::handle_(event, win);
265
266#ifdef HAVE_FLTK_FULLSCREEN
267 // This is hackish and the result of the dodgy focus handling in FLTK.
268 // The basic problem is that FLTK's view of focus and the system's tend
269 // to differ, and as a result we do not see all the FL_FOCUS events we
270 // need. Fortunately we can grab them here...
271
272 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
273
274 if (dw && fullscreenSystemKeys) {
275 switch (event) {
276 case FL_FOCUS:
277 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
278 // some issues we need to work around:
279 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
280 // b) Gaining focus on the system level causes FLTK to switch
281 // window level on OS X.
282 if (dw->fullscreen_active())
283 dw->grabKeyboard();
284 break;
285
286 case FL_UNFOCUS:
287 // FIXME: We need to relinquish control when the entire window loses
288 // focus as it is very tied to this specific window on some
289 // platforms and we want to be able to open subwindows.
290 dw->ungrabKeyboard();
291 break;
292 }
293 }
294#endif
295
296 return ret;
297}
298
299
Pierre Ossman407a5c32011-05-26 14:48:29 +0000300void DesktopWindow::grabKeyboard()
301{
302 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
303 // correct widget regardless of which low level window got the system
304 // event.
305
306 // FIXME: Push this stuff into FLTK.
307
308#if defined(WIN32)
309 int ret;
310
311 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
312 if (ret != 0)
313 vlog.error(_("Failure grabbing keyboard"));
314#elif defined(__APPLE__)
315 int ret;
316
317 ret = cocoa_capture_display(this);
318 if (ret != 0)
319 vlog.error(_("Failure grabbing keyboard"));
320#else
321 int ret;
322
323 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000324 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000325 if (ret) {
326 if (ret == AlreadyGrabbed) {
327 // It seems like we can race with the WM in some cases.
328 // Try again in a bit.
329 if (!Fl::has_timeout(handleGrab, this))
330 Fl::add_timeout(0.500, handleGrab, this);
331 } else {
332 vlog.error(_("Failure grabbing keyboard"));
333 }
334 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000335
336 // We also need to grab the pointer as some WMs like to grab buttons
337 // combined with modifies (e.g. Alt+Button0 in metacity).
338 ret = XGrabPointer(fl_display, fl_xid(this), True,
339 ButtonPressMask|ButtonReleaseMask|
340 ButtonMotionMask|PointerMotionMask,
341 GrabModeAsync, GrabModeAsync,
342 None, None, CurrentTime);
343 if (ret)
344 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000345#endif
346}
347
348
349void DesktopWindow::ungrabKeyboard()
350{
351 Fl::remove_timeout(handleGrab, this);
352
353#if defined(WIN32)
354 win32_disable_lowlevel_keyboard(fl_xid(this));
355#elif defined(__APPLE__)
356 cocoa_release_display(this);
357#else
358 // FLTK has a grab so lets not mess with it
359 if (Fl::grab())
360 return;
361
Pierre Ossman53fd5442011-11-17 10:19:19 +0000362 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000363 XUngrabKeyboard(fl_display, fl_event_time);
364#endif
365}
366
367
368void DesktopWindow::handleGrab(void *data)
369{
370 DesktopWindow *self = (DesktopWindow*)data;
371
372 assert(self);
373
374#ifdef HAVE_FLTK_FULLSCREEN
375 if (!fullscreenSystemKeys)
376 return;
377 if (!self->fullscreen_active())
378 return;
379
380 self->grabKeyboard();
381#endif
382}
383
384
Pierre Ossman6455d852011-06-01 09:33:00 +0000385void DesktopWindow::repositionViewport()
386{
387 int new_x, new_y;
388
389 // Deal with some scrolling corner cases:
390 //
391 // a) If the window is larger then the viewport, center the viewport.
392 // b) If the window is smaller than the viewport, make sure there is
393 // no wasted space on the sides.
394 //
395 // FIXME: Doesn't compensate for scroll widget size properly.
396
397 new_x = viewport->x();
398 new_y = viewport->y();
399
400 if (w() > viewport->w())
401 new_x = (w() - viewport->w()) / 2;
402 else {
403 if (viewport->x() > 0)
404 new_x = 0;
405 else if (w() > (viewport->x() + viewport->w()))
406 new_x = w() - viewport->w();
407 }
408
409 // Same thing for y axis
410 if (h() > viewport->h())
411 new_y = (h() - viewport->h()) / 2;
412 else {
413 if (viewport->y() > 0)
414 new_y = 0;
415 else if (h() > (viewport->y() + viewport->h()))
416 new_y = h() - viewport->h();
417 }
418
419 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
420 viewport->position(new_x, new_y);
421
422 // The scroll widget does not notice when you move around child widgets,
423 // so redraw everything to make sure things update.
424 redraw();
425 }
426}
427
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000428void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
429{
430 exit_vncviewer();
431}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000432
433
434void DesktopWindow::handleOptions(void *data)
435{
436 DesktopWindow *self = (DesktopWindow*)data;
437
438#ifdef HAVE_FLTK_FULLSCREEN
439 if (self->fullscreen_active() && fullscreenSystemKeys)
440 self->grabKeyboard();
441 else
442 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000443
Peter Åstrand17067b12011-07-18 07:45:14 +0000444 if (fullScreen && !self->fullscreen_active()) {
445 // Some WMs (Metacity) apparently requires that the size limits
446 // are removed before fullscreen
447 self->size_range(100, 100, 0, 0);
Pierre Ossman91911642011-05-26 14:57:51 +0000448 self->fullscreen();
Peter Åstrand17067b12011-07-18 07:45:14 +0000449 } else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000450 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000451#endif
452}