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.61 by root, Mon Aug 7 15:12:48 2006 UTC vs.
Revision 1.148 by root, Wed Dec 17 14:25:01 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines