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.14 by root, Thu Jul 7 19:37:47 2005 UTC vs.
Revision 1.31 by root, Sun Jan 29 22:27:04 2006 UTC

25#include <rxvttoolkit.h> 25#include <rxvttoolkit.h>
26 26
27#include <unistd.h> 27#include <unistd.h>
28#include <fcntl.h> 28#include <fcntl.h>
29 29
30#include <sys/utsname.h>
31
30#ifndef NO_SLOW_LINK_SUPPORT 32#ifndef NO_SLOW_LINK_SUPPORT
31# include <sys/socket.h> 33# include <sys/socket.h>
32# include <sys/un.h> 34# include <sys/un.h>
33#endif 35#endif
34 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
35refcounted::refcounted (const char *id) 79refcounted::refcounted (const char *id)
36{ 80{
37 this->id = strdup (id); 81 this->id = strdup (id);
38} 82}
39 83
47{ 91{
48 for (T **i = this->begin (); i < this->end (); ++i) 92 for (T **i = this->begin (); i < this->end (); ++i)
49 { 93 {
50 if (!strcmp (id, (*i)->id)) 94 if (!strcmp (id, (*i)->id))
51 { 95 {
52 (*i)->referenced++; 96 ++(*i)->referenced;
97 (*i)->ref_next ();
53 return *i; 98 return *i;
54 } 99 }
55 } 100 }
56 101
57 T *obj = new T (id); 102 T *obj = new T (id);
58 103
59 obj->referenced = 1;
60
61 if (obj && obj->init ()) 104 if (obj && obj->ref_init ())
62 { 105 {
106 obj->referenced = 1;
63 this->push_back (obj); 107 this->push_back (obj);
64 return obj; 108 return obj;
65 } 109 }
66 else 110 else
67 { 111 {
91} 135}
92 136
93///////////////////////////////////////////////////////////////////////////// 137/////////////////////////////////////////////////////////////////////////////
94 138
95#ifdef USE_XIM 139#ifdef USE_XIM
140
96static void 141static void
97#if XIMCB_PROTO_BROKEN 142#if XIMCB_PROTO_BROKEN
98im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) 143im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
99#else 144#else
100im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) 145im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
107 152
108 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); 153 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
109 display->im_change_cb (); 154 display->im_change_cb ();
110} 155}
111 156
157bool
112bool rxvt_xim::init () 158rxvt_xim::ref_init ()
113{ 159{
114 display = GET_R->display; //HACK: TODO 160 display = GET_R->display; //HACK: TODO
115 161
116 xim = XOpenIM (display->display, NULL, NULL, NULL); 162 xim = XOpenIM (display->display, NULL, NULL, NULL);
117 163
130rxvt_xim::~rxvt_xim () 176rxvt_xim::~rxvt_xim ()
131{ 177{
132 if (xim) 178 if (xim)
133 XCloseIM (xim); 179 XCloseIM (xim);
134} 180}
181
135#endif 182#endif
183
184/////////////////////////////////////////////////////////////////////////////
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}
136 222
137///////////////////////////////////////////////////////////////////////////// 223/////////////////////////////////////////////////////////////////////////////
138 224
139rxvt_display::rxvt_display (const char *id) 225rxvt_display::rxvt_display (const char *id)
140: refcounted (id) 226: refcounted (id)
141, x_ev (this, &rxvt_display::x_cb) 227, x_ev (this, &rxvt_display::x_cb)
142, selection_owner (0) 228, selection_owner (0)
143{ 229{
144} 230}
145 231
232XrmDatabase
233rxvt_display::get_resources ()
234{
235 char *homedir = (char *)getenv ("HOME");
236 char fname[1024];
237
238 /*
239 * get resources using the X library function
240 */
241 char *displayResource, *xe;
242 XrmDatabase database, rdb1;
243
244 database = NULL;
245
246 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
247
248 // 6. System wide per application default file.
249
250 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
251 if ((xe = (char *)getenv ("XAPPLRESDIR")))
252 {
253 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
254
255 if ((rdb1 = XrmGetFileDatabase (fname)))
256 XrmMergeDatabases (rdb1, &database);
257 }
258
259 // 5. User's per application default file.
260 // none
261
262 // 4. User's defaults file.
263 /* Get any Xserver defaults */
264 displayResource = XResourceManagerString (display);
265
266 if (displayResource != NULL)
267 {
268 if ((rdb1 = XrmGetStringDatabase (displayResource)))
269 XrmMergeDatabases (rdb1, &database);
270 }
271 else if (homedir)
272 {
273 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
274
275 if ((rdb1 = XrmGetFileDatabase (fname)))
276 XrmMergeDatabases (rdb1, &database);
277 }
278
279 /* Get screen specific resources */
280 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
281
282 if (displayResource != NULL)
283 {
284 if ((rdb1 = XrmGetStringDatabase (displayResource)))
285 /* Merge with screen-independent resources */
286 XrmMergeDatabases (rdb1, &database);
287
288 XFree (displayResource);
289 }
290
291 // 3. User's per host defaults file
292 /* Add in XENVIRONMENT file */
293 if ((xe = (char *)getenv ("XENVIRONMENT"))
294 && (rdb1 = XrmGetFileDatabase (xe)))
295 XrmMergeDatabases (rdb1, &database);
296 else if (homedir)
297 {
298 struct utsname un;
299
300 if (!uname (&un))
301 {
302 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
303
304 if ((rdb1 = XrmGetFileDatabase (fname)))
305 XrmMergeDatabases (rdb1, &database);
306 }
307 }
308
309 return database;
310}
311
146bool rxvt_display::init () 312bool rxvt_display::ref_init ()
147{ 313{
148#ifdef LOCAL_X_IS_UNIX 314#ifdef LOCAL_X_IS_UNIX
149 if (id[0] == ':') 315 if (id[0] == ':')
150 { 316 {
151 val = rxvt_malloc (5 + strlen (id) + 1); 317 val = rxvt_malloc (5 + strlen (id) + 1);
153 strcat (val, id); 319 strcat (val, id);
154 display = XOpenDisplay (val); 320 display = XOpenDisplay (val);
155 free (val); 321 free (val);
156 } 322 }
157 else 323 else
324#endif
158 display = 0; 325 display = 0;
159#endif
160 326
161 if (!display) 327 if (!display)
162 display = XOpenDisplay (id); 328 display = XOpenDisplay (id);
163 329
164 if (!display) 330 if (!display)
165 return false; 331 return false;
166 332
167 screen = DefaultScreen (display); 333 screen = DefaultScreen (display);
168 root = DefaultRootWindow (display); 334 root = DefaultRootWindow (display);
169 visual = DefaultVisual (display, screen);
170 cmap = DefaultColormap (display, screen);
171 depth = DefaultDepth (display, screen);
172 335
173 int fd = XConnectionNumber (display); 336 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
337 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
174 338
175#ifndef NO_SLOW_LINK_SUPPORT 339 XrmSetDatabase (display, get_resources ());
176 // try to detetc wether we have a local connection.
177 // assume unix domains socket == local, everything else not
178 // TODO: might want to check for inet/127.0.0.1
179 is_local = 0;
180 sockaddr_un sa;
181 socklen_t sl = sizeof (sa);
182
183 if (!getsockname (fd, (sockaddr *)&sa, &sl))
184 is_local = sa.sun_family == AF_LOCAL;
185#endif
186 340
187#ifdef POINTER_BLANK 341#ifdef POINTER_BLANK
188 XColor blackcolour; 342 XColor blackcolour;
189 blackcolour.red = 0; 343 blackcolour.red = 0;
190 blackcolour.green = 0; 344 blackcolour.green = 0;
193 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', 347 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
194 &blackcolour, &blackcolour); 348 &blackcolour, &blackcolour);
195 XUnloadFont (display, f); 349 XUnloadFont (display, f);
196#endif 350#endif
197 351
198#ifdef PREFER_24BIT 352 int fd = XConnectionNumber (display);
199 /*
200 * If depth is not 24, look for a 24bit visual.
201 */
202 if (depth != 24)
203 {
204 XVisualInfo vinfo;
205 353
206 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) 354#ifndef NO_SLOW_LINK_SUPPORT
207 { 355 // try to detect wether we have a local connection.
208 depth = 24; 356 // assume unix domains socket == local, everything else not
209 visual = vinfo.visual; 357 // TODO: might want to check for inet/127.0.0.1
210 cmap = XCreateColormap (display, 358 is_local = 0;
211 RootWindow (display, screen), 359 sockaddr_un sa;
212 visual, AllocNone); 360 socklen_t sl = sizeof (sa);
213 } 361
214 } 362 if (!getsockname (fd, (sockaddr *)&sa, &sl))
363 is_local = sa.sun_family == AF_LOCAL;
215#endif 364#endif
216 365
217 x_ev.start (fd, EVENT_READ); 366 x_ev.start (fd, EVENT_READ);
218 fcntl (fd, F_SETFD, FD_CLOEXEC); 367 fcntl (fd, F_SETFD, FD_CLOEXEC);
219 368
220 XSelectInput (display, root, PropertyChangeMask); 369 XSelectInput (display, root, PropertyChangeMask);
221#ifdef USE_XIM
222 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
223#endif
224 370
225 flush (); 371 flush ();
226 372
227 return true; 373 return true;
228} 374}
229 375
376void
377rxvt_display::ref_next ()
378{
379 // TODO: somehow check wether the database files/resources changed
380 // before re-loading/parsing
381 XrmDestroyDatabase (XrmGetDatabase (display));
382 XrmSetDatabase (display, get_resources ());
383}
384
230rxvt_display::~rxvt_display () 385rxvt_display::~rxvt_display ()
231{ 386{
387 if (!display)
388 return;
389
390#ifdef POINTER_BLANK
391 XFreeCursor (display, blank_cursor);
392#endif
232 x_ev.stop (); 393 x_ev.stop ();
233#ifdef USE_XIM 394#ifdef USE_XIM
234 xims.clear (); 395 xims.clear ();
235#endif 396#endif
236
237 if (display)
238 XCloseDisplay (display); 397 XCloseDisplay (display);
239} 398}
240 399
241#ifdef USE_XIM 400#ifdef USE_XIM
242void rxvt_display::im_change_cb () 401void rxvt_display::im_change_cb ()
243{ 402{
251 // registers, as xlib crashes due to a race otherwise. 410 // registers, as xlib crashes due to a race otherwise.
252 Atom actual_type, *atoms; 411 Atom actual_type, *atoms;
253 int actual_format; 412 int actual_format;
254 unsigned long nitems, bytes_after; 413 unsigned long nitems, bytes_after;
255 414
256 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, 415 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
257 False, XA_ATOM, &actual_type, &actual_format, 416 False, XA_ATOM, &actual_type, &actual_format,
258 &nitems, &bytes_after, (unsigned char **)&atoms) 417 &nitems, &bytes_after, (unsigned char **)&atoms)
259 != Success ) 418 != Success )
260 return; 419 return;
261 420
279 XNextEvent (display, &xev); 438 XNextEvent (display, &xev);
280 439
281#ifdef USE_XIM 440#ifdef USE_XIM
282 if (!XFilterEvent (&xev, None)) 441 if (!XFilterEvent (&xev, None))
283 { 442 {
284
285 if (xev.type == PropertyNotify 443 if (xev.type == PropertyNotify
286 && xev.xany.window == root 444 && xev.xany.window == root
287 && xev.xproperty.atom == xa_xim_servers) 445 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
288 im_change_check (); 446 im_change_check ();
289#endif 447#endif
290 for (int i = xw.size (); i--; ) 448 for (int i = xw.size (); i--; )
291 { 449 {
292 if (!xw[i]) 450 if (!xw[i])
382refcache<rxvt_display> displays; 540refcache<rxvt_display> displays;
383 541
384///////////////////////////////////////////////////////////////////////////// 542/////////////////////////////////////////////////////////////////////////////
385 543
386bool 544bool
387rxvt_color::set (rxvt_display *display, Pixel p) 545rxvt_color::set (rxvt_screen *screen, const char *name)
388{ 546{
389#if XFT 547#if XFT
390 XColor xc; 548 int l = strlen (name);
549 rxvt_rgba r;
550 char eos;
551 int mult;
391 552
392 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))
393 if (!XQueryColor (display->display, display->cmap, &xc)) 554 mult = rxvt_rgba::MAX_CC / 0x000f;
394 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);
395 563
396 XRenderColor d; 564 r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult;
397 565 return set (screen, r);
398 d.red = xc.red;
399 d.green = xc.green;
400 d.blue = xc.blue;
401 d.alpha = 0xffff;
402
403 return
404 XftColorAllocValue (display->display,
405 display->visual,
406 display->cmap,
407 &d, &c);
408#else
409 this->p = p;
410#endif
411
412 return true;
413}
414
415bool
416rxvt_color::set (rxvt_display *display, const char *name)
417{
418#if XFT
419 return XftColorAllocName (display->display, display->visual, display->cmap,
420 name, &c);
421#else 566#else
422 XColor xc; 567 XColor xc;
423 568
424 if (XParseColor (display->display, display->cmap, name, &xc)) 569 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
425 return set (display, xc.red, xc.green, xc.blue); 570 return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue));
426 571
427 return false; 572 return false;
428#endif 573#endif
429} 574}
430 575
431bool 576bool
432rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) 577rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba)
433{ 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
434 XColor xc; 615 XColor xc;
435 616
436 xc.red = cr; 617 xc.red = rgba.r;
437 xc.green = cg; 618 xc.green = rgba.g;
438 xc.blue = cb; 619 xc.blue = rgba.b;
439 xc.flags = DoRed | DoGreen | DoBlue; 620 xc.flags = DoRed | DoGreen | DoBlue;
440 621
441 if (XAllocColor (display->display, display->cmap, &xc)) 622 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
442 return set (display, xc.pixel); 623 {
624 p = xc.pixel;
625 return true;
626 }
443 627
444 return false; 628 return false;
629#endif
445} 630}
446 631
447void 632void
448rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) 633rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba)
449{ 634{
450#if XFT 635#if XFT
451 cr = c.color.red; 636 rgba.r = c.color.red;
452 cg = c.color.green; 637 rgba.g = c.color.green;
453 cb = c.color.blue; 638 rgba.b = c.color.blue;
639 rgba.a = c.color.alpha;
454#else 640#else
455 XColor c; 641 XColor c;
456 642
457 c.pixel = p; 643 c.pixel = p;
458 XQueryColor (display->display, display->cmap, &c); 644 XQueryColor (screen->xdisp, screen->cmap, &c);
459 645
460 cr = c.red; 646 rgba.r = c.red;
461 cg = c.green; 647 rgba.g = c.green;
462 cb = c.blue; 648 rgba.b = c.blue;
649 rgba.a = rxvt_rgba::MAX_CC;
463#endif 650#endif
464} 651}
465 652
466void 653void
467rxvt_color::free (rxvt_display *display) 654rxvt_color::free (rxvt_screen *screen)
468{ 655{
469#if XFT 656#if XFT
470 XftColorFree (display->display, display->visual, display->cmap, &c); 657 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
471#else 658#else
472 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); 659 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
473#endif 660#endif
474} 661}
475 662
476rxvt_color 663rxvt_color
477rxvt_color::fade (rxvt_display *display, int percent) 664rxvt_color::fade (rxvt_screen *screen, int percent)
478{ 665{
479 unsigned short cr, cg, cb;
480 rxvt_color faded; 666 rxvt_color faded;
481 667
482 get (display, cr, cg, cb); 668 rxvt_rgba c;
483 faded.set (display, 669 get (screen, c);
484 cr * percent / 100, 670
485 cg * percent / 100, 671 c.r = lerp (0, c.r, percent);
486 cb * percent / 100); 672 c.g = lerp (0, c.g, percent);
673 c.b = lerp (0, c.b, percent);
674
675 faded.set (screen, c);
487 676
488 return faded; 677 return faded;
489} 678}
490 679
680rxvt_color
681rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
682{
683 rxvt_rgba c, fc;
684 rxvt_color faded;
685
686 get (screen, c);
687 fadeto.get (screen, fc);
688
689 faded.set (
690 screen,
691 rxvt_rgba (
692 lerp (fc.r, c.r, percent),
693 lerp (fc.g, c.g, percent),
694 lerp (fc.b, c.b, percent),
695 lerp (fc.a, c.a, percent)
696 )
697 );
698
699 return faded;
700}
701

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines