blob: 234e004943f9d241a9a6d62a239ba3738759f6fe [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001/* 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
36static 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
61using namespace rfb;
62
63static rfb::LogWriter vlog("DesktopWindow");
64
65DesktopWindow::DesktopWindow(Display* dpy, int w, int h,
66 const rfb::PixelFormat& serverPF,
67 CConn* cc_, TXWindow* parent)
Pierre Ossman9760ff62009-03-25 10:32:07 +000068 : TXWindow(dpy, w, h, parent), cc(cc_), im(0), updateTimer(this),
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000069 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();
Adam Tkac20fcb972009-10-07 15:11:08 +000082 setNoCursor();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000083 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
91DesktopWindow::~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
101void DesktopWindow::setViewport(TXViewport* viewport_)
102{
103 viewport = viewport_;
104 viewport->setChild(this);
105}
106
107// Cursor stuff
108
109void 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
128void 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) {
Adam Tkac20fcb972009-10-07 15:11:08 +0000142 if (dotWhenNoCursor)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000143 vlog.debug("cursor is empty - using dot");
Adam Tkac20fcb972009-10-07 15:11:08 +0000144 setNoCursor();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000145 cursorAvailable = false;
146 return;
147 }
148
149 cursor.hotspot = hotspot;
150
151 cursor.setSize(width, height);
152 cursor.setPF(getPF());
153 cursor.imageRect(cursor.getRect(), data);
154
155 cursorBacking.setSize(width, height);
156 cursorBacking.setPF(getPF());
157
158 delete [] cursor.mask.buf;
159 cursor.mask.buf = new rdr::U8[mask_len];
160 memcpy(cursor.mask.buf, mask, mask_len);
161
162 Pixel pix0, pix1;
163 rdr::U8Array bitmap(cursor.getBitmap(&pix0, &pix1));
164 if (bitmap.buf && cursor.getRect().contains(cursor.hotspot)) {
165 int bytesPerRow = (cursor.width() + 7) / 8;
166 for (int j = 0; j < cursor.height(); j++) {
167 for (int i = 0; i < bytesPerRow; i++) {
168 bitmap.buf[j * bytesPerRow + i]
169 = reverseBits[bitmap.buf[j * bytesPerRow + i]];
170 cursor.mask.buf[j * bytesPerRow + i]
171 = reverseBits[cursor.mask.buf[j * bytesPerRow + i]];
172 }
173 }
174 Pixmap source = XCreateBitmapFromData(dpy, win(), (char*)bitmap.buf,
175 cursor.width(), cursor.height());
176 Pixmap mask = XCreateBitmapFromData(dpy, win(), (char*)cursor.mask.buf,
177 cursor.width(), cursor.height());
178 Colour rgb;
179 XColor fg, bg;
180 getPF().rgbFromPixel(pix1, im->getColourMap(), &rgb);
181 fg.red = rgb.r; fg.green = rgb.g; fg.blue = rgb.b;
182 getPF().rgbFromPixel(pix0, im->getColourMap(), &rgb);
183 bg.red = rgb.r; bg.green = rgb.g; bg.blue = rgb.b;
184 if (localXCursor)
185 XFreeCursor(dpy, localXCursor);
186 localXCursor = XCreatePixmapCursor(dpy, source, mask, &fg, &bg,
187 cursor.hotspot.x, cursor.hotspot.y);
188 XDefineCursor(dpy, win(), localXCursor);
189 XFreePixmap(dpy, source);
190 XFreePixmap(dpy, mask);
191 cursorAvailable = false;
192 return;
193 }
194
195 if (!cursorAvailable) {
196 XDefineCursor(dpy, win(), noCursor);
197 cursorAvailable = true;
198 }
199
200 showLocalCursor();
201}
202
203void DesktopWindow::resetLocalCursor()
204{
205 hideLocalCursor();
Adam Tkac20fcb972009-10-07 15:11:08 +0000206 setNoCursor();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000207 cursorAvailable = false;
208}
209
210void DesktopWindow::hideLocalCursor()
211{
212 // - Blit the cursor backing store over the cursor
213 if (cursorVisible) {
214 cursorVisible = false;
215 im->imageRect(cursorBackingRect, cursorBacking.data);
Pierre Ossman24ed17a2009-06-22 11:56:19 +0000216 damageRect(cursorBackingRect);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000217 }
218}
219
220void DesktopWindow::showLocalCursor()
221{
222 if (cursorAvailable && !cursorVisible) {
223 if (!getPF().equal(cursor.getPF()) ||
224 cursor.getRect().is_empty()) {
225 vlog.error("attempting to render invalid local cursor");
Adam Tkac20fcb972009-10-07 15:11:08 +0000226 setNoCursor();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000227 cursorAvailable = false;
228 return;
229 }
230 cursorVisible = true;
231
232 rfb::Rect cursorRect = (cursor.getRect().translate(cursorPos).
233 translate(cursor.hotspot.negate()));
234 cursorBackingRect = cursorRect.intersect(im->getRect());
235 im->getImage(cursorBacking.data, cursorBackingRect);
236
237 im->maskRect(cursorRect, cursor.data, cursor.mask.buf);
Pierre Ossman24ed17a2009-06-22 11:56:19 +0000238 damageRect(cursorBackingRect);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000239 }
240}
241
242// setColourMapEntries() changes some of the entries in the colourmap.
243// Unfortunately these messages are often sent one at a time, so we delay the
244// settings taking effect by 100ms. This is because recalculating the internal
245// translation table can be expensive.
246void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
247 rdr::U16* rgbs)
248{
249 im->setColourMapEntries(firstColour, nColours, rgbs);
250 if (!setColourMapEntriesTimer.isStarted())
251 setColourMapEntriesTimer.start(100);
252}
253
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000254void DesktopWindow::serverCutText(const char* str, rdr::U32 len)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000255{
256 if (acceptClipboard) {
257 newServerCutText = true;
258 delete [] serverCutText_;
259 serverCutText_ = new char[len+1];
260 memcpy(serverCutText_, str, len);
261 serverCutText_[len] = 0;
262 }
263}
264
265
Pierre Ossman9760ff62009-03-25 10:32:07 +0000266// Update the actual window with the changed parts of the framebuffer.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000267
268void DesktopWindow::framebufferUpdateEnd()
269{
Pierre Ossman9760ff62009-03-25 10:32:07 +0000270 updateWindow();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000271}
272
273
274// invertRect() flips all the bits in every pixel in the given rectangle
275
276void DesktopWindow::invertRect(const Rect& r)
277{
278 int stride;
279 rdr::U8* p = im->getPixelsRW(r, &stride);
280 for (int y = 0; y < r.height(); y++) {
281 for (int x = 0; x < r.width(); x++) {
282 switch (getPF().bpp) {
283 case 8: ((rdr::U8* )p)[x+y*stride] ^= 0xff; break;
284 case 16: ((rdr::U16*)p)[x+y*stride] ^= 0xffff; break;
285 case 32: ((rdr::U32*)p)[x+y*stride] ^= 0xffffffff; break;
286 }
287 }
288 }
Pierre Ossman9760ff62009-03-25 10:32:07 +0000289 damageRect(r);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000290}
291
292
293// resize() - resize the window and the image, taking care to remove the local
294// cursor first.
295void DesktopWindow::resize(int w, int h)
296{
297 hideLocalCursor();
298 TXWindow::resize(w, h);
299 im->resize(w, h);
300}
301
302
Pierre Ossman9760ff62009-03-25 10:32:07 +0000303// Copy the areas of the framebuffer that have been changed (damaged)
304// to the displayed window.
305
306void DesktopWindow::updateWindow()
307{
308 Rect r;
309
310 updateTimer.stop();
311
312 r = damage.get_bounding_rect();
313 damage.clear();
314
315 im->put(win(), gc, r);
Pierre Ossmanb900e362009-04-01 13:47:30 +0000316 XFlush(dpy);
Pierre Ossman9760ff62009-03-25 10:32:07 +0000317}
318
319
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000320bool 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 Ossman9760ff62009-03-25 10:32:07 +0000329 } else if (timer == &updateTimer) {
330 updateWindow();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000331 }
332 return false;
333}
334
335
336void 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 }
Pierre Ossman24ed17a2009-06-22 11:56:19 +0000357 updateWindow();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000358 }
359 }
360}
361
362
363// handleXEvent() handles the various X events on the window
364void DesktopWindow::handleEvent(TXWindow* w, XEvent* ev)
365{
366 switch (ev->type) {
367 case GraphicsExpose:
368 case Expose:
369 im->put(win(), gc, Rect(ev->xexpose.x, ev->xexpose.y,
370 ev->xexpose.x + ev->xexpose.width,
371 ev->xexpose.y + ev->xexpose.height));
372 break;
373
374 case MotionNotify:
375 while (XCheckTypedWindowEvent(dpy, win(), MotionNotify, ev));
376 if (viewport && viewport->bumpScrollEvent(&ev->xmotion)) break;
377 handlePointerEvent(Point(ev->xmotion.x, ev->xmotion.y),
378 (ev->xmotion.state & 0x1f00) >> 8);
379 break;
380
381 case ButtonPress:
382 handlePointerEvent(Point(ev->xbutton.x, ev->xbutton.y),
383 (((ev->xbutton.state & 0x1f00) >> 8) |
384 (1 << (ev->xbutton.button-1))));
385 break;
386
387 case ButtonRelease:
388 handlePointerEvent(Point(ev->xbutton.x, ev->xbutton.y),
389 (((ev->xbutton.state & 0x1f00) >> 8) &
390 ~(1 << (ev->xbutton.button-1))));
391 break;
392
393 case KeyPress:
394 if (!viewOnly) {
395 KeySym ks;
396 char keyname[256];
397 XLookupString(&ev->xkey, keyname, 256, &ks, NULL);
398 bool fakeShiftPress = false;
399
400 // Turn ISO_Left_Tab into shifted Tab
401 if (ks == XK_ISO_Left_Tab) {
402 fakeShiftPress = !(ev->xkey.state & ShiftMask);
403 ks = XK_Tab;
404 }
405
406 if (fakeShiftPress)
407 cc->writer()->keyEvent(XK_Shift_L, true);
408
409 downKeysym[ev->xkey.keycode] = ks;
410 cc->writer()->keyEvent(ks, true);
411
412 if (fakeShiftPress)
413 cc->writer()->keyEvent(XK_Shift_L, false);
414 break;
415 }
416
417 case KeyRelease:
418 if (!viewOnly) {
419 if (downKeysym[ev->xkey.keycode]) {
420 cc->writer()->keyEvent(downKeysym[ev->xkey.keycode], false);
421 downKeysym[ev->xkey.keycode] = 0;
422 }
423 }
424 break;
425
426 case EnterNotify:
427 newSelection = 0;
428 if (sendPrimary && !selectionOwner(XA_PRIMARY)) {
429 XConvertSelection(dpy, XA_PRIMARY, xaTIMESTAMP, xaSELECTION_TIME,
430 win(), ev->xcrossing.time);
431 } else if (!selectionOwner(xaCLIPBOARD)) {
432 XConvertSelection(dpy, xaCLIPBOARD, xaTIMESTAMP, xaSELECTION_TIME,
433 win(), ev->xcrossing.time);
434 }
435 break;
436
437 case LeaveNotify:
438 if (serverCutText_ && newServerCutText) {
439 newServerCutText = false;
440 vlog.debug("acquiring primary and clipboard selections");
441 XStoreBytes(dpy, serverCutText_, strlen(serverCutText_));
442 ownSelection(XA_PRIMARY, ev->xcrossing.time);
443 ownSelection(xaCLIPBOARD, ev->xcrossing.time);
444 currentSelectionTime = ev->xcrossing.time;
445 }
446 // Release all keys - this should probably done on a FocusOut event, but
447 // LeaveNotify is near enough...
448 for (int i = 8; i < 256; i++) {
449 if (downKeysym[i]) {
450 cc->writer()->keyEvent(downKeysym[i], false);
451 downKeysym[i] = 0;
452 }
453 }
454 break;
455 }
456}
457
458// selectionRequest() is called when we are the selection owner and another X
459// client has requested the selection. We simply put the server's cut text
460// into the requested property. TXWindow will handle the rest.
461bool DesktopWindow::selectionRequest(Window requestor,
462 Atom selection, Atom property)
463{
464 XChangeProperty(dpy, requestor, property, XA_STRING, 8,
465 PropModeReplace, (unsigned char*)serverCutText_,
466 strlen(serverCutText_));
467 return true;
468}
469
470
471// selectionNotify() is called when we have requested any information about a
472// selection from the selection owner. Note that there are two selections,
473// PRIMARY and CLIPBOARD, plus the cut buffer, and we try to use whichever is
474// the most recent of the three.
475//
476// There are two different "targets" for which selectionNotify() is called, the
477// timestamp and the actual string value of the selection. We always use the
478// timestamp to decide which selection to retrieve.
479//
480// The first time selectionNotify() is called is when we are trying to find the
481// timestamp of the selections at initialisation. This should be called first
482// for PRIMARY, then we call XConvertSelection() for CLIPBOARD. The second
483// time should be the result for CLIPBOARD. At this stage we've got the
484// "currentSelectionTime" so we return.
485//
486// Subsequently selectionNotify() is called whenever the mouse enters the
487// viewer window. Again, the first time it is called should be the timestamp
488// for PRIMARY, and we then request the timestamp for CLIPBOARD. When
489// selectionNotify() is called again with the timestamp for CLIPBOARD, we now
490// know if either selection is "new" i.e. later than the previous value of
491// currentSelectionTime. The last thing to check is the timestamp on the cut
492// buffer. If the cut buffer is newest we send that to the server, otherwise
493// if one of the selections was newer, we request the string value of that
494// selection.
495//
496// Finally, if we get selectionNotify() called for the string value of a
497// selection, we sent that to the server.
498//
499// As a final minor complication, when one of the selections is actually owned
500// by us, we don't request the details for it.
501
502// TIME_LATER treats 0 as meaning a long time ago, so a==0 means a cannot be
503// later than b. This is different to the usual meaning of CurrentTime.
504#define TIME_LATER(a, b) ((a) != 0 && ((b) == 0 || (long)((a) - (b)) > 0))
505
506
507void DesktopWindow::selectionNotify(XSelectionEvent* ev, Atom type, int format,
508 int nitems, void* data)
509{
510 if (ev->requestor != win())
511 return;
512
513 if (ev->target == xaTIMESTAMP) {
514 if (ev->property == xaSELECTION_TIME) {
515 if (data && format == 32 && nitems == 1) {
516 Time t = *(rdr::U32 *)data;
517 vlog.debug("selection (%d) time is %d, later %d",
518 ev->selection, t, TIME_LATER(t, currentSelectionTime));
519 if (TIME_LATER(t, currentSelectionTime)) {
520 currentSelectionTime = t;
521 newSelection = ev->selection;
522 }
523 }
524 } else {
525 vlog.debug("no selection (%d)",ev->selection);
526 }
527
528 if (ev->selection == XA_PRIMARY) {
529 if (!selectionOwner(xaCLIPBOARD)) {
530 XConvertSelection(dpy, xaCLIPBOARD, xaTIMESTAMP, xaSELECTION_TIME,
531 win(), ev->time);
532 return;
533 }
534 } else if (ev->selection != xaCLIPBOARD) {
535 vlog.error("unknown selection %d",ev->selection);
536 return;
537 }
538
539 if (gettingInitialSelectionTime) {
540 gettingInitialSelectionTime = false;
541 return;
542 }
543
544 if (!sendClipboard) return;
545 if (sendPrimary) {
546 vlog.debug("cut buffer time is %d, later %d", cutBufferTime,
547 TIME_LATER(cutBufferTime, currentSelectionTime));
548 if (TIME_LATER(cutBufferTime, currentSelectionTime)) {
549 currentSelectionTime = cutBufferTime;
550 int len;
551 char* str = XFetchBytes(dpy, &len);
552 if (str) {
553 if (!viewOnly) {
554 vlog.debug("sending cut buffer to server");
555 cc->writer()->clientCutText(str, len);
556 }
557 XFree(str);
558 return;
559 }
560 }
561 }
562 if (newSelection) {
563 XConvertSelection(dpy, newSelection, XA_STRING, xaSELECTION_STRING,
564 win(), CurrentTime);
565 }
566
567 } else if (ev->target == XA_STRING) {
568 if (!sendClipboard) return;
569 if (ev->property == xaSELECTION_STRING) {
570 if (data && format == 8) {
571 if (!viewOnly) {
572 vlog.debug("sending %s selection to server",
573 ev->selection == XA_PRIMARY ? "primary" :
574 ev->selection == xaCLIPBOARD ? "clipboard" : "unknown" );
575 cc->writer()->clientCutText((char*)data, nitems);
576 }
577 }
578 }
579 }
580}