Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | // |
| 19 | // DesktopWindow.cxx |
| 20 | // |
| 21 | |
| 22 | #include "DesktopWindow.h" |
| 23 | #include "CConn.h" |
| 24 | #include <rfb/CMsgWriter.h> |
| 25 | #include <rfb/LogWriter.h> |
| 26 | #include <X11/keysym.h> |
| 27 | #include <X11/Xatom.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include "parameters.h" |
| 31 | |
| 32 | #ifndef XK_ISO_Left_Tab |
| 33 | #define XK_ISO_Left_Tab 0xFE20 |
| 34 | #endif |
| 35 | |
| 36 | static rdr::U8 reverseBits[] = { |
| 37 | 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, |
| 38 | 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, |
| 39 | 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, |
| 40 | 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, |
| 41 | 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, |
| 42 | 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, |
| 43 | 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, |
| 44 | 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, |
| 45 | 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, |
| 46 | 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, |
| 47 | 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1, |
| 48 | 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, |
| 49 | 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, |
| 50 | 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, |
| 51 | 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd, |
| 52 | 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, |
| 53 | 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, |
| 54 | 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, |
| 55 | 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, |
| 56 | 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, |
| 57 | 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, |
| 58 | 0x3f, 0xbf, 0x7f, 0xff |
| 59 | }; |
| 60 | |
| 61 | using namespace rfb; |
| 62 | |
| 63 | static rfb::LogWriter vlog("DesktopWindow"); |
| 64 | |
| 65 | DesktopWindow::DesktopWindow(Display* dpy, int w, int h, |
| 66 | const rfb::PixelFormat& serverPF, |
| 67 | CConn* cc_, TXWindow* parent) |
Pierre Ossman | 9760ff6 | 2009-03-25 10:32:07 +0000 | [diff] [blame^] | 68 | : TXWindow(dpy, w, h, parent), cc(cc_), im(0), updateTimer(this), |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 69 | cursorVisible(false), cursorAvailable(false), currentSelectionTime(0), |
| 70 | newSelection(0), gettingInitialSelectionTime(true), |
| 71 | newServerCutText(false), serverCutText_(0), |
| 72 | setColourMapEntriesTimer(this), viewport(0), |
| 73 | pointerEventTimer(this), |
| 74 | lastButtonMask(0) |
| 75 | { |
| 76 | setEventHandler(this); |
| 77 | gc = XCreateGC(dpy, win(), 0, 0); |
| 78 | addEventMask(ExposureMask | ButtonPressMask | ButtonReleaseMask | |
| 79 | PointerMotionMask | KeyPressMask | KeyReleaseMask | |
| 80 | EnterWindowMask | LeaveWindowMask); |
| 81 | createXCursors(); |
| 82 | XDefineCursor(dpy, win(), dotCursor); |
| 83 | im = new TXImage(dpy, width(), height()); |
| 84 | if (!serverPF.trueColour) |
| 85 | im->setPF(serverPF); |
| 86 | XConvertSelection(dpy, sendPrimary ? XA_PRIMARY : xaCLIPBOARD, xaTIMESTAMP, |
| 87 | xaSELECTION_TIME, win(), CurrentTime); |
| 88 | memset(downKeysym, 0, 256*4); |
| 89 | } |
| 90 | |
| 91 | DesktopWindow::~DesktopWindow() |
| 92 | { |
| 93 | XFreeGC(dpy, gc); |
| 94 | XFreeCursor(dpy, dotCursor); |
| 95 | XFreeCursor(dpy, noCursor); |
| 96 | if (localXCursor) |
| 97 | XFreeCursor(dpy, localXCursor); |
| 98 | delete im; |
| 99 | } |
| 100 | |
| 101 | void DesktopWindow::setViewport(TXViewport* viewport_) |
| 102 | { |
| 103 | viewport = viewport_; |
| 104 | viewport->setChild(this); |
| 105 | } |
| 106 | |
| 107 | // Cursor stuff |
| 108 | |
| 109 | void DesktopWindow::createXCursors() |
| 110 | { |
| 111 | static char dotSource[] = { 0x00, 0x0e, 0x0e, 0x0e, 0x00 }; |
| 112 | static char dotMask[] = { 0x1f, 0x1f, 0x1f, 0x1f, 0x1f }; |
| 113 | Pixmap source = XCreateBitmapFromData(dpy, win(), dotSource, 5, 5); |
| 114 | Pixmap mask = XCreateBitmapFromData(dpy, win(), dotMask, 5, 5); |
| 115 | XColor fg, bg; |
| 116 | fg.red = fg.green = fg.blue = 0; |
| 117 | bg.red = bg.green = bg.blue = 0xffff; |
| 118 | dotCursor = XCreatePixmapCursor(dpy, source, mask, &fg, &bg, 2, 2); |
| 119 | XFreePixmap(dpy, source); |
| 120 | XFreePixmap(dpy, mask); |
| 121 | char zero = 0; |
| 122 | Pixmap empty = XCreateBitmapFromData(dpy, win(), &zero, 1, 1); |
| 123 | noCursor = XCreatePixmapCursor(dpy, empty, empty, &fg, &bg, 0, 0); |
| 124 | XFreePixmap(dpy, empty); |
| 125 | localXCursor = 0; |
| 126 | } |
| 127 | |
| 128 | void DesktopWindow::setCursor(int width, int height, const Point& hotspot, |
| 129 | void* data, void* mask) |
| 130 | { |
| 131 | if (!useLocalCursor) return; |
| 132 | |
| 133 | hideLocalCursor(); |
| 134 | |
| 135 | int mask_len = ((width+7)/8) * height; |
| 136 | |
| 137 | int i; |
| 138 | for (i = 0; i < mask_len; i++) |
| 139 | if (((rdr::U8*)mask)[i]) break; |
| 140 | |
| 141 | if (i == mask_len) { |
| 142 | if (dotWhenNoCursor) { |
| 143 | vlog.debug("cursor is empty - using dot"); |
| 144 | XDefineCursor(dpy, win(), dotCursor); |
| 145 | } else { |
| 146 | XDefineCursor(dpy, win(), noCursor); |
| 147 | } |
| 148 | cursorAvailable = false; |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | cursor.hotspot = hotspot; |
| 153 | |
| 154 | cursor.setSize(width, height); |
| 155 | cursor.setPF(getPF()); |
| 156 | cursor.imageRect(cursor.getRect(), data); |
| 157 | |
| 158 | cursorBacking.setSize(width, height); |
| 159 | cursorBacking.setPF(getPF()); |
| 160 | |
| 161 | delete [] cursor.mask.buf; |
| 162 | cursor.mask.buf = new rdr::U8[mask_len]; |
| 163 | memcpy(cursor.mask.buf, mask, mask_len); |
| 164 | |
| 165 | Pixel pix0, pix1; |
| 166 | rdr::U8Array bitmap(cursor.getBitmap(&pix0, &pix1)); |
| 167 | if (bitmap.buf && cursor.getRect().contains(cursor.hotspot)) { |
| 168 | int bytesPerRow = (cursor.width() + 7) / 8; |
| 169 | for (int j = 0; j < cursor.height(); j++) { |
| 170 | for (int i = 0; i < bytesPerRow; i++) { |
| 171 | bitmap.buf[j * bytesPerRow + i] |
| 172 | = reverseBits[bitmap.buf[j * bytesPerRow + i]]; |
| 173 | cursor.mask.buf[j * bytesPerRow + i] |
| 174 | = reverseBits[cursor.mask.buf[j * bytesPerRow + i]]; |
| 175 | } |
| 176 | } |
| 177 | Pixmap source = XCreateBitmapFromData(dpy, win(), (char*)bitmap.buf, |
| 178 | cursor.width(), cursor.height()); |
| 179 | Pixmap mask = XCreateBitmapFromData(dpy, win(), (char*)cursor.mask.buf, |
| 180 | cursor.width(), cursor.height()); |
| 181 | Colour rgb; |
| 182 | XColor fg, bg; |
| 183 | getPF().rgbFromPixel(pix1, im->getColourMap(), &rgb); |
| 184 | fg.red = rgb.r; fg.green = rgb.g; fg.blue = rgb.b; |
| 185 | getPF().rgbFromPixel(pix0, im->getColourMap(), &rgb); |
| 186 | bg.red = rgb.r; bg.green = rgb.g; bg.blue = rgb.b; |
| 187 | if (localXCursor) |
| 188 | XFreeCursor(dpy, localXCursor); |
| 189 | localXCursor = XCreatePixmapCursor(dpy, source, mask, &fg, &bg, |
| 190 | cursor.hotspot.x, cursor.hotspot.y); |
| 191 | XDefineCursor(dpy, win(), localXCursor); |
| 192 | XFreePixmap(dpy, source); |
| 193 | XFreePixmap(dpy, mask); |
| 194 | cursorAvailable = false; |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | if (!cursorAvailable) { |
| 199 | XDefineCursor(dpy, win(), noCursor); |
| 200 | cursorAvailable = true; |
| 201 | } |
| 202 | |
| 203 | showLocalCursor(); |
| 204 | } |
| 205 | |
| 206 | void DesktopWindow::resetLocalCursor() |
| 207 | { |
| 208 | hideLocalCursor(); |
| 209 | XDefineCursor(dpy, win(), dotCursor); |
| 210 | cursorAvailable = false; |
| 211 | } |
| 212 | |
| 213 | void DesktopWindow::hideLocalCursor() |
| 214 | { |
| 215 | // - Blit the cursor backing store over the cursor |
| 216 | if (cursorVisible) { |
| 217 | cursorVisible = false; |
| 218 | im->imageRect(cursorBackingRect, cursorBacking.data); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | void DesktopWindow::showLocalCursor() |
| 223 | { |
| 224 | if (cursorAvailable && !cursorVisible) { |
| 225 | if (!getPF().equal(cursor.getPF()) || |
| 226 | cursor.getRect().is_empty()) { |
| 227 | vlog.error("attempting to render invalid local cursor"); |
| 228 | XDefineCursor(dpy, win(), dotCursor); |
| 229 | cursorAvailable = false; |
| 230 | return; |
| 231 | } |
| 232 | cursorVisible = true; |
| 233 | |
| 234 | rfb::Rect cursorRect = (cursor.getRect().translate(cursorPos). |
| 235 | translate(cursor.hotspot.negate())); |
| 236 | cursorBackingRect = cursorRect.intersect(im->getRect()); |
| 237 | im->getImage(cursorBacking.data, cursorBackingRect); |
| 238 | |
| 239 | im->maskRect(cursorRect, cursor.data, cursor.mask.buf); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | // setColourMapEntries() changes some of the entries in the colourmap. |
| 244 | // Unfortunately these messages are often sent one at a time, so we delay the |
| 245 | // settings taking effect by 100ms. This is because recalculating the internal |
| 246 | // translation table can be expensive. |
| 247 | void DesktopWindow::setColourMapEntries(int firstColour, int nColours, |
| 248 | rdr::U16* rgbs) |
| 249 | { |
| 250 | im->setColourMapEntries(firstColour, nColours, rgbs); |
| 251 | if (!setColourMapEntriesTimer.isStarted()) |
| 252 | setColourMapEntriesTimer.start(100); |
| 253 | } |
| 254 | |
Adam Tkac | acf6c6b | 2009-02-13 12:42:05 +0000 | [diff] [blame] | 255 | void DesktopWindow::serverCutText(const char* str, rdr::U32 len) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 256 | { |
| 257 | if (acceptClipboard) { |
| 258 | newServerCutText = true; |
| 259 | delete [] serverCutText_; |
| 260 | serverCutText_ = new char[len+1]; |
| 261 | memcpy(serverCutText_, str, len); |
| 262 | serverCutText_[len] = 0; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
Pierre Ossman | 9760ff6 | 2009-03-25 10:32:07 +0000 | [diff] [blame^] | 267 | // Update the actual window with the changed parts of the framebuffer. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 268 | |
| 269 | void DesktopWindow::framebufferUpdateEnd() |
| 270 | { |
Pierre Ossman | 9760ff6 | 2009-03-25 10:32:07 +0000 | [diff] [blame^] | 271 | updateWindow(); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | // invertRect() flips all the bits in every pixel in the given rectangle |
| 276 | |
| 277 | void DesktopWindow::invertRect(const Rect& r) |
| 278 | { |
| 279 | int stride; |
| 280 | rdr::U8* p = im->getPixelsRW(r, &stride); |
| 281 | for (int y = 0; y < r.height(); y++) { |
| 282 | for (int x = 0; x < r.width(); x++) { |
| 283 | switch (getPF().bpp) { |
| 284 | case 8: ((rdr::U8* )p)[x+y*stride] ^= 0xff; break; |
| 285 | case 16: ((rdr::U16*)p)[x+y*stride] ^= 0xffff; break; |
| 286 | case 32: ((rdr::U32*)p)[x+y*stride] ^= 0xffffffff; break; |
| 287 | } |
| 288 | } |
| 289 | } |
Pierre Ossman | 9760ff6 | 2009-03-25 10:32:07 +0000 | [diff] [blame^] | 290 | damageRect(r); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
| 294 | // resize() - resize the window and the image, taking care to remove the local |
| 295 | // cursor first. |
| 296 | void DesktopWindow::resize(int w, int h) |
| 297 | { |
| 298 | hideLocalCursor(); |
| 299 | TXWindow::resize(w, h); |
| 300 | im->resize(w, h); |
| 301 | } |
| 302 | |
| 303 | |
Pierre Ossman | 9760ff6 | 2009-03-25 10:32:07 +0000 | [diff] [blame^] | 304 | // Copy the areas of the framebuffer that have been changed (damaged) |
| 305 | // to the displayed window. |
| 306 | |
| 307 | void DesktopWindow::updateWindow() |
| 308 | { |
| 309 | Rect r; |
| 310 | |
| 311 | updateTimer.stop(); |
| 312 | |
| 313 | r = damage.get_bounding_rect(); |
| 314 | damage.clear(); |
| 315 | |
| 316 | im->put(win(), gc, r); |
| 317 | } |
| 318 | |
| 319 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 320 | bool DesktopWindow::handleTimeout(rfb::Timer* timer) |
| 321 | { |
| 322 | if (timer == &setColourMapEntriesTimer) { |
| 323 | im->updateColourMap(); |
| 324 | im->put(win(), gc, im->getRect()); |
| 325 | } else if (timer == &pointerEventTimer) { |
| 326 | if (!viewOnly) { |
| 327 | cc->writer()->pointerEvent(lastPointerPos, lastButtonMask); |
| 328 | } |
Pierre Ossman | 9760ff6 | 2009-03-25 10:32:07 +0000 | [diff] [blame^] | 329 | } else if (timer == &updateTimer) { |
| 330 | updateWindow(); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 331 | } |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | void DesktopWindow::handlePointerEvent(const Point& pos, int buttonMask) |
| 337 | { |
| 338 | if (!viewOnly) { |
| 339 | if (pointerEventInterval == 0 || buttonMask != lastButtonMask) { |
| 340 | cc->writer()->pointerEvent(pos, buttonMask); |
| 341 | } else { |
| 342 | if (!pointerEventTimer.isStarted()) |
| 343 | pointerEventTimer.start(pointerEventInterval); |
| 344 | } |
| 345 | lastPointerPos = pos; |
| 346 | lastButtonMask = buttonMask; |
| 347 | } |
| 348 | // - If local cursor rendering is enabled then use it |
| 349 | if (cursorAvailable) { |
| 350 | // - Render the cursor! |
| 351 | if (!pos.equals(cursorPos)) { |
| 352 | hideLocalCursor(); |
| 353 | if (im->getRect().contains(pos)) { |
| 354 | cursorPos = pos; |
| 355 | showLocalCursor(); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | |
| 362 | // handleXEvent() handles the various X events on the window |
| 363 | void DesktopWindow::handleEvent(TXWindow* w, XEvent* ev) |
| 364 | { |
| 365 | switch (ev->type) { |
| 366 | case GraphicsExpose: |
| 367 | case Expose: |
| 368 | im->put(win(), gc, Rect(ev->xexpose.x, ev->xexpose.y, |
| 369 | ev->xexpose.x + ev->xexpose.width, |
| 370 | ev->xexpose.y + ev->xexpose.height)); |
| 371 | break; |
| 372 | |
| 373 | case MotionNotify: |
| 374 | while (XCheckTypedWindowEvent(dpy, win(), MotionNotify, ev)); |
| 375 | if (viewport && viewport->bumpScrollEvent(&ev->xmotion)) break; |
| 376 | handlePointerEvent(Point(ev->xmotion.x, ev->xmotion.y), |
| 377 | (ev->xmotion.state & 0x1f00) >> 8); |
| 378 | break; |
| 379 | |
| 380 | case ButtonPress: |
| 381 | handlePointerEvent(Point(ev->xbutton.x, ev->xbutton.y), |
| 382 | (((ev->xbutton.state & 0x1f00) >> 8) | |
| 383 | (1 << (ev->xbutton.button-1)))); |
| 384 | break; |
| 385 | |
| 386 | case ButtonRelease: |
| 387 | handlePointerEvent(Point(ev->xbutton.x, ev->xbutton.y), |
| 388 | (((ev->xbutton.state & 0x1f00) >> 8) & |
| 389 | ~(1 << (ev->xbutton.button-1)))); |
| 390 | break; |
| 391 | |
| 392 | case KeyPress: |
| 393 | if (!viewOnly) { |
| 394 | KeySym ks; |
| 395 | char keyname[256]; |
| 396 | XLookupString(&ev->xkey, keyname, 256, &ks, NULL); |
| 397 | bool fakeShiftPress = false; |
| 398 | |
| 399 | // Turn ISO_Left_Tab into shifted Tab |
| 400 | if (ks == XK_ISO_Left_Tab) { |
| 401 | fakeShiftPress = !(ev->xkey.state & ShiftMask); |
| 402 | ks = XK_Tab; |
| 403 | } |
| 404 | |
| 405 | if (fakeShiftPress) |
| 406 | cc->writer()->keyEvent(XK_Shift_L, true); |
| 407 | |
| 408 | downKeysym[ev->xkey.keycode] = ks; |
| 409 | cc->writer()->keyEvent(ks, true); |
| 410 | |
| 411 | if (fakeShiftPress) |
| 412 | cc->writer()->keyEvent(XK_Shift_L, false); |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | case KeyRelease: |
| 417 | if (!viewOnly) { |
| 418 | if (downKeysym[ev->xkey.keycode]) { |
| 419 | cc->writer()->keyEvent(downKeysym[ev->xkey.keycode], false); |
| 420 | downKeysym[ev->xkey.keycode] = 0; |
| 421 | } |
| 422 | } |
| 423 | break; |
| 424 | |
| 425 | case EnterNotify: |
| 426 | newSelection = 0; |
| 427 | if (sendPrimary && !selectionOwner(XA_PRIMARY)) { |
| 428 | XConvertSelection(dpy, XA_PRIMARY, xaTIMESTAMP, xaSELECTION_TIME, |
| 429 | win(), ev->xcrossing.time); |
| 430 | } else if (!selectionOwner(xaCLIPBOARD)) { |
| 431 | XConvertSelection(dpy, xaCLIPBOARD, xaTIMESTAMP, xaSELECTION_TIME, |
| 432 | win(), ev->xcrossing.time); |
| 433 | } |
| 434 | break; |
| 435 | |
| 436 | case LeaveNotify: |
| 437 | if (serverCutText_ && newServerCutText) { |
| 438 | newServerCutText = false; |
| 439 | vlog.debug("acquiring primary and clipboard selections"); |
| 440 | XStoreBytes(dpy, serverCutText_, strlen(serverCutText_)); |
| 441 | ownSelection(XA_PRIMARY, ev->xcrossing.time); |
| 442 | ownSelection(xaCLIPBOARD, ev->xcrossing.time); |
| 443 | currentSelectionTime = ev->xcrossing.time; |
| 444 | } |
| 445 | // Release all keys - this should probably done on a FocusOut event, but |
| 446 | // LeaveNotify is near enough... |
| 447 | for (int i = 8; i < 256; i++) { |
| 448 | if (downKeysym[i]) { |
| 449 | cc->writer()->keyEvent(downKeysym[i], false); |
| 450 | downKeysym[i] = 0; |
| 451 | } |
| 452 | } |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // selectionRequest() is called when we are the selection owner and another X |
| 458 | // client has requested the selection. We simply put the server's cut text |
| 459 | // into the requested property. TXWindow will handle the rest. |
| 460 | bool DesktopWindow::selectionRequest(Window requestor, |
| 461 | Atom selection, Atom property) |
| 462 | { |
| 463 | XChangeProperty(dpy, requestor, property, XA_STRING, 8, |
| 464 | PropModeReplace, (unsigned char*)serverCutText_, |
| 465 | strlen(serverCutText_)); |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | // selectionNotify() is called when we have requested any information about a |
| 471 | // selection from the selection owner. Note that there are two selections, |
| 472 | // PRIMARY and CLIPBOARD, plus the cut buffer, and we try to use whichever is |
| 473 | // the most recent of the three. |
| 474 | // |
| 475 | // There are two different "targets" for which selectionNotify() is called, the |
| 476 | // timestamp and the actual string value of the selection. We always use the |
| 477 | // timestamp to decide which selection to retrieve. |
| 478 | // |
| 479 | // The first time selectionNotify() is called is when we are trying to find the |
| 480 | // timestamp of the selections at initialisation. This should be called first |
| 481 | // for PRIMARY, then we call XConvertSelection() for CLIPBOARD. The second |
| 482 | // time should be the result for CLIPBOARD. At this stage we've got the |
| 483 | // "currentSelectionTime" so we return. |
| 484 | // |
| 485 | // Subsequently selectionNotify() is called whenever the mouse enters the |
| 486 | // viewer window. Again, the first time it is called should be the timestamp |
| 487 | // for PRIMARY, and we then request the timestamp for CLIPBOARD. When |
| 488 | // selectionNotify() is called again with the timestamp for CLIPBOARD, we now |
| 489 | // know if either selection is "new" i.e. later than the previous value of |
| 490 | // currentSelectionTime. The last thing to check is the timestamp on the cut |
| 491 | // buffer. If the cut buffer is newest we send that to the server, otherwise |
| 492 | // if one of the selections was newer, we request the string value of that |
| 493 | // selection. |
| 494 | // |
| 495 | // Finally, if we get selectionNotify() called for the string value of a |
| 496 | // selection, we sent that to the server. |
| 497 | // |
| 498 | // As a final minor complication, when one of the selections is actually owned |
| 499 | // by us, we don't request the details for it. |
| 500 | |
| 501 | // TIME_LATER treats 0 as meaning a long time ago, so a==0 means a cannot be |
| 502 | // later than b. This is different to the usual meaning of CurrentTime. |
| 503 | #define TIME_LATER(a, b) ((a) != 0 && ((b) == 0 || (long)((a) - (b)) > 0)) |
| 504 | |
| 505 | |
| 506 | void DesktopWindow::selectionNotify(XSelectionEvent* ev, Atom type, int format, |
| 507 | int nitems, void* data) |
| 508 | { |
| 509 | if (ev->requestor != win()) |
| 510 | return; |
| 511 | |
| 512 | if (ev->target == xaTIMESTAMP) { |
| 513 | if (ev->property == xaSELECTION_TIME) { |
| 514 | if (data && format == 32 && nitems == 1) { |
| 515 | Time t = *(rdr::U32 *)data; |
| 516 | vlog.debug("selection (%d) time is %d, later %d", |
| 517 | ev->selection, t, TIME_LATER(t, currentSelectionTime)); |
| 518 | if (TIME_LATER(t, currentSelectionTime)) { |
| 519 | currentSelectionTime = t; |
| 520 | newSelection = ev->selection; |
| 521 | } |
| 522 | } |
| 523 | } else { |
| 524 | vlog.debug("no selection (%d)",ev->selection); |
| 525 | } |
| 526 | |
| 527 | if (ev->selection == XA_PRIMARY) { |
| 528 | if (!selectionOwner(xaCLIPBOARD)) { |
| 529 | XConvertSelection(dpy, xaCLIPBOARD, xaTIMESTAMP, xaSELECTION_TIME, |
| 530 | win(), ev->time); |
| 531 | return; |
| 532 | } |
| 533 | } else if (ev->selection != xaCLIPBOARD) { |
| 534 | vlog.error("unknown selection %d",ev->selection); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | if (gettingInitialSelectionTime) { |
| 539 | gettingInitialSelectionTime = false; |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | if (!sendClipboard) return; |
| 544 | if (sendPrimary) { |
| 545 | vlog.debug("cut buffer time is %d, later %d", cutBufferTime, |
| 546 | TIME_LATER(cutBufferTime, currentSelectionTime)); |
| 547 | if (TIME_LATER(cutBufferTime, currentSelectionTime)) { |
| 548 | currentSelectionTime = cutBufferTime; |
| 549 | int len; |
| 550 | char* str = XFetchBytes(dpy, &len); |
| 551 | if (str) { |
| 552 | if (!viewOnly) { |
| 553 | vlog.debug("sending cut buffer to server"); |
| 554 | cc->writer()->clientCutText(str, len); |
| 555 | } |
| 556 | XFree(str); |
| 557 | return; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | if (newSelection) { |
| 562 | XConvertSelection(dpy, newSelection, XA_STRING, xaSELECTION_STRING, |
| 563 | win(), CurrentTime); |
| 564 | } |
| 565 | |
| 566 | } else if (ev->target == XA_STRING) { |
| 567 | if (!sendClipboard) return; |
| 568 | if (ev->property == xaSELECTION_STRING) { |
| 569 | if (data && format == 8) { |
| 570 | if (!viewOnly) { |
| 571 | vlog.debug("sending %s selection to server", |
| 572 | ev->selection == XA_PRIMARY ? "primary" : |
| 573 | ev->selection == xaCLIPBOARD ? "clipboard" : "unknown" ); |
| 574 | cc->writer()->clientCutText((char*)data, nitems); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |