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