blob: 0e438e7fd7b212972f5d715b9ba2c2e02717461c [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 Ossman63ca58e2011-05-26 14:59:32 +000072#ifdef HAVE_FLTK_FULLSCREEN
73 if (fullScreen)
74 fullscreen();
75#endif
76
Pierre Ossman5156d5e2011-03-09 09:42:34 +000077 show();
Pierre Ossman3f6c4d02011-04-15 14:09:09 +000078
79 // The window manager might give us an initial window size that is different
80 // than the one we requested, and in those cases we need to manually adjust
81 // the scroll widget for things to behave sanely.
82 if ((w != this->w()) || (h != this->h()))
83 scroll->size(this->w(), this->h());
Pierre Ossman5156d5e2011-03-09 09:42:34 +000084}
85
86
87DesktopWindow::~DesktopWindow()
88{
Pierre Ossman407a5c32011-05-26 14:48:29 +000089 // Unregister all timeouts in case they get a change tro trigger
90 // again later when this object is already gone.
91 Fl::remove_timeout(handleGrab, this);
92
93 OptionsDialog::removeCallback(handleOptions);
94
Pierre Ossmand50b3d12011-04-15 07:46:56 +000095 // FLTK automatically deletes all child widgets, so we shouldn't touch
96 // them ourselves here
Pierre Ossman5156d5e2011-03-09 09:42:34 +000097}
98
99
100void DesktopWindow::setServerPF(const rfb::PixelFormat& pf)
101{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000102 viewport->setServerPF(pf);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000103}
104
105
106const rfb::PixelFormat &DesktopWindow::getPreferredPF()
107{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000108 return viewport->getPreferredPF();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000109}
110
111
112// Cursor stuff
113
114void DesktopWindow::setCursor(int width, int height, const Point& hotspot,
115 void* data, void* mask)
116{
117}
118
119
120void DesktopWindow::setName(const char *name)
121{
122 CharArray windowNameStr;
123 windowNameStr.replaceBuf(new char[256]);
124
125 snprintf(windowNameStr.buf, 256, _("TigerVNC: %.240s"), name);
126
127 copy_label(windowNameStr.buf);
128}
129
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000130
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000131void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
132 rdr::U16* rgbs)
133{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000134 viewport->setColourMapEntries(firstColour, nColours, rgbs);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000135}
136
137
138// Copy the areas of the framebuffer that have been changed (damaged)
139// to the displayed window.
140
141void DesktopWindow::updateWindow()
142{
Pierre Ossmand50b3d12011-04-15 07:46:56 +0000143 viewport->updateWindow();
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000144}
145
146
Pierre Ossman4ae229f2011-04-15 12:58:31 +0000147void DesktopWindow::resize(int x, int y, int w, int h)
148{
149 // Deal with some scrolling corner cases:
150 //
151 // a) If the window is larger then the viewport, center the viewport.
152 // b) If the window is smaller than the viewport, make sure there is
153 // no wasted space on the sides.
154 //
155 // FIXME: Doesn't compensate for scroll widget size properly.
156 if (w > viewport->w())
157 viewport->position((w - viewport->w()) / 2, viewport->y());
158 else {
159 if (viewport->x() > 0)
160 viewport->position(0, viewport->y());
161 else if (w > (viewport->x() + viewport->w()))
162 viewport->position(w - viewport->w(), viewport->y());
163 }
164
165 // Same thing for y axis
166 if (h > viewport->h())
167 viewport->position(viewport->x(), (h - viewport->h()) / 2);
168 else {
169 if (viewport->y() > 0)
170 viewport->position(viewport->x(), 0);
171 else if (h > (viewport->y() + viewport->h()))
172 viewport->position(viewport->x(), h - viewport->h());
173 }
174
175 Fl_Window::resize(x, y, w, h);
176}
177
178
Pierre Ossman407a5c32011-05-26 14:48:29 +0000179int DesktopWindow::handle(int event)
180{
181 switch (event) {
182#ifdef HAVE_FLTK_FULLSCREEN
183 case FL_FOCUS:
184 // FIXME: We reassert the keyboard grabbing on focus/unfocus as FLTK
185 // releases the grab when someone calls Fl::grab(0)
186 case FL_FULLSCREEN:
Pierre Ossman105738c2011-05-26 14:57:25 +0000187 if (event == FL_FULLSCREEN)
188 fullScreen.setParam(fullscreen_active());
Pierre Ossman1870ab12011-05-26 14:53:49 +0000189
Pierre Ossman407a5c32011-05-26 14:48:29 +0000190 if (!fullscreenSystemKeys)
191 break;
192
193 if (fullscreen_active())
194 grabKeyboard();
195 else
196 ungrabKeyboard();
197
198 break;
199 case FL_UNFOCUS:
200 // FIXME: We need to relinquish control when the entire window loses
201 // focus as it seems to interfere with the WM:s ability to handle
202 // interactions with popups' window decorations.
203 ungrabKeyboard();
204 break;
205#endif
206 case FL_SHORTCUT:
207 // Sometimes the focus gets out of whack and we fall through to the
208 // shortcut dispatching. Try to make things sane again...
209 if (Fl::focus() == NULL) {
210 take_focus();
211 Fl::handle(FL_KEYDOWN, this);
212 }
213 return 1;
214 }
215
216 return Fl_Window::handle(event);
217}
218
219
220void DesktopWindow::grabKeyboard()
221{
222 // Grabbing the keyboard is fairly safe as FLTK reroutes events to the
223 // correct widget regardless of which low level window got the system
224 // event.
225
226 // FIXME: Push this stuff into FLTK.
227
228#if defined(WIN32)
229 int ret;
230
231 ret = win32_enable_lowlevel_keyboard(fl_xid(this));
232 if (ret != 0)
233 vlog.error(_("Failure grabbing keyboard"));
234#elif defined(__APPLE__)
235 int ret;
236
237 ret = cocoa_capture_display(this);
238 if (ret != 0)
239 vlog.error(_("Failure grabbing keyboard"));
240#else
241 int ret;
242
243 ret = XGrabKeyboard(fl_display, fl_xid(this), True,
244 GrabModeAsync, GrabModeAsync, fl_event_time);
245 if (ret) {
246 if (ret == AlreadyGrabbed) {
247 // It seems like we can race with the WM in some cases.
248 // Try again in a bit.
249 if (!Fl::has_timeout(handleGrab, this))
250 Fl::add_timeout(0.500, handleGrab, this);
251 } else {
252 vlog.error(_("Failure grabbing keyboard"));
253 }
254 }
255#endif
256}
257
258
259void DesktopWindow::ungrabKeyboard()
260{
261 Fl::remove_timeout(handleGrab, this);
262
263#if defined(WIN32)
264 win32_disable_lowlevel_keyboard(fl_xid(this));
265#elif defined(__APPLE__)
266 cocoa_release_display(this);
267#else
268 // FLTK has a grab so lets not mess with it
269 if (Fl::grab())
270 return;
271
272 XUngrabKeyboard(fl_display, fl_event_time);
273#endif
274}
275
276
277void DesktopWindow::handleGrab(void *data)
278{
279 DesktopWindow *self = (DesktopWindow*)data;
280
281 assert(self);
282
283#ifdef HAVE_FLTK_FULLSCREEN
284 if (!fullscreenSystemKeys)
285 return;
286 if (!self->fullscreen_active())
287 return;
288
289 self->grabKeyboard();
290#endif
291}
292
293
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000294void DesktopWindow::handleClose(Fl_Widget *wnd, void *data)
295{
296 exit_vncviewer();
297}
Pierre Ossman407a5c32011-05-26 14:48:29 +0000298
299
300void DesktopWindow::handleOptions(void *data)
301{
302 DesktopWindow *self = (DesktopWindow*)data;
303
304#ifdef HAVE_FLTK_FULLSCREEN
305 if (self->fullscreen_active() && fullscreenSystemKeys)
306 self->grabKeyboard();
307 else
308 self->ungrabKeyboard();
Pierre Ossman91911642011-05-26 14:57:51 +0000309
310 if (fullScreen && !self->fullscreen_active())
311 self->fullscreen();
312 else if (!fullScreen && self->fullscreen_active())
313 self->fullscreen_off();
Pierre Ossman407a5c32011-05-26 14:48:29 +0000314#endif
315}