Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +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 | |
| 24 | #include <FL/fl_draw.H> |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 25 | #include <FL/fl_ask.H> |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 26 | |
| 27 | #include <rfb/CMsgWriter.h> |
| 28 | #include <rfb/LogWriter.h> |
| 29 | |
| 30 | // FLTK can pull in the X11 headers on some systems |
| 31 | #ifndef XK_VoidSymbol |
| 32 | #define XK_MISCELLANY |
| 33 | #define XK_XKB_KEYS |
| 34 | #include <rfb/keysymdef.h> |
| 35 | #endif |
| 36 | |
Pierre Ossman | cb0cffe | 2011-05-20 14:55:10 +0000 | [diff] [blame] | 37 | #ifndef XF86XK_ModeLock |
| 38 | #include <rfb/XF86keysym.h> |
| 39 | #endif |
| 40 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 41 | #include "Viewport.h" |
| 42 | #include "CConn.h" |
Pierre Ossman | d463b57 | 2011-05-16 12:04:43 +0000 | [diff] [blame] | 43 | #include "OptionsDialog.h" |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 44 | #include "i18n.h" |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 45 | #include "fltk_layout.h" |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 46 | #include "parameters.h" |
| 47 | #include "keysym2ucs.h" |
| 48 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 49 | using namespace rfb; |
| 50 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 51 | extern void exit_vncviewer(); |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 52 | extern void about_vncviewer(); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 53 | |
| 54 | static rfb::LogWriter vlog("Viewport"); |
| 55 | |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 56 | // Menu constants |
| 57 | |
Pierre Ossman | 4c613d3 | 2011-05-26 14:14:06 +0000 | [diff] [blame] | 58 | enum { ID_EXIT, ID_FULLSCREEN, ID_CTRL, ID_ALT, ID_MENUKEY, ID_CTRLALTDEL, |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 59 | ID_REFRESH, ID_OPTIONS, ID_INFO, ID_ABOUT, ID_DISMISS }; |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 60 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 61 | Viewport::Viewport(int w, int h, const rfb::PixelFormat& serverPF, CConn* cc_) |
| 62 | : Fl_Widget(0, 0, w, h), cc(cc_), frameBuffer(NULL), pixelTrans(NULL), |
| 63 | lastPointerPos(0, 0), lastButtonMask(0) |
| 64 | { |
Pierre Ossman | f8c5ef6 | 2011-05-13 12:47:54 +0000 | [diff] [blame] | 65 | // FLTK STR #2599 must be fixed for proper dead keys support |
| 66 | #ifdef HAVE_FLTK_DEAD_KEYS |
| 67 | set_simple_keyboard(); |
| 68 | #endif |
| 69 | |
Pierre Ossman | 4a6be4a | 2011-05-19 14:49:18 +0000 | [diff] [blame] | 70 | // FLTK STR #2636 gives us the ability to monitor clipboard changes |
| 71 | #ifdef HAVE_FLTK_CLIPBOARD |
| 72 | Fl::add_clipboard_notify(handleClipboardChange, this); |
| 73 | #endif |
| 74 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 75 | frameBuffer = new ManagedPixelBuffer(getPreferredPF(), w, h); |
| 76 | assert(frameBuffer); |
| 77 | |
| 78 | setServerPF(serverPF); |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 79 | |
| 80 | contextMenu = new Fl_Menu_Button(0, 0, 0, 0); |
Pierre Ossman | ad9d1ae | 2011-05-26 14:16:02 +0000 | [diff] [blame] | 81 | // Setting box type to FL_NO_BOX prevents it from trying to draw the |
| 82 | // button component (which we don't want) |
| 83 | contextMenu->box(FL_NO_BOX); |
| 84 | |
Pierre Ossman | b043ad1 | 2011-06-01 09:26:57 +0000 | [diff] [blame] | 85 | // The (invisible) button associated with this widget can mess with |
| 86 | // things like Fl_Scroll so we need to get rid of any parents. |
| 87 | // Unfortunately that's not possible because of STR #2654, but |
| 88 | // reparenting to the current window works for most cases. |
| 89 | window()->add(contextMenu); |
| 90 | |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 91 | initContextMenu(); |
Pierre Ossman | 4e7271e | 2011-05-24 12:47:12 +0000 | [diff] [blame] | 92 | |
| 93 | setMenuKey(); |
| 94 | |
| 95 | OptionsDialog::addCallback(handleOptions, this); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | Viewport::~Viewport() |
| 100 | { |
| 101 | // Unregister all timeouts in case they get a change tro trigger |
| 102 | // again later when this object is already gone. |
| 103 | Fl::remove_timeout(handleUpdateTimeout, this); |
| 104 | Fl::remove_timeout(handleColourMap, this); |
| 105 | Fl::remove_timeout(handlePointerTimeout, this); |
| 106 | |
Pierre Ossman | 4a6be4a | 2011-05-19 14:49:18 +0000 | [diff] [blame] | 107 | #ifdef HAVE_FLTK_CLIPBOARD |
| 108 | Fl::remove_clipboard_notify(handleClipboardChange); |
| 109 | #endif |
| 110 | |
Pierre Ossman | 4e7271e | 2011-05-24 12:47:12 +0000 | [diff] [blame] | 111 | OptionsDialog::removeCallback(handleOptions); |
| 112 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 113 | delete frameBuffer; |
| 114 | |
| 115 | if (pixelTrans) |
| 116 | delete pixelTrans; |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 117 | |
| 118 | // FLTK automatically deletes all child widgets, so we shouldn't touch |
| 119 | // them ourselves here |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | void Viewport::setServerPF(const rfb::PixelFormat& pf) |
| 124 | { |
| 125 | if (pixelTrans) |
| 126 | delete pixelTrans; |
| 127 | pixelTrans = NULL; |
| 128 | |
| 129 | if (pf.equal(getPreferredPF())) |
| 130 | return; |
| 131 | |
| 132 | pixelTrans = new PixelTransformer(); |
| 133 | pixelTrans->init(pf, &colourMap, getPreferredPF()); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | const rfb::PixelFormat &Viewport::getPreferredPF() |
| 138 | { |
| 139 | static PixelFormat prefPF(32, 24, false, true, 255, 255, 255, 0, 8, 16); |
| 140 | |
| 141 | return prefPF; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | // setColourMapEntries() changes some of the entries in the colourmap. |
| 146 | // Unfortunately these messages are often sent one at a time, so we delay the |
| 147 | // settings taking effect by 100ms. This is because recalculating the internal |
| 148 | // translation table can be expensive. |
| 149 | void Viewport::setColourMapEntries(int firstColour, int nColours, |
| 150 | rdr::U16* rgbs) |
| 151 | { |
| 152 | for (int i = 0; i < nColours; i++) |
| 153 | colourMap.set(firstColour+i, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]); |
| 154 | |
| 155 | if (!Fl::has_timeout(handleColourMap, this)) |
| 156 | Fl::add_timeout(0.100, handleColourMap, this); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | // Copy the areas of the framebuffer that have been changed (damaged) |
| 161 | // to the displayed window. |
| 162 | |
| 163 | void Viewport::updateWindow() |
| 164 | { |
| 165 | Rect r; |
| 166 | |
| 167 | Fl::remove_timeout(handleUpdateTimeout, this); |
| 168 | |
| 169 | r = damage.get_bounding_rect(); |
Pierre Ossman | a6e2077 | 2011-04-15 11:10:52 +0000 | [diff] [blame] | 170 | Fl_Widget::damage(FL_DAMAGE_USER1, r.tl.x + x(), r.tl.y + y(), r.width(), r.height()); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 171 | |
| 172 | damage.clear(); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | void Viewport::draw() |
| 177 | { |
| 178 | int X, Y, W, H; |
| 179 | |
| 180 | int pixel_bytes, stride_bytes; |
| 181 | const uchar *buf_start; |
| 182 | |
| 183 | // Check what actually needs updating |
Pierre Ossman | a6e2077 | 2011-04-15 11:10:52 +0000 | [diff] [blame] | 184 | fl_clip_box(x(), y(), w(), h(), X, Y, W, H); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 185 | if ((W == 0) || (H == 0)) |
| 186 | return; |
| 187 | |
| 188 | pixel_bytes = frameBuffer->getPF().bpp/8; |
| 189 | stride_bytes = pixel_bytes * frameBuffer->getStride(); |
| 190 | buf_start = frameBuffer->data + |
Pierre Ossman | a6e2077 | 2011-04-15 11:10:52 +0000 | [diff] [blame] | 191 | pixel_bytes * (X - x()) + |
| 192 | stride_bytes * (Y - y()); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 193 | |
| 194 | // FIXME: Check how efficient this thing really is |
| 195 | fl_draw_image(buf_start, X, Y, W, H, pixel_bytes, stride_bytes); |
| 196 | } |
| 197 | |
| 198 | |
Pierre Ossman | e00f0aa | 2011-06-01 09:31:53 +0000 | [diff] [blame] | 199 | void Viewport::resize(int x, int y, int w, int h) |
| 200 | { |
| 201 | rfb::ManagedPixelBuffer* newBuffer; |
| 202 | rfb::Rect rect; |
| 203 | |
| 204 | // FIXME: Resize should probably be a feature of ManagedPixelBuffer |
| 205 | |
| 206 | if ((w == frameBuffer->width()) && (h == frameBuffer->height())) |
| 207 | goto end; |
| 208 | |
| 209 | newBuffer = new ManagedPixelBuffer(frameBuffer->getPF(), w, h); |
| 210 | assert(newBuffer); |
| 211 | |
| 212 | rect.setXYWH(0, 0, |
| 213 | __rfbmin(newBuffer->width(), frameBuffer->width()), |
| 214 | __rfbmin(newBuffer->height(), frameBuffer->height())); |
| 215 | newBuffer->imageRect(rect, frameBuffer->data, frameBuffer->getStride()); |
| 216 | |
| 217 | // Black out any new areas |
| 218 | |
| 219 | if (newBuffer->width() > frameBuffer->width()) { |
| 220 | rect.setXYWH(frameBuffer->width(), 0, |
| 221 | newBuffer->width() - frameBuffer->width(), |
| 222 | newBuffer->height()); |
| 223 | newBuffer->fillRect(rect, 0); |
| 224 | } |
| 225 | |
| 226 | if (newBuffer->height() > frameBuffer->height()) { |
| 227 | rect.setXYWH(0, frameBuffer->height(), |
| 228 | newBuffer->width(), |
| 229 | newBuffer->height() - frameBuffer->height()); |
| 230 | newBuffer->fillRect(rect, 0); |
| 231 | } |
| 232 | |
| 233 | delete frameBuffer; |
| 234 | frameBuffer = newBuffer; |
| 235 | |
| 236 | end: |
| 237 | Fl_Widget::resize(x, y, w, h); |
| 238 | } |
| 239 | |
| 240 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 241 | int Viewport::handle(int event) |
| 242 | { |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 243 | char *buffer; |
Pierre Ossman | 4a6be4a | 2011-05-19 14:49:18 +0000 | [diff] [blame] | 244 | int ret; |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 245 | int buttonMask, wheelMask; |
| 246 | DownMap::const_iterator iter; |
| 247 | |
| 248 | switch (event) { |
Pierre Ossman | 4a6be4a | 2011-05-19 14:49:18 +0000 | [diff] [blame] | 249 | case FL_PASTE: |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 250 | buffer = new char[Fl::event_length() + 1]; |
| 251 | |
Pierre Ossman | 4a6be4a | 2011-05-19 14:49:18 +0000 | [diff] [blame] | 252 | // This is documented as to ASCII, but actually does to 8859-1 |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 253 | ret = fl_utf8toa(Fl::event_text(), Fl::event_length(), buffer, |
| 254 | Fl::event_length() + 1); |
| 255 | assert(ret < (Fl::event_length() + 1)); |
| 256 | |
Pierre Ossman | 4a6be4a | 2011-05-19 14:49:18 +0000 | [diff] [blame] | 257 | vlog.debug("Sending clipboard data: '%s'", buffer); |
| 258 | cc->writer()->clientCutText(buffer, ret); |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 259 | |
| 260 | delete [] buffer; |
| 261 | |
Pierre Ossman | 4a6be4a | 2011-05-19 14:49:18 +0000 | [diff] [blame] | 262 | return 1; |
Pierre Ossman | 689c458 | 2011-05-26 15:39:41 +0000 | [diff] [blame] | 263 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 264 | case FL_ENTER: |
| 265 | // Yes, we would like some pointer events please! |
| 266 | return 1; |
| 267 | case FL_PUSH: |
| 268 | case FL_RELEASE: |
| 269 | case FL_DRAG: |
| 270 | case FL_MOVE: |
| 271 | case FL_MOUSEWHEEL: |
| 272 | buttonMask = 0; |
| 273 | if (Fl::event_button1()) |
| 274 | buttonMask |= 1; |
| 275 | if (Fl::event_button2()) |
| 276 | buttonMask |= 2; |
| 277 | if (Fl::event_button3()) |
| 278 | buttonMask |= 4; |
| 279 | |
| 280 | if (event == FL_MOUSEWHEEL) { |
Pierre Ossman | df0ed9f | 2011-05-24 11:33:43 +0000 | [diff] [blame] | 281 | wheelMask = 0; |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 282 | if (Fl::event_dy() < 0) |
Pierre Ossman | df0ed9f | 2011-05-24 11:33:43 +0000 | [diff] [blame] | 283 | wheelMask |= 8; |
| 284 | if (Fl::event_dy() > 0) |
| 285 | wheelMask |= 16; |
| 286 | if (Fl::event_dx() < 0) |
| 287 | wheelMask |= 32; |
| 288 | if (Fl::event_dx() > 0) |
| 289 | wheelMask |= 64; |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 290 | |
| 291 | // A quick press of the wheel "button", followed by a immediate |
| 292 | // release below |
Pierre Ossman | 6a464be | 2011-04-15 12:57:31 +0000 | [diff] [blame] | 293 | handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 294 | buttonMask | wheelMask); |
| 295 | } |
| 296 | |
Pierre Ossman | 6a464be | 2011-04-15 12:57:31 +0000 | [diff] [blame] | 297 | handlePointerEvent(Point(Fl::event_x() - x(), Fl::event_y() - y()), buttonMask); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 298 | return 1; |
| 299 | |
| 300 | case FL_FOCUS: |
| 301 | // Yes, we would like some focus please! |
| 302 | return 1; |
| 303 | |
| 304 | case FL_UNFOCUS: |
| 305 | // Release all keys that were pressed as that generally makes most |
| 306 | // sense (e.g. Alt+Tab where we only see the Alt press) |
Pierre Ossman | 71f295a | 2011-05-19 14:55:12 +0000 | [diff] [blame] | 307 | for (iter = downKeySym.begin();iter != downKeySym.end();++iter) { |
| 308 | vlog.debug("Key released: 0x%04x => 0x%04x", iter->first, iter->second); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 309 | cc->writer()->keyEvent(iter->second, false); |
Pierre Ossman | 71f295a | 2011-05-19 14:55:12 +0000 | [diff] [blame] | 310 | } |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 311 | downKeySym.clear(); |
| 312 | return 1; |
| 313 | |
| 314 | case FL_KEYDOWN: |
Pierre Ossman | 4e7271e | 2011-05-24 12:47:12 +0000 | [diff] [blame] | 315 | if (menuKeyCode && (Fl::event_key() == menuKeyCode)) { |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 316 | popupContextMenu(); |
| 317 | return 1; |
| 318 | } |
| 319 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 320 | handleKeyEvent(Fl::event_key(), Fl::event_original_key(), |
| 321 | Fl::event_text(), true); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 322 | return 1; |
| 323 | |
| 324 | case FL_KEYUP: |
Pierre Ossman | 4e7271e | 2011-05-24 12:47:12 +0000 | [diff] [blame] | 325 | if (menuKeyCode && (Fl::event_key() == menuKeyCode)) |
| 326 | return 1; |
| 327 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 328 | handleKeyEvent(Fl::event_key(), Fl::event_original_key(), |
| 329 | Fl::event_text(), false); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | return Fl_Widget::handle(event); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | void Viewport::handleUpdateTimeout(void *data) |
| 338 | { |
| 339 | Viewport *self = (Viewport *)data; |
| 340 | |
| 341 | assert(self); |
| 342 | |
| 343 | self->updateWindow(); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | void Viewport::handleColourMap(void *data) |
| 348 | { |
| 349 | Viewport *self = (Viewport *)data; |
| 350 | |
| 351 | assert(self); |
| 352 | |
| 353 | if (self->pixelTrans != NULL) |
| 354 | self->pixelTrans->setColourMapEntries(0, 0); |
| 355 | |
| 356 | self->Fl_Widget::damage(FL_DAMAGE_ALL); |
| 357 | } |
| 358 | |
| 359 | |
Pierre Ossman | 4a6be4a | 2011-05-19 14:49:18 +0000 | [diff] [blame] | 360 | void Viewport::handleClipboardChange(int source, void *data) |
| 361 | { |
| 362 | Viewport *self = (Viewport *)data; |
| 363 | |
| 364 | assert(self); |
| 365 | |
| 366 | if (!sendPrimary && (source == 0)) |
| 367 | return; |
| 368 | |
| 369 | Fl::paste(*self, source); |
| 370 | } |
| 371 | |
| 372 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 373 | void Viewport::handlePointerEvent(const rfb::Point& pos, int buttonMask) |
| 374 | { |
| 375 | if (!viewOnly) { |
| 376 | if (pointerEventInterval == 0 || buttonMask != lastButtonMask) { |
| 377 | cc->writer()->pointerEvent(pos, buttonMask); |
| 378 | } else { |
| 379 | if (!Fl::has_timeout(handlePointerTimeout, this)) |
| 380 | Fl::add_timeout((double)pointerEventInterval/1000.0, |
| 381 | handlePointerTimeout, this); |
| 382 | } |
| 383 | lastPointerPos = pos; |
| 384 | lastButtonMask = buttonMask; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | |
| 389 | void Viewport::handlePointerTimeout(void *data) |
| 390 | { |
| 391 | Viewport *self = (Viewport *)data; |
| 392 | |
| 393 | assert(self); |
| 394 | |
| 395 | self->cc->writer()->pointerEvent(self->lastPointerPos, self->lastButtonMask); |
| 396 | } |
| 397 | |
| 398 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 399 | rdr::U32 Viewport::translateKeyEvent(int keyCode, int origKeyCode, const char *keyText) |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 400 | { |
| 401 | unsigned ucs; |
| 402 | |
| 403 | // First check for function keys |
| 404 | if ((keyCode > FL_F) && (keyCode <= FL_F_Last)) |
| 405 | return XK_F1 + (keyCode - FL_F - 1); |
| 406 | |
| 407 | // Numpad numbers |
| 408 | if ((keyCode >= (FL_KP + '0')) && (keyCode <= (FL_KP + '9'))) |
| 409 | return XK_KP_0 + (keyCode - (FL_KP + '0')); |
| 410 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 411 | // FLTK does some special remapping of numpad keys when numlock is off |
| 412 | if ((origKeyCode >= FL_KP) && (origKeyCode <= FL_KP_Last)) { |
| 413 | switch (keyCode) { |
| 414 | case FL_F+1: |
| 415 | return XK_KP_F1; |
| 416 | case FL_F+2: |
| 417 | return XK_KP_F2; |
| 418 | case FL_F+3: |
| 419 | return XK_KP_F3; |
| 420 | case FL_F+4: |
| 421 | return XK_KP_F4; |
| 422 | case FL_Home: |
| 423 | return XK_KP_Home; |
| 424 | case FL_Left: |
| 425 | return XK_KP_Left; |
| 426 | case FL_Up: |
| 427 | return XK_KP_Up; |
| 428 | case FL_Right: |
| 429 | return XK_KP_Right; |
| 430 | case FL_Down: |
| 431 | return XK_KP_Down; |
| 432 | case FL_Page_Up: |
| 433 | return XK_KP_Page_Up; |
| 434 | case FL_Page_Down: |
| 435 | return XK_KP_Page_Down; |
| 436 | case FL_End: |
| 437 | return XK_KP_End; |
| 438 | case FL_Insert: |
| 439 | return XK_KP_Insert; |
| 440 | case FL_Delete: |
| 441 | return XK_KP_Delete; |
| 442 | } |
| 443 | } |
| 444 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 445 | // Then other special keys |
| 446 | switch (keyCode) { |
| 447 | case FL_BackSpace: |
| 448 | return XK_BackSpace; |
| 449 | case FL_Tab: |
| 450 | return XK_Tab; |
| 451 | case FL_Enter: |
| 452 | return XK_Return; |
| 453 | case FL_Pause: |
| 454 | return XK_Pause; |
| 455 | case FL_Scroll_Lock: |
| 456 | return XK_Scroll_Lock; |
| 457 | case FL_Escape: |
| 458 | return XK_Escape; |
| 459 | case FL_Home: |
| 460 | return XK_Home; |
| 461 | case FL_Left: |
| 462 | return XK_Left; |
| 463 | case FL_Up: |
| 464 | return XK_Up; |
| 465 | case FL_Right: |
| 466 | return XK_Right; |
| 467 | case FL_Down: |
| 468 | return XK_Down; |
| 469 | case FL_Page_Up: |
| 470 | return XK_Page_Up; |
| 471 | case FL_Page_Down: |
| 472 | return XK_Page_Down; |
| 473 | case FL_End: |
| 474 | return XK_End; |
| 475 | case FL_Print: |
| 476 | return XK_Print; |
| 477 | case FL_Insert: |
| 478 | return XK_Insert; |
| 479 | case FL_Menu: |
| 480 | return XK_Menu; |
| 481 | case FL_Help: |
| 482 | return XK_Help; |
| 483 | case FL_Num_Lock: |
| 484 | return XK_Num_Lock; |
| 485 | case FL_Shift_L: |
| 486 | return XK_Shift_L; |
| 487 | case FL_Shift_R: |
| 488 | return XK_Shift_R; |
| 489 | case FL_Control_L: |
| 490 | return XK_Control_L; |
| 491 | case FL_Control_R: |
| 492 | return XK_Control_R; |
| 493 | case FL_Caps_Lock: |
| 494 | return XK_Caps_Lock; |
| 495 | case FL_Meta_L: |
| 496 | return XK_Super_L; |
| 497 | case FL_Meta_R: |
| 498 | return XK_Super_R; |
| 499 | case FL_Alt_L: |
| 500 | return XK_Alt_L; |
| 501 | case FL_Alt_R: |
| 502 | return XK_Alt_R; |
| 503 | case FL_Delete: |
| 504 | return XK_Delete; |
| 505 | case FL_KP_Enter: |
| 506 | return XK_KP_Enter; |
| 507 | case FL_KP + '=': |
| 508 | return XK_KP_Equal; |
| 509 | case FL_KP + '*': |
| 510 | return XK_KP_Multiply; |
| 511 | case FL_KP + '+': |
| 512 | return XK_KP_Add; |
| 513 | case FL_KP + ',': |
| 514 | return XK_KP_Separator; |
| 515 | case FL_KP + '-': |
| 516 | return XK_KP_Subtract; |
| 517 | case FL_KP + '.': |
| 518 | return XK_KP_Decimal; |
| 519 | case FL_KP + '/': |
| 520 | return XK_KP_Divide; |
Pierre Ossman | cb0cffe | 2011-05-20 14:55:10 +0000 | [diff] [blame] | 521 | #ifdef HAVE_FLTK_MEDIAKEYS |
| 522 | case FL_Volume_Down: |
| 523 | return XF86XK_AudioLowerVolume; |
| 524 | case FL_Volume_Mute: |
| 525 | return XF86XK_AudioMute; |
| 526 | case FL_Volume_Up: |
| 527 | return XF86XK_AudioRaiseVolume; |
| 528 | case FL_Media_Play: |
| 529 | return XF86XK_AudioPlay; |
| 530 | case FL_Media_Stop: |
| 531 | return XF86XK_AudioStop; |
| 532 | case FL_Media_Prev: |
| 533 | return XF86XK_AudioPrev; |
| 534 | case FL_Media_Next: |
| 535 | return XF86XK_AudioNext; |
| 536 | case FL_Home_Page: |
| 537 | return XF86XK_HomePage; |
| 538 | case FL_Mail: |
| 539 | return XF86XK_Mail; |
| 540 | case FL_Search: |
| 541 | return XF86XK_Search; |
| 542 | case FL_Back: |
| 543 | return XF86XK_Back; |
| 544 | case FL_Forward: |
| 545 | return XF86XK_Forward; |
| 546 | case FL_Stop: |
| 547 | return XF86XK_Stop; |
| 548 | case FL_Refresh: |
| 549 | return XF86XK_Refresh; |
| 550 | case FL_Sleep: |
| 551 | return XF86XK_Sleep; |
| 552 | case FL_Favorites: |
| 553 | return XF86XK_Favorites; |
| 554 | #endif |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 555 | case XK_ISO_Level3_Shift: |
| 556 | // FLTK tends to let this one leak through on X11... |
| 557 | return XK_ISO_Level3_Shift; |
Pierre Ossman | a75f8f8 | 2011-04-29 11:12:02 +0000 | [diff] [blame] | 558 | case XK_Multi_key: |
| 559 | // Same for this... |
| 560 | return XK_Multi_key; |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 563 | // Unknown special key? |
| 564 | if (keyText[0] == '\0') { |
| 565 | vlog.error(_("Unknown FLTK key code %d (0x%04x)"), keyCode, keyCode); |
| 566 | return XK_VoidSymbol; |
| 567 | } |
| 568 | |
| 569 | // Look up the symbol the key produces and translate that from Unicode |
| 570 | // to a X11 keysym. |
| 571 | if (fl_utf_nb_char((const unsigned char*)keyText, strlen(keyText)) != 1) { |
| 572 | vlog.error(_("Multiple characters given for key code %d (0x%04x): '%s'"), |
| 573 | keyCode, keyCode, keyText); |
| 574 | return XK_VoidSymbol; |
| 575 | } |
| 576 | |
| 577 | ucs = fl_utf8decode(keyText, NULL, NULL); |
| 578 | return ucs2keysym(ucs); |
| 579 | } |
| 580 | |
| 581 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 582 | void Viewport::handleKeyEvent(int keyCode, int origKeyCode, const char *keyText, bool down) |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 583 | { |
| 584 | rdr::U32 keySym; |
| 585 | |
| 586 | if (viewOnly) |
| 587 | return; |
| 588 | |
| 589 | // Because of the way keyboards work, we cannot expect to have the same |
| 590 | // symbol on release as when pressed. This breaks the VNC protocol however, |
| 591 | // so we need to keep track of what keysym a key _code_ generated on press |
| 592 | // and send the same on release. |
| 593 | if (!down) { |
| 594 | DownMap::iterator iter; |
| 595 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 596 | iter = downKeySym.find(origKeyCode); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 597 | if (iter == downKeySym.end()) { |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 598 | vlog.error(_("Unexpected release of FLTK key code %d (0x%04x)"), |
| 599 | origKeyCode, origKeyCode); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 600 | return; |
| 601 | } |
| 602 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 603 | vlog.debug("Key released: 0x%04x => 0x%04x", origKeyCode, iter->second); |
Pierre Ossman | 71f295a | 2011-05-19 14:55:12 +0000 | [diff] [blame] | 604 | |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 605 | cc->writer()->keyEvent(iter->second, false); |
| 606 | |
| 607 | downKeySym.erase(iter); |
| 608 | |
| 609 | return; |
| 610 | } |
| 611 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 612 | keySym = translateKeyEvent(keyCode, origKeyCode, keyText); |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 613 | if (keySym == XK_VoidSymbol) |
| 614 | return; |
| 615 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 616 | vlog.debug("Key pressed: 0x%04x (0x%04x) '%s' => 0x%04x", |
| 617 | origKeyCode, keyCode, keyText, keySym); |
Pierre Ossman | 71f295a | 2011-05-19 14:55:12 +0000 | [diff] [blame] | 618 | |
Pierre Ossman | cd6ddef | 2011-05-20 12:05:20 +0000 | [diff] [blame] | 619 | downKeySym[origKeyCode] = keySym; |
Pierre Ossman | d50b3d1 | 2011-04-15 07:46:56 +0000 | [diff] [blame] | 620 | cc->writer()->keyEvent(keySym, down); |
| 621 | } |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 622 | |
| 623 | |
| 624 | void Viewport::initContextMenu() |
| 625 | { |
Pierre Ossman | 4e7271e | 2011-05-24 12:47:12 +0000 | [diff] [blame] | 626 | contextMenu->clear(); |
| 627 | |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 628 | contextMenu->add(_("Exit viewer"), 0, NULL, (void*)ID_EXIT, FL_MENU_DIVIDER); |
| 629 | |
Pierre Ossman | 4c613d3 | 2011-05-26 14:14:06 +0000 | [diff] [blame] | 630 | #ifdef HAVE_FLTK_FULLSCREEN |
| 631 | contextMenu->add(_("Full screen"), 0, NULL, (void*)ID_FULLSCREEN, FL_MENU_DIVIDER); |
| 632 | #endif |
| 633 | |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 634 | contextMenu->add(_("Ctrl"), 0, NULL, (void*)ID_CTRL, FL_MENU_TOGGLE); |
| 635 | contextMenu->add(_("Alt"), 0, NULL, (void*)ID_ALT, FL_MENU_TOGGLE); |
Pierre Ossman | 4e7271e | 2011-05-24 12:47:12 +0000 | [diff] [blame] | 636 | |
| 637 | if (menuKeyCode) { |
| 638 | char sendMenuKey[64]; |
| 639 | snprintf(sendMenuKey, 64, _("Send %s"), (const char *)menuKey); |
| 640 | contextMenu->add(sendMenuKey, 0, NULL, (void*)ID_MENUKEY, 0); |
| 641 | contextMenu->add("Secret shortcut menu key", menuKeyCode, NULL, (void*)ID_MENUKEY, FL_MENU_INVISIBLE); |
| 642 | } |
| 643 | |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 644 | contextMenu->add(_("Send Ctrl-Alt-Del"), 0, NULL, (void*)ID_CTRLALTDEL, FL_MENU_DIVIDER); |
| 645 | |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 646 | contextMenu->add(_("Refresh screen"), 0, NULL, (void*)ID_REFRESH, FL_MENU_DIVIDER); |
| 647 | |
Pierre Ossman | d463b57 | 2011-05-16 12:04:43 +0000 | [diff] [blame] | 648 | contextMenu->add(_("Options..."), 0, NULL, (void*)ID_OPTIONS, 0); |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 649 | contextMenu->add(_("Connection info..."), 0, NULL, (void*)ID_INFO, 0); |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 650 | contextMenu->add(_("About TigerVNC viewer..."), 0, NULL, (void*)ID_ABOUT, FL_MENU_DIVIDER); |
| 651 | |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 652 | contextMenu->add(_("Dismiss menu"), 0, NULL, (void*)ID_DISMISS, 0); |
| 653 | } |
| 654 | |
| 655 | |
| 656 | void Viewport::popupContextMenu() |
| 657 | { |
| 658 | const Fl_Menu_Item *m; |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 659 | char buffer[1024]; |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 660 | |
Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 661 | // FIXME: Hacky workaround for the fact that menus grab and ungrab the |
| 662 | // keyboard. Releasing focus here triggers some events on ungrab |
| 663 | // that DesktopWindow can catch and trigger some voodoo. |
| 664 | // See DesktopWindow::handle(). |
| 665 | if (window()->contains(Fl::focus())) |
| 666 | Fl::focus(NULL); |
| 667 | |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 668 | m = contextMenu->popup(); |
| 669 | if (m == NULL) |
| 670 | return; |
| 671 | |
| 672 | switch (m->argument()) { |
| 673 | case ID_EXIT: |
| 674 | exit_vncviewer(); |
| 675 | break; |
Pierre Ossman | 4c613d3 | 2011-05-26 14:14:06 +0000 | [diff] [blame] | 676 | #ifdef HAVE_FLTK_FULLSCREEN |
| 677 | case ID_FULLSCREEN: |
| 678 | if (window()->fullscreen_active()) |
| 679 | window()->fullscreen_off(); |
| 680 | else |
| 681 | window()->fullscreen(); |
| 682 | break; |
| 683 | #endif |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 684 | case ID_CTRL: |
| 685 | if (!viewOnly) |
| 686 | cc->writer()->keyEvent(XK_Control_L, m->value()); |
| 687 | break; |
| 688 | case ID_ALT: |
| 689 | if (!viewOnly) |
| 690 | cc->writer()->keyEvent(XK_Alt_L, m->value()); |
| 691 | break; |
| 692 | case ID_MENUKEY: |
| 693 | if (!viewOnly) { |
Pierre Ossman | 4e7271e | 2011-05-24 12:47:12 +0000 | [diff] [blame] | 694 | handleKeyEvent(menuKeyCode, menuKeyCode, "", true); |
| 695 | handleKeyEvent(menuKeyCode, menuKeyCode, "", false); |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 696 | } |
| 697 | break; |
| 698 | case ID_CTRLALTDEL: |
| 699 | if (!viewOnly) { |
| 700 | cc->writer()->keyEvent(XK_Control_L, true); |
| 701 | cc->writer()->keyEvent(XK_Alt_L, true); |
| 702 | cc->writer()->keyEvent(XK_Delete, true); |
| 703 | cc->writer()->keyEvent(XK_Delete, false); |
| 704 | cc->writer()->keyEvent(XK_Alt_L, false); |
| 705 | cc->writer()->keyEvent(XK_Control_L, false); |
| 706 | } |
| 707 | break; |
Pierre Ossman | d4c61ce | 2011-04-29 11:18:12 +0000 | [diff] [blame] | 708 | case ID_REFRESH: |
| 709 | cc->refreshFramebuffer(); |
| 710 | break; |
Pierre Ossman | d463b57 | 2011-05-16 12:04:43 +0000 | [diff] [blame] | 711 | case ID_OPTIONS: |
| 712 | OptionsDialog::showDialog(); |
| 713 | break; |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 714 | case ID_INFO: |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 715 | if (fltk_escape(cc->connectionInfo(), buffer, sizeof(buffer)) < sizeof(buffer)) { |
| 716 | fl_message_title(_("VNC connection info")); |
| 717 | fl_message(buffer); |
| 718 | } |
Pierre Ossman | 2eb1d11 | 2011-05-16 12:18:08 +0000 | [diff] [blame] | 719 | break; |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 720 | case ID_ABOUT: |
| 721 | about_vncviewer(); |
| 722 | break; |
Pierre Ossman | c7bfaac | 2011-04-29 11:08:11 +0000 | [diff] [blame] | 723 | case ID_DISMISS: |
| 724 | // Don't need to do anything |
| 725 | break; |
| 726 | } |
| 727 | } |
Pierre Ossman | 4e7271e | 2011-05-24 12:47:12 +0000 | [diff] [blame] | 728 | |
| 729 | |
| 730 | void Viewport::setMenuKey() |
| 731 | { |
| 732 | const char *menuKeyStr; |
| 733 | |
| 734 | menuKeyCode = 0; |
| 735 | |
| 736 | menuKeyStr = menuKey; |
| 737 | if (menuKeyStr[0] == 'F') { |
| 738 | int num = atoi(menuKeyStr + 1); |
| 739 | if ((num >= 1) && (num <= 12)) |
| 740 | menuKeyCode = FL_F + num; |
| 741 | } |
| 742 | |
| 743 | // Need to repopulate the context menu as it contains references to |
| 744 | // the menu key |
| 745 | initContextMenu(); |
| 746 | } |
| 747 | |
| 748 | |
| 749 | void Viewport::handleOptions(void *data) |
| 750 | { |
| 751 | Viewport *self = (Viewport*)data; |
| 752 | |
| 753 | self->setMenuKey(); |
| 754 | } |