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