1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <rfb/LogWriter.h>
#include "DesktopWindow.h"
#include "OptionsDialog.h"
#include "i18n.h"
#include "parameters.h"
#include "vncviewer.h"
#include <FL/Fl_Scroll.H>
#include <FL/x.H>
#ifdef WIN32
#include "win32.h"
#endif
#ifdef __APPLE__
#include "cocoa.h"
#endif
using namespace rfb;
static rfb::LogWriter vlog("DesktopWindow");
DesktopWindow::DesktopWindow(int w, int h, const char *name,
const rfb::PixelFormat& serverPF,
CConn* cc_)
: Fl_Window(w, h)
{
// Allow resize
size_range(100, 100, w, h);
Fl_Scroll *scroll = new Fl_Scroll(0, 0, w, h);
scroll->color(FL_BLACK);
// Automatically adjust the scroll box to the window
resizable(scroll);
viewport = new Viewport(w, h, serverPF, cc_);
scroll->end();
callback(handleClose, this);
setName(name);
OptionsDialog::addCallback(handleOptions, this);
// Hack. See below...
Fl::event_dispatch(&fltkHandle);
#ifdef HAVE_FLTK_FULLSCREEN
if (fullScreen) {
// See comment in DesktopWindow::handleOptions
size_range(100, 100, 0, 0);
fullscreen();
} else
#endif
{
// If we are creating a window which is equal to the size on the
// screen on X11, many WMs will treat this as a legacy fullscreen
// request. This is not what we want. Besides, it doesn't really
// make sense to try to create a window which is larger than the
// available work space.
size(__rfbmin(w, Fl::w()), __rfbmin(h, Fl::h()));
}
show();
// The window manager might give us an initial window size that is different
// than the one we requested, and in those cases we need to manually adjust
// the scroll widget for things to behave sanely.
if ((w != this->w()) || (h != this->h()))
scroll->size(this->w(), this->h());
}
DesktopWindow::~DesktopWindow()
{
// Unregister all timeouts in case they get a change tro trigger
// again later when this object is already gone.
Fl::remove_timeout(handleGrab, this);
OptionsDialog::removeCallback(handleOptions);
// FLTK automatically deletes all child widgets, so we shouldn't touch
// them ourselves here
}
void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
{
viewport->setServerPF(pf);
}
const rfb::PixelFormat &DesktopWindow::getPreferredPF()
{
return viewport->getPreferredPF();
}
void DesktopWindow::setName(const char *name)
{
CharArray windowNameStr;
windowNameStr.replaceBuf(new char[256]);
snprintf(windowNameStr.buf, 256, "%.240s - TigerVNC", name);
copy_label(windowNameStr.buf);
}
void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
rdr::U16* rgbs)
{
viewport->setColourMapEntries(firstColour, nColours, rgbs);
}
// Copy the areas of the framebuffer that have been changed (damaged)
// to the displayed window.
void DesktopWindow::updateWindow()
{
viewport->updateWindow();
}
void DesktopWindow::resizeFramebuffer(int new_w, int new_h)
{
if ((new_w == viewport->w()) && (new_h == viewport->h()))
return;
// Turn off size limitations for a bit while we juggle things around
size_range(100, 100, 0, 0);
// If we're letting the viewport match the window perfectly, then
// keep things that way for the new size, otherwise just keep things
// like they are.
#ifdef HAVE_FLTK_FULLSCREEN
if (!fullscreen_active()) {
#endif
if ((w() == viewport->w()) && (h() == viewport->h()))
size(new_w, new_h);
else {
// Make sure the window isn't too big
if ((w() > new_w) || (h() > new_h))
size(__rfbmin(w(), new_w), __rfbmin(h(), new_h));
}
#ifdef HAVE_FLTK_FULLSCREEN
}
#endif
viewport->size(new_w, new_h);
// We might not resize the main window, so we need to manually call this
// to make sure the viewport is centered.
repositionViewport();
// Update allowed resize range
#ifdef HAVE_FLTK_FULLSCREEN
if (!fullscreen_active())
#endif
size_range(100, 100, new_w, new_h);
// repositionViewport() makes sure the scroll widget notices any changes
// in position, but it might be just the size that changes so we also
// need a poke here as well.
redraw();
}
void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
void* data, void* mask)
{
viewport->setCursor(width, height, hotspot, data, mask);
}
void DesktopWindow::resize(int x, int y, int w, int h)
{
Fl_Window::resize(x, y, w, h);
// Deal with some scrolling corner cases
repositionViewport();
}
int DesktopWindow::handle(int event)
{
switch (event) {
#ifdef HAVE_FLTK_FULLSCREEN
case FL_FULLSCREEN:
if (event == FL_FULLSCREEN) {
fullScreen.setParam(fullscreen_active());
if (!fullscreen_active()) {
size_range(100, 100, viewport->w(), viewport->h());
size(viewport->w(), viewport->h());
} else {
// We need to turn off the size limitations for proper
// fullscreen support, but in case fullscreen is activated via
// the WM, this is a bit of a problem. In practice, it seems to
// work to change the size limits after we have recieved the
// FL_FULLSCREEN event, at least with my Metacity.
size_range(100, 100, 0, 0);
}
}
if (!fullscreenSystemKeys)
break;
if (fullscreen_active())
grabKeyboard();
else
ungrabKeyboard();
break;
#endif
case FL_SHORTCUT:
// Sometimes the focus gets out of whack and we fall through to the
// shortcut dispatching. Try to make things sane again...
if (Fl::focus() == NULL) {
take_focus();
Fl::handle(FL_KEYDOWN, this);
}
return 1;
}
return Fl_Window::handle(event);
}
int DesktopWindow::fltkHandle(int event, Fl_Window *win)
{
int ret;
ret = Fl::handle_(event, win);
#ifdef HAVE_FLTK_FULLSCREEN
// This is hackish and the result of the dodgy focus handling in FLTK.
// The basic problem is that FLTK's view of focus and the system's tend
// to differ, and as a result we do not see all the FL_FOCUS events we
// need. Fortunately we can grab them here...
DesktopWindow *dw = dynamic_cast<DesktopWindow*>(win);
if (dw && fullscreenSystemKeys) {
switch (event) {
case FL_FOCUS:
// FIXME: We reassert the keyboard grabbing on focus as FLTK there are
// some issues we need to work around:
// a) Fl::grab(0) on X11 will release the keyboard grab for us.
// b) Gaining focus on the system level causes FLTK to switch
// window level on OS X.
if (dw->fullscreen_active())
dw->grabKeyboard();
break;
case FL_UNFOCUS:
// FIXME: We need to relinquish control when the entire window loses
// focus as it is very tied to this specific window on some
// platforms and we want to be able to open subwindows.
dw->ungrabKeyboard();
break;
}
}
#endif
return ret;
}
void DesktopWindow::grabKeyboard()
{
// Grabbing the keyboard is fairly safe as FLTK reroutes events to the
// correct widget regardless of which low level window got the system
// event.
// FIXME: Push this stuff into FLTK.
#if defined(WIN32)
int ret;
ret = win32_enable_lowlevel_keyboard(fl_xid(this));
if (ret != 0)
vlog.error(_("Failure grabbing keyboard"));
#elif defined(__APPLE__)
int ret;
ret = cocoa_capture_display(this);
if (ret != 0)
vlog.error(_("Failure grabbing keyboard"));
#else
int ret;
ret = XGrabKeyboard(fl_display, fl_xid(this), True,
GrabModeAsync, GrabModeAsync, CurrentTime);
if (ret) {
if (ret == AlreadyGrabbed) {
// It seems like we can race with the WM in some cases.
// Try again in a bit.
if (!Fl::has_timeout(handleGrab, this))
Fl::add_timeout(0.500, handleGrab, this);
} else {
vlog.error(_("Failure grabbing keyboard"));
}
}
#endif
}
void DesktopWindow::ungrabKeyboard()
{
Fl::remove_timeout(handleGrab, this);
#if defined(WIN32)
win32_disable_lowlevel_keyboard(fl_xid(this));
#elif defined(__APPLE__)
cocoa_release_display(this);
#else
// FLTK has a grab so lets not mess with it
if (Fl::grab())
return;
XUngrabKeyboard(fl_display, fl_event_time);
#endif
}
void DesktopWindow::handleGrab(void *data)
{
DesktopWindow *self = (DesktopWindow*)data;
assert(self);
#ifdef HAVE_FLTK_FULLSCREEN
if (!fullscreenSystemKeys)
return;
if (!self->fullscreen_active())
return;
self->grabKeyboard();
#endif
}
void DesktopWindow::repositionViewport()
{
int new_x, new_y;
// Deal with some scrolling corner cases:
//
// a) If the window is larger then the viewport, center the viewport.
// b) If the window is smaller than the viewport, make sure there is
// no wasted space on the sides.
//
// FIXME: Doesn't compensate for scroll widget size properly.
new_x = viewport->x();
new_y = viewport->y();
if (w() > viewport->w())
new_x = (w() - viewport->w()) / 2;
else {
if (viewport->x() > 0)
new_x = 0;
else if (w() > (viewport->x() + viewport->w()))
new_x = w() - viewport->w();
}
// Same thing for y axis
if (h() > viewport->h())
new_y = (h() - viewport->h()) / 2;
else {
if (viewport->y() > 0)
new_y = 0;
else if (h() > (viewport->y() + viewport->h()))
new_y = h() - viewport->h();
}
if ((new_x != viewport->x()) || (new_y != viewport->y())) {
viewport->position(new_x, new_y);
// The scroll widget does not notice when you move around child widgets,
// so redraw everything to make sure things update.
redraw();
}
}
void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
{
exit_vncviewer();
}
void DesktopWindow::handleOptions(void *data)
{
DesktopWindow *self = (DesktopWindow*)data;
#ifdef HAVE_FLTK_FULLSCREEN
if (self->fullscreen_active() && fullscreenSystemKeys)
self->grabKeyboard();
else
self->ungrabKeyboard();
if (fullScreen && !self->fullscreen_active()) {
// Some WMs (Metacity) apparently requires that the size limits
// are removed before fullscreen
self->size_range(100, 100, 0, 0);
self->fullscreen();
} else if (!fullScreen && self->fullscreen_active())
self->fullscreen_off();
#endif
}
|