1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: rxvtcolor.C |
2 | * File: rxvttoolkit.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
6 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
7 | * |
7 | * |
… | |
… | |
82 | delete obj; |
82 | delete obj; |
83 | } |
83 | } |
84 | } |
84 | } |
85 | |
85 | |
86 | template<class T> |
86 | template<class T> |
87 | refcache<T>::~refcache () |
87 | void refcache<T>::clear () |
88 | { |
88 | { |
89 | while (this->size ()) |
89 | while (this->size ()) |
90 | put (*this->begin ()); |
90 | put (*this->begin ()); |
91 | } |
91 | } |
92 | |
92 | |
… | |
… | |
101 | #endif |
101 | #endif |
102 | { |
102 | { |
103 | rxvt_xim *xim = (rxvt_xim *)client_data; |
103 | rxvt_xim *xim = (rxvt_xim *)client_data; |
104 | rxvt_display *display = xim->display; |
104 | rxvt_display *display = xim->display; |
105 | |
105 | |
|
|
106 | xim->xim = 0; |
|
|
107 | |
106 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
108 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
107 | |
|
|
108 | display->im_change_cb (); |
109 | display->im_change_cb (); |
109 | } |
110 | } |
110 | |
111 | |
111 | bool rxvt_xim::init () |
112 | bool rxvt_xim::init () |
112 | { |
113 | { |
… | |
… | |
142 | { |
143 | { |
143 | } |
144 | } |
144 | |
145 | |
145 | bool rxvt_display::init () |
146 | bool rxvt_display::init () |
146 | { |
147 | { |
|
|
148 | #ifdef LOCAL_X_IS_UNIX |
|
|
149 | if (id[0] == ':') |
|
|
150 | { |
|
|
151 | val = rxvt_malloc (5 + strlen (id) + 1); |
|
|
152 | strcpy (val, "unix/"); |
|
|
153 | strcat (val, id); |
|
|
154 | display = XOpenDisplay (val); |
|
|
155 | printf ("OD %s => %p\n", val, display);//D |
|
|
156 | free (val); |
|
|
157 | } |
|
|
158 | else |
|
|
159 | #endif |
|
|
160 | display = 0; |
|
|
161 | |
|
|
162 | if (!display) |
147 | display = XOpenDisplay (id); |
163 | display = XOpenDisplay (id); |
|
|
164 | printf ("O2 %s => %p\n", id, display);//D |
148 | |
165 | |
149 | if (!display) |
166 | if (!display) |
150 | return false; |
167 | return false; |
151 | |
168 | |
152 | screen = DefaultScreen (display); |
169 | screen = DefaultScreen (display); |
… | |
… | |
165 | sockaddr_un sa; |
182 | sockaddr_un sa; |
166 | socklen_t sl = sizeof (sa); |
183 | socklen_t sl = sizeof (sa); |
167 | |
184 | |
168 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
185 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
169 | is_local = sa.sun_family == AF_LOCAL; |
186 | is_local = sa.sun_family == AF_LOCAL; |
|
|
187 | #endif |
|
|
188 | |
|
|
189 | #ifdef POINTER_BLANK |
|
|
190 | XColor blackcolour; |
|
|
191 | blackcolour.red = 0; |
|
|
192 | blackcolour.green = 0; |
|
|
193 | blackcolour.blue = 0; |
|
|
194 | Font f = XLoadFont (display, "fixed"); |
|
|
195 | blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', |
|
|
196 | &blackcolour, &blackcolour); |
|
|
197 | XUnloadFont (display, f); |
170 | #endif |
198 | #endif |
171 | |
199 | |
172 | #ifdef PREFER_24BIT |
200 | #ifdef PREFER_24BIT |
173 | /* |
201 | /* |
174 | * If depth is not 24, look for a 24bit visual. |
202 | * If depth is not 24, look for a 24bit visual. |
… | |
… | |
202 | } |
230 | } |
203 | |
231 | |
204 | rxvt_display::~rxvt_display () |
232 | rxvt_display::~rxvt_display () |
205 | { |
233 | { |
206 | x_ev.stop (); |
234 | x_ev.stop (); |
|
|
235 | #ifdef USE_XIM |
|
|
236 | xims.clear (); |
|
|
237 | #endif |
207 | |
238 | |
208 | if (display) |
239 | if (display) |
209 | XCloseDisplay (display); |
240 | XCloseDisplay (display); |
210 | } |
241 | } |
211 | |
242 | |
… | |
… | |
213 | void rxvt_display::im_change_cb () |
244 | void rxvt_display::im_change_cb () |
214 | { |
245 | { |
215 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
246 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
216 | (*i)->call (); |
247 | (*i)->call (); |
217 | } |
248 | } |
|
|
249 | |
|
|
250 | void rxvt_display::im_change_check () |
|
|
251 | { |
|
|
252 | // try to only call im_change_cb when a new input method |
|
|
253 | // registers, as xlib crashes due to a race otherwise. |
|
|
254 | Atom actual_type, *atoms; |
|
|
255 | int actual_format; |
|
|
256 | unsigned long nitems, bytes_after; |
|
|
257 | |
|
|
258 | if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, |
|
|
259 | False, XA_ATOM, &actual_type, &actual_format, |
|
|
260 | &nitems, &bytes_after, (unsigned char **)&atoms) |
|
|
261 | != Success ) |
|
|
262 | return; |
|
|
263 | |
|
|
264 | if (actual_type == XA_ATOM && actual_format == 32) |
|
|
265 | for (int i = 0; i < nitems; i++) |
|
|
266 | if (XGetSelectionOwner (display, atoms[i])) |
|
|
267 | { |
|
|
268 | im_change_cb (); |
|
|
269 | break; |
|
|
270 | } |
|
|
271 | |
|
|
272 | XFree (atoms); |
|
|
273 | } |
218 | #endif |
274 | #endif |
219 | |
275 | |
220 | void rxvt_display::x_cb (io_watcher &w, short revents) |
276 | void rxvt_display::x_cb (io_watcher &w, short revents) |
221 | { |
277 | { |
222 | do |
278 | do |
223 | { |
279 | { |
224 | XEvent xev; |
280 | XEvent xev; |
225 | XNextEvent (display, &xev); |
281 | XNextEvent (display, &xev); |
226 | |
282 | |
227 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
|
|
228 | |
|
|
229 | #ifdef USE_XIM |
283 | #ifdef USE_XIM |
230 | if (xev.type == PropertyNotify |
284 | if (!XFilterEvent (&xev, None)) |
231 | && xev.xany.window == root |
|
|
232 | && xev.xproperty.atom == xa_xim_servers) |
|
|
233 | im_change_cb (); |
|
|
234 | #endif |
|
|
235 | |
|
|
236 | for (int i = xw.size (); i--; ) |
|
|
237 | { |
285 | { |
|
|
286 | |
|
|
287 | if (xev.type == PropertyNotify |
|
|
288 | && xev.xany.window == root |
|
|
289 | && xev.xproperty.atom == xa_xim_servers) |
|
|
290 | im_change_check (); |
|
|
291 | #endif |
|
|
292 | for (int i = xw.size (); i--; ) |
|
|
293 | { |
238 | if (!xw[i]) |
294 | if (!xw[i]) |
239 | xw.erase_unordered (i); |
295 | xw.erase_unordered (i); |
240 | else if (xw[i]->window == xev.xany.window) |
296 | else if (xw[i]->window == xev.xany.window) |
241 | xw[i]->call (xev); |
297 | xw[i]->call (xev); |
|
|
298 | } |
|
|
299 | #ifdef USE_XIM |
242 | } |
300 | } |
|
|
301 | #endif |
243 | } |
302 | } |
244 | while (XPending (display)); |
303 | while (XEventsQueued (display, QueuedAlready)); |
245 | |
304 | |
246 | flush (); |
305 | XFlush (display); |
247 | } |
306 | } |
248 | |
307 | |
249 | void rxvt_display::flush () |
308 | void rxvt_display::flush () |
250 | { |
309 | { |
251 | for (;;) |
310 | if (XEventsQueued (display, QueuedAlready)) |
252 | { |
311 | x_cb (x_ev, EVENT_READ); |
253 | if (!XPending (display)) |
|
|
254 | break; |
|
|
255 | |
312 | |
256 | x_cb (x_ev, 0); |
313 | XFlush (display); |
257 | } |
|
|
258 | } |
314 | } |
259 | |
315 | |
260 | void rxvt_display::reg (xevent_watcher *w) |
316 | void rxvt_display::reg (xevent_watcher *w) |
261 | { |
317 | { |
262 | xw.push_back (w); |
318 | xw.push_back (w); |
… | |
… | |
309 | return xim; |
365 | return xim; |
310 | } |
366 | } |
311 | |
367 | |
312 | void rxvt_display::put_xim (rxvt_xim *xim) |
368 | void rxvt_display::put_xim (rxvt_xim *xim) |
313 | { |
369 | { |
|
|
370 | #if XLIB_IS_RACEFREE |
314 | xims.put (xim); |
371 | xims.put (xim); |
|
|
372 | #endif |
315 | } |
373 | } |
316 | #endif |
374 | #endif |
317 | |
375 | |
318 | Atom rxvt_display::atom (const char *name) |
376 | Atom rxvt_display::atom (const char *name) |
319 | { |
377 | { |
… | |
… | |
357 | } |
415 | } |
358 | |
416 | |
359 | bool |
417 | bool |
360 | rxvt_color::set (rxvt_display *display, const char *name) |
418 | rxvt_color::set (rxvt_display *display, const char *name) |
361 | { |
419 | { |
|
|
420 | #if XFT |
|
|
421 | return XftColorAllocName (display->display, display->visual, display->cmap, |
|
|
422 | name, &c); |
|
|
423 | #else |
362 | XColor xc; |
424 | XColor xc; |
363 | |
425 | |
364 | if (XParseColor (display->display, display->cmap, name, &xc)) |
426 | if (XParseColor (display->display, display->cmap, name, &xc)) |
365 | return set (display, xc.red, xc.green, xc.blue); |
427 | return set (display, xc.red, xc.green, xc.blue); |
366 | |
428 | |
367 | return false; |
429 | return false; |
|
|
430 | #endif |
368 | } |
431 | } |
369 | |
432 | |
370 | bool |
433 | bool |
371 | rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
434 | rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
372 | { |
435 | { |