|
|
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 | #ifndef NO_SLOW_LINK_SUPPORT |
|
|
31 | # include <sys/socket.h> |
|
|
32 | # include <sys/un.h> |
|
|
33 | #endif |
|
|
34 | |
|
|
35 | class byteorder byteorder; |
|
|
36 | |
|
|
37 | byteorder::byteorder () |
|
|
38 | { |
|
|
39 | union { |
|
|
40 | uint32_t u; |
|
|
41 | uint8_t b[4]; |
|
|
42 | } w; |
|
|
43 | |
|
|
44 | w.b[0] = 0x11; |
|
|
45 | w.b[1] = 0x22; |
|
|
46 | w.b[2] = 0x33; |
|
|
47 | w.b[3] = 0x44; |
|
|
48 | |
|
|
49 | e = w.u; |
|
|
50 | } |
|
|
51 | |
8 | refcounted::refcounted (const char *id) |
52 | refcounted::refcounted (const char *id) |
9 | { |
53 | { |
10 | this->id = STRDUP (id); |
54 | this->id = STRDUP (id); |
11 | } |
55 | } |
12 | |
56 | |
… | |
… | |
16 | } |
60 | } |
17 | |
61 | |
18 | template<class T> |
62 | template<class T> |
19 | T *refcache<T>::get (const char *id) |
63 | T *refcache<T>::get (const char *id) |
20 | { |
64 | { |
21 | for (T **i = begin (); i < end (); ++i) |
65 | for (T **i = this->begin (); i < this->end (); ++i) |
22 | { |
66 | { |
23 | if (!strcmp (id, (*i)->id)) |
67 | if (!strcmp (id, (*i)->id)) |
24 | { |
68 | { |
25 | (*i)->referenced++; |
69 | (*i)->referenced++; |
26 | return *i; |
70 | return *i; |
… | |
… | |
31 | |
75 | |
32 | obj->referenced = 1; |
76 | obj->referenced = 1; |
33 | |
77 | |
34 | if (obj && obj->init ()) |
78 | if (obj && obj->init ()) |
35 | { |
79 | { |
36 | push_back (obj); |
80 | this->push_back (obj); |
37 | return obj; |
81 | return obj; |
38 | } |
82 | } |
39 | else |
83 | else |
40 | { |
84 | { |
41 | delete obj; |
85 | delete obj; |
… | |
… | |
49 | if (!obj) |
93 | if (!obj) |
50 | return; |
94 | return; |
51 | |
95 | |
52 | if (!--obj->referenced) |
96 | if (!--obj->referenced) |
53 | { |
97 | { |
54 | erase (find (begin (), end (), obj)); |
98 | this->erase (find (this->begin (), this->end (), obj)); |
55 | delete obj; |
99 | delete obj; |
56 | } |
100 | } |
57 | } |
101 | } |
58 | |
102 | |
59 | template<class T> |
103 | template<class T> |
60 | refcache<T>::~refcache () |
104 | refcache<T>::~refcache () |
61 | { |
105 | { |
62 | while (size ()) |
106 | while (this->size ()) |
63 | put (*begin ()); |
107 | put (*this->begin ()); |
64 | } |
108 | } |
65 | |
109 | |
66 | ///////////////////////////////////////////////////////////////////////////// |
110 | ///////////////////////////////////////////////////////////////////////////// |
67 | |
111 | |
|
|
112 | #ifdef USE_XIM |
68 | static void |
113 | static void |
|
|
114 | #if XIMCB_PROTO_BROKEN |
|
|
115 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
|
|
116 | #else |
69 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
117 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
|
|
118 | #endif |
70 | { |
119 | { |
71 | rxvt_xim *xim = (rxvt_xim *)client_data; |
120 | rxvt_xim *xim = (rxvt_xim *)client_data; |
72 | rxvt_display *display = xim->display; |
121 | rxvt_display *display = xim->display; |
73 | |
122 | |
74 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
123 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
… | |
… | |
97 | rxvt_xim::~rxvt_xim () |
146 | rxvt_xim::~rxvt_xim () |
98 | { |
147 | { |
99 | if (xim) |
148 | if (xim) |
100 | XCloseIM (xim); |
149 | XCloseIM (xim); |
101 | } |
150 | } |
|
|
151 | #endif |
102 | |
152 | |
103 | ///////////////////////////////////////////////////////////////////////////// |
153 | ///////////////////////////////////////////////////////////////////////////// |
104 | |
154 | |
105 | rxvt_display::rxvt_display (const char *id) |
155 | rxvt_display::rxvt_display (const char *id) |
106 | : refcounted (id) |
156 | : refcounted (id) |
… | |
… | |
110 | } |
160 | } |
111 | |
161 | |
112 | bool rxvt_display::init () |
162 | bool rxvt_display::init () |
113 | { |
163 | { |
114 | display = XOpenDisplay (id); |
164 | display = XOpenDisplay (id); |
|
|
165 | |
|
|
166 | if (!display) |
|
|
167 | return false; |
115 | |
168 | |
116 | screen = DefaultScreen (display); |
169 | screen = DefaultScreen (display); |
117 | root = DefaultRootWindow (display); |
170 | root = DefaultRootWindow (display); |
118 | visual = DefaultVisual (display, screen); |
171 | visual = DefaultVisual (display, screen); |
119 | cmap = DefaultColormap (display, screen); |
172 | cmap = DefaultColormap (display, screen); |
120 | depth = DefaultDepth (display, screen); |
173 | depth = DefaultDepth (display, screen); |
|
|
174 | |
|
|
175 | int fd = XConnectionNumber (display); |
|
|
176 | |
|
|
177 | #ifndef NO_SLOW_LINK_SUPPORT |
|
|
178 | // try to detetc wether we have a local connection. |
|
|
179 | // assume unix domains socket == local, everything else not |
|
|
180 | // TODO: might want to check for inet/127.0.0.1 |
|
|
181 | is_local = 0; |
|
|
182 | sockaddr_un sa; |
|
|
183 | socklen_t sl = sizeof (sa); |
|
|
184 | |
|
|
185 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
|
|
186 | is_local = sa.sun_family == AF_LOCAL; |
|
|
187 | #endif |
121 | |
188 | |
122 | #ifdef PREFER_24BIT |
189 | #ifdef PREFER_24BIT |
123 | /* |
190 | /* |
124 | * If depth is not 24, look for a 24bit visual. |
191 | * If depth is not 24, look for a 24bit visual. |
125 | */ |
192 | */ |
… | |
… | |
136 | visual, AllocNone); |
203 | visual, AllocNone); |
137 | } |
204 | } |
138 | } |
205 | } |
139 | #endif |
206 | #endif |
140 | |
207 | |
141 | int fd = XConnectionNumber (display); |
|
|
142 | x_ev.start (fd, EVENT_READ); |
208 | x_ev.start (fd, EVENT_READ); |
143 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
209 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
144 | |
210 | |
145 | XSelectInput (display, root, PropertyChangeMask); |
211 | XSelectInput (display, root, PropertyChangeMask); |
|
|
212 | #ifdef USE_XIM |
146 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
213 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
214 | #endif |
147 | |
215 | |
148 | flush (); |
216 | flush (); |
149 | |
217 | |
150 | return true; |
218 | return true; |
151 | } |
219 | } |
152 | |
220 | |
153 | rxvt_display::~rxvt_display () |
221 | rxvt_display::~rxvt_display () |
154 | { |
222 | { |
155 | x_ev.stop (); |
223 | x_ev.stop (); |
156 | |
224 | |
|
|
225 | if (display) |
157 | XCloseDisplay (display); |
226 | XCloseDisplay (display); |
158 | } |
227 | } |
159 | |
228 | |
|
|
229 | #ifdef USE_XIM |
160 | void rxvt_display::im_change_cb () |
230 | void rxvt_display::im_change_cb () |
161 | { |
231 | { |
162 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
232 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
163 | (*i)->call (); |
233 | (*i)->call (); |
164 | } |
234 | } |
|
|
235 | #endif |
165 | |
236 | |
166 | void rxvt_display::x_cb (io_watcher &w, short revents) |
237 | void rxvt_display::x_cb (io_watcher &w, short revents) |
167 | { |
238 | { |
168 | do |
239 | do |
169 | { |
240 | { |
170 | XEvent xev; |
241 | XEvent xev; |
171 | XNextEvent (display, &xev); |
242 | XNextEvent (display, &xev); |
172 | |
243 | |
173 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
244 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
174 | |
245 | |
|
|
246 | #ifdef USE_XIM |
175 | if (xev.type == PropertyNotify |
247 | if (xev.type == PropertyNotify |
176 | && xev.xany.window == root |
248 | && xev.xany.window == root |
177 | && xev.xproperty.atom == xa_xim_servers) |
249 | && xev.xproperty.atom == xa_xim_servers) |
178 | im_change_cb (); |
250 | im_change_cb (); |
|
|
251 | #endif |
179 | |
252 | |
180 | for (int i = xw.size (); i--; ) |
253 | for (int i = xw.size (); i--; ) |
181 | { |
254 | { |
182 | if (!xw[i]) |
255 | if (!xw[i]) |
183 | xw.erase_unordered (i); |
256 | xw.erase_unordered (i); |
… | |
… | |
192 | |
265 | |
193 | void rxvt_display::flush () |
266 | void rxvt_display::flush () |
194 | { |
267 | { |
195 | for (;;) |
268 | for (;;) |
196 | { |
269 | { |
197 | XFlush (display); |
|
|
198 | |
|
|
199 | if (!XPending (display)) |
270 | if (!XPending (display)) |
200 | break; |
271 | break; |
201 | |
272 | |
202 | x_cb (x_ev, 0); |
273 | x_cb (x_ev, 0); |
203 | } |
274 | } |
… | |
… | |
213 | { |
284 | { |
214 | if (w->active) |
285 | if (w->active) |
215 | xw[w->active - 1] = 0; |
286 | xw[w->active - 1] = 0; |
216 | } |
287 | } |
217 | |
288 | |
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) |
289 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
229 | { |
290 | { |
230 | if (selection_owner && selection_owner != owner) |
291 | if (selection_owner && selection_owner != owner) |
231 | selection_owner->selection_clear (); |
292 | selection_owner->selection_clear (); |
232 | |
293 | |
233 | selection_owner = owner; |
294 | selection_owner = owner; |
234 | } |
295 | } |
235 | |
296 | |
|
|
297 | #ifdef USE_XIM |
|
|
298 | void rxvt_display::reg (im_watcher *w) |
|
|
299 | { |
|
|
300 | imw.push_back (w); |
|
|
301 | } |
|
|
302 | |
|
|
303 | void rxvt_display::unreg (im_watcher *w) |
|
|
304 | { |
|
|
305 | imw.erase (find (imw.begin (), imw.end (), w)); |
|
|
306 | } |
|
|
307 | |
236 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
308 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
237 | { |
309 | { |
238 | // asprintf is a GNU and *BSD extension.. sorry... |
|
|
239 | char *id; |
310 | char *id; |
|
|
311 | int l, m; |
240 | |
312 | |
241 | if (asprintf (&id, "%s\n%s", locale, modifiers) < 0) |
313 | l = strlen (locale); |
|
|
314 | m = strlen (modifiers); |
|
|
315 | |
|
|
316 | if (!(id = (char *)malloc (l + m + 2))) |
242 | return 0; |
317 | return 0; |
243 | |
318 | |
|
|
319 | memcpy (id, locale, l); id[l] = '\n'; |
|
|
320 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
|
|
321 | |
244 | rxvt_xim *xim = xims.get (id); |
322 | rxvt_xim *xim = xims.get (id); |
245 | |
323 | |
246 | free (id); |
324 | free (id); |
247 | |
325 | |
248 | return xim; |
326 | return xim; |
… | |
… | |
250 | |
328 | |
251 | void rxvt_display::put_xim (rxvt_xim *xim) |
329 | void rxvt_display::put_xim (rxvt_xim *xim) |
252 | { |
330 | { |
253 | xims.put (xim); |
331 | xims.put (xim); |
254 | } |
332 | } |
|
|
333 | #endif |
|
|
334 | |
|
|
335 | Atom rxvt_display::atom (const char *name) |
|
|
336 | { |
|
|
337 | return XInternAtom (display, name, False); |
|
|
338 | } |
255 | |
339 | |
256 | ///////////////////////////////////////////////////////////////////////////// |
340 | ///////////////////////////////////////////////////////////////////////////// |
257 | |
341 | |
258 | template refcache<rxvt_display>; |
342 | template class refcache<rxvt_display>; |
259 | refcache<rxvt_display> displays; |
343 | refcache<rxvt_display> displays; |
260 | |
344 | |
261 | ///////////////////////////////////////////////////////////////////////////// |
345 | ///////////////////////////////////////////////////////////////////////////// |
262 | |
346 | |
263 | bool |
347 | bool |
… | |
… | |
343 | #else |
427 | #else |
344 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
428 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
345 | #endif |
429 | #endif |
346 | } |
430 | } |
347 | |
431 | |
|
|
432 | rxvt_color |
|
|
433 | rxvt_color::fade (rxvt_display *display, int percent) |
|
|
434 | { |
|
|
435 | unsigned short cr, cg, cb; |
|
|
436 | rxvt_color faded; |
|
|
437 | |
|
|
438 | get (display, cr, cg, cb); |
|
|
439 | faded.set (display, |
|
|
440 | cr * percent / 100, |
|
|
441 | cg * percent / 100, |
|
|
442 | cb * percent / 100); |
|
|
443 | |
|
|
444 | return faded; |
|
|
445 | } |
|
|
446 | |