|
|
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 |
69 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
109 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
70 | { |
110 | { |
71 | rxvt_xim *xim = (rxvt_xim *)client_data; |
111 | rxvt_xim *xim = (rxvt_xim *)client_data; |
72 | rxvt_display *display = xim->display; |
112 | rxvt_display *display = xim->display; |
… | |
… | |
97 | rxvt_xim::~rxvt_xim () |
137 | rxvt_xim::~rxvt_xim () |
98 | { |
138 | { |
99 | if (xim) |
139 | if (xim) |
100 | XCloseIM (xim); |
140 | XCloseIM (xim); |
101 | } |
141 | } |
|
|
142 | #endif |
102 | |
143 | |
103 | ///////////////////////////////////////////////////////////////////////////// |
144 | ///////////////////////////////////////////////////////////////////////////// |
104 | |
145 | |
105 | rxvt_display::rxvt_display (const char *id) |
146 | rxvt_display::rxvt_display (const char *id) |
106 | : refcounted (id) |
147 | : refcounted (id) |
… | |
… | |
141 | } |
182 | } |
142 | #endif |
183 | #endif |
143 | |
184 | |
144 | int fd = XConnectionNumber (display); |
185 | int fd = XConnectionNumber (display); |
145 | x_ev.start (fd, EVENT_READ); |
186 | x_ev.start (fd, EVENT_READ); |
146 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
187 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
147 | |
188 | |
148 | XSelectInput (display, root, PropertyChangeMask); |
189 | XSelectInput (display, root, PropertyChangeMask); |
|
|
190 | #ifdef USE_XIM |
149 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
191 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
192 | #endif |
150 | |
193 | |
151 | flush (); |
194 | flush (); |
152 | |
195 | |
153 | return true; |
196 | return true; |
154 | } |
197 | } |
… | |
… | |
159 | |
202 | |
160 | if (display) |
203 | if (display) |
161 | XCloseDisplay (display); |
204 | XCloseDisplay (display); |
162 | } |
205 | } |
163 | |
206 | |
|
|
207 | #ifdef USE_XIM |
164 | void rxvt_display::im_change_cb () |
208 | void rxvt_display::im_change_cb () |
165 | { |
209 | { |
166 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
210 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
167 | (*i)->call (); |
211 | (*i)->call (); |
168 | } |
212 | } |
|
|
213 | #endif |
169 | |
214 | |
170 | void rxvt_display::x_cb (io_watcher &w, short revents) |
215 | void rxvt_display::x_cb (io_watcher &w, short revents) |
171 | { |
216 | { |
172 | do |
217 | do |
173 | { |
218 | { |
174 | XEvent xev; |
219 | XEvent xev; |
175 | XNextEvent (display, &xev); |
220 | XNextEvent (display, &xev); |
176 | |
221 | |
177 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
222 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
178 | |
223 | |
|
|
224 | #ifdef USE_XIM |
179 | if (xev.type == PropertyNotify |
225 | if (xev.type == PropertyNotify |
180 | && xev.xany.window == root |
226 | && xev.xany.window == root |
181 | && xev.xproperty.atom == xa_xim_servers) |
227 | && xev.xproperty.atom == xa_xim_servers) |
182 | im_change_cb (); |
228 | im_change_cb (); |
|
|
229 | #endif |
183 | |
230 | |
184 | for (int i = xw.size (); i--; ) |
231 | for (int i = xw.size (); i--; ) |
185 | { |
232 | { |
186 | if (!xw[i]) |
233 | if (!xw[i]) |
187 | xw.erase_unordered (i); |
234 | xw.erase_unordered (i); |
… | |
… | |
217 | { |
264 | { |
218 | if (w->active) |
265 | if (w->active) |
219 | xw[w->active - 1] = 0; |
266 | xw[w->active - 1] = 0; |
220 | } |
267 | } |
221 | |
268 | |
222 | void rxvt_display::reg (im_watcher *w) |
|
|
223 | { |
|
|
224 | imw.push_back (w); |
|
|
225 | } |
|
|
226 | |
|
|
227 | void rxvt_display::unreg (im_watcher *w) |
|
|
228 | { |
|
|
229 | imw.erase (find (imw.begin (), imw.end (), w)); |
|
|
230 | } |
|
|
231 | |
|
|
232 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
269 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
233 | { |
270 | { |
234 | if (selection_owner && selection_owner != owner) |
271 | if (selection_owner && selection_owner != owner) |
235 | selection_owner->selection_clear (); |
272 | selection_owner->selection_clear (); |
236 | |
273 | |
237 | selection_owner = owner; |
274 | selection_owner = owner; |
238 | } |
275 | } |
239 | |
276 | |
|
|
277 | #ifdef USE_XIM |
|
|
278 | void rxvt_display::reg (im_watcher *w) |
|
|
279 | { |
|
|
280 | imw.push_back (w); |
|
|
281 | } |
|
|
282 | |
|
|
283 | void rxvt_display::unreg (im_watcher *w) |
|
|
284 | { |
|
|
285 | imw.erase (find (imw.begin (), imw.end (), w)); |
|
|
286 | } |
|
|
287 | |
240 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
288 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
241 | { |
289 | { |
242 | // asprintf is a GNU and *BSD extension.. sorry... |
|
|
243 | char *id; |
290 | char *id; |
|
|
291 | int l, m; |
244 | |
292 | |
245 | if (asprintf (&id, "%s\n%s", locale, modifiers) < 0) |
293 | l = strlen (locale); |
|
|
294 | m = strlen (modifiers); |
|
|
295 | |
|
|
296 | if (!(id = (char *)malloc (l + m + 2))) |
246 | return 0; |
297 | return 0; |
247 | |
298 | |
|
|
299 | memcpy (id, locale, l); id[l] = '\n'; |
|
|
300 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
|
|
301 | |
248 | rxvt_xim *xim = xims.get (id); |
302 | rxvt_xim *xim = xims.get (id); |
249 | |
303 | |
250 | free (id); |
304 | free (id); |
251 | |
305 | |
252 | return xim; |
306 | return xim; |
… | |
… | |
254 | |
308 | |
255 | void rxvt_display::put_xim (rxvt_xim *xim) |
309 | void rxvt_display::put_xim (rxvt_xim *xim) |
256 | { |
310 | { |
257 | xims.put (xim); |
311 | xims.put (xim); |
258 | } |
312 | } |
|
|
313 | #endif |
|
|
314 | |
|
|
315 | Atom rxvt_display::atom (const char *name) |
|
|
316 | { |
|
|
317 | return XInternAtom (display, name, False); |
|
|
318 | } |
259 | |
319 | |
260 | ///////////////////////////////////////////////////////////////////////////// |
320 | ///////////////////////////////////////////////////////////////////////////// |
261 | |
321 | |
262 | template refcache<rxvt_display>; |
322 | template class refcache<rxvt_display>; |
263 | refcache<rxvt_display> displays; |
323 | refcache<rxvt_display> displays; |
264 | |
324 | |
265 | ///////////////////////////////////////////////////////////////////////////// |
325 | ///////////////////////////////////////////////////////////////////////////// |
266 | |
326 | |
267 | bool |
327 | bool |