ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.10
Committed: Wed Dec 29 19:59:46 2004 UTC (19 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_7
Changes since 1.8: +6 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*--------------------------------*-C-*---------------------------------*
2 * File: rxvttoolkit.C
3 *----------------------------------------------------------------------*
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 #ifndef NO_SLOW_LINK_SUPPORT
31 # include <sys/socket.h>
32 # include <sys/un.h>
33 #endif
34
35 refcounted::refcounted (const char *id)
36 {
37 this->id = strdup (id);
38 }
39
40 refcounted::~refcounted ()
41 {
42 free (id);
43 }
44
45 template<class T>
46 T *refcache<T>::get (const char *id)
47 {
48 for (T **i = this->begin (); i < this->end (); ++i)
49 {
50 if (!strcmp (id, (*i)->id))
51 {
52 (*i)->referenced++;
53 return *i;
54 }
55 }
56
57 T *obj = new T (id);
58
59 obj->referenced = 1;
60
61 if (obj && obj->init ())
62 {
63 this->push_back (obj);
64 return obj;
65 }
66 else
67 {
68 delete obj;
69 return 0;
70 }
71 }
72
73 template<class T>
74 void refcache<T>::put (T *obj)
75 {
76 if (!obj)
77 return;
78
79 if (!--obj->referenced)
80 {
81 this->erase (find (this->begin (), this->end (), obj));
82 delete obj;
83 }
84 }
85
86 template<class T>
87 refcache<T>::~refcache ()
88 {
89 while (this->size ())
90 put (*this->begin ());
91 }
92
93 /////////////////////////////////////////////////////////////////////////////
94
95 #ifdef USE_XIM
96 static void
97 #if XIMCB_PROTO_BROKEN
98 im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
99 #else
100 im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
101 #endif
102 {
103 rxvt_xim *xim = (rxvt_xim *)client_data;
104 rxvt_display *display = xim->display;
105
106 xim->xim = 0;
107
108 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
109 display->im_change_cb ();
110 }
111
112 bool rxvt_xim::init ()
113 {
114 display = GET_R->display; //HACK: TODO
115
116 xim = XOpenIM (display->display, NULL, NULL, NULL);
117
118 if (!xim)
119 return false;
120
121 XIMCallback ximcallback;
122 ximcallback.client_data = (XPointer)this;
123 ximcallback.callback = im_destroy_cb;
124
125 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
126
127 return true;
128 }
129
130 rxvt_xim::~rxvt_xim ()
131 {
132 if (xim)
133 XCloseIM (xim);
134 }
135 #endif
136
137 /////////////////////////////////////////////////////////////////////////////
138
139 rxvt_display::rxvt_display (const char *id)
140 : refcounted (id)
141 , x_ev (this, &rxvt_display::x_cb)
142 , selection_owner (0)
143 {
144 }
145
146 bool rxvt_display::init ()
147 {
148 display = XOpenDisplay (id);
149
150 if (!display)
151 return false;
152
153 screen = DefaultScreen (display);
154 root = DefaultRootWindow (display);
155 visual = DefaultVisual (display, screen);
156 cmap = DefaultColormap (display, screen);
157 depth = DefaultDepth (display, screen);
158
159 int fd = XConnectionNumber (display);
160
161 #ifndef NO_SLOW_LINK_SUPPORT
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
173 #ifdef PREFER_24BIT
174 /*
175 * If depth is not 24, look for a 24bit visual.
176 */
177 if (depth != 24)
178 {
179 XVisualInfo vinfo;
180
181 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
182 {
183 depth = 24;
184 visual = vinfo.visual;
185 cmap = XCreateColormap (display,
186 RootWindow (display, screen),
187 visual, AllocNone);
188 }
189 }
190 #endif
191
192 x_ev.start (fd, EVENT_READ);
193 fcntl (fd, F_SETFD, FD_CLOEXEC);
194
195 XSelectInput (display, root, PropertyChangeMask);
196 #ifdef USE_XIM
197 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
198 #endif
199
200 flush ();
201
202 return true;
203 }
204
205 rxvt_display::~rxvt_display ()
206 {
207 x_ev.stop ();
208
209 if (display)
210 XCloseDisplay (display);
211 }
212
213 #ifdef USE_XIM
214 void rxvt_display::im_change_cb ()
215 {
216 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
217 (*i)->call ();
218 }
219
220 void rxvt_display::im_change_check ()
221 {
222 // make sure we only call im_change_cb when a new input method
223 // registers, as xlib crashes due to a race otherwise.
224 Atom actual_type, *atoms;
225 int actual_format;
226 unsigned long nitems, bytes_after;
227
228 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
229 False, XA_ATOM, &actual_type, &actual_format,
230 &nitems, &bytes_after, (unsigned char **)&atoms)
231 != Success )
232 return;
233
234 if (actual_type == XA_ATOM && actual_format == 32)
235 for (int i = 0; i < nitems; i++)
236 if (XGetSelectionOwner (display, atoms[i]))
237 {
238 im_change_cb ();
239 break;
240 }
241
242 XFree (atoms);
243 }
244 #endif
245
246 void rxvt_display::x_cb (io_watcher &w, short revents)
247 {
248 do
249 {
250 XEvent xev;
251 XNextEvent (display, &xev);
252
253 #ifdef USE_XIM
254 if (!XFilterEvent (&xev, None))
255 {
256
257 if (xev.type == PropertyNotify
258 && xev.xany.window == root
259 && xev.xproperty.atom == xa_xim_servers)
260 im_change_check ();
261 #endif
262 for (int i = xw.size (); i--; )
263 {
264 if (!xw[i])
265 xw.erase_unordered (i);
266 else if (xw[i]->window == xev.xany.window)
267 xw[i]->call (xev);
268 }
269 #ifdef USE_XIM
270 }
271 #endif
272 }
273 while (XEventsQueued (display, QueuedAlready));
274
275 XFlush (display);
276 }
277
278 void rxvt_display::flush ()
279 {
280 if (XEventsQueued (display, QueuedAlready))
281 x_cb (x_ev, EVENT_READ);
282
283 XFlush (display);
284 }
285
286 void rxvt_display::reg (xevent_watcher *w)
287 {
288 xw.push_back (w);
289 w->active = xw.size ();
290 }
291
292 void rxvt_display::unreg (xevent_watcher *w)
293 {
294 if (w->active)
295 xw[w->active - 1] = 0;
296 }
297
298 void rxvt_display::set_selection_owner (rxvt_term *owner)
299 {
300 if (selection_owner && selection_owner != owner)
301 selection_owner->selection_clear ();
302
303 selection_owner = owner;
304 }
305
306 #ifdef USE_XIM
307 void rxvt_display::reg (im_watcher *w)
308 {
309 imw.push_back (w);
310 }
311
312 void rxvt_display::unreg (im_watcher *w)
313 {
314 imw.erase (find (imw.begin (), imw.end (), w));
315 }
316
317 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
318 {
319 char *id;
320 int l, m;
321
322 l = strlen (locale);
323 m = strlen (modifiers);
324
325 if (!(id = (char *)malloc (l + m + 2)))
326 return 0;
327
328 memcpy (id, locale, l); id[l] = '\n';
329 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
330
331 rxvt_xim *xim = xims.get (id);
332
333 free (id);
334
335 return xim;
336 }
337
338 void rxvt_display::put_xim (rxvt_xim *xim)
339 {
340 #if XLIB_IS_RACEFREE
341 xims.put (xim);
342 #endif
343 }
344 #endif
345
346 Atom rxvt_display::atom (const char *name)
347 {
348 return XInternAtom (display, name, False);
349 }
350
351 /////////////////////////////////////////////////////////////////////////////
352
353 template class refcache<rxvt_display>;
354 refcache<rxvt_display> displays;
355
356 /////////////////////////////////////////////////////////////////////////////
357
358 bool
359 rxvt_color::set (rxvt_display *display, Pixel p)
360 {
361 #if XFT
362 XColor xc;
363
364 xc.pixel = p;
365 if (!XQueryColor (display->display, display->cmap, &xc))
366 return false;
367
368 XRenderColor d;
369
370 d.red = xc.red;
371 d.green = xc.green;
372 d.blue = xc.blue;
373 d.alpha = 0xffff;
374
375 return
376 XftColorAllocValue (display->display,
377 display->visual,
378 display->cmap,
379 &d, &c);
380 #else
381 this->p = p;
382 #endif
383
384 return true;
385 }
386
387 bool
388 rxvt_color::set (rxvt_display *display, const char *name)
389 {
390 #if XFT
391 return XftColorAllocName (display->display, display->visual, display->cmap,
392 name, &c);
393 #else
394 XColor xc;
395
396 if (XParseColor (display->display, display->cmap, name, &xc))
397 return set (display, xc.red, xc.green, xc.blue);
398
399 return false;
400 #endif
401 }
402
403 bool
404 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
405 {
406 XColor xc;
407
408 xc.red = cr;
409 xc.green = cg;
410 xc.blue = cb;
411 xc.flags = DoRed | DoGreen | DoBlue;
412
413 if (XAllocColor (display->display, display->cmap, &xc))
414 return set (display, xc.pixel);
415
416 return false;
417 }
418
419 void
420 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
421 {
422 #if XFT
423 cr = c.color.red;
424 cg = c.color.green;
425 cb = c.color.blue;
426 #else
427 XColor c;
428
429 c.pixel = p;
430 XQueryColor (display->display, display->cmap, &c);
431
432 cr = c.red;
433 cg = c.green;
434 cb = c.blue;
435 #endif
436 }
437
438 void
439 rxvt_color::free (rxvt_display *display)
440 {
441 #if XFT
442 XftColorFree (display->display, display->visual, display->cmap, &c);
443 #else
444 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
445 #endif
446 }
447
448 rxvt_color
449 rxvt_color::fade (rxvt_display *display, int percent)
450 {
451 unsigned short cr, cg, cb;
452 rxvt_color faded;
453
454 get (display, cr, cg, cb);
455 faded.set (display,
456 cr * percent / 100,
457 cg * percent / 100,
458 cb * percent / 100);
459
460 return faded;
461 }
462