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.31 by root, Sun Jan 29 22:27:04 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#if ENABLE_FRILLS
55 "_MOTIF_WM_HINTS",
56#endif
57#if ENABLE_EWMH
58 "_NET_WM_PID",
59 "_NET_WM_NAME",
60 "_NET_WM_ICON_NAME",
61 "_NET_WM_PING",
62#endif
63#if USE_XIM
64 "WM_LOCALE_NAME",
65 "XIM_SERVERS",
66#endif
67#ifdef TRANSPARENT
68 "_XROOTPMAP_ID",
69 "ESETROOT_PMAP_ID",
70#endif
71#if ENABLE_XEMBED
72 "_XEMBED",
73 "_XEMBED_INFO",
74#endif
75 };
76
77/////////////////////////////////////////////////////////////////////////////
78
37refcounted::refcounted (const char *id) 79refcounted::refcounted (const char *id)
38{ 80{
39 this->id = strdup (id); 81 this->id = strdup (id);
40} 82}
41 83
139 181
140#endif 182#endif
141 183
142///////////////////////////////////////////////////////////////////////////// 184/////////////////////////////////////////////////////////////////////////////
143 185
186void
187rxvt_screen::set (rxvt_display *disp)
188{
189 display = disp;
190 xdisp = disp->display;
191
192 Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
193
194 depth = DefaultDepthOfScreen (screen);
195 visual = DefaultVisualOfScreen (screen);
196 cmap = DefaultColormapOfScreen (screen);
197}
198
199void
200rxvt_screen::set (rxvt_display *disp, int bitdepth)
201{
202 set (disp);
203
204#if XFT
205 XVisualInfo vinfo;
206
207 if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo))
208 {
209 depth = bitdepth;
210 visual = vinfo.visual;
211 cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
212 }
213#endif
214}
215
216void
217rxvt_screen::clear ()
218{
219 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
220 XFreeColormap (xdisp, cmap);
221}
222
223/////////////////////////////////////////////////////////////////////////////
224
144rxvt_display::rxvt_display (const char *id) 225rxvt_display::rxvt_display (const char *id)
145: refcounted (id) 226: refcounted (id)
146, x_ev (this, &rxvt_display::x_cb) 227, x_ev (this, &rxvt_display::x_cb)
147, selection_owner (0) 228, selection_owner (0)
148{ 229{
249 if (!display) 330 if (!display)
250 return false; 331 return false;
251 332
252 screen = DefaultScreen (display); 333 screen = DefaultScreen (display);
253 root = DefaultRootWindow (display); 334 root = DefaultRootWindow (display);
254 visual = DefaultVisual (display, screen); 335
255 cmap = DefaultColormap (display, screen); 336 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
256 depth = DefaultDepth (display, screen); 337 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
257 338
258 XrmSetDatabase (display, get_resources ()); 339 XrmSetDatabase (display, get_resources ());
259 340
260#ifdef POINTER_BLANK 341#ifdef POINTER_BLANK
261 XColor blackcolour; 342 XColor blackcolour;
266 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', 347 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
267 &blackcolour, &blackcolour); 348 &blackcolour, &blackcolour);
268 XUnloadFont (display, f); 349 XUnloadFont (display, f);
269#endif 350#endif
270 351
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); 352 int fd = XConnectionNumber (display);
291 353
292#ifndef NO_SLOW_LINK_SUPPORT 354#ifndef NO_SLOW_LINK_SUPPORT
293 // try to detect wether we have a local connection. 355 // try to detect wether we have a local connection.
294 // assume unix domains socket == local, everything else not 356 // assume unix domains socket == local, everything else not
303 365
304 x_ev.start (fd, EVENT_READ); 366 x_ev.start (fd, EVENT_READ);
305 fcntl (fd, F_SETFD, FD_CLOEXEC); 367 fcntl (fd, F_SETFD, FD_CLOEXEC);
306 368
307 XSelectInput (display, root, PropertyChangeMask); 369 XSelectInput (display, root, PropertyChangeMask);
308#ifdef USE_XIM
309 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
310#endif
311 370
312 flush (); 371 flush ();
313 372
314 return true; 373 return true;
315} 374}
351 // registers, as xlib crashes due to a race otherwise. 410 // registers, as xlib crashes due to a race otherwise.
352 Atom actual_type, *atoms; 411 Atom actual_type, *atoms;
353 int actual_format; 412 int actual_format;
354 unsigned long nitems, bytes_after; 413 unsigned long nitems, bytes_after;
355 414
356 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, 415 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
357 False, XA_ATOM, &actual_type, &actual_format, 416 False, XA_ATOM, &actual_type, &actual_format,
358 &nitems, &bytes_after, (unsigned char **)&atoms) 417 &nitems, &bytes_after, (unsigned char **)&atoms)
359 != Success ) 418 != Success )
360 return; 419 return;
361 420
381#ifdef USE_XIM 440#ifdef USE_XIM
382 if (!XFilterEvent (&xev, None)) 441 if (!XFilterEvent (&xev, None))
383 { 442 {
384 if (xev.type == PropertyNotify 443 if (xev.type == PropertyNotify
385 && xev.xany.window == root 444 && xev.xany.window == root
386 && xev.xproperty.atom == xa_xim_servers) 445 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
387 im_change_check (); 446 im_change_check ();
388#endif 447#endif
389 for (int i = xw.size (); i--; ) 448 for (int i = xw.size (); i--; )
390 { 449 {
391 if (!xw[i]) 450 if (!xw[i])
481refcache<rxvt_display> displays; 540refcache<rxvt_display> displays;
482 541
483///////////////////////////////////////////////////////////////////////////// 542/////////////////////////////////////////////////////////////////////////////
484 543
485bool 544bool
486rxvt_color::set (rxvt_display *display, Pixel p) 545rxvt_color::set (rxvt_screen *screen, const char *name)
487{ 546{
488#if XFT 547#if XFT
489 XColor xc; 548 int l = strlen (name);
549 rxvt_rgba r;
550 char eos;
551 int mult;
490 552
491 xc.pixel = p; 553 if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
492 if (!XQueryColor (display->display, display->cmap, &xc)) 554 mult = rxvt_rgba::MAX_CC / 0x000f;
493 return false; 555 else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
556 mult = rxvt_rgba::MAX_CC / 0x00ff;
557 else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
558 mult = rxvt_rgba::MAX_CC / 0xffff;
559 else if (l == 4+5*4 && 4 == sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos))
560 mult = rxvt_rgba::MAX_CC / 0xffff;
561 else
562 return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c);
494 563
495 XRenderColor d; 564 r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult;
496 565 return set (screen, r);
497 d.red = xc.red;
498 d.green = xc.green;
499 d.blue = xc.blue;
500 d.alpha = 0xffff;
501
502 return
503 XftColorAllocValue (display->display,
504 display->visual,
505 display->cmap,
506 &d, &c);
507#else
508 this->p = p;
509#endif
510
511 return true;
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 566#else
521 XColor xc; 567 XColor xc;
522 568
523 if (XParseColor (display->display, display->cmap, name, &xc)) 569 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
524 return set (display, xc.red, xc.green, xc.blue); 570 return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue));
525 571
526 return false; 572 return false;
527#endif 573#endif
528} 574}
529 575
530bool 576bool
531rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) 577rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba)
532{ 578{
579#if XFT
580 XRenderPictFormat *format;
581
582 // FUCKING Xft gets it wrong, of course, so work around it
583 // transparency users should eat shit and die, and then
584 // XRenderQueryPictIndexValues themselves plenty.
585 if (screen->visual->c_class == TrueColor
586 && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual)))
587 {
588 // the fun lies in doing everything manually...
589 c.color.red = rgba.r;
590 c.color.green = rgba.g;
591 c.color.blue = rgba.b;
592 c.color.alpha = rgba.a;
593
594 c.pixel = (rgba.r >> (16 - popcount (format->direct.redMask )) << format->direct.red)
595 | (rgba.g >> (16 - popcount (format->direct.greenMask)) << format->direct.green)
596 | (rgba.b >> (16 - popcount (format->direct.blueMask )) << format->direct.blue)
597 | (rgba.a >> (16 - popcount (format->direct.alphaMask)) << format->direct.alpha);
598
599 return true;
600 }
601 else
602 {
603 XRenderColor d;
604
605 d.red = rgba.r;
606 d.green = rgba.g;
607 d.blue = rgba.b;
608 d.alpha = rgba.a;
609
610 return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c);
611 }
612
613 return false;
614#else
533 XColor xc; 615 XColor xc;
534 616
535 xc.red = cr; 617 xc.red = rgba.r;
536 xc.green = cg; 618 xc.green = rgba.g;
537 xc.blue = cb; 619 xc.blue = rgba.b;
538 xc.flags = DoRed | DoGreen | DoBlue; 620 xc.flags = DoRed | DoGreen | DoBlue;
539 621
540 if (XAllocColor (display->display, display->cmap, &xc)) 622 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
541 return set (display, xc.pixel); 623 {
624 p = xc.pixel;
625 return true;
626 }
542 627
543 return false; 628 return false;
629#endif
544} 630}
545 631
546void 632void
547rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) 633rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba)
548{ 634{
549#if XFT 635#if XFT
550 cr = c.color.red; 636 rgba.r = c.color.red;
551 cg = c.color.green; 637 rgba.g = c.color.green;
552 cb = c.color.blue; 638 rgba.b = c.color.blue;
639 rgba.a = c.color.alpha;
553#else 640#else
554 XColor c; 641 XColor c;
555 642
556 c.pixel = p; 643 c.pixel = p;
557 XQueryColor (display->display, display->cmap, &c); 644 XQueryColor (screen->xdisp, screen->cmap, &c);
558 645
559 cr = c.red; 646 rgba.r = c.red;
560 cg = c.green; 647 rgba.g = c.green;
561 cb = c.blue; 648 rgba.b = c.blue;
649 rgba.a = rxvt_rgba::MAX_CC;
562#endif 650#endif
563} 651}
564 652
565void 653void
566rxvt_color::free (rxvt_display *display) 654rxvt_color::free (rxvt_screen *screen)
567{ 655{
568#if XFT 656#if XFT
569 XftColorFree (display->display, display->visual, display->cmap, &c); 657 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
570#else 658#else
571 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); 659 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
572#endif 660#endif
573} 661}
574 662
575rxvt_color 663rxvt_color
576rxvt_color::fade (rxvt_display *display, int percent) 664rxvt_color::fade (rxvt_screen *screen, int percent)
577{ 665{
578 percent = 100 - percent;
579
580 unsigned short cr, cg, cb;
581 rxvt_color faded; 666 rxvt_color faded;
582 667
583 get (display, cr, cg, cb); 668 rxvt_rgba c;
669 get (screen, c);
584 670
585 faded.set ( 671 c.r = lerp (0, c.r, percent);
586 display, 672 c.g = lerp (0, c.g, percent);
587 cr * percent / 100, 673 c.b = lerp (0, c.b, percent);
588 cg * percent / 100, 674
589 cb * percent / 100 675 faded.set (screen, c);
590 );
591 676
592 return faded; 677 return faded;
593} 678}
594 679
595#define LERP(a,b,p) (a * p + b * (100 - p)) / 100
596
597rxvt_color 680rxvt_color
598rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto) 681rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
599{ 682{
600 percent = 100 - percent; 683 rxvt_rgba c, fc;
601
602 unsigned short cr, cg, cb;
603 unsigned short fcr, fcg, fcb;
604 rxvt_color faded; 684 rxvt_color faded;
605 685
606 get (display, cr, cg, cb); 686 get (screen, c);
607 fadeto.get(display, fcr, fcg, fcb); 687 fadeto.get (screen, fc);
608 688
609 faded.set ( 689 faded.set (
610 display, 690 screen,
691 rxvt_rgba (
611 LERP (cr, fcr, percent), 692 lerp (fc.r, c.r, percent),
612 LERP (cg, fcg, percent), 693 lerp (fc.g, c.g, percent),
613 LERP (cb, fcb, percent) 694 lerp (fc.b, c.b, percent),
695 lerp (fc.a, c.a, percent)
696 )
614 ); 697 );
615 698
616 return faded; 699 return faded;
617} 700}
618 701

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines