blob: 456e372fb20328bedee7f1f809df0cea928499ef [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
20#include <assert.h>
21#include <stdio.h>
22#include <string.h>
23
Pierre Ossman4ae229f2011-04-15 12:58:31 +000024#include <FL/Fl_Scroll.H>
Pierre Ossman407a5c32011-05-26 14:48:29 +000025#include <FL/x.H>
Pierre Ossman4ae229f2011-04-15 12:58:31 +000026
Pierre Ossman5156d5e2011-03-09 09:42:34 +000027#include <rfb/LogWriter.h>
28
29#include "DesktopWindow.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000030#include "OptionsDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000031#include "i18n.h"
Pierre Ossman407a5c32011-05-26 14:48:29 +000032#include "parameters.h"
33
34#ifdef WIN32
35#include "win32.h"
36#endif
37
38#ifdef __APPLE__
39#include "cocoa.h"
40#endif
Pierre Ossman89f868a2011-04-11 11:59:31 +000041
Pierre Ossman5156d5e2011-03-09 09:42:34 +000042using namespace rfb;
43
44extern void exit_vncviewer();
45
46static rfb::LogWriter vlog("DesktopWindow");
47
48DesktopWindow::DesktopWindow(int w, int h, const char *name,
49 const rfb::PixelFormat& serverPF,
50 CConn* cc_)
Pierre Ossmand50b3d12011-04-15 07:46:56 +000051 : Fl_Window(w, h)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000052{
Pierre Ossman4ae229f2011-04-15 12:58:31 +000053 // Allow resize
Pierre Ossman343e2e02011-04-15 13:00:12 +000054 size_range(100, 100, w, h);
Pierre Ossman4ae229f2011-04-15 12:58:31 +000055
56 Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h);
57 scroll->color(FL_BLACK);
58
59 // Automatically adjust the scroll box to the window
60 resizable(scroll);
61
Pierre Ossmand50b3d12011-04-15 07:46:56 +000062 viewport = new Viewport(w, h, serverPF, cc_);
63
Pierre Ossman4ae229f2011-04-15 12:58:31 +000064 scroll->end();
65
Pierre Ossman5156d5e2011-03-09 09:42:34 +000066 callback(handleClose, this);
67
68 setName(name);
69
Pierre Ossman407a5c32011-05-26 14:48:29 +000070 OptionsDialog::addCallback(handleOptions, this);
71
Pierre Ossmana4f0f182011-06-14 13:36:57 +000072 // Hack. See below...
73 Fl::event_dispatch(&fltkHandle);
74
Pierre Ossman63ca58e2011-05-26 14:59:32 +000075#ifdef HAVE_FLTK_FULLSCREEN
76 if (fullScreen)
77 fullscreen();
78#endif
79
Pierre Ossman5156d5e2011-03-09 09:42:34 +000080 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +000081
82 // The window manager might give us an initial window size that is different
83 // than the one we requested, and in those cases we need to manually adjust
84 // the scroll widget for things to behave sanely.
85 if ((w != this->w()) || (h != this->h()))
86 scroll->size(this->w(), this->h());
Pierre Ossman5156d5e2011-03-09 09:42:34 +000087}
88
89
90DesktopWindow::~DesktopWindow()
91{
Pierre Ossman407a5c32011-05-26 14:48:29 +000092 // Unregister all timeouts in case they get a change tro trigger
93 // again later when this object is already gone.
94 Fl::remove_timeout(handleGrab, this);
95
96 OptionsDialog::removeCallback(handleOptions);
97
Pierre Ossmand50b3d12011-04-15 07:46:56 +000098 // FLTK automatically deletes all child widgets, so we shouldn't touch
99 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000100}
101
102
103void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
104{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000105 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000106}
107
108
109const rfb::PixelFormat &DesktopWindow::getPreferredPF()
110{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000111 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000112}
113
114
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000115void DesktopWindow::setName(const char *name)
116{
117 CharArray windowNameStr;
118 windowNameStr.replaceBuf(new char[256]);
119
120 snprintf(windowNameStr.buf, 256, _("TigerVNC: %.240s"), name);
121
122 copy_label(windowNameStr.buf);
123}
124
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000125
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000126void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
127 rdr::U16* rgbs)
128{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000129 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000130}
131
132
133// Copy the areas of the framebuffer that have been changed (damaged)
134// to the displayed window.
135
136void DesktopWindow::updateWindow()
137{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000138 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000139}
140
141
Pierre Ossman6455d852011-06-01 09:33:00 +0000142void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
143{
144 if ((new_w == viewport->w()) && (new_h == viewport->h()))
145 return;
146
147 // Turn off size limitations for a bit while we juggle things around
148 size_range(100, 100, 0, 0);
149
150 // If we're letting the viewport match the window perfectly, then
151 // keep things that way for the new size, otherwise just keep things
152 // like they are.
153 if ((w() == viewport->w()) && (h() == viewport->h()))
154 size(new_w, new_h);
155 else {
156#ifdef HAVE_FLTK_FULLSCREEN
157 if (!fullscreen_active()) {
158#endif
159 // Make sure the window isn't too big
160 if ((w() > new_w) || (h() > new_h))
161 size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
162#ifdef HAVE_FLTK_FULLSCREEN
163 }
164#endif
165 }
166
167 viewport->size(new_w, new_h);
168
169 // We might not resize the main window, so we need to manually call this
170 // to make sure the viewport is centered.
171 repositionViewport();
172
173 // Update allowed resize range
174 size_range(100, 100, new_w, new_h);
175
176 // repositionViewport() makes sure the scroll widget notices any changes
177 // in position, but it might be just the size that changes so we also
178 // need a poke here as well.
179 redraw();
180}
181
182
Pierre Ossman835b4ef2011-06-08 17:02:36 +0000183void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
184 void* data, void* mask)
185{
186 viewport->setCursor(width, height, hotspot, data, mask);
187}
188
189
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000190void DesktopWindow::resize(int x, int y, int w, int h)
191{
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000192 Fl_Window::resize(x, y, w, h);
Pierre Ossman6455d852011-06-01 09:33:00 +0000193
194 // Deal with some scrolling corner cases
195 repositionViewport();
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000196}
197
198
Pierre Ossman407a5c32011-05-26 14:48:29 +0000199int DesktopWindow::handle(int event)
200{
201 switch (event) {
202#ifdef HAVE_FLTK_FULLSCREEN
Pierre Ossman407a5c32011-05-26 14:48:29 +0000203 case FL_FULLSCREEN:
Pierre Ossman105738c2011-05-26 14:57:25 +0000204 if (event == FL_FULLSCREEN)
205 fullScreen.setParam(fullscreen_active());
Pierre Ossman1870ab12011-05-26 14:53:49 +0000206
Pierre Ossman407a5c32011-05-26 14:48:29 +0000207 if (!fullscreenSystemKeys)
208 break;
209
210 if (fullscreen_active())
211 grabKeyboard();
212 else
213 ungrabKeyboard();
214
215 break;
Pierre Ossman407a5c32011-05-26 14:48:29 +0000216#endif
217 case FL_SHORTCUT:
218 // Sometimes the focus gets out of whack and we fall through to the
219 // shortcut dispatching. Try to make things sane again...
220 if (Fl::focus() == NULL) {
221 take_focus();
222 Fl::handle(FL_KEYDOWN, this);
223 }
224 return 1;
225 }
226
227 return Fl_Window::handle(event);
228}
229
230
Pierre Ossmana4f0f182011-06-14 13:36:57 +0000231int DesktopWindow::fltkHandle(int event, Fl_Window *win)
232{
233 int ret;
234
235 ret = Fl::handle_(event, win);
236
237#ifdef HAVE_FLTK_FULLSCREEN
238 // This is hackish and the result of the dodgy focus handling in FLTK.
239 // The basic problem is that FLTK's view of focus and the system's tend
240 // to differ, and as a result we do not see all the FL_FOCUS events we
241 // need. Fortunately we can grab them here...
242
243 DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
244
245 if (dw && fullscreenSystemKeys) {
246 switch (event) {
247 case FL_FOCUS:
248 // FIXME: We reassert the keyboard grabbing on focus as FLTK there are
249 // some issues we need to work around:
250 // a) Fl::grab(0) on X11 will release the keyboard grab for us.
251 // b) Gaining focus on the system level causes FLTK to switch
252 // window level on OS X.
253 if (dw->fullscreen_active())
254 dw->grabKeyboard();
255 break;
256
257 case FL_UNFOCUS:
258 // FIXME: We need to relinquish control when the entire window loses
259 // focus as it is very tied to this specific window on some
260 // platforms and we want to be able to open subwindows.
261 dw->ungrabKeyboard();
262 break;
263 }
264 }
265#endif
266
267 return ret;
268}
269
270
Pierre Ossman407a5c32011-05-26 14:48:29 +0000271void DesktopWindow::grabKeyboard()
272{
273 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
274 // correct widget regardless of which low level window got the system
275 // event.
276
277 // FIXME: Push this stuff into FLTK.
278
279#if defined(WIN32)
280 int ret;
281
282 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
283 if (ret != 0)
284 vlog.error(_("Failure grabbing keyboard"));
285#elif defined(__APPLE__)
286 int ret;
287
288 ret = cocoa_capture_display(this);
289 if (ret != 0)
290 vlog.error(_("Failure grabbing keyboard"));
291#else
292 int ret;
293
294 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
295 GrabModeAsync, GrabModeAsync, fl_event_time);
296 if (ret) {
297 if (ret == AlreadyGrabbed) {
298 // It seems like we can race with the WM in some cases.
299 // Try again in a bit.
300 if (!Fl::has_timeout(handleGrab, this))
301 Fl::add_timeout(0.500, handleGrab, this);
302 } else {
303 vlog.error(_("Failure grabbing keyboard"));
304 }
305 }
306#endif
307}
308
309
310void DesktopWindow::ungrabKeyboard()
311{
312 Fl::remove_timeout(handleGrab, this);
313
314#if defined(WIN32)
315 win32_disable_lowlevel_keyboard(fl_xid(this));
316#elif defined(__APPLE__)
317 cocoa_release_display(this);
318#else
319 // FLTK has a grab so lets not mess with it
320 if (Fl::grab())
321 return;
322
323 XUngrabKeyboard(fl_display, fl_event_time);
324#endif
325}
326
327
328void DesktopWindow::handleGrab(void *data)
329{
330 DesktopWindow *self = (DesktopWindow*)data;
331
332 assert(self);
333
334#ifdef HAVE_FLTK_FULLSCREEN
335 if (!fullscreenSystemKeys)
336 return;
337 if (!self->fullscreen_active())
338 return;
339
340 self->grabKeyboard();
341#endif
342}
343
344
Pierre Ossman6455d852011-06-01 09:33:00 +0000345void DesktopWindow::repositionViewport()
346{
347 int new_x, new_y;
348
349 // Deal with some scrolling corner cases:
350 //
351 // a) If the window is larger then the viewport, center the viewport.
352 // b) If the window is smaller than the viewport, make sure there is
353 // no wasted space on the sides.
354 //
355 // FIXME: Doesn't compensate for scroll widget size properly.
356
357 new_x = viewport->x();
358 new_y = viewport->y();
359
360 if (w() > viewport->w())
361 new_x = (w() - viewport->w()) / 2;
362 else {
363 if (viewport->x() > 0)
364 new_x = 0;
365 else if (w() > (viewport->x() + viewport->w()))
366 new_x = w() - viewport->w();
367 }
368
369 // Same thing for y axis
370 if (h() > viewport->h())
371 new_y = (h() - viewport->h()) / 2;
372 else {
373 if (viewport->y() > 0)
374 new_y = 0;
375 else if (h() > (viewport->y() + viewport->h()))
376 new_y = h() - viewport->h();
377 }
378
379 if ((new_x != viewport->x()) || (new_y != viewport->y())) {
380 viewport->position(new_x, new_y);
381
382 // The scroll widget does not notice when you move around child widgets,
383 // so redraw everything to make sure things update.
384 redraw();
385 }
386}
387
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000388void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
389{
390 exit_vncviewer();
391}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000392
393
394void DesktopWindow::handleOptions(void *data)
395{
396 DesktopWindow *self = (DesktopWindow*)data;
397
398#ifdef HAVE_FLTK_FULLSCREEN
399 if (self->fullscreen_active() && fullscreenSystemKeys)
400 self->grabKeyboard();
401 else
402 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000403
404 if (fullScreen && !self->fullscreen_active())
405 self->fullscreen();
406 else if (!fullScreen && self->fullscreen_active())
407 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000408#endif
409}