ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.6
Committed: Sat Dec 11 17:18:29 2004 UTC (19 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_4
Changes since 1.5: +1 -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 flush ();
276 }
277
278 void rxvt_display::flush ()
279 {
280 for (;;)
281 {
282 if (!XPending (display))
283 break;
284
285 x_cb (x_ev, 0);
286 }
287 }
288
289 void rxvt_display::reg (xevent_watcher *w)
290 {
291 xw.push_back (w);
292 w->active = xw.size ();
293 }
294
295 void rxvt_display::unreg (xevent_watcher *w)
296 {
297 if (w->active)
298 xw[w->active - 1] = 0;
299 }
300
301 void rxvt_display::set_selection_owner (rxvt_term *owner)
302 {
303 if (selection_owner && selection_owner != owner)
304 selection_owner->selection_clear ();
305
306 selection_owner = owner;
307 }
308
309 #ifdef USE_XIM
310 void rxvt_display::reg (im_watcher *w)
311 {
312 imw.push_back (w);
313 }
314
315 void rxvt_display::unreg (im_watcher *w)
316 {
317 imw.erase (find (imw.begin (), imw.end (), w));
318 }
319
320 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
321 {
322 char *id;
323 int l, m;
324
325 l = strlen (locale);
326 m = strlen (modifiers);
327
328 if (!(id = (char *)malloc (l + m + 2)))
329 return 0;
330
331 memcpy (id, locale, l); id[l] = '\n';
332 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
333
334 rxvt_xim *xim = xims.get (id);
335
336 free (id);
337
338 return xim;
339 }
340
341 void rxvt_display::put_xim (rxvt_xim *xim)
342 {
343 xims.put (xim);
344 }
345 #endif
346
347 Atom rxvt_display::atom (const char *name)
348 {
349 return XInternAtom (display, name, False);
350 }
351
352 /////////////////////////////////////////////////////////////////////////////
353
354 template class refcache<rxvt_display>;
355 refcache<rxvt_display> displays;
356
357 /////////////////////////////////////////////////////////////////////////////
358
359 bool
360 rxvt_color::set (rxvt_display *display, Pixel p)
361 {
362 #if XFT
363 XColor xc;
364
365 xc.pixel = p;
366 if (!XQueryColor (display->display, display->cmap, &xc))
367 return false;
368
369 XRenderColor d;
370
371 d.red = xc.red;
372 d.green = xc.green;
373 d.blue = xc.blue;
374 d.alpha = 0xffff;
375
376 return
377 XftColorAllocValue (display->display,
378 display->visual,
379 display->cmap,
380 &d, &c);
381 #else
382 this->p = p;
383 #endif
384
385 return true;
386 }
387
388 bool
389 rxvt_color::set (rxvt_display *display, const char *name)
390 {
391 XColor xc;
392
393 if (XParseColor (display->display, display->cmap, name, &xc))
394 return set (display, xc.red, xc.green, xc.blue);
395
396 return false;
397 }
398
399 bool
400 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
401 {
402 XColor xc;
403
404 xc.red = cr;
405 xc.green = cg;
406 xc.blue = cb;
407 xc.flags = DoRed | DoGreen | DoBlue;
408
409 if (XAllocColor (display->display, display->cmap, &xc))
410 return set (display, xc.pixel);
411
412 return false;
413 }
414
415 void
416 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
417 {
418 #if XFT
419 cr = c.color.red;
420 cg = c.color.green;
421 cb = c.color.blue;
422 #else
423 XColor c;
424
425 c.pixel = p;
426 XQueryColor (display->display, display->cmap, &c);
427
428 cr = c.red;
429 cg = c.green;
430 cb = c.blue;
431 #endif
432 }
433
434 void
435 rxvt_color::free (rxvt_display *display)
436 {
437 #if XFT
438 XftColorFree (display->display, display->visual, display->cmap, &c);
439 #else
440 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
441 #endif
442 }
443
444 rxvt_color
445 rxvt_color::fade (rxvt_display *display, int percent)
446 {
447 unsigned short cr, cg, cb;
448 rxvt_color faded;
449
450 get (display, cr, cg, cb);
451 faded.set (display,
452 cr * percent / 100,
453 cg * percent / 100,
454 cb * percent / 100);
455
456 return faded;
457 }
458