|
|
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 | |
|
|
47 | refcounted::refcounted (const char *id) |
|
|
48 | { |
|
|
49 | this->id = STRDUP (id); |
|
|
50 | } |
|
|
51 | |
|
|
52 | refcounted::~refcounted () |
|
|
53 | { |
|
|
54 | free (id); |
|
|
55 | } |
|
|
56 | |
|
|
57 | template<class T> |
|
|
58 | T *refcache<T>::get (const char *id) |
|
|
59 | { |
|
|
60 | for (T **i = this->begin (); i < this->end (); ++i) |
|
|
61 | { |
|
|
62 | if (!strcmp (id, (*i)->id)) |
|
|
63 | { |
|
|
64 | (*i)->referenced++; |
|
|
65 | return *i; |
|
|
66 | } |
|
|
67 | } |
|
|
68 | |
|
|
69 | T *obj = new T (id); |
|
|
70 | |
|
|
71 | obj->referenced = 1; |
|
|
72 | |
|
|
73 | if (obj && obj->init ()) |
|
|
74 | { |
|
|
75 | this->push_back (obj); |
|
|
76 | return obj; |
|
|
77 | } |
|
|
78 | else |
|
|
79 | { |
|
|
80 | delete obj; |
|
|
81 | return 0; |
|
|
82 | } |
|
|
83 | } |
|
|
84 | |
|
|
85 | template<class T> |
|
|
86 | void refcache<T>::put (T *obj) |
|
|
87 | { |
|
|
88 | if (!obj) |
|
|
89 | return; |
|
|
90 | |
|
|
91 | if (!--obj->referenced) |
|
|
92 | { |
|
|
93 | this->erase (find (this->begin (), this->end (), obj)); |
|
|
94 | delete obj; |
|
|
95 | } |
|
|
96 | } |
|
|
97 | |
|
|
98 | template<class T> |
|
|
99 | refcache<T>::~refcache () |
|
|
100 | { |
|
|
101 | while (this->size ()) |
|
|
102 | put (*this->begin ()); |
|
|
103 | } |
|
|
104 | |
8 | ///////////////////////////////////////////////////////////////////////////// |
105 | ///////////////////////////////////////////////////////////////////////////// |
9 | |
106 | |
|
|
107 | #ifdef USE_XIM |
|
|
108 | static void |
|
|
109 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
|
|
110 | { |
|
|
111 | rxvt_xim *xim = (rxvt_xim *)client_data; |
|
|
112 | rxvt_display *display = xim->display; |
|
|
113 | |
|
|
114 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
|
|
115 | |
|
|
116 | display->im_change_cb (); |
|
|
117 | } |
|
|
118 | |
|
|
119 | bool rxvt_xim::init () |
|
|
120 | { |
|
|
121 | display = GET_R->display; //HACK: TODO |
|
|
122 | |
|
|
123 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
|
|
124 | |
|
|
125 | if (!xim) |
|
|
126 | return false; |
|
|
127 | |
|
|
128 | XIMCallback ximcallback; |
|
|
129 | ximcallback.client_data = (XPointer)this; |
|
|
130 | ximcallback.callback = im_destroy_cb; |
|
|
131 | |
|
|
132 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); |
|
|
133 | |
|
|
134 | return true; |
|
|
135 | } |
|
|
136 | |
|
|
137 | rxvt_xim::~rxvt_xim () |
|
|
138 | { |
|
|
139 | if (xim) |
|
|
140 | XCloseIM (xim); |
|
|
141 | } |
|
|
142 | #endif |
|
|
143 | |
|
|
144 | ///////////////////////////////////////////////////////////////////////////// |
|
|
145 | |
10 | rxvt_display::rxvt_display (const char *name) |
146 | rxvt_display::rxvt_display (const char *id) |
|
|
147 | : refcounted (id) |
11 | : x_watcher (this, &rxvt_display::x_event) |
148 | , x_ev (this, &rxvt_display::x_cb) |
12 | , selection_owner (0) |
149 | , selection_owner (0) |
13 | { |
150 | { |
14 | this->name = STRDUP (name); |
|
|
15 | } |
151 | } |
16 | |
152 | |
17 | rxvt_display::~rxvt_display () |
|
|
18 | { |
|
|
19 | free (name); |
|
|
20 | } |
|
|
21 | |
|
|
22 | bool rxvt_display::open () |
153 | bool rxvt_display::init () |
23 | { |
154 | { |
24 | display = XOpenDisplay (name); |
155 | display = XOpenDisplay (id); |
|
|
156 | |
|
|
157 | if (!display) |
|
|
158 | return false; |
25 | |
159 | |
26 | screen = DefaultScreen (display); |
160 | screen = DefaultScreen (display); |
27 | root = DefaultRootWindow (display); |
161 | root = DefaultRootWindow (display); |
28 | visual = DefaultVisual (display, screen); |
162 | visual = DefaultVisual (display, screen); |
29 | cmap = DefaultColormap (display, screen); |
163 | cmap = DefaultColormap (display, screen); |
… | |
… | |
47 | } |
181 | } |
48 | } |
182 | } |
49 | #endif |
183 | #endif |
50 | |
184 | |
51 | int fd = XConnectionNumber (display); |
185 | int fd = XConnectionNumber (display); |
52 | x_watcher.start (fd, EVENT_READ); |
186 | x_ev.start (fd, EVENT_READ); |
53 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
187 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
188 | |
|
|
189 | XSelectInput (display, root, PropertyChangeMask); |
|
|
190 | #ifdef USE_XIM |
|
|
191 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
192 | #endif |
|
|
193 | |
|
|
194 | flush (); |
54 | |
195 | |
55 | return true; |
196 | return true; |
56 | } |
197 | } |
57 | |
198 | |
58 | void rxvt_display::close () |
199 | rxvt_display::~rxvt_display () |
59 | { |
200 | { |
60 | x_watcher.stop (); |
201 | x_ev.stop (); |
61 | |
202 | |
|
|
203 | if (display) |
62 | XCloseDisplay (display); |
204 | XCloseDisplay (display); |
63 | } |
205 | } |
64 | |
206 | |
|
|
207 | #ifdef USE_XIM |
|
|
208 | void rxvt_display::im_change_cb () |
|
|
209 | { |
|
|
210 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
|
|
211 | (*i)->call (); |
|
|
212 | } |
|
|
213 | #endif |
|
|
214 | |
65 | void rxvt_display::x_event (io_watcher &w, short revents) |
215 | void rxvt_display::x_cb (io_watcher &w, short revents) |
66 | { |
216 | { |
67 | do |
217 | do |
68 | { |
218 | { |
69 | XEvent xev; |
219 | XEvent xev; |
70 | XNextEvent (display, &xev); |
220 | XNextEvent (display, &xev); |
|
|
221 | |
|
|
222 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
|
|
223 | |
|
|
224 | #ifdef USE_XIM |
|
|
225 | if (xev.type == PropertyNotify |
|
|
226 | && xev.xany.window == root |
|
|
227 | && xev.xproperty.atom == xa_xim_servers) |
|
|
228 | im_change_cb (); |
|
|
229 | #endif |
71 | |
230 | |
72 | for (int i = xw.size (); i--; ) |
231 | for (int i = xw.size (); i--; ) |
73 | { |
232 | { |
74 | if (!xw[i]) |
233 | if (!xw[i]) |
75 | xw.erase_unordered (i); |
234 | xw.erase_unordered (i); |
76 | else if (xw[i]->window == xev.xany.window) |
235 | else if (xw[i]->window == xev.xany.window) |
77 | xw[i]->call (xev); |
236 | xw[i]->call (xev); |
78 | } |
237 | } |
79 | } |
238 | } |
80 | while (XPending (display)); |
239 | while (XPending (display)); |
|
|
240 | |
|
|
241 | flush (); |
|
|
242 | } |
|
|
243 | |
|
|
244 | void rxvt_display::flush () |
|
|
245 | { |
|
|
246 | for (;;) |
|
|
247 | { |
|
|
248 | XFlush (display); |
|
|
249 | |
|
|
250 | if (!XPending (display)) |
|
|
251 | break; |
|
|
252 | |
|
|
253 | x_cb (x_ev, 0); |
|
|
254 | } |
81 | } |
255 | } |
82 | |
256 | |
83 | void rxvt_display::reg (xevent_watcher *w) |
257 | void rxvt_display::reg (xevent_watcher *w) |
84 | { |
258 | { |
85 | xw.push_back (w); |
259 | xw.push_back (w); |
… | |
… | |
98 | selection_owner->selection_clear (); |
272 | selection_owner->selection_clear (); |
99 | |
273 | |
100 | selection_owner = owner; |
274 | selection_owner = owner; |
101 | } |
275 | } |
102 | |
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 | |
|
|
288 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
|
|
289 | { |
|
|
290 | char *id; |
|
|
291 | int l, m; |
|
|
292 | |
|
|
293 | l = strlen (locale); |
|
|
294 | m = strlen (modifiers); |
|
|
295 | |
|
|
296 | if (!(id = (char *)malloc (l + m + 2))) |
|
|
297 | return 0; |
|
|
298 | |
|
|
299 | memcpy (id, locale, l); id[l] = '\n'; |
|
|
300 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
|
|
301 | |
|
|
302 | rxvt_xim *xim = xims.get (id); |
|
|
303 | |
|
|
304 | free (id); |
|
|
305 | |
|
|
306 | return xim; |
|
|
307 | } |
|
|
308 | |
|
|
309 | void rxvt_display::put_xim (rxvt_xim *xim) |
|
|
310 | { |
|
|
311 | xims.put (xim); |
|
|
312 | } |
|
|
313 | #endif |
|
|
314 | |
|
|
315 | Atom rxvt_display::atom (const char *name) |
|
|
316 | { |
|
|
317 | return XInternAtom (display, name, False); |
|
|
318 | } |
|
|
319 | |
103 | ///////////////////////////////////////////////////////////////////////////// |
320 | ///////////////////////////////////////////////////////////////////////////// |
104 | |
321 | |
|
|
322 | template class refcache<rxvt_display>; |
105 | rxvt_displays displays; |
323 | refcache<rxvt_display> displays; |
106 | |
|
|
107 | rxvt_display *rxvt_displays::get (const char *name) |
|
|
108 | { |
|
|
109 | for (rxvt_display **i = list.begin (); i < list.end (); ++i) |
|
|
110 | { |
|
|
111 | if (!strcmp (name, (*i)->name)) |
|
|
112 | { |
|
|
113 | (*i)->referenced++; |
|
|
114 | return *i; |
|
|
115 | } |
|
|
116 | } |
|
|
117 | |
|
|
118 | rxvt_display *display = new rxvt_display (name); |
|
|
119 | |
|
|
120 | display->referenced = 1; |
|
|
121 | |
|
|
122 | if (display && display->open ()) |
|
|
123 | list.push_back (display); |
|
|
124 | else |
|
|
125 | { |
|
|
126 | delete display; |
|
|
127 | display = 0; |
|
|
128 | } |
|
|
129 | |
|
|
130 | return display; |
|
|
131 | } |
|
|
132 | |
|
|
133 | void rxvt_displays::release (rxvt_display *display) |
|
|
134 | { |
|
|
135 | if (!--display->referenced) |
|
|
136 | { |
|
|
137 | display->close (); |
|
|
138 | delete display; |
|
|
139 | list.erase (find (list.begin (), list.end (), display)); |
|
|
140 | } |
|
|
141 | } |
|
|
142 | |
324 | |
143 | ///////////////////////////////////////////////////////////////////////////// |
325 | ///////////////////////////////////////////////////////////////////////////// |
144 | |
326 | |
145 | bool |
327 | bool |
146 | rxvt_color::set (rxvt_display *display, Pixel p) |
328 | rxvt_color::set (rxvt_display *display, Pixel p) |
… | |
… | |
221 | rxvt_color::free (rxvt_display *display) |
403 | rxvt_color::free (rxvt_display *display) |
222 | { |
404 | { |
223 | #if XFT |
405 | #if XFT |
224 | XftColorFree (display->display, display->visual, display->cmap, &c); |
406 | XftColorFree (display->display, display->visual, display->cmap, &c); |
225 | #else |
407 | #else |
226 | XFreeColors (display->display, display->cmap, &c, 1, AllPlanes); |
408 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
227 | #endif |
409 | #endif |
228 | } |
410 | } |
229 | |
411 | |