|
|
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> |
3 | #include <rxvtcolor.h> |
25 | #include <rxvtcolor.h> |
4 | |
26 | |
5 | #include <unistd.h> |
27 | #include <unistd.h> |
… | |
… | |
33 | } |
55 | } |
34 | |
56 | |
35 | template<class T> |
57 | template<class T> |
36 | T *refcache<T>::get (const char *id) |
58 | T *refcache<T>::get (const char *id) |
37 | { |
59 | { |
38 | for (T **i = begin (); i < end (); ++i) |
60 | for (T **i = this->begin (); i < this->end (); ++i) |
39 | { |
61 | { |
40 | if (!strcmp (id, (*i)->id)) |
62 | if (!strcmp (id, (*i)->id)) |
41 | { |
63 | { |
42 | (*i)->referenced++; |
64 | (*i)->referenced++; |
43 | return *i; |
65 | return *i; |
… | |
… | |
48 | |
70 | |
49 | obj->referenced = 1; |
71 | obj->referenced = 1; |
50 | |
72 | |
51 | if (obj && obj->init ()) |
73 | if (obj && obj->init ()) |
52 | { |
74 | { |
53 | push_back (obj); |
75 | this->push_back (obj); |
54 | return obj; |
76 | return obj; |
55 | } |
77 | } |
56 | else |
78 | else |
57 | { |
79 | { |
58 | delete obj; |
80 | delete obj; |
… | |
… | |
66 | if (!obj) |
88 | if (!obj) |
67 | return; |
89 | return; |
68 | |
90 | |
69 | if (!--obj->referenced) |
91 | if (!--obj->referenced) |
70 | { |
92 | { |
71 | erase (find (begin (), end (), obj)); |
93 | this->erase (find (this->begin (), this->end (), obj)); |
72 | delete obj; |
94 | delete obj; |
73 | } |
95 | } |
74 | } |
96 | } |
75 | |
97 | |
76 | template<class T> |
98 | template<class T> |
77 | refcache<T>::~refcache () |
99 | refcache<T>::~refcache () |
78 | { |
100 | { |
79 | while (size ()) |
101 | while (this->size ()) |
80 | put (*begin ()); |
102 | put (*this->begin ()); |
81 | } |
103 | } |
82 | |
104 | |
83 | ///////////////////////////////////////////////////////////////////////////// |
105 | ///////////////////////////////////////////////////////////////////////////// |
84 | |
106 | |
|
|
107 | #ifdef USE_XIM |
85 | static void |
108 | static void |
|
|
109 | #if XIMCB_PROTO_BROKEN |
|
|
110 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
|
|
111 | #else |
86 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
112 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
|
|
113 | #endif |
87 | { |
114 | { |
88 | rxvt_xim *xim = (rxvt_xim *)client_data; |
115 | rxvt_xim *xim = (rxvt_xim *)client_data; |
89 | rxvt_display *display = xim->display; |
116 | rxvt_display *display = xim->display; |
90 | |
117 | |
91 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
118 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
… | |
… | |
114 | rxvt_xim::~rxvt_xim () |
141 | rxvt_xim::~rxvt_xim () |
115 | { |
142 | { |
116 | if (xim) |
143 | if (xim) |
117 | XCloseIM (xim); |
144 | XCloseIM (xim); |
118 | } |
145 | } |
|
|
146 | #endif |
119 | |
147 | |
120 | ///////////////////////////////////////////////////////////////////////////// |
148 | ///////////////////////////////////////////////////////////////////////////// |
121 | |
149 | |
122 | rxvt_display::rxvt_display (const char *id) |
150 | rxvt_display::rxvt_display (const char *id) |
123 | : refcounted (id) |
151 | : refcounted (id) |
… | |
… | |
158 | } |
186 | } |
159 | #endif |
187 | #endif |
160 | |
188 | |
161 | int fd = XConnectionNumber (display); |
189 | int fd = XConnectionNumber (display); |
162 | x_ev.start (fd, EVENT_READ); |
190 | x_ev.start (fd, EVENT_READ); |
163 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
191 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
164 | |
192 | |
165 | XSelectInput (display, root, PropertyChangeMask); |
193 | XSelectInput (display, root, PropertyChangeMask); |
|
|
194 | #ifdef USE_XIM |
166 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
195 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
196 | #endif |
167 | |
197 | |
168 | flush (); |
198 | flush (); |
169 | |
199 | |
170 | return true; |
200 | return true; |
171 | } |
201 | } |
… | |
… | |
176 | |
206 | |
177 | if (display) |
207 | if (display) |
178 | XCloseDisplay (display); |
208 | XCloseDisplay (display); |
179 | } |
209 | } |
180 | |
210 | |
|
|
211 | #ifdef USE_XIM |
181 | void rxvt_display::im_change_cb () |
212 | void rxvt_display::im_change_cb () |
182 | { |
213 | { |
183 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
214 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
184 | (*i)->call (); |
215 | (*i)->call (); |
185 | } |
216 | } |
|
|
217 | #endif |
186 | |
218 | |
187 | void rxvt_display::x_cb (io_watcher &w, short revents) |
219 | void rxvt_display::x_cb (io_watcher &w, short revents) |
188 | { |
220 | { |
189 | do |
221 | do |
190 | { |
222 | { |
191 | XEvent xev; |
223 | XEvent xev; |
192 | XNextEvent (display, &xev); |
224 | XNextEvent (display, &xev); |
193 | |
225 | |
194 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
226 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
195 | |
227 | |
|
|
228 | #ifdef USE_XIM |
196 | if (xev.type == PropertyNotify |
229 | if (xev.type == PropertyNotify |
197 | && xev.xany.window == root |
230 | && xev.xany.window == root |
198 | && xev.xproperty.atom == xa_xim_servers) |
231 | && xev.xproperty.atom == xa_xim_servers) |
199 | im_change_cb (); |
232 | im_change_cb (); |
|
|
233 | #endif |
200 | |
234 | |
201 | for (int i = xw.size (); i--; ) |
235 | for (int i = xw.size (); i--; ) |
202 | { |
236 | { |
203 | if (!xw[i]) |
237 | if (!xw[i]) |
204 | xw.erase_unordered (i); |
238 | xw.erase_unordered (i); |
… | |
… | |
213 | |
247 | |
214 | void rxvt_display::flush () |
248 | void rxvt_display::flush () |
215 | { |
249 | { |
216 | for (;;) |
250 | for (;;) |
217 | { |
251 | { |
218 | XFlush (display); |
|
|
219 | |
|
|
220 | if (!XPending (display)) |
252 | if (!XPending (display)) |
221 | break; |
253 | break; |
222 | |
254 | |
223 | x_cb (x_ev, 0); |
255 | x_cb (x_ev, 0); |
224 | } |
256 | } |
… | |
… | |
234 | { |
266 | { |
235 | if (w->active) |
267 | if (w->active) |
236 | xw[w->active - 1] = 0; |
268 | xw[w->active - 1] = 0; |
237 | } |
269 | } |
238 | |
270 | |
239 | void rxvt_display::reg (im_watcher *w) |
|
|
240 | { |
|
|
241 | imw.push_back (w); |
|
|
242 | } |
|
|
243 | |
|
|
244 | void rxvt_display::unreg (im_watcher *w) |
|
|
245 | { |
|
|
246 | imw.erase (find (imw.begin (), imw.end (), w)); |
|
|
247 | } |
|
|
248 | |
|
|
249 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
271 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
250 | { |
272 | { |
251 | if (selection_owner && selection_owner != owner) |
273 | if (selection_owner && selection_owner != owner) |
252 | selection_owner->selection_clear (); |
274 | selection_owner->selection_clear (); |
253 | |
275 | |
254 | selection_owner = owner; |
276 | selection_owner = owner; |
255 | } |
277 | } |
256 | |
278 | |
|
|
279 | #ifdef USE_XIM |
|
|
280 | void rxvt_display::reg (im_watcher *w) |
|
|
281 | { |
|
|
282 | imw.push_back (w); |
|
|
283 | } |
|
|
284 | |
|
|
285 | void rxvt_display::unreg (im_watcher *w) |
|
|
286 | { |
|
|
287 | imw.erase (find (imw.begin (), imw.end (), w)); |
|
|
288 | } |
|
|
289 | |
257 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
290 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
258 | { |
291 | { |
259 | char *id; |
292 | char *id; |
260 | int l, m; |
293 | int l, m; |
261 | |
294 | |
… | |
… | |
277 | |
310 | |
278 | void rxvt_display::put_xim (rxvt_xim *xim) |
311 | void rxvt_display::put_xim (rxvt_xim *xim) |
279 | { |
312 | { |
280 | xims.put (xim); |
313 | xims.put (xim); |
281 | } |
314 | } |
|
|
315 | #endif |
282 | |
316 | |
283 | Atom rxvt_display::atom (const char *name) |
317 | Atom rxvt_display::atom (const char *name) |
284 | { |
318 | { |
285 | return XInternAtom (display, name, False); |
319 | return XInternAtom (display, name, False); |
286 | } |
320 | } |
287 | |
321 | |
288 | ///////////////////////////////////////////////////////////////////////////// |
322 | ///////////////////////////////////////////////////////////////////////////// |
289 | |
323 | |
290 | template refcache<rxvt_display>; |
324 | template class refcache<rxvt_display>; |
291 | refcache<rxvt_display> displays; |
325 | refcache<rxvt_display> displays; |
292 | |
326 | |
293 | ///////////////////////////////////////////////////////////////////////////// |
327 | ///////////////////////////////////////////////////////////////////////////// |
294 | |
328 | |
295 | bool |
329 | bool |
… | |
… | |
375 | #else |
409 | #else |
376 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
410 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
377 | #endif |
411 | #endif |
378 | } |
412 | } |
379 | |
413 | |
|
|
414 | rxvt_color |
|
|
415 | rxvt_color::fade (rxvt_display *display, int percent) |
|
|
416 | { |
|
|
417 | unsigned short cr, cg, cb; |
|
|
418 | rxvt_color faded; |
|
|
419 | |
|
|
420 | get (display, cr, cg, cb); |
|
|
421 | faded.set (display, |
|
|
422 | cr * percent / 100, |
|
|
423 | cg * percent / 100, |
|
|
424 | cb * percent / 100); |
|
|
425 | |
|
|
426 | return faded; |
|
|
427 | } |
|
|
428 | |