… | |
… | |
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 | |
|
|
37 | const char *const xa_names[] = |
|
|
38 | { |
|
|
39 | "TEXT", |
|
|
40 | "COMPOUND_TEXT", |
|
|
41 | "UTF8_STRING", |
|
|
42 | "MULTIPLE", |
|
|
43 | "TARGETS", |
|
|
44 | "TIMESTAMP", |
|
|
45 | "VT_SELECTION", |
|
|
46 | "INCR", |
|
|
47 | "WM_PROTOCOLS", |
|
|
48 | "WM_DELETE_WINDOW", |
|
|
49 | "CLIPBOARD", |
|
|
50 | #if ENABLE_FRILLS |
|
|
51 | "_MOTIF_WM_HINTS", |
|
|
52 | #endif |
|
|
53 | #if ENABLE_EWMH |
|
|
54 | "_NET_WM_PID", |
|
|
55 | "_NET_WM_NAME", |
|
|
56 | "_NET_WM_ICON_NAME", |
|
|
57 | "_NET_WM_PING", |
|
|
58 | #endif |
|
|
59 | #if USE_XIM |
|
|
60 | "WM_LOCALE_NAME", |
|
|
61 | "XIM_SERVERS", |
|
|
62 | #endif |
|
|
63 | #ifdef TRANSPARENT |
|
|
64 | "_XROOTPMAP_ID", |
|
|
65 | "ESETROOT_PMAP_ID", |
|
|
66 | #endif |
|
|
67 | #if ENABLE_XEMBED |
|
|
68 | "_XEMBED", |
|
|
69 | "_XEMBED_INFO", |
|
|
70 | #endif |
|
|
71 | }; |
|
|
72 | |
|
|
73 | ///////////////////////////////////////////////////////////////////////////// |
|
|
74 | |
35 | refcounted::refcounted (const char *id) |
75 | refcounted::refcounted (const char *id) |
36 | { |
76 | { |
37 | this->id = strdup (id); |
77 | this->id = strdup (id); |
38 | } |
78 | } |
39 | |
79 | |
… | |
… | |
47 | { |
87 | { |
48 | for (T **i = this->begin (); i < this->end (); ++i) |
88 | for (T **i = this->begin (); i < this->end (); ++i) |
49 | { |
89 | { |
50 | if (!strcmp (id, (*i)->id)) |
90 | if (!strcmp (id, (*i)->id)) |
51 | { |
91 | { |
52 | (*i)->referenced++; |
92 | ++(*i)->referenced; |
|
|
93 | (*i)->ref_next (); |
53 | return *i; |
94 | return *i; |
54 | } |
95 | } |
55 | } |
96 | } |
56 | |
97 | |
57 | T *obj = new T (id); |
98 | T *obj = new T (id); |
58 | |
99 | |
59 | obj->referenced = 1; |
|
|
60 | |
|
|
61 | if (obj && obj->init ()) |
100 | if (obj && obj->ref_init ()) |
62 | { |
101 | { |
|
|
102 | obj->referenced = 1; |
63 | this->push_back (obj); |
103 | this->push_back (obj); |
64 | return obj; |
104 | return obj; |
65 | } |
105 | } |
66 | else |
106 | else |
67 | { |
107 | { |
… | |
… | |
82 | delete obj; |
122 | delete obj; |
83 | } |
123 | } |
84 | } |
124 | } |
85 | |
125 | |
86 | template<class T> |
126 | template<class T> |
87 | refcache<T>::~refcache () |
127 | void refcache<T>::clear () |
88 | { |
128 | { |
89 | while (this->size ()) |
129 | while (this->size ()) |
90 | put (*this->begin ()); |
130 | put (*this->begin ()); |
91 | } |
131 | } |
92 | |
132 | |
93 | ///////////////////////////////////////////////////////////////////////////// |
133 | ///////////////////////////////////////////////////////////////////////////// |
94 | |
134 | |
95 | #ifdef USE_XIM |
135 | #ifdef USE_XIM |
|
|
136 | |
96 | static void |
137 | static void |
97 | #if XIMCB_PROTO_BROKEN |
138 | #if XIMCB_PROTO_BROKEN |
98 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
139 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
99 | #else |
140 | #else |
100 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
141 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
… | |
… | |
107 | |
148 | |
108 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
149 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
109 | display->im_change_cb (); |
150 | display->im_change_cb (); |
110 | } |
151 | } |
111 | |
152 | |
|
|
153 | bool |
112 | bool rxvt_xim::init () |
154 | rxvt_xim::ref_init () |
113 | { |
155 | { |
114 | display = GET_R->display; //HACK: TODO |
156 | display = GET_R->display; //HACK: TODO |
115 | |
157 | |
116 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
158 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
117 | |
159 | |
… | |
… | |
130 | rxvt_xim::~rxvt_xim () |
172 | rxvt_xim::~rxvt_xim () |
131 | { |
173 | { |
132 | if (xim) |
174 | if (xim) |
133 | XCloseIM (xim); |
175 | XCloseIM (xim); |
134 | } |
176 | } |
|
|
177 | |
135 | #endif |
178 | #endif |
136 | |
179 | |
137 | ///////////////////////////////////////////////////////////////////////////// |
180 | ///////////////////////////////////////////////////////////////////////////// |
138 | |
181 | |
139 | rxvt_display::rxvt_display (const char *id) |
182 | rxvt_display::rxvt_display (const char *id) |
… | |
… | |
141 | , x_ev (this, &rxvt_display::x_cb) |
184 | , x_ev (this, &rxvt_display::x_cb) |
142 | , selection_owner (0) |
185 | , selection_owner (0) |
143 | { |
186 | { |
144 | } |
187 | } |
145 | |
188 | |
|
|
189 | XrmDatabase |
|
|
190 | rxvt_display::get_resources () |
|
|
191 | { |
|
|
192 | char *homedir = (char *)getenv ("HOME"); |
|
|
193 | char fname[1024]; |
|
|
194 | |
|
|
195 | /* |
|
|
196 | * get resources using the X library function |
|
|
197 | */ |
|
|
198 | char *displayResource, *xe; |
|
|
199 | XrmDatabase database, rdb1; |
|
|
200 | |
|
|
201 | database = NULL; |
|
|
202 | |
|
|
203 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
|
|
204 | |
|
|
205 | // 6. System wide per application default file. |
|
|
206 | |
|
|
207 | /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ |
|
|
208 | if ((xe = (char *)getenv ("XAPPLRESDIR"))) |
|
|
209 | { |
|
|
210 | snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); |
|
|
211 | |
|
|
212 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
213 | XrmMergeDatabases (rdb1, &database); |
|
|
214 | } |
|
|
215 | |
|
|
216 | // 5. User's per application default file. |
|
|
217 | // none |
|
|
218 | |
|
|
219 | // 4. User's defaults file. |
|
|
220 | /* Get any Xserver defaults */ |
|
|
221 | displayResource = XResourceManagerString (display); |
|
|
222 | |
|
|
223 | if (displayResource != NULL) |
|
|
224 | { |
|
|
225 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
|
|
226 | XrmMergeDatabases (rdb1, &database); |
|
|
227 | } |
|
|
228 | else if (homedir) |
|
|
229 | { |
|
|
230 | snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); |
|
|
231 | |
|
|
232 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
233 | XrmMergeDatabases (rdb1, &database); |
|
|
234 | } |
|
|
235 | |
|
|
236 | /* Get screen specific resources */ |
|
|
237 | displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); |
|
|
238 | |
|
|
239 | if (displayResource != NULL) |
|
|
240 | { |
|
|
241 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
|
|
242 | /* Merge with screen-independent resources */ |
|
|
243 | XrmMergeDatabases (rdb1, &database); |
|
|
244 | |
|
|
245 | XFree (displayResource); |
|
|
246 | } |
|
|
247 | |
|
|
248 | // 3. User's per host defaults file |
|
|
249 | /* Add in XENVIRONMENT file */ |
|
|
250 | if ((xe = (char *)getenv ("XENVIRONMENT")) |
|
|
251 | && (rdb1 = XrmGetFileDatabase (xe))) |
|
|
252 | XrmMergeDatabases (rdb1, &database); |
|
|
253 | else if (homedir) |
|
|
254 | { |
|
|
255 | struct utsname un; |
|
|
256 | |
|
|
257 | if (!uname (&un)) |
|
|
258 | { |
|
|
259 | snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename); |
|
|
260 | |
|
|
261 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
262 | XrmMergeDatabases (rdb1, &database); |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
|
|
266 | return database; |
|
|
267 | } |
|
|
268 | |
146 | bool rxvt_display::init () |
269 | bool rxvt_display::ref_init () |
147 | { |
270 | { |
|
|
271 | #ifdef LOCAL_X_IS_UNIX |
|
|
272 | if (id[0] == ':') |
|
|
273 | { |
|
|
274 | val = rxvt_malloc (5 + strlen (id) + 1); |
|
|
275 | strcpy (val, "unix/"); |
|
|
276 | strcat (val, id); |
|
|
277 | display = XOpenDisplay (val); |
|
|
278 | free (val); |
|
|
279 | } |
|
|
280 | else |
|
|
281 | #endif |
|
|
282 | display = 0; |
|
|
283 | |
|
|
284 | if (!display) |
148 | display = XOpenDisplay (id); |
285 | display = XOpenDisplay (id); |
149 | |
286 | |
150 | if (!display) |
287 | if (!display) |
151 | return false; |
288 | return false; |
152 | |
289 | |
153 | screen = DefaultScreen (display); |
290 | screen = DefaultScreen (display); |
154 | root = DefaultRootWindow (display); |
291 | root = DefaultRootWindow (display); |
155 | visual = DefaultVisual (display, screen); |
292 | visual = DefaultVisual (display, screen); |
156 | cmap = DefaultColormap (display, screen); |
293 | cmap = DefaultColormap (display, screen); |
157 | depth = DefaultDepth (display, screen); |
294 | depth = DefaultDepth (display, screen); |
158 | |
295 | |
159 | int fd = XConnectionNumber (display); |
296 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
|
|
297 | XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); |
160 | |
298 | |
161 | #ifndef NO_SLOW_LINK_SUPPORT |
299 | XrmSetDatabase (display, get_resources ()); |
162 | // try to detetc wether we have a local connection. |
|
|
163 | // assume unix domains socket == local, everything else not |
|
|
164 | // TODO: might want to check for inet/127.0.0.1 |
|
|
165 | is_local = 0; |
|
|
166 | sockaddr_un sa; |
|
|
167 | socklen_t sl = sizeof (sa); |
|
|
168 | |
300 | |
169 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
301 | #ifdef POINTER_BLANK |
170 | is_local = sa.sun_family == AF_LOCAL; |
302 | XColor blackcolour; |
|
|
303 | blackcolour.red = 0; |
|
|
304 | blackcolour.green = 0; |
|
|
305 | blackcolour.blue = 0; |
|
|
306 | Font f = XLoadFont (display, "fixed"); |
|
|
307 | blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', |
|
|
308 | &blackcolour, &blackcolour); |
|
|
309 | XUnloadFont (display, f); |
171 | #endif |
310 | #endif |
172 | |
311 | |
173 | #ifdef PREFER_24BIT |
312 | #ifdef PREFER_24BIT |
174 | /* |
313 | /* |
175 | * If depth is not 24, look for a 24bit visual. |
314 | * If depth is not 24, look for a 24bit visual. |
… | |
… | |
187 | visual, AllocNone); |
326 | visual, AllocNone); |
188 | } |
327 | } |
189 | } |
328 | } |
190 | #endif |
329 | #endif |
191 | |
330 | |
|
|
331 | int fd = XConnectionNumber (display); |
|
|
332 | |
|
|
333 | #ifndef NO_SLOW_LINK_SUPPORT |
|
|
334 | // try to detect wether we have a local connection. |
|
|
335 | // assume unix domains socket == local, everything else not |
|
|
336 | // TODO: might want to check for inet/127.0.0.1 |
|
|
337 | is_local = 0; |
|
|
338 | sockaddr_un sa; |
|
|
339 | socklen_t sl = sizeof (sa); |
|
|
340 | |
|
|
341 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
|
|
342 | is_local = sa.sun_family == AF_LOCAL; |
|
|
343 | #endif |
|
|
344 | |
192 | x_ev.start (fd, EVENT_READ); |
345 | x_ev.start (fd, EVENT_READ); |
193 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
346 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
194 | |
347 | |
195 | XSelectInput (display, root, PropertyChangeMask); |
348 | XSelectInput (display, root, PropertyChangeMask); |
|
|
349 | |
|
|
350 | flush (); |
|
|
351 | |
|
|
352 | return true; |
|
|
353 | } |
|
|
354 | |
|
|
355 | void |
|
|
356 | rxvt_display::ref_next () |
|
|
357 | { |
|
|
358 | // TODO: somehow check wether the database files/resources changed |
|
|
359 | // before re-loading/parsing |
|
|
360 | XrmDestroyDatabase (XrmGetDatabase (display)); |
|
|
361 | XrmSetDatabase (display, get_resources ()); |
|
|
362 | } |
|
|
363 | |
|
|
364 | rxvt_display::~rxvt_display () |
|
|
365 | { |
|
|
366 | if (!display) |
|
|
367 | return; |
|
|
368 | |
|
|
369 | #ifdef POINTER_BLANK |
|
|
370 | XFreeCursor (display, blank_cursor); |
|
|
371 | #endif |
|
|
372 | x_ev.stop (); |
196 | #ifdef USE_XIM |
373 | #ifdef USE_XIM |
197 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
374 | xims.clear (); |
198 | #endif |
375 | #endif |
199 | |
|
|
200 | flush (); |
|
|
201 | |
|
|
202 | return true; |
|
|
203 | } |
|
|
204 | |
|
|
205 | rxvt_display::~rxvt_display () |
|
|
206 | { |
|
|
207 | x_ev.stop (); |
|
|
208 | |
|
|
209 | if (display) |
|
|
210 | XCloseDisplay (display); |
376 | XCloseDisplay (display); |
211 | } |
377 | } |
212 | |
378 | |
213 | #ifdef USE_XIM |
379 | #ifdef USE_XIM |
214 | void rxvt_display::im_change_cb () |
380 | void rxvt_display::im_change_cb () |
215 | { |
381 | { |
… | |
… | |
217 | (*i)->call (); |
383 | (*i)->call (); |
218 | } |
384 | } |
219 | |
385 | |
220 | void rxvt_display::im_change_check () |
386 | void rxvt_display::im_change_check () |
221 | { |
387 | { |
222 | // make sure we only call im_change_cb when a new input method |
388 | // try to only call im_change_cb when a new input method |
223 | // registers, as xlib crashes due to a race otherwise. |
389 | // registers, as xlib crashes due to a race otherwise. |
224 | Atom actual_type, *atoms; |
390 | Atom actual_type, *atoms; |
225 | int actual_format; |
391 | int actual_format; |
226 | unsigned long nitems, bytes_after; |
392 | unsigned long nitems, bytes_after; |
227 | |
393 | |
228 | if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, |
394 | if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
229 | False, XA_ATOM, &actual_type, &actual_format, |
395 | False, XA_ATOM, &actual_type, &actual_format, |
230 | &nitems, &bytes_after, (unsigned char **)&atoms) |
396 | &nitems, &bytes_after, (unsigned char **)&atoms) |
231 | != Success ) |
397 | != Success ) |
232 | return; |
398 | return; |
233 | |
399 | |
… | |
… | |
251 | XNextEvent (display, &xev); |
417 | XNextEvent (display, &xev); |
252 | |
418 | |
253 | #ifdef USE_XIM |
419 | #ifdef USE_XIM |
254 | if (!XFilterEvent (&xev, None)) |
420 | if (!XFilterEvent (&xev, None)) |
255 | { |
421 | { |
256 | |
|
|
257 | if (xev.type == PropertyNotify |
422 | if (xev.type == PropertyNotify |
258 | && xev.xany.window == root |
423 | && xev.xany.window == root |
259 | && xev.xproperty.atom == xa_xim_servers) |
424 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
260 | im_change_check (); |
425 | im_change_check (); |
261 | #endif |
426 | #endif |
262 | for (int i = xw.size (); i--; ) |
427 | for (int i = xw.size (); i--; ) |
263 | { |
428 | { |
264 | if (!xw[i]) |
429 | if (!xw[i]) |
… | |
… | |
270 | } |
435 | } |
271 | #endif |
436 | #endif |
272 | } |
437 | } |
273 | while (XEventsQueued (display, QueuedAlready)); |
438 | while (XEventsQueued (display, QueuedAlready)); |
274 | |
439 | |
275 | flush (); |
440 | XFlush (display); |
276 | } |
441 | } |
277 | |
442 | |
278 | void rxvt_display::flush () |
443 | void rxvt_display::flush () |
279 | { |
444 | { |
280 | for (;;) |
445 | if (XEventsQueued (display, QueuedAlready)) |
281 | { |
446 | x_cb (x_ev, EVENT_READ); |
282 | if (!XPending (display)) |
|
|
283 | break; |
|
|
284 | |
447 | |
285 | x_cb (x_ev, 0); |
448 | XFlush (display); |
286 | } |
|
|
287 | } |
449 | } |
288 | |
450 | |
289 | void rxvt_display::reg (xevent_watcher *w) |
451 | void rxvt_display::reg (xevent_watcher *w) |
290 | { |
452 | { |
291 | xw.push_back (w); |
453 | xw.push_back (w); |
… | |
… | |
338 | return xim; |
500 | return xim; |
339 | } |
501 | } |
340 | |
502 | |
341 | void rxvt_display::put_xim (rxvt_xim *xim) |
503 | void rxvt_display::put_xim (rxvt_xim *xim) |
342 | { |
504 | { |
|
|
505 | #if XLIB_IS_RACEFREE |
343 | xims.put (xim); |
506 | xims.put (xim); |
|
|
507 | #endif |
344 | } |
508 | } |
345 | #endif |
509 | #endif |
346 | |
510 | |
347 | Atom rxvt_display::atom (const char *name) |
511 | Atom rxvt_display::atom (const char *name) |
348 | { |
512 | { |
… | |
… | |
447 | } |
611 | } |
448 | |
612 | |
449 | rxvt_color |
613 | rxvt_color |
450 | rxvt_color::fade (rxvt_display *display, int percent) |
614 | rxvt_color::fade (rxvt_display *display, int percent) |
451 | { |
615 | { |
|
|
616 | percent = 100 - percent; |
|
|
617 | |
452 | unsigned short cr, cg, cb; |
618 | unsigned short cr, cg, cb; |
453 | rxvt_color faded; |
619 | rxvt_color faded; |
454 | |
620 | |
455 | get (display, cr, cg, cb); |
621 | get (display, cr, cg, cb); |
|
|
622 | |
456 | faded.set (display, |
623 | faded.set ( |
|
|
624 | display, |
457 | cr * percent / 100, |
625 | cr * percent / 100, |
458 | cg * percent / 100, |
626 | cg * percent / 100, |
459 | cb * percent / 100); |
627 | cb * percent / 100 |
|
|
628 | ); |
460 | |
629 | |
461 | return faded; |
630 | return faded; |
462 | } |
631 | } |
463 | |
632 | |
|
|
633 | #define LERP(a,b,p) (a * p + b * (100 - p)) / 100 |
|
|
634 | |
|
|
635 | rxvt_color |
|
|
636 | rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto) |
|
|
637 | { |
|
|
638 | percent = 100 - percent; |
|
|
639 | |
|
|
640 | unsigned short cr, cg, cb; |
|
|
641 | unsigned short fcr, fcg, fcb; |
|
|
642 | rxvt_color faded; |
|
|
643 | |
|
|
644 | get (display, cr, cg, cb); |
|
|
645 | fadeto.get(display, fcr, fcg, fcb); |
|
|
646 | |
|
|
647 | faded.set ( |
|
|
648 | display, |
|
|
649 | LERP (cr, fcr, percent), |
|
|
650 | LERP (cg, fcg, percent), |
|
|
651 | LERP (cb, fcb, percent) |
|
|
652 | ); |
|
|
653 | |
|
|
654 | return faded; |
|
|
655 | } |
|
|
656 | |