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.1 by root, Sun Aug 15 00:37:04 2004 UTC vs.
Revision 1.28 by root, Sun Jan 29 20:51:28 2006 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: rxvtcolor.C 2 * File: rxvttoolkit.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> 6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
7 * 7 *
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
37const char *const xa_names[] =
38 {
39 "TEXT",
40 "COMPOUND_TEXT",
41 "UTF8_STRING",
42 "MULTIPLE",
43 "TARGETS",
44 "TIMESTAMP",
45 "VT_SELECTION",
46 "INCR",
47 "WM_PROTOCOLS",
48 "WM_DELETE_WINDOW",
49 "CLIPBOARD",
50#if ENABLE_FRILLS
51 "_MOTIF_WM_HINTS",
52#endif
53#if ENABLE_EWMH
54 "_NET_WM_PID",
55 "_NET_WM_NAME",
56 "_NET_WM_ICON_NAME",
57 "_NET_WM_PING",
58#endif
59#if USE_XIM
60 "WM_LOCALE_NAME",
61 "XIM_SERVERS",
62#endif
63#ifdef TRANSPARENT
64 "_XROOTPMAP_ID",
65 "ESETROOT_PMAP_ID",
66#endif
67#if ENABLE_XEMBED
68 "_XEMBED",
69 "_XEMBED_INFO",
70#endif
71 };
72
73/////////////////////////////////////////////////////////////////////////////
74
35refcounted::refcounted (const char *id) 75refcounted::refcounted (const char *id)
36{ 76{
37 this->id = STRDUP (id); 77 this->id = strdup (id);
38} 78}
39 79
40refcounted::~refcounted () 80refcounted::~refcounted ()
41{ 81{
42 free (id); 82 free (id);
47{ 87{
48 for (T **i = this->begin (); i < this->end (); ++i) 88 for (T **i = this->begin (); i < this->end (); ++i)
49 { 89 {
50 if (!strcmp (id, (*i)->id)) 90 if (!strcmp (id, (*i)->id))
51 { 91 {
52 (*i)->referenced++; 92 ++(*i)->referenced;
93 (*i)->ref_next ();
53 return *i; 94 return *i;
54 } 95 }
55 } 96 }
56 97
57 T *obj = new T (id); 98 T *obj = new T (id);
58 99
59 obj->referenced = 1;
60
61 if (obj && obj->init ()) 100 if (obj && obj->ref_init ())
62 { 101 {
102 obj->referenced = 1;
63 this->push_back (obj); 103 this->push_back (obj);
64 return obj; 104 return obj;
65 } 105 }
66 else 106 else
67 { 107 {
82 delete obj; 122 delete obj;
83 } 123 }
84} 124}
85 125
86template<class T> 126template<class T>
87refcache<T>::~refcache () 127void refcache<T>::clear ()
88{ 128{
89 while (this->size ()) 129 while (this->size ())
90 put (*this->begin ()); 130 put (*this->begin ());
91} 131}
92 132
93///////////////////////////////////////////////////////////////////////////// 133/////////////////////////////////////////////////////////////////////////////
94 134
95#ifdef USE_XIM 135#ifdef USE_XIM
136
96static void 137static void
97#if XIMCB_PROTO_BROKEN 138#if XIMCB_PROTO_BROKEN
98im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) 139im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
99#else 140#else
100im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) 141im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
101#endif 142#endif
102{ 143{
103 rxvt_xim *xim = (rxvt_xim *)client_data; 144 rxvt_xim *xim = (rxvt_xim *)client_data;
104 rxvt_display *display = xim->display; 145 rxvt_display *display = xim->display;
105 146
147 xim->xim = 0;
148
106 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); 149 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
107
108 display->im_change_cb (); 150 display->im_change_cb ();
109} 151}
110 152
153bool
111bool rxvt_xim::init () 154rxvt_xim::ref_init ()
112{ 155{
113 display = GET_R->display; //HACK: TODO 156 display = GET_R->display; //HACK: TODO
114 157
115 xim = XOpenIM (display->display, NULL, NULL, NULL); 158 xim = XOpenIM (display->display, NULL, NULL, NULL);
116 159
129rxvt_xim::~rxvt_xim () 172rxvt_xim::~rxvt_xim ()
130{ 173{
131 if (xim) 174 if (xim)
132 XCloseIM (xim); 175 XCloseIM (xim);
133} 176}
177
134#endif 178#endif
179
180/////////////////////////////////////////////////////////////////////////////
181
182void
183rxvt_screen::set (rxvt_display *disp)
184{
185 display = disp;
186 xdisp = disp->display;
187
188 Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
189
190 depth = DefaultDepthOfScreen (screen);
191 visual = DefaultVisualOfScreen (screen);
192 cmap = DefaultColormapOfScreen (screen);
193}
194
195void
196rxvt_screen::set (rxvt_display *disp, int bitdepth)
197{
198 set (disp);
199
200 XVisualInfo vinfo;
201
202 if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo))
203 {
204 depth = bitdepth;
205 visual = vinfo.visual;
206 cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
207 }
208}
209
210void
211rxvt_screen::clear ()
212{
213 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
214 XFreeColormap (xdisp, cmap);
215}
135 216
136///////////////////////////////////////////////////////////////////////////// 217/////////////////////////////////////////////////////////////////////////////
137 218
138rxvt_display::rxvt_display (const char *id) 219rxvt_display::rxvt_display (const char *id)
139: refcounted (id) 220: refcounted (id)
140, x_ev (this, &rxvt_display::x_cb) 221, x_ev (this, &rxvt_display::x_cb)
141, selection_owner (0) 222, selection_owner (0)
142{ 223{
143} 224}
144 225
226XrmDatabase
227rxvt_display::get_resources ()
228{
229 char *homedir = (char *)getenv ("HOME");
230 char fname[1024];
231
232 /*
233 * get resources using the X library function
234 */
235 char *displayResource, *xe;
236 XrmDatabase database, rdb1;
237
238 database = NULL;
239
240 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
241
242 // 6. System wide per application default file.
243
244 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
245 if ((xe = (char *)getenv ("XAPPLRESDIR")))
246 {
247 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
248
249 if ((rdb1 = XrmGetFileDatabase (fname)))
250 XrmMergeDatabases (rdb1, &database);
251 }
252
253 // 5. User's per application default file.
254 // none
255
256 // 4. User's defaults file.
257 /* Get any Xserver defaults */
258 displayResource = XResourceManagerString (display);
259
260 if (displayResource != NULL)
261 {
262 if ((rdb1 = XrmGetStringDatabase (displayResource)))
263 XrmMergeDatabases (rdb1, &database);
264 }
265 else if (homedir)
266 {
267 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
268
269 if ((rdb1 = XrmGetFileDatabase (fname)))
270 XrmMergeDatabases (rdb1, &database);
271 }
272
273 /* Get screen specific resources */
274 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
275
276 if (displayResource != NULL)
277 {
278 if ((rdb1 = XrmGetStringDatabase (displayResource)))
279 /* Merge with screen-independent resources */
280 XrmMergeDatabases (rdb1, &database);
281
282 XFree (displayResource);
283 }
284
285 // 3. User's per host defaults file
286 /* Add in XENVIRONMENT file */
287 if ((xe = (char *)getenv ("XENVIRONMENT"))
288 && (rdb1 = XrmGetFileDatabase (xe)))
289 XrmMergeDatabases (rdb1, &database);
290 else if (homedir)
291 {
292 struct utsname un;
293
294 if (!uname (&un))
295 {
296 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
297
298 if ((rdb1 = XrmGetFileDatabase (fname)))
299 XrmMergeDatabases (rdb1, &database);
300 }
301 }
302
303 return database;
304}
305
145bool rxvt_display::init () 306bool rxvt_display::ref_init ()
146{ 307{
308#ifdef LOCAL_X_IS_UNIX
309 if (id[0] == ':')
310 {
311 val = rxvt_malloc (5 + strlen (id) + 1);
312 strcpy (val, "unix/");
313 strcat (val, id);
314 display = XOpenDisplay (val);
315 free (val);
316 }
317 else
318#endif
319 display = 0;
320
321 if (!display)
147 display = XOpenDisplay (id); 322 display = XOpenDisplay (id);
148 323
149 if (!display) 324 if (!display)
150 return false; 325 return false;
151 326
152 screen = DefaultScreen (display); 327 screen = DefaultScreen (display);
153 root = DefaultRootWindow (display); 328 root = DefaultRootWindow (display);
154 visual = DefaultVisual (display, screen); 329
155 cmap = DefaultColormap (display, screen); 330 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
156 depth = DefaultDepth (display, screen); 331 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
332
333 XrmSetDatabase (display, get_resources ());
334
335#ifdef POINTER_BLANK
336 XColor blackcolour;
337 blackcolour.red = 0;
338 blackcolour.green = 0;
339 blackcolour.blue = 0;
340 Font f = XLoadFont (display, "fixed");
341 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
342 &blackcolour, &blackcolour);
343 XUnloadFont (display, f);
344#endif
157 345
158 int fd = XConnectionNumber (display); 346 int fd = XConnectionNumber (display);
159 347
160#ifndef NO_SLOW_LINK_SUPPORT 348#ifndef NO_SLOW_LINK_SUPPORT
161 // try to detetc wether we have a local connection. 349 // try to detect wether we have a local connection.
162 // assume unix domains socket == local, everything else not 350 // assume unix domains socket == local, everything else not
163 // TODO: might want to check for inet/127.0.0.1 351 // TODO: might want to check for inet/127.0.0.1
164 is_local = 0; 352 is_local = 0;
165 sockaddr_un sa; 353 sockaddr_un sa;
166 socklen_t sl = sizeof (sa); 354 socklen_t sl = sizeof (sa);
167 355
168 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 356 if (!getsockname (fd, (sockaddr *)&sa, &sl))
169 is_local = sa.sun_family == AF_LOCAL; 357 is_local = sa.sun_family == AF_LOCAL;
170#endif 358#endif
171 359
172#ifdef PREFER_24BIT
173 /*
174 * If depth is not 24, look for a 24bit visual.
175 */
176 if (depth != 24)
177 {
178 XVisualInfo vinfo;
179
180 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
181 {
182 depth = 24;
183 visual = vinfo.visual;
184 cmap = XCreateColormap (display,
185 RootWindow (display, screen),
186 visual, AllocNone);
187 }
188 }
189#endif
190
191 x_ev.start (fd, EVENT_READ); 360 x_ev.start (fd, EVENT_READ);
192 fcntl (fd, F_SETFD, FD_CLOEXEC); 361 fcntl (fd, F_SETFD, FD_CLOEXEC);
193 362
194 XSelectInput (display, root, PropertyChangeMask); 363 XSelectInput (display, root, PropertyChangeMask);
364
365 flush ();
366
367 return true;
368}
369
370void
371rxvt_display::ref_next ()
372{
373 // TODO: somehow check wether the database files/resources changed
374 // before re-loading/parsing
375 XrmDestroyDatabase (XrmGetDatabase (display));
376 XrmSetDatabase (display, get_resources ());
377}
378
379rxvt_display::~rxvt_display ()
380{
381 if (!display)
382 return;
383
384#ifdef POINTER_BLANK
385 XFreeCursor (display, blank_cursor);
386#endif
387 x_ev.stop ();
195#ifdef USE_XIM 388#ifdef USE_XIM
196 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); 389 xims.clear ();
197#endif 390#endif
198
199 flush ();
200
201 return true;
202}
203
204rxvt_display::~rxvt_display ()
205{
206 x_ev.stop ();
207
208 if (display)
209 XCloseDisplay (display); 391 XCloseDisplay (display);
210} 392}
211 393
212#ifdef USE_XIM 394#ifdef USE_XIM
213void rxvt_display::im_change_cb () 395void rxvt_display::im_change_cb ()
214{ 396{
215 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 397 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
216 (*i)->call (); 398 (*i)->call ();
217} 399}
400
401void rxvt_display::im_change_check ()
402{
403 // try to only call im_change_cb when a new input method
404 // registers, as xlib crashes due to a race otherwise.
405 Atom actual_type, *atoms;
406 int actual_format;
407 unsigned long nitems, bytes_after;
408
409 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
410 False, XA_ATOM, &actual_type, &actual_format,
411 &nitems, &bytes_after, (unsigned char **)&atoms)
412 != Success )
413 return;
414
415 if (actual_type == XA_ATOM && actual_format == 32)
416 for (int i = 0; i < nitems; i++)
417 if (XGetSelectionOwner (display, atoms[i]))
418 {
419 im_change_cb ();
420 break;
421 }
422
423 XFree (atoms);
424}
218#endif 425#endif
219 426
220void rxvt_display::x_cb (io_watcher &w, short revents) 427void rxvt_display::x_cb (io_watcher &w, short revents)
221{ 428{
222 do 429 do
223 { 430 {
224 XEvent xev; 431 XEvent xev;
225 XNextEvent (display, &xev); 432 XNextEvent (display, &xev);
226 433
227 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
228
229#ifdef USE_XIM 434#ifdef USE_XIM
230 if (xev.type == PropertyNotify 435 if (!XFilterEvent (&xev, None))
231 && xev.xany.window == root
232 && xev.xproperty.atom == xa_xim_servers)
233 im_change_cb ();
234#endif
235
236 for (int i = xw.size (); i--; )
237 { 436 {
437 if (xev.type == PropertyNotify
438 && xev.xany.window == root
439 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
440 im_change_check ();
441#endif
442 for (int i = xw.size (); i--; )
443 {
238 if (!xw[i]) 444 if (!xw[i])
239 xw.erase_unordered (i); 445 xw.erase_unordered (i);
240 else if (xw[i]->window == xev.xany.window) 446 else if (xw[i]->window == xev.xany.window)
241 xw[i]->call (xev); 447 xw[i]->call (xev);
448 }
449#ifdef USE_XIM
242 } 450 }
451#endif
243 } 452 }
244 while (XPending (display)); 453 while (XEventsQueued (display, QueuedAlready));
245 454
246 flush (); 455 XFlush (display);
247} 456}
248 457
249void rxvt_display::flush () 458void rxvt_display::flush ()
250{ 459{
251 for (;;) 460 if (XEventsQueued (display, QueuedAlready))
252 { 461 x_cb (x_ev, EVENT_READ);
253 if (!XPending (display))
254 break;
255 462
256 x_cb (x_ev, 0); 463 XFlush (display);
257 }
258} 464}
259 465
260void rxvt_display::reg (xevent_watcher *w) 466void rxvt_display::reg (xevent_watcher *w)
261{ 467{
262 xw.push_back (w); 468 xw.push_back (w);
309 return xim; 515 return xim;
310} 516}
311 517
312void rxvt_display::put_xim (rxvt_xim *xim) 518void rxvt_display::put_xim (rxvt_xim *xim)
313{ 519{
520#if XLIB_IS_RACEFREE
314 xims.put (xim); 521 xims.put (xim);
522#endif
315} 523}
316#endif 524#endif
317 525
318Atom rxvt_display::atom (const char *name) 526Atom rxvt_display::atom (const char *name)
319{ 527{
326refcache<rxvt_display> displays; 534refcache<rxvt_display> displays;
327 535
328///////////////////////////////////////////////////////////////////////////// 536/////////////////////////////////////////////////////////////////////////////
329 537
330bool 538bool
331rxvt_color::set (rxvt_display *display, Pixel p) 539rxvt_color::set (rxvt_screen *screen, const char *name)
332{ 540{
333#if XFT 541#if XFT
542 int l = strlen (name);
543 rxvt_rgba r;
544 char eos;
545 int mult;
546
547 if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
548 mult = 0x1111;
549 else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
550 mult = 0x0101;
551 else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos))
552 mult = 0x0001;
553 else
554 return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c);
555
556 r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult;
557 return set (screen, r);
558#else
334 XColor xc; 559 XColor xc;
335 560
336 xc.pixel = p; 561 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
337 if (!XQueryColor (display->display, display->cmap, &xc)) 562 return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue));
563
338 return false; 564 return false;
565#endif
566}
339 567
568bool
569rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba)
570{
571#if XFT
340 XRenderColor d; 572 XRenderColor d;
341 573
342 d.red = xc.red; 574 d.red = rgba.r;
343 d.green = xc.green; 575 d.green = rgba.g;
344 d.blue = xc.blue; 576 d.blue = rgba.b;
345 d.alpha = 0xffff; 577 d.alpha = rgba.a;
346 578
347 return 579 return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c);
348 XftColorAllocValue (display->display,
349 display->visual,
350 display->cmap,
351 &d, &c);
352#else 580#else
353 this->p = p;
354#endif
355
356 return true;
357}
358
359bool
360rxvt_color::set (rxvt_display *display, const char *name)
361{
362 XColor xc; 581 XColor xc;
363 582
364 if (XParseColor (display->display, display->cmap, name, &xc)) 583 xc.red = rgba.r;
365 return set (display, xc.red, xc.green, xc.blue); 584 xc.green = rgba.g;
585 xc.blue = rgba.b;
586 xc.flags = DoRed | DoGreen | DoBlue;
587
588 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
589 {
590 p = xc.pixel;
591 return true;
592 }
366 593
367 return false; 594 return false;
368} 595#endif
369
370bool
371rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
372{
373 XColor xc;
374
375 xc.red = cr;
376 xc.green = cg;
377 xc.blue = cb;
378 xc.flags = DoRed | DoGreen | DoBlue;
379
380 if (XAllocColor (display->display, display->cmap, &xc))
381 return set (display, xc.pixel);
382
383 return false;
384} 596}
385 597
386void 598void
387rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) 599rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba)
388{ 600{
389#if XFT 601#if XFT
390 cr = c.color.red; 602 rgba.r = c.color.red;
391 cg = c.color.green; 603 rgba.g = c.color.green;
392 cb = c.color.blue; 604 rgba.b = c.color.blue;
605 rgba.a = c.color.alpha;
393#else 606#else
394 XColor c; 607 XColor c;
395 608
396 c.pixel = p; 609 c.pixel = p;
397 XQueryColor (display->display, display->cmap, &c); 610 XQueryColor (screen->xdisp, screen->cmap, &c);
398 611
399 cr = c.red; 612 rgba.r = c.red;
400 cg = c.green; 613 rgba.g = c.green;
401 cb = c.blue; 614 rgba.b = c.blue;
615 rgba.a = rxvt_rgba::MAX_CC;
402#endif 616#endif
403} 617}
404 618
405void 619void
406rxvt_color::free (rxvt_display *display) 620rxvt_color::free (rxvt_screen *screen)
407{ 621{
408#if XFT 622#if XFT
409 XftColorFree (display->display, display->visual, display->cmap, &c); 623 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
410#else 624#else
411 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); 625 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
412#endif 626#endif
413} 627}
414 628
415rxvt_color 629rxvt_color
416rxvt_color::fade (rxvt_display *display, int percent) 630rxvt_color::fade (rxvt_screen *screen, int percent)
417{ 631{
418 unsigned short cr, cg, cb;
419 rxvt_color faded; 632 rxvt_color faded;
420 633
421 get (display, cr, cg, cb); 634 rxvt_rgba c;
422 faded.set (display, 635 get (screen, c);
423 cr * percent / 100, 636
424 cg * percent / 100, 637 c.r = lerp (0, c.r, percent);
425 cb * percent / 100); 638 c.g = lerp (0, c.g, percent);
639 c.b = lerp (0, c.b, percent);
640
641 faded.set (screen, c);
426 642
427 return faded; 643 return faded;
428} 644}
429 645
646rxvt_color
647rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
648{
649 rxvt_rgba c, fc;
650 rxvt_color faded;
651
652 get (screen, c);
653 fadeto.get (screen, fc);
654
655 faded.set (
656 screen,
657 rxvt_rgba (
658 lerp (fc.r, c.r, percent),
659 lerp (fc.g, c.g, percent),
660 lerp (fc.b, c.b, percent),
661 lerp (fc.a, c.a, percent)
662 )
663 );
664
665 return faded;
666}
667

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines