ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.20
Committed: Fri Jan 6 02:11:15 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.19: +0 -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 void refcache<T>::clear ()
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 #ifdef LOCAL_X_IS_UNIX
149 if (id[0] == ':')
150 {
151 val = rxvt_malloc (5 + strlen (id) + 1);
152 strcpy (val, "unix/");
153 strcat (val, id);
154 display = XOpenDisplay (val);
155 free (val);
156 }
157 else
158 #endif
159 display = 0;
160
161 if (!display)
162 display = XOpenDisplay (id);
163
164 if (!display)
165 return false;
166
167 screen = DefaultScreen (display);
168 root = DefaultRootWindow (display);
169 visual = DefaultVisual (display, screen);
170 cmap = DefaultColormap (display, screen);
171 depth = DefaultDepth (display, screen);
172
173 int fd = XConnectionNumber (display);
174
175 #ifndef NO_SLOW_LINK_SUPPORT
176 // try to detect wether we have a local connection.
177 // assume unix domains socket == local, everything else not
178 // TODO: might want to check for inet/127.0.0.1
179 is_local = 0;
180 sockaddr_un sa;
181 socklen_t sl = sizeof (sa);
182
183 if (!getsockname (fd, (sockaddr *)&sa, &sl))
184 is_local = sa.sun_family == AF_LOCAL;
185 #endif
186
187 #ifdef POINTER_BLANK
188 XColor blackcolour;
189 blackcolour.red = 0;
190 blackcolour.green = 0;
191 blackcolour.blue = 0;
192 Font f = XLoadFont (display, "fixed");
193 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
194 &blackcolour, &blackcolour);
195 XUnloadFont (display, f);
196 #endif
197
198 #ifdef PREFER_24BIT
199 /*
200 * If depth is not 24, look for a 24bit visual.
201 */
202 if (depth != 24)
203 {
204 XVisualInfo vinfo;
205
206 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
207 {
208 depth = 24;
209 visual = vinfo.visual;
210 cmap = XCreateColormap (display,
211 RootWindow (display, screen),
212 visual, AllocNone);
213 }
214 }
215 #endif
216
217 x_ev.start (fd, EVENT_READ);
218 fcntl (fd, F_SETFD, FD_CLOEXEC);
219
220 XSelectInput (display, root, PropertyChangeMask);
221 #ifdef USE_XIM
222 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
223 #endif
224
225 flush ();
226
227 return true;
228 }
229
230 rxvt_display::~rxvt_display ()
231 {
232 x_ev.stop ();
233 #ifdef USE_XIM
234 xims.clear ();
235 #endif
236
237 if (display)
238 XCloseDisplay (display);
239 }
240
241 #ifdef USE_XIM
242 void rxvt_display::im_change_cb ()
243 {
244 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
245 (*i)->call ();
246 }
247
248 void rxvt_display::im_change_check ()
249 {
250 // try to only call im_change_cb when a new input method
251 // registers, as xlib crashes due to a race otherwise.
252 Atom actual_type, *atoms;
253 int actual_format;
254 unsigned long nitems, bytes_after;
255
256 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
257 False, XA_ATOM, &actual_type, &actual_format,
258 &nitems, &bytes_after, (unsigned char **)&atoms)
259 != Success )
260 return;
261
262 if (actual_type == XA_ATOM && actual_format == 32)
263 for (int i = 0; i < nitems; i++)
264 if (XGetSelectionOwner (display, atoms[i]))
265 {
266 im_change_cb ();
267 break;
268 }
269
270 XFree (atoms);
271 }
272 #endif
273
274 void rxvt_display::x_cb (io_watcher &w, short revents)
275 {
276 do
277 {
278 XEvent xev;
279 XNextEvent (display, &xev);
280
281 #ifdef USE_XIM
282 if (!XFilterEvent (&xev, None))
283 {
284 if (xev.type == PropertyNotify
285 && xev.xany.window == root
286 && xev.xproperty.atom == xa_xim_servers)
287 im_change_check ();
288 #endif
289 for (int i = xw.size (); i--; )
290 {
291 if (!xw[i])
292 xw.erase_unordered (i);
293 else if (xw[i]->window == xev.xany.window)
294 xw[i]->call (xev);
295 }
296 #ifdef USE_XIM
297 }
298 #endif
299 }
300 while (XEventsQueued (display, QueuedAlready));
301
302 XFlush (display);
303 }
304
305 void rxvt_display::flush ()
306 {
307 if (XEventsQueued (display, QueuedAlready))
308 x_cb (x_ev, EVENT_READ);
309
310 XFlush (display);
311 }
312
313 void rxvt_display::reg (xevent_watcher *w)
314 {
315 xw.push_back (w);
316 w->active = xw.size ();
317 }
318
319 void rxvt_display::unreg (xevent_watcher *w)
320 {
321 if (w->active)
322 xw[w->active - 1] = 0;
323 }
324
325 void rxvt_display::set_selection_owner (rxvt_term *owner)
326 {
327 if (selection_owner && selection_owner != owner)
328 selection_owner->selection_clear ();
329
330 selection_owner = owner;
331 }
332
333 #ifdef USE_XIM
334 void rxvt_display::reg (im_watcher *w)
335 {
336 imw.push_back (w);
337 }
338
339 void rxvt_display::unreg (im_watcher *w)
340 {
341 imw.erase (find (imw.begin (), imw.end (), w));
342 }
343
344 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
345 {
346 char *id;
347 int l, m;
348
349 l = strlen (locale);
350 m = strlen (modifiers);
351
352 if (!(id = (char *)malloc (l + m + 2)))
353 return 0;
354
355 memcpy (id, locale, l); id[l] = '\n';
356 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
357
358 rxvt_xim *xim = xims.get (id);
359
360 free (id);
361
362 return xim;
363 }
364
365 void rxvt_display::put_xim (rxvt_xim *xim)
366 {
367 #if XLIB_IS_RACEFREE
368 xims.put (xim);
369 #endif
370 }
371 #endif
372
373 Atom rxvt_display::atom (const char *name)
374 {
375 return XInternAtom (display, name, False);
376 }
377
378 /////////////////////////////////////////////////////////////////////////////
379
380 template class refcache<rxvt_display>;
381 refcache<rxvt_display> displays;
382
383 /////////////////////////////////////////////////////////////////////////////
384
385 bool
386 rxvt_color::set (rxvt_display *display, Pixel p)
387 {
388 #if XFT
389 XColor xc;
390
391 xc.pixel = p;
392 if (!XQueryColor (display->display, display->cmap, &xc))
393 return false;
394
395 XRenderColor d;
396
397 d.red = xc.red;
398 d.green = xc.green;
399 d.blue = xc.blue;
400 d.alpha = 0xffff;
401
402 return
403 XftColorAllocValue (display->display,
404 display->visual,
405 display->cmap,
406 &d, &c);
407 #else
408 this->p = p;
409 #endif
410
411 return true;
412 }
413
414 bool
415 rxvt_color::set (rxvt_display *display, const char *name)
416 {
417 #if XFT
418 return XftColorAllocName (display->display, display->visual, display->cmap,
419 name, &c);
420 #else
421 XColor xc;
422
423 if (XParseColor (display->display, display->cmap, name, &xc))
424 return set (display, xc.red, xc.green, xc.blue);
425
426 return false;
427 #endif
428 }
429
430 bool
431 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
432 {
433 XColor xc;
434
435 xc.red = cr;
436 xc.green = cg;
437 xc.blue = cb;
438 xc.flags = DoRed | DoGreen | DoBlue;
439
440 if (XAllocColor (display->display, display->cmap, &xc))
441 return set (display, xc.pixel);
442
443 return false;
444 }
445
446 void
447 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
448 {
449 #if XFT
450 cr = c.color.red;
451 cg = c.color.green;
452 cb = c.color.blue;
453 #else
454 XColor c;
455
456 c.pixel = p;
457 XQueryColor (display->display, display->cmap, &c);
458
459 cr = c.red;
460 cg = c.green;
461 cb = c.blue;
462 #endif
463 }
464
465 void
466 rxvt_color::free (rxvt_display *display)
467 {
468 #if XFT
469 XftColorFree (display->display, display->visual, display->cmap, &c);
470 #else
471 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
472 #endif
473 }
474
475 rxvt_color
476 rxvt_color::fade (rxvt_display *display, int percent)
477 {
478 percent = 100 - percent;
479
480 unsigned short cr, cg, cb;
481 rxvt_color faded;
482
483 get (display, cr, cg, cb);
484
485 faded.set (
486 display,
487 cr * percent / 100,
488 cg * percent / 100,
489 cb * percent / 100
490 );
491
492 return faded;
493 }
494
495 #define LERP(a,b,p) (a * p + b * (100 - p)) / 100
496
497 rxvt_color
498 rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto)
499 {
500 percent = 100 - percent;
501
502 unsigned short cr, cg, cb;
503 unsigned short fcr, fcg, fcb;
504 rxvt_color faded;
505
506 get (display, cr, cg, cb);
507 fadeto.get(display, fcr, fcg, fcb);
508
509 faded.set (
510 display,
511 LERP (cr, fcr, percent),
512 LERP (cg, fcg, percent),
513 LERP (cb, fcb, percent)
514 );
515
516 return faded;
517 }
518