blob: 290539161ef47f4101e91b7f8460add6072817fe [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
Peter Åstrandc359f362011-08-23 12:04:46 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Pierre Ossman5156d5e2011-03-09 09:42:34 +000024#include <assert.h>
25#include <stdio.h>
26#include <string.h>
27
Pierre Ossman5156d5e2011-03-09 09:42:34 +000028#include <rfb/LogWriter.h>
Pierre Ossmanff473402012-07-04 11:27:47 +000029#include <rfb/CMsgWriter.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000030
31#include "DesktopWindow.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000032#include "OptionsDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000033#include "i18n.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000034#include "parameters.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000035#include "vncviewer.h"
Pierre Ossmanff473402012-07-04 11:27:47 +000036#include "CConn.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000037
DRCb65bb932011-06-24 03:17:00 +000038#include <FL/Fl_Scroll.H>
39#include <FL/x.H>
40
Pierre Ossman407a5c32011-05-26 14:48:29 +000041#ifdef WIN32
42#include "win32.h"
43#endif
44
45#ifdef __APPLE__
46#include "cocoa.h"
47#endif
Pierre Ossman89f868a2011-04-11 11:59:31 +000048
Pierre Ossman5156d5e2011-03-09 09:42:34 +000049using namespace rfb;
50
Pierre Ossman5156d5e2011-03-09 09:42:34 +000051static rfb::LogWriter vlog("DesktopWindow");
52
53DesktopWindow::DesktopWindow(int w, int h, const char *name,
54 const rfb::PixelFormat& serverPF,
55 CConn* cc_)
Pierre Ossmanff473402012-07-04 11:27:47 +000056 : Fl_Window(w, h), cc(cc_), firstUpdate(true)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000057{
Pierre Ossman4ae229f2011-04-15 12:58:31 +000058 Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h);
59 scroll->color(FL_BLACK);
60
61 // Automatically adjust the scroll box to the window
62 resizable(scroll);
63
Pierre Ossmanff473402012-07-04 11:27:47 +000064 viewport = new Viewport(w, h, serverPF, cc);
Pierre Ossmand50b3d12011-04-15 07:46:56 +000065
Pierre Ossman4ae229f2011-04-15 12:58:31 +000066 scroll->end();
67
Pierre Ossman5156d5e2011-03-09 09:42:34 +000068 callback(handleClose, this);
69
70 setName(name);
71
Pierre Ossman407a5c32011-05-26 14:48:29 +000072 OptionsDialog::addCallback(handleOptions, this);
73
Pierre Ossmana4f0f182011-06-14 13:36:57 +000074 // Hack. See below...
75 Fl::event_dispatch(&fltkHandle);
76
Pierre Ossman63ca58e2011-05-26 14:59:32 +000077#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossmanff473402012-07-04 11:27:47 +000078 if (fullScreen)
Pierre Ossman63ca58e2011-05-26 14:59:32 +000079 fullscreen();
Pierre Ossmanff473402012-07-04 11:27:47 +000080 else
Pierre Ossman63ca58e2011-05-26 14:59:32 +000081#endif
Peter Åstrandd62482e2011-08-02 08:33:27 +000082 {
83 // If we are creating a window which is equal to the size on the
84 // screen on X11, many WMs will treat this as a legacy fullscreen
85 // request. This is not what we want. Besides, it doesn't really
86 // make sense to try to create a window which is larger than the
87 // available work space.
88 size(__rfbmin(w, Fl::w()), __rfbmin(h, Fl::h()));
89 }
Pierre Ossman63ca58e2011-05-26 14:59:32 +000090
Pierre Ossman5156d5e2011-03-09 09:42:34 +000091 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +000092
93 // The window manager might give us an initial window size that is different
94 // than the one we requested, and in those cases we need to manually adjust
95 // the scroll widget for things to behave sanely.
96 if ((w != this->w()) || (h != this->h()))
97 scroll->size(this->w(), this->h());
Pierre Ossman5156d5e2011-03-09 09:42:34 +000098}
99
100
101DesktopWindow::~DesktopWindow()
102{
Pierre Ossman407a5c32011-05-26 14:48:29 +0000103 // Unregister all timeouts in case they get a change tro trigger
104 // again later when this object is already gone.
105 Fl::remove_timeout(handleGrab, this);
Pierre Ossmanff473402012-07-04 11:27:47 +0000106 Fl::remove_timeout(handleResizeTimeout, this);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000107
108 OptionsDialog::removeCallback(handleOptions);
109
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000110 // FLTK automatically deletes all child widgets, so we shouldn't touch
111 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000112}
113
114
115void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
116{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000117 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000118}
119
120
121const rfb::PixelFormat &DesktopWindow::getPreferredPF()
122{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000123 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000124}
125
126
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000127void DesktopWindow::setName(const char *name)
128{
129 CharArray windowNameStr;
130 windowNameStr.replaceBuf(new char[256]);
131
Pierre Ossman27820ba2011-09-30 12:54:24 +0000132 snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000133
134 copy_label(windowNameStr.buf);
135}
136
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000137
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000138void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
139 rdr::U16* rgbs)
140{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000141 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000142}
143
144
145// Copy the areas of the framebuffer that have been changed (damaged)
146// to the displayed window.
147
148void DesktopWindow::updateWindow()
149{
Pierre Ossmanff473402012-07-04 11:27:47 +0000150 if (firstUpdate) {
151 if (cc->cp.supportsSetDesktopSize)
152 remoteResize();
153 firstUpdate = false;
154 }
155
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000156 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000157}
158
159
Pierre Ossman6455d852011-06-01 09:33:00 +0000160void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
161{
162 if ((new_w == viewport->w()) && (new_h == viewport->h()))
163 return;
164
Pierre Ossman6455d852011-06-01 09:33:00 +0000165 // If we're letting the viewport match the window perfectly, then
166 // keep things that way for the new size, otherwise just keep things
167 // like they are.
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000168#ifdef HAVE_FLTK_FULLSCREEN
169 if (!fullscreen_active()) {
170#endif
Pierre Ossman29e4a162011-11-21 14:03:31 +0000171 if ((w() == viewport->w()) && (h() == viewport->h()))
172 size(new_w, new_h);
173 else {
174 // Make sure the window isn't too big. We do this manually because
175 // we have to disable the window size restriction (and it isn't
176 // entirely trustworthy to begin with).
Pierre Ossman6455d852011-06-01 09:33:00 +0000177 if ((w() > new_w) || (h() > new_h))
178 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
Pierre Ossman29e4a162011-11-21 14:03:31 +0000179 }
Peter Åstrand1d03cbc2011-08-01 10:34:38 +0000180#ifdef HAVE_FLTK_FULLSCREEN
181 }
182#endif
Pierre Ossman6455d852011-06-01 09:33:00 +0000183
184 viewport->size(new_w, new_h);
185
186 // We might not resize the main window, so we need to manually call this
187 // to make sure the viewport is centered.
188 repositionViewport();
189
Pierre Ossman6455d852011-06-01 09:33:00 +0000190 // repositionViewport() makes sure the scroll widget notices any changes
191 // in position, but it might be just the size that changes so we also
192 // need a poke here as well.
193 redraw();
194}
195
196
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000197void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
198 void* data, void* mask)
199{
200 viewport->setCursor(width, height, hotspot, data, mask);
201}
202
203
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000204void DesktopWindow::resize(int x, int y, int w, int h)
205{
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000206 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000207
Pierre Ossmanff473402012-07-04 11:27:47 +0000208 // Try to get the remote size to match our window size, provided
209 // the following conditions are true:
210 //
211 // a) The user has this feature turned on
212 // b) The server supports it
213 // c) We're not still waiting for a chance to handle DesktopSize
214 //
215 if (not firstUpdate and ::remoteResize and cc->cp.supportsSetDesktopSize) {
216 // We delay updating the remote desktop as we tend to get a flood
217 // of resize events as the user is dragging the window.
218 Fl::remove_timeout(handleResizeTimeout, this);
219 Fl::add_timeout(0.5, handleResizeTimeout, this);
220 }
221
Pierre Ossman6455d852011-06-01 09:33:00 +0000222 // Deal with some scrolling corner cases
223 repositionViewport();
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000224}
225
226
Pierre Ossman407a5c32011-05-26 14:48:29 +0000227int DesktopWindow::handle(int event)
228{
229 switch (event) {
230#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000231 case FL_FULLSCREEN:
Pierre Ossman29e4a162011-11-21 14:03:31 +0000232 fullScreen.setParam(fullscreen_active());
233
Pierre Ossman407a5c32011-05-26 14:48:29 +0000234 if (!fullscreenSystemKeys)
235 break;
236
237 if (fullscreen_active())
238 grabKeyboard();
239 else
240 ungrabKeyboard();
241
242 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000243#endif
244 case FL_SHORTCUT:
245 // Sometimes the focus gets out of whack and we fall through to the
246 // shortcut dispatching. Try to make things sane again...
247 if (Fl::focus() == NULL) {
248 take_focus();
249 Fl::handle(FL_KEYDOWN, this);
250 }
251 return 1;
252 }
253
254 return Fl_Window::handle(event);
255}
256
257
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000258int DesktopWindow::fltkHandle(int event, Fl_Window *win)
259{
260 int ret;
261
262 ret = Fl::handle_(event, win);
263
264#ifdef HAVE_FLTK_FULLSCREEN
265 // This is hackish and the result of the dodgy focus handling in FLTK.
266 // The basic problem is that FLTK's view of focus and the system's tend
267 // to differ, and as a result we do not see all the FL_FOCUS events we
268 // need. Fortunately we can grab them here...
269
270 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
271
272 if (dw && fullscreenSystemKeys) {
273 switch (event) {
274 case FL_FOCUS:
275 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
276 // some issues we need to work around:
277 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
278 // b) Gaining focus on the system level causes FLTK to switch
279 // window level on OS X.
280 if (dw->fullscreen_active())
281 dw->grabKeyboard();
282 break;
283
284 case FL_UNFOCUS:
285 // FIXME: We need to relinquish control when the entire window loses
286 // focus as it is very tied to this specific window on some
287 // platforms and we want to be able to open subwindows.
288 dw->ungrabKeyboard();
289 break;
290 }
291 }
292#endif
293
294 return ret;
295}
296
297
Pierre Ossman407a5c32011-05-26 14:48:29 +0000298void DesktopWindow::grabKeyboard()
299{
300 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
301 // correct widget regardless of which low level window got the system
302 // event.
303
304 // FIXME: Push this stuff into FLTK.
305
306#if defined(WIN32)
307 int ret;
308
309 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
310 if (ret != 0)
311 vlog.error(_("Failure grabbing keyboard"));
312#elif defined(__APPLE__)
313 int ret;
314
315 ret = cocoa_capture_display(this);
316 if (ret != 0)
317 vlog.error(_("Failure grabbing keyboard"));
318#else
319 int ret;
320
321 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
Peter Åstrandf52860b2011-07-18 07:42:16 +0000322 GrabModeAsync, GrabModeAsync, CurrentTime);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000323 if (ret) {
324 if (ret == AlreadyGrabbed) {
325 // It seems like we can race with the WM in some cases.
326 // Try again in a bit.
327 if (!Fl::has_timeout(handleGrab, this))
328 Fl::add_timeout(0.500, handleGrab, this);
329 } else {
330 vlog.error(_("Failure grabbing keyboard"));
331 }
332 }
Pierre Ossman53fd5442011-11-17 10:19:19 +0000333
334 // We also need to grab the pointer as some WMs like to grab buttons
335 // combined with modifies (e.g. Alt+Button0 in metacity).
336 ret = XGrabPointer(fl_display, fl_xid(this), True,
337 ButtonPressMask|ButtonReleaseMask|
338 ButtonMotionMask|PointerMotionMask,
339 GrabModeAsync, GrabModeAsync,
340 None, None, CurrentTime);
341 if (ret)
342 vlog.error(_("Failure grabbing mouse"));
Pierre Ossman407a5c32011-05-26 14:48:29 +0000343#endif
344}
345
346
347void DesktopWindow::ungrabKeyboard()
348{
349 Fl::remove_timeout(handleGrab, this);
350
351#if defined(WIN32)
352 win32_disable_lowlevel_keyboard(fl_xid(this));
353#elif defined(__APPLE__)
354 cocoa_release_display(this);
355#else
356 // FLTK has a grab so lets not mess with it
357 if (Fl::grab())
358 return;
359
Pierre Ossman53fd5442011-11-17 10:19:19 +0000360 XUngrabPointer(fl_display, fl_event_time);
Pierre Ossman407a5c32011-05-26 14:48:29 +0000361 XUngrabKeyboard(fl_display, fl_event_time);
362#endif
363}
364
365
366void DesktopWindow::handleGrab(void *data)
367{
368 DesktopWindow *self = (DesktopWindow*)data;
369
370 assert(self);
371
372#ifdef HAVE_FLTK_FULLSCREEN
373 if (!fullscreenSystemKeys)
374 return;
375 if (!self->fullscreen_active())
376 return;
377
378 self->grabKeyboard();
379#endif
380}
381
382
Pierre Ossmanff473402012-07-04 11:27:47 +0000383void DesktopWindow::handleResizeTimeout(void *data)
384{
385 DesktopWindow *self = (DesktopWindow *)data;
386
387 assert(self);
388
389 self->remoteResize();
390}
391
392
393void DesktopWindow::remoteResize()
394{
395 int width, height;
396 ScreenSet layout;
397
398 if (firstUpdate) {
399 if (sscanf(desktopSize.getValueStr(), "%dx%d", &width, &height) != 2)
400 return;
401 } else {
402 width = w();
403 height = h();
404 }
405
406 layout = cc->cp.screenLayout;
407
408 if (layout.num_screens() == 0)
409 layout.add_screen(rfb::Screen());
410 else if (layout.num_screens() != 1) {
411 ScreenSet::iterator iter;
412
413 while (true) {
414 iter = layout.begin();
415 ++iter;
416
417 if (iter == layout.end())
418 break;
419
420 layout.remove_screen(iter->id);
421 }
422 }
423
424 layout.begin()->dimensions.tl.x = 0;
425 layout.begin()->dimensions.tl.y = 0;
426 layout.begin()->dimensions.br.x = width;
427 layout.begin()->dimensions.br.y = height;
428
429 // Do we actually change anything?
430 if ((width == cc->cp.width) &&
431 (height == cc->cp.height) &&
432 (layout == cc->cp.screenLayout))
433 return;
434
435 vlog.debug("Requesting framebuffer resize from %dx%d to %dx%d",
436 cc->cp.width, cc->cp.height, width, height);
437
438 cc->writer()->writeSetDesktopSize(width, height, layout);
439}
440
441
Pierre Ossman6455d852011-06-01 09:33:00 +0000442void DesktopWindow::repositionViewport()
443{
444 int new_x, new_y;
445
446 // Deal with some scrolling corner cases:
447 //
448 // a) If the window is larger then the viewport, center the viewport.
449 // b) If the window is smaller than the viewport, make sure there is
450 // no wasted space on the sides.
451 //
452 // FIXME: Doesn't compensate for scroll widget size properly.
453
454 new_x = viewport->x();
455 new_y = viewport->y();
456
457 if (w() > viewport->w())
458 new_x = (w() - viewport->w()) / 2;
459 else {
460 if (viewport->x() > 0)
461 new_x = 0;
462 else if (w() > (viewport->x() + viewport->w()))
463 new_x = w() - viewport->w();
464 }
465
466 // Same thing for y axis
467 if (h() > viewport->h())
468 new_y = (h() - viewport->h()) / 2;
469 else {
470 if (viewport->y() > 0)
471 new_y = 0;
472 else if (h() > (viewport->y() + viewport->h()))
473 new_y = h() - viewport->h();
474 }
475
476 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
477 viewport->position(new_x, new_y);
478
479 // The scroll widget does not notice when you move around child widgets,
480 // so redraw everything to make sure things update.
481 redraw();
482 }
483}
484
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000485void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
486{
487 exit_vncviewer();
488}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000489
490
491void DesktopWindow::handleOptions(void *data)
492{
493 DesktopWindow *self = (DesktopWindow*)data;
494
495#ifdef HAVE_FLTK_FULLSCREEN
496 if (self->fullscreen_active() && fullscreenSystemKeys)
497 self->grabKeyboard();
498 else
499 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000500
Pierre Ossmanff473402012-07-04 11:27:47 +0000501 if (fullScreen && !self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000502 self->fullscreen();
Pierre Ossmanff473402012-07-04 11:27:47 +0000503 else if (!fullScreen && self->fullscreen_active())
Pierre Ossman91911642011-05-26 14:57:51 +0000504 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000505#endif
506}