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