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.25 by root, Wed Jan 25 02:42:06 2006 UTC vs.
Revision 1.49 by root, Thu Feb 2 14:41:00 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
37const char *const xa_names[] = 41const char *const xa_names[] =
38 { 42{
39 "TEXT", 43 "TEXT",
40 "COMPOUND_TEXT", 44 "COMPOUND_TEXT",
41 "UTF8_STRING", 45 "UTF8_STRING",
42 "MULTIPLE", 46 "MULTIPLE",
43 "TARGETS", 47 "TARGETS",
44 "TIMESTAMP", 48 "TIMESTAMP",
45 "VT_SELECTION", 49 "VT_SELECTION",
46 "INCR", 50 "INCR",
47 "WM_PROTOCOLS", 51 "WM_PROTOCOLS",
48 "WM_DELETE_WINDOW", 52 "WM_DELETE_WINDOW",
49 "CLIPBOARD", 53 "CLIPBOARD",
54 "AVERAGE_WIDTH",
55 "WEIGHT_NAME",
56 "SLANT",
57 "CHARSET_REGISTRY",
58 "CHARSET_ENCODING",
50#if ENABLE_FRILLS 59#if ENABLE_FRILLS
51 "_MOTIF_WM_HINTS", 60 "_MOTIF_WM_HINTS",
52#endif 61#endif
53#if ENABLE_EWMH 62#if ENABLE_EWMH
54 "_NET_WM_PID", 63 "_NET_WM_PID",
55 "_NET_WM_NAME", 64 "_NET_WM_NAME",
56 "_NET_WM_ICON_NAME", 65 "_NET_WM_ICON_NAME",
57 "_NET_WM_PING", 66 "_NET_WM_PING",
58#endif 67#endif
59#if USE_XIM 68#if USE_XIM
60 "WM_LOCALE_NAME", 69 "WM_LOCALE_NAME",
61 "XIM_SERVERS", 70 "XIM_SERVERS",
62#endif 71#endif
63#ifdef TRANSPARENT 72#ifdef TRANSPARENT
64 "_XROOTPMAP_ID", 73 "_XROOTPMAP_ID",
65 "ESETROOT_PMAP_ID", 74 "ESETROOT_PMAP_ID",
66#endif 75#endif
67#if ENABLE_XEMBED 76#if ENABLE_XEMBED
68 "_XEMBED", 77 "_XEMBED",
69 "_XEMBED_INFO", 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 "cursor",
87# if USE_XIM
88 "TRANSPORT",
89 "LOCALES",
90 "_XIM_PROTOCOL",
91 "_XIM_XCONNECT",
92 "_XIM_MOREDATA",
70#endif 93# endif
71 }; 94#endif
95};
72 96
73///////////////////////////////////////////////////////////////////////////// 97/////////////////////////////////////////////////////////////////////////////
74 98
75refcounted::refcounted (const char *id) 99refcounted::refcounted (const char *id)
76{ 100{
153bool 177bool
154rxvt_xim::ref_init () 178rxvt_xim::ref_init ()
155{ 179{
156 display = GET_R->display; //HACK: TODO 180 display = GET_R->display; //HACK: TODO
157 181
158 xim = XOpenIM (display->display, NULL, NULL, NULL); 182 xim = XOpenIM (display->display, 0, 0, 0);
159 183
160 if (!xim) 184 if (!xim)
161 return false; 185 return false;
162 186
163 XIMCallback ximcallback; 187 XIMCallback ximcallback;
164 ximcallback.client_data = (XPointer)this; 188 ximcallback.client_data = (XPointer)this;
165 ximcallback.callback = im_destroy_cb; 189 ximcallback.callback = im_destroy_cb;
166 190
167 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); 191 XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0);
168 192
169 return true; 193 return true;
170} 194}
171 195
172rxvt_xim::~rxvt_xim () 196rxvt_xim::~rxvt_xim ()
174 if (xim) 198 if (xim)
175 XCloseIM (xim); 199 XCloseIM (xim);
176} 200}
177 201
178#endif 202#endif
203
204/////////////////////////////////////////////////////////////////////////////
205
206void
207rxvt_screen::set (rxvt_display *disp)
208{
209 display = disp;
210 xdisp = disp->display;
211
212 Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
213
214 depth = DefaultDepthOfScreen (screen);
215 visual = DefaultVisualOfScreen (screen);
216 cmap = DefaultColormapOfScreen (screen);
217}
218
219void
220rxvt_screen::set (rxvt_display *disp, int bitdepth)
221{
222 set (disp);
223
224#if XFT
225 XVisualInfo vinfo;
226
227 if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo))
228 {
229 depth = bitdepth;
230 visual = vinfo.visual;
231 cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
232 }
233#endif
234}
235
236void
237rxvt_screen::clear ()
238{
239 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
240 XFreeColormap (xdisp, cmap);
241}
179 242
180///////////////////////////////////////////////////////////////////////////// 243/////////////////////////////////////////////////////////////////////////////
181 244
182rxvt_display::rxvt_display (const char *id) 245rxvt_display::rxvt_display (const char *id)
183: refcounted (id) 246: refcounted (id)
185, selection_owner (0) 248, selection_owner (0)
186{ 249{
187} 250}
188 251
189XrmDatabase 252XrmDatabase
190rxvt_display::get_resources () 253rxvt_display::get_resources (bool refresh)
191{ 254{
192 char *homedir = (char *)getenv ("HOME"); 255 char *homedir = (char *)getenv ("HOME");
193 char fname[1024]; 256 char fname[1024];
194 257
195 /* 258 /*
196 * get resources using the X library function 259 * get resources using the X library function
197 */ 260 */
198 char *displayResource, *xe; 261 char *displayResource, *xe;
199 XrmDatabase database, rdb1; 262 XrmDatabase rdb1, database = 0;
200
201 database = NULL;
202 263
203 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 264 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
204 265
205 // 6. System wide per application default file. 266 // 6. System wide per application default file.
206 267
216 // 5. User's per application default file. 277 // 5. User's per application default file.
217 // none 278 // none
218 279
219 // 4. User's defaults file. 280 // 4. User's defaults file.
220 /* Get any Xserver defaults */ 281 /* Get any Xserver defaults */
282 if (refresh)
283 {
284 // fucking xlib keeps a copy of the rm string
285 Atom actual_type;
286 int actual_format;
287 unsigned long nitems, nremaining;
288 char *val = 0;
289
290#if XLIB_ILLEGAL_ACCESS
291 if (display->xdefaults)
292 XFree (display->xdefaults);
293#endif
294
295 if (XGetWindowProperty (display, root, XA_RESOURCE_MANAGER,
296 0L, 100000000L, False,
297 XA_STRING, &actual_type, &actual_format,
298 &nitems, &nremaining,
299 (unsigned char **)&val) == Success
300 && actual_type == XA_STRING
301 && actual_format == 8)
302 displayResource = val;
303 else
304 {
305 displayResource = 0;
306 if (val)
307 XFree(val);
308 }
309
310#if XLIB_ILLEGAL_ACCESS
311 display->xdefaults = displayResource;
312#endif
313 }
314 else
221 displayResource = XResourceManagerString (display); 315 displayResource = XResourceManagerString (display);
222 316
223 if (displayResource != NULL) 317 if (displayResource)
224 { 318 {
225 if ((rdb1 = XrmGetStringDatabase (displayResource))) 319 if ((rdb1 = XrmGetStringDatabase (displayResource)))
226 XrmMergeDatabases (rdb1, &database); 320 XrmMergeDatabases (rdb1, &database);
227 } 321 }
228 else if (homedir) 322 else if (homedir)
231 325
232 if ((rdb1 = XrmGetFileDatabase (fname))) 326 if ((rdb1 = XrmGetFileDatabase (fname)))
233 XrmMergeDatabases (rdb1, &database); 327 XrmMergeDatabases (rdb1, &database);
234 } 328 }
235 329
330#if !XLIB_ILLEGAL_ACCESS
331 if (refresh && displayResource)
332 XFree (displayResource);
333#endif
334
236 /* Get screen specific resources */ 335 /* Get screen specific resources */
237 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); 336 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
238 337
239 if (displayResource != NULL) 338 if (displayResource)
240 { 339 {
241 if ((rdb1 = XrmGetStringDatabase (displayResource))) 340 if ((rdb1 = XrmGetStringDatabase (displayResource)))
242 /* Merge with screen-independent resources */ 341 /* Merge with screen-independent resources */
243 XrmMergeDatabases (rdb1, &database); 342 XrmMergeDatabases (rdb1, &database);
244 343
285 display = XOpenDisplay (id); 384 display = XOpenDisplay (id);
286 385
287 if (!display) 386 if (!display)
288 return false; 387 return false;
289 388
290 screen = DefaultScreen (display); 389 screen = DefaultScreen (display);
291 root = DefaultRootWindow (display); 390 root = DefaultRootWindow (display);
292 visual = DefaultVisual (display, screen);
293 cmap = DefaultColormap (display, screen);
294 depth = DefaultDepth (display, screen);
295 391
296 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); 392 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
297 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); 393 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
298 394
299 XrmSetDatabase (display, get_resources ()); 395 XrmSetDatabase (display, get_resources (false));
300 396
301#ifdef POINTER_BLANK 397#ifdef POINTER_BLANK
302 XColor blackcolour; 398 XColor blackcolour;
303 blackcolour.red = 0; 399 blackcolour.red = 0;
304 blackcolour.green = 0; 400 blackcolour.green = 0;
307 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', 403 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
308 &blackcolour, &blackcolour); 404 &blackcolour, &blackcolour);
309 XUnloadFont (display, f); 405 XUnloadFont (display, f);
310#endif 406#endif
311 407
312#ifdef PREFER_24BIT
313 /*
314 * If depth is not 24, look for a 24bit visual.
315 */
316 if (depth != 24)
317 {
318 XVisualInfo vinfo;
319
320 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
321 {
322 depth = 24;
323 visual = vinfo.visual;
324 cmap = XCreateColormap (display,
325 RootWindow (display, screen),
326 visual, AllocNone);
327 }
328 }
329#endif
330
331 int fd = XConnectionNumber (display); 408 int fd = XConnectionNumber (display);
332 409
333#ifndef NO_SLOW_LINK_SUPPORT 410#ifndef NO_SLOW_LINK_SUPPORT
334 // try to detect wether we have a local connection. 411 // try to detect wether we have a local connection.
335 // assume unix domains socket == local, everything else not 412 // assume unix domains socket == local, everything else not
354 431
355void 432void
356rxvt_display::ref_next () 433rxvt_display::ref_next ()
357{ 434{
358 // TODO: somehow check wether the database files/resources changed 435 // TODO: somehow check wether the database files/resources changed
359 // before re-loading/parsing 436 // before affording re-loading/parsing
360 XrmDestroyDatabase (XrmGetDatabase (display)); 437 XrmDestroyDatabase (XrmGetDatabase (display));
361 XrmSetDatabase (display, get_resources ()); 438 XrmSetDatabase (display, get_resources (true));
362} 439}
363 440
364rxvt_display::~rxvt_display () 441rxvt_display::~rxvt_display ()
365{ 442{
366 if (!display) 443 if (!display)
467 544
468 selection_owner = owner; 545 selection_owner = owner;
469} 546}
470 547
471#ifdef USE_XIM 548#ifdef USE_XIM
549
472void rxvt_display::reg (im_watcher *w) 550void rxvt_display::reg (im_watcher *w)
473{ 551{
474 imw.push_back (w); 552 imw.push_back (w);
475} 553}
476 554
500 return xim; 578 return xim;
501} 579}
502 580
503void rxvt_display::put_xim (rxvt_xim *xim) 581void rxvt_display::put_xim (rxvt_xim *xim)
504{ 582{
505#if XLIB_IS_RACEFREE 583# if XLIB_IS_RACEFREE
506 xims.put (xim); 584 xims.put (xim);
507#endif 585# endif
508} 586}
587
509#endif 588#endif
510 589
511Atom rxvt_display::atom (const char *name) 590Atom rxvt_display::atom (const char *name)
512{ 591{
513 return XInternAtom (display, name, False); 592 return XInternAtom (display, name, False);
517 596
518template class refcache<rxvt_display>; 597template class refcache<rxvt_display>;
519refcache<rxvt_display> displays; 598refcache<rxvt_display> displays;
520 599
521///////////////////////////////////////////////////////////////////////////// 600/////////////////////////////////////////////////////////////////////////////
522 601
523bool 602bool
524rxvt_color::set (rxvt_display *display, Pixel p) 603rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
525{ 604{
526#if XFT 605#if XFT
527 XColor xc; 606 XRenderPictFormat *format;
528 607
529 xc.pixel = p; 608 // FUCKING Xft gets it wrong, of course, so work around it.
530 if (!XQueryColor (display->display, display->cmap, &xc)) 609 // Transparency users should eat shit and die, and then
531 return false; 610 // XRenderQueryPictIndexValues themselves plenty.
611 if ((screen->visual->c_class == TrueColor)
612 && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual)))
613 {
614 // the fun lies in doing everything manually...
615 c.color.red = color.r;
616 c.color.green = color.g;
617 c.color.blue = color.b;
618 c.color.alpha = color.a;
532 619
620 c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red )
621 | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green)
622 | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue )
623 | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha);
624
625 return true;
626 }
627 else
628 {
533 XRenderColor d; 629 XRenderColor d;
534 630
535 d.red = xc.red; 631 d.red = color.r;
536 d.green = xc.green; 632 d.green = color.g;
537 d.blue = xc.blue; 633 d.blue = color.b;
538 d.alpha = 0xffff; 634 d.alpha = color.a;
539 635
540 return 636 return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c);
541 XftColorAllocValue (display->display, 637 }
542 display->visual,
543 display->cmap,
544 &d, &c);
545#else 638#else
546 this->p = p; 639 c.red = color.r;
547#endif 640 c.green = color.g;
641 c.blue = color.b;
548 642
643 if (screen->visual->c_class == TrueColor)
644 {
645 c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask ))
646 / rgba::MAX_CC) << ctz (screen->visual->red_mask )
647 | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask))
648 / rgba::MAX_CC) << ctz (screen->visual->green_mask)
649 | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask ))
650 / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
651
652 return true;
653 }
654 else if (XAllocColor (screen->xdisp, screen->cmap, &c))
549 return true; 655 return true;
656 else
657 c.pixel = (color.r + color.g + color.b) > 128*3
658 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp))
659 : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp));
660#endif
661
662 return false;
550} 663}
551 664
552bool 665bool
553rxvt_color::set (rxvt_display *display, const char *name) 666rxvt_color::set (rxvt_screen *screen, const char *name)
667{
668 rgba c;
669 char eos;
670 int skip;
671
672 // parse the nonstandard "[alphapercent]" prefix
673 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
674 {
675 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a);
676 name += skip;
677 }
678 else
679 c.a = rgba::MAX_CC;
680
681 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
682 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
683 {
684 XColor xc, xc_exact;
685
686 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
687 {
688 c.r = xc.red;
689 c.g = xc.green;
690 c.b = xc.blue;
691 }
692 else
693 {
694 c.r = 0xffff;
695 c.g = 0x6969;
696 c.b = 0xb4b4;
697
698 rxvt_warn ("unable to parse color '%s', using pink instead.\n", name);
699 }
700 }
701
702 return set (screen, c);
703}
704
705bool
706rxvt_color::set (rxvt_screen *screen, const rgba &color)
707{
708 bool got = alloc (screen, color);
709
710#if !ENABLE_MINIMAL
711 int cmap_size = screen->visual->map_entries;
712
713 if (!got
714 && screen->visual->c_class == PseudoColor
715 && cmap_size < 4096)
716 {
717 XColor *colors = new XColor [screen->visual->map_entries];
718
719 for (int i = 0; i < cmap_size; i++)
720 colors [i].pixel = i;
721
722 // many kilobytes transfer per colour, but pseudocolor isn't worth
723 // many extra optimisations.
724 XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size);
725
726 int diff = 0x7fffffffUL;
727 XColor *best = colors;
728
729 for (int i = 0; i < cmap_size; i++)
730 {
731 int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2))
732 + (squared_diff<int> (color.g >> 2, colors [i].green >> 2))
733 + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2));
734
735 if (d < diff)
736 {
737 diff = d;
738 best = colors + i;
739 }
740 }
741
742 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n",
743 // color.r, color.g, color.b, best->red, best->green, best->blue, diff);
744
745 got = alloc (screen, rgba (best->red, best->green, best->blue));
746
747 delete colors;
748 }
749#endif
750
751 return got;
752}
753
754void
755rxvt_color::get (rgba &color)
554{ 756{
555#if XFT 757#if XFT
556 return XftColorAllocName (display->display, display->visual, display->cmap, 758 color.r = c.color.red;
557 name, &c); 759 color.g = c.color.green;
760 color.b = c.color.blue;
761 color.a = c.color.alpha;
558#else 762#else
559 XColor xc; 763 color.r = c.red;
560 764 color.g = c.green;
561 if (XParseColor (display->display, display->cmap, name, &xc)) 765 color.b = c.blue;
562 return set (display, xc.red, xc.green, xc.blue); 766 color.a = rgba::MAX_CC;
563
564 return false;
565#endif 767#endif
566} 768}
567 769
568bool 770void
569rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) 771rxvt_color::get (XColor &color)
570{ 772{
571 XColor xc; 773 rgba c;
774 get (c);
572 775
573 xc.red = cr; 776 color.red = c.r;
574 xc.green = cg; 777 color.green = c.g;
575 xc.blue = cb; 778 color.blue = c.b;
576 xc.flags = DoRed | DoGreen | DoBlue; 779 color.pixel = (Pixel)*this;
577
578 if (XAllocColor (display->display, display->cmap, &xc))
579 return set (display, xc.pixel);
580
581 return false;
582} 780}
583 781
584void 782void
585rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) 783rxvt_color::free (rxvt_screen *screen)
586{ 784{
587#if XFT 785#if XFT
588 cr = c.color.red; 786 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
589 cg = c.color.green;
590 cb = c.color.blue;
591#else 787#else
592 XColor c; 788 XFreeColors (screen->xdisp, screen->cmap, &c.pixel, 1, AllPlanes);
593
594 c.pixel = p;
595 XQueryColor (display->display, display->cmap, &c);
596
597 cr = c.red;
598 cg = c.green;
599 cb = c.blue;
600#endif 789#endif
601} 790}
602 791
603void 792void
604rxvt_color::free (rxvt_display *display) 793rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to)
605{ 794{
606#if XFT 795 rgba c;
607 XftColorFree (display->display, display->visual, display->cmap, &c); 796 get (c);
608#else
609 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
610#endif
611}
612 797
613rxvt_color 798 result.set (
614rxvt_color::fade (rxvt_display *display, int percent) 799 screen,
615{ 800 rgba (
616 percent = 100 - percent; 801 lerp (c.r, to.r, percent),
617 802 lerp (c.g, to.g, percent),
618 unsigned short cr, cg, cb; 803 lerp (c.b, to.b, percent),
619 rxvt_color faded; 804 lerp (c.a, to.a, percent)
620 805 )
621 get (display, cr, cg, cb);
622
623 faded.set (
624 display,
625 cr * percent / 100,
626 cg * percent / 100,
627 cb * percent / 100
628 ); 806 );
629
630 return faded;
631} 807}
632 808
633#define LERP(a,b,p) (a * p + b * (100 - p)) / 100
634
635rxvt_color
636rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto)
637{
638 percent = 100 - percent;
639
640 unsigned short cr, cg, cb;
641 unsigned short fcr, fcg, fcb;
642 rxvt_color faded;
643
644 get (display, cr, cg, cb);
645 fadeto.get(display, fcr, fcg, fcb);
646
647 faded.set (
648 display,
649 LERP (cr, fcr, percent),
650 LERP (cg, fcg, percent),
651 LERP (cb, fcb, percent)
652 );
653
654 return faded;
655}
656

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines