blob: 51f3b850c434da34ff7e32886b2650f51c67277d [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();
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
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) {
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
206void DesktopWindow::resetLocalCursor()
207{
208 hideLocalCursor();
209 XDefineCursor(dpy, win(), dotCursor);
210 cursorAvailable = false;
211}
212
213void DesktopWindow::hideLocalCursor()
214{
215 // - Blit the cursor backing store over the cursor
216 if (cursorVisible) {
217 cursorVisible = false;
218 im->imageRect(cursorBackingRect, cursorBacking.data);
Pierre Ossman24ed17a2009-06-22 11:56:19 +0000219 damageRect(cursorBackingRect);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000220 }
221}
222
223void DesktopWindow::showLocalCursor()
224{
225 if (cursorAvailable && !cursorVisible) {
226 if (!getPF().equal(cursor.getPF()) ||
227 cursor.getRect().is_empty()) {
228 vlog.error("attempting to render invalid local cursor");
229 XDefineCursor(dpy, win(), dotCursor);
230 cursorAvailable = false;
231 return;
232 }
233 cursorVisible = true;
234
235 rfb::Rect cursorRect = (cursor.getRect().translate(cursorPos).
236 translate(cursor.hotspot.negate()));
237 cursorBackingRect = cursorRect.intersect(im->getRect());
238 im->getImage(cursorBacking.data, cursorBackingRect);
239
240 im->maskRect(cursorRect, cursor.data, cursor.mask.buf);
Pierre Ossman24ed17a2009-06-22 11:56:19 +0000241 damageRect(cursorBackingRect);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000242 }
243}
244
245// setColourMapEntries() changes some of the entries in the colourmap.
246// Unfortunately these messages are often sent one at a time, so we delay the
247// settings taking effect by 100ms. This is because recalculating the internal
248// translation table can be expensive.
249void DesktopWindow::setColourMapEntries(int firstColour, int nColours,
250 rdr::U16* rgbs)
251{
252 im->setColourMapEntries(firstColour, nColours, rgbs);
253 if (!setColourMapEntriesTimer.isStarted())
254 setColourMapEntriesTimer.start(100);
255}
256
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000257void DesktopWindow::serverCutText(const char* str, rdr::U32 len)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000258{
259 if (acceptClipboard) {
260 newServerCutText = true;
261 delete [] serverCutText_;
262 serverCutText_ = new char[len+1];
263 memcpy(serverCutText_, str, len);
264 serverCutText_[len] = 0;
265 }
266}
267
268
Pierre Ossman9760ff62009-03-25 10:32:07 +0000269// Update the actual window with the changed parts of the framebuffer.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000270
271void DesktopWindow::framebufferUpdateEnd()
272{
Pierre Ossman9760ff62009-03-25 10:32:07 +0000273 updateWindow();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000274}
275
276
277// invertRect() flips all the bits in every pixel in the given rectangle
278
279void DesktopWindow::invertRect(const Rect& r)
280{
281 int stride;
282 rdr::U8* p = im->getPixelsRW(r, &stride);
283 for (int y = 0; y < r.height(); y++) {
284 for (int x = 0; x < r.width(); x++) {
285 switch (getPF().bpp) {
286 case 8: ((rdr::U8* )p)[x+y*stride] ^= 0xff; break;
287 case 16: ((rdr::U16*)p)[x+y*stride] ^= 0xffff; break;
288 case 32: ((rdr::U32*)p)[x+y*stride] ^= 0xffffffff; break;
289 }
290 }
291 }
Pierre Ossman9760ff62009-03-25 10:32:07 +0000292 damageRect(r);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000293}
294
295
296// resize() - resize the window and the image, taking care to remove the local
297// cursor first.
298void DesktopWindow::resize(int w, int h)
299{
300 hideLocalCursor();
301 TXWindow::resize(w, h);
302 im->resize(w, h);
303}
304
305
Pierre Ossman9760ff62009-03-25 10:32:07 +0000306// Copy the areas of the framebuffer that have been changed (damaged)
307// to the displayed window.
308
309void DesktopWindow::updateWindow()
310{
311 Rect r;
312
313 updateTimer.stop();
314
315 r = damage.get_bounding_rect();
316 damage.clear();
317
318 im->put(win(), gc, r);
Pierre Ossmanb900e362009-04-01 13:47:30 +0000319 XFlush(dpy);
Pierre Ossman9760ff62009-03-25 10:32:07 +0000320}
321
322
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000323bool DesktopWindow::handleTimeout(rfb::Timer* timer)
324{
325 if (timer == &setColourMapEntriesTimer) {
326 im->updateColourMap();
327 im->put(win(), gc, im->getRect());
328 } else if (timer == &pointerEventTimer) {
329 if (!viewOnly) {
330 cc->writer()->pointerEvent(lastPointerPos, lastButtonMask);
331 }
Pierre Ossman9760ff62009-03-25 10:32:07 +0000332 } else if (timer == &updateTimer) {
333 updateWindow();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000334 }
335 return false;
336}
337
338
339void DesktopWindow::handlePointerEvent(const Point& pos, int buttonMask)
340{
341 if (!viewOnly) {
342 if (pointerEventInterval == 0 || buttonMask != lastButtonMask) {
343 cc->writer()->pointerEvent(pos, buttonMask);
344 } else {
345 if (!pointerEventTimer.isStarted())
346 pointerEventTimer.start(pointerEventInterval);
347 }
348 lastPointerPos = pos;
349 lastButtonMask = buttonMask;
350 }
351 // - If local cursor rendering is enabled then use it
352 if (cursorAvailable) {
353 // - Render the cursor!
354 if (!pos.equals(cursorPos)) {
355 hideLocalCursor();
356 if (im->getRect().contains(pos)) {
357 cursorPos = pos;
358 showLocalCursor();
359 }
Pierre Ossman24ed17a2009-06-22 11:56:19 +0000360 updateWindow();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000361 }
362 }
363}
364
365
366// handleXEvent() handles the various X events on the window
367void DesktopWindow::handleEvent(TXWindow* w, XEvent* ev)
368{
369 switch (ev->type) {
370 case GraphicsExpose:
371 case Expose:
372 im->put(win(), gc, Rect(ev->xexpose.x, ev->xexpose.y,
373 ev->xexpose.x + ev->xexpose.width,
374 ev->xexpose.y + ev->xexpose.height));
375 break;
376
377 case MotionNotify:
378 while (XCheckTypedWindowEvent(dpy, win(), MotionNotify, ev));
379 if (viewport && viewport->bumpScrollEvent(&ev->xmotion)) break;
380 handlePointerEvent(Point(ev->xmotion.x, ev->xmotion.y),
381 (ev->xmotion.state & 0x1f00) >> 8);
382 break;
383
384 case ButtonPress:
385 handlePointerEvent(Point(ev->xbutton.x, ev->xbutton.y),
386 (((ev->xbutton.state & 0x1f00) >> 8) |
387 (1 << (ev->xbutton.button-1))));
388 break;
389
390 case ButtonRelease:
391 handlePointerEvent(Point(ev->xbutton.x, ev->xbutton.y),
392 (((ev->xbutton.state & 0x1f00) >> 8) &
393 ~(1 << (ev->xbutton.button-1))));
394 break;
395
396 case KeyPress:
397 if (!viewOnly) {
398 KeySym ks;
399 char keyname[256];
400 XLookupString(&ev->xkey, keyname, 256, &ks, NULL);
401 bool fakeShiftPress = false;
402
403 // Turn ISO_Left_Tab into shifted Tab
404 if (ks == XK_ISO_Left_Tab) {
405 fakeShiftPress = !(ev->xkey.state & ShiftMask);
406 ks = XK_Tab;
407 }
408
409 if (fakeShiftPress)
410 cc->writer()->keyEvent(XK_Shift_L, true);
411
412 downKeysym[ev->xkey.keycode] = ks;
413 cc->writer()->keyEvent(ks, true);
414
415 if (fakeShiftPress)
416 cc->writer()->keyEvent(XK_Shift_L, false);
417 break;
418 }
419
420 case KeyRelease:
421 if (!viewOnly) {
422 if (downKeysym[ev->xkey.keycode]) {
423 cc->writer()->keyEvent(downKeysym[ev->xkey.keycode], false);
424 downKeysym[ev->xkey.keycode] = 0;
425 }
426 }
427 break;
428
429 case EnterNotify:
430 newSelection = 0;
431 if (sendPrimary && !selectionOwner(XA_PRIMARY)) {
432 XConvertSelection(dpy, XA_PRIMARY, xaTIMESTAMP, xaSELECTION_TIME,
433 win(), ev->xcrossing.time);
434 } else if (!selectionOwner(xaCLIPBOARD)) {
435 XConvertSelection(dpy, xaCLIPBOARD, xaTIMESTAMP, xaSELECTION_TIME,
436 win(), ev->xcrossing.time);
437 }
438 break;
439
440 case LeaveNotify:
441 if (serverCutText_ && newServerCutText) {
442 newServerCutText = false;
443 vlog.debug("acquiring primary and clipboard selections");
444 XStoreBytes(dpy, serverCutText_, strlen(serverCutText_));
445 ownSelection(XA_PRIMARY, ev->xcrossing.time);
446 ownSelection(xaCLIPBOARD, ev->xcrossing.time);
447 currentSelectionTime = ev->xcrossing.time;
448 }
449 // Release all keys - this should probably done on a FocusOut event, but
450 // LeaveNotify is near enough...
451 for (int i = 8; i < 256; i++) {
452 if (downKeysym[i]) {
453 cc->writer()->keyEvent(downKeysym[i], false);
454 downKeysym[i] = 0;
455 }
456 }
457 break;
458 }
459}
460
461// selectionRequest() is called when we are the selection owner and another X
462// client has requested the selection. We simply put the server's cut text
463// into the requested property. TXWindow will handle the rest.
464bool DesktopWindow::selectionRequest(Window requestor,
465 Atom selection, Atom property)
466{
467 XChangeProperty(dpy, requestor, property, XA_STRING, 8,
468 PropModeReplace, (unsigned char*)serverCutText_,
469 strlen(serverCutText_));
470 return true;
471}
472
473
474// selectionNotify() is called when we have requested any information about a
475// selection from the selection owner. Note that there are two selections,
476// PRIMARY and CLIPBOARD, plus the cut buffer, and we try to use whichever is
477// the most recent of the three.
478//
479// There are two different "targets" for which selectionNotify() is called, the
480// timestamp and the actual string value of the selection. We always use the
481// timestamp to decide which selection to retrieve.
482//
483// The first time selectionNotify() is called is when we are trying to find the
484// timestamp of the selections at initialisation. This should be called first
485// for PRIMARY, then we call XConvertSelection() for CLIPBOARD. The second
486// time should be the result for CLIPBOARD. At this stage we've got the
487// "currentSelectionTime" so we return.
488//
489// Subsequently selectionNotify() is called whenever the mouse enters the
490// viewer window. Again, the first time it is called should be the timestamp
491// for PRIMARY, and we then request the timestamp for CLIPBOARD. When
492// selectionNotify() is called again with the timestamp for CLIPBOARD, we now
493// know if either selection is "new" i.e. later than the previous value of
494// currentSelectionTime. The last thing to check is the timestamp on the cut
495// buffer. If the cut buffer is newest we send that to the server, otherwise
496// if one of the selections was newer, we request the string value of that
497// selection.
498//
499// Finally, if we get selectionNotify() called for the string value of a
500// selection, we sent that to the server.
501//
502// As a final minor complication, when one of the selections is actually owned
503// by us, we don't request the details for it.
504
505// TIME_LATER treats 0 as meaning a long time ago, so a==0 means a cannot be
506// later than b. This is different to the usual meaning of CurrentTime.
507#define TIME_LATER(a, b) ((a) != 0 && ((b) == 0 || (long)((a) - (b)) > 0))
508
509
510void DesktopWindow::selectionNotify(XSelectionEvent* ev, Atom type, int format,
511 int nitems, void* data)
512{
513 if (ev->requestor != win())
514 return;
515
516 if (ev->target == xaTIMESTAMP) {
517 if (ev->property == xaSELECTION_TIME) {
518 if (data && format == 32 && nitems == 1) {
519 Time t = *(rdr::U32 *)data;
520 vlog.debug("selection (%d) time is %d, later %d",
521 ev->selection, t, TIME_LATER(t, currentSelectionTime));
522 if (TIME_LATER(t, currentSelectionTime)) {
523 currentSelectionTime = t;
524 newSelection = ev->selection;
525 }
526 }
527 } else {
528 vlog.debug("no selection (%d)",ev->selection);
529 }
530
531 if (ev->selection == XA_PRIMARY) {
532 if (!selectionOwner(xaCLIPBOARD)) {
533 XConvertSelection(dpy, xaCLIPBOARD, xaTIMESTAMP, xaSELECTION_TIME,
534 win(), ev->time);
535 return;
536 }
537 } else if (ev->selection != xaCLIPBOARD) {
538 vlog.error("unknown selection %d",ev->selection);
539 return;
540 }
541
542 if (gettingInitialSelectionTime) {
543 gettingInitialSelectionTime = false;
544 return;
545 }
546
547 if (!sendClipboard) return;
548 if (sendPrimary) {
549 vlog.debug("cut buffer time is %d, later %d", cutBufferTime,
550 TIME_LATER(cutBufferTime, currentSelectionTime));
551 if (TIME_LATER(cutBufferTime, currentSelectionTime)) {
552 currentSelectionTime = cutBufferTime;
553 int len;
554 char* str = XFetchBytes(dpy, &len);
555 if (str) {
556 if (!viewOnly) {
557 vlog.debug("sending cut buffer to server");
558 cc->writer()->clientCutText(str, len);
559 }
560 XFree(str);
561 return;
562 }
563 }
564 }
565 if (newSelection) {
566 XConvertSelection(dpy, newSelection, XA_STRING, xaSELECTION_STRING,
567 win(), CurrentTime);
568 }
569
570 } else if (ev->target == XA_STRING) {
571 if (!sendClipboard) return;
572 if (ev->property == xaSELECTION_STRING) {
573 if (data && format == 8) {
574 if (!viewOnly) {
575 vlog.debug("sending %s selection to server",
576 ev->selection == XA_PRIMARY ? "primary" :
577 ev->selection == xaCLIPBOARD ? "clipboard" : "unknown" );
578 cc->writer()->clientCutText((char*)data, nitems);
579 }
580 }
581 }
582 }
583}