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.15 by root, Wed Jul 13 00:51:25 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);
152 strcpy (val, "unix/"); 318 strcpy (val, "unix/");
153 strcat (val, id); 319 strcat (val, id);
154 display = XOpenDisplay (val); 320 display = XOpenDisplay (val);
155 printf ("OD %s => %p\n", val, display);//D
156 free (val); 321 free (val);
157 } 322 }
158 else 323 else
159#endif 324#endif
160 display = 0; 325 display = 0;
161 326
162 if (!display) 327 if (!display)
163 display = XOpenDisplay (id); 328 display = XOpenDisplay (id);
164 printf ("O2 %s => %p\n", id, display);//D
165 329
166 if (!display) 330 if (!display)
167 return false; 331 return false;
168 332
169 screen = DefaultScreen (display); 333 screen = DefaultScreen (display);
170 root = DefaultRootWindow (display); 334 root = DefaultRootWindow (display);
171 visual = DefaultVisual (display, screen);
172 cmap = DefaultColormap (display, screen);
173 depth = DefaultDepth (display, screen);
174 335
175 int fd = XConnectionNumber (display); 336 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
337 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
176 338
177#ifndef NO_SLOW_LINK_SUPPORT 339 XrmSetDatabase (display, get_resources ());
178 // try to detetc wether we have a local connection.
179 // assume unix domains socket == local, everything else not
180 // TODO: might want to check for inet/127.0.0.1
181 is_local = 0;
182 sockaddr_un sa;
183 socklen_t sl = sizeof (sa);
184
185 if (!getsockname (fd, (sockaddr *)&sa, &sl))
186 is_local = sa.sun_family == AF_LOCAL;
187#endif
188 340
189#ifdef POINTER_BLANK 341#ifdef POINTER_BLANK
190 XColor blackcolour; 342 XColor blackcolour;
191 blackcolour.red = 0; 343 blackcolour.red = 0;
192 blackcolour.green = 0; 344 blackcolour.green = 0;
195 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', 347 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
196 &blackcolour, &blackcolour); 348 &blackcolour, &blackcolour);
197 XUnloadFont (display, f); 349 XUnloadFont (display, f);
198#endif 350#endif
199 351
200#ifdef PREFER_24BIT 352 int fd = XConnectionNumber (display);
201 /*
202 * If depth is not 24, look for a 24bit visual.
203 */
204 if (depth != 24)
205 {
206 XVisualInfo vinfo;
207 353
208 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) 354#ifndef NO_SLOW_LINK_SUPPORT
209 { 355 // try to detect wether we have a local connection.
210 depth = 24; 356 // assume unix domains socket == local, everything else not
211 visual = vinfo.visual; 357 // TODO: might want to check for inet/127.0.0.1
212 cmap = XCreateColormap (display, 358 is_local = 0;
213 RootWindow (display, screen), 359 sockaddr_un sa;
214 visual, AllocNone); 360 socklen_t sl = sizeof (sa);
215 } 361
216 } 362 if (!getsockname (fd, (sockaddr *)&sa, &sl))
363 is_local = sa.sun_family == AF_LOCAL;
217#endif 364#endif
218 365
219 x_ev.start (fd, EVENT_READ); 366 x_ev.start (fd, EVENT_READ);
220 fcntl (fd, F_SETFD, FD_CLOEXEC); 367 fcntl (fd, F_SETFD, FD_CLOEXEC);
221 368
222 XSelectInput (display, root, PropertyChangeMask); 369 XSelectInput (display, root, PropertyChangeMask);
223#ifdef USE_XIM
224 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
225#endif
226 370
227 flush (); 371 flush ();
228 372
229 return true; 373 return true;
230} 374}
231 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
232rxvt_display::~rxvt_display () 385rxvt_display::~rxvt_display ()
233{ 386{
387 if (!display)
388 return;
389
390#ifdef POINTER_BLANK
391 XFreeCursor (display, blank_cursor);
392#endif
234 x_ev.stop (); 393 x_ev.stop ();
235#ifdef USE_XIM 394#ifdef USE_XIM
236 xims.clear (); 395 xims.clear ();
237#endif 396#endif
238
239 if (display)
240 XCloseDisplay (display); 397 XCloseDisplay (display);
241} 398}
242 399
243#ifdef USE_XIM 400#ifdef USE_XIM
244void rxvt_display::im_change_cb () 401void rxvt_display::im_change_cb ()
245{ 402{
253 // registers, as xlib crashes due to a race otherwise. 410 // registers, as xlib crashes due to a race otherwise.
254 Atom actual_type, *atoms; 411 Atom actual_type, *atoms;
255 int actual_format; 412 int actual_format;
256 unsigned long nitems, bytes_after; 413 unsigned long nitems, bytes_after;
257 414
258 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, 415 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
259 False, XA_ATOM, &actual_type, &actual_format, 416 False, XA_ATOM, &actual_type, &actual_format,
260 &nitems, &bytes_after, (unsigned char **)&atoms) 417 &nitems, &bytes_after, (unsigned char **)&atoms)
261 != Success ) 418 != Success )
262 return; 419 return;
263 420
281 XNextEvent (display, &xev); 438 XNextEvent (display, &xev);
282 439
283#ifdef USE_XIM 440#ifdef USE_XIM
284 if (!XFilterEvent (&xev, None)) 441 if (!XFilterEvent (&xev, None))
285 { 442 {
286
287 if (xev.type == PropertyNotify 443 if (xev.type == PropertyNotify
288 && xev.xany.window == root 444 && xev.xany.window == root
289 && xev.xproperty.atom == xa_xim_servers) 445 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
290 im_change_check (); 446 im_change_check ();
291#endif 447#endif
292 for (int i = xw.size (); i--; ) 448 for (int i = xw.size (); i--; )
293 { 449 {
294 if (!xw[i]) 450 if (!xw[i])
384refcache<rxvt_display> displays; 540refcache<rxvt_display> displays;
385 541
386///////////////////////////////////////////////////////////////////////////// 542/////////////////////////////////////////////////////////////////////////////
387 543
388bool 544bool
389rxvt_color::set (rxvt_display *display, Pixel p) 545rxvt_color::set (rxvt_screen *screen, const char *name)
390{ 546{
391#if XFT 547#if XFT
392 XColor xc; 548 int l = strlen (name);
549 rxvt_rgba r;
550 char eos;
551 int mult;
393 552
394 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))
395 if (!XQueryColor (display->display, display->cmap, &xc)) 554 mult = rxvt_rgba::MAX_CC / 0x000f;
396 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);
397 563
398 XRenderColor d; 564 r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult;
399 565 return set (screen, r);
400 d.red = xc.red;
401 d.green = xc.green;
402 d.blue = xc.blue;
403 d.alpha = 0xffff;
404
405 return
406 XftColorAllocValue (display->display,
407 display->visual,
408 display->cmap,
409 &d, &c);
410#else
411 this->p = p;
412#endif
413
414 return true;
415}
416
417bool
418rxvt_color::set (rxvt_display *display, const char *name)
419{
420#if XFT
421 return XftColorAllocName (display->display, display->visual, display->cmap,
422 name, &c);
423#else 566#else
424 XColor xc; 567 XColor xc;
425 568
426 if (XParseColor (display->display, display->cmap, name, &xc)) 569 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
427 return set (display, xc.red, xc.green, xc.blue); 570 return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue));
428 571
429 return false; 572 return false;
430#endif 573#endif
431} 574}
432 575
433bool 576bool
434rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) 577rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba)
435{ 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
436 XColor xc; 615 XColor xc;
437 616
438 xc.red = cr; 617 xc.red = rgba.r;
439 xc.green = cg; 618 xc.green = rgba.g;
440 xc.blue = cb; 619 xc.blue = rgba.b;
441 xc.flags = DoRed | DoGreen | DoBlue; 620 xc.flags = DoRed | DoGreen | DoBlue;
442 621
443 if (XAllocColor (display->display, display->cmap, &xc)) 622 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
444 return set (display, xc.pixel); 623 {
624 p = xc.pixel;
625 return true;
626 }
445 627
446 return false; 628 return false;
629#endif
447} 630}
448 631
449void 632void
450rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) 633rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba)
451{ 634{
452#if XFT 635#if XFT
453 cr = c.color.red; 636 rgba.r = c.color.red;
454 cg = c.color.green; 637 rgba.g = c.color.green;
455 cb = c.color.blue; 638 rgba.b = c.color.blue;
639 rgba.a = c.color.alpha;
456#else 640#else
457 XColor c; 641 XColor c;
458 642
459 c.pixel = p; 643 c.pixel = p;
460 XQueryColor (display->display, display->cmap, &c); 644 XQueryColor (screen->xdisp, screen->cmap, &c);
461 645
462 cr = c.red; 646 rgba.r = c.red;
463 cg = c.green; 647 rgba.g = c.green;
464 cb = c.blue; 648 rgba.b = c.blue;
649 rgba.a = rxvt_rgba::MAX_CC;
465#endif 650#endif
466} 651}
467 652
468void 653void
469rxvt_color::free (rxvt_display *display) 654rxvt_color::free (rxvt_screen *screen)
470{ 655{
471#if XFT 656#if XFT
472 XftColorFree (display->display, display->visual, display->cmap, &c); 657 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
473#else 658#else
474 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); 659 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
475#endif 660#endif
476} 661}
477 662
478rxvt_color 663rxvt_color
479rxvt_color::fade (rxvt_display *display, int percent) 664rxvt_color::fade (rxvt_screen *screen, int percent)
480{ 665{
481 unsigned short cr, cg, cb;
482 rxvt_color faded; 666 rxvt_color faded;
483 667
484 get (display, cr, cg, cb); 668 rxvt_rgba c;
485 faded.set (display, 669 get (screen, c);
486 cr * percent / 100, 670
487 cg * percent / 100, 671 c.r = lerp (0, c.r, percent);
488 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);
489 676
490 return faded; 677 return faded;
491} 678}
492 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