|
|
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> |
|
|
29 | |
|
|
30 | #ifndef NO_SLOW_LINK_SUPPORT |
|
|
31 | # include <sys/socket.h> |
|
|
32 | # include <sys/un.h> |
|
|
33 | #endif |
7 | |
34 | |
8 | class byteorder byteorder; |
35 | class byteorder byteorder; |
9 | |
36 | |
10 | byteorder::byteorder () |
37 | byteorder::byteorder () |
11 | { |
38 | { |
… | |
… | |
33 | } |
60 | } |
34 | |
61 | |
35 | template<class T> |
62 | template<class T> |
36 | T *refcache<T>::get (const char *id) |
63 | T *refcache<T>::get (const char *id) |
37 | { |
64 | { |
38 | for (T **i = begin (); i < end (); ++i) |
65 | for (T **i = this->begin (); i < this->end (); ++i) |
39 | { |
66 | { |
40 | if (!strcmp (id, (*i)->id)) |
67 | if (!strcmp (id, (*i)->id)) |
41 | { |
68 | { |
42 | (*i)->referenced++; |
69 | (*i)->referenced++; |
43 | return *i; |
70 | return *i; |
… | |
… | |
48 | |
75 | |
49 | obj->referenced = 1; |
76 | obj->referenced = 1; |
50 | |
77 | |
51 | if (obj && obj->init ()) |
78 | if (obj && obj->init ()) |
52 | { |
79 | { |
53 | push_back (obj); |
80 | this->push_back (obj); |
54 | return obj; |
81 | return obj; |
55 | } |
82 | } |
56 | else |
83 | else |
57 | { |
84 | { |
58 | delete obj; |
85 | delete obj; |
… | |
… | |
66 | if (!obj) |
93 | if (!obj) |
67 | return; |
94 | return; |
68 | |
95 | |
69 | if (!--obj->referenced) |
96 | if (!--obj->referenced) |
70 | { |
97 | { |
71 | erase (find (begin (), end (), obj)); |
98 | this->erase (find (this->begin (), this->end (), obj)); |
72 | delete obj; |
99 | delete obj; |
73 | } |
100 | } |
74 | } |
101 | } |
75 | |
102 | |
76 | template<class T> |
103 | template<class T> |
77 | refcache<T>::~refcache () |
104 | refcache<T>::~refcache () |
78 | { |
105 | { |
79 | while (size ()) |
106 | while (this->size ()) |
80 | put (*begin ()); |
107 | put (*this->begin ()); |
81 | } |
108 | } |
82 | |
109 | |
83 | ///////////////////////////////////////////////////////////////////////////// |
110 | ///////////////////////////////////////////////////////////////////////////// |
84 | |
111 | |
|
|
112 | #ifdef USE_XIM |
85 | static void |
113 | static void |
|
|
114 | #if XIMCB_PROTO_BROKEN |
|
|
115 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
|
|
116 | #else |
86 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
117 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
|
|
118 | #endif |
87 | { |
119 | { |
88 | rxvt_xim *xim = (rxvt_xim *)client_data; |
120 | rxvt_xim *xim = (rxvt_xim *)client_data; |
89 | rxvt_display *display = xim->display; |
121 | rxvt_display *display = xim->display; |
90 | |
122 | |
91 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
123 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
… | |
… | |
114 | rxvt_xim::~rxvt_xim () |
146 | rxvt_xim::~rxvt_xim () |
115 | { |
147 | { |
116 | if (xim) |
148 | if (xim) |
117 | XCloseIM (xim); |
149 | XCloseIM (xim); |
118 | } |
150 | } |
|
|
151 | #endif |
119 | |
152 | |
120 | ///////////////////////////////////////////////////////////////////////////// |
153 | ///////////////////////////////////////////////////////////////////////////// |
121 | |
154 | |
122 | rxvt_display::rxvt_display (const char *id) |
155 | rxvt_display::rxvt_display (const char *id) |
123 | : refcounted (id) |
156 | : refcounted (id) |
… | |
… | |
136 | screen = DefaultScreen (display); |
169 | screen = DefaultScreen (display); |
137 | root = DefaultRootWindow (display); |
170 | root = DefaultRootWindow (display); |
138 | visual = DefaultVisual (display, screen); |
171 | visual = DefaultVisual (display, screen); |
139 | cmap = DefaultColormap (display, screen); |
172 | cmap = DefaultColormap (display, screen); |
140 | 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 |
141 | |
188 | |
142 | #ifdef PREFER_24BIT |
189 | #ifdef PREFER_24BIT |
143 | /* |
190 | /* |
144 | * If depth is not 24, look for a 24bit visual. |
191 | * If depth is not 24, look for a 24bit visual. |
145 | */ |
192 | */ |
… | |
… | |
156 | visual, AllocNone); |
203 | visual, AllocNone); |
157 | } |
204 | } |
158 | } |
205 | } |
159 | #endif |
206 | #endif |
160 | |
207 | |
161 | int fd = XConnectionNumber (display); |
|
|
162 | x_ev.start (fd, EVENT_READ); |
208 | x_ev.start (fd, EVENT_READ); |
163 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
209 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
164 | |
210 | |
165 | XSelectInput (display, root, PropertyChangeMask); |
211 | XSelectInput (display, root, PropertyChangeMask); |
|
|
212 | #ifdef USE_XIM |
166 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
213 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
214 | #endif |
167 | |
215 | |
168 | flush (); |
216 | flush (); |
169 | |
217 | |
170 | return true; |
218 | return true; |
171 | } |
219 | } |
… | |
… | |
176 | |
224 | |
177 | if (display) |
225 | if (display) |
178 | XCloseDisplay (display); |
226 | XCloseDisplay (display); |
179 | } |
227 | } |
180 | |
228 | |
|
|
229 | #ifdef USE_XIM |
181 | void rxvt_display::im_change_cb () |
230 | void rxvt_display::im_change_cb () |
182 | { |
231 | { |
183 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
232 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
184 | (*i)->call (); |
233 | (*i)->call (); |
185 | } |
234 | } |
|
|
235 | #endif |
186 | |
236 | |
187 | void rxvt_display::x_cb (io_watcher &w, short revents) |
237 | void rxvt_display::x_cb (io_watcher &w, short revents) |
188 | { |
238 | { |
189 | do |
239 | do |
190 | { |
240 | { |
191 | XEvent xev; |
241 | XEvent xev; |
192 | XNextEvent (display, &xev); |
242 | XNextEvent (display, &xev); |
193 | |
243 | |
194 | //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 |
195 | |
245 | |
|
|
246 | #ifdef USE_XIM |
196 | if (xev.type == PropertyNotify |
247 | if (xev.type == PropertyNotify |
197 | && xev.xany.window == root |
248 | && xev.xany.window == root |
198 | && xev.xproperty.atom == xa_xim_servers) |
249 | && xev.xproperty.atom == xa_xim_servers) |
199 | im_change_cb (); |
250 | im_change_cb (); |
|
|
251 | #endif |
200 | |
252 | |
201 | for (int i = xw.size (); i--; ) |
253 | for (int i = xw.size (); i--; ) |
202 | { |
254 | { |
203 | if (!xw[i]) |
255 | if (!xw[i]) |
204 | xw.erase_unordered (i); |
256 | xw.erase_unordered (i); |
… | |
… | |
213 | |
265 | |
214 | void rxvt_display::flush () |
266 | void rxvt_display::flush () |
215 | { |
267 | { |
216 | for (;;) |
268 | for (;;) |
217 | { |
269 | { |
218 | XFlush (display); |
|
|
219 | |
|
|
220 | if (!XPending (display)) |
270 | if (!XPending (display)) |
221 | break; |
271 | break; |
222 | |
272 | |
223 | x_cb (x_ev, 0); |
273 | x_cb (x_ev, 0); |
224 | } |
274 | } |
… | |
… | |
234 | { |
284 | { |
235 | if (w->active) |
285 | if (w->active) |
236 | xw[w->active - 1] = 0; |
286 | xw[w->active - 1] = 0; |
237 | } |
287 | } |
238 | |
288 | |
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) |
289 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
250 | { |
290 | { |
251 | if (selection_owner && selection_owner != owner) |
291 | if (selection_owner && selection_owner != owner) |
252 | selection_owner->selection_clear (); |
292 | selection_owner->selection_clear (); |
253 | |
293 | |
254 | selection_owner = owner; |
294 | selection_owner = owner; |
255 | } |
295 | } |
256 | |
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 | |
257 | 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) |
258 | { |
309 | { |
259 | // asprintf is a GNU and *BSD extension.. sorry... |
|
|
260 | char *id; |
310 | char *id; |
|
|
311 | int l, m; |
261 | |
312 | |
262 | 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))) |
263 | return 0; |
317 | return 0; |
264 | |
318 | |
|
|
319 | memcpy (id, locale, l); id[l] = '\n'; |
|
|
320 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
|
|
321 | |
265 | rxvt_xim *xim = xims.get (id); |
322 | rxvt_xim *xim = xims.get (id); |
266 | |
323 | |
267 | free (id); |
324 | free (id); |
268 | |
325 | |
269 | return xim; |
326 | return xim; |
… | |
… | |
271 | |
328 | |
272 | void rxvt_display::put_xim (rxvt_xim *xim) |
329 | void rxvt_display::put_xim (rxvt_xim *xim) |
273 | { |
330 | { |
274 | xims.put (xim); |
331 | xims.put (xim); |
275 | } |
332 | } |
|
|
333 | #endif |
276 | |
334 | |
277 | Atom rxvt_display::atom (const char *name) |
335 | Atom rxvt_display::atom (const char *name) |
278 | { |
336 | { |
279 | return XInternAtom (display, name, False); |
337 | return XInternAtom (display, name, False); |
280 | } |
338 | } |
281 | |
339 | |
282 | ///////////////////////////////////////////////////////////////////////////// |
340 | ///////////////////////////////////////////////////////////////////////////// |
283 | |
341 | |
284 | template refcache<rxvt_display>; |
342 | template class refcache<rxvt_display>; |
285 | refcache<rxvt_display> displays; |
343 | refcache<rxvt_display> displays; |
286 | |
344 | |
287 | ///////////////////////////////////////////////////////////////////////////// |
345 | ///////////////////////////////////////////////////////////////////////////// |
288 | |
346 | |
289 | bool |
347 | bool |
… | |
… | |
369 | #else |
427 | #else |
370 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
428 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
371 | #endif |
429 | #endif |
372 | } |
430 | } |
373 | |
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 | |