ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
(Generate patch)

Comparing rxvt-unicode/src/rxvttoolkit.C (file contents):
Revision 1.42 by root, Tue Jan 31 16:57:35 2006 UTC vs.
Revision 1.146 by root, Sun Dec 14 04:52:10 2014 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*----------------------------------------------------------------------*
2 * File: rxvttoolkit.C 2 * File: rxvttoolkit.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> 6 * Copyright (c) 2003-2011 Marc Lehmann <schmorp@schmorp.de>
7 * Copyright (c) 2011 Emanuele Giaquinta <e.giaquinta@glauco.it>
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version. 12 * (at your option) any later version.
12 * 13 *
13 * This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 23
23#include "../config.h" 24#include "../config.h"
24#include <rxvt.h> 25#include <rxvt.h>
25#include <rxvttoolkit.h> 26#include <rxvttoolkit.h>
26 27
28#include <stdlib.h>
29
27#include <unistd.h> 30#include <unistd.h>
28#include <fcntl.h> 31#include <fcntl.h>
29 32
30#include <sys/utsname.h> 33#include <sys/utsname.h>
31
32#ifndef NO_SLOW_LINK_SUPPORT
33# include <sys/socket.h> 34#include <sys/socket.h>
34# include <sys/un.h> 35#include <sys/un.h>
35#endif
36 36
37#if XFT 37#if XFT
38# include <X11/extensions/Xrender.h> 38# include <X11/extensions/Xrender.h>
39#endif 39#endif
40 40
41const char *const xa_names[] = 41static const char *const xa_names[] =
42{ 42{
43 "TEXT", 43 "TEXT",
44 "COMPOUND_TEXT", 44 "COMPOUND_TEXT",
45 "UTF8_STRING", 45 "UTF8_STRING",
46 "MULTIPLE", 46 "MULTIPLE",
62#if ENABLE_EWMH 62#if ENABLE_EWMH
63 "_NET_WM_PID", 63 "_NET_WM_PID",
64 "_NET_WM_NAME", 64 "_NET_WM_NAME",
65 "_NET_WM_ICON_NAME", 65 "_NET_WM_ICON_NAME",
66 "_NET_WM_PING", 66 "_NET_WM_PING",
67 "_NET_WM_ICON",
67#endif 68#endif
68#if USE_XIM 69#if USE_XIM
69 "WM_LOCALE_NAME", 70 "WM_LOCALE_NAME",
70 "XIM_SERVERS", 71 "XIM_SERVERS",
71#endif 72#endif
72#ifdef TRANSPARENT 73#if HAVE_IMG || ENABLE_PERL
73 "_XROOTPMAP_ID", 74 "_XROOTPMAP_ID",
74 "ESETROOT_PMAP_ID", 75 "ESETROOT_PMAP_ID",
75#endif 76#endif
76#if ENABLE_XEMBED 77#if ENABLE_XEMBED
77 "_XEMBED", 78 "_XEMBED",
154 put (*this->begin ()); 155 put (*this->begin ());
155} 156}
156 157
157///////////////////////////////////////////////////////////////////////////// 158/////////////////////////////////////////////////////////////////////////////
158 159
159#ifdef USE_XIM 160#if USE_XIM
160 161
161static void 162static void
162#if XIMCB_PROTO_BROKEN 163#if XIMCB_PROTO_BROKEN
163im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) 164im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
164#else 165#else
177bool 178bool
178rxvt_xim::ref_init () 179rxvt_xim::ref_init ()
179{ 180{
180 display = GET_R->display; //HACK: TODO 181 display = GET_R->display; //HACK: TODO
181 182
182 xim = XOpenIM (display->display, NULL, NULL, NULL); 183 xim = XOpenIM (display->dpy, 0, 0, 0);
183 184
184 if (!xim) 185 if (!xim)
185 return false; 186 return false;
186 187
187 XIMCallback ximcallback; 188 XIMCallback ximcallback;
188 ximcallback.client_data = (XPointer)this; 189 ximcallback.client_data = (XPointer)this;
189 ximcallback.callback = im_destroy_cb; 190 ximcallback.callback = im_destroy_cb;
190 191
191 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); 192 XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0);
192 193
193 return true; 194 return true;
194} 195}
195 196
196rxvt_xim::~rxvt_xim () 197rxvt_xim::~rxvt_xim ()
201 202
202#endif 203#endif
203 204
204///////////////////////////////////////////////////////////////////////////// 205/////////////////////////////////////////////////////////////////////////////
205 206
207#if XFT
208rxvt_drawable::~rxvt_drawable ()
209{
210 if (xftdrawable)
211 XftDrawDestroy (xftdrawable);
212}
213
214rxvt_drawable::operator XftDraw *()
215{
216 if (!xftdrawable)
217 xftdrawable = XftDrawCreate (screen->dpy, drawable, screen->visual, screen->cmap);
218
219 return xftdrawable;
220}
221#endif
222
223/////////////////////////////////////////////////////////////////////////////
224
225#if XFT
226
227// not strictly necessary as it is only used with superclass of zero_initialised
228rxvt_screen::rxvt_screen ()
229: scratch_area (0)
230{
231}
232
233rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h)
234{
235 if (!scratch_area || w > scratch_w || h > scratch_h)
236 {
237 if (scratch_area)
238 {
239 XFreePixmap (dpy, scratch_area->drawable);
240 delete scratch_area;
241 }
242
243 Pixmap pm = XCreatePixmap (dpy, RootWindowOfScreen (ScreenOfDisplay (dpy, display->screen)),
244 scratch_w = w, scratch_h = h, depth);
245
246 scratch_area = new rxvt_drawable (this, pm);
247 }
248
249 return *scratch_area;
250}
251
252#endif
253
206void 254void
207rxvt_screen::set (rxvt_display *disp) 255rxvt_screen::set (rxvt_display *disp)
208{ 256{
209 display = disp; 257 display = disp;
210 xdisp = disp->display; 258 dpy = disp->dpy;
211 259
212 Screen *screen = ScreenOfDisplay (xdisp, disp->screen); 260 Screen *screen = ScreenOfDisplay (dpy, disp->screen);
213 261
214 depth = DefaultDepthOfScreen (screen); 262 depth = DefaultDepthOfScreen (screen);
215 visual = DefaultVisualOfScreen (screen); 263 visual = DefaultVisualOfScreen (screen);
216 cmap = DefaultColormapOfScreen (screen); 264 cmap = DefaultColormapOfScreen (screen);
217} 265}
218 266
219void 267#if ENABLE_FRILLS
220rxvt_screen::set (rxvt_display *disp, int bitdepth)
221{
222 set (disp);
223 268
269void
270rxvt_screen::select_visual (int id)
271{
272 XVisualInfo vinfo;
273 vinfo.visualid = id;
274 int n;
275
276 if (XVisualInfo *vi = XGetVisualInfo (dpy, VisualIDMask, &vinfo, &n))
277 {
278 depth = vi->depth;
279 visual = vi->visual;
280
281 XFree (vi);
282
283 cmap = XCreateColormap (dpy, display->root, visual, AllocNone);
284 }
285 else
286 rxvt_warn ("no visual found for requested id 0x%02x, using default visual.\n", id);
287}
288
289void
290rxvt_screen::select_depth (int bitdepth)
291{
292 XVisualInfo vinfo;
293
294 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo))
295 select_visual (vinfo.visualid);
296 else
297 rxvt_warn ("no visual found for requested depth %d, using default visual.\n", bitdepth);
298}
299
300#endif
301
302void
303rxvt_screen::clear ()
304{
224#if XFT 305#if XFT
225 XVisualInfo vinfo; 306 if (scratch_area)
226
227 if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo))
228 {
229 depth = bitdepth;
230 visual = vinfo.visual;
231 cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
232 } 307 {
308 XFreePixmap (dpy, scratch_area->drawable);
309 delete scratch_area;
310 }
233#endif 311#endif
234}
235 312
236void
237rxvt_screen::clear ()
238{
239 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen))) 313 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (dpy, display->screen)))
240 XFreeColormap (xdisp, cmap); 314 XFreeColormap (dpy, cmap);
241} 315}
242 316
243///////////////////////////////////////////////////////////////////////////// 317/////////////////////////////////////////////////////////////////////////////
244 318
245rxvt_display::rxvt_display (const char *id) 319rxvt_display::rxvt_display (const char *id)
246: refcounted (id) 320: refcounted (id)
247, x_ev (this, &rxvt_display::x_cb)
248, selection_owner (0) 321, selection_owner (0)
322, clipboard_owner (0)
249{ 323{
324 x_ev .set<rxvt_display, &rxvt_display::x_cb > (this);
325 flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this);
250} 326}
251 327
252XrmDatabase 328XrmDatabase
253rxvt_display::get_resources () 329rxvt_display::get_resources (bool refresh)
254{ 330{
255 char *homedir = (char *)getenv ("HOME"); 331 char *homedir = getenv ("HOME");
256 char fname[1024]; 332 char fname[1024];
257 333
258 /* 334 /*
259 * get resources using the X library function 335 * get resources using the X library function
260 */ 336 */
261 char *displayResource, *xe; 337 char *displayResource, *xe;
262 XrmDatabase database, rdb1; 338 XrmDatabase rdb1, database = 0;
263 339
264 database = NULL; 340#if !XLIB_ILLEGAL_ACCESS
341 /* work around a bug in XrmSetDatabase where it frees the db, see ref_next */
342 database = XrmGetStringDatabase ("");
343#endif
265 344
266 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 345 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
346 // as opposed to "standard practise", we always read in ~/.Xdefaults
267 347
268 // 6. System wide per application default file. 348 // 6. System wide per application default file.
269 349
270 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ 350 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
271 if ((xe = (char *)getenv ("XAPPLRESDIR"))) 351 if ((xe = getenv ("XAPPLRESDIR")))
272 { 352 {
273 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); 353 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
274 354
275 if ((rdb1 = XrmGetFileDatabase (fname))) 355 if ((rdb1 = XrmGetFileDatabase (fname)))
276 XrmMergeDatabases (rdb1, &database); 356 XrmMergeDatabases (rdb1, &database);
278 358
279 // 5. User's per application default file. 359 // 5. User's per application default file.
280 // none 360 // none
281 361
282 // 4. User's defaults file. 362 // 4. User's defaults file.
363 if (homedir)
364 {
365 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
366
367 if ((rdb1 = XrmGetFileDatabase (fname)))
368 XrmMergeDatabases (rdb1, &database);
369 }
370
283 /* Get any Xserver defaults */ 371 /* Get any Xserver defaults */
372 if (refresh)
373 {
374 // fucking xlib keeps a copy of the rm string
375 Atom actual_type;
376 int actual_format;
377 unsigned long nitems, nremaining;
378 char *val = 0;
379
380#if XLIB_ILLEGAL_ACCESS
381 if (dpy->xdefaults)
382 XFree (dpy->xdefaults);
383#endif
384
385 if (XGetWindowProperty (dpy, RootWindow (dpy, 0), XA_RESOURCE_MANAGER,
386 0L, 100000000L, False,
387 XA_STRING, &actual_type, &actual_format,
388 &nitems, &nremaining,
389 (unsigned char **)&val) == Success
390 && actual_type == XA_STRING
391 && actual_format == 8)
392 displayResource = val;
393 else
394 {
395 displayResource = 0;
396
397 if (val)
398 XFree (val);
399 }
400
401#if XLIB_ILLEGAL_ACCESS
402 dpy->xdefaults = displayResource;
403#endif
404 }
405 else
284 displayResource = XResourceManagerString (display); 406 displayResource = XResourceManagerString (dpy);
285 407
286 if (displayResource != NULL) 408 if (displayResource)
287 { 409 {
288 if ((rdb1 = XrmGetStringDatabase (displayResource))) 410 if ((rdb1 = XrmGetStringDatabase (displayResource)))
289 XrmMergeDatabases (rdb1, &database); 411 XrmMergeDatabases (rdb1, &database);
290 } 412 }
291 else if (homedir)
292 {
293 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
294 413
295 if ((rdb1 = XrmGetFileDatabase (fname))) 414#if !XLIB_ILLEGAL_ACCESS
296 XrmMergeDatabases (rdb1, &database); 415 if (refresh && displayResource)
297 } 416 XFree (displayResource);
417#endif
298 418
299 /* Get screen specific resources */ 419 /* Get screen specific resources */
300 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); 420 displayResource = XScreenResourceString (ScreenOfDisplay (dpy, screen));
301 421
302 if (displayResource != NULL) 422 if (displayResource)
303 { 423 {
304 if ((rdb1 = XrmGetStringDatabase (displayResource))) 424 if ((rdb1 = XrmGetStringDatabase (displayResource)))
305 /* Merge with screen-independent resources */ 425 /* Merge with screen-independent resources */
306 XrmMergeDatabases (rdb1, &database); 426 XrmMergeDatabases (rdb1, &database);
307 427
308 XFree (displayResource); 428 XFree (displayResource);
309 } 429 }
310 430
311 // 3. User's per host defaults file 431 // 3. User's per host defaults file
312 /* Add in XENVIRONMENT file */ 432 /* Add in XENVIRONMENT file */
313 if ((xe = (char *)getenv ("XENVIRONMENT")) 433 if ((xe = getenv ("XENVIRONMENT"))
314 && (rdb1 = XrmGetFileDatabase (xe))) 434 && (rdb1 = XrmGetFileDatabase (xe)))
315 XrmMergeDatabases (rdb1, &database); 435 XrmMergeDatabases (rdb1, &database);
316 else if (homedir) 436 else if (homedir)
317 { 437 {
318 struct utsname un; 438 struct utsname un;
332bool rxvt_display::ref_init () 452bool rxvt_display::ref_init ()
333{ 453{
334#ifdef LOCAL_X_IS_UNIX 454#ifdef LOCAL_X_IS_UNIX
335 if (id[0] == ':') 455 if (id[0] == ':')
336 { 456 {
337 val = rxvt_malloc (5 + strlen (id) + 1); 457 char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1);
458
338 strcpy (val, "unix/"); 459 strcpy (val, "unix/");
339 strcat (val, id); 460 strcat (val, id);
461
340 display = XOpenDisplay (val); 462 dpy = XOpenDisplay (val);
341 free (val);
342 } 463 }
343 else 464 else
344#endif 465#endif
345 display = 0; 466 dpy = 0;
346 467
347 if (!display) 468 if (!dpy)
348 display = XOpenDisplay (id); 469 dpy = XOpenDisplay (id);
349 470
350 if (!display) 471 if (!dpy)
351 return false; 472 return false;
352 473
353 screen = DefaultScreen (display); 474 screen = DefaultScreen (dpy);
354 root = DefaultRootWindow (display); 475 root = DefaultRootWindow (dpy);
355 476
356 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); 477 assert (ecb_array_length (xa_names) == NUM_XA);
357 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); 478 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa);
358 479
359 XrmSetDatabase (display, get_resources ()); 480 XrmSetDatabase (dpy, get_resources (false));
360 481
361#ifdef POINTER_BLANK 482#ifdef POINTER_BLANK
362 XColor blackcolour; 483 XColor blackcolour;
363 blackcolour.red = 0; 484 blackcolour.red = 0;
364 blackcolour.green = 0; 485 blackcolour.green = 0;
365 blackcolour.blue = 0; 486 blackcolour.blue = 0;
366 Font f = XLoadFont (display, "fixed"); 487 Font f = XLoadFont (dpy, "fixed");
367 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', 488 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ',
368 &blackcolour, &blackcolour); 489 &blackcolour, &blackcolour);
369 XUnloadFont (display, f); 490 XUnloadFont (dpy, f);
370#endif 491#endif
371 492
493 flags = 0;
494#if XRENDER
495 int major, minor;
496 if (XRenderQueryVersion (dpy, &major, &minor))
497 if (major > 0 || (major == 0 && minor >= 11))
498 {
499 flags |= DISPLAY_HAS_RENDER;
500
501 if (XFilters *filters = XRenderQueryFilters (dpy, root))
502 {
503 for (int i = 0; i < filters->nfilter; i++)
504 if (!strcmp (filters->filter [i], FilterConvolution))
505 flags |= DISPLAY_HAS_RENDER_CONV;
506
507 XFree (filters);
508 }
509 }
510#endif
511
372 int fd = XConnectionNumber (display); 512 int fd = XConnectionNumber (dpy);
373 513
374#ifndef NO_SLOW_LINK_SUPPORT
375 // try to detect wether we have a local connection. 514 // try to detect whether we have a local connection.
376 // assume unix domains socket == local, everything else not 515 // assume unix domain socket == local, everything else not
377 // TODO: might want to check for inet/127.0.0.1 516 // TODO: might want to check for inet/127.0.0.1
378 is_local = 0; 517 is_local = 0;
379 sockaddr_un sa; 518 sockaddr_un sa;
380 socklen_t sl = sizeof (sa); 519 socklen_t sl = sizeof (sa);
381 520
382 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 521 if (!getsockname (fd, (sockaddr *)&sa, &sl))
383 is_local = sa.sun_family == AF_LOCAL; 522 is_local = sa.sun_family == AF_UNIX;
384#endif
385 523
524 flush_ev.start ();
386 x_ev.start (fd, EVENT_READ); 525 x_ev.start (fd, ev::READ);
387 fcntl (fd, F_SETFD, FD_CLOEXEC); 526 fcntl (fd, F_SETFD, FD_CLOEXEC);
388 527
389 XSelectInput (display, root, PropertyChangeMask); 528 XSelectInput (dpy, root, PropertyChangeMask);
390 529
391 flush (); 530 flush ();
392 531
393 return true; 532 return true;
394} 533}
395 534
396void 535void
397rxvt_display::ref_next () 536rxvt_display::ref_next ()
398{ 537{
399 // TODO: somehow check wether the database files/resources changed 538 // TODO: somehow check whether the database files/resources changed
400 // before re-loading/parsing 539 // before affording re-loading/parsing
401 XrmDestroyDatabase (XrmGetDatabase (display)); 540 XrmDestroyDatabase (XrmGetDatabase (dpy));
541#if XLIB_ILLEGAL_ACCESS
542 /* work around a bug in XrmSetDatabase where it frees the db */
543 dpy->db = 0;
544#endif
402 XrmSetDatabase (display, get_resources ()); 545 XrmSetDatabase (dpy, get_resources (true));
403} 546}
404 547
405rxvt_display::~rxvt_display () 548rxvt_display::~rxvt_display ()
406{ 549{
407 if (!display) 550 if (!dpy)
408 return; 551 return;
409 552
410#ifdef POINTER_BLANK 553#ifdef POINTER_BLANK
411 XFreeCursor (display, blank_cursor); 554 XFreeCursor (dpy, blank_cursor);
412#endif 555#endif
413 x_ev.stop (); 556 x_ev.stop ();
557 flush_ev.stop ();
414#ifdef USE_XIM 558#if USE_XIM
415 xims.clear (); 559 xims.clear ();
416#endif 560#endif
561 XrmDestroyDatabase (XrmGetDatabase (dpy));
417 XCloseDisplay (display); 562 XCloseDisplay (dpy);
418} 563}
419 564
420#ifdef USE_XIM 565#if USE_XIM
421void rxvt_display::im_change_cb () 566void rxvt_display::im_change_cb ()
422{ 567{
423 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 568 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
424 (*i)->call (); 569 (*i)->call ();
425} 570}
430 // registers, as xlib crashes due to a race otherwise. 575 // registers, as xlib crashes due to a race otherwise.
431 Atom actual_type, *atoms; 576 Atom actual_type, *atoms;
432 int actual_format; 577 int actual_format;
433 unsigned long nitems, bytes_after; 578 unsigned long nitems, bytes_after;
434 579
435 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L, 580 if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
436 False, XA_ATOM, &actual_type, &actual_format, 581 False, XA_ATOM, &actual_type, &actual_format,
437 &nitems, &bytes_after, (unsigned char **)&atoms) 582 &nitems, &bytes_after, (unsigned char **)&atoms)
438 != Success ) 583 != Success)
439 return; 584 return;
440 585
441 if (actual_type == XA_ATOM && actual_format == 32) 586 if (actual_type == XA_ATOM && actual_format == 32)
442 for (int i = 0; i < nitems; i++) 587 for (int i = 0; i < nitems; i++)
443 if (XGetSelectionOwner (display, atoms[i])) 588 if (XGetSelectionOwner (dpy, atoms[i]))
444 { 589 {
445 im_change_cb (); 590 im_change_cb ();
446 break; 591 break;
447 } 592 }
448 593
449 XFree (atoms); 594 XFree (atoms);
450} 595}
451#endif 596#endif
452 597
453void rxvt_display::x_cb (io_watcher &w, short revents) 598void rxvt_display::x_cb (ev::io &w, int revents)
454{ 599{
600 flush_ev.start ();
601}
602
603void rxvt_display::flush_cb (ev::prepare &w, int revents)
604{
605 while (XEventsQueued (dpy, QueuedAfterFlush))
455 do 606 do
456 { 607 {
457 XEvent xev; 608 XEvent xev;
458 XNextEvent (display, &xev); 609 XNextEvent (dpy, &xev);
459 610
460#ifdef USE_XIM 611#if USE_XIM
461 if (!XFilterEvent (&xev, None)) 612 if (!XFilterEvent (&xev, None))
462 { 613 {
463 if (xev.type == PropertyNotify 614 if (xev.type == PropertyNotify
464 && xev.xany.window == root 615 && xev.xany.window == root
465 && xev.xproperty.atom == xa[XA_XIM_SERVERS]) 616 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
466 im_change_check (); 617 im_change_check ();
467#endif 618#endif
619 if (xev.type == MappingNotify)
620 XRefreshKeyboardMapping (&xev.xmapping);
621
468 for (int i = xw.size (); i--; ) 622 for (int i = xw.size (); i--; )
469 { 623 {
470 if (!xw[i]) 624 if (!xw[i])
471 xw.erase_unordered (i); 625 xw.erase_unordered (i);
472 else if (xw[i]->window == xev.xany.window) 626 else if (xw[i]->window == xev.xany.window)
473 xw[i]->call (xev); 627 xw[i]->call (xev);
474 } 628 }
475#ifdef USE_XIM 629#if USE_XIM
476 } 630 }
477#endif 631#endif
478 } 632 }
479 while (XEventsQueued (display, QueuedAlready)); 633 while (XEventsQueued (dpy, QueuedAlready));
480 634
481 XFlush (display); 635 w.stop ();
482}
483
484void rxvt_display::flush ()
485{
486 if (XEventsQueued (display, QueuedAlready))
487 x_cb (x_ev, EVENT_READ);
488
489 XFlush (display);
490} 636}
491 637
492void rxvt_display::reg (xevent_watcher *w) 638void rxvt_display::reg (xevent_watcher *w)
493{ 639{
640 if (!w->active)
641 {
494 xw.push_back (w); 642 xw.push_back (w);
495 w->active = xw.size (); 643 w->active = xw.size ();
644 }
496} 645}
497 646
498void rxvt_display::unreg (xevent_watcher *w) 647void rxvt_display::unreg (xevent_watcher *w)
499{ 648{
500 if (w->active) 649 if (w->active)
650 {
501 xw[w->active - 1] = 0; 651 xw[w->active - 1] = 0;
652 w->active = 0;
653 }
502} 654}
503 655
504void rxvt_display::set_selection_owner (rxvt_term *owner) 656void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard)
505{ 657{
658 rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner;
659
506 if (selection_owner && selection_owner != owner) 660 if (cur_owner && cur_owner != owner)
507 selection_owner->selection_clear (); 661 {
662 rxvt_term *term = cur_owner;
663 term->selection_clear (clipboard);
664 term->flush ();
665 }
508 666
509 selection_owner = owner; 667 cur_owner = owner;
510} 668}
511 669
512#ifdef USE_XIM 670#if USE_XIM
671
513void rxvt_display::reg (im_watcher *w) 672void rxvt_display::reg (im_watcher *w)
514{ 673{
515 imw.push_back (w); 674 imw.push_back (w);
516} 675}
517 676
526 int l, m; 685 int l, m;
527 686
528 l = strlen (locale); 687 l = strlen (locale);
529 m = strlen (modifiers); 688 m = strlen (modifiers);
530 689
531 if (!(id = (char *)malloc (l + m + 2))) 690 if (!(id = rxvt_temp_buf<char> (l + m + 2)))
532 return 0; 691 return 0;
533 692
534 memcpy (id, locale, l); id[l] = '\n'; 693 memcpy (id, locale, l); id[l] = '\n';
535 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; 694 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
536 695
537 rxvt_xim *xim = xims.get (id); 696 rxvt_xim *xim = xims.get (id);
538 697
539 free (id);
540
541 return xim; 698 return xim;
542} 699}
543 700
544void rxvt_display::put_xim (rxvt_xim *xim) 701void rxvt_display::put_xim (rxvt_xim *xim)
545{ 702{
546#if XLIB_IS_RACEFREE 703# if XLIB_IS_RACEFREE
547 xims.put (xim); 704 xims.put (xim);
548#endif 705# endif
549} 706}
707
550#endif 708#endif
551 709
552Atom rxvt_display::atom (const char *name) 710Atom rxvt_display::atom (const char *name)
553{ 711{
554 return XInternAtom (display, name, False); 712 return XInternAtom (dpy, name, False);
713}
714
715Pixmap
716rxvt_display::get_pixmap_property (Atom property)
717{
718 Pixmap pixmap = None;
719
720 int aformat;
721 unsigned long nitems, bytes_after;
722 Atom atype;
723 unsigned char *prop;
724 int result = XGetWindowProperty (dpy, root, property,
725 0L, 1L, False, XA_PIXMAP, &atype, &aformat,
726 &nitems, &bytes_after, &prop);
727 if (result == Success)
728 {
729 if (atype == XA_PIXMAP)
730 pixmap = *(Pixmap *)prop;
731 XFree (prop);
732 }
733
734 return pixmap;
555} 735}
556 736
557///////////////////////////////////////////////////////////////////////////// 737/////////////////////////////////////////////////////////////////////////////
558 738
559template class refcache<rxvt_display>; 739template class refcache<rxvt_display>;
560refcache<rxvt_display> displays; 740refcache<rxvt_display> displays;
561 741
562///////////////////////////////////////////////////////////////////////////// 742/////////////////////////////////////////////////////////////////////////////
563 743//
744
745static unsigned int
746insert_component (unsigned int value, unsigned int mask, unsigned int shift)
747{
748 return (value * (mask + 1) >> 16) << shift;
749}
750
564bool 751bool
565rxvt_color::alloc (rxvt_screen *screen, const rxvt_rgba &color) 752rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
566{ 753{
754 //TODO: only supports 24 bit
755 unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a;
756
567#if XFT 757#if XFT
568 XRenderPictFormat *format; 758 XRenderPictFormat *format;
569 759
570 // FUCKING Xft gets it wrong, of course, so work around it 760 // FUCKING Xft gets it wrong, of course, so work around it.
571 // transparency users should eat shit and die, and then 761 // Transparency users should eat shit and die, and then
572 // XRenderQueryPictIndexValues themselves plenty. 762 // XRenderQueryPictIndexValues themselves plenty.
573 if ((screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) 763 if ((screen->visual->c_class == TrueColor)
574 && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) 764 && (format = XRenderFindVisualFormat (screen->dpy, screen->visual)))
575 { 765 {
576 // the fun lies in doing everything manually... 766 // the fun lies in doing everything manually...
577 c.color.red = color.r; 767 c.color.red = color.r;
578 c.color.green = color.g; 768 c.color.green = color.g;
579 c.color.blue = color.b; 769 c.color.blue = color.b;
580 c.color.alpha = color.a; 770 c.color.alpha = alpha;
581 771
582 c.pixel = ((color.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red ) 772 // Xft wants premultiplied alpha, but abuses the alpha channel
583 | ((color.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green) 773 // as blend factor, and doesn't allow us to set the alpha channel
584 | ((color.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue ) 774 c.color.red = c.color.red * alpha / 0xffff;
585 | ((color.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha); 775 c.color.green = c.color.green * alpha / 0xffff;
776 c.color.blue = c.color.blue * alpha / 0xffff;
777
778 c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red )
779 | insert_component (c.color.green, format->direct.greenMask, format->direct.green)
780 | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue )
781 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha);
586 782
587 return true; 783 return true;
588 } 784 }
589 else 785 else
590 { 786 {
591 XRenderColor d; 787 XRenderColor d;
592 788
593 d.red = color.r; 789 d.red = color.r;
594 d.green = color.g; 790 d.green = color.g;
595 d.blue = color.b; 791 d.blue = color.b;
596 d.alpha = color.a; 792 d.alpha = alpha;
597 793
598 return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); 794 if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c))
795 return true;
599 } 796 }
600#else 797#else
601 if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) 798 c.red = color.r;
799 c.green = color.g;
800 c.blue = color.b;
801
802 if (screen->visual->c_class == TrueColor)
602 { 803 {
603 p = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) 804 c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask ))
604 / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ) 805 | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask))
605 | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask))
606 / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask)
607 | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) 806 | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask ));
608 / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask );
609 807
610 return true; 808 return true;
611 } 809 }
612 else
613 {
614 XColor xc;
615
616 xc.red = color.r;
617 xc.green = color.g;
618 xc.blue = color.b;
619
620 if (XAllocColor (screen->xdisp, screen->cmap, &xc)) 810 else if (XAllocColor (screen->dpy, screen->cmap, &c))
621 {
622 p = xc.pixel;
623 return true; 811 return true;
624 } 812#endif
625 else 813
626 p = (color.r + color.g + color.b) > 128*3 814 //TODO: set c.color* or c.*
815 c.pixel = (color.r * 2 + color.g * 3 + color.b) >= 0x8000 * 6
627 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) 816 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy))
628 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); 817 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy));
629 }
630#endif
631 818
632 return false; 819 return false;
633} 820}
634 821
635bool 822bool
636rxvt_color::set (rxvt_screen *screen, const char *name) 823rxvt_color::set (rxvt_screen *screen, const char *name)
637{ 824{
638 rxvt_rgba c; 825 rgba c;
639 char eos; 826 char eos;
640 int skip; 827 int skip;
641 828
829 c.a = rgba::MAX_CC;
830
831 // parse the nonstandard "[alphapercent]" prefix
642 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) 832 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
643 { 833 {
644 printf ("X %d\n", c.a);
645 c.a = lerp<int, int, int> (0, rxvt_rgba::MAX_CC, c.a); 834 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a);
646 name += skip; 835 name += skip;
647 printf ("y %04x\n", c.a);
648 } 836 }
649 else
650 c.a = rxvt_rgba::MAX_CC;
651 837
652 // parse the non-standard rgba format 838 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
653 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) 839 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
654 { 840 {
655 XColor xc, xc_exact; 841 XColor xc;
656 842
657 if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) 843 if (XParseColor (screen->dpy, screen->cmap, name, &xc))
658 { 844 {
659 c.r = xc.red; 845 c.r = xc.red;
660 c.g = xc.green; 846 c.g = xc.green;
661 c.b = xc.blue; 847 c.b = xc.blue;
662 } 848 }
672 858
673 return set (screen, c); 859 return set (screen, c);
674} 860}
675 861
676bool 862bool
677rxvt_color::set (rxvt_screen *screen, const rxvt_rgba &color) 863rxvt_color::set (rxvt_screen *screen, const rgba &color)
678{ 864{
679 bool got = alloc (screen, color); 865 bool got = alloc (screen, color);
680 866
681#if !ENABLE_MINIMAL 867#if !ENABLE_MINIMAL
682 int cmap_size = screen->visual->map_entries; 868 int cmap_size = screen->visual->map_entries;
687 { 873 {
688 XColor *colors = new XColor [screen->visual->map_entries]; 874 XColor *colors = new XColor [screen->visual->map_entries];
689 875
690 for (int i = 0; i < cmap_size; i++) 876 for (int i = 0; i < cmap_size; i++)
691 colors [i].pixel = i; 877 colors [i].pixel = i;
692 878
879 // many kilobytes transfer per colour, but pseudocolor isn't worth
880 // many extra optimisations.
693 XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); 881 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
694 882
883 while (cmap_size)
884 {
695 int diff = 0x7fffffffUL; 885 int diff = 0x7fffffffL;
696 XColor *best = colors; 886 XColor *best = colors;
697 887
698 for (int i = 0; i < cmap_size; i++) 888 for (int i = 0; i < cmap_size; i++)
699 {
700 int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2))
701 + (squared_diff<int> (color.g >> 2, colors [i].green >> 2))
702 + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2));
703
704 if (d < diff)
705 { 889 {
890 // simple weighted rgb distance sucks, but keeps it simple
891 int d = abs (color.r - colors [i].red ) * 2
892 + abs (color.g - colors [i].green) * 3
893 + abs (color.b - colors [i].blue );
894
895 if (d < diff)
896 {
706 diff = d; 897 diff = d;
707 best = colors + i; 898 best = colors + i;
899 }
708 } 900 }
709 }
710 901
711 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", 902 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d,%d)\n",
712 // color.r, color.g, color.b, best->red, best->green, best->blue, diff); 903 // color.r, color.g, color.b, best->red, best->green, best->blue, diff, best - colors);
713 904
714 got = alloc (screen, rxvt_rgba (best->red, best->green, best->blue)); 905 got = alloc (screen, rgba (best->red, best->green, best->blue));
715 906
907 if (got)
908 break;
909
910 *best = colors [--cmap_size];
911 }
912
716 delete colors; 913 delete [] colors;
717 } 914 }
718#endif 915#endif
719 916
720 return got; 917 return got;
721} 918}
722 919
723void 920void
724rxvt_color::get (rxvt_screen *screen, rxvt_rgba &color) 921rxvt_color::get (rgba &color) const
725{ 922{
726#if XFT 923#if XFT
924 //TODO premultiplied alpha??
727 color.r = c.color.red; 925 color.r = c.color.red;
728 color.g = c.color.green; 926 color.g = c.color.green;
729 color.b = c.color.blue; 927 color.b = c.color.blue;
730 color.a = c.color.alpha; 928 color.a = c.color.alpha;
731#else 929#else
732 XColor c;
733
734 c.pixel = p;
735 XQueryColor (screen->xdisp, screen->cmap, &c);
736
737 color.r = c.red; 930 color.r = c.red;
738 color.g = c.green; 931 color.g = c.green;
739 color.b = c.blue; 932 color.b = c.blue;
740 color.a = rxvt_rgba::MAX_CC; 933 color.a = rgba::MAX_CC;
741#endif 934#endif
742} 935}
743 936
744void 937void
938rxvt_color::get (XColor &color) const
939{
940 rgba c;
941 get (c);
942
943 color.red = c.r;
944 color.green = c.g;
945 color.blue = c.b;
946 color.pixel = (Pixel)*this;
947}
948
949void
745rxvt_color::free (rxvt_screen *screen) 950rxvt_color::free (rxvt_screen *screen)
746{ 951{
952 if (screen->visual->c_class == TrueColor)
953 return; // nothing to do
954
747#if XFT 955#if XFT
748 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); 956 XftColorFree (screen->dpy, screen->visual, screen->cmap, &c);
749#else 957#else
750 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); 958 XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes);
751#endif 959#endif
752} 960}
753 961
754void 962void
755rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rxvt_rgba &to) 963rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to)
756{ 964{
757 rxvt_rgba c; 965 rgba c;
758 get (screen, c); 966 get (c);
759 967
760 result.set ( 968 result.set (
761 screen, 969 screen,
762 rxvt_rgba ( 970 rgba (
763 lerp (c.r, to.r, percent), 971 lerp (c.r, to.r, percent),
764 lerp (c.g, to.g, percent), 972 lerp (c.g, to.g, percent),
765 lerp (c.b, to.b, percent), 973 lerp (c.b, to.b, percent),
766 lerp (c.a, to.a, percent) 974 lerp (c.a, to.a, percent)
767 ) 975 )
768 ); 976 );
769} 977}
770 978
979rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term)
980: display (disp), request_time (tm), request_win (win), request_prop (prop), term (term)
981{
982 assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard);
983
984 timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this);
985 timer_ev.repeat = 10.;
986 x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this);
987
988 incr_buf = 0;
989 incr_buf_size = incr_buf_fill = 0;
990 selection_wait = Sel_normal;
991 selection_type = selnum;
992 cb_sv = 0;
993}
994
995void
996rxvt_selection::stop ()
997{
998 free (incr_buf);
999 incr_buf = 0;
1000 timer_ev.stop ();
1001 x_ev.stop (display);
1002}
1003
1004rxvt_selection::~rxvt_selection ()
1005{
1006 stop ();
1007}
1008
1009void
1010rxvt_selection::run ()
1011{
1012 int selnum = selection_type;
1013
1014#if ENABLE_FRILLS
1015 if (selnum == Sel_Primary && display->selection_owner)
1016 {
1017 /* internal selection */
1018 char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len);
1019 finish (str, strlen (str));
1020 free (str);
1021 return;
1022 }
1023#endif
1024
1025#if X_HAVE_UTF8_STRING
1026 selection_type = Sel_UTF8String;
1027 if (request (display->xa[XA_UTF8_STRING], selnum))
1028 return;
1029#else
1030 selection_type = Sel_CompoundText;
1031 if (request (display->xa[XA_COMPOUND_TEXT], selnum))
1032 return;
1033#endif
1034
1035 // fallback to CUT_BUFFER0 if the requested property has no owner
1036 handle_selection (display->root, XA_CUT_BUFFER0, false);
1037}
1038
1039void
1040rxvt_selection::finish (char *data, unsigned int len)
1041{
1042 if (!cb_sv)
1043 {
1044 if (data)
1045 term->paste (data, len);
1046
1047 term->selection_req = 0;
1048 delete this;
1049 }
1050#if ENABLE_PERL
1051 else
1052 {
1053 stop (); // we do not really trust perl callbacks
1054 rxvt_perl.selection_finish (this, data, len);
1055 }
1056#endif
1057}
1058
1059bool
1060rxvt_selection::request (Atom target, int selnum)
1061{
1062 Atom sel;
1063
1064 selection_type |= selnum;
1065
1066 if (selnum == Sel_Primary)
1067 sel = XA_PRIMARY;
1068 else if (selnum == Sel_Secondary)
1069 sel = XA_SECONDARY;
1070 else
1071 sel = display->xa[XA_CLIPBOARD];
1072
1073 if (XGetSelectionOwner (display->dpy, sel) != None)
1074 {
1075 XConvertSelection (display->dpy, sel, target, request_prop,
1076 request_win, request_time);
1077 x_ev.start (display, request_win);
1078 timer_ev.again ();
1079 return true;
1080 }
1081
1082 return false;
1083}
1084
1085void
1086rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop)
1087{
1088 Display *dpy = display->dpy;
1089 char *data = 0;
1090 unsigned int data_len = 0;
1091 unsigned long bytes_after;
1092 XTextProperty ct;
1093
1094 // check for failed XConvertSelection
1095 if (prop == None)
1096 {
1097 bool error = true;
1098 int selnum = selection_type & Sel_whereMask;
1099
1100 if (selection_type & Sel_CompoundText)
1101 {
1102 selection_type = 0;
1103 error = !request (XA_STRING, selnum);
1104 }
1105
1106 if (selection_type & Sel_UTF8String)
1107 {
1108 selection_type = Sel_CompoundText;
1109 error = !request (display->xa[XA_COMPOUND_TEXT], selnum);
1110 }
1111
1112 if (error)
1113 {
1114 ct.value = 0;
1115 goto bailout;
1116 }
1117
1118 return;
1119 }
1120
1121 // length == (2^31 - 1) / 4, as gdk
1122 if (XGetWindowProperty (dpy, win, prop,
1123 0, 0x1fffffff,
1124 delete_prop, AnyPropertyType,
1125 &ct.encoding, &ct.format,
1126 &ct.nitems, &bytes_after,
1127 &ct.value) != Success)
1128 {
1129 ct.value = 0;
1130 goto bailout;
1131 }
1132
1133 if (ct.encoding == None)
1134 goto bailout;
1135
1136 if (ct.value == 0)
1137 goto bailout;
1138
1139 if (ct.encoding == display->xa[XA_INCR])
1140 {
1141 // INCR selection, start handshake
1142 if (!delete_prop)
1143 XDeleteProperty (dpy, win, prop);
1144
1145 selection_wait = Sel_incr;
1146 timer_ev.again ();
1147
1148 goto bailout;
1149 }
1150
1151 if (ct.nitems == 0)
1152 {
1153 if (selection_wait == Sel_incr)
1154 {
1155 XFree (ct.value);
1156
1157 // finally complete, now paste the whole thing
1158 selection_wait = Sel_normal;
1159 ct.value = (unsigned char *)incr_buf;
1160 ct.nitems = incr_buf_fill;
1161 incr_buf = 0;
1162 timer_ev.stop ();
1163 }
1164 else
1165 {
1166 // avoid recursion
1167 if (win != display->root || prop != XA_CUT_BUFFER0)
1168 {
1169 XFree (ct.value);
1170
1171 // fallback to CUT_BUFFER0 if the requested property
1172 // has an owner but is empty
1173 handle_selection (display->root, XA_CUT_BUFFER0, False);
1174 return;
1175 }
1176
1177 goto bailout;
1178 }
1179 }
1180 else if (selection_wait == Sel_incr)
1181 {
1182 timer_ev.again ();
1183
1184 while (incr_buf_fill + ct.nitems > incr_buf_size)
1185 {
1186 incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024;
1187 incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size);
1188 }
1189
1190 memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems);
1191 incr_buf_fill += ct.nitems;
1192
1193 goto bailout;
1194 }
1195
1196 char **cl;
1197 int cr;
1198
1199 // we honour the first item only
1200
1201#if !ENABLE_MINIMAL
1202 // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it
1203 // so recode it manually
1204 if (ct.encoding == display->xa[XA_UTF8_STRING])
1205 {
1206 wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems);
1207 data = rxvt_wcstombs (w);
1208 free (w);
1209 }
1210 else
1211#endif
1212 if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0
1213 && cl)
1214 {
1215 data = strdup (cl[0]);
1216 XFreeStringList (cl);
1217 }
1218 else
1219 {
1220 // paste raw
1221 data = strdup ((const char *)ct.value);
1222 }
1223
1224 data_len = strlen (data);
1225
1226bailout:
1227 XFree (ct.value);
1228
1229 if (selection_wait == Sel_normal)
1230 {
1231 finish (data, data_len);
1232 free (data);
1233 }
1234}
1235
1236void
1237rxvt_selection::timer_cb (ev::timer &w, int revents)
1238{
1239 if (selection_wait == Sel_incr)
1240 rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n");
1241
1242 finish ();
1243}
1244
1245void
1246rxvt_selection::x_cb (XEvent &xev)
1247{
1248 switch (xev.type)
1249 {
1250 case PropertyNotify:
1251 if (selection_wait == Sel_incr
1252 && xev.xproperty.atom == request_prop
1253 && xev.xproperty.state == PropertyNewValue)
1254 handle_selection (xev.xproperty.window, xev.xproperty.atom, true);
1255 break;
1256
1257 case SelectionNotify:
1258 if (selection_wait == Sel_normal
1259 && xev.xselection.time == request_time)
1260 {
1261 timer_ev.stop ();
1262 handle_selection (xev.xselection.requestor, xev.xselection.property, true);
1263 }
1264 break;
1265 }
1266}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines