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.24 by root, Wed Jan 25 00:42:21 2006 UTC vs.
Revision 1.34 by root, Mon Jan 30 02:21:20 2006 UTC

32#ifndef NO_SLOW_LINK_SUPPORT 32#ifndef NO_SLOW_LINK_SUPPORT
33# include <sys/socket.h> 33# include <sys/socket.h>
34# include <sys/un.h> 34# include <sys/un.h>
35#endif 35#endif
36 36
37#if XFT
38# include <X11/extensions/Xrender.h>
39#endif
40
41const char *const xa_names[] =
42{
43 "TEXT",
44 "COMPOUND_TEXT",
45 "UTF8_STRING",
46 "MULTIPLE",
47 "TARGETS",
48 "TIMESTAMP",
49 "VT_SELECTION",
50 "INCR",
51 "WM_PROTOCOLS",
52 "WM_DELETE_WINDOW",
53 "CLIPBOARD",
54 "AVERAGE_WIDTH",
55 "WEIGHT_NAME",
56 "SLANT",
57 "CHARSET_REGISTRY",
58 "CHARSET_ENCODING",
59#if ENABLE_FRILLS
60 "_MOTIF_WM_HINTS",
61#endif
62#if ENABLE_EWMH
63 "_NET_WM_PID",
64 "_NET_WM_NAME",
65 "_NET_WM_ICON_NAME",
66 "_NET_WM_PING",
67#endif
68#if USE_XIM
69 "WM_LOCALE_NAME",
70 "XIM_SERVERS",
71#endif
72#ifdef TRANSPARENT
73 "_XROOTPMAP_ID",
74 "ESETROOT_PMAP_ID",
75#endif
76#if ENABLE_XEMBED
77 "_XEMBED",
78 "_XEMBED_INFO",
79#endif
80#if !ENABLE_MINIMAL
81 "SCREEN_RESOURCES",
82 "XDCCC_LINEAR_RGB_CORRECTION",
83 "XDCCC_LINEAR_RGB_MATRICES",
84 "WM_COLORMAP_WINDOWS",
85 "WM_STATE",
86#endif
87};
88
89/////////////////////////////////////////////////////////////////////////////
90
37refcounted::refcounted (const char *id) 91refcounted::refcounted (const char *id)
38{ 92{
39 this->id = strdup (id); 93 this->id = strdup (id);
40} 94}
41 95
139 193
140#endif 194#endif
141 195
142///////////////////////////////////////////////////////////////////////////// 196/////////////////////////////////////////////////////////////////////////////
143 197
198void
199rxvt_screen::set (rxvt_display *disp)
200{
201 display = disp;
202 xdisp = disp->display;
203
204 Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
205
206 depth = DefaultDepthOfScreen (screen);
207 visual = DefaultVisualOfScreen (screen);
208 cmap = DefaultColormapOfScreen (screen);
209}
210
211void
212rxvt_screen::set (rxvt_display *disp, int bitdepth)
213{
214 set (disp);
215
216#if XFT
217 XVisualInfo vinfo;
218
219 if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo))
220 {
221 depth = bitdepth;
222 visual = vinfo.visual;
223 cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
224 }
225#endif
226}
227
228void
229rxvt_screen::clear ()
230{
231 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
232 XFreeColormap (xdisp, cmap);
233}
234
235/////////////////////////////////////////////////////////////////////////////
236
144rxvt_display::rxvt_display (const char *id) 237rxvt_display::rxvt_display (const char *id)
145: refcounted (id) 238: refcounted (id)
146, x_ev (this, &rxvt_display::x_cb) 239, x_ev (this, &rxvt_display::x_cb)
147, selection_owner (0) 240, selection_owner (0)
148{ 241{
249 if (!display) 342 if (!display)
250 return false; 343 return false;
251 344
252 screen = DefaultScreen (display); 345 screen = DefaultScreen (display);
253 root = DefaultRootWindow (display); 346 root = DefaultRootWindow (display);
254 visual = DefaultVisual (display, screen); 347
255 cmap = DefaultColormap (display, screen); 348 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
256 depth = DefaultDepth (display, screen); 349 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
257 350
258 XrmSetDatabase (display, get_resources ()); 351 XrmSetDatabase (display, get_resources ());
259 352
260#ifdef POINTER_BLANK 353#ifdef POINTER_BLANK
261 XColor blackcolour; 354 XColor blackcolour;
266 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', 359 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
267 &blackcolour, &blackcolour); 360 &blackcolour, &blackcolour);
268 XUnloadFont (display, f); 361 XUnloadFont (display, f);
269#endif 362#endif
270 363
271#ifdef PREFER_24BIT
272 /*
273 * If depth is not 24, look for a 24bit visual.
274 */
275 if (depth != 24)
276 {
277 XVisualInfo vinfo;
278
279 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
280 {
281 depth = 24;
282 visual = vinfo.visual;
283 cmap = XCreateColormap (display,
284 RootWindow (display, screen),
285 visual, AllocNone);
286 }
287 }
288#endif
289
290 int fd = XConnectionNumber (display); 364 int fd = XConnectionNumber (display);
291 365
292#ifndef NO_SLOW_LINK_SUPPORT 366#ifndef NO_SLOW_LINK_SUPPORT
293 // try to detect wether we have a local connection. 367 // try to detect wether we have a local connection.
294 // assume unix domains socket == local, everything else not 368 // assume unix domains socket == local, everything else not
303 377
304 x_ev.start (fd, EVENT_READ); 378 x_ev.start (fd, EVENT_READ);
305 fcntl (fd, F_SETFD, FD_CLOEXEC); 379 fcntl (fd, F_SETFD, FD_CLOEXEC);
306 380
307 XSelectInput (display, root, PropertyChangeMask); 381 XSelectInput (display, root, PropertyChangeMask);
308#ifdef USE_XIM
309 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
310#endif
311 382
312 flush (); 383 flush ();
313 384
314 return true; 385 return true;
315} 386}
351 // registers, as xlib crashes due to a race otherwise. 422 // registers, as xlib crashes due to a race otherwise.
352 Atom actual_type, *atoms; 423 Atom actual_type, *atoms;
353 int actual_format; 424 int actual_format;
354 unsigned long nitems, bytes_after; 425 unsigned long nitems, bytes_after;
355 426
356 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, 427 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
357 False, XA_ATOM, &actual_type, &actual_format, 428 False, XA_ATOM, &actual_type, &actual_format,
358 &nitems, &bytes_after, (unsigned char **)&atoms) 429 &nitems, &bytes_after, (unsigned char **)&atoms)
359 != Success ) 430 != Success )
360 return; 431 return;
361 432
381#ifdef USE_XIM 452#ifdef USE_XIM
382 if (!XFilterEvent (&xev, None)) 453 if (!XFilterEvent (&xev, None))
383 { 454 {
384 if (xev.type == PropertyNotify 455 if (xev.type == PropertyNotify
385 && xev.xany.window == root 456 && xev.xany.window == root
386 && xev.xproperty.atom == xa_xim_servers) 457 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
387 im_change_check (); 458 im_change_check ();
388#endif 459#endif
389 for (int i = xw.size (); i--; ) 460 for (int i = xw.size (); i--; )
390 { 461 {
391 if (!xw[i]) 462 if (!xw[i])
481refcache<rxvt_display> displays; 552refcache<rxvt_display> displays;
482 553
483///////////////////////////////////////////////////////////////////////////// 554/////////////////////////////////////////////////////////////////////////////
484 555
485bool 556bool
486rxvt_color::set (rxvt_display *display, Pixel p) 557rxvt_color::set (rxvt_screen *screen, const char *name)
487{ 558{
488#if XFT 559#if XFT
489 XColor xc; 560 int l = strlen (name);
561 rxvt_rgba r;
562 char eos;
563 int mult;
490 564
491 xc.pixel = p; 565 // shortcutting this saves countless server RTTs for the built-in colours
492 if (!XQueryColor (display->display, display->cmap, &xc)) 566 if (l == 3+3*3 && 3 == sscanf (name, "rgb:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos))
493 return false; 567 {
568 r.a = rxvt_rgba::MAX_CC;
569 mult = rxvt_rgba::MAX_CC / 0x00ff;
570 }
494 571
495 XRenderColor d; 572 // parse a number of non-standard ARGB colour specifications
573 else if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
574 mult = rxvt_rgba::MAX_CC / 0x000f;
575 else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
576 mult = rxvt_rgba::MAX_CC / 0x00ff;
577 else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
578 mult = rxvt_rgba::MAX_CC / 0xffff;
579 else if (l == 4+5*4 && 4 == sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos))
580 mult = rxvt_rgba::MAX_CC / 0xffff;
496 581
497 d.red = xc.red; 582 // slow case: server round trip
498 d.green = xc.green; 583 else
499 d.blue = xc.blue; 584 return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c);
500 d.alpha = 0xffff;
501 585
502 return 586 r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult;
503 XftColorAllocValue (display->display,
504 display->visual,
505 display->cmap,
506 &d, &c);
507#else
508 this->p = p;
509#endif
510 587
511 return true; 588 return set (screen, r);
512}
513
514bool
515rxvt_color::set (rxvt_display *display, const char *name)
516{
517#if XFT
518 return XftColorAllocName (display->display, display->visual, display->cmap,
519 name, &c);
520#else 589#else
521 XColor xc; 590 XColor xc;
522 591
523 if (XParseColor (display->display, display->cmap, name, &xc)) 592 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
524 return set (display, xc.red, xc.green, xc.blue); 593 return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue));
525 594
526 return false; 595 return false;
527#endif 596#endif
528} 597}
529 598
530bool 599bool
531rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) 600rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba)
532{ 601{
602#if XFT
603 XRenderPictFormat *format;
604
605 // FUCKING Xft gets it wrong, of course, so work around it
606 // transparency users should eat shit and die, and then
607 // XRenderQueryPictIndexValues themselves plenty.
608 if (screen->visual->c_class == TrueColor
609 && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual)))
610 {
611 // the fun lies in doing everything manually...
612 c.color.red = rgba.r;
613 c.color.green = rgba.g;
614 c.color.blue = rgba.b;
615 c.color.alpha = rgba.a;
616
617 c.pixel = ((rgba.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red )
618 | ((rgba.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green)
619 | ((rgba.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue )
620 | ((rgba.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha);
621
622 return true;
623 }
624 else
625 {
626 XRenderColor d;
627
628 d.red = rgba.r;
629 d.green = rgba.g;
630 d.blue = rgba.b;
631 d.alpha = rgba.a;
632
633 return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c);
634 }
635
636 return false;
637#else
533 XColor xc; 638 XColor xc;
534 639
535 xc.red = cr; 640 xc.red = rgba.r;
536 xc.green = cg; 641 xc.green = rgba.g;
537 xc.blue = cb; 642 xc.blue = rgba.b;
538 xc.flags = DoRed | DoGreen | DoBlue; 643 xc.flags = DoRed | DoGreen | DoBlue;
539 644
540 if (XAllocColor (display->display, display->cmap, &xc)) 645 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
541 return set (display, xc.pixel); 646 {
647 p = xc.pixel;
648 return true;
649 }
542 650
543 return false; 651 return false;
652#endif
544} 653}
545 654
546void 655void
547rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) 656rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba)
548{ 657{
549#if XFT 658#if XFT
550 cr = c.color.red; 659 rgba.r = c.color.red;
551 cg = c.color.green; 660 rgba.g = c.color.green;
552 cb = c.color.blue; 661 rgba.b = c.color.blue;
662 rgba.a = c.color.alpha;
553#else 663#else
554 XColor c; 664 XColor c;
555 665
556 c.pixel = p; 666 c.pixel = p;
557 XQueryColor (display->display, display->cmap, &c); 667 XQueryColor (screen->xdisp, screen->cmap, &c);
558 668
559 cr = c.red; 669 rgba.r = c.red;
560 cg = c.green; 670 rgba.g = c.green;
561 cb = c.blue; 671 rgba.b = c.blue;
672 rgba.a = rxvt_rgba::MAX_CC;
562#endif 673#endif
563} 674}
564 675
565void 676void
566rxvt_color::free (rxvt_display *display) 677rxvt_color::free (rxvt_screen *screen)
567{ 678{
568#if XFT 679#if XFT
569 XftColorFree (display->display, display->visual, display->cmap, &c); 680 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
570#else 681#else
571 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); 682 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
572#endif 683#endif
573} 684}
574 685
575rxvt_color 686rxvt_color
576rxvt_color::fade (rxvt_display *display, int percent) 687rxvt_color::fade (rxvt_screen *screen, int percent)
577{ 688{
578 percent = 100 - percent;
579
580 unsigned short cr, cg, cb;
581 rxvt_color faded; 689 rxvt_color faded;
582 690
583 get (display, cr, cg, cb); 691 rxvt_rgba c;
692 get (screen, c);
584 693
585 faded.set ( 694 c.r = lerp (0, c.r, percent);
586 display, 695 c.g = lerp (0, c.g, percent);
587 cr * percent / 100, 696 c.b = lerp (0, c.b, percent);
588 cg * percent / 100, 697
589 cb * percent / 100 698 faded.set (screen, c);
590 );
591 699
592 return faded; 700 return faded;
593} 701}
594 702
595#define LERP(a,b,p) (a * p + b * (100 - p)) / 100
596
597rxvt_color 703rxvt_color
598rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto) 704rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
599{ 705{
600 percent = 100 - percent; 706 rxvt_rgba c, fc;
601
602 unsigned short cr, cg, cb;
603 unsigned short fcr, fcg, fcb;
604 rxvt_color faded; 707 rxvt_color faded;
605 708
606 get (display, cr, cg, cb); 709 get (screen, c);
607 fadeto.get(display, fcr, fcg, fcb); 710 fadeto.get (screen, fc);
608 711
609 faded.set ( 712 faded.set (
610 display, 713 screen,
714 rxvt_rgba (
611 LERP (cr, fcr, percent), 715 lerp (fc.r, c.r, percent),
612 LERP (cg, fcg, percent), 716 lerp (fc.g, c.g, percent),
613 LERP (cb, fcb, percent) 717 lerp (fc.b, c.b, percent),
718 lerp (fc.a, c.a, percent)
719 )
614 ); 720 );
615 721
616 return faded; 722 return faded;
617} 723}
618 724

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines