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.2 by root, Sun Aug 15 04:54:21 2004 UTC vs.
Revision 1.25 by root, Wed Jan 25 02:42:06 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
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
135 179
136///////////////////////////////////////////////////////////////////////////// 180/////////////////////////////////////////////////////////////////////////////
137 181
138rxvt_display::rxvt_display (const char *id) 182rxvt_display::rxvt_display (const char *id)
140, x_ev (this, &rxvt_display::x_cb) 184, x_ev (this, &rxvt_display::x_cb)
141, selection_owner (0) 185, selection_owner (0)
142{ 186{
143} 187}
144 188
189XrmDatabase
190rxvt_display::get_resources ()
191{
192 char *homedir = (char *)getenv ("HOME");
193 char fname[1024];
194
195 /*
196 * get resources using the X library function
197 */
198 char *displayResource, *xe;
199 XrmDatabase database, rdb1;
200
201 database = NULL;
202
203 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
204
205 // 6. System wide per application default file.
206
207 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
208 if ((xe = (char *)getenv ("XAPPLRESDIR")))
209 {
210 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
211
212 if ((rdb1 = XrmGetFileDatabase (fname)))
213 XrmMergeDatabases (rdb1, &database);
214 }
215
216 // 5. User's per application default file.
217 // none
218
219 // 4. User's defaults file.
220 /* Get any Xserver defaults */
221 displayResource = XResourceManagerString (display);
222
223 if (displayResource != NULL)
224 {
225 if ((rdb1 = XrmGetStringDatabase (displayResource)))
226 XrmMergeDatabases (rdb1, &database);
227 }
228 else if (homedir)
229 {
230 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
231
232 if ((rdb1 = XrmGetFileDatabase (fname)))
233 XrmMergeDatabases (rdb1, &database);
234 }
235
236 /* Get screen specific resources */
237 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
238
239 if (displayResource != NULL)
240 {
241 if ((rdb1 = XrmGetStringDatabase (displayResource)))
242 /* Merge with screen-independent resources */
243 XrmMergeDatabases (rdb1, &database);
244
245 XFree (displayResource);
246 }
247
248 // 3. User's per host defaults file
249 /* Add in XENVIRONMENT file */
250 if ((xe = (char *)getenv ("XENVIRONMENT"))
251 && (rdb1 = XrmGetFileDatabase (xe)))
252 XrmMergeDatabases (rdb1, &database);
253 else if (homedir)
254 {
255 struct utsname un;
256
257 if (!uname (&un))
258 {
259 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
260
261 if ((rdb1 = XrmGetFileDatabase (fname)))
262 XrmMergeDatabases (rdb1, &database);
263 }
264 }
265
266 return database;
267}
268
145bool rxvt_display::init () 269bool rxvt_display::ref_init ()
146{ 270{
271#ifdef LOCAL_X_IS_UNIX
272 if (id[0] == ':')
273 {
274 val = rxvt_malloc (5 + strlen (id) + 1);
275 strcpy (val, "unix/");
276 strcat (val, id);
277 display = XOpenDisplay (val);
278 free (val);
279 }
280 else
281#endif
282 display = 0;
283
284 if (!display)
147 display = XOpenDisplay (id); 285 display = XOpenDisplay (id);
148 286
149 if (!display) 287 if (!display)
150 return false; 288 return false;
151 289
152 screen = DefaultScreen (display); 290 screen = DefaultScreen (display);
153 root = DefaultRootWindow (display); 291 root = DefaultRootWindow (display);
154 visual = DefaultVisual (display, screen); 292 visual = DefaultVisual (display, screen);
155 cmap = DefaultColormap (display, screen); 293 cmap = DefaultColormap (display, screen);
156 depth = DefaultDepth (display, screen); 294 depth = DefaultDepth (display, screen);
157 295
158 int fd = XConnectionNumber (display); 296 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
297 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
159 298
160#ifndef NO_SLOW_LINK_SUPPORT 299 XrmSetDatabase (display, get_resources ());
161 // try to detetc wether we have a local connection.
162 // assume unix domains socket == local, everything else not
163 // TODO: might want to check for inet/127.0.0.1
164 is_local = 0;
165 sockaddr_un sa;
166 socklen_t sl = sizeof (sa);
167 300
168 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 301#ifdef POINTER_BLANK
169 is_local = sa.sun_family == AF_LOCAL; 302 XColor blackcolour;
303 blackcolour.red = 0;
304 blackcolour.green = 0;
305 blackcolour.blue = 0;
306 Font f = XLoadFont (display, "fixed");
307 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
308 &blackcolour, &blackcolour);
309 XUnloadFont (display, f);
170#endif 310#endif
171 311
172#ifdef PREFER_24BIT 312#ifdef PREFER_24BIT
173 /* 313 /*
174 * If depth is not 24, look for a 24bit visual. 314 * If depth is not 24, look for a 24bit visual.
186 visual, AllocNone); 326 visual, AllocNone);
187 } 327 }
188 } 328 }
189#endif 329#endif
190 330
331 int fd = XConnectionNumber (display);
332
333#ifndef NO_SLOW_LINK_SUPPORT
334 // try to detect wether we have a local connection.
335 // assume unix domains socket == local, everything else not
336 // TODO: might want to check for inet/127.0.0.1
337 is_local = 0;
338 sockaddr_un sa;
339 socklen_t sl = sizeof (sa);
340
341 if (!getsockname (fd, (sockaddr *)&sa, &sl))
342 is_local = sa.sun_family == AF_LOCAL;
343#endif
344
191 x_ev.start (fd, EVENT_READ); 345 x_ev.start (fd, EVENT_READ);
192 fcntl (fd, F_SETFD, FD_CLOEXEC); 346 fcntl (fd, F_SETFD, FD_CLOEXEC);
193 347
194 XSelectInput (display, root, PropertyChangeMask); 348 XSelectInput (display, root, PropertyChangeMask);
349
350 flush ();
351
352 return true;
353}
354
355void
356rxvt_display::ref_next ()
357{
358 // TODO: somehow check wether the database files/resources changed
359 // before re-loading/parsing
360 XrmDestroyDatabase (XrmGetDatabase (display));
361 XrmSetDatabase (display, get_resources ());
362}
363
364rxvt_display::~rxvt_display ()
365{
366 if (!display)
367 return;
368
369#ifdef POINTER_BLANK
370 XFreeCursor (display, blank_cursor);
371#endif
372 x_ev.stop ();
195#ifdef USE_XIM 373#ifdef USE_XIM
196 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); 374 xims.clear ();
197#endif 375#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); 376 XCloseDisplay (display);
210} 377}
211 378
212#ifdef USE_XIM 379#ifdef USE_XIM
213void rxvt_display::im_change_cb () 380void rxvt_display::im_change_cb ()
214{ 381{
215 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 382 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
216 (*i)->call (); 383 (*i)->call ();
217} 384}
385
386void rxvt_display::im_change_check ()
387{
388 // try to only call im_change_cb when a new input method
389 // registers, as xlib crashes due to a race otherwise.
390 Atom actual_type, *atoms;
391 int actual_format;
392 unsigned long nitems, bytes_after;
393
394 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
395 False, XA_ATOM, &actual_type, &actual_format,
396 &nitems, &bytes_after, (unsigned char **)&atoms)
397 != Success )
398 return;
399
400 if (actual_type == XA_ATOM && actual_format == 32)
401 for (int i = 0; i < nitems; i++)
402 if (XGetSelectionOwner (display, atoms[i]))
403 {
404 im_change_cb ();
405 break;
406 }
407
408 XFree (atoms);
409}
218#endif 410#endif
219 411
220void rxvt_display::x_cb (io_watcher &w, short revents) 412void rxvt_display::x_cb (io_watcher &w, short revents)
221{ 413{
222 do 414 do
223 { 415 {
224 XEvent xev; 416 XEvent xev;
225 XNextEvent (display, &xev); 417 XNextEvent (display, &xev);
226 418
227 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
228
229#ifdef USE_XIM 419#ifdef USE_XIM
230 if (xev.type == PropertyNotify 420 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 { 421 {
422 if (xev.type == PropertyNotify
423 && xev.xany.window == root
424 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
425 im_change_check ();
426#endif
427 for (int i = xw.size (); i--; )
428 {
238 if (!xw[i]) 429 if (!xw[i])
239 xw.erase_unordered (i); 430 xw.erase_unordered (i);
240 else if (xw[i]->window == xev.xany.window) 431 else if (xw[i]->window == xev.xany.window)
241 xw[i]->call (xev); 432 xw[i]->call (xev);
433 }
434#ifdef USE_XIM
242 } 435 }
436#endif
243 } 437 }
244 while (XPending (display)); 438 while (XEventsQueued (display, QueuedAlready));
245 439
246 flush (); 440 XFlush (display);
247} 441}
248 442
249void rxvt_display::flush () 443void rxvt_display::flush ()
250{ 444{
251 for (;;) 445 if (XEventsQueued (display, QueuedAlready))
252 { 446 x_cb (x_ev, EVENT_READ);
253 if (!XPending (display))
254 break;
255 447
256 x_cb (x_ev, 0); 448 XFlush (display);
257 }
258} 449}
259 450
260void rxvt_display::reg (xevent_watcher *w) 451void rxvt_display::reg (xevent_watcher *w)
261{ 452{
262 xw.push_back (w); 453 xw.push_back (w);
309 return xim; 500 return xim;
310} 501}
311 502
312void rxvt_display::put_xim (rxvt_xim *xim) 503void rxvt_display::put_xim (rxvt_xim *xim)
313{ 504{
505#if XLIB_IS_RACEFREE
314 xims.put (xim); 506 xims.put (xim);
507#endif
315} 508}
316#endif 509#endif
317 510
318Atom rxvt_display::atom (const char *name) 511Atom rxvt_display::atom (const char *name)
319{ 512{
357} 550}
358 551
359bool 552bool
360rxvt_color::set (rxvt_display *display, const char *name) 553rxvt_color::set (rxvt_display *display, const char *name)
361{ 554{
555#if XFT
556 return XftColorAllocName (display->display, display->visual, display->cmap,
557 name, &c);
558#else
362 XColor xc; 559 XColor xc;
363 560
364 if (XParseColor (display->display, display->cmap, name, &xc)) 561 if (XParseColor (display->display, display->cmap, name, &xc))
365 return set (display, xc.red, xc.green, xc.blue); 562 return set (display, xc.red, xc.green, xc.blue);
366 563
367 return false; 564 return false;
565#endif
368} 566}
369 567
370bool 568bool
371rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) 569rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
372{ 570{
413} 611}
414 612
415rxvt_color 613rxvt_color
416rxvt_color::fade (rxvt_display *display, int percent) 614rxvt_color::fade (rxvt_display *display, int percent)
417{ 615{
616 percent = 100 - percent;
617
418 unsigned short cr, cg, cb; 618 unsigned short cr, cg, cb;
419 rxvt_color faded; 619 rxvt_color faded;
420 620
421 get (display, cr, cg, cb); 621 get (display, cr, cg, cb);
622
422 faded.set (display, 623 faded.set (
624 display,
423 cr * percent / 100, 625 cr * percent / 100,
424 cg * percent / 100, 626 cg * percent / 100,
425 cb * percent / 100); 627 cb * percent / 100
628 );
426 629
427 return faded; 630 return faded;
428} 631}
429 632
633#define LERP(a,b,p) (a * p + b * (100 - p)) / 100
634
635rxvt_color
636rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto)
637{
638 percent = 100 - percent;
639
640 unsigned short cr, cg, cb;
641 unsigned short fcr, fcg, fcb;
642 rxvt_color faded;
643
644 get (display, cr, cg, cb);
645 fadeto.get(display, fcr, fcg, fcb);
646
647 faded.set (
648 display,
649 LERP (cr, fcr, percent),
650 LERP (cg, fcg, percent),
651 LERP (cb, fcb, percent)
652 );
653
654 return faded;
655}
656

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines