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