ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.1
Committed: Sun Aug 15 00:37:04 2004 UTC (19 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# Content
1 /*--------------------------------*-C-*---------------------------------*
2 * File: rxvtcolor.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 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
107
108 display->im_change_cb ();
109 }
110
111 bool rxvt_xim::init ()
112 {
113 display = GET_R->display; //HACK: TODO
114
115 xim = XOpenIM (display->display, NULL, NULL, NULL);
116
117 if (!xim)
118 return false;
119
120 XIMCallback ximcallback;
121 ximcallback.client_data = (XPointer)this;
122 ximcallback.callback = im_destroy_cb;
123
124 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
125
126 return true;
127 }
128
129 rxvt_xim::~rxvt_xim ()
130 {
131 if (xim)
132 XCloseIM (xim);
133 }
134 #endif
135
136 /////////////////////////////////////////////////////////////////////////////
137
138 rxvt_display::rxvt_display (const char *id)
139 : refcounted (id)
140 , x_ev (this, &rxvt_display::x_cb)
141 , selection_owner (0)
142 {
143 }
144
145 bool rxvt_display::init ()
146 {
147 display = XOpenDisplay (id);
148
149 if (!display)
150 return false;
151
152 screen = DefaultScreen (display);
153 root = DefaultRootWindow (display);
154 visual = DefaultVisual (display, screen);
155 cmap = DefaultColormap (display, screen);
156 depth = DefaultDepth (display, screen);
157
158 int fd = XConnectionNumber (display);
159
160 #ifndef NO_SLOW_LINK_SUPPORT
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
168 if (!getsockname (fd, (sockaddr *)&sa, &sl))
169 is_local = sa.sun_family == AF_LOCAL;
170 #endif
171
172 #ifdef PREFER_24BIT
173 /*
174 * If depth is not 24, look for a 24bit visual.
175 */
176 if (depth != 24)
177 {
178 XVisualInfo vinfo;
179
180 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
181 {
182 depth = 24;
183 visual = vinfo.visual;
184 cmap = XCreateColormap (display,
185 RootWindow (display, screen),
186 visual, AllocNone);
187 }
188 }
189 #endif
190
191 x_ev.start (fd, EVENT_READ);
192 fcntl (fd, F_SETFD, FD_CLOEXEC);
193
194 XSelectInput (display, root, PropertyChangeMask);
195 #ifdef USE_XIM
196 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
197 #endif
198
199 flush ();
200
201 return true;
202 }
203
204 rxvt_display::~rxvt_display ()
205 {
206 x_ev.stop ();
207
208 if (display)
209 XCloseDisplay (display);
210 }
211
212 #ifdef USE_XIM
213 void rxvt_display::im_change_cb ()
214 {
215 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
216 (*i)->call ();
217 }
218 #endif
219
220 void rxvt_display::x_cb (io_watcher &w, short revents)
221 {
222 do
223 {
224 XEvent xev;
225 XNextEvent (display, &xev);
226
227 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
228
229 #ifdef USE_XIM
230 if (xev.type == PropertyNotify
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 {
238 if (!xw[i])
239 xw.erase_unordered (i);
240 else if (xw[i]->window == xev.xany.window)
241 xw[i]->call (xev);
242 }
243 }
244 while (XPending (display));
245
246 flush ();
247 }
248
249 void rxvt_display::flush ()
250 {
251 for (;;)
252 {
253 if (!XPending (display))
254 break;
255
256 x_cb (x_ev, 0);
257 }
258 }
259
260 void rxvt_display::reg (xevent_watcher *w)
261 {
262 xw.push_back (w);
263 w->active = xw.size ();
264 }
265
266 void rxvt_display::unreg (xevent_watcher *w)
267 {
268 if (w->active)
269 xw[w->active - 1] = 0;
270 }
271
272 void rxvt_display::set_selection_owner (rxvt_term *owner)
273 {
274 if (selection_owner && selection_owner != owner)
275 selection_owner->selection_clear ();
276
277 selection_owner = owner;
278 }
279
280 #ifdef USE_XIM
281 void rxvt_display::reg (im_watcher *w)
282 {
283 imw.push_back (w);
284 }
285
286 void rxvt_display::unreg (im_watcher *w)
287 {
288 imw.erase (find (imw.begin (), imw.end (), w));
289 }
290
291 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
292 {
293 char *id;
294 int l, m;
295
296 l = strlen (locale);
297 m = strlen (modifiers);
298
299 if (!(id = (char *)malloc (l + m + 2)))
300 return 0;
301
302 memcpy (id, locale, l); id[l] = '\n';
303 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
304
305 rxvt_xim *xim = xims.get (id);
306
307 free (id);
308
309 return xim;
310 }
311
312 void rxvt_display::put_xim (rxvt_xim *xim)
313 {
314 xims.put (xim);
315 }
316 #endif
317
318 Atom rxvt_display::atom (const char *name)
319 {
320 return XInternAtom (display, name, False);
321 }
322
323 /////////////////////////////////////////////////////////////////////////////
324
325 template class refcache<rxvt_display>;
326 refcache<rxvt_display> displays;
327
328 /////////////////////////////////////////////////////////////////////////////
329
330 bool
331 rxvt_color::set (rxvt_display *display, Pixel p)
332 {
333 #if XFT
334 XColor xc;
335
336 xc.pixel = p;
337 if (!XQueryColor (display->display, display->cmap, &xc))
338 return false;
339
340 XRenderColor d;
341
342 d.red = xc.red;
343 d.green = xc.green;
344 d.blue = xc.blue;
345 d.alpha = 0xffff;
346
347 return
348 XftColorAllocValue (display->display,
349 display->visual,
350 display->cmap,
351 &d, &c);
352 #else
353 this->p = p;
354 #endif
355
356 return true;
357 }
358
359 bool
360 rxvt_color::set (rxvt_display *display, const char *name)
361 {
362 XColor xc;
363
364 if (XParseColor (display->display, display->cmap, name, &xc))
365 return set (display, xc.red, xc.green, xc.blue);
366
367 return false;
368 }
369
370 bool
371 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
372 {
373 XColor xc;
374
375 xc.red = cr;
376 xc.green = cg;
377 xc.blue = cb;
378 xc.flags = DoRed | DoGreen | DoBlue;
379
380 if (XAllocColor (display->display, display->cmap, &xc))
381 return set (display, xc.pixel);
382
383 return false;
384 }
385
386 void
387 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
388 {
389 #if XFT
390 cr = c.color.red;
391 cg = c.color.green;
392 cb = c.color.blue;
393 #else
394 XColor c;
395
396 c.pixel = p;
397 XQueryColor (display->display, display->cmap, &c);
398
399 cr = c.red;
400 cg = c.green;
401 cb = c.blue;
402 #endif
403 }
404
405 void
406 rxvt_color::free (rxvt_display *display)
407 {
408 #if XFT
409 XftColorFree (display->display, display->visual, display->cmap, &c);
410 #else
411 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
412 #endif
413 }
414
415 rxvt_color
416 rxvt_color::fade (rxvt_display *display, int percent)
417 {
418 unsigned short cr, cg, cb;
419 rxvt_color faded;
420
421 get (display, cr, cg, cb);
422 faded.set (display,
423 cr * percent / 100,
424 cg * percent / 100,
425 cb * percent / 100);
426
427 return faded;
428 }
429