*** empty log message ***
[dana/urxvt.git] / src / rxvttoolkit.C
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 #ifdef POINTER_BLANK
233   XFreeCursor (display, blank_cursor);
234 #endif
235   x_ev.stop ();
236 #ifdef USE_XIM
237   xims.clear ();
238 #endif
239
240   if (display)
241     XCloseDisplay (display);
242 }
243
244 #ifdef USE_XIM
245 void rxvt_display::im_change_cb ()
246 {
247   for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
248     (*i)->call ();
249 }
250
251 void rxvt_display::im_change_check ()
252 {
253   // try to only call im_change_cb when a new input method
254   // registers, as xlib crashes due to a race otherwise.
255   Atom actual_type, *atoms;
256   int actual_format;
257   unsigned long nitems, bytes_after;
258
259   if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
260                           False, XA_ATOM, &actual_type, &actual_format,
261                           &nitems, &bytes_after, (unsigned char **)&atoms)
262       != Success )
263     return;
264
265   if (actual_type == XA_ATOM  && actual_format == 32)
266     for (int i = 0; i < nitems; i++)
267       if (XGetSelectionOwner (display, atoms[i]))
268         {
269           im_change_cb ();
270           break;
271         }
272
273   XFree (atoms);
274 }
275 #endif
276
277 void rxvt_display::x_cb (io_watcher &w, short revents)
278 {
279   do
280     {
281       XEvent xev;
282       XNextEvent (display, &xev);
283
284 #ifdef USE_XIM
285       if (!XFilterEvent (&xev, None))
286         {
287           if (xev.type == PropertyNotify
288               && xev.xany.window == root
289               && xev.xproperty.atom == xa_xim_servers)
290             im_change_check ();
291 #endif
292           for (int i = xw.size (); i--; )
293             {
294               if (!xw[i])
295                 xw.erase_unordered (i);
296               else if (xw[i]->window == xev.xany.window)
297                 xw[i]->call (xev);
298             }
299 #ifdef USE_XIM
300         }
301 #endif
302     }
303   while (XEventsQueued (display, QueuedAlready));
304
305   XFlush (display);
306 }
307
308 void rxvt_display::flush ()
309 {
310   if (XEventsQueued (display, QueuedAlready))
311     x_cb (x_ev, EVENT_READ);
312
313   XFlush (display);
314 }
315
316 void rxvt_display::reg (xevent_watcher *w)
317 {
318   xw.push_back (w);
319   w->active = xw.size ();
320 }
321
322 void rxvt_display::unreg (xevent_watcher *w)
323 {
324   if (w->active)
325     xw[w->active - 1] = 0;
326 }
327
328 void rxvt_display::set_selection_owner (rxvt_term *owner)
329 {
330   if (selection_owner && selection_owner != owner)
331     selection_owner->selection_clear ();
332
333   selection_owner = owner;
334 }
335
336 #ifdef USE_XIM
337 void rxvt_display::reg (im_watcher *w)
338 {
339   imw.push_back (w);
340 }
341
342 void rxvt_display::unreg (im_watcher *w)
343 {
344   imw.erase (find (imw.begin (), imw.end (), w));
345 }
346
347 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
348 {
349   char *id;
350   int l, m;
351
352   l = strlen (locale);
353   m = strlen (modifiers);
354
355   if (!(id = (char *)malloc (l + m + 2)))
356     return 0;
357
358   memcpy (id, locale, l); id[l] = '\n';
359   memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
360
361   rxvt_xim *xim = xims.get (id);
362
363   free (id);
364
365   return xim;
366 }
367
368 void rxvt_display::put_xim (rxvt_xim *xim)
369 {
370 #if XLIB_IS_RACEFREE
371   xims.put (xim);
372 #endif
373 }
374 #endif
375
376 Atom rxvt_display::atom (const char *name)
377 {
378   return XInternAtom (display, name, False);
379 }
380
381 /////////////////////////////////////////////////////////////////////////////
382
383 template class refcache<rxvt_display>;
384 refcache<rxvt_display> displays;
385
386 /////////////////////////////////////////////////////////////////////////////
387   
388 bool
389 rxvt_color::set (rxvt_display *display, Pixel p)
390 {
391 #if XFT
392   XColor xc;
393
394   xc.pixel = p;
395   if (!XQueryColor (display->display, display->cmap, &xc))
396     return false;
397
398   XRenderColor d;
399
400   d.red   = xc.red;
401   d.green = xc.green;
402   d.blue  = xc.blue;
403   d.alpha = 0xffff;
404
405   return
406     XftColorAllocValue (display->display, 
407                         display->visual,
408                         display->cmap,
409                         &d, &c);
410 #else
411   this->p = p;
412 #endif
413
414   return true;
415 }
416
417 bool
418 rxvt_color::set (rxvt_display *display, const char *name)
419 {
420 #if XFT
421   return XftColorAllocName (display->display, display->visual, display->cmap,
422                             name, &c);
423 #else
424   XColor xc;
425
426   if (XParseColor (display->display, display->cmap, name, &xc))
427     return set (display, xc.red, xc.green, xc.blue);
428
429   return false;
430 #endif
431 }
432
433 bool
434 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
435 {
436   XColor xc;
437
438   xc.red   = cr;
439   xc.green = cg;
440   xc.blue  = cb;
441   xc.flags = DoRed | DoGreen | DoBlue;
442
443   if (XAllocColor (display->display, display->cmap, &xc))
444     return set (display, xc.pixel);
445
446   return false;
447 }
448
449 void 
450 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
451 {
452 #if XFT
453   cr = c.color.red;
454   cg = c.color.green;
455   cb = c.color.blue;
456 #else
457   XColor c;
458
459   c.pixel = p;
460   XQueryColor (display->display, display->cmap, &c);
461
462   cr = c.red;
463   cg = c.green;
464   cb = c.blue;
465 #endif
466 }
467
468 void 
469 rxvt_color::free (rxvt_display *display)
470 {
471 #if XFT
472   XftColorFree (display->display, display->visual, display->cmap, &c);
473 #else
474   XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
475 #endif
476 }
477
478 rxvt_color
479 rxvt_color::fade (rxvt_display *display, int percent)
480 {
481   percent = 100 - percent;
482
483   unsigned short cr, cg, cb;
484   rxvt_color faded;
485
486   get (display, cr, cg, cb);
487
488   faded.set (
489     display,
490     cr * percent / 100,
491     cg * percent / 100,
492     cb * percent / 100
493   );
494
495   return faded;
496 }
497
498 #define LERP(a,b,p) (a * p + b * (100 - p)) / 100
499
500 rxvt_color
501 rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto)
502 {
503   percent = 100 - percent;
504
505   unsigned short cr, cg, cb;
506   unsigned short fcr, fcg, fcb;
507   rxvt_color faded;
508   
509   get (display, cr, cg, cb);
510   fadeto.get(display, fcr, fcg, fcb);
511
512   faded.set (
513     display,
514     LERP (cr, fcr, percent),
515     LERP (cg, fcg, percent),
516     LERP (cb, fcb, percent)
517   );
518
519   return faded;
520 }
521