1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*----------------------------------------------------------------------* |
2 | * File: rxvttoolkit.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-2011 Marc Lehmann <schmorp@schmorp.de> |
|
|
7 | * Copyright (c) 2011 Emanuele Giaquinta <e.giaquinta@glauco.it> |
7 | * |
8 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * the Free Software Foundation; either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * (at your option) any later version. |
12 | * |
13 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
… | |
… | |
22 | |
23 | |
23 | #include "../config.h" |
24 | #include "../config.h" |
24 | #include <rxvt.h> |
25 | #include <rxvt.h> |
25 | #include <rxvttoolkit.h> |
26 | #include <rxvttoolkit.h> |
26 | |
27 | |
|
|
28 | #include <stdlib.h> |
|
|
29 | |
27 | #include <unistd.h> |
30 | #include <unistd.h> |
28 | #include <fcntl.h> |
31 | #include <fcntl.h> |
29 | |
32 | |
30 | #include <sys/utsname.h> |
33 | #include <sys/utsname.h> |
31 | |
|
|
32 | #ifndef NO_SLOW_LINK_SUPPORT |
|
|
33 | # include <sys/socket.h> |
34 | #include <sys/socket.h> |
34 | # include <sys/un.h> |
35 | #include <sys/un.h> |
35 | #endif |
|
|
36 | |
36 | |
|
|
37 | #if XFT |
|
|
38 | # include <X11/extensions/Xrender.h> |
|
|
39 | #endif |
|
|
40 | |
37 | const char *const xa_names[] = |
41 | static const char *const xa_names[] = |
38 | { |
42 | { |
39 | "TEXT", |
43 | "TEXT", |
40 | "COMPOUND_TEXT", |
44 | "COMPOUND_TEXT", |
41 | "UTF8_STRING", |
45 | "UTF8_STRING", |
42 | "MULTIPLE", |
46 | "MULTIPLE", |
43 | "TARGETS", |
47 | "TARGETS", |
44 | "TIMESTAMP", |
48 | "TIMESTAMP", |
45 | "VT_SELECTION", |
49 | "VT_SELECTION", |
46 | "INCR", |
50 | "INCR", |
47 | "WM_PROTOCOLS", |
51 | "WM_PROTOCOLS", |
48 | "WM_DELETE_WINDOW", |
52 | "WM_DELETE_WINDOW", |
49 | "CLIPBOARD", |
53 | "CLIPBOARD", |
|
|
54 | "AVERAGE_WIDTH", |
|
|
55 | "WEIGHT_NAME", |
|
|
56 | "SLANT", |
|
|
57 | "CHARSET_REGISTRY", |
|
|
58 | "CHARSET_ENCODING", |
50 | #if ENABLE_FRILLS |
59 | #if ENABLE_FRILLS |
51 | "_MOTIF_WM_HINTS", |
60 | "_MOTIF_WM_HINTS", |
52 | #endif |
61 | #endif |
53 | #if ENABLE_EWMH |
62 | #if ENABLE_EWMH |
54 | "_NET_WM_PID", |
63 | "_NET_WM_PID", |
55 | "_NET_WM_NAME", |
64 | "_NET_WM_NAME", |
56 | "_NET_WM_ICON_NAME", |
65 | "_NET_WM_ICON_NAME", |
57 | "_NET_WM_PING", |
66 | "_NET_WM_PING", |
|
|
67 | "_NET_WM_ICON", |
58 | #endif |
68 | #endif |
59 | #if USE_XIM |
69 | #if USE_XIM |
60 | "WM_LOCALE_NAME", |
70 | "WM_LOCALE_NAME", |
61 | "XIM_SERVERS", |
71 | "XIM_SERVERS", |
62 | #endif |
72 | #endif |
63 | #ifdef TRANSPARENT |
73 | #if HAVE_IMG || ENABLE_PERL |
64 | "_XROOTPMAP_ID", |
74 | "_XROOTPMAP_ID", |
65 | "ESETROOT_PMAP_ID", |
75 | "ESETROOT_PMAP_ID", |
66 | #endif |
76 | #endif |
67 | #if ENABLE_XEMBED |
77 | #if ENABLE_XEMBED |
68 | "_XEMBED", |
78 | "_XEMBED", |
69 | "_XEMBED_INFO", |
79 | "_XEMBED_INFO", |
|
|
80 | #endif |
|
|
81 | #if !ENABLE_MINIMAL |
|
|
82 | "SCREEN_RESOURCES", |
|
|
83 | "XDCCC_LINEAR_RGB_CORRECTION", |
|
|
84 | "XDCCC_LINEAR_RGB_MATRICES", |
|
|
85 | "WM_COLORMAP_WINDOWS", |
|
|
86 | "WM_STATE", |
|
|
87 | "cursor", |
|
|
88 | # if USE_XIM |
|
|
89 | "TRANSPORT", |
|
|
90 | "LOCALES", |
|
|
91 | "_XIM_PROTOCOL", |
|
|
92 | "_XIM_XCONNECT", |
|
|
93 | "_XIM_MOREDATA", |
70 | #endif |
94 | # endif |
71 | }; |
95 | #endif |
|
|
96 | }; |
72 | |
97 | |
73 | ///////////////////////////////////////////////////////////////////////////// |
98 | ///////////////////////////////////////////////////////////////////////////// |
74 | |
99 | |
75 | refcounted::refcounted (const char *id) |
100 | refcounted::refcounted (const char *id) |
76 | { |
101 | { |
… | |
… | |
130 | put (*this->begin ()); |
155 | put (*this->begin ()); |
131 | } |
156 | } |
132 | |
157 | |
133 | ///////////////////////////////////////////////////////////////////////////// |
158 | ///////////////////////////////////////////////////////////////////////////// |
134 | |
159 | |
135 | #ifdef USE_XIM |
160 | #if USE_XIM |
136 | |
161 | |
137 | static void |
162 | static void |
138 | #if XIMCB_PROTO_BROKEN |
163 | #if XIMCB_PROTO_BROKEN |
139 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
164 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
140 | #else |
165 | #else |
… | |
… | |
153 | bool |
178 | bool |
154 | rxvt_xim::ref_init () |
179 | rxvt_xim::ref_init () |
155 | { |
180 | { |
156 | display = GET_R->display; //HACK: TODO |
181 | display = GET_R->display; //HACK: TODO |
157 | |
182 | |
158 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
183 | xim = XOpenIM (display->dpy, 0, 0, 0); |
159 | |
184 | |
160 | if (!xim) |
185 | if (!xim) |
161 | return false; |
186 | return false; |
162 | |
187 | |
163 | XIMCallback ximcallback; |
188 | XIMCallback ximcallback; |
164 | ximcallback.client_data = (XPointer)this; |
189 | ximcallback.client_data = (XPointer)this; |
165 | ximcallback.callback = im_destroy_cb; |
190 | ximcallback.callback = im_destroy_cb; |
166 | |
191 | |
167 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); |
192 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0); |
168 | |
193 | |
169 | return true; |
194 | return true; |
170 | } |
195 | } |
171 | |
196 | |
172 | rxvt_xim::~rxvt_xim () |
197 | rxvt_xim::~rxvt_xim () |
… | |
… | |
177 | |
202 | |
178 | #endif |
203 | #endif |
179 | |
204 | |
180 | ///////////////////////////////////////////////////////////////////////////// |
205 | ///////////////////////////////////////////////////////////////////////////// |
181 | |
206 | |
|
|
207 | #if XFT |
|
|
208 | rxvt_drawable::~rxvt_drawable () |
|
|
209 | { |
|
|
210 | if (xftdrawable) |
|
|
211 | XftDrawDestroy (xftdrawable); |
|
|
212 | } |
|
|
213 | |
|
|
214 | rxvt_drawable::operator XftDraw *() |
|
|
215 | { |
|
|
216 | if (!xftdrawable) |
|
|
217 | xftdrawable = XftDrawCreate (screen->dpy, drawable, screen->visual, screen->cmap); |
|
|
218 | |
|
|
219 | return xftdrawable; |
|
|
220 | } |
|
|
221 | #endif |
|
|
222 | |
|
|
223 | ///////////////////////////////////////////////////////////////////////////// |
|
|
224 | |
|
|
225 | #if XFT |
|
|
226 | |
|
|
227 | // not strictly necessary as it is only used with superclass of zero_initialised |
|
|
228 | rxvt_screen::rxvt_screen () |
|
|
229 | : scratch_area (0) |
|
|
230 | { |
|
|
231 | } |
|
|
232 | |
|
|
233 | rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) |
|
|
234 | { |
|
|
235 | if (!scratch_area || w > scratch_w || h > scratch_h) |
|
|
236 | { |
|
|
237 | if (scratch_area) |
|
|
238 | { |
|
|
239 | XFreePixmap (dpy, scratch_area->drawable); |
|
|
240 | delete scratch_area; |
|
|
241 | } |
|
|
242 | |
|
|
243 | Pixmap pm = XCreatePixmap (dpy, RootWindowOfScreen (ScreenOfDisplay (dpy, display->screen)), |
|
|
244 | scratch_w = w, scratch_h = h, depth); |
|
|
245 | |
|
|
246 | scratch_area = new rxvt_drawable (this, pm); |
|
|
247 | } |
|
|
248 | |
|
|
249 | return *scratch_area; |
|
|
250 | } |
|
|
251 | |
|
|
252 | #endif |
|
|
253 | |
182 | void |
254 | void |
183 | rxvt_screen::set (rxvt_display *disp) |
255 | rxvt_screen::set (rxvt_display *disp) |
184 | { |
256 | { |
185 | display = disp; |
257 | display = disp; |
186 | xdisp = disp->display; |
258 | dpy = disp->dpy; |
187 | |
259 | |
188 | Screen *screen = ScreenOfDisplay (xdisp, disp->screen); |
260 | Screen *screen = ScreenOfDisplay (dpy, disp->screen); |
189 | |
261 | |
190 | depth = DefaultDepthOfScreen (screen); |
262 | depth = DefaultDepthOfScreen (screen); |
191 | visual = DefaultVisualOfScreen (screen); |
263 | visual = DefaultVisualOfScreen (screen); |
192 | cmap = DefaultColormapOfScreen (screen); |
264 | cmap = DefaultColormapOfScreen (screen); |
193 | } |
265 | } |
194 | |
266 | |
195 | void |
267 | #if ENABLE_FRILLS |
196 | rxvt_screen::set (rxvt_display *disp, int bitdepth) |
|
|
197 | { |
|
|
198 | set (disp); |
|
|
199 | |
268 | |
|
|
269 | void |
|
|
270 | rxvt_screen::select_visual (int id) |
|
|
271 | { |
200 | XVisualInfo vinfo; |
272 | XVisualInfo vinfo; |
|
|
273 | vinfo.visualid = id; |
|
|
274 | int n; |
201 | |
275 | |
|
|
276 | if (XVisualInfo *vi = XGetVisualInfo (dpy, VisualIDMask, &vinfo, &n)) |
|
|
277 | { |
|
|
278 | depth = vi->depth; |
|
|
279 | visual = vi->visual; |
|
|
280 | |
|
|
281 | XFree (vi); |
|
|
282 | |
|
|
283 | cmap = XCreateColormap (dpy, display->root, visual, AllocNone); |
|
|
284 | } |
|
|
285 | else |
|
|
286 | rxvt_warn ("no visual found for requested id 0x%02x, using default visual.\n", id); |
|
|
287 | } |
|
|
288 | |
|
|
289 | void |
|
|
290 | rxvt_screen::select_depth (int bitdepth) |
|
|
291 | { |
|
|
292 | XVisualInfo vinfo; |
|
|
293 | |
202 | if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) |
294 | if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) |
203 | { |
|
|
204 | depth = bitdepth; |
|
|
205 | visual = vinfo.visual; |
295 | select_visual (vinfo.visualid); |
206 | cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); |
296 | else |
207 | } |
297 | rxvt_warn ("no visual found for requested depth %d, using default visual.\n", bitdepth); |
208 | } |
298 | } |
|
|
299 | |
|
|
300 | #endif |
209 | |
301 | |
210 | void |
302 | void |
211 | rxvt_screen::clear () |
303 | rxvt_screen::clear () |
212 | { |
304 | { |
|
|
305 | #if XFT |
|
|
306 | if (scratch_area) |
|
|
307 | { |
|
|
308 | XFreePixmap (dpy, scratch_area->drawable); |
|
|
309 | delete scratch_area; |
|
|
310 | } |
|
|
311 | #endif |
|
|
312 | |
213 | if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen))) |
313 | if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (dpy, display->screen))) |
214 | XFreeColormap (xdisp, cmap); |
314 | XFreeColormap (dpy, cmap); |
215 | } |
315 | } |
216 | |
316 | |
217 | ///////////////////////////////////////////////////////////////////////////// |
317 | ///////////////////////////////////////////////////////////////////////////// |
218 | |
318 | |
219 | rxvt_display::rxvt_display (const char *id) |
319 | rxvt_display::rxvt_display (const char *id) |
220 | : refcounted (id) |
320 | : refcounted (id) |
221 | , x_ev (this, &rxvt_display::x_cb) |
|
|
222 | , selection_owner (0) |
321 | , selection_owner (0) |
|
|
322 | , clipboard_owner (0) |
223 | { |
323 | { |
|
|
324 | x_ev .set<rxvt_display, &rxvt_display::x_cb > (this); |
|
|
325 | flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this); |
224 | } |
326 | } |
225 | |
327 | |
226 | XrmDatabase |
328 | XrmDatabase |
227 | rxvt_display::get_resources () |
329 | rxvt_display::get_resources (bool refresh) |
228 | { |
330 | { |
229 | char *homedir = (char *)getenv ("HOME"); |
331 | char *homedir = getenv ("HOME"); |
230 | char fname[1024]; |
332 | char fname[1024]; |
231 | |
333 | |
232 | /* |
334 | /* |
233 | * get resources using the X library function |
335 | * get resources using the X library function |
234 | */ |
336 | */ |
235 | char *displayResource, *xe; |
337 | char *displayResource, *xe; |
236 | XrmDatabase database, rdb1; |
338 | XrmDatabase rdb1, database = 0; |
237 | |
339 | |
238 | database = NULL; |
340 | #if !XLIB_ILLEGAL_ACCESS |
|
|
341 | /* work around a bug in XrmSetDatabase where it frees the db, see ref_next */ |
|
|
342 | database = XrmGetStringDatabase (""); |
|
|
343 | #endif |
239 | |
344 | |
240 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
345 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
|
|
346 | // as opposed to "standard practise", we always read in ~/.Xdefaults |
241 | |
347 | |
242 | // 6. System wide per application default file. |
348 | // 6. System wide per application default file. |
243 | |
349 | |
244 | /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ |
350 | /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ |
245 | if ((xe = (char *)getenv ("XAPPLRESDIR"))) |
351 | if ((xe = getenv ("XAPPLRESDIR"))) |
246 | { |
352 | { |
247 | snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); |
353 | snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); |
248 | |
354 | |
249 | if ((rdb1 = XrmGetFileDatabase (fname))) |
355 | if ((rdb1 = XrmGetFileDatabase (fname))) |
250 | XrmMergeDatabases (rdb1, &database); |
356 | XrmMergeDatabases (rdb1, &database); |
… | |
… | |
252 | |
358 | |
253 | // 5. User's per application default file. |
359 | // 5. User's per application default file. |
254 | // none |
360 | // none |
255 | |
361 | |
256 | // 4. User's defaults file. |
362 | // 4. User's defaults file. |
|
|
363 | if (homedir) |
|
|
364 | { |
|
|
365 | snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); |
|
|
366 | |
|
|
367 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
368 | XrmMergeDatabases (rdb1, &database); |
|
|
369 | } |
|
|
370 | |
257 | /* Get any Xserver defaults */ |
371 | /* Get any Xserver defaults */ |
|
|
372 | if (refresh) |
|
|
373 | { |
|
|
374 | // fucking xlib keeps a copy of the rm string |
|
|
375 | Atom actual_type; |
|
|
376 | int actual_format; |
|
|
377 | unsigned long nitems, nremaining; |
|
|
378 | char *val = 0; |
|
|
379 | |
|
|
380 | #if XLIB_ILLEGAL_ACCESS |
|
|
381 | if (dpy->xdefaults) |
|
|
382 | XFree (dpy->xdefaults); |
|
|
383 | #endif |
|
|
384 | |
|
|
385 | if (XGetWindowProperty (dpy, RootWindow (dpy, 0), XA_RESOURCE_MANAGER, |
|
|
386 | 0L, 100000000L, False, |
|
|
387 | XA_STRING, &actual_type, &actual_format, |
|
|
388 | &nitems, &nremaining, |
|
|
389 | (unsigned char **)&val) == Success |
|
|
390 | && actual_type == XA_STRING |
|
|
391 | && actual_format == 8) |
|
|
392 | displayResource = val; |
|
|
393 | else |
|
|
394 | { |
|
|
395 | displayResource = 0; |
|
|
396 | |
|
|
397 | if (val) |
|
|
398 | XFree (val); |
|
|
399 | } |
|
|
400 | |
|
|
401 | #if XLIB_ILLEGAL_ACCESS |
|
|
402 | dpy->xdefaults = displayResource; |
|
|
403 | #endif |
|
|
404 | } |
|
|
405 | else |
258 | displayResource = XResourceManagerString (display); |
406 | displayResource = XResourceManagerString (dpy); |
259 | |
407 | |
260 | if (displayResource != NULL) |
408 | if (displayResource) |
261 | { |
409 | { |
262 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
410 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
263 | XrmMergeDatabases (rdb1, &database); |
411 | XrmMergeDatabases (rdb1, &database); |
264 | } |
412 | } |
265 | else if (homedir) |
|
|
266 | { |
|
|
267 | snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); |
|
|
268 | |
413 | |
269 | if ((rdb1 = XrmGetFileDatabase (fname))) |
414 | #if !XLIB_ILLEGAL_ACCESS |
270 | XrmMergeDatabases (rdb1, &database); |
415 | if (refresh && displayResource) |
271 | } |
416 | XFree (displayResource); |
|
|
417 | #endif |
272 | |
418 | |
273 | /* Get screen specific resources */ |
419 | /* Get screen specific resources */ |
274 | displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); |
420 | displayResource = XScreenResourceString (ScreenOfDisplay (dpy, screen)); |
275 | |
421 | |
276 | if (displayResource != NULL) |
422 | if (displayResource) |
277 | { |
423 | { |
278 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
424 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
279 | /* Merge with screen-independent resources */ |
425 | /* Merge with screen-independent resources */ |
280 | XrmMergeDatabases (rdb1, &database); |
426 | XrmMergeDatabases (rdb1, &database); |
281 | |
427 | |
282 | XFree (displayResource); |
428 | XFree (displayResource); |
283 | } |
429 | } |
284 | |
430 | |
285 | // 3. User's per host defaults file |
431 | // 3. User's per host defaults file |
286 | /* Add in XENVIRONMENT file */ |
432 | /* Add in XENVIRONMENT file */ |
287 | if ((xe = (char *)getenv ("XENVIRONMENT")) |
433 | if ((xe = getenv ("XENVIRONMENT")) |
288 | && (rdb1 = XrmGetFileDatabase (xe))) |
434 | && (rdb1 = XrmGetFileDatabase (xe))) |
289 | XrmMergeDatabases (rdb1, &database); |
435 | XrmMergeDatabases (rdb1, &database); |
290 | else if (homedir) |
436 | else if (homedir) |
291 | { |
437 | { |
292 | struct utsname un; |
438 | struct utsname un; |
… | |
… | |
306 | bool rxvt_display::ref_init () |
452 | bool rxvt_display::ref_init () |
307 | { |
453 | { |
308 | #ifdef LOCAL_X_IS_UNIX |
454 | #ifdef LOCAL_X_IS_UNIX |
309 | if (id[0] == ':') |
455 | if (id[0] == ':') |
310 | { |
456 | { |
311 | val = rxvt_malloc (5 + strlen (id) + 1); |
457 | char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1); |
|
|
458 | |
312 | strcpy (val, "unix/"); |
459 | strcpy (val, "unix/"); |
313 | strcat (val, id); |
460 | strcat (val, id); |
|
|
461 | |
314 | display = XOpenDisplay (val); |
462 | dpy = XOpenDisplay (val); |
315 | free (val); |
|
|
316 | } |
463 | } |
317 | else |
464 | else |
318 | #endif |
465 | #endif |
319 | display = 0; |
466 | dpy = 0; |
320 | |
467 | |
321 | if (!display) |
468 | if (!dpy) |
322 | display = XOpenDisplay (id); |
469 | dpy = XOpenDisplay (id); |
323 | |
470 | |
324 | if (!display) |
471 | if (!dpy) |
325 | return false; |
472 | return false; |
326 | |
473 | |
327 | screen = DefaultScreen (display); |
474 | screen = DefaultScreen (dpy); |
328 | root = DefaultRootWindow (display); |
475 | root = DefaultRootWindow (dpy); |
329 | |
476 | |
330 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
477 | assert (ecb_array_length (xa_names) == NUM_XA); |
331 | XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); |
478 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
332 | |
479 | |
333 | XrmSetDatabase (display, get_resources ()); |
480 | XrmSetDatabase (dpy, get_resources (false)); |
334 | |
481 | |
335 | #ifdef POINTER_BLANK |
482 | #ifdef POINTER_BLANK |
336 | XColor blackcolour; |
483 | XColor blackcolour; |
337 | blackcolour.red = 0; |
484 | blackcolour.red = 0; |
338 | blackcolour.green = 0; |
485 | blackcolour.green = 0; |
339 | blackcolour.blue = 0; |
486 | blackcolour.blue = 0; |
340 | Font f = XLoadFont (display, "fixed"); |
487 | Font f = XLoadFont (dpy, "fixed"); |
341 | blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', |
488 | blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', |
342 | &blackcolour, &blackcolour); |
489 | &blackcolour, &blackcolour); |
343 | XUnloadFont (display, f); |
490 | XUnloadFont (dpy, f); |
344 | #endif |
491 | #endif |
345 | |
492 | |
|
|
493 | flags = 0; |
|
|
494 | #if XRENDER |
|
|
495 | int major, minor; |
|
|
496 | if (XRenderQueryVersion (dpy, &major, &minor)) |
|
|
497 | if (major > 0 || (major == 0 && minor >= 11)) |
|
|
498 | { |
|
|
499 | flags |= DISPLAY_HAS_RENDER; |
|
|
500 | |
|
|
501 | if (XFilters *filters = XRenderQueryFilters (dpy, root)) |
|
|
502 | { |
|
|
503 | for (int i = 0; i < filters->nfilter; i++) |
|
|
504 | if (!strcmp (filters->filter [i], FilterConvolution)) |
|
|
505 | flags |= DISPLAY_HAS_RENDER_CONV; |
|
|
506 | |
|
|
507 | XFree (filters); |
|
|
508 | } |
|
|
509 | } |
|
|
510 | #endif |
|
|
511 | |
346 | int fd = XConnectionNumber (display); |
512 | int fd = XConnectionNumber (dpy); |
347 | |
513 | |
348 | #ifndef NO_SLOW_LINK_SUPPORT |
|
|
349 | // try to detect wether we have a local connection. |
514 | // try to detect whether we have a local connection. |
350 | // assume unix domains socket == local, everything else not |
515 | // assume unix domain socket == local, everything else not |
351 | // TODO: might want to check for inet/127.0.0.1 |
516 | // TODO: might want to check for inet/127.0.0.1 |
352 | is_local = 0; |
517 | is_local = 0; |
353 | sockaddr_un sa; |
518 | sockaddr_un sa; |
354 | socklen_t sl = sizeof (sa); |
519 | socklen_t sl = sizeof (sa); |
355 | |
520 | |
356 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
521 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
357 | is_local = sa.sun_family == AF_LOCAL; |
522 | is_local = sa.sun_family == AF_UNIX; |
358 | #endif |
|
|
359 | |
523 | |
|
|
524 | flush_ev.start (); |
360 | x_ev.start (fd, EVENT_READ); |
525 | x_ev.start (fd, ev::READ); |
361 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
526 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
362 | |
527 | |
363 | XSelectInput (display, root, PropertyChangeMask); |
528 | XSelectInput (dpy, root, PropertyChangeMask); |
364 | |
529 | |
365 | flush (); |
530 | flush (); |
366 | |
531 | |
367 | return true; |
532 | return true; |
368 | } |
533 | } |
369 | |
534 | |
370 | void |
535 | void |
371 | rxvt_display::ref_next () |
536 | rxvt_display::ref_next () |
372 | { |
537 | { |
373 | // TODO: somehow check wether the database files/resources changed |
538 | // TODO: somehow check whether the database files/resources changed |
374 | // before re-loading/parsing |
539 | // before affording re-loading/parsing |
375 | XrmDestroyDatabase (XrmGetDatabase (display)); |
540 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
|
|
541 | #if XLIB_ILLEGAL_ACCESS |
|
|
542 | /* work around a bug in XrmSetDatabase where it frees the db */ |
|
|
543 | dpy->db = 0; |
|
|
544 | #endif |
376 | XrmSetDatabase (display, get_resources ()); |
545 | XrmSetDatabase (dpy, get_resources (true)); |
377 | } |
546 | } |
378 | |
547 | |
379 | rxvt_display::~rxvt_display () |
548 | rxvt_display::~rxvt_display () |
380 | { |
549 | { |
381 | if (!display) |
550 | if (!dpy) |
382 | return; |
551 | return; |
383 | |
552 | |
384 | #ifdef POINTER_BLANK |
553 | #ifdef POINTER_BLANK |
385 | XFreeCursor (display, blank_cursor); |
554 | XFreeCursor (dpy, blank_cursor); |
386 | #endif |
555 | #endif |
387 | x_ev.stop (); |
556 | x_ev.stop (); |
|
|
557 | flush_ev.stop (); |
388 | #ifdef USE_XIM |
558 | #if USE_XIM |
389 | xims.clear (); |
559 | xims.clear (); |
390 | #endif |
560 | #endif |
|
|
561 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
391 | XCloseDisplay (display); |
562 | XCloseDisplay (dpy); |
392 | } |
563 | } |
393 | |
564 | |
394 | #ifdef USE_XIM |
565 | #if USE_XIM |
395 | void rxvt_display::im_change_cb () |
566 | void rxvt_display::im_change_cb () |
396 | { |
567 | { |
397 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
568 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
398 | (*i)->call (); |
569 | (*i)->call (); |
399 | } |
570 | } |
… | |
… | |
404 | // registers, as xlib crashes due to a race otherwise. |
575 | // registers, as xlib crashes due to a race otherwise. |
405 | Atom actual_type, *atoms; |
576 | Atom actual_type, *atoms; |
406 | int actual_format; |
577 | int actual_format; |
407 | unsigned long nitems, bytes_after; |
578 | unsigned long nitems, bytes_after; |
408 | |
579 | |
409 | if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
580 | if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
410 | False, XA_ATOM, &actual_type, &actual_format, |
581 | False, XA_ATOM, &actual_type, &actual_format, |
411 | &nitems, &bytes_after, (unsigned char **)&atoms) |
582 | &nitems, &bytes_after, (unsigned char **)&atoms) |
412 | != Success ) |
583 | != Success) |
413 | return; |
584 | return; |
414 | |
585 | |
415 | if (actual_type == XA_ATOM && actual_format == 32) |
586 | if (actual_type == XA_ATOM && actual_format == 32) |
416 | for (int i = 0; i < nitems; i++) |
587 | for (int i = 0; i < nitems; i++) |
417 | if (XGetSelectionOwner (display, atoms[i])) |
588 | if (XGetSelectionOwner (dpy, atoms[i])) |
418 | { |
589 | { |
419 | im_change_cb (); |
590 | im_change_cb (); |
420 | break; |
591 | break; |
421 | } |
592 | } |
422 | |
593 | |
423 | XFree (atoms); |
594 | XFree (atoms); |
424 | } |
595 | } |
425 | #endif |
596 | #endif |
426 | |
597 | |
427 | void rxvt_display::x_cb (io_watcher &w, short revents) |
598 | void rxvt_display::x_cb (ev::io &w, int revents) |
428 | { |
599 | { |
|
|
600 | flush_ev.start (); |
|
|
601 | } |
|
|
602 | |
|
|
603 | void rxvt_display::flush_cb (ev::prepare &w, int revents) |
|
|
604 | { |
|
|
605 | while (XEventsQueued (dpy, QueuedAfterFlush)) |
429 | do |
606 | do |
430 | { |
607 | { |
431 | XEvent xev; |
608 | XEvent xev; |
432 | XNextEvent (display, &xev); |
609 | XNextEvent (dpy, &xev); |
433 | |
610 | |
434 | #ifdef USE_XIM |
611 | #if USE_XIM |
435 | if (!XFilterEvent (&xev, None)) |
612 | if (!XFilterEvent (&xev, None)) |
436 | { |
613 | { |
437 | if (xev.type == PropertyNotify |
614 | if (xev.type == PropertyNotify |
438 | && xev.xany.window == root |
615 | && xev.xany.window == root |
439 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
616 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
440 | im_change_check (); |
617 | im_change_check (); |
441 | #endif |
618 | #endif |
|
|
619 | if (xev.type == MappingNotify) |
|
|
620 | XRefreshKeyboardMapping (&xev.xmapping); |
|
|
621 | |
442 | for (int i = xw.size (); i--; ) |
622 | for (int i = xw.size (); i--; ) |
443 | { |
623 | { |
444 | if (!xw[i]) |
624 | if (!xw[i]) |
445 | xw.erase_unordered (i); |
625 | xw.erase_unordered (i); |
446 | else if (xw[i]->window == xev.xany.window) |
626 | else if (xw[i]->window == xev.xany.window) |
447 | xw[i]->call (xev); |
627 | xw[i]->call (xev); |
448 | } |
628 | } |
449 | #ifdef USE_XIM |
629 | #if USE_XIM |
450 | } |
630 | } |
451 | #endif |
631 | #endif |
452 | } |
632 | } |
453 | while (XEventsQueued (display, QueuedAlready)); |
633 | while (XEventsQueued (dpy, QueuedAlready)); |
454 | |
634 | |
455 | XFlush (display); |
635 | w.stop (); |
456 | } |
|
|
457 | |
|
|
458 | void rxvt_display::flush () |
|
|
459 | { |
|
|
460 | if (XEventsQueued (display, QueuedAlready)) |
|
|
461 | x_cb (x_ev, EVENT_READ); |
|
|
462 | |
|
|
463 | XFlush (display); |
|
|
464 | } |
636 | } |
465 | |
637 | |
466 | void rxvt_display::reg (xevent_watcher *w) |
638 | void rxvt_display::reg (xevent_watcher *w) |
467 | { |
639 | { |
|
|
640 | if (!w->active) |
|
|
641 | { |
468 | xw.push_back (w); |
642 | xw.push_back (w); |
469 | w->active = xw.size (); |
643 | w->active = xw.size (); |
|
|
644 | } |
470 | } |
645 | } |
471 | |
646 | |
472 | void rxvt_display::unreg (xevent_watcher *w) |
647 | void rxvt_display::unreg (xevent_watcher *w) |
473 | { |
648 | { |
474 | if (w->active) |
649 | if (w->active) |
|
|
650 | { |
475 | xw[w->active - 1] = 0; |
651 | xw[w->active - 1] = 0; |
|
|
652 | w->active = 0; |
|
|
653 | } |
476 | } |
654 | } |
477 | |
655 | |
478 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
656 | void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard) |
479 | { |
657 | { |
|
|
658 | rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner; |
|
|
659 | |
480 | if (selection_owner && selection_owner != owner) |
660 | if (cur_owner && cur_owner != owner) |
481 | selection_owner->selection_clear (); |
661 | { |
|
|
662 | rxvt_term *term = cur_owner; |
|
|
663 | term->selection_clear (clipboard); |
|
|
664 | term->flush (); |
|
|
665 | } |
482 | |
666 | |
483 | selection_owner = owner; |
667 | cur_owner = owner; |
484 | } |
668 | } |
485 | |
669 | |
486 | #ifdef USE_XIM |
670 | #if USE_XIM |
|
|
671 | |
487 | void rxvt_display::reg (im_watcher *w) |
672 | void rxvt_display::reg (im_watcher *w) |
488 | { |
673 | { |
489 | imw.push_back (w); |
674 | imw.push_back (w); |
490 | } |
675 | } |
491 | |
676 | |
… | |
… | |
500 | int l, m; |
685 | int l, m; |
501 | |
686 | |
502 | l = strlen (locale); |
687 | l = strlen (locale); |
503 | m = strlen (modifiers); |
688 | m = strlen (modifiers); |
504 | |
689 | |
505 | if (!(id = (char *)malloc (l + m + 2))) |
690 | if (!(id = rxvt_temp_buf<char> (l + m + 2))) |
506 | return 0; |
691 | return 0; |
507 | |
692 | |
508 | memcpy (id, locale, l); id[l] = '\n'; |
693 | memcpy (id, locale, l); id[l] = '\n'; |
509 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
694 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
510 | |
695 | |
511 | rxvt_xim *xim = xims.get (id); |
696 | rxvt_xim *xim = xims.get (id); |
512 | |
697 | |
513 | free (id); |
|
|
514 | |
|
|
515 | return xim; |
698 | return xim; |
516 | } |
699 | } |
517 | |
700 | |
518 | void rxvt_display::put_xim (rxvt_xim *xim) |
701 | void rxvt_display::put_xim (rxvt_xim *xim) |
519 | { |
702 | { |
520 | #if XLIB_IS_RACEFREE |
703 | # if XLIB_IS_RACEFREE |
521 | xims.put (xim); |
704 | xims.put (xim); |
522 | #endif |
705 | # endif |
523 | } |
706 | } |
|
|
707 | |
524 | #endif |
708 | #endif |
525 | |
709 | |
526 | Atom rxvt_display::atom (const char *name) |
710 | Atom rxvt_display::atom (const char *name) |
527 | { |
711 | { |
528 | return XInternAtom (display, name, False); |
712 | return XInternAtom (dpy, name, False); |
|
|
713 | } |
|
|
714 | |
|
|
715 | Pixmap |
|
|
716 | rxvt_display::get_pixmap_property (Atom property) |
|
|
717 | { |
|
|
718 | Pixmap pixmap = None; |
|
|
719 | |
|
|
720 | int aformat; |
|
|
721 | unsigned long nitems, bytes_after; |
|
|
722 | Atom atype; |
|
|
723 | unsigned char *prop; |
|
|
724 | int result = XGetWindowProperty (dpy, root, property, |
|
|
725 | 0L, 1L, False, XA_PIXMAP, &atype, &aformat, |
|
|
726 | &nitems, &bytes_after, &prop); |
|
|
727 | if (result == Success) |
|
|
728 | { |
|
|
729 | if (atype == XA_PIXMAP) |
|
|
730 | pixmap = *(Pixmap *)prop; |
|
|
731 | XFree (prop); |
|
|
732 | } |
|
|
733 | |
|
|
734 | return pixmap; |
529 | } |
735 | } |
530 | |
736 | |
531 | ///////////////////////////////////////////////////////////////////////////// |
737 | ///////////////////////////////////////////////////////////////////////////// |
532 | |
738 | |
533 | template class refcache<rxvt_display>; |
739 | template class refcache<rxvt_display>; |
534 | refcache<rxvt_display> displays; |
740 | refcache<rxvt_display> displays; |
535 | |
741 | |
536 | ///////////////////////////////////////////////////////////////////////////// |
742 | ///////////////////////////////////////////////////////////////////////////// |
537 | |
743 | // |
|
|
744 | |
|
|
745 | static unsigned int |
|
|
746 | insert_component (unsigned int value, unsigned int mask, unsigned int shift) |
|
|
747 | { |
|
|
748 | return (value * (mask + 1) >> 16) << shift; |
|
|
749 | } |
|
|
750 | |
|
|
751 | bool |
|
|
752 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
|
|
753 | { |
|
|
754 | //TODO: only supports 24 bit |
|
|
755 | unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
|
|
756 | |
|
|
757 | #if XFT |
|
|
758 | XRenderPictFormat *format; |
|
|
759 | |
|
|
760 | // FUCKING Xft gets it wrong, of course, so work around it. |
|
|
761 | // Transparency users should eat shit and die, and then |
|
|
762 | // XRenderQueryPictIndexValues themselves plenty. |
|
|
763 | if ((screen->visual->c_class == TrueColor) |
|
|
764 | && (format = XRenderFindVisualFormat (screen->dpy, screen->visual))) |
|
|
765 | { |
|
|
766 | // the fun lies in doing everything manually... |
|
|
767 | c.color.red = color.r; |
|
|
768 | c.color.green = color.g; |
|
|
769 | c.color.blue = color.b; |
|
|
770 | c.color.alpha = alpha; |
|
|
771 | |
|
|
772 | // Xft wants premultiplied alpha, but abuses the alpha channel |
|
|
773 | // as blend factor, and doesn't allow us to set the alpha channel |
|
|
774 | c.color.red = c.color.red * alpha / 0xffff; |
|
|
775 | c.color.green = c.color.green * alpha / 0xffff; |
|
|
776 | c.color.blue = c.color.blue * alpha / 0xffff; |
|
|
777 | |
|
|
778 | c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red ) |
|
|
779 | | insert_component (c.color.green, format->direct.greenMask, format->direct.green) |
|
|
780 | | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue ) |
|
|
781 | | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); |
|
|
782 | |
|
|
783 | return true; |
|
|
784 | } |
|
|
785 | else |
|
|
786 | { |
|
|
787 | XRenderColor d; |
|
|
788 | |
|
|
789 | d.red = color.r; |
|
|
790 | d.green = color.g; |
|
|
791 | d.blue = color.b; |
|
|
792 | d.alpha = alpha; |
|
|
793 | |
|
|
794 | // XftColorAlloc always returns 100% transparent pixels(!) |
|
|
795 | if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c)) |
|
|
796 | return true; |
|
|
797 | } |
|
|
798 | #else |
|
|
799 | c.red = color.r; |
|
|
800 | c.green = color.g; |
|
|
801 | c.blue = color.b; |
|
|
802 | |
|
|
803 | if (screen->visual->c_class == TrueColor) |
|
|
804 | { |
|
|
805 | c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask )) |
|
|
806 | | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask)) |
|
|
807 | | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask )); |
|
|
808 | |
|
|
809 | return true; |
|
|
810 | } |
|
|
811 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
|
|
812 | return true; |
|
|
813 | #endif |
|
|
814 | |
|
|
815 | //TODO: set c.color* or c.* |
|
|
816 | c.pixel = (color.r * 2 + color.g * 3 + color.b) >= 0x8000 * 6 |
|
|
817 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
|
|
818 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
|
|
819 | |
|
|
820 | return false; |
|
|
821 | } |
|
|
822 | |
538 | bool |
823 | bool |
539 | rxvt_color::set (rxvt_screen *screen, const char *name) |
824 | rxvt_color::set (rxvt_screen *screen, const char *name) |
540 | { |
825 | { |
|
|
826 | rgba c; |
|
|
827 | char eos; |
|
|
828 | int skip; |
|
|
829 | |
|
|
830 | c.a = rgba::MAX_CC; |
|
|
831 | |
|
|
832 | // parse the nonstandard "[alphapercent]" prefix |
|
|
833 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
|
|
834 | { |
|
|
835 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
|
|
836 | name += skip; |
|
|
837 | } |
|
|
838 | |
|
|
839 | // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format |
|
|
840 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
|
|
841 | { |
|
|
842 | XColor xc; |
|
|
843 | |
|
|
844 | if (XParseColor (screen->dpy, screen->cmap, name, &xc)) |
|
|
845 | { |
|
|
846 | c.r = xc.red; |
|
|
847 | c.g = xc.green; |
|
|
848 | c.b = xc.blue; |
|
|
849 | } |
|
|
850 | else |
|
|
851 | { |
|
|
852 | c.r = 0xffff; |
|
|
853 | c.g = 0x6969; |
|
|
854 | c.b = 0xb4b4; |
|
|
855 | |
|
|
856 | rxvt_warn ("unable to parse color '%s', using pink instead.\n", name); |
|
|
857 | } |
|
|
858 | } |
|
|
859 | |
|
|
860 | return set (screen, c); |
|
|
861 | } |
|
|
862 | |
|
|
863 | bool |
|
|
864 | rxvt_color::set (rxvt_screen *screen, const rgba &color) |
|
|
865 | { |
|
|
866 | bool got = alloc (screen, color); |
|
|
867 | |
|
|
868 | #if !ENABLE_MINIMAL |
|
|
869 | int cmap_size = screen->visual->map_entries; |
|
|
870 | |
|
|
871 | if (!got |
|
|
872 | && screen->visual->c_class == PseudoColor |
|
|
873 | && cmap_size < 4096) |
|
|
874 | { |
|
|
875 | XColor *colors = new XColor [screen->visual->map_entries]; |
|
|
876 | |
|
|
877 | for (int i = 0; i < cmap_size; i++) |
|
|
878 | colors [i].pixel = i; |
|
|
879 | |
|
|
880 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
|
|
881 | // many extra optimisations. |
|
|
882 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
|
|
883 | |
|
|
884 | while (cmap_size) |
|
|
885 | { |
|
|
886 | int diff = 0x7fffffffL; |
|
|
887 | XColor *best = colors; |
|
|
888 | |
|
|
889 | for (int i = 0; i < cmap_size; i++) |
|
|
890 | { |
|
|
891 | // simple weighted rgb distance sucks, but keeps it simple |
|
|
892 | int d = abs (color.r - colors [i].red ) * 2 |
|
|
893 | + abs (color.g - colors [i].green) * 3 |
|
|
894 | + abs (color.b - colors [i].blue ); |
|
|
895 | |
|
|
896 | if (d < diff) |
|
|
897 | { |
|
|
898 | diff = d; |
|
|
899 | best = colors + i; |
|
|
900 | } |
|
|
901 | } |
|
|
902 | |
|
|
903 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d,%d)\n", |
|
|
904 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff, best - colors); |
|
|
905 | |
|
|
906 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
|
|
907 | |
|
|
908 | if (got) |
|
|
909 | break; |
|
|
910 | |
|
|
911 | *best = colors [--cmap_size]; |
|
|
912 | } |
|
|
913 | |
|
|
914 | delete [] colors; |
|
|
915 | } |
|
|
916 | #endif |
|
|
917 | |
|
|
918 | return got; |
|
|
919 | } |
|
|
920 | |
|
|
921 | void |
|
|
922 | rxvt_color::get (rgba &color) const |
|
|
923 | { |
541 | #if XFT |
924 | #if XFT |
542 | int l = strlen (name); |
925 | //TODO premultiplied alpha?? |
543 | rxvt_rgba r; |
926 | color.r = c.color.red; |
544 | char eos; |
927 | color.g = c.color.green; |
545 | int mult; |
928 | color.b = c.color.blue; |
546 | |
929 | color.a = c.color.alpha; |
547 | if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
548 | mult = 0x1111; |
|
|
549 | else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
550 | mult = 0x0101; |
|
|
551 | else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
552 | mult = 0x0001; |
|
|
553 | else |
|
|
554 | return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c); |
|
|
555 | |
|
|
556 | r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult; |
|
|
557 | return set (screen, r); |
|
|
558 | #else |
930 | #else |
559 | XColor xc; |
931 | color.r = c.red; |
560 | |
932 | color.g = c.green; |
561 | if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
933 | color.b = c.blue; |
562 | return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue)); |
934 | color.a = rgba::MAX_CC; |
563 | |
|
|
564 | return false; |
|
|
565 | #endif |
935 | #endif |
566 | } |
936 | } |
567 | |
937 | |
568 | bool |
938 | void |
|
|
939 | rxvt_color::get (XColor &color) const |
|
|
940 | { |
|
|
941 | rgba c; |
|
|
942 | get (c); |
|
|
943 | |
|
|
944 | color.red = c.r; |
|
|
945 | color.green = c.g; |
|
|
946 | color.blue = c.b; |
|
|
947 | color.pixel = (Pixel)*this; |
|
|
948 | } |
|
|
949 | |
|
|
950 | void |
569 | rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) |
951 | rxvt_color::free (rxvt_screen *screen) |
570 | { |
952 | { |
|
|
953 | if (screen->visual->c_class == TrueColor) |
|
|
954 | return; // nothing to do |
|
|
955 | |
571 | #if XFT |
956 | #if XFT |
572 | XRenderColor d; |
|
|
573 | |
|
|
574 | d.red = rgba.r; |
|
|
575 | d.green = rgba.g; |
|
|
576 | d.blue = rgba.b; |
|
|
577 | d.alpha = rgba.a; |
|
|
578 | |
|
|
579 | return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); |
957 | XftColorFree (screen->dpy, screen->visual, screen->cmap, &c); |
580 | #else |
958 | #else |
581 | XColor xc; |
|
|
582 | |
|
|
583 | xc.red = rgba.r; |
|
|
584 | xc.green = rgba.g; |
|
|
585 | xc.blue = rgba.b; |
|
|
586 | xc.flags = DoRed | DoGreen | DoBlue; |
|
|
587 | |
|
|
588 | if (XAllocColor (screen->xdisp, screen->cmap, &xc)) |
|
|
589 | { |
|
|
590 | p = xc.pixel; |
|
|
591 | return true; |
|
|
592 | } |
|
|
593 | |
|
|
594 | return false; |
|
|
595 | #endif |
|
|
596 | } |
|
|
597 | |
|
|
598 | void |
|
|
599 | rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba) |
|
|
600 | { |
|
|
601 | #if XFT |
|
|
602 | rgba.r = c.color.red; |
|
|
603 | rgba.g = c.color.green; |
|
|
604 | rgba.b = c.color.blue; |
|
|
605 | rgba.a = c.color.alpha; |
|
|
606 | #else |
|
|
607 | XColor c; |
|
|
608 | |
|
|
609 | c.pixel = p; |
|
|
610 | XQueryColor (screen->xdisp, screen->cmap, &c); |
|
|
611 | |
|
|
612 | rgba.r = c.red; |
|
|
613 | rgba.g = c.green; |
|
|
614 | rgba.b = c.blue; |
|
|
615 | rgba.a = rxvt_rgba::MAX_CC; |
|
|
616 | #endif |
|
|
617 | } |
|
|
618 | |
|
|
619 | void |
|
|
620 | rxvt_color::free (rxvt_screen *screen) |
|
|
621 | { |
|
|
622 | #if XFT |
|
|
623 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
|
|
624 | #else |
|
|
625 | XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); |
959 | XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes); |
626 | #endif |
960 | #endif |
627 | } |
961 | } |
628 | |
962 | |
629 | rxvt_color |
963 | void |
630 | rxvt_color::fade (rxvt_screen *screen, int percent) |
|
|
631 | { |
|
|
632 | rxvt_color faded; |
|
|
633 | |
|
|
634 | rxvt_rgba c; |
|
|
635 | get (screen, c); |
|
|
636 | |
|
|
637 | c.r = lerp (0, c.r, percent); |
|
|
638 | c.g = lerp (0, c.g, percent); |
|
|
639 | c.b = lerp (0, c.b, percent); |
|
|
640 | |
|
|
641 | faded.set (screen, c); |
|
|
642 | |
|
|
643 | return faded; |
|
|
644 | } |
|
|
645 | |
|
|
646 | rxvt_color |
|
|
647 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto) |
964 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to) |
648 | { |
965 | { |
649 | rxvt_rgba c, fc; |
966 | rgba c; |
650 | rxvt_color faded; |
967 | get (c); |
651 | |
|
|
652 | get (screen, c); |
|
|
653 | fadeto.get (screen, fc); |
|
|
654 | |
968 | |
655 | faded.set ( |
969 | result.set ( |
656 | screen, |
970 | screen, |
657 | rxvt_rgba ( |
971 | rgba ( |
658 | lerp (fc.r, c.r, percent), |
972 | lerp (c.r, to.r, percent), |
659 | lerp (fc.g, c.g, percent), |
973 | lerp (c.g, to.g, percent), |
660 | lerp (fc.b, c.b, percent), |
974 | lerp (c.b, to.b, percent), |
661 | lerp (fc.a, c.a, percent) |
975 | lerp (c.a, to.a, percent) |
662 | ) |
976 | ) |
663 | ); |
977 | ); |
|
|
978 | } |
664 | |
979 | |
|
|
980 | rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term) |
|
|
981 | : display (disp), request_time (tm), request_win (win), request_prop (prop), term (term) |
|
|
982 | { |
|
|
983 | assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); |
|
|
984 | |
|
|
985 | timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this); |
|
|
986 | timer_ev.repeat = 10.; |
|
|
987 | x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this); |
|
|
988 | |
|
|
989 | incr_buf = 0; |
|
|
990 | incr_buf_size = incr_buf_fill = 0; |
|
|
991 | selection_wait = Sel_normal; |
|
|
992 | selection_type = selnum; |
|
|
993 | cb_sv = 0; |
|
|
994 | } |
|
|
995 | |
|
|
996 | void |
|
|
997 | rxvt_selection::stop () |
|
|
998 | { |
|
|
999 | free (incr_buf); |
|
|
1000 | incr_buf = 0; |
|
|
1001 | timer_ev.stop (); |
|
|
1002 | x_ev.stop (display); |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | rxvt_selection::~rxvt_selection () |
|
|
1006 | { |
|
|
1007 | stop (); |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | void |
|
|
1011 | rxvt_selection::run () |
|
|
1012 | { |
|
|
1013 | int selnum = selection_type; |
|
|
1014 | |
|
|
1015 | #if ENABLE_FRILLS |
|
|
1016 | if (selnum == Sel_Primary && display->selection_owner) |
|
|
1017 | { |
|
|
1018 | /* internal selection */ |
|
|
1019 | char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len); |
|
|
1020 | finish (str, strlen (str)); |
|
|
1021 | free (str); |
|
|
1022 | return; |
|
|
1023 | } |
|
|
1024 | #endif |
|
|
1025 | |
|
|
1026 | #if X_HAVE_UTF8_STRING |
|
|
1027 | selection_type = Sel_UTF8String; |
|
|
1028 | if (request (display->xa[XA_UTF8_STRING], selnum)) |
|
|
1029 | return; |
|
|
1030 | #else |
|
|
1031 | selection_type = Sel_CompoundText; |
|
|
1032 | if (request (display->xa[XA_COMPOUND_TEXT], selnum)) |
|
|
1033 | return; |
|
|
1034 | #endif |
|
|
1035 | |
|
|
1036 | // fallback to CUT_BUFFER0 if the requested property has no owner |
|
|
1037 | handle_selection (display->root, XA_CUT_BUFFER0, false); |
|
|
1038 | } |
|
|
1039 | |
|
|
1040 | void |
|
|
1041 | rxvt_selection::finish (char *data, unsigned int len) |
|
|
1042 | { |
|
|
1043 | if (!cb_sv) |
|
|
1044 | { |
|
|
1045 | if (data) |
|
|
1046 | term->paste (data, len); |
|
|
1047 | |
|
|
1048 | term->selection_req = 0; |
|
|
1049 | delete this; |
|
|
1050 | } |
|
|
1051 | #if ENABLE_PERL |
|
|
1052 | else |
|
|
1053 | { |
|
|
1054 | stop (); // we do not really trust perl callbacks |
|
|
1055 | rxvt_perl.selection_finish (this, data, len); |
|
|
1056 | } |
|
|
1057 | #endif |
|
|
1058 | } |
|
|
1059 | |
|
|
1060 | bool |
|
|
1061 | rxvt_selection::request (Atom target, int selnum) |
|
|
1062 | { |
|
|
1063 | Atom sel; |
|
|
1064 | |
|
|
1065 | selection_type |= selnum; |
|
|
1066 | |
|
|
1067 | if (selnum == Sel_Primary) |
|
|
1068 | sel = XA_PRIMARY; |
|
|
1069 | else if (selnum == Sel_Secondary) |
|
|
1070 | sel = XA_SECONDARY; |
|
|
1071 | else |
|
|
1072 | sel = display->xa[XA_CLIPBOARD]; |
|
|
1073 | |
|
|
1074 | if (XGetSelectionOwner (display->dpy, sel) != None) |
|
|
1075 | { |
|
|
1076 | XConvertSelection (display->dpy, sel, target, request_prop, |
|
|
1077 | request_win, request_time); |
|
|
1078 | x_ev.start (display, request_win); |
|
|
1079 | timer_ev.again (); |
|
|
1080 | return true; |
|
|
1081 | } |
|
|
1082 | |
665 | return faded; |
1083 | return false; |
666 | } |
1084 | } |
667 | |
1085 | |
|
|
1086 | void |
|
|
1087 | rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop) |
|
|
1088 | { |
|
|
1089 | Display *dpy = display->dpy; |
|
|
1090 | char *data = 0; |
|
|
1091 | unsigned int data_len = 0; |
|
|
1092 | unsigned long bytes_after; |
|
|
1093 | XTextProperty ct; |
|
|
1094 | |
|
|
1095 | // check for failed XConvertSelection |
|
|
1096 | if (prop == None) |
|
|
1097 | { |
|
|
1098 | bool error = true; |
|
|
1099 | int selnum = selection_type & Sel_whereMask; |
|
|
1100 | |
|
|
1101 | if (selection_type & Sel_CompoundText) |
|
|
1102 | { |
|
|
1103 | selection_type = 0; |
|
|
1104 | error = !request (XA_STRING, selnum); |
|
|
1105 | } |
|
|
1106 | |
|
|
1107 | if (selection_type & Sel_UTF8String) |
|
|
1108 | { |
|
|
1109 | selection_type = Sel_CompoundText; |
|
|
1110 | error = !request (display->xa[XA_COMPOUND_TEXT], selnum); |
|
|
1111 | } |
|
|
1112 | |
|
|
1113 | if (error) |
|
|
1114 | { |
|
|
1115 | ct.value = 0; |
|
|
1116 | goto bailout; |
|
|
1117 | } |
|
|
1118 | |
|
|
1119 | return; |
|
|
1120 | } |
|
|
1121 | |
|
|
1122 | // length == (2^31 - 1) / 4, as gdk |
|
|
1123 | if (XGetWindowProperty (dpy, win, prop, |
|
|
1124 | 0, 0x1fffffff, |
|
|
1125 | delete_prop, AnyPropertyType, |
|
|
1126 | &ct.encoding, &ct.format, |
|
|
1127 | &ct.nitems, &bytes_after, |
|
|
1128 | &ct.value) != Success) |
|
|
1129 | { |
|
|
1130 | ct.value = 0; |
|
|
1131 | goto bailout; |
|
|
1132 | } |
|
|
1133 | |
|
|
1134 | if (ct.encoding == None) |
|
|
1135 | goto bailout; |
|
|
1136 | |
|
|
1137 | if (ct.value == 0) |
|
|
1138 | goto bailout; |
|
|
1139 | |
|
|
1140 | if (ct.encoding == display->xa[XA_INCR]) |
|
|
1141 | { |
|
|
1142 | // INCR selection, start handshake |
|
|
1143 | if (!delete_prop) |
|
|
1144 | XDeleteProperty (dpy, win, prop); |
|
|
1145 | |
|
|
1146 | selection_wait = Sel_incr; |
|
|
1147 | timer_ev.again (); |
|
|
1148 | |
|
|
1149 | goto bailout; |
|
|
1150 | } |
|
|
1151 | |
|
|
1152 | if (ct.nitems == 0) |
|
|
1153 | { |
|
|
1154 | if (selection_wait == Sel_incr) |
|
|
1155 | { |
|
|
1156 | XFree (ct.value); |
|
|
1157 | |
|
|
1158 | // finally complete, now paste the whole thing |
|
|
1159 | selection_wait = Sel_normal; |
|
|
1160 | ct.value = (unsigned char *)incr_buf; |
|
|
1161 | ct.nitems = incr_buf_fill; |
|
|
1162 | incr_buf = 0; |
|
|
1163 | timer_ev.stop (); |
|
|
1164 | } |
|
|
1165 | else |
|
|
1166 | { |
|
|
1167 | // avoid recursion |
|
|
1168 | if (win != display->root || prop != XA_CUT_BUFFER0) |
|
|
1169 | { |
|
|
1170 | XFree (ct.value); |
|
|
1171 | |
|
|
1172 | // fallback to CUT_BUFFER0 if the requested property |
|
|
1173 | // has an owner but is empty |
|
|
1174 | handle_selection (display->root, XA_CUT_BUFFER0, False); |
|
|
1175 | return; |
|
|
1176 | } |
|
|
1177 | |
|
|
1178 | goto bailout; |
|
|
1179 | } |
|
|
1180 | } |
|
|
1181 | else if (selection_wait == Sel_incr) |
|
|
1182 | { |
|
|
1183 | timer_ev.again (); |
|
|
1184 | |
|
|
1185 | while (incr_buf_fill + ct.nitems > incr_buf_size) |
|
|
1186 | { |
|
|
1187 | incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024; |
|
|
1188 | incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size); |
|
|
1189 | } |
|
|
1190 | |
|
|
1191 | memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems); |
|
|
1192 | incr_buf_fill += ct.nitems; |
|
|
1193 | |
|
|
1194 | goto bailout; |
|
|
1195 | } |
|
|
1196 | |
|
|
1197 | char **cl; |
|
|
1198 | int cr; |
|
|
1199 | |
|
|
1200 | // we honour the first item only |
|
|
1201 | |
|
|
1202 | #if !ENABLE_MINIMAL |
|
|
1203 | // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it |
|
|
1204 | // so recode it manually |
|
|
1205 | if (ct.encoding == display->xa[XA_UTF8_STRING]) |
|
|
1206 | { |
|
|
1207 | wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems); |
|
|
1208 | data = rxvt_wcstombs (w); |
|
|
1209 | free (w); |
|
|
1210 | } |
|
|
1211 | else |
|
|
1212 | #endif |
|
|
1213 | if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0 |
|
|
1214 | && cl) |
|
|
1215 | { |
|
|
1216 | data = strdup (cl[0]); |
|
|
1217 | XFreeStringList (cl); |
|
|
1218 | } |
|
|
1219 | else |
|
|
1220 | { |
|
|
1221 | // paste raw |
|
|
1222 | data = strdup ((const char *)ct.value); |
|
|
1223 | } |
|
|
1224 | |
|
|
1225 | data_len = strlen (data); |
|
|
1226 | |
|
|
1227 | bailout: |
|
|
1228 | XFree (ct.value); |
|
|
1229 | |
|
|
1230 | if (selection_wait == Sel_normal) |
|
|
1231 | { |
|
|
1232 | finish (data, data_len); |
|
|
1233 | free (data); |
|
|
1234 | } |
|
|
1235 | } |
|
|
1236 | |
|
|
1237 | void |
|
|
1238 | rxvt_selection::timer_cb (ev::timer &w, int revents) |
|
|
1239 | { |
|
|
1240 | if (selection_wait == Sel_incr) |
|
|
1241 | rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); |
|
|
1242 | |
|
|
1243 | finish (); |
|
|
1244 | } |
|
|
1245 | |
|
|
1246 | void |
|
|
1247 | rxvt_selection::x_cb (XEvent &xev) |
|
|
1248 | { |
|
|
1249 | switch (xev.type) |
|
|
1250 | { |
|
|
1251 | case PropertyNotify: |
|
|
1252 | if (selection_wait == Sel_incr |
|
|
1253 | && xev.xproperty.atom == request_prop |
|
|
1254 | && xev.xproperty.state == PropertyNewValue) |
|
|
1255 | handle_selection (xev.xproperty.window, xev.xproperty.atom, true); |
|
|
1256 | break; |
|
|
1257 | |
|
|
1258 | case SelectionNotify: |
|
|
1259 | if (selection_wait == Sel_normal |
|
|
1260 | && xev.xselection.time == request_time) |
|
|
1261 | { |
|
|
1262 | timer_ev.stop (); |
|
|
1263 | handle_selection (xev.xselection.requestor, xev.xselection.property, true); |
|
|
1264 | } |
|
|
1265 | break; |
|
|
1266 | } |
|
|
1267 | } |