|
|
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> |
6 | #include <fcntl.h> |
28 | #include <fcntl.h> |
7 | |
29 | |
|
|
30 | class byteorder byteorder; |
|
|
31 | |
|
|
32 | byteorder::byteorder () |
|
|
33 | { |
|
|
34 | union { |
|
|
35 | uint32_t u; |
|
|
36 | uint8_t b[4]; |
|
|
37 | } w; |
|
|
38 | |
|
|
39 | w.b[0] = 0x11; |
|
|
40 | w.b[1] = 0x22; |
|
|
41 | w.b[2] = 0x33; |
|
|
42 | w.b[3] = 0x44; |
|
|
43 | |
|
|
44 | e = w.u; |
|
|
45 | } |
|
|
46 | |
8 | refcounted::refcounted (const char *id) |
47 | refcounted::refcounted (const char *id) |
9 | { |
48 | { |
10 | this->id = STRDUP (id); |
49 | this->id = STRDUP (id); |
11 | } |
50 | } |
12 | |
51 | |
… | |
… | |
16 | } |
55 | } |
17 | |
56 | |
18 | template<class T> |
57 | template<class T> |
19 | T *refcache<T>::get (const char *id) |
58 | T *refcache<T>::get (const char *id) |
20 | { |
59 | { |
21 | for (T **i = begin (); i < end (); ++i) |
60 | for (T **i = this->begin (); i < this->end (); ++i) |
22 | { |
61 | { |
23 | if (!strcmp (id, (*i)->id)) |
62 | if (!strcmp (id, (*i)->id)) |
24 | { |
63 | { |
25 | (*i)->referenced++; |
64 | (*i)->referenced++; |
26 | return *i; |
65 | return *i; |
… | |
… | |
31 | |
70 | |
32 | obj->referenced = 1; |
71 | obj->referenced = 1; |
33 | |
72 | |
34 | if (obj && obj->init ()) |
73 | if (obj && obj->init ()) |
35 | { |
74 | { |
36 | push_back (obj); |
75 | this->push_back (obj); |
37 | return obj; |
76 | return obj; |
38 | } |
77 | } |
39 | else |
78 | else |
40 | { |
79 | { |
41 | delete obj; |
80 | delete obj; |
… | |
… | |
49 | if (!obj) |
88 | if (!obj) |
50 | return; |
89 | return; |
51 | |
90 | |
52 | if (!--obj->referenced) |
91 | if (!--obj->referenced) |
53 | { |
92 | { |
54 | erase (find (begin (), end (), obj)); |
93 | this->erase (find (this->begin (), this->end (), obj)); |
55 | delete obj; |
94 | delete obj; |
56 | } |
95 | } |
57 | } |
96 | } |
58 | |
97 | |
59 | template<class T> |
98 | template<class T> |
60 | refcache<T>::~refcache () |
99 | refcache<T>::~refcache () |
61 | { |
100 | { |
62 | while (size ()) |
101 | while (this->size ()) |
63 | put (*begin ()); |
102 | put (*this->begin ()); |
64 | } |
103 | } |
65 | |
104 | |
66 | ///////////////////////////////////////////////////////////////////////////// |
105 | ///////////////////////////////////////////////////////////////////////////// |
67 | |
106 | |
|
|
107 | #ifdef USE_XIM |
68 | static void |
108 | static void |
|
|
109 | #if XIMCB_PROTO_BROKEN |
|
|
110 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
|
|
111 | #else |
69 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
112 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
|
|
113 | #endif |
70 | { |
114 | { |
71 | rxvt_xim *xim = (rxvt_xim *)client_data; |
115 | rxvt_xim *xim = (rxvt_xim *)client_data; |
72 | rxvt_display *display = xim->display; |
116 | rxvt_display *display = xim->display; |
73 | |
117 | |
74 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
118 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
… | |
… | |
97 | rxvt_xim::~rxvt_xim () |
141 | rxvt_xim::~rxvt_xim () |
98 | { |
142 | { |
99 | if (xim) |
143 | if (xim) |
100 | XCloseIM (xim); |
144 | XCloseIM (xim); |
101 | } |
145 | } |
|
|
146 | #endif |
102 | |
147 | |
103 | ///////////////////////////////////////////////////////////////////////////// |
148 | ///////////////////////////////////////////////////////////////////////////// |
104 | |
149 | |
105 | rxvt_display::rxvt_display (const char *id) |
150 | rxvt_display::rxvt_display (const char *id) |
106 | : refcounted (id) |
151 | : refcounted (id) |
… | |
… | |
110 | } |
155 | } |
111 | |
156 | |
112 | bool rxvt_display::init () |
157 | bool rxvt_display::init () |
113 | { |
158 | { |
114 | display = XOpenDisplay (id); |
159 | display = XOpenDisplay (id); |
|
|
160 | |
|
|
161 | if (!display) |
|
|
162 | return false; |
115 | |
163 | |
116 | screen = DefaultScreen (display); |
164 | screen = DefaultScreen (display); |
117 | root = DefaultRootWindow (display); |
165 | root = DefaultRootWindow (display); |
118 | visual = DefaultVisual (display, screen); |
166 | visual = DefaultVisual (display, screen); |
119 | cmap = DefaultColormap (display, screen); |
167 | cmap = DefaultColormap (display, screen); |
… | |
… | |
138 | } |
186 | } |
139 | #endif |
187 | #endif |
140 | |
188 | |
141 | int fd = XConnectionNumber (display); |
189 | int fd = XConnectionNumber (display); |
142 | x_ev.start (fd, EVENT_READ); |
190 | x_ev.start (fd, EVENT_READ); |
143 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
191 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
144 | |
192 | |
145 | XSelectInput (display, root, PropertyChangeMask); |
193 | XSelectInput (display, root, PropertyChangeMask); |
|
|
194 | #ifdef USE_XIM |
146 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
195 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
196 | #endif |
147 | |
197 | |
148 | flush (); |
198 | flush (); |
149 | |
199 | |
150 | return true; |
200 | return true; |
151 | } |
201 | } |
152 | |
202 | |
153 | rxvt_display::~rxvt_display () |
203 | rxvt_display::~rxvt_display () |
154 | { |
204 | { |
155 | x_ev.stop (); |
205 | x_ev.stop (); |
156 | |
206 | |
|
|
207 | if (display) |
157 | XCloseDisplay (display); |
208 | XCloseDisplay (display); |
158 | } |
209 | } |
159 | |
210 | |
|
|
211 | #ifdef USE_XIM |
160 | void rxvt_display::im_change_cb () |
212 | void rxvt_display::im_change_cb () |
161 | { |
213 | { |
162 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
214 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
163 | (*i)->call (); |
215 | (*i)->call (); |
164 | } |
216 | } |
|
|
217 | #endif |
165 | |
218 | |
166 | void rxvt_display::x_cb (io_watcher &w, short revents) |
219 | void rxvt_display::x_cb (io_watcher &w, short revents) |
167 | { |
220 | { |
168 | do |
221 | do |
169 | { |
222 | { |
170 | XEvent xev; |
223 | XEvent xev; |
171 | XNextEvent (display, &xev); |
224 | XNextEvent (display, &xev); |
172 | |
225 | |
173 | //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 |
174 | |
227 | |
|
|
228 | #ifdef USE_XIM |
175 | if (xev.type == PropertyNotify |
229 | if (xev.type == PropertyNotify |
176 | && xev.xany.window == root |
230 | && xev.xany.window == root |
177 | && xev.xproperty.atom == xa_xim_servers) |
231 | && xev.xproperty.atom == xa_xim_servers) |
178 | im_change_cb (); |
232 | im_change_cb (); |
|
|
233 | #endif |
179 | |
234 | |
180 | for (int i = xw.size (); i--; ) |
235 | for (int i = xw.size (); i--; ) |
181 | { |
236 | { |
182 | if (!xw[i]) |
237 | if (!xw[i]) |
183 | xw.erase_unordered (i); |
238 | xw.erase_unordered (i); |
… | |
… | |
192 | |
247 | |
193 | void rxvt_display::flush () |
248 | void rxvt_display::flush () |
194 | { |
249 | { |
195 | for (;;) |
250 | for (;;) |
196 | { |
251 | { |
197 | XFlush (display); |
|
|
198 | |
|
|
199 | if (!XPending (display)) |
252 | if (!XPending (display)) |
200 | break; |
253 | break; |
201 | |
254 | |
202 | x_cb (x_ev, 0); |
255 | x_cb (x_ev, 0); |
203 | } |
256 | } |
… | |
… | |
213 | { |
266 | { |
214 | if (w->active) |
267 | if (w->active) |
215 | xw[w->active - 1] = 0; |
268 | xw[w->active - 1] = 0; |
216 | } |
269 | } |
217 | |
270 | |
218 | void rxvt_display::reg (im_watcher *w) |
|
|
219 | { |
|
|
220 | imw.push_back (w); |
|
|
221 | } |
|
|
222 | |
|
|
223 | void rxvt_display::unreg (im_watcher *w) |
|
|
224 | { |
|
|
225 | imw.erase (find (imw.begin (), imw.end (), w)); |
|
|
226 | } |
|
|
227 | |
|
|
228 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
271 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
229 | { |
272 | { |
230 | if (selection_owner && selection_owner != owner) |
273 | if (selection_owner && selection_owner != owner) |
231 | selection_owner->selection_clear (); |
274 | selection_owner->selection_clear (); |
232 | |
275 | |
233 | selection_owner = owner; |
276 | selection_owner = owner; |
234 | } |
277 | } |
235 | |
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 | |
236 | 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) |
237 | { |
291 | { |
238 | // asprintf is a GNU and *BSD extension.. sorry... |
|
|
239 | char *id; |
292 | char *id; |
|
|
293 | int l, m; |
240 | |
294 | |
241 | if (asprintf (&id, "%s\n%s", locale, modifiers) < 0) |
295 | l = strlen (locale); |
|
|
296 | m = strlen (modifiers); |
|
|
297 | |
|
|
298 | if (!(id = (char *)malloc (l + m + 2))) |
242 | return 0; |
299 | return 0; |
243 | |
300 | |
|
|
301 | memcpy (id, locale, l); id[l] = '\n'; |
|
|
302 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
|
|
303 | |
244 | rxvt_xim *xim = xims.get (id); |
304 | rxvt_xim *xim = xims.get (id); |
245 | |
305 | |
246 | free (id); |
306 | free (id); |
247 | |
307 | |
248 | return xim; |
308 | return xim; |
… | |
… | |
250 | |
310 | |
251 | void rxvt_display::put_xim (rxvt_xim *xim) |
311 | void rxvt_display::put_xim (rxvt_xim *xim) |
252 | { |
312 | { |
253 | xims.put (xim); |
313 | xims.put (xim); |
254 | } |
314 | } |
|
|
315 | #endif |
|
|
316 | |
|
|
317 | Atom rxvt_display::atom (const char *name) |
|
|
318 | { |
|
|
319 | return XInternAtom (display, name, False); |
|
|
320 | } |
255 | |
321 | |
256 | ///////////////////////////////////////////////////////////////////////////// |
322 | ///////////////////////////////////////////////////////////////////////////// |
257 | |
323 | |
258 | template refcache<rxvt_display>; |
324 | template class refcache<rxvt_display>; |
259 | refcache<rxvt_display> displays; |
325 | refcache<rxvt_display> displays; |
260 | |
326 | |
261 | ///////////////////////////////////////////////////////////////////////////// |
327 | ///////////////////////////////////////////////////////////////////////////// |
262 | |
328 | |
263 | bool |
329 | bool |
… | |
… | |
343 | #else |
409 | #else |
344 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
410 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
345 | #endif |
411 | #endif |
346 | } |
412 | } |
347 | |
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 | |