1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: rxvtcolor.C |
2 | * File: rxvttoolkit.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
6 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
7 | * |
7 | * |
… | |
… | |
32 | # include <sys/un.h> |
32 | # include <sys/un.h> |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | refcounted::refcounted (const char *id) |
35 | refcounted::refcounted (const char *id) |
36 | { |
36 | { |
37 | this->id = STRDUP (id); |
37 | this->id = strdup (id); |
38 | } |
38 | } |
39 | |
39 | |
40 | refcounted::~refcounted () |
40 | refcounted::~refcounted () |
41 | { |
41 | { |
42 | free (id); |
42 | free (id); |
… | |
… | |
82 | delete obj; |
82 | delete obj; |
83 | } |
83 | } |
84 | } |
84 | } |
85 | |
85 | |
86 | template<class T> |
86 | template<class T> |
87 | refcache<T>::~refcache () |
87 | void refcache<T>::clear () |
88 | { |
88 | { |
89 | while (this->size ()) |
89 | while (this->size ()) |
90 | put (*this->begin ()); |
90 | put (*this->begin ()); |
91 | } |
91 | } |
92 | |
92 | |
… | |
… | |
101 | #endif |
101 | #endif |
102 | { |
102 | { |
103 | rxvt_xim *xim = (rxvt_xim *)client_data; |
103 | rxvt_xim *xim = (rxvt_xim *)client_data; |
104 | rxvt_display *display = xim->display; |
104 | rxvt_display *display = xim->display; |
105 | |
105 | |
|
|
106 | xim->xim = 0; |
|
|
107 | |
106 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
108 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
107 | |
|
|
108 | display->im_change_cb (); |
109 | display->im_change_cb (); |
109 | } |
110 | } |
110 | |
111 | |
111 | bool rxvt_xim::init () |
112 | bool rxvt_xim::init () |
112 | { |
113 | { |
… | |
… | |
142 | { |
143 | { |
143 | } |
144 | } |
144 | |
145 | |
145 | bool rxvt_display::init () |
146 | bool rxvt_display::init () |
146 | { |
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) |
147 | display = XOpenDisplay (id); |
162 | display = XOpenDisplay (id); |
148 | |
163 | |
149 | if (!display) |
164 | if (!display) |
150 | return false; |
165 | return false; |
151 | |
166 | |
152 | screen = DefaultScreen (display); |
167 | screen = DefaultScreen (display); |
… | |
… | |
156 | depth = DefaultDepth (display, screen); |
171 | depth = DefaultDepth (display, screen); |
157 | |
172 | |
158 | int fd = XConnectionNumber (display); |
173 | int fd = XConnectionNumber (display); |
159 | |
174 | |
160 | #ifndef NO_SLOW_LINK_SUPPORT |
175 | #ifndef NO_SLOW_LINK_SUPPORT |
161 | // try to detetc wether we have a local connection. |
176 | // try to detect wether we have a local connection. |
162 | // assume unix domains socket == local, everything else not |
177 | // assume unix domains socket == local, everything else not |
163 | // TODO: might want to check for inet/127.0.0.1 |
178 | // TODO: might want to check for inet/127.0.0.1 |
164 | is_local = 0; |
179 | is_local = 0; |
165 | sockaddr_un sa; |
180 | sockaddr_un sa; |
166 | socklen_t sl = sizeof (sa); |
181 | socklen_t sl = sizeof (sa); |
167 | |
182 | |
168 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
183 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
169 | is_local = sa.sun_family == AF_LOCAL; |
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); |
170 | #endif |
196 | #endif |
171 | |
197 | |
172 | #ifdef PREFER_24BIT |
198 | #ifdef PREFER_24BIT |
173 | /* |
199 | /* |
174 | * If depth is not 24, look for a 24bit visual. |
200 | * If depth is not 24, look for a 24bit visual. |
… | |
… | |
201 | return true; |
227 | return true; |
202 | } |
228 | } |
203 | |
229 | |
204 | rxvt_display::~rxvt_display () |
230 | rxvt_display::~rxvt_display () |
205 | { |
231 | { |
|
|
232 | if (!display) |
|
|
233 | return; |
|
|
234 | |
|
|
235 | #ifdef POINTER_BLANK |
|
|
236 | XFreeCursor (display, blank_cursor); |
|
|
237 | #endif |
206 | x_ev.stop (); |
238 | x_ev.stop (); |
207 | |
239 | #ifdef USE_XIM |
208 | if (display) |
240 | xims.clear (); |
|
|
241 | #endif |
209 | XCloseDisplay (display); |
242 | XCloseDisplay (display); |
210 | } |
243 | } |
211 | |
244 | |
212 | #ifdef USE_XIM |
245 | #ifdef USE_XIM |
213 | void rxvt_display::im_change_cb () |
246 | void rxvt_display::im_change_cb () |
214 | { |
247 | { |
215 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
248 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
216 | (*i)->call (); |
249 | (*i)->call (); |
217 | } |
250 | } |
|
|
251 | |
|
|
252 | void rxvt_display::im_change_check () |
|
|
253 | { |
|
|
254 | // try to only call im_change_cb when a new input method |
|
|
255 | // registers, as xlib crashes due to a race otherwise. |
|
|
256 | Atom actual_type, *atoms; |
|
|
257 | int actual_format; |
|
|
258 | unsigned long nitems, bytes_after; |
|
|
259 | |
|
|
260 | if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, |
|
|
261 | False, XA_ATOM, &actual_type, &actual_format, |
|
|
262 | &nitems, &bytes_after, (unsigned char **)&atoms) |
|
|
263 | != Success ) |
|
|
264 | return; |
|
|
265 | |
|
|
266 | if (actual_type == XA_ATOM && actual_format == 32) |
|
|
267 | for (int i = 0; i < nitems; i++) |
|
|
268 | if (XGetSelectionOwner (display, atoms[i])) |
|
|
269 | { |
|
|
270 | im_change_cb (); |
|
|
271 | break; |
|
|
272 | } |
|
|
273 | |
|
|
274 | XFree (atoms); |
|
|
275 | } |
218 | #endif |
276 | #endif |
219 | |
277 | |
220 | void rxvt_display::x_cb (io_watcher &w, short revents) |
278 | void rxvt_display::x_cb (io_watcher &w, short revents) |
221 | { |
279 | { |
222 | do |
280 | do |
223 | { |
281 | { |
224 | XEvent xev; |
282 | XEvent xev; |
225 | XNextEvent (display, &xev); |
283 | XNextEvent (display, &xev); |
226 | |
284 | |
227 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
|
|
228 | |
|
|
229 | #ifdef USE_XIM |
285 | #ifdef USE_XIM |
230 | if (xev.type == PropertyNotify |
286 | if (!XFilterEvent (&xev, None)) |
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 | { |
287 | { |
|
|
288 | if (xev.type == PropertyNotify |
|
|
289 | && xev.xany.window == root |
|
|
290 | && xev.xproperty.atom == xa_xim_servers) |
|
|
291 | im_change_check (); |
|
|
292 | #endif |
|
|
293 | for (int i = xw.size (); i--; ) |
|
|
294 | { |
238 | if (!xw[i]) |
295 | if (!xw[i]) |
239 | xw.erase_unordered (i); |
296 | xw.erase_unordered (i); |
240 | else if (xw[i]->window == xev.xany.window) |
297 | else if (xw[i]->window == xev.xany.window) |
241 | xw[i]->call (xev); |
298 | xw[i]->call (xev); |
|
|
299 | } |
|
|
300 | #ifdef USE_XIM |
242 | } |
301 | } |
|
|
302 | #endif |
243 | } |
303 | } |
244 | while (XPending (display)); |
304 | while (XEventsQueued (display, QueuedAlready)); |
245 | |
305 | |
246 | flush (); |
306 | XFlush (display); |
247 | } |
307 | } |
248 | |
308 | |
249 | void rxvt_display::flush () |
309 | void rxvt_display::flush () |
250 | { |
310 | { |
251 | for (;;) |
311 | if (XEventsQueued (display, QueuedAlready)) |
252 | { |
312 | x_cb (x_ev, EVENT_READ); |
253 | if (!XPending (display)) |
|
|
254 | break; |
|
|
255 | |
313 | |
256 | x_cb (x_ev, 0); |
314 | XFlush (display); |
257 | } |
|
|
258 | } |
315 | } |
259 | |
316 | |
260 | void rxvt_display::reg (xevent_watcher *w) |
317 | void rxvt_display::reg (xevent_watcher *w) |
261 | { |
318 | { |
262 | xw.push_back (w); |
319 | xw.push_back (w); |
… | |
… | |
309 | return xim; |
366 | return xim; |
310 | } |
367 | } |
311 | |
368 | |
312 | void rxvt_display::put_xim (rxvt_xim *xim) |
369 | void rxvt_display::put_xim (rxvt_xim *xim) |
313 | { |
370 | { |
|
|
371 | #if XLIB_IS_RACEFREE |
314 | xims.put (xim); |
372 | xims.put (xim); |
|
|
373 | #endif |
315 | } |
374 | } |
316 | #endif |
375 | #endif |
317 | |
376 | |
318 | Atom rxvt_display::atom (const char *name) |
377 | Atom rxvt_display::atom (const char *name) |
319 | { |
378 | { |
… | |
… | |
357 | } |
416 | } |
358 | |
417 | |
359 | bool |
418 | bool |
360 | rxvt_color::set (rxvt_display *display, const char *name) |
419 | rxvt_color::set (rxvt_display *display, const char *name) |
361 | { |
420 | { |
|
|
421 | #if XFT |
|
|
422 | return XftColorAllocName (display->display, display->visual, display->cmap, |
|
|
423 | name, &c); |
|
|
424 | #else |
362 | XColor xc; |
425 | XColor xc; |
363 | |
426 | |
364 | if (XParseColor (display->display, display->cmap, name, &xc)) |
427 | if (XParseColor (display->display, display->cmap, name, &xc)) |
365 | return set (display, xc.red, xc.green, xc.blue); |
428 | return set (display, xc.red, xc.green, xc.blue); |
366 | |
429 | |
367 | return false; |
430 | return false; |
|
|
431 | #endif |
368 | } |
432 | } |
369 | |
433 | |
370 | bool |
434 | bool |
371 | rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
435 | rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
372 | { |
436 | { |
… | |
… | |
413 | } |
477 | } |
414 | |
478 | |
415 | rxvt_color |
479 | rxvt_color |
416 | rxvt_color::fade (rxvt_display *display, int percent) |
480 | rxvt_color::fade (rxvt_display *display, int percent) |
417 | { |
481 | { |
|
|
482 | percent = 100 - percent; |
|
|
483 | |
418 | unsigned short cr, cg, cb; |
484 | unsigned short cr, cg, cb; |
419 | rxvt_color faded; |
485 | rxvt_color faded; |
420 | |
486 | |
421 | get (display, cr, cg, cb); |
487 | get (display, cr, cg, cb); |
|
|
488 | |
422 | faded.set (display, |
489 | faded.set ( |
|
|
490 | display, |
423 | cr * percent / 100, |
491 | cr * percent / 100, |
424 | cg * percent / 100, |
492 | cg * percent / 100, |
425 | cb * percent / 100); |
493 | cb * percent / 100 |
|
|
494 | ); |
426 | |
495 | |
427 | return faded; |
496 | return faded; |
428 | } |
497 | } |
429 | |
498 | |
|
|
499 | #define LERP(a,b,p) (a * p + b * (100 - p)) / 100 |
|
|
500 | |
|
|
501 | rxvt_color |
|
|
502 | rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto) |
|
|
503 | { |
|
|
504 | percent = 100 - percent; |
|
|
505 | |
|
|
506 | unsigned short cr, cg, cb; |
|
|
507 | unsigned short fcr, fcg, fcb; |
|
|
508 | rxvt_color faded; |
|
|
509 | |
|
|
510 | get (display, cr, cg, cb); |
|
|
511 | fadeto.get(display, fcr, fcg, fcb); |
|
|
512 | |
|
|
513 | faded.set ( |
|
|
514 | display, |
|
|
515 | LERP (cr, fcr, percent), |
|
|
516 | LERP (cg, fcg, percent), |
|
|
517 | LERP (cb, fcb, percent) |
|
|
518 | ); |
|
|
519 | |
|
|
520 | return faded; |
|
|
521 | } |
|
|
522 | |