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 | 63ca58e | 2011-05-26 14:59:32 +0000 | [diff] [blame] | 72 | #ifdef HAVE_FLTK_FULLSCREEN |
| 73 | if (fullScreen) |
| 74 | fullscreen(); |
| 75 | #endif |
| 76 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 77 | show(); |
Pierre Ossman | 3f6c4d0 | 2011-04-15 14:09:09 +0000 | [diff] [blame] | 78 | |
| 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 Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
| 87 | DesktopWindow::~DesktopWindow() |
| 88 | { |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 89 | // 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 Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 95 | // FLTK automatically deletes all child widgets, so we shouldn't touch |
| 96 | // them ourselves here |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
| 100 | void DesktopWindow::setServerPF(const rfb::PixelFormat& pf) |
| 101 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 102 | viewport->setServerPF(pf); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | const rfb::PixelFormat &DesktopWindow::getPreferredPF() |
| 107 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 108 | return viewport->getPreferredPF(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 112 | void DesktopWindow::setName(const char *name) |
| 113 | { |
| 114 | CharArray windowNameStr; |
| 115 | windowNameStr.replaceBuf(new char[256]); |
| 116 | |
| 117 | snprintf(windowNameStr.buf, 256, _("TigerVNC: %.240s"), name); |
| 118 | |
| 119 | copy_label(windowNameStr.buf); |
| 120 | } |
| 121 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 122 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 123 | void DesktopWindow::setColourMapEntries(int firstColour, int nColours, |
| 124 | rdr::U16* rgbs) |
| 125 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 126 | viewport->setColourMapEntries(firstColour, nColours, rgbs); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
| 130 | // Copy the areas of the framebuffer that have been changed (damaged) |
| 131 | // to the displayed window. |
| 132 | |
| 133 | void DesktopWindow::updateWindow() |
| 134 | { |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 135 | viewport->updateWindow(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 139 | void DesktopWindow::resizeFramebuffer(int new_w, int new_h) |
| 140 | { |
| 141 | if ((new_w == viewport->w()) && (new_h == viewport->h())) |
| 142 | return; |
| 143 | |
| 144 | // Turn off size limitations for a bit while we juggle things around |
| 145 | size_range(100, 100, 0, 0); |
| 146 | |
| 147 | // If we're letting the viewport match the window perfectly, then |
| 148 | // keep things that way for the new size, otherwise just keep things |
| 149 | // like they are. |
| 150 | if ((w() == viewport->w()) && (h() == viewport->h())) |
| 151 | size(new_w, new_h); |
| 152 | else { |
| 153 | #ifdef HAVE_FLTK_FULLSCREEN |
| 154 | if (!fullscreen_active()) { |
| 155 | #endif |
| 156 | // Make sure the window isn't too big |
| 157 | if ((w() > new_w) || (h() > new_h)) |
| 158 | size(__rfbmin(w(), new_w), __rfbmin(h(), new_h)); |
| 159 | #ifdef HAVE_FLTK_FULLSCREEN |
| 160 | } |
| 161 | #endif |
| 162 | } |
| 163 | |
| 164 | viewport->size(new_w, new_h); |
| 165 | |
| 166 | // We might not resize the main window, so we need to manually call this |
| 167 | // to make sure the viewport is centered. |
| 168 | repositionViewport(); |
| 169 | |
| 170 | // Update allowed resize range |
| 171 | size_range(100, 100, new_w, new_h); |
| 172 | |
| 173 | // repositionViewport() makes sure the scroll widget notices any changes |
| 174 | // in position, but it might be just the size that changes so we also |
| 175 | // need a poke here as well. |
| 176 | redraw(); |
| 177 | } |
| 178 | |
| 179 | |
Pierre Ossman | 835b4ef | 2011-06-08 17:02:36 +0000 | [diff] [blame] | 180 | void DesktopWindow::setCursor(int width, int height, const Point& hotspot, |
| 181 | void* data, void* mask) |
| 182 | { |
| 183 | viewport->setCursor(width, height, hotspot, data, mask); |
| 184 | } |
| 185 | |
| 186 | |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 187 | void DesktopWindow::resize(int x, int y, int w, int h) |
| 188 | { |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 189 | Fl_Window::resize(x, y, w, h); |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 190 | |
| 191 | // Deal with some scrolling corner cases |
| 192 | repositionViewport(); |
Pierre Ossman | 4ae229f | 2011-04-15 12:58:31 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 196 | int DesktopWindow::handle(int event) |
| 197 | { |
| 198 | switch (event) { |
| 199 | #ifdef HAVE_FLTK_FULLSCREEN |
| 200 | case FL_FOCUS: |
| 201 | // FIXME: We reassert the keyboard grabbing on focus/unfocus as FLTK |
| 202 | // releases the grab when someone calls Fl::grab(0) |
| 203 | case FL_FULLSCREEN: |
Pierre Ossman | 105738c | 2011-05-26 14:57:25 +0000 | [diff] [blame] | 204 | if (event == FL_FULLSCREEN) |
| 205 | fullScreen.setParam(fullscreen_active()); |
Pierre Ossman | 1870ab1 | 2011-05-26 14:53:49 +0000 | [diff] [blame] | 206 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 207 | if (!fullscreenSystemKeys) |
| 208 | break; |
| 209 | |
| 210 | if (fullscreen_active()) |
| 211 | grabKeyboard(); |
| 212 | else |
| 213 | ungrabKeyboard(); |
| 214 | |
| 215 | break; |
| 216 | case FL_UNFOCUS: |
| 217 | // FIXME: We need to relinquish control when the entire window loses |
| 218 | // focus as it seems to interfere with the WM:s ability to handle |
| 219 | // interactions with popups' window decorations. |
| 220 | ungrabKeyboard(); |
| 221 | break; |
| 222 | #endif |
| 223 | case FL_SHORTCUT: |
| 224 | // Sometimes the focus gets out of whack and we fall through to the |
| 225 | // shortcut dispatching. Try to make things sane again... |
| 226 | if (Fl::focus() == NULL) { |
| 227 | take_focus(); |
| 228 | Fl::handle(FL_KEYDOWN, this); |
| 229 | } |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | return Fl_Window::handle(event); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | void DesktopWindow::grabKeyboard() |
| 238 | { |
| 239 | // Grabbing the keyboard is fairly safe as FLTK reroutes events to the |
| 240 | // correct widget regardless of which low level window got the system |
| 241 | // event. |
| 242 | |
| 243 | // FIXME: Push this stuff into FLTK. |
| 244 | |
| 245 | #if defined(WIN32) |
| 246 | int ret; |
| 247 | |
| 248 | ret = win32_enable_lowlevel_keyboard(fl_xid(this)); |
| 249 | if (ret != 0) |
| 250 | vlog.error(_("Failure grabbing keyboard")); |
| 251 | #elif defined(__APPLE__) |
| 252 | int ret; |
| 253 | |
| 254 | ret = cocoa_capture_display(this); |
| 255 | if (ret != 0) |
| 256 | vlog.error(_("Failure grabbing keyboard")); |
| 257 | #else |
| 258 | int ret; |
| 259 | |
| 260 | ret = XGrabKeyboard(fl_display, fl_xid(this), True, |
| 261 | GrabModeAsync, GrabModeAsync, fl_event_time); |
| 262 | if (ret) { |
| 263 | if (ret == AlreadyGrabbed) { |
| 264 | // It seems like we can race with the WM in some cases. |
| 265 | // Try again in a bit. |
| 266 | if (!Fl::has_timeout(handleGrab, this)) |
| 267 | Fl::add_timeout(0.500, handleGrab, this); |
| 268 | } else { |
| 269 | vlog.error(_("Failure grabbing keyboard")); |
| 270 | } |
| 271 | } |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | |
| 276 | void DesktopWindow::ungrabKeyboard() |
| 277 | { |
| 278 | Fl::remove_timeout(handleGrab, this); |
| 279 | |
| 280 | #if defined(WIN32) |
| 281 | win32_disable_lowlevel_keyboard(fl_xid(this)); |
| 282 | #elif defined(__APPLE__) |
| 283 | cocoa_release_display(this); |
| 284 | #else |
| 285 | // FLTK has a grab so lets not mess with it |
| 286 | if (Fl::grab()) |
| 287 | return; |
| 288 | |
| 289 | XUngrabKeyboard(fl_display, fl_event_time); |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void DesktopWindow::handleGrab(void *data) |
| 295 | { |
| 296 | DesktopWindow *self = (DesktopWindow*)data; |
| 297 | |
| 298 | assert(self); |
| 299 | |
| 300 | #ifdef HAVE_FLTK_FULLSCREEN |
| 301 | if (!fullscreenSystemKeys) |
| 302 | return; |
| 303 | if (!self->fullscreen_active()) |
| 304 | return; |
| 305 | |
| 306 | self->grabKeyboard(); |
| 307 | #endif |
| 308 | } |
| 309 | |
| 310 | |
Pierre Ossman | 6455d85 | 2011-06-01 09:33:00 +0000 | [diff] [blame] | 311 | void DesktopWindow::repositionViewport() |
| 312 | { |
| 313 | int new_x, new_y; |
| 314 | |
| 315 | // Deal with some scrolling corner cases: |
| 316 | // |
| 317 | // a) If the window is larger then the viewport, center the viewport. |
| 318 | // b) If the window is smaller than the viewport, make sure there is |
| 319 | // no wasted space on the sides. |
| 320 | // |
| 321 | // FIXME: Doesn't compensate for scroll widget size properly. |
| 322 | |
| 323 | new_x = viewport->x(); |
| 324 | new_y = viewport->y(); |
| 325 | |
| 326 | if (w() > viewport->w()) |
| 327 | new_x = (w() - viewport->w()) / 2; |
| 328 | else { |
| 329 | if (viewport->x() > 0) |
| 330 | new_x = 0; |
| 331 | else if (w() > (viewport->x() + viewport->w())) |
| 332 | new_x = w() - viewport->w(); |
| 333 | } |
| 334 | |
| 335 | // Same thing for y axis |
| 336 | if (h() > viewport->h()) |
| 337 | new_y = (h() - viewport->h()) / 2; |
| 338 | else { |
| 339 | if (viewport->y() > 0) |
| 340 | new_y = 0; |
| 341 | else if (h() > (viewport->y() + viewport->h())) |
| 342 | new_y = h() - viewport->h(); |
| 343 | } |
| 344 | |
| 345 | if ((new_x != viewport->x()) || (new_y != viewport->y())) { |
| 346 | viewport->position(new_x, new_y); |
| 347 | |
| 348 | // The scroll widget does not notice when you move around child widgets, |
| 349 | // so redraw everything to make sure things update. |
| 350 | redraw(); |
| 351 | } |
| 352 | } |
| 353 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 354 | void DesktopWindow::handleClose(Fl_Widget *wnd, void *data) |
| 355 | { |
| 356 | exit_vncviewer(); |
| 357 | } |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 358 | |
| 359 | |
| 360 | void DesktopWindow::handleOptions(void *data) |
| 361 | { |
| 362 | DesktopWindow *self = (DesktopWindow*)data; |
| 363 | |
| 364 | #ifdef HAVE_FLTK_FULLSCREEN |
| 365 | if (self->fullscreen_active() && fullscreenSystemKeys) |
| 366 | self->grabKeyboard(); |
| 367 | else |
| 368 | self->ungrabKeyboard(); |
Pierre Ossman | 9191164 | 2011-05-26 14:57:51 +0000 | [diff] [blame] | 369 | |
| 370 | if (fullScreen && !self->fullscreen_active()) |
| 371 | self->fullscreen(); |
| 372 | else if (!fullScreen && self->fullscreen_active()) |
| 373 | self->fullscreen_off(); |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 374 | #endif |
| 375 | } |