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 |
37 | #if XFT |
38 | # include <X11/extensions/Xrender.h> |
38 | # include <X11/extensions/Xrender.h> |
39 | #endif |
39 | #endif |
40 | |
40 | |
41 | const char *const xa_names[] = |
41 | static const char *const xa_names[] = |
42 | { |
42 | { |
43 | "TEXT", |
43 | "TEXT", |
44 | "COMPOUND_TEXT", |
44 | "COMPOUND_TEXT", |
45 | "UTF8_STRING", |
45 | "UTF8_STRING", |
46 | "MULTIPLE", |
46 | "MULTIPLE", |
… | |
… | |
62 | #if ENABLE_EWMH |
62 | #if ENABLE_EWMH |
63 | "_NET_WM_PID", |
63 | "_NET_WM_PID", |
64 | "_NET_WM_NAME", |
64 | "_NET_WM_NAME", |
65 | "_NET_WM_ICON_NAME", |
65 | "_NET_WM_ICON_NAME", |
66 | "_NET_WM_PING", |
66 | "_NET_WM_PING", |
|
|
67 | "_NET_WM_ICON", |
67 | #endif |
68 | #endif |
68 | #if USE_XIM |
69 | #if USE_XIM |
69 | "WM_LOCALE_NAME", |
70 | "WM_LOCALE_NAME", |
70 | "XIM_SERVERS", |
71 | "XIM_SERVERS", |
71 | #endif |
72 | #endif |
72 | #ifdef TRANSPARENT |
73 | #if HAVE_IMG || ENABLE_PERL |
73 | "_XROOTPMAP_ID", |
74 | "_XROOTPMAP_ID", |
74 | "ESETROOT_PMAP_ID", |
75 | "ESETROOT_PMAP_ID", |
75 | #endif |
76 | #endif |
76 | #if ENABLE_XEMBED |
77 | #if ENABLE_XEMBED |
77 | "_XEMBED", |
78 | "_XEMBED", |
… | |
… | |
154 | put (*this->begin ()); |
155 | put (*this->begin ()); |
155 | } |
156 | } |
156 | |
157 | |
157 | ///////////////////////////////////////////////////////////////////////////// |
158 | ///////////////////////////////////////////////////////////////////////////// |
158 | |
159 | |
159 | #ifdef USE_XIM |
160 | #if USE_XIM |
160 | |
161 | |
161 | static void |
162 | static void |
162 | #if XIMCB_PROTO_BROKEN |
163 | #if XIMCB_PROTO_BROKEN |
163 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
164 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
164 | #else |
165 | #else |
… | |
… | |
186 | |
187 | |
187 | XIMCallback ximcallback; |
188 | XIMCallback ximcallback; |
188 | ximcallback.client_data = (XPointer)this; |
189 | ximcallback.client_data = (XPointer)this; |
189 | ximcallback.callback = im_destroy_cb; |
190 | ximcallback.callback = im_destroy_cb; |
190 | |
191 | |
191 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0); |
192 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0); |
192 | |
193 | |
193 | return true; |
194 | return true; |
194 | } |
195 | } |
195 | |
196 | |
196 | rxvt_xim::~rxvt_xim () |
197 | rxvt_xim::~rxvt_xim () |
… | |
… | |
229 | { |
230 | { |
230 | } |
231 | } |
231 | |
232 | |
232 | rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) |
233 | rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) |
233 | { |
234 | { |
234 | // it's actually faster to re-allocate every time. don't ask me |
|
|
235 | // why, but its likely no big deal there are no roundtrips |
|
|
236 | // (I think/hope). |
|
|
237 | if (!scratch_area || w > scratch_w || h > scratch_h || 1/*D*/) |
235 | if (!scratch_area || w > scratch_w || h > scratch_h) |
238 | { |
236 | { |
239 | if (scratch_area) |
237 | if (scratch_area) |
240 | { |
238 | { |
241 | XFreePixmap (dpy, scratch_area->drawable); |
239 | XFreePixmap (dpy, scratch_area->drawable); |
242 | delete scratch_area; |
240 | delete scratch_area; |
… | |
… | |
264 | depth = DefaultDepthOfScreen (screen); |
262 | depth = DefaultDepthOfScreen (screen); |
265 | visual = DefaultVisualOfScreen (screen); |
263 | visual = DefaultVisualOfScreen (screen); |
266 | cmap = DefaultColormapOfScreen (screen); |
264 | cmap = DefaultColormapOfScreen (screen); |
267 | } |
265 | } |
268 | |
266 | |
269 | void |
267 | #if ENABLE_FRILLS |
270 | rxvt_screen::set (rxvt_display *disp, int bitdepth) |
|
|
271 | { |
|
|
272 | set (disp); |
|
|
273 | |
268 | |
274 | #if XFT |
269 | void |
|
|
270 | rxvt_screen::select_visual (int id) |
|
|
271 | { |
275 | XVisualInfo vinfo; |
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; |
276 | |
293 | |
277 | if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) |
294 | if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) |
278 | { |
|
|
279 | depth = bitdepth; |
|
|
280 | visual = vinfo.visual; |
295 | select_visual (vinfo.visualid); |
281 | cmap = XCreateColormap (dpy, disp->root, visual, AllocNone); |
296 | else |
282 | } |
297 | rxvt_warn ("no visual found for requested depth %d, using default visual.\n", bitdepth); |
283 | #endif |
|
|
284 | } |
298 | } |
|
|
299 | |
|
|
300 | #endif |
285 | |
301 | |
286 | void |
302 | void |
287 | rxvt_screen::clear () |
303 | rxvt_screen::clear () |
288 | { |
304 | { |
289 | #if XFT |
305 | #if XFT |
… | |
… | |
300 | |
316 | |
301 | ///////////////////////////////////////////////////////////////////////////// |
317 | ///////////////////////////////////////////////////////////////////////////// |
302 | |
318 | |
303 | rxvt_display::rxvt_display (const char *id) |
319 | rxvt_display::rxvt_display (const char *id) |
304 | : refcounted (id) |
320 | : refcounted (id) |
305 | , x_ev (this, &rxvt_display::x_cb) |
|
|
306 | , selection_owner (0) |
321 | , selection_owner (0) |
|
|
322 | , clipboard_owner (0) |
307 | { |
323 | { |
|
|
324 | x_ev .set<rxvt_display, &rxvt_display::x_cb > (this); |
|
|
325 | flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this); |
308 | } |
326 | } |
309 | |
327 | |
310 | XrmDatabase |
328 | XrmDatabase |
311 | rxvt_display::get_resources (bool refresh) |
329 | rxvt_display::get_resources (bool refresh) |
312 | { |
330 | { |
313 | char *homedir = (char *)getenv ("HOME"); |
331 | char *homedir = getenv ("HOME"); |
314 | char fname[1024]; |
332 | char fname[1024]; |
315 | |
333 | |
316 | /* |
334 | /* |
317 | * get resources using the X library function |
335 | * get resources using the X library function |
318 | */ |
336 | */ |
319 | char *displayResource, *xe; |
337 | char *displayResource, *xe; |
320 | XrmDatabase rdb1, database = 0; |
338 | XrmDatabase rdb1, database = 0; |
321 | |
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 | |
322 | // 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 |
323 | |
347 | |
324 | // 6. System wide per application default file. |
348 | // 6. System wide per application default file. |
325 | |
349 | |
326 | /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ |
350 | /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ |
327 | if ((xe = (char *)getenv ("XAPPLRESDIR"))) |
351 | if ((xe = getenv ("XAPPLRESDIR"))) |
328 | { |
352 | { |
329 | snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); |
353 | snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); |
330 | |
354 | |
331 | if ((rdb1 = XrmGetFileDatabase (fname))) |
355 | if ((rdb1 = XrmGetFileDatabase (fname))) |
332 | XrmMergeDatabases (rdb1, &database); |
356 | XrmMergeDatabases (rdb1, &database); |
… | |
… | |
334 | |
358 | |
335 | // 5. User's per application default file. |
359 | // 5. User's per application default file. |
336 | // none |
360 | // none |
337 | |
361 | |
338 | // 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 | |
339 | /* Get any Xserver defaults */ |
371 | /* Get any Xserver defaults */ |
340 | if (refresh) |
372 | if (refresh) |
341 | { |
373 | { |
342 | // fucking xlib keeps a copy of the rm string |
374 | // fucking xlib keeps a copy of the rm string |
343 | Atom actual_type; |
375 | Atom actual_type; |
… | |
… | |
348 | #if XLIB_ILLEGAL_ACCESS |
380 | #if XLIB_ILLEGAL_ACCESS |
349 | if (dpy->xdefaults) |
381 | if (dpy->xdefaults) |
350 | XFree (dpy->xdefaults); |
382 | XFree (dpy->xdefaults); |
351 | #endif |
383 | #endif |
352 | |
384 | |
353 | if (XGetWindowProperty (dpy, root, XA_RESOURCE_MANAGER, |
385 | if (XGetWindowProperty (dpy, RootWindow (dpy, 0), XA_RESOURCE_MANAGER, |
354 | 0L, 100000000L, False, |
386 | 0L, 100000000L, False, |
355 | XA_STRING, &actual_type, &actual_format, |
387 | XA_STRING, &actual_type, &actual_format, |
356 | &nitems, &nremaining, |
388 | &nitems, &nremaining, |
357 | (unsigned char **)&val) == Success |
389 | (unsigned char **)&val) == Success |
358 | && actual_type == XA_STRING |
390 | && actual_type == XA_STRING |
359 | && actual_format == 8) |
391 | && actual_format == 8) |
360 | displayResource = val; |
392 | displayResource = val; |
361 | else |
393 | else |
362 | { |
394 | { |
363 | displayResource = 0; |
395 | displayResource = 0; |
|
|
396 | |
364 | if (val) |
397 | if (val) |
365 | XFree(val); |
398 | XFree (val); |
366 | } |
399 | } |
367 | |
400 | |
368 | #if XLIB_ILLEGAL_ACCESS |
401 | #if XLIB_ILLEGAL_ACCESS |
369 | dpy->xdefaults = displayResource; |
402 | dpy->xdefaults = displayResource; |
370 | #endif |
403 | #endif |
371 | } |
404 | } |
… | |
… | |
373 | displayResource = XResourceManagerString (dpy); |
406 | displayResource = XResourceManagerString (dpy); |
374 | |
407 | |
375 | if (displayResource) |
408 | if (displayResource) |
376 | { |
409 | { |
377 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
410 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
378 | XrmMergeDatabases (rdb1, &database); |
|
|
379 | } |
|
|
380 | else if (homedir) |
|
|
381 | { |
|
|
382 | snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); |
|
|
383 | |
|
|
384 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
385 | XrmMergeDatabases (rdb1, &database); |
411 | XrmMergeDatabases (rdb1, &database); |
386 | } |
412 | } |
387 | |
413 | |
388 | #if !XLIB_ILLEGAL_ACCESS |
414 | #if !XLIB_ILLEGAL_ACCESS |
389 | if (refresh && displayResource) |
415 | if (refresh && displayResource) |
… | |
… | |
402 | XFree (displayResource); |
428 | XFree (displayResource); |
403 | } |
429 | } |
404 | |
430 | |
405 | // 3. User's per host defaults file |
431 | // 3. User's per host defaults file |
406 | /* Add in XENVIRONMENT file */ |
432 | /* Add in XENVIRONMENT file */ |
407 | if ((xe = (char *)getenv ("XENVIRONMENT")) |
433 | if ((xe = getenv ("XENVIRONMENT")) |
408 | && (rdb1 = XrmGetFileDatabase (xe))) |
434 | && (rdb1 = XrmGetFileDatabase (xe))) |
409 | XrmMergeDatabases (rdb1, &database); |
435 | XrmMergeDatabases (rdb1, &database); |
410 | else if (homedir) |
436 | else if (homedir) |
411 | { |
437 | { |
412 | struct utsname un; |
438 | struct utsname un; |
… | |
… | |
426 | bool rxvt_display::ref_init () |
452 | bool rxvt_display::ref_init () |
427 | { |
453 | { |
428 | #ifdef LOCAL_X_IS_UNIX |
454 | #ifdef LOCAL_X_IS_UNIX |
429 | if (id[0] == ':') |
455 | if (id[0] == ':') |
430 | { |
456 | { |
431 | val = rxvt_malloc (5 + strlen (id) + 1); |
457 | char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1); |
|
|
458 | |
432 | strcpy (val, "unix/"); |
459 | strcpy (val, "unix/"); |
433 | strcat (val, id); |
460 | strcat (val, id); |
|
|
461 | |
434 | dpy = XOpenDisplay (val); |
462 | dpy = XOpenDisplay (val); |
435 | free (val); |
|
|
436 | } |
463 | } |
437 | else |
464 | else |
438 | #endif |
465 | #endif |
439 | dpy = 0; |
466 | dpy = 0; |
440 | |
467 | |
… | |
… | |
445 | return false; |
472 | return false; |
446 | |
473 | |
447 | screen = DefaultScreen (dpy); |
474 | screen = DefaultScreen (dpy); |
448 | root = DefaultRootWindow (dpy); |
475 | root = DefaultRootWindow (dpy); |
449 | |
476 | |
450 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
477 | assert (ecb_array_length (xa_names) == NUM_XA); |
451 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
478 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
452 | |
479 | |
453 | XrmSetDatabase (dpy, get_resources (false)); |
480 | XrmSetDatabase (dpy, get_resources (false)); |
454 | |
481 | |
455 | #ifdef POINTER_BLANK |
482 | #ifdef POINTER_BLANK |
… | |
… | |
461 | blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', |
488 | blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', |
462 | &blackcolour, &blackcolour); |
489 | &blackcolour, &blackcolour); |
463 | XUnloadFont (dpy, f); |
490 | XUnloadFont (dpy, f); |
464 | #endif |
491 | #endif |
465 | |
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 | |
466 | int fd = XConnectionNumber (dpy); |
512 | int fd = XConnectionNumber (dpy); |
467 | |
513 | |
468 | #ifndef NO_SLOW_LINK_SUPPORT |
|
|
469 | // try to detect wether we have a local connection. |
514 | // try to detect whether we have a local connection. |
470 | // assume unix domains socket == local, everything else not |
515 | // assume unix domain socket == local, everything else not |
471 | // TODO: might want to check for inet/127.0.0.1 |
516 | // TODO: might want to check for inet/127.0.0.1 |
472 | is_local = 0; |
517 | is_local = 0; |
473 | sockaddr_un sa; |
518 | sockaddr_un sa; |
474 | socklen_t sl = sizeof (sa); |
519 | socklen_t sl = sizeof (sa); |
475 | |
520 | |
476 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
521 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
477 | is_local = sa.sun_family == AF_LOCAL; |
522 | is_local = sa.sun_family == AF_UNIX; |
478 | #endif |
|
|
479 | |
523 | |
|
|
524 | flush_ev.start (); |
480 | x_ev.start (fd, EVENT_READ); |
525 | x_ev.start (fd, ev::READ); |
481 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
526 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
482 | |
527 | |
483 | XSelectInput (dpy, root, PropertyChangeMask); |
528 | XSelectInput (dpy, root, PropertyChangeMask); |
484 | |
529 | |
485 | flush (); |
530 | flush (); |
… | |
… | |
488 | } |
533 | } |
489 | |
534 | |
490 | void |
535 | void |
491 | rxvt_display::ref_next () |
536 | rxvt_display::ref_next () |
492 | { |
537 | { |
493 | // TODO: somehow check wether the database files/resources changed |
538 | // TODO: somehow check whether the database files/resources changed |
494 | // before affording re-loading/parsing |
539 | // before affording re-loading/parsing |
495 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
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 |
496 | XrmSetDatabase (dpy, get_resources (true)); |
545 | XrmSetDatabase (dpy, get_resources (true)); |
497 | } |
546 | } |
498 | |
547 | |
499 | rxvt_display::~rxvt_display () |
548 | rxvt_display::~rxvt_display () |
500 | { |
549 | { |
… | |
… | |
503 | |
552 | |
504 | #ifdef POINTER_BLANK |
553 | #ifdef POINTER_BLANK |
505 | XFreeCursor (dpy, blank_cursor); |
554 | XFreeCursor (dpy, blank_cursor); |
506 | #endif |
555 | #endif |
507 | x_ev.stop (); |
556 | x_ev.stop (); |
|
|
557 | flush_ev.stop (); |
508 | #ifdef USE_XIM |
558 | #if USE_XIM |
509 | xims.clear (); |
559 | xims.clear (); |
510 | #endif |
560 | #endif |
|
|
561 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
511 | XCloseDisplay (dpy); |
562 | XCloseDisplay (dpy); |
512 | } |
563 | } |
513 | |
564 | |
514 | #ifdef USE_XIM |
565 | #if USE_XIM |
515 | void rxvt_display::im_change_cb () |
566 | void rxvt_display::im_change_cb () |
516 | { |
567 | { |
517 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
568 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
518 | (*i)->call (); |
569 | (*i)->call (); |
519 | } |
570 | } |
… | |
… | |
527 | unsigned long nitems, bytes_after; |
578 | unsigned long nitems, bytes_after; |
528 | |
579 | |
529 | if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
580 | if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
530 | False, XA_ATOM, &actual_type, &actual_format, |
581 | False, XA_ATOM, &actual_type, &actual_format, |
531 | &nitems, &bytes_after, (unsigned char **)&atoms) |
582 | &nitems, &bytes_after, (unsigned char **)&atoms) |
532 | != Success ) |
583 | != Success) |
533 | return; |
584 | return; |
534 | |
585 | |
535 | if (actual_type == XA_ATOM && actual_format == 32) |
586 | if (actual_type == XA_ATOM && actual_format == 32) |
536 | for (int i = 0; i < nitems; i++) |
587 | for (int i = 0; i < nitems; i++) |
537 | if (XGetSelectionOwner (dpy, atoms[i])) |
588 | if (XGetSelectionOwner (dpy, atoms[i])) |
538 | { |
589 | { |
539 | im_change_cb (); |
590 | im_change_cb (); |
540 | break; |
591 | break; |
… | |
… | |
542 | |
593 | |
543 | XFree (atoms); |
594 | XFree (atoms); |
544 | } |
595 | } |
545 | #endif |
596 | #endif |
546 | |
597 | |
547 | void rxvt_display::x_cb (io_watcher &w, short revents) |
598 | void rxvt_display::x_cb (ev::io &w, int revents) |
548 | { |
599 | { |
|
|
600 | flush_ev.start (); |
|
|
601 | } |
|
|
602 | |
|
|
603 | void rxvt_display::flush_cb (ev::prepare &w, int revents) |
|
|
604 | { |
|
|
605 | while (XEventsQueued (dpy, QueuedAfterFlush)) |
549 | do |
606 | do |
550 | { |
607 | { |
551 | XEvent xev; |
608 | XEvent xev; |
552 | XNextEvent (dpy, &xev); |
609 | XNextEvent (dpy, &xev); |
553 | |
610 | |
554 | #ifdef USE_XIM |
611 | #if USE_XIM |
555 | if (!XFilterEvent (&xev, None)) |
612 | if (!XFilterEvent (&xev, None)) |
556 | { |
613 | { |
557 | if (xev.type == PropertyNotify |
614 | if (xev.type == PropertyNotify |
558 | && xev.xany.window == root |
615 | && xev.xany.window == root |
559 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
616 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
560 | im_change_check (); |
617 | im_change_check (); |
561 | #endif |
618 | #endif |
|
|
619 | if (xev.type == MappingNotify) |
|
|
620 | XRefreshKeyboardMapping (&xev.xmapping); |
|
|
621 | |
562 | for (int i = xw.size (); i--; ) |
622 | for (int i = xw.size (); i--; ) |
563 | { |
623 | { |
564 | if (!xw[i]) |
624 | if (!xw[i]) |
565 | xw.erase_unordered (i); |
625 | xw.erase_unordered (i); |
566 | else if (xw[i]->window == xev.xany.window) |
626 | else if (xw[i]->window == xev.xany.window) |
567 | xw[i]->call (xev); |
627 | xw[i]->call (xev); |
568 | } |
628 | } |
569 | #ifdef USE_XIM |
629 | #if USE_XIM |
570 | } |
630 | } |
571 | #endif |
631 | #endif |
572 | } |
632 | } |
573 | while (XEventsQueued (dpy, QueuedAlready)); |
633 | while (XEventsQueued (dpy, QueuedAlready)); |
574 | |
634 | |
575 | XFlush (dpy); |
635 | w.stop (); |
576 | } |
|
|
577 | |
|
|
578 | void rxvt_display::flush () |
|
|
579 | { |
|
|
580 | if (XEventsQueued (dpy, QueuedAlready)) |
|
|
581 | x_cb (x_ev, EVENT_READ); |
|
|
582 | |
|
|
583 | XFlush (dpy); |
|
|
584 | } |
636 | } |
585 | |
637 | |
586 | void rxvt_display::reg (xevent_watcher *w) |
638 | void rxvt_display::reg (xevent_watcher *w) |
587 | { |
639 | { |
|
|
640 | if (!w->active) |
|
|
641 | { |
588 | xw.push_back (w); |
642 | xw.push_back (w); |
589 | w->active = xw.size (); |
643 | w->active = xw.size (); |
|
|
644 | } |
590 | } |
645 | } |
591 | |
646 | |
592 | void rxvt_display::unreg (xevent_watcher *w) |
647 | void rxvt_display::unreg (xevent_watcher *w) |
593 | { |
648 | { |
594 | if (w->active) |
649 | if (w->active) |
|
|
650 | { |
595 | xw[w->active - 1] = 0; |
651 | xw[w->active - 1] = 0; |
|
|
652 | w->active = 0; |
|
|
653 | } |
596 | } |
654 | } |
597 | |
655 | |
598 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
656 | void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard) |
599 | { |
657 | { |
|
|
658 | rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner; |
|
|
659 | |
600 | if (selection_owner && selection_owner != owner) |
660 | if (cur_owner && cur_owner != owner) |
601 | selection_owner->selection_clear (); |
661 | { |
|
|
662 | rxvt_term *term = cur_owner; |
|
|
663 | term->selection_clear (clipboard); |
|
|
664 | term->flush (); |
|
|
665 | } |
602 | |
666 | |
603 | selection_owner = owner; |
667 | cur_owner = owner; |
604 | } |
668 | } |
605 | |
669 | |
606 | #ifdef USE_XIM |
670 | #if USE_XIM |
607 | |
671 | |
608 | void rxvt_display::reg (im_watcher *w) |
672 | void rxvt_display::reg (im_watcher *w) |
609 | { |
673 | { |
610 | imw.push_back (w); |
674 | imw.push_back (w); |
611 | } |
675 | } |
… | |
… | |
621 | int l, m; |
685 | int l, m; |
622 | |
686 | |
623 | l = strlen (locale); |
687 | l = strlen (locale); |
624 | m = strlen (modifiers); |
688 | m = strlen (modifiers); |
625 | |
689 | |
626 | if (!(id = (char *)malloc (l + m + 2))) |
690 | if (!(id = rxvt_temp_buf<char> (l + m + 2))) |
627 | return 0; |
691 | return 0; |
628 | |
692 | |
629 | memcpy (id, locale, l); id[l] = '\n'; |
693 | memcpy (id, locale, l); id[l] = '\n'; |
630 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
694 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
631 | |
695 | |
632 | rxvt_xim *xim = xims.get (id); |
696 | rxvt_xim *xim = xims.get (id); |
633 | |
|
|
634 | free (id); |
|
|
635 | |
697 | |
636 | return xim; |
698 | return xim; |
637 | } |
699 | } |
638 | |
700 | |
639 | void rxvt_display::put_xim (rxvt_xim *xim) |
701 | void rxvt_display::put_xim (rxvt_xim *xim) |
… | |
… | |
648 | Atom rxvt_display::atom (const char *name) |
710 | Atom rxvt_display::atom (const char *name) |
649 | { |
711 | { |
650 | return XInternAtom (dpy, name, False); |
712 | return XInternAtom (dpy, name, False); |
651 | } |
713 | } |
652 | |
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; |
|
|
735 | } |
|
|
736 | |
653 | ///////////////////////////////////////////////////////////////////////////// |
737 | ///////////////////////////////////////////////////////////////////////////// |
654 | |
738 | |
655 | template class refcache<rxvt_display>; |
739 | template class refcache<rxvt_display>; |
656 | refcache<rxvt_display> displays; |
740 | refcache<rxvt_display> displays; |
657 | |
741 | |
658 | ///////////////////////////////////////////////////////////////////////////// |
742 | ///////////////////////////////////////////////////////////////////////////// |
659 | |
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 | |
660 | bool |
751 | bool |
661 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
752 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
662 | { |
753 | { |
|
|
754 | //TODO: only supports 24 bit |
|
|
755 | unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
|
|
756 | |
663 | #if XFT |
757 | #if XFT |
664 | XRenderPictFormat *format; |
758 | XRenderPictFormat *format; |
|
|
759 | |
|
|
760 | // not needed by XftColorAlloc, but by the other paths (ours |
|
|
761 | // and fallback), so just set all components here. |
|
|
762 | c.color.red = color.r; |
|
|
763 | c.color.green = color.g; |
|
|
764 | c.color.blue = color.b; |
|
|
765 | c.color.alpha = alpha; |
665 | |
766 | |
666 | // FUCKING Xft gets it wrong, of course, so work around it. |
767 | // FUCKING Xft gets it wrong, of course, so work around it. |
667 | // Transparency users should eat shit and die, and then |
768 | // Transparency users should eat shit and die, and then |
668 | // XRenderQueryPictIndexValues themselves plenty. |
769 | // XRenderQueryPictIndexValues themselves plenty. |
669 | if ((screen->visual->c_class == TrueColor) |
770 | if ((screen->visual->c_class == TrueColor) |
670 | && (format = XRenderFindVisualFormat (screen->dpy, screen->visual))) |
771 | && (format = XRenderFindVisualFormat (screen->dpy, screen->visual))) |
671 | { |
772 | { |
672 | // the fun lies in doing everything manually... |
773 | // the fun lies in doing everything manually... |
673 | c.color.red = color.r; |
|
|
674 | c.color.green = color.g; |
|
|
675 | c.color.blue = color.b; |
|
|
676 | c.color.alpha = color.a; |
|
|
677 | |
774 | |
|
|
775 | // Xft wants premultiplied alpha, but abuses the alpha channel |
|
|
776 | // as blend factor, and doesn't allow us to set the alpha channel |
|
|
777 | c.color.red = c.color.red * alpha / 0xffff; |
|
|
778 | c.color.green = c.color.green * alpha / 0xffff; |
|
|
779 | c.color.blue = c.color.blue * alpha / 0xffff; |
|
|
780 | |
678 | c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red ) |
781 | c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red ) |
679 | | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green) |
782 | | insert_component (c.color.green, format->direct.greenMask, format->direct.green) |
680 | | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue ) |
783 | | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue ) |
681 | | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha); |
784 | | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); |
682 | |
785 | |
683 | return true; |
786 | return true; |
684 | } |
787 | } |
685 | else |
788 | else |
686 | { |
789 | { |
687 | XRenderColor d; |
790 | XRenderColor d; |
688 | |
791 | |
689 | d.red = color.r; |
792 | d.red = color.r; |
690 | d.green = color.g; |
793 | d.green = color.g; |
691 | d.blue = color.b; |
794 | d.blue = color.b; |
692 | d.alpha = color.a; |
795 | d.alpha = alpha; |
693 | |
796 | |
|
|
797 | // XftColorAlloc always returns 100% transparent pixels(!) |
694 | return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); |
798 | if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c)) |
|
|
799 | return true; |
695 | } |
800 | } |
696 | #else |
801 | #else |
697 | c.red = color.r; |
802 | c.red = color.r; |
698 | c.green = color.g; |
803 | c.green = color.g; |
699 | c.blue = color.b; |
804 | c.blue = color.b; |
700 | |
805 | |
701 | if (screen->visual->c_class == TrueColor) |
806 | if (screen->visual->c_class == TrueColor) |
702 | { |
807 | { |
703 | c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
808 | c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask )) |
704 | / rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
809 | | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask)) |
705 | | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
|
|
706 | / rgba::MAX_CC) << ctz (screen->visual->green_mask) |
|
|
707 | | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
810 | | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask )); |
708 | / rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
|
|
709 | |
811 | |
710 | return true; |
812 | return true; |
711 | } |
813 | } |
712 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
814 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
713 | return true; |
815 | return true; |
714 | else |
816 | #endif |
|
|
817 | |
715 | c.pixel = (color.r + color.g + color.b) > 128*3 |
818 | c.pixel = (color.r * 2 + color.g * 3 + color.b) >= 0x8000 * 6 |
716 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
819 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
717 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
820 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
718 | #endif |
|
|
719 | |
821 | |
720 | return false; |
822 | return false; |
721 | } |
823 | } |
722 | |
824 | |
723 | bool |
825 | bool |
724 | rxvt_color::set (rxvt_screen *screen, const char *name) |
826 | rxvt_color::set (rxvt_screen *screen, const char *name) |
725 | { |
827 | { |
726 | rgba c; |
828 | rgba c; |
727 | char eos; |
829 | char eos; |
728 | int skip; |
830 | int skip = 0; |
|
|
831 | |
|
|
832 | c.a = rgba::MAX_CC; |
729 | |
833 | |
730 | // parse the nonstandard "[alphapercent]" prefix |
834 | // parse the nonstandard "[alphapercent]" prefix |
731 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
835 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
732 | { |
836 | { |
733 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
837 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
734 | name += skip; |
838 | name += skip; |
735 | } |
839 | } |
736 | else |
|
|
737 | c.a = rgba::MAX_CC; |
|
|
738 | |
840 | |
739 | // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format |
841 | // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format |
740 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
842 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
741 | { |
843 | { |
742 | XColor xc, xc_exact; |
844 | XColor xc; |
743 | |
845 | |
744 | if (XParseColor (screen->dpy, screen->cmap, name, &xc)) |
846 | if (XParseColor (screen->dpy, screen->cmap, name, &xc)) |
745 | { |
847 | { |
746 | c.r = xc.red; |
848 | c.r = xc.red; |
747 | c.g = xc.green; |
849 | c.g = xc.green; |
… | |
… | |
774 | { |
876 | { |
775 | XColor *colors = new XColor [screen->visual->map_entries]; |
877 | XColor *colors = new XColor [screen->visual->map_entries]; |
776 | |
878 | |
777 | for (int i = 0; i < cmap_size; i++) |
879 | for (int i = 0; i < cmap_size; i++) |
778 | colors [i].pixel = i; |
880 | colors [i].pixel = i; |
779 | |
881 | |
780 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
882 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
781 | // many extra optimisations. |
883 | // many extra optimisations. |
782 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
884 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
783 | |
885 | |
|
|
886 | while (cmap_size) |
|
|
887 | { |
784 | int diff = 0x7fffffffUL; |
888 | int diff = 0x7fffffffL; |
785 | XColor *best = colors; |
889 | XColor *best = colors; |
786 | |
890 | |
787 | for (int i = 0; i < cmap_size; i++) |
891 | for (int i = 0; i < cmap_size; i++) |
788 | { |
|
|
789 | int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2)) |
|
|
790 | + (squared_diff<int> (color.g >> 2, colors [i].green >> 2)) |
|
|
791 | + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2)); |
|
|
792 | |
|
|
793 | if (d < diff) |
|
|
794 | { |
892 | { |
|
|
893 | // simple weighted rgb distance sucks, but keeps it simple |
|
|
894 | int d = abs (color.r - colors [i].red ) * 2 |
|
|
895 | + abs (color.g - colors [i].green) * 3 |
|
|
896 | + abs (color.b - colors [i].blue ); |
|
|
897 | |
|
|
898 | if (d < diff) |
|
|
899 | { |
795 | diff = d; |
900 | diff = d; |
796 | best = colors + i; |
901 | best = colors + i; |
|
|
902 | } |
797 | } |
903 | } |
798 | } |
|
|
799 | |
904 | |
800 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
905 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d,%d)\n", |
801 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
906 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff, best - colors); |
802 | |
907 | |
803 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
908 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
804 | |
909 | |
|
|
910 | if (got) |
|
|
911 | break; |
|
|
912 | |
|
|
913 | *best = colors [--cmap_size]; |
|
|
914 | } |
|
|
915 | |
805 | delete colors; |
916 | delete [] colors; |
806 | } |
917 | } |
807 | #endif |
918 | #endif |
808 | |
919 | |
809 | return got; |
920 | return got; |
810 | } |
921 | } |
811 | |
922 | |
812 | void |
923 | void |
813 | rxvt_color::get (rgba &color) |
924 | rxvt_color::get (rgba &color) const |
814 | { |
925 | { |
815 | #if XFT |
926 | #if XFT |
|
|
927 | |
816 | color.r = c.color.red; |
928 | color.r = c.color.red; |
817 | color.g = c.color.green; |
929 | color.g = c.color.green; |
818 | color.b = c.color.blue; |
930 | color.b = c.color.blue; |
819 | color.a = c.color.alpha; |
931 | color.a = c.color.alpha; |
|
|
932 | |
|
|
933 | if (IN_RANGE_INC (color.a, 0x0001, 0xfffe)) |
|
|
934 | { |
|
|
935 | color.r = color.r * 0xffff / color.a; |
|
|
936 | color.g = color.g * 0xffff / color.a; |
|
|
937 | color.b = color.b * 0xffff / color.a; |
|
|
938 | } |
|
|
939 | |
820 | #else |
940 | #else |
|
|
941 | |
821 | color.r = c.red; |
942 | color.r = c.red; |
822 | color.g = c.green; |
943 | color.g = c.green; |
823 | color.b = c.blue; |
944 | color.b = c.blue; |
824 | color.a = rgba::MAX_CC; |
945 | color.a = rgba::MAX_CC; |
825 | #endif |
|
|
826 | } |
|
|
827 | |
946 | |
|
|
947 | #endif |
|
|
948 | } |
|
|
949 | |
828 | void |
950 | void |
829 | rxvt_color::get (XColor &color) |
951 | rxvt_color::get (XColor &color) const |
830 | { |
952 | { |
831 | rgba c; |
953 | rgba c; |
832 | get (c); |
954 | get (c); |
833 | |
955 | |
834 | color.red = c.r; |
956 | color.red = c.r; |
835 | color.green = c.g; |
957 | color.green = c.g; |
836 | color.blue = c.b; |
958 | color.blue = c.b; |
837 | color.pixel = (Pixel)*this; |
959 | color.pixel = (Pixel)*this; |
838 | } |
960 | } |
839 | |
961 | |
840 | void |
962 | void |
841 | rxvt_color::free (rxvt_screen *screen) |
963 | rxvt_color::free (rxvt_screen *screen) |
842 | { |
964 | { |
|
|
965 | if (screen->visual->c_class == TrueColor) |
|
|
966 | return; // nothing to do |
|
|
967 | |
843 | #if XFT |
968 | #if XFT |
844 | XftColorFree (screen->dpy, screen->visual, screen->cmap, &c); |
969 | XftColorFree (screen->dpy, screen->visual, screen->cmap, &c); |
845 | #else |
970 | #else |
846 | XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes); |
971 | XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes); |
847 | #endif |
972 | #endif |
… | |
… | |
862 | lerp (c.a, to.a, percent) |
987 | lerp (c.a, to.a, percent) |
863 | ) |
988 | ) |
864 | ); |
989 | ); |
865 | } |
990 | } |
866 | |
991 | |
|
|
992 | rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term) |
|
|
993 | : display (disp), request_time (tm), request_win (win), request_prop (prop), term (term) |
|
|
994 | { |
|
|
995 | assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); |
|
|
996 | |
|
|
997 | timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this); |
|
|
998 | timer_ev.repeat = 10.; |
|
|
999 | x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this); |
|
|
1000 | |
|
|
1001 | incr_buf = 0; |
|
|
1002 | incr_buf_size = incr_buf_fill = 0; |
|
|
1003 | selection_wait = Sel_normal; |
|
|
1004 | selection_type = selnum; |
|
|
1005 | cb_sv = 0; |
|
|
1006 | } |
|
|
1007 | |
|
|
1008 | void |
|
|
1009 | rxvt_selection::stop () |
|
|
1010 | { |
|
|
1011 | free (incr_buf); |
|
|
1012 | incr_buf = 0; |
|
|
1013 | timer_ev.stop (); |
|
|
1014 | x_ev.stop (display); |
|
|
1015 | } |
|
|
1016 | |
|
|
1017 | rxvt_selection::~rxvt_selection () |
|
|
1018 | { |
|
|
1019 | stop (); |
|
|
1020 | } |
|
|
1021 | |
|
|
1022 | void |
|
|
1023 | rxvt_selection::run () |
|
|
1024 | { |
|
|
1025 | int selnum = selection_type; |
|
|
1026 | |
|
|
1027 | #if ENABLE_FRILLS |
|
|
1028 | if (selnum == Sel_Primary && display->selection_owner) |
|
|
1029 | { |
|
|
1030 | /* internal selection */ |
|
|
1031 | char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len); |
|
|
1032 | finish (str, strlen (str)); |
|
|
1033 | free (str); |
|
|
1034 | return; |
|
|
1035 | } |
|
|
1036 | #endif |
|
|
1037 | |
|
|
1038 | #if X_HAVE_UTF8_STRING |
|
|
1039 | selection_type = Sel_UTF8String; |
|
|
1040 | if (request (display->xa[XA_UTF8_STRING], selnum)) |
|
|
1041 | return; |
|
|
1042 | #else |
|
|
1043 | selection_type = Sel_CompoundText; |
|
|
1044 | if (request (display->xa[XA_COMPOUND_TEXT], selnum)) |
|
|
1045 | return; |
|
|
1046 | #endif |
|
|
1047 | |
|
|
1048 | // fallback to CUT_BUFFER0 if the requested property has no owner |
|
|
1049 | handle_selection (display->root, XA_CUT_BUFFER0, false); |
|
|
1050 | } |
|
|
1051 | |
|
|
1052 | void |
|
|
1053 | rxvt_selection::finish (char *data, unsigned int len) |
|
|
1054 | { |
|
|
1055 | if (!cb_sv) |
|
|
1056 | { |
|
|
1057 | if (data) |
|
|
1058 | term->paste (data, len); |
|
|
1059 | |
|
|
1060 | term->selection_req = 0; |
|
|
1061 | delete this; |
|
|
1062 | } |
|
|
1063 | #if ENABLE_PERL |
|
|
1064 | else |
|
|
1065 | { |
|
|
1066 | stop (); // we do not really trust perl callbacks |
|
|
1067 | rxvt_perl.selection_finish (this, data, len); |
|
|
1068 | } |
|
|
1069 | #endif |
|
|
1070 | } |
|
|
1071 | |
|
|
1072 | bool |
|
|
1073 | rxvt_selection::request (Atom target, int selnum) |
|
|
1074 | { |
|
|
1075 | Atom sel; |
|
|
1076 | |
|
|
1077 | selection_type |= selnum; |
|
|
1078 | |
|
|
1079 | if (selnum == Sel_Primary) |
|
|
1080 | sel = XA_PRIMARY; |
|
|
1081 | else if (selnum == Sel_Secondary) |
|
|
1082 | sel = XA_SECONDARY; |
|
|
1083 | else |
|
|
1084 | sel = display->xa[XA_CLIPBOARD]; |
|
|
1085 | |
|
|
1086 | if (XGetSelectionOwner (display->dpy, sel) != None) |
|
|
1087 | { |
|
|
1088 | XConvertSelection (display->dpy, sel, target, request_prop, |
|
|
1089 | request_win, request_time); |
|
|
1090 | x_ev.start (display, request_win); |
|
|
1091 | timer_ev.again (); |
|
|
1092 | return true; |
|
|
1093 | } |
|
|
1094 | |
|
|
1095 | return false; |
|
|
1096 | } |
|
|
1097 | |
|
|
1098 | void |
|
|
1099 | rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop) |
|
|
1100 | { |
|
|
1101 | Display *dpy = display->dpy; |
|
|
1102 | char *data = 0; |
|
|
1103 | unsigned int data_len = 0; |
|
|
1104 | unsigned long bytes_after; |
|
|
1105 | XTextProperty ct; |
|
|
1106 | |
|
|
1107 | // check for failed XConvertSelection |
|
|
1108 | if (prop == None) |
|
|
1109 | { |
|
|
1110 | bool error = true; |
|
|
1111 | int selnum = selection_type & Sel_whereMask; |
|
|
1112 | |
|
|
1113 | if (selection_type & Sel_CompoundText) |
|
|
1114 | { |
|
|
1115 | selection_type = 0; |
|
|
1116 | error = !request (XA_STRING, selnum); |
|
|
1117 | } |
|
|
1118 | |
|
|
1119 | if (selection_type & Sel_UTF8String) |
|
|
1120 | { |
|
|
1121 | selection_type = Sel_CompoundText; |
|
|
1122 | error = !request (display->xa[XA_COMPOUND_TEXT], selnum); |
|
|
1123 | } |
|
|
1124 | |
|
|
1125 | if (error) |
|
|
1126 | { |
|
|
1127 | ct.value = 0; |
|
|
1128 | goto bailout; |
|
|
1129 | } |
|
|
1130 | |
|
|
1131 | return; |
|
|
1132 | } |
|
|
1133 | |
|
|
1134 | // length == (2^31 - 1) / 4, as gdk |
|
|
1135 | if (XGetWindowProperty (dpy, win, prop, |
|
|
1136 | 0, 0x1fffffff, |
|
|
1137 | delete_prop, AnyPropertyType, |
|
|
1138 | &ct.encoding, &ct.format, |
|
|
1139 | &ct.nitems, &bytes_after, |
|
|
1140 | &ct.value) != Success) |
|
|
1141 | { |
|
|
1142 | ct.value = 0; |
|
|
1143 | goto bailout; |
|
|
1144 | } |
|
|
1145 | |
|
|
1146 | if (ct.encoding == None) |
|
|
1147 | goto bailout; |
|
|
1148 | |
|
|
1149 | if (ct.value == 0) |
|
|
1150 | goto bailout; |
|
|
1151 | |
|
|
1152 | if (ct.encoding == display->xa[XA_INCR]) |
|
|
1153 | { |
|
|
1154 | // INCR selection, start handshake |
|
|
1155 | if (!delete_prop) |
|
|
1156 | XDeleteProperty (dpy, win, prop); |
|
|
1157 | |
|
|
1158 | selection_wait = Sel_incr; |
|
|
1159 | timer_ev.again (); |
|
|
1160 | |
|
|
1161 | goto bailout; |
|
|
1162 | } |
|
|
1163 | |
|
|
1164 | if (ct.nitems == 0) |
|
|
1165 | { |
|
|
1166 | if (selection_wait == Sel_incr) |
|
|
1167 | { |
|
|
1168 | XFree (ct.value); |
|
|
1169 | |
|
|
1170 | // finally complete, now paste the whole thing |
|
|
1171 | selection_wait = Sel_normal; |
|
|
1172 | ct.value = (unsigned char *)incr_buf; |
|
|
1173 | ct.nitems = incr_buf_fill; |
|
|
1174 | incr_buf = 0; |
|
|
1175 | timer_ev.stop (); |
|
|
1176 | } |
|
|
1177 | else |
|
|
1178 | { |
|
|
1179 | // avoid recursion |
|
|
1180 | if (win != display->root || prop != XA_CUT_BUFFER0) |
|
|
1181 | { |
|
|
1182 | XFree (ct.value); |
|
|
1183 | |
|
|
1184 | // fallback to CUT_BUFFER0 if the requested property |
|
|
1185 | // has an owner but is empty |
|
|
1186 | handle_selection (display->root, XA_CUT_BUFFER0, False); |
|
|
1187 | return; |
|
|
1188 | } |
|
|
1189 | |
|
|
1190 | goto bailout; |
|
|
1191 | } |
|
|
1192 | } |
|
|
1193 | else if (selection_wait == Sel_incr) |
|
|
1194 | { |
|
|
1195 | timer_ev.again (); |
|
|
1196 | |
|
|
1197 | while (incr_buf_fill + ct.nitems > incr_buf_size) |
|
|
1198 | { |
|
|
1199 | incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024; |
|
|
1200 | incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size); |
|
|
1201 | } |
|
|
1202 | |
|
|
1203 | memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems); |
|
|
1204 | incr_buf_fill += ct.nitems; |
|
|
1205 | |
|
|
1206 | goto bailout; |
|
|
1207 | } |
|
|
1208 | |
|
|
1209 | char **cl; |
|
|
1210 | int cr; |
|
|
1211 | |
|
|
1212 | // we honour the first item only |
|
|
1213 | |
|
|
1214 | #if !ENABLE_MINIMAL |
|
|
1215 | // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it |
|
|
1216 | // so recode it manually |
|
|
1217 | if (ct.encoding == display->xa[XA_UTF8_STRING]) |
|
|
1218 | { |
|
|
1219 | wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems); |
|
|
1220 | data = rxvt_wcstombs (w); |
|
|
1221 | free (w); |
|
|
1222 | } |
|
|
1223 | else |
|
|
1224 | #endif |
|
|
1225 | if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0 |
|
|
1226 | && cl) |
|
|
1227 | { |
|
|
1228 | data = strdup (cl[0]); |
|
|
1229 | XFreeStringList (cl); |
|
|
1230 | } |
|
|
1231 | else |
|
|
1232 | { |
|
|
1233 | // paste raw |
|
|
1234 | data = strdup ((const char *)ct.value); |
|
|
1235 | } |
|
|
1236 | |
|
|
1237 | data_len = strlen (data); |
|
|
1238 | |
|
|
1239 | bailout: |
|
|
1240 | XFree (ct.value); |
|
|
1241 | |
|
|
1242 | if (selection_wait == Sel_normal) |
|
|
1243 | { |
|
|
1244 | finish (data, data_len); |
|
|
1245 | free (data); |
|
|
1246 | } |
|
|
1247 | } |
|
|
1248 | |
|
|
1249 | void |
|
|
1250 | rxvt_selection::timer_cb (ev::timer &w, int revents) |
|
|
1251 | { |
|
|
1252 | if (selection_wait == Sel_incr) |
|
|
1253 | rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); |
|
|
1254 | |
|
|
1255 | finish (); |
|
|
1256 | } |
|
|
1257 | |
|
|
1258 | void |
|
|
1259 | rxvt_selection::x_cb (XEvent &xev) |
|
|
1260 | { |
|
|
1261 | switch (xev.type) |
|
|
1262 | { |
|
|
1263 | case PropertyNotify: |
|
|
1264 | if (selection_wait == Sel_incr |
|
|
1265 | && xev.xproperty.atom == request_prop |
|
|
1266 | && xev.xproperty.state == PropertyNewValue) |
|
|
1267 | handle_selection (xev.xproperty.window, xev.xproperty.atom, true); |
|
|
1268 | break; |
|
|
1269 | |
|
|
1270 | case SelectionNotify: |
|
|
1271 | if (selection_wait == Sel_normal |
|
|
1272 | && xev.xselection.time == request_time) |
|
|
1273 | { |
|
|
1274 | timer_ev.stop (); |
|
|
1275 | handle_selection (xev.xselection.requestor, xev.xselection.property, true); |
|
|
1276 | } |
|
|
1277 | break; |
|
|
1278 | } |
|
|
1279 | } |