ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.26
Committed: Wed Jan 25 21:03:04 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.25: +62 -47 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*--------------------------------*-C-*---------------------------------*
2 root 1.5 * File: rxvttoolkit.C
3 root 1.1 *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6     * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
7     *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21     *----------------------------------------------------------------------*/
22    
23     #include "../config.h"
24     #include <rxvt.h>
25     #include <rxvttoolkit.h>
26    
27     #include <unistd.h>
28     #include <fcntl.h>
29    
30 root 1.23 #include <sys/utsname.h>
31    
32 root 1.1 #ifndef NO_SLOW_LINK_SUPPORT
33     # include <sys/socket.h>
34     # include <sys/un.h>
35     #endif
36    
37 root 1.25 const 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    
75 root 1.1 refcounted::refcounted (const char *id)
76     {
77 root 1.2 this->id = strdup (id);
78 root 1.1 }
79    
80     refcounted::~refcounted ()
81     {
82     free (id);
83     }
84    
85     template<class T>
86     T *refcache<T>::get (const char *id)
87     {
88     for (T **i = this->begin (); i < this->end (); ++i)
89     {
90     if (!strcmp (id, (*i)->id))
91     {
92 root 1.23 ++(*i)->referenced;
93     (*i)->ref_next ();
94 root 1.1 return *i;
95     }
96     }
97    
98     T *obj = new T (id);
99    
100 root 1.23 if (obj && obj->ref_init ())
101 root 1.1 {
102 root 1.23 obj->referenced = 1;
103 root 1.1 this->push_back (obj);
104     return obj;
105     }
106     else
107     {
108     delete obj;
109     return 0;
110     }
111     }
112    
113     template<class T>
114     void refcache<T>::put (T *obj)
115     {
116     if (!obj)
117     return;
118    
119     if (!--obj->referenced)
120     {
121     this->erase (find (this->begin (), this->end (), obj));
122     delete obj;
123     }
124     }
125    
126     template<class T>
127 root 1.12 void refcache<T>::clear ()
128 root 1.1 {
129     while (this->size ())
130     put (*this->begin ());
131     }
132    
133     /////////////////////////////////////////////////////////////////////////////
134    
135     #ifdef USE_XIM
136 root 1.24
137 root 1.1 static void
138     #if XIMCB_PROTO_BROKEN
139     im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
140     #else
141     im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
142     #endif
143     {
144     rxvt_xim *xim = (rxvt_xim *)client_data;
145     rxvt_display *display = xim->display;
146    
147 root 1.5 xim->xim = 0;
148    
149 root 1.1 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
150     display->im_change_cb ();
151     }
152    
153 root 1.23 bool
154     rxvt_xim::ref_init ()
155 root 1.1 {
156     display = GET_R->display; //HACK: TODO
157    
158     xim = XOpenIM (display->display, NULL, NULL, NULL);
159    
160     if (!xim)
161     return false;
162    
163     XIMCallback ximcallback;
164     ximcallback.client_data = (XPointer)this;
165     ximcallback.callback = im_destroy_cb;
166    
167     XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
168    
169     return true;
170     }
171    
172     rxvt_xim::~rxvt_xim ()
173     {
174     if (xim)
175     XCloseIM (xim);
176     }
177 root 1.24
178 root 1.1 #endif
179    
180     /////////////////////////////////////////////////////////////////////////////
181    
182 root 1.26 void
183     rxvt_screen::set (rxvt_display *disp)
184     {
185     display = disp;
186    
187     xdisp = disp->display;
188    
189     Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
190    
191     depth = DefaultDepthOfScreen (screen);
192     visual = DefaultVisualOfScreen (screen);
193     cmap = DefaultColormapOfScreen (screen);
194     }
195    
196     void
197     rxvt_screen::set (rxvt_display *disp, int depth)
198     {
199     set (disp);
200    
201     XVisualInfo vinfo;
202    
203     if (XMatchVisualInfo (xdisp, display->screen, depth, TrueColor, &vinfo))
204     {
205     this->depth = depth;
206     this->visual = vinfo.visual;
207     this->cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
208     }
209     }
210    
211     void
212     rxvt_screen::clear ()
213     {
214     if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
215     XFreeColormap (xdisp, cmap);
216     }
217    
218     /////////////////////////////////////////////////////////////////////////////
219    
220 root 1.1 rxvt_display::rxvt_display (const char *id)
221     : refcounted (id)
222     , x_ev (this, &rxvt_display::x_cb)
223     , selection_owner (0)
224     {
225     }
226    
227 root 1.23 XrmDatabase
228     rxvt_display::get_resources ()
229     {
230     char *homedir = (char *)getenv ("HOME");
231     char fname[1024];
232    
233     /*
234     * get resources using the X library function
235     */
236     char *displayResource, *xe;
237     XrmDatabase database, rdb1;
238    
239     database = NULL;
240    
241     // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
242    
243     // 6. System wide per application default file.
244    
245     /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
246     if ((xe = (char *)getenv ("XAPPLRESDIR")))
247     {
248     snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
249    
250     if ((rdb1 = XrmGetFileDatabase (fname)))
251     XrmMergeDatabases (rdb1, &database);
252     }
253    
254     // 5. User's per application default file.
255     // none
256    
257     // 4. User's defaults file.
258     /* Get any Xserver defaults */
259     displayResource = XResourceManagerString (display);
260    
261     if (displayResource != NULL)
262     {
263     if ((rdb1 = XrmGetStringDatabase (displayResource)))
264     XrmMergeDatabases (rdb1, &database);
265     }
266     else if (homedir)
267     {
268     snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
269    
270     if ((rdb1 = XrmGetFileDatabase (fname)))
271     XrmMergeDatabases (rdb1, &database);
272     }
273    
274     /* Get screen specific resources */
275     displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
276    
277     if (displayResource != NULL)
278     {
279     if ((rdb1 = XrmGetStringDatabase (displayResource)))
280     /* Merge with screen-independent resources */
281     XrmMergeDatabases (rdb1, &database);
282    
283     XFree (displayResource);
284     }
285    
286     // 3. User's per host defaults file
287     /* Add in XENVIRONMENT file */
288     if ((xe = (char *)getenv ("XENVIRONMENT"))
289     && (rdb1 = XrmGetFileDatabase (xe)))
290     XrmMergeDatabases (rdb1, &database);
291     else if (homedir)
292     {
293     struct utsname un;
294    
295     if (!uname (&un))
296     {
297     snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
298    
299     if ((rdb1 = XrmGetFileDatabase (fname)))
300     XrmMergeDatabases (rdb1, &database);
301     }
302     }
303    
304     return database;
305     }
306    
307     bool rxvt_display::ref_init ()
308 root 1.1 {
309 root 1.14 #ifdef LOCAL_X_IS_UNIX
310     if (id[0] == ':')
311     {
312     val = rxvt_malloc (5 + strlen (id) + 1);
313     strcpy (val, "unix/");
314     strcat (val, id);
315     display = XOpenDisplay (val);
316     free (val);
317     }
318     else
319 root 1.15 #endif
320 root 1.14 display = 0;
321    
322     if (!display)
323     display = XOpenDisplay (id);
324 root 1.1
325     if (!display)
326     return false;
327    
328     screen = DefaultScreen (display);
329     root = DefaultRootWindow (display);
330    
331 root 1.25 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
332     XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
333    
334 root 1.23 XrmSetDatabase (display, get_resources ());
335 root 1.1
336 root 1.12 #ifdef POINTER_BLANK
337     XColor blackcolour;
338     blackcolour.red = 0;
339     blackcolour.green = 0;
340     blackcolour.blue = 0;
341     Font f = XLoadFont (display, "fixed");
342     blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
343     &blackcolour, &blackcolour);
344     XUnloadFont (display, f);
345     #endif
346    
347 root 1.23 int fd = XConnectionNumber (display);
348    
349     #ifndef NO_SLOW_LINK_SUPPORT
350     // try to detect wether we have a local connection.
351     // assume unix domains socket == local, everything else not
352     // TODO: might want to check for inet/127.0.0.1
353     is_local = 0;
354     sockaddr_un sa;
355     socklen_t sl = sizeof (sa);
356    
357     if (!getsockname (fd, (sockaddr *)&sa, &sl))
358     is_local = sa.sun_family == AF_LOCAL;
359     #endif
360    
361 root 1.1 x_ev.start (fd, EVENT_READ);
362     fcntl (fd, F_SETFD, FD_CLOEXEC);
363    
364     XSelectInput (display, root, PropertyChangeMask);
365    
366     flush ();
367    
368     return true;
369     }
370    
371 root 1.23 void
372     rxvt_display::ref_next ()
373     {
374     // TODO: somehow check wether the database files/resources changed
375     // before re-loading/parsing
376     XrmDestroyDatabase (XrmGetDatabase (display));
377     XrmSetDatabase (display, get_resources ());
378     }
379    
380 root 1.1 rxvt_display::~rxvt_display ()
381     {
382 root 1.22 if (!display)
383     return;
384    
385 root 1.21 #ifdef POINTER_BLANK
386     XFreeCursor (display, blank_cursor);
387     #endif
388 root 1.1 x_ev.stop ();
389 root 1.12 #ifdef USE_XIM
390     xims.clear ();
391     #endif
392 root 1.22 XCloseDisplay (display);
393 root 1.1 }
394    
395     #ifdef USE_XIM
396     void rxvt_display::im_change_cb ()
397     {
398     for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
399     (*i)->call ();
400     }
401 root 1.5
402     void rxvt_display::im_change_check ()
403     {
404 root 1.13 // try to only call im_change_cb when a new input method
405 root 1.5 // registers, as xlib crashes due to a race otherwise.
406     Atom actual_type, *atoms;
407     int actual_format;
408     unsigned long nitems, bytes_after;
409    
410 root 1.25 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
411 root 1.5 False, XA_ATOM, &actual_type, &actual_format,
412     &nitems, &bytes_after, (unsigned char **)&atoms)
413     != Success )
414     return;
415    
416     if (actual_type == XA_ATOM && actual_format == 32)
417     for (int i = 0; i < nitems; i++)
418     if (XGetSelectionOwner (display, atoms[i]))
419     {
420     im_change_cb ();
421     break;
422     }
423    
424     XFree (atoms);
425     }
426 root 1.1 #endif
427    
428     void rxvt_display::x_cb (io_watcher &w, short revents)
429     {
430     do
431     {
432     XEvent xev;
433     XNextEvent (display, &xev);
434    
435 root 1.5 #ifdef USE_XIM
436     if (!XFilterEvent (&xev, None))
437     {
438     if (xev.type == PropertyNotify
439     && xev.xany.window == root
440 root 1.25 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
441 root 1.5 im_change_check ();
442     #endif
443     for (int i = xw.size (); i--; )
444     {
445     if (!xw[i])
446     xw.erase_unordered (i);
447     else if (xw[i]->window == xev.xany.window)
448     xw[i]->call (xev);
449     }
450 root 1.1 #ifdef USE_XIM
451 root 1.5 }
452 root 1.1 #endif
453     }
454 root 1.6 while (XEventsQueued (display, QueuedAlready));
455 root 1.1
456 root 1.10 XFlush (display);
457 root 1.1 }
458    
459     void rxvt_display::flush ()
460     {
461 root 1.10 if (XEventsQueued (display, QueuedAlready))
462     x_cb (x_ev, EVENT_READ);
463    
464 root 1.8 XFlush (display);
465 root 1.1 }
466    
467     void rxvt_display::reg (xevent_watcher *w)
468     {
469     xw.push_back (w);
470     w->active = xw.size ();
471     }
472    
473     void rxvt_display::unreg (xevent_watcher *w)
474     {
475     if (w->active)
476     xw[w->active - 1] = 0;
477     }
478    
479     void rxvt_display::set_selection_owner (rxvt_term *owner)
480     {
481     if (selection_owner && selection_owner != owner)
482     selection_owner->selection_clear ();
483    
484     selection_owner = owner;
485     }
486    
487     #ifdef USE_XIM
488     void rxvt_display::reg (im_watcher *w)
489     {
490     imw.push_back (w);
491     }
492    
493     void rxvt_display::unreg (im_watcher *w)
494     {
495     imw.erase (find (imw.begin (), imw.end (), w));
496     }
497    
498     rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
499     {
500     char *id;
501     int l, m;
502    
503     l = strlen (locale);
504     m = strlen (modifiers);
505    
506     if (!(id = (char *)malloc (l + m + 2)))
507     return 0;
508    
509     memcpy (id, locale, l); id[l] = '\n';
510     memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
511    
512     rxvt_xim *xim = xims.get (id);
513    
514     free (id);
515    
516     return xim;
517     }
518    
519     void rxvt_display::put_xim (rxvt_xim *xim)
520     {
521 root 1.10 #if XLIB_IS_RACEFREE
522 root 1.1 xims.put (xim);
523 root 1.10 #endif
524 root 1.1 }
525     #endif
526    
527     Atom rxvt_display::atom (const char *name)
528     {
529     return XInternAtom (display, name, False);
530     }
531    
532     /////////////////////////////////////////////////////////////////////////////
533    
534     template class refcache<rxvt_display>;
535     refcache<rxvt_display> displays;
536    
537     /////////////////////////////////////////////////////////////////////////////
538    
539     bool
540 root 1.26 rxvt_color::set (rxvt_screen *screen, Pixel p)
541 root 1.1 {
542     #if XFT
543     XColor xc;
544    
545     xc.pixel = p;
546 root 1.26 if (!XQueryColor (screen->xdisp, screen->cmap, &xc))
547 root 1.1 return false;
548    
549     XRenderColor d;
550    
551     d.red = xc.red;
552     d.green = xc.green;
553     d.blue = xc.blue;
554     d.alpha = 0xffff;
555    
556     return
557 root 1.26 XftColorAllocValue (screen->xdisp,
558     screen->visual,
559     screen->cmap,
560 root 1.1 &d, &c);
561     #else
562     this->p = p;
563     #endif
564    
565     return true;
566     }
567    
568     bool
569 root 1.26 rxvt_color::set (rxvt_screen *screen, const char *name)
570 root 1.1 {
571 root 1.7 #if XFT
572 root 1.26 return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c);
573 root 1.7 #else
574 root 1.1 XColor xc;
575    
576 root 1.26 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
577     return set (screen, xc.red, xc.green, xc.blue);
578 root 1.1
579     return false;
580 root 1.7 #endif
581 root 1.1 }
582    
583     bool
584 root 1.26 rxvt_color::set (rxvt_screen *screen, unsigned short cr, unsigned short cg, unsigned short cb)
585 root 1.1 {
586     XColor xc;
587    
588     xc.red = cr;
589     xc.green = cg;
590     xc.blue = cb;
591     xc.flags = DoRed | DoGreen | DoBlue;
592    
593 root 1.26 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
594     return set (screen, xc.pixel);
595 root 1.1
596     return false;
597     }
598    
599     void
600 root 1.26 rxvt_color::get (rxvt_screen *screen, unsigned short &cr, unsigned short &cg, unsigned short &cb)
601 root 1.1 {
602     #if XFT
603     cr = c.color.red;
604     cg = c.color.green;
605     cb = c.color.blue;
606     #else
607     XColor c;
608    
609     c.pixel = p;
610 root 1.26 XQueryColor (screen->xdisp, screen->cmap, &c);
611 root 1.1
612     cr = c.red;
613     cg = c.green;
614     cb = c.blue;
615     #endif
616     }
617    
618     void
619 root 1.26 rxvt_color::free (rxvt_screen *screen)
620 root 1.1 {
621     #if XFT
622 root 1.26 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
623 root 1.1 #else
624 root 1.26 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
625 root 1.1 #endif
626     }
627    
628     rxvt_color
629 root 1.26 rxvt_color::fade (rxvt_screen *screen, int percent)
630 root 1.1 {
631 root 1.18 percent = 100 - percent;
632    
633 root 1.1 unsigned short cr, cg, cb;
634     rxvt_color faded;
635    
636 root 1.26 get (screen, cr, cg, cb);
637 root 1.18
638     faded.set (
639 root 1.26 screen,
640 root 1.18 cr * percent / 100,
641     cg * percent / 100,
642     cb * percent / 100
643     );
644    
645     return faded;
646     }
647    
648 root 1.19 #define LERP(a,b,p) (a * p + b * (100 - p)) / 100
649 root 1.18
650     rxvt_color
651 root 1.26 rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
652 root 1.18 {
653     percent = 100 - percent;
654    
655     unsigned short cr, cg, cb;
656     unsigned short fcr, fcg, fcb;
657     rxvt_color faded;
658    
659 root 1.26 get (screen, cr, cg, cb);
660     fadeto.get (screen, fcr, fcg, fcb);
661 root 1.18
662     faded.set (
663 root 1.26 screen,
664 root 1.18 LERP (cr, fcr, percent),
665     LERP (cg, fcg, percent),
666     LERP (cb, fcb, percent)
667     );
668 root 1.1
669     return faded;
670     }
671