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