Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 1 | /* 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 Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 24 | #include <FL/Fl_Scroll.H> |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 25 | #include <FL/x.H> |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 26 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 27 | #include <rfb/LogWriter.h> |
| 28 | |
| 29 | #include "DesktopWindow.h" |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 30 | #include "OptionsDialog.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 31 | #include "i18n.h" |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 32 | #include "parameters.h" |
| 33 | |
| 34 | #ifdef WIN32 |
| 35 | #include "win32.h" |
| 36 | #endif |
| 37 | |
| 38 | #ifdef __APPLE__ |
| 39 | #include "cocoa.h" |
| 40 | #endif |
Pierre Ossman | 89f868a | 2011-04-11 11:59:31 +0000 | [diff] [blame] | 41 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 42 | using namespace rfb; |
| 43 | |
| 44 | extern void exit_vncviewer(); |
| 45 | |
| 46 | static rfb::LogWriter vlog("DesktopWindow"); |
| 47 | |
| 48 | DesktopWindow::DesktopWindow(int w, int h, const char *name, |
| 49 | const rfb::PixelFormat& serverPF, |
| 50 | CConn* cc_) |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 51 | : Fl_Window(w, h) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 52 | { |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 53 | // Allow resize |
Pierre Ossman | 343e2e0 | 2011-04-15 13:00:12 +0000 | [diff] [blame] | 54 | size_range(100, 100, w, h); |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 55 | |
| 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 Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 62 | viewport = new Viewport(w, h, serverPF, cc_); |
| 63 | |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 64 | scroll->end(); |
| 65 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 66 | callback(handleClose, this); |
| 67 | |
| 68 | setName(name); |
| 69 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 70 | OptionsDialog::addCallback(handleOptions, this); |
| 71 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 72 | show(); |
Pierre Ossman | 3f6c4d0 | 2011-04-15 14:09:09 +0000 | [diff] [blame] | 73 | |
| 74 | // The window manager might give us an initial window size that is different |
| 75 | // than the one we requested, and in those cases we need to manually adjust |
| 76 | // the scroll widget for things to behave sanely. |
| 77 | if ((w != this->w()) || (h != this->h())) |
| 78 | scroll->size(this->w(), this->h()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | DesktopWindow::~DesktopWindow() |
| 83 | { |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 84 | // Unregister all timeouts in case they get a change tro trigger |
| 85 | // again later when this object is already gone. |
| 86 | Fl::remove_timeout(handleGrab, this); |
| 87 | |
| 88 | OptionsDialog::removeCallback(handleOptions); |
| 89 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 90 | // FLTK automatically deletes all child widgets, so we shouldn't touch |
| 91 | // them ourselves here |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
| 95 | void DesktopWindow::setServerPF(const rfb::PixelFormat& pf) |
| 96 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 97 | viewport->setServerPF(pf); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
| 101 | const rfb::PixelFormat &DesktopWindow::getPreferredPF() |
| 102 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 103 | return viewport->getPreferredPF(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | // Cursor stuff |
| 108 | |
| 109 | void DesktopWindow::setCursor(int width, int height, const Point& hotspot, |
| 110 | void* data, void* mask) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void 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 Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 125 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 126 | void DesktopWindow::setColourMapEntries(int firstColour, int nColours, |
| 127 | rdr::U16* rgbs) |
| 128 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 129 | viewport->setColourMapEntries(firstColour, nColours, rgbs); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | // Copy the areas of the framebuffer that have been changed (damaged) |
| 134 | // to the displayed window. |
| 135 | |
| 136 | void DesktopWindow::updateWindow() |
| 137 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 138 | viewport->updateWindow(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 142 | void DesktopWindow::resize(int x, int y, int w, int h) |
| 143 | { |
| 144 | // Deal with some scrolling corner cases: |
| 145 | // |
| 146 | // a) If the window is larger then the viewport, center the viewport. |
| 147 | // b) If the window is smaller than the viewport, make sure there is |
| 148 | // no wasted space on the sides. |
| 149 | // |
| 150 | // FIXME: Doesn't compensate for scroll widget size properly. |
| 151 | if (w > viewport->w()) |
| 152 | viewport->position((w - viewport->w()) / 2, viewport->y()); |
| 153 | else { |
| 154 | if (viewport->x() > 0) |
| 155 | viewport->position(0, viewport->y()); |
| 156 | else if (w > (viewport->x() + viewport->w())) |
| 157 | viewport->position(w - viewport->w(), viewport->y()); |
| 158 | } |
| 159 | |
| 160 | // Same thing for y axis |
| 161 | if (h > viewport->h()) |
| 162 | viewport->position(viewport->x(), (h - viewport->h()) / 2); |
| 163 | else { |
| 164 | if (viewport->y() > 0) |
| 165 | viewport->position(viewport->x(), 0); |
| 166 | else if (h > (viewport->y() + viewport->h())) |
| 167 | viewport->position(viewport->x(), h - viewport->h()); |
| 168 | } |
| 169 | |
| 170 | Fl_Window::resize(x, y, w, h); |
| 171 | } |
| 172 | |
| 173 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 174 | int DesktopWindow::handle(int event) |
| 175 | { |
| 176 | switch (event) { |
| 177 | #ifdef HAVE_FLTK_FULLSCREEN |
| 178 | case FL_FOCUS: |
| 179 | // FIXME: We reassert the keyboard grabbing on focus/unfocus as FLTK |
| 180 | // releases the grab when someone calls Fl::grab(0) |
| 181 | case FL_FULLSCREEN: |
Pierre Ossman | 1870ab1 | 2011-05-26 14:53:49 +0000 | [diff] [blame^] | 182 | fullScreen.setParam(fullscreen_active()); |
| 183 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 184 | if (!fullscreenSystemKeys) |
| 185 | break; |
| 186 | |
| 187 | if (fullscreen_active()) |
| 188 | grabKeyboard(); |
| 189 | else |
| 190 | ungrabKeyboard(); |
| 191 | |
| 192 | break; |
| 193 | case FL_UNFOCUS: |
| 194 | // FIXME: We need to relinquish control when the entire window loses |
| 195 | // focus as it seems to interfere with the WM:s ability to handle |
| 196 | // interactions with popups' window decorations. |
| 197 | ungrabKeyboard(); |
| 198 | break; |
| 199 | #endif |
| 200 | case FL_SHORTCUT: |
| 201 | // Sometimes the focus gets out of whack and we fall through to the |
| 202 | // shortcut dispatching. Try to make things sane again... |
| 203 | if (Fl::focus() == NULL) { |
| 204 | take_focus(); |
| 205 | Fl::handle(FL_KEYDOWN, this); |
| 206 | } |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | return Fl_Window::handle(event); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | void DesktopWindow::grabKeyboard() |
| 215 | { |
| 216 | // Grabbing the keyboard is fairly safe as FLTK reroutes events to the |
| 217 | // correct widget regardless of which low level window got the system |
| 218 | // event. |
| 219 | |
| 220 | // FIXME: Push this stuff into FLTK. |
| 221 | |
| 222 | #if defined(WIN32) |
| 223 | int ret; |
| 224 | |
| 225 | ret = win32_enable_lowlevel_keyboard(fl_xid(this)); |
| 226 | if (ret != 0) |
| 227 | vlog.error(_("Failure grabbing keyboard")); |
| 228 | #elif defined(__APPLE__) |
| 229 | int ret; |
| 230 | |
| 231 | ret = cocoa_capture_display(this); |
| 232 | if (ret != 0) |
| 233 | vlog.error(_("Failure grabbing keyboard")); |
| 234 | #else |
| 235 | int ret; |
| 236 | |
| 237 | ret = XGrabKeyboard(fl_display, fl_xid(this), True, |
| 238 | GrabModeAsync, GrabModeAsync, fl_event_time); |
| 239 | if (ret) { |
| 240 | if (ret == AlreadyGrabbed) { |
| 241 | // It seems like we can race with the WM in some cases. |
| 242 | // Try again in a bit. |
| 243 | if (!Fl::has_timeout(handleGrab, this)) |
| 244 | Fl::add_timeout(0.500, handleGrab, this); |
| 245 | } else { |
| 246 | vlog.error(_("Failure grabbing keyboard")); |
| 247 | } |
| 248 | } |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | |
| 253 | void DesktopWindow::ungrabKeyboard() |
| 254 | { |
| 255 | Fl::remove_timeout(handleGrab, this); |
| 256 | |
| 257 | #if defined(WIN32) |
| 258 | win32_disable_lowlevel_keyboard(fl_xid(this)); |
| 259 | #elif defined(__APPLE__) |
| 260 | cocoa_release_display(this); |
| 261 | #else |
| 262 | // FLTK has a grab so lets not mess with it |
| 263 | if (Fl::grab()) |
| 264 | return; |
| 265 | |
| 266 | XUngrabKeyboard(fl_display, fl_event_time); |
| 267 | #endif |
| 268 | } |
| 269 | |
| 270 | |
| 271 | void DesktopWindow::handleGrab(void *data) |
| 272 | { |
| 273 | DesktopWindow *self = (DesktopWindow*)data; |
| 274 | |
| 275 | assert(self); |
| 276 | |
| 277 | #ifdef HAVE_FLTK_FULLSCREEN |
| 278 | if (!fullscreenSystemKeys) |
| 279 | return; |
| 280 | if (!self->fullscreen_active()) |
| 281 | return; |
| 282 | |
| 283 | self->grabKeyboard(); |
| 284 | #endif |
| 285 | } |
| 286 | |
| 287 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 288 | void DesktopWindow::handleClose(Fl_Widget *wnd, void *data) |
| 289 | { |
| 290 | exit_vncviewer(); |
| 291 | } |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 292 | |
| 293 | |
| 294 | void DesktopWindow::handleOptions(void *data) |
| 295 | { |
| 296 | DesktopWindow *self = (DesktopWindow*)data; |
| 297 | |
| 298 | #ifdef HAVE_FLTK_FULLSCREEN |
| 299 | if (self->fullscreen_active() && fullscreenSystemKeys) |
| 300 | self->grabKeyboard(); |
| 301 | else |
| 302 | self->ungrabKeyboard(); |
| 303 | #endif |
| 304 | } |