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