… | |
… | |
25 | #include <rxvttoolkit.h> |
25 | #include <rxvttoolkit.h> |
26 | |
26 | |
27 | #include <unistd.h> |
27 | #include <unistd.h> |
28 | #include <fcntl.h> |
28 | #include <fcntl.h> |
29 | |
29 | |
|
|
30 | #include <sys/utsname.h> |
|
|
31 | |
30 | #ifndef NO_SLOW_LINK_SUPPORT |
32 | #ifndef NO_SLOW_LINK_SUPPORT |
31 | # include <sys/socket.h> |
33 | # include <sys/socket.h> |
32 | # include <sys/un.h> |
34 | # include <sys/un.h> |
33 | #endif |
35 | #endif |
34 | |
36 | |
… | |
… | |
47 | { |
49 | { |
48 | for (T **i = this->begin (); i < this->end (); ++i) |
50 | for (T **i = this->begin (); i < this->end (); ++i) |
49 | { |
51 | { |
50 | if (!strcmp (id, (*i)->id)) |
52 | if (!strcmp (id, (*i)->id)) |
51 | { |
53 | { |
52 | (*i)->referenced++; |
54 | ++(*i)->referenced; |
|
|
55 | (*i)->ref_next (); |
53 | return *i; |
56 | return *i; |
54 | } |
57 | } |
55 | } |
58 | } |
56 | |
59 | |
57 | T *obj = new T (id); |
60 | T *obj = new T (id); |
58 | |
61 | |
59 | obj->referenced = 1; |
|
|
60 | |
|
|
61 | if (obj && obj->init ()) |
62 | if (obj && obj->ref_init ()) |
62 | { |
63 | { |
|
|
64 | obj->referenced = 1; |
63 | this->push_back (obj); |
65 | this->push_back (obj); |
64 | return obj; |
66 | return obj; |
65 | } |
67 | } |
66 | else |
68 | else |
67 | { |
69 | { |
… | |
… | |
82 | delete obj; |
84 | delete obj; |
83 | } |
85 | } |
84 | } |
86 | } |
85 | |
87 | |
86 | template<class T> |
88 | template<class T> |
87 | refcache<T>::~refcache () |
89 | void refcache<T>::clear () |
88 | { |
90 | { |
89 | while (this->size ()) |
91 | while (this->size ()) |
90 | put (*this->begin ()); |
92 | put (*this->begin ()); |
91 | } |
93 | } |
92 | |
94 | |
… | |
… | |
107 | |
109 | |
108 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
110 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
109 | display->im_change_cb (); |
111 | display->im_change_cb (); |
110 | } |
112 | } |
111 | |
113 | |
|
|
114 | bool |
112 | bool rxvt_xim::init () |
115 | rxvt_xim::ref_init () |
113 | { |
116 | { |
114 | display = GET_R->display; //HACK: TODO |
117 | display = GET_R->display; //HACK: TODO |
115 | |
118 | |
116 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
119 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
117 | |
120 | |
… | |
… | |
141 | , x_ev (this, &rxvt_display::x_cb) |
144 | , x_ev (this, &rxvt_display::x_cb) |
142 | , selection_owner (0) |
145 | , selection_owner (0) |
143 | { |
146 | { |
144 | } |
147 | } |
145 | |
148 | |
|
|
149 | XrmDatabase |
|
|
150 | rxvt_display::get_resources () |
|
|
151 | { |
|
|
152 | char *homedir = (char *)getenv ("HOME"); |
|
|
153 | char fname[1024]; |
|
|
154 | |
|
|
155 | /* |
|
|
156 | * get resources using the X library function |
|
|
157 | */ |
|
|
158 | char *displayResource, *xe; |
|
|
159 | XrmDatabase database, rdb1; |
|
|
160 | |
|
|
161 | database = NULL; |
|
|
162 | |
|
|
163 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
|
|
164 | |
|
|
165 | // 6. System wide per application default file. |
|
|
166 | |
|
|
167 | /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ |
|
|
168 | if ((xe = (char *)getenv ("XAPPLRESDIR"))) |
|
|
169 | { |
|
|
170 | snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); |
|
|
171 | |
|
|
172 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
173 | XrmMergeDatabases (rdb1, &database); |
|
|
174 | } |
|
|
175 | |
|
|
176 | // 5. User's per application default file. |
|
|
177 | // none |
|
|
178 | |
|
|
179 | // 4. User's defaults file. |
|
|
180 | /* Get any Xserver defaults */ |
|
|
181 | displayResource = XResourceManagerString (display); |
|
|
182 | |
|
|
183 | if (displayResource != NULL) |
|
|
184 | { |
|
|
185 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
|
|
186 | XrmMergeDatabases (rdb1, &database); |
|
|
187 | } |
|
|
188 | else if (homedir) |
|
|
189 | { |
|
|
190 | snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); |
|
|
191 | |
|
|
192 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
193 | XrmMergeDatabases (rdb1, &database); |
|
|
194 | } |
|
|
195 | |
|
|
196 | /* Get screen specific resources */ |
|
|
197 | displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); |
|
|
198 | |
|
|
199 | if (displayResource != NULL) |
|
|
200 | { |
|
|
201 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
|
|
202 | /* Merge with screen-independent resources */ |
|
|
203 | XrmMergeDatabases (rdb1, &database); |
|
|
204 | |
|
|
205 | XFree (displayResource); |
|
|
206 | } |
|
|
207 | |
|
|
208 | // 3. User's per host defaults file |
|
|
209 | /* Add in XENVIRONMENT file */ |
|
|
210 | if ((xe = (char *)getenv ("XENVIRONMENT")) |
|
|
211 | && (rdb1 = XrmGetFileDatabase (xe))) |
|
|
212 | XrmMergeDatabases (rdb1, &database); |
|
|
213 | else if (homedir) |
|
|
214 | { |
|
|
215 | struct utsname un; |
|
|
216 | |
|
|
217 | if (!uname (&un)) |
|
|
218 | { |
|
|
219 | snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename); |
|
|
220 | |
|
|
221 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
222 | XrmMergeDatabases (rdb1, &database); |
|
|
223 | } |
|
|
224 | } |
|
|
225 | |
|
|
226 | return database; |
|
|
227 | } |
|
|
228 | |
146 | bool rxvt_display::init () |
229 | bool rxvt_display::ref_init () |
147 | { |
230 | { |
|
|
231 | #ifdef LOCAL_X_IS_UNIX |
|
|
232 | if (id[0] == ':') |
|
|
233 | { |
|
|
234 | val = rxvt_malloc (5 + strlen (id) + 1); |
|
|
235 | strcpy (val, "unix/"); |
|
|
236 | strcat (val, id); |
|
|
237 | display = XOpenDisplay (val); |
|
|
238 | free (val); |
|
|
239 | } |
|
|
240 | else |
|
|
241 | #endif |
|
|
242 | display = 0; |
|
|
243 | |
|
|
244 | if (!display) |
148 | display = XOpenDisplay (id); |
245 | display = XOpenDisplay (id); |
149 | |
246 | |
150 | if (!display) |
247 | if (!display) |
151 | return false; |
248 | return false; |
152 | |
249 | |
153 | screen = DefaultScreen (display); |
250 | screen = DefaultScreen (display); |
154 | root = DefaultRootWindow (display); |
251 | root = DefaultRootWindow (display); |
155 | visual = DefaultVisual (display, screen); |
252 | visual = DefaultVisual (display, screen); |
156 | cmap = DefaultColormap (display, screen); |
253 | cmap = DefaultColormap (display, screen); |
157 | depth = DefaultDepth (display, screen); |
254 | depth = DefaultDepth (display, screen); |
158 | |
255 | |
159 | int fd = XConnectionNumber (display); |
256 | XrmSetDatabase (display, get_resources ()); |
160 | |
257 | |
161 | #ifndef NO_SLOW_LINK_SUPPORT |
258 | #ifdef POINTER_BLANK |
162 | // try to detetc wether we have a local connection. |
259 | XColor blackcolour; |
163 | // assume unix domains socket == local, everything else not |
260 | blackcolour.red = 0; |
164 | // TODO: might want to check for inet/127.0.0.1 |
261 | blackcolour.green = 0; |
165 | is_local = 0; |
262 | blackcolour.blue = 0; |
166 | sockaddr_un sa; |
263 | Font f = XLoadFont (display, "fixed"); |
167 | socklen_t sl = sizeof (sa); |
264 | blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', |
168 | |
265 | &blackcolour, &blackcolour); |
169 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
266 | XUnloadFont (display, f); |
170 | is_local = sa.sun_family == AF_LOCAL; |
|
|
171 | #endif |
267 | #endif |
172 | |
268 | |
173 | #ifdef PREFER_24BIT |
269 | #ifdef PREFER_24BIT |
174 | /* |
270 | /* |
175 | * If depth is not 24, look for a 24bit visual. |
271 | * If depth is not 24, look for a 24bit visual. |
… | |
… | |
187 | visual, AllocNone); |
283 | visual, AllocNone); |
188 | } |
284 | } |
189 | } |
285 | } |
190 | #endif |
286 | #endif |
191 | |
287 | |
|
|
288 | int fd = XConnectionNumber (display); |
|
|
289 | |
|
|
290 | #ifndef NO_SLOW_LINK_SUPPORT |
|
|
291 | // try to detect wether we have a local connection. |
|
|
292 | // assume unix domains socket == local, everything else not |
|
|
293 | // TODO: might want to check for inet/127.0.0.1 |
|
|
294 | is_local = 0; |
|
|
295 | sockaddr_un sa; |
|
|
296 | socklen_t sl = sizeof (sa); |
|
|
297 | |
|
|
298 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
|
|
299 | is_local = sa.sun_family == AF_LOCAL; |
|
|
300 | #endif |
|
|
301 | |
192 | x_ev.start (fd, EVENT_READ); |
302 | x_ev.start (fd, EVENT_READ); |
193 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
303 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
194 | |
304 | |
195 | XSelectInput (display, root, PropertyChangeMask); |
305 | XSelectInput (display, root, PropertyChangeMask); |
196 | #ifdef USE_XIM |
306 | #ifdef USE_XIM |
… | |
… | |
200 | flush (); |
310 | flush (); |
201 | |
311 | |
202 | return true; |
312 | return true; |
203 | } |
313 | } |
204 | |
314 | |
|
|
315 | void |
|
|
316 | rxvt_display::ref_next () |
|
|
317 | { |
|
|
318 | // TODO: somehow check wether the database files/resources changed |
|
|
319 | // before re-loading/parsing |
|
|
320 | XrmDestroyDatabase (XrmGetDatabase (display)); |
|
|
321 | XrmSetDatabase (display, get_resources ()); |
|
|
322 | } |
|
|
323 | |
205 | rxvt_display::~rxvt_display () |
324 | rxvt_display::~rxvt_display () |
206 | { |
325 | { |
|
|
326 | if (!display) |
|
|
327 | return; |
|
|
328 | |
|
|
329 | #ifdef POINTER_BLANK |
|
|
330 | XFreeCursor (display, blank_cursor); |
|
|
331 | #endif |
207 | x_ev.stop (); |
332 | x_ev.stop (); |
208 | |
333 | #ifdef USE_XIM |
209 | if (display) |
334 | xims.clear (); |
|
|
335 | #endif |
210 | XCloseDisplay (display); |
336 | XCloseDisplay (display); |
211 | } |
337 | } |
212 | |
338 | |
213 | #ifdef USE_XIM |
339 | #ifdef USE_XIM |
214 | void rxvt_display::im_change_cb () |
340 | void rxvt_display::im_change_cb () |
215 | { |
341 | { |
… | |
… | |
217 | (*i)->call (); |
343 | (*i)->call (); |
218 | } |
344 | } |
219 | |
345 | |
220 | void rxvt_display::im_change_check () |
346 | void rxvt_display::im_change_check () |
221 | { |
347 | { |
222 | // make sure we only call im_change_cb when a new input method |
348 | // try to only call im_change_cb when a new input method |
223 | // registers, as xlib crashes due to a race otherwise. |
349 | // registers, as xlib crashes due to a race otherwise. |
224 | Atom actual_type, *atoms; |
350 | Atom actual_type, *atoms; |
225 | int actual_format; |
351 | int actual_format; |
226 | unsigned long nitems, bytes_after; |
352 | unsigned long nitems, bytes_after; |
227 | |
353 | |
… | |
… | |
251 | XNextEvent (display, &xev); |
377 | XNextEvent (display, &xev); |
252 | |
378 | |
253 | #ifdef USE_XIM |
379 | #ifdef USE_XIM |
254 | if (!XFilterEvent (&xev, None)) |
380 | if (!XFilterEvent (&xev, None)) |
255 | { |
381 | { |
256 | |
|
|
257 | if (xev.type == PropertyNotify |
382 | if (xev.type == PropertyNotify |
258 | && xev.xany.window == root |
383 | && xev.xany.window == root |
259 | && xev.xproperty.atom == xa_xim_servers) |
384 | && xev.xproperty.atom == xa_xim_servers) |
260 | im_change_check (); |
385 | im_change_check (); |
261 | #endif |
386 | #endif |
… | |
… | |
270 | } |
395 | } |
271 | #endif |
396 | #endif |
272 | } |
397 | } |
273 | while (XEventsQueued (display, QueuedAlready)); |
398 | while (XEventsQueued (display, QueuedAlready)); |
274 | |
399 | |
275 | flush (); |
400 | XFlush (display); |
276 | } |
401 | } |
277 | |
402 | |
278 | void rxvt_display::flush () |
403 | void rxvt_display::flush () |
279 | { |
404 | { |
|
|
405 | if (XEventsQueued (display, QueuedAlready)) |
|
|
406 | x_cb (x_ev, EVENT_READ); |
|
|
407 | |
280 | XFlush (display); |
408 | XFlush (display); |
281 | } |
409 | } |
282 | |
410 | |
283 | void rxvt_display::reg (xevent_watcher *w) |
411 | void rxvt_display::reg (xevent_watcher *w) |
284 | { |
412 | { |
… | |
… | |
332 | return xim; |
460 | return xim; |
333 | } |
461 | } |
334 | |
462 | |
335 | void rxvt_display::put_xim (rxvt_xim *xim) |
463 | void rxvt_display::put_xim (rxvt_xim *xim) |
336 | { |
464 | { |
|
|
465 | #if XLIB_IS_RACEFREE |
337 | xims.put (xim); |
466 | xims.put (xim); |
|
|
467 | #endif |
338 | } |
468 | } |
339 | #endif |
469 | #endif |
340 | |
470 | |
341 | Atom rxvt_display::atom (const char *name) |
471 | Atom rxvt_display::atom (const char *name) |
342 | { |
472 | { |
… | |
… | |
441 | } |
571 | } |
442 | |
572 | |
443 | rxvt_color |
573 | rxvt_color |
444 | rxvt_color::fade (rxvt_display *display, int percent) |
574 | rxvt_color::fade (rxvt_display *display, int percent) |
445 | { |
575 | { |
|
|
576 | percent = 100 - percent; |
|
|
577 | |
446 | unsigned short cr, cg, cb; |
578 | unsigned short cr, cg, cb; |
447 | rxvt_color faded; |
579 | rxvt_color faded; |
448 | |
580 | |
449 | get (display, cr, cg, cb); |
581 | get (display, cr, cg, cb); |
|
|
582 | |
450 | faded.set (display, |
583 | faded.set ( |
|
|
584 | display, |
451 | cr * percent / 100, |
585 | cr * percent / 100, |
452 | cg * percent / 100, |
586 | cg * percent / 100, |
453 | cb * percent / 100); |
587 | cb * percent / 100 |
|
|
588 | ); |
454 | |
589 | |
455 | return faded; |
590 | return faded; |
456 | } |
591 | } |
457 | |
592 | |
|
|
593 | #define LERP(a,b,p) (a * p + b * (100 - p)) / 100 |
|
|
594 | |
|
|
595 | rxvt_color |
|
|
596 | rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto) |
|
|
597 | { |
|
|
598 | percent = 100 - percent; |
|
|
599 | |
|
|
600 | unsigned short cr, cg, cb; |
|
|
601 | unsigned short fcr, fcg, fcb; |
|
|
602 | rxvt_color faded; |
|
|
603 | |
|
|
604 | get (display, cr, cg, cb); |
|
|
605 | fadeto.get(display, fcr, fcg, fcb); |
|
|
606 | |
|
|
607 | faded.set ( |
|
|
608 | display, |
|
|
609 | LERP (cr, fcr, percent), |
|
|
610 | LERP (cg, fcg, percent), |
|
|
611 | LERP (cb, fcb, percent) |
|
|
612 | ); |
|
|
613 | |
|
|
614 | return faded; |
|
|
615 | } |
|
|
616 | |