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.68 by root, Tue Jun 26 00:40:19 2007 UTC vs.
Revision 1.147 by root, Wed Dec 17 13:16:18 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 ENABLE_TRANSPARENCY 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#ifdef HAVE_AFTERIMAGE 477 assert (ecb_array_length (xa_names) == NUM_XA);
449 ::dpy = dpy; /* init global var from libAfter... */
450 asv = create_asvisual_for_id (dpy, screen, DefaultDepth (dpy,screen),
451 XVisualIDFromVisual (DefaultVisual (dpy, screen)), None, NULL);
452#endif
453
454 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
455 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); 478 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa);
456 479
457 XrmSetDatabase (dpy, get_resources (false)); 480 XrmSetDatabase (dpy, get_resources (false));
458 481
459#ifdef POINTER_BLANK 482#ifdef POINTER_BLANK
465 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', 488 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ',
466 &blackcolour, &blackcolour); 489 &blackcolour, &blackcolour);
467 XUnloadFont (dpy, f); 490 XUnloadFont (dpy, f);
468#endif 491#endif
469 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
470 int fd = XConnectionNumber (dpy); 512 int fd = XConnectionNumber (dpy);
471 513
472 // try to detect wether we have a local connection. 514 // try to detect whether we have a local connection.
473 // assume unix domains socket == local, everything else not 515 // assume unix domain socket == local, everything else not
474 // TODO: might want to check for inet/127.0.0.1 516 // TODO: might want to check for inet/127.0.0.1
475 is_local = 0; 517 is_local = 0;
476 sockaddr_un sa; 518 sockaddr_un sa;
477 socklen_t sl = sizeof (sa); 519 socklen_t sl = sizeof (sa);
478 520
479 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 521 if (!getsockname (fd, (sockaddr *)&sa, &sl))
480 is_local = sa.sun_family == AF_UNIX; 522 is_local = sa.sun_family == AF_UNIX;
481 523
524 flush_ev.start ();
482 x_ev.start (fd, EVENT_READ); 525 x_ev.start (fd, ev::READ);
483 fcntl (fd, F_SETFD, FD_CLOEXEC); 526 fcntl (fd, F_SETFD, FD_CLOEXEC);
484 527
485 XSelectInput (dpy, root, PropertyChangeMask); 528 XSelectInput (dpy, root, PropertyChangeMask);
486 529
487 flush (); 530 flush ();
490} 533}
491 534
492void 535void
493rxvt_display::ref_next () 536rxvt_display::ref_next ()
494{ 537{
495 // TODO: somehow check wether the database files/resources changed 538 // TODO: somehow check whether the database files/resources changed
496 // before affording re-loading/parsing 539 // before affording re-loading/parsing
497 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
498 XrmSetDatabase (dpy, get_resources (true)); 545 XrmSetDatabase (dpy, get_resources (true));
499} 546}
500 547
501rxvt_display::~rxvt_display () 548rxvt_display::~rxvt_display ()
502{ 549{
505 552
506#ifdef POINTER_BLANK 553#ifdef POINTER_BLANK
507 XFreeCursor (dpy, blank_cursor); 554 XFreeCursor (dpy, blank_cursor);
508#endif 555#endif
509 x_ev.stop (); 556 x_ev.stop ();
557 flush_ev.stop ();
510#ifdef USE_XIM 558#if USE_XIM
511 xims.clear (); 559 xims.clear ();
512#endif 560#endif
561 XrmDestroyDatabase (XrmGetDatabase (dpy));
513 XCloseDisplay (dpy); 562 XCloseDisplay (dpy);
514} 563}
515 564
516#ifdef USE_XIM 565#if USE_XIM
517void rxvt_display::im_change_cb () 566void rxvt_display::im_change_cb ()
518{ 567{
519 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 568 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
520 (*i)->call (); 569 (*i)->call ();
521} 570}
529 unsigned long nitems, bytes_after; 578 unsigned long nitems, bytes_after;
530 579
531 if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, 580 if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
532 False, XA_ATOM, &actual_type, &actual_format, 581 False, XA_ATOM, &actual_type, &actual_format,
533 &nitems, &bytes_after, (unsigned char **)&atoms) 582 &nitems, &bytes_after, (unsigned char **)&atoms)
534 != Success ) 583 != Success)
535 return; 584 return;
536 585
537 if (actual_type == XA_ATOM && actual_format == 32) 586 if (actual_type == XA_ATOM && actual_format == 32)
538 for (int i = 0; i < nitems; i++) 587 for (int i = 0; i < nitems; i++)
539 if (XGetSelectionOwner (dpy, atoms[i])) 588 if (XGetSelectionOwner (dpy, atoms[i]))
540 { 589 {
541 im_change_cb (); 590 im_change_cb ();
542 break; 591 break;
544 593
545 XFree (atoms); 594 XFree (atoms);
546} 595}
547#endif 596#endif
548 597
549void rxvt_display::x_cb (io_watcher &w, short revents) 598void rxvt_display::x_cb (ev::io &w, int revents)
550{ 599{
600 flush_ev.start ();
601}
602
603void rxvt_display::flush_cb (ev::prepare &w, int revents)
604{
605 while (XEventsQueued (dpy, QueuedAfterFlush))
551 do 606 do
552 { 607 {
553 XEvent xev; 608 XEvent xev;
554 XNextEvent (dpy, &xev); 609 XNextEvent (dpy, &xev);
555 610
556#ifdef USE_XIM 611#if USE_XIM
557 if (!XFilterEvent (&xev, None)) 612 if (!XFilterEvent (&xev, None))
558 { 613 {
559 if (xev.type == PropertyNotify 614 if (xev.type == PropertyNotify
560 && xev.xany.window == root 615 && xev.xany.window == root
561 && xev.xproperty.atom == xa[XA_XIM_SERVERS]) 616 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
562 im_change_check (); 617 im_change_check ();
563#endif 618#endif
619 if (xev.type == MappingNotify)
620 XRefreshKeyboardMapping (&xev.xmapping);
621
564 for (int i = xw.size (); i--; ) 622 for (int i = xw.size (); i--; )
565 { 623 {
566 if (!xw[i]) 624 if (!xw[i])
567 xw.erase_unordered (i); 625 xw.erase_unordered (i);
568 else if (xw[i]->window == xev.xany.window) 626 else if (xw[i]->window == xev.xany.window)
569 xw[i]->call (xev); 627 xw[i]->call (xev);
570 } 628 }
571#ifdef USE_XIM 629#if USE_XIM
572 } 630 }
573#endif 631#endif
574 } 632 }
575 while (XEventsQueued (dpy, QueuedAlready)); 633 while (XEventsQueued (dpy, QueuedAlready));
576 634
577 XFlush (dpy); 635 w.stop ();
578}
579
580void rxvt_display::flush ()
581{
582 if (XEventsQueued (dpy, QueuedAlready))
583 x_cb (x_ev, EVENT_READ);
584
585 XFlush (dpy);
586} 636}
587 637
588void rxvt_display::reg (xevent_watcher *w) 638void rxvt_display::reg (xevent_watcher *w)
589{ 639{
590 if (!w->active) 640 if (!w->active)
601 xw[w->active - 1] = 0; 651 xw[w->active - 1] = 0;
602 w->active = 0; 652 w->active = 0;
603 } 653 }
604} 654}
605 655
606void rxvt_display::set_selection_owner (rxvt_term *owner) 656void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard)
607{ 657{
658 rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner;
659
608 if (selection_owner && selection_owner != owner) 660 if (cur_owner && cur_owner != owner)
609 selection_owner->selection_clear (); 661 {
662 rxvt_term *term = cur_owner;
663 term->selection_clear (clipboard);
664 term->flush ();
665 }
610 666
611 selection_owner = owner; 667 cur_owner = owner;
612} 668}
613 669
614#ifdef USE_XIM 670#if USE_XIM
615 671
616void rxvt_display::reg (im_watcher *w) 672void rxvt_display::reg (im_watcher *w)
617{ 673{
618 imw.push_back (w); 674 imw.push_back (w);
619} 675}
629 int l, m; 685 int l, m;
630 686
631 l = strlen (locale); 687 l = strlen (locale);
632 m = strlen (modifiers); 688 m = strlen (modifiers);
633 689
634 if (!(id = (char *)malloc (l + m + 2))) 690 if (!(id = rxvt_temp_buf<char> (l + m + 2)))
635 return 0; 691 return 0;
636 692
637 memcpy (id, locale, l); id[l] = '\n'; 693 memcpy (id, locale, l); id[l] = '\n';
638 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; 694 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
639 695
640 rxvt_xim *xim = xims.get (id); 696 rxvt_xim *xim = xims.get (id);
641
642 free (id);
643 697
644 return xim; 698 return xim;
645} 699}
646 700
647void rxvt_display::put_xim (rxvt_xim *xim) 701void rxvt_display::put_xim (rxvt_xim *xim)
656Atom rxvt_display::atom (const char *name) 710Atom rxvt_display::atom (const char *name)
657{ 711{
658 return XInternAtom (dpy, name, False); 712 return XInternAtom (dpy, name, False);
659} 713}
660 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
661///////////////////////////////////////////////////////////////////////////// 737/////////////////////////////////////////////////////////////////////////////
662 738
663template class refcache<rxvt_display>; 739template class refcache<rxvt_display>;
664refcache<rxvt_display> displays; 740refcache<rxvt_display> displays;
665 741
669static unsigned int 745static unsigned int
670insert_component (unsigned int value, unsigned int mask, unsigned int shift) 746insert_component (unsigned int value, unsigned int mask, unsigned int shift)
671{ 747{
672 return (value * (mask + 1) >> 16) << shift; 748 return (value * (mask + 1) >> 16) << shift;
673} 749}
674 750
675bool 751bool
676rxvt_color::alloc (rxvt_screen *screen, const rgba &color) 752rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
677{ 753{
754 //TODO: only supports 24 bit
755 unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a;
756
678#if XFT 757#if XFT
679 XRenderPictFormat *format; 758 XRenderPictFormat *format;
680 759
681 // FUCKING Xft gets it wrong, of course, so work around it. 760 // FUCKING Xft gets it wrong, of course, so work around it.
682 // Transparency users should eat shit and die, and then 761 // Transparency users should eat shit and die, and then
686 { 765 {
687 // the fun lies in doing everything manually... 766 // the fun lies in doing everything manually...
688 c.color.red = color.r; 767 c.color.red = color.r;
689 c.color.green = color.g; 768 c.color.green = color.g;
690 c.color.blue = color.b; 769 c.color.blue = color.b;
691 c.color.alpha = color.a; 770 c.color.alpha = alpha;
692 771
772 // Xft wants premultiplied alpha, but abuses the alpha channel
773 // as blend factor, and doesn't allow us to set the alpha channel
774 c.color.red = c.color.red * alpha / 0xffff;
775 c.color.green = c.color.green * alpha / 0xffff;
776 c.color.blue = c.color.blue * alpha / 0xffff;
777
693 c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red ) 778 c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red )
694 | insert_component (color.g, format->direct.greenMask, format->direct.green) 779 | insert_component (c.color.green, format->direct.greenMask, format->direct.green)
695 | insert_component (color.b, format->direct.blueMask , format->direct.blue ) 780 | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue )
696 | insert_component (color.a, format->direct.alphaMask, format->direct.alpha); 781 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha);
697 782
698 return true; 783 return true;
699 } 784 }
700 else 785 else
701 { 786 {
702 XRenderColor d; 787 XRenderColor d;
703 788
704 d.red = color.r; 789 d.red = color.r;
705 d.green = color.g; 790 d.green = color.g;
706 d.blue = color.b; 791 d.blue = color.b;
707 d.alpha = color.a; 792 d.alpha = alpha;
708 793
794 // XftColorAlloc always returns 100% transparent pixels(!)
709 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); 795 if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c))
796 return true;
710 } 797 }
711#else 798#else
712 c.red = color.r; 799 c.red = color.r;
713 c.green = color.g; 800 c.green = color.g;
714 c.blue = color.b; 801 c.blue = color.b;
715 802
716 if (screen->visual->c_class == TrueColor) 803 if (screen->visual->c_class == TrueColor)
717 { 804 {
718 c.pixel = (color.r >> (16 - popcount (screen->visual->red_mask )) << ctz (screen->visual->red_mask )) 805 c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask ))
719 | (color.g >> (16 - popcount (screen->visual->green_mask)) << ctz (screen->visual->green_mask)) 806 | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask))
720 | (color.b >> (16 - popcount (screen->visual->blue_mask )) << ctz (screen->visual->blue_mask )); 807 | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask ));
721 808
722 return true; 809 return true;
723 } 810 }
724 else if (XAllocColor (screen->dpy, screen->cmap, &c)) 811 else if (XAllocColor (screen->dpy, screen->cmap, &c))
725 return true; 812 return true;
726 else 813#endif
814
815 //TODO: set c.color* or c.*
727 c.pixel = (color.r + color.g + color.b) > 128*3 816 c.pixel = (color.r * 2 + color.g * 3 + color.b) >= 0x8000 * 6
728 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) 817 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy))
729 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); 818 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy));
730#endif
731 819
732 return false; 820 return false;
733} 821}
734 822
735bool 823bool
737{ 825{
738 rgba c; 826 rgba c;
739 char eos; 827 char eos;
740 int skip; 828 int skip;
741 829
830 c.a = rgba::MAX_CC;
831
742 // parse the nonstandard "[alphapercent]" prefix 832 // parse the nonstandard "[alphapercent]" prefix
743 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) 833 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
744 { 834 {
745 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); 835 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a);
746 name += skip; 836 name += skip;
747 } 837 }
748 else
749 c.a = rgba::MAX_CC;
750 838
751 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format 839 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
752 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) 840 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
753 { 841 {
754 XColor xc, xc_exact; 842 XColor xc;
755 843
756 if (XParseColor (screen->dpy, screen->cmap, name, &xc)) 844 if (XParseColor (screen->dpy, screen->cmap, name, &xc))
757 { 845 {
758 c.r = xc.red; 846 c.r = xc.red;
759 c.g = xc.green; 847 c.g = xc.green;
786 { 874 {
787 XColor *colors = new XColor [screen->visual->map_entries]; 875 XColor *colors = new XColor [screen->visual->map_entries];
788 876
789 for (int i = 0; i < cmap_size; i++) 877 for (int i = 0; i < cmap_size; i++)
790 colors [i].pixel = i; 878 colors [i].pixel = i;
791 879
792 // many kilobytes transfer per colour, but pseudocolor isn't worth 880 // many kilobytes transfer per colour, but pseudocolor isn't worth
793 // many extra optimisations. 881 // many extra optimisations.
794 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); 882 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
795 883
884 while (cmap_size)
885 {
796 int diff = 0x7fffffffUL; 886 int diff = 0x7fffffffL;
797 XColor *best = colors; 887 XColor *best = colors;
798 888
799 for (int i = 0; i < cmap_size; i++) 889 for (int i = 0; i < cmap_size; i++)
800 {
801 int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2))
802 + (squared_diff<int> (color.g >> 2, colors [i].green >> 2))
803 + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2));
804
805 if (d < diff)
806 { 890 {
891 // simple weighted rgb distance sucks, but keeps it simple
892 int d = abs (color.r - colors [i].red ) * 2
893 + abs (color.g - colors [i].green) * 3
894 + abs (color.b - colors [i].blue );
895
896 if (d < diff)
897 {
807 diff = d; 898 diff = d;
808 best = colors + i; 899 best = colors + i;
900 }
809 } 901 }
810 }
811 902
812 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", 903 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d,%d)\n",
813 // color.r, color.g, color.b, best->red, best->green, best->blue, diff); 904 // color.r, color.g, color.b, best->red, best->green, best->blue, diff, best - colors);
814 905
815 got = alloc (screen, rgba (best->red, best->green, best->blue)); 906 got = alloc (screen, rgba (best->red, best->green, best->blue));
907
908 if (got)
909 break;
910
911 *best = colors [--cmap_size];
912 }
816 913
817 delete [] colors; 914 delete [] colors;
818 } 915 }
819#endif 916#endif
820 917
821 return got; 918 return got;
822} 919}
823 920
824void 921void
825rxvt_color::get (rgba &color) 922rxvt_color::get (rgba &color) const
826{ 923{
827#if XFT 924#if XFT
925 //TODO premultiplied alpha??
828 color.r = c.color.red; 926 color.r = c.color.red;
829 color.g = c.color.green; 927 color.g = c.color.green;
830 color.b = c.color.blue; 928 color.b = c.color.blue;
831 color.a = c.color.alpha; 929 color.a = c.color.alpha;
832#else 930#else
836 color.a = rgba::MAX_CC; 934 color.a = rgba::MAX_CC;
837#endif 935#endif
838} 936}
839 937
840void 938void
841rxvt_color::get (XColor &color) 939rxvt_color::get (XColor &color) const
842{ 940{
843 rgba c; 941 rgba c;
844 get (c); 942 get (c);
845 943
846 color.red = c.r; 944 color.red = c.r;
847 color.green = c.g; 945 color.green = c.g;
848 color.blue = c.b; 946 color.blue = c.b;
849 color.pixel = (Pixel)*this; 947 color.pixel = (Pixel)*this;
850} 948}
851 949
852void 950void
853rxvt_color::free (rxvt_screen *screen) 951rxvt_color::free (rxvt_screen *screen)
854{ 952{
855 if (screen->visual->c_class == TrueColor) 953 if (screen->visual->c_class == TrueColor)
856 return; // nothing to do 954 return; // nothing to do
857 955
877 lerp (c.a, to.a, percent) 975 lerp (c.a, to.a, percent)
878 ) 976 )
879 ); 977 );
880} 978}
881 979
980rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term)
981: display (disp), request_time (tm), request_win (win), request_prop (prop), term (term)
982{
983 assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard);
984
985 timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this);
986 timer_ev.repeat = 10.;
987 x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this);
988
989 incr_buf = 0;
990 incr_buf_size = incr_buf_fill = 0;
991 selection_wait = Sel_normal;
992 selection_type = selnum;
993 cb_sv = 0;
994}
995
996void
997rxvt_selection::stop ()
998{
999 free (incr_buf);
1000 incr_buf = 0;
1001 timer_ev.stop ();
1002 x_ev.stop (display);
1003}
1004
1005rxvt_selection::~rxvt_selection ()
1006{
1007 stop ();
1008}
1009
1010void
1011rxvt_selection::run ()
1012{
1013 int selnum = selection_type;
1014
1015#if ENABLE_FRILLS
1016 if (selnum == Sel_Primary && display->selection_owner)
1017 {
1018 /* internal selection */
1019 char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len);
1020 finish (str, strlen (str));
1021 free (str);
1022 return;
1023 }
1024#endif
1025
1026#if X_HAVE_UTF8_STRING
1027 selection_type = Sel_UTF8String;
1028 if (request (display->xa[XA_UTF8_STRING], selnum))
1029 return;
1030#else
1031 selection_type = Sel_CompoundText;
1032 if (request (display->xa[XA_COMPOUND_TEXT], selnum))
1033 return;
1034#endif
1035
1036 // fallback to CUT_BUFFER0 if the requested property has no owner
1037 handle_selection (display->root, XA_CUT_BUFFER0, false);
1038}
1039
1040void
1041rxvt_selection::finish (char *data, unsigned int len)
1042{
1043 if (!cb_sv)
1044 {
1045 if (data)
1046 term->paste (data, len);
1047
1048 term->selection_req = 0;
1049 delete this;
1050 }
1051#if ENABLE_PERL
1052 else
1053 {
1054 stop (); // we do not really trust perl callbacks
1055 rxvt_perl.selection_finish (this, data, len);
1056 }
1057#endif
1058}
1059
1060bool
1061rxvt_selection::request (Atom target, int selnum)
1062{
1063 Atom sel;
1064
1065 selection_type |= selnum;
1066
1067 if (selnum == Sel_Primary)
1068 sel = XA_PRIMARY;
1069 else if (selnum == Sel_Secondary)
1070 sel = XA_SECONDARY;
1071 else
1072 sel = display->xa[XA_CLIPBOARD];
1073
1074 if (XGetSelectionOwner (display->dpy, sel) != None)
1075 {
1076 XConvertSelection (display->dpy, sel, target, request_prop,
1077 request_win, request_time);
1078 x_ev.start (display, request_win);
1079 timer_ev.again ();
1080 return true;
1081 }
1082
1083 return false;
1084}
1085
1086void
1087rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop)
1088{
1089 Display *dpy = display->dpy;
1090 char *data = 0;
1091 unsigned int data_len = 0;
1092 unsigned long bytes_after;
1093 XTextProperty ct;
1094
1095 // check for failed XConvertSelection
1096 if (prop == None)
1097 {
1098 bool error = true;
1099 int selnum = selection_type & Sel_whereMask;
1100
1101 if (selection_type & Sel_CompoundText)
1102 {
1103 selection_type = 0;
1104 error = !request (XA_STRING, selnum);
1105 }
1106
1107 if (selection_type & Sel_UTF8String)
1108 {
1109 selection_type = Sel_CompoundText;
1110 error = !request (display->xa[XA_COMPOUND_TEXT], selnum);
1111 }
1112
1113 if (error)
1114 {
1115 ct.value = 0;
1116 goto bailout;
1117 }
1118
1119 return;
1120 }
1121
1122 // length == (2^31 - 1) / 4, as gdk
1123 if (XGetWindowProperty (dpy, win, prop,
1124 0, 0x1fffffff,
1125 delete_prop, AnyPropertyType,
1126 &ct.encoding, &ct.format,
1127 &ct.nitems, &bytes_after,
1128 &ct.value) != Success)
1129 {
1130 ct.value = 0;
1131 goto bailout;
1132 }
1133
1134 if (ct.encoding == None)
1135 goto bailout;
1136
1137 if (ct.value == 0)
1138 goto bailout;
1139
1140 if (ct.encoding == display->xa[XA_INCR])
1141 {
1142 // INCR selection, start handshake
1143 if (!delete_prop)
1144 XDeleteProperty (dpy, win, prop);
1145
1146 selection_wait = Sel_incr;
1147 timer_ev.again ();
1148
1149 goto bailout;
1150 }
1151
1152 if (ct.nitems == 0)
1153 {
1154 if (selection_wait == Sel_incr)
1155 {
1156 XFree (ct.value);
1157
1158 // finally complete, now paste the whole thing
1159 selection_wait = Sel_normal;
1160 ct.value = (unsigned char *)incr_buf;
1161 ct.nitems = incr_buf_fill;
1162 incr_buf = 0;
1163 timer_ev.stop ();
1164 }
1165 else
1166 {
1167 // avoid recursion
1168 if (win != display->root || prop != XA_CUT_BUFFER0)
1169 {
1170 XFree (ct.value);
1171
1172 // fallback to CUT_BUFFER0 if the requested property
1173 // has an owner but is empty
1174 handle_selection (display->root, XA_CUT_BUFFER0, False);
1175 return;
1176 }
1177
1178 goto bailout;
1179 }
1180 }
1181 else if (selection_wait == Sel_incr)
1182 {
1183 timer_ev.again ();
1184
1185 while (incr_buf_fill + ct.nitems > incr_buf_size)
1186 {
1187 incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024;
1188 incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size);
1189 }
1190
1191 memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems);
1192 incr_buf_fill += ct.nitems;
1193
1194 goto bailout;
1195 }
1196
1197 char **cl;
1198 int cr;
1199
1200 // we honour the first item only
1201
1202#if !ENABLE_MINIMAL
1203 // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it
1204 // so recode it manually
1205 if (ct.encoding == display->xa[XA_UTF8_STRING])
1206 {
1207 wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems);
1208 data = rxvt_wcstombs (w);
1209 free (w);
1210 }
1211 else
1212#endif
1213 if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0
1214 && cl)
1215 {
1216 data = strdup (cl[0]);
1217 XFreeStringList (cl);
1218 }
1219 else
1220 {
1221 // paste raw
1222 data = strdup ((const char *)ct.value);
1223 }
1224
1225 data_len = strlen (data);
1226
1227bailout:
1228 XFree (ct.value);
1229
1230 if (selection_wait == Sel_normal)
1231 {
1232 finish (data, data_len);
1233 free (data);
1234 }
1235}
1236
1237void
1238rxvt_selection::timer_cb (ev::timer &w, int revents)
1239{
1240 if (selection_wait == Sel_incr)
1241 rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n");
1242
1243 finish ();
1244}
1245
1246void
1247rxvt_selection::x_cb (XEvent &xev)
1248{
1249 switch (xev.type)
1250 {
1251 case PropertyNotify:
1252 if (selection_wait == Sel_incr
1253 && xev.xproperty.atom == request_prop
1254 && xev.xproperty.state == PropertyNewValue)
1255 handle_selection (xev.xproperty.window, xev.xproperty.atom, true);
1256 break;
1257
1258 case SelectionNotify:
1259 if (selection_wait == Sel_normal
1260 && xev.xselection.time == request_time)
1261 {
1262 timer_ev.stop ();
1263 handle_selection (xev.xselection.requestor, xev.xselection.property, true);
1264 }
1265 break;
1266 }
1267}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines