1 | /*----------------------------------------------------------------------* |
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-2007 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 | #include <sys/socket.h> |
34 | #include <sys/socket.h> |
… | |
… | |
33 | |
36 | |
34 | #if XFT |
37 | #if XFT |
35 | # include <X11/extensions/Xrender.h> |
38 | # include <X11/extensions/Xrender.h> |
36 | #endif |
39 | #endif |
37 | |
40 | |
38 | const char *const xa_names[] = |
41 | static const char *const xa_names[] = |
39 | { |
42 | { |
40 | "TEXT", |
43 | "TEXT", |
41 | "COMPOUND_TEXT", |
44 | "COMPOUND_TEXT", |
42 | "UTF8_STRING", |
45 | "UTF8_STRING", |
43 | "MULTIPLE", |
46 | "MULTIPLE", |
… | |
… | |
65 | #endif |
68 | #endif |
66 | #if USE_XIM |
69 | #if USE_XIM |
67 | "WM_LOCALE_NAME", |
70 | "WM_LOCALE_NAME", |
68 | "XIM_SERVERS", |
71 | "XIM_SERVERS", |
69 | #endif |
72 | #endif |
70 | #ifdef ENABLE_TRANSPARENCY |
73 | #if HAVE_IMG || ENABLE_PERL |
71 | "_XROOTPMAP_ID", |
74 | "_XROOTPMAP_ID", |
72 | "ESETROOT_PMAP_ID", |
75 | "ESETROOT_PMAP_ID", |
73 | #endif |
76 | #endif |
74 | #if ENABLE_XEMBED |
77 | #if ENABLE_XEMBED |
75 | "_XEMBED", |
78 | "_XEMBED", |
… | |
… | |
152 | put (*this->begin ()); |
155 | put (*this->begin ()); |
153 | } |
156 | } |
154 | |
157 | |
155 | ///////////////////////////////////////////////////////////////////////////// |
158 | ///////////////////////////////////////////////////////////////////////////// |
156 | |
159 | |
157 | #ifdef USE_XIM |
160 | #if USE_XIM |
158 | |
161 | |
159 | static void |
162 | static void |
160 | #if XIMCB_PROTO_BROKEN |
163 | #if XIMCB_PROTO_BROKEN |
161 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
164 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
162 | #else |
165 | #else |
… | |
… | |
227 | { |
230 | { |
228 | } |
231 | } |
229 | |
232 | |
230 | rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) |
233 | rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) |
231 | { |
234 | { |
232 | // it's actually faster to re-allocate every time. don't ask me |
|
|
233 | // why, but its likely no big deal there are no roundtrips |
|
|
234 | // (I think/hope). |
|
|
235 | if (!scratch_area || w > scratch_w || h > scratch_h || 1/*D*/) |
235 | if (!scratch_area || w > scratch_w || h > scratch_h) |
236 | { |
236 | { |
237 | if (scratch_area) |
237 | if (scratch_area) |
238 | { |
238 | { |
239 | XFreePixmap (dpy, scratch_area->drawable); |
239 | XFreePixmap (dpy, scratch_area->drawable); |
240 | delete scratch_area; |
240 | delete scratch_area; |
… | |
… | |
262 | depth = DefaultDepthOfScreen (screen); |
262 | depth = DefaultDepthOfScreen (screen); |
263 | visual = DefaultVisualOfScreen (screen); |
263 | visual = DefaultVisualOfScreen (screen); |
264 | cmap = DefaultColormapOfScreen (screen); |
264 | cmap = DefaultColormapOfScreen (screen); |
265 | } |
265 | } |
266 | |
266 | |
|
|
267 | #if ENABLE_FRILLS |
|
|
268 | |
267 | void |
269 | void |
268 | rxvt_screen::select_visual (int bitdepth) |
270 | rxvt_screen::select_visual (int id) |
269 | { |
271 | { |
270 | #if XFT |
|
|
271 | 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; |
272 | |
293 | |
273 | if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) |
294 | if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) |
274 | { |
|
|
275 | depth = bitdepth; |
|
|
276 | visual = vinfo.visual; |
295 | select_visual (vinfo.visualid); |
277 | cmap = XCreateColormap (dpy, display->root, visual, AllocNone); |
296 | else |
278 | } |
297 | rxvt_warn ("no visual found for requested depth %d, using default visual.\n", bitdepth); |
279 | #endif |
|
|
280 | } |
298 | } |
|
|
299 | |
|
|
300 | #endif |
281 | |
301 | |
282 | void |
302 | void |
283 | rxvt_screen::clear () |
303 | rxvt_screen::clear () |
284 | { |
304 | { |
285 | #if XFT |
305 | #if XFT |
… | |
… | |
314 | /* |
334 | /* |
315 | * get resources using the X library function |
335 | * get resources using the X library function |
316 | */ |
336 | */ |
317 | char *displayResource, *xe; |
337 | char *displayResource, *xe; |
318 | XrmDatabase rdb1, database = 0; |
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 |
319 | |
344 | |
320 | // 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 |
321 | // as opposed to "standard practise", we always read in ~/.Xdefaults |
346 | // as opposed to "standard practise", we always read in ~/.Xdefaults |
322 | |
347 | |
323 | // 6. System wide per application default file. |
348 | // 6. System wide per application default file. |
… | |
… | |
362 | XA_STRING, &actual_type, &actual_format, |
387 | XA_STRING, &actual_type, &actual_format, |
363 | &nitems, &nremaining, |
388 | &nitems, &nremaining, |
364 | (unsigned char **)&val) == Success |
389 | (unsigned char **)&val) == Success |
365 | && actual_type == XA_STRING |
390 | && actual_type == XA_STRING |
366 | && actual_format == 8) |
391 | && actual_format == 8) |
367 | displayResource = val; |
392 | displayResource = val; |
368 | else |
393 | else |
369 | { |
394 | { |
370 | displayResource = 0; |
395 | displayResource = 0; |
371 | |
396 | |
372 | if (val) |
397 | if (val) |
373 | XFree(val); |
398 | XFree (val); |
374 | } |
399 | } |
375 | |
400 | |
376 | #if XLIB_ILLEGAL_ACCESS |
401 | #if XLIB_ILLEGAL_ACCESS |
377 | dpy->xdefaults = displayResource; |
402 | dpy->xdefaults = displayResource; |
378 | #endif |
403 | #endif |
379 | } |
404 | } |
… | |
… | |
427 | bool rxvt_display::ref_init () |
452 | bool rxvt_display::ref_init () |
428 | { |
453 | { |
429 | #ifdef LOCAL_X_IS_UNIX |
454 | #ifdef LOCAL_X_IS_UNIX |
430 | if (id[0] == ':') |
455 | if (id[0] == ':') |
431 | { |
456 | { |
432 | val = rxvt_malloc (5 + strlen (id) + 1); |
457 | char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1); |
|
|
458 | |
433 | strcpy (val, "unix/"); |
459 | strcpy (val, "unix/"); |
434 | strcat (val, id); |
460 | strcat (val, id); |
|
|
461 | |
435 | dpy = XOpenDisplay (val); |
462 | dpy = XOpenDisplay (val); |
436 | free (val); |
|
|
437 | } |
463 | } |
438 | else |
464 | else |
439 | #endif |
465 | #endif |
440 | dpy = 0; |
466 | dpy = 0; |
441 | |
467 | |
… | |
… | |
446 | return false; |
472 | return false; |
447 | |
473 | |
448 | screen = DefaultScreen (dpy); |
474 | screen = DefaultScreen (dpy); |
449 | root = DefaultRootWindow (dpy); |
475 | root = DefaultRootWindow (dpy); |
450 | |
476 | |
451 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
477 | assert (ecb_array_length (xa_names) == NUM_XA); |
452 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
478 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
453 | |
479 | |
454 | XrmSetDatabase (dpy, get_resources (false)); |
480 | XrmSetDatabase (dpy, get_resources (false)); |
455 | |
481 | |
456 | #ifdef POINTER_BLANK |
482 | #ifdef POINTER_BLANK |
… | |
… | |
462 | blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', |
488 | blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', |
463 | &blackcolour, &blackcolour); |
489 | &blackcolour, &blackcolour); |
464 | XUnloadFont (dpy, f); |
490 | XUnloadFont (dpy, f); |
465 | #endif |
491 | #endif |
466 | |
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 | |
467 | int fd = XConnectionNumber (dpy); |
512 | int fd = XConnectionNumber (dpy); |
468 | |
513 | |
469 | // try to detect wether we have a local connection. |
514 | // try to detect whether we have a local connection. |
470 | // assume unix domain 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); |
… | |
… | |
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 | { |
… | |
… | |
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 (); |
508 | flush_ev.stop (); |
557 | flush_ev.stop (); |
509 | #ifdef USE_XIM |
558 | #if USE_XIM |
510 | xims.clear (); |
559 | xims.clear (); |
511 | #endif |
560 | #endif |
|
|
561 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
512 | XCloseDisplay (dpy); |
562 | XCloseDisplay (dpy); |
513 | } |
563 | } |
514 | |
564 | |
515 | #ifdef USE_XIM |
565 | #if USE_XIM |
516 | void rxvt_display::im_change_cb () |
566 | void rxvt_display::im_change_cb () |
517 | { |
567 | { |
518 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
568 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
519 | (*i)->call (); |
569 | (*i)->call (); |
520 | } |
570 | } |
… | |
… | |
528 | unsigned long nitems, bytes_after; |
578 | unsigned long nitems, bytes_after; |
529 | |
579 | |
530 | if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
580 | if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
531 | False, XA_ATOM, &actual_type, &actual_format, |
581 | False, XA_ATOM, &actual_type, &actual_format, |
532 | &nitems, &bytes_after, (unsigned char **)&atoms) |
582 | &nitems, &bytes_after, (unsigned char **)&atoms) |
533 | != Success ) |
583 | != Success) |
534 | return; |
584 | return; |
535 | |
585 | |
536 | if (actual_type == XA_ATOM && actual_format == 32) |
586 | if (actual_type == XA_ATOM && actual_format == 32) |
537 | for (int i = 0; i < nitems; i++) |
587 | for (int i = 0; i < nitems; i++) |
538 | if (XGetSelectionOwner (dpy, atoms[i])) |
588 | if (XGetSelectionOwner (dpy, atoms[i])) |
539 | { |
589 | { |
540 | im_change_cb (); |
590 | im_change_cb (); |
541 | break; |
591 | break; |
… | |
… | |
556 | do |
606 | do |
557 | { |
607 | { |
558 | XEvent xev; |
608 | XEvent xev; |
559 | XNextEvent (dpy, &xev); |
609 | XNextEvent (dpy, &xev); |
560 | |
610 | |
561 | #ifdef USE_XIM |
611 | #if USE_XIM |
562 | if (!XFilterEvent (&xev, None)) |
612 | if (!XFilterEvent (&xev, None)) |
563 | { |
613 | { |
564 | if (xev.type == PropertyNotify |
614 | if (xev.type == PropertyNotify |
565 | && xev.xany.window == root |
615 | && xev.xany.window == root |
566 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
616 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
… | |
… | |
574 | if (!xw[i]) |
624 | if (!xw[i]) |
575 | xw.erase_unordered (i); |
625 | xw.erase_unordered (i); |
576 | else if (xw[i]->window == xev.xany.window) |
626 | else if (xw[i]->window == xev.xany.window) |
577 | xw[i]->call (xev); |
627 | xw[i]->call (xev); |
578 | } |
628 | } |
579 | #ifdef USE_XIM |
629 | #if USE_XIM |
580 | } |
630 | } |
581 | #endif |
631 | #endif |
582 | } |
632 | } |
583 | while (XEventsQueued (dpy, QueuedAlready)); |
633 | while (XEventsQueued (dpy, QueuedAlready)); |
584 | |
634 | |
… | |
… | |
607 | { |
657 | { |
608 | rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner; |
658 | rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner; |
609 | |
659 | |
610 | if (cur_owner && cur_owner != owner) |
660 | if (cur_owner && cur_owner != owner) |
611 | { |
661 | { |
|
|
662 | rxvt_term *term = cur_owner; |
612 | cur_owner->selection_clear (clipboard); |
663 | term->selection_clear (clipboard); |
613 | cur_owner->flush (); |
664 | term->flush (); |
614 | } |
665 | } |
615 | |
666 | |
616 | cur_owner = owner; |
667 | cur_owner = owner; |
617 | } |
668 | } |
618 | |
669 | |
619 | #ifdef USE_XIM |
670 | #if USE_XIM |
620 | |
671 | |
621 | void rxvt_display::reg (im_watcher *w) |
672 | void rxvt_display::reg (im_watcher *w) |
622 | { |
673 | { |
623 | imw.push_back (w); |
674 | imw.push_back (w); |
624 | } |
675 | } |
… | |
… | |
634 | int l, m; |
685 | int l, m; |
635 | |
686 | |
636 | l = strlen (locale); |
687 | l = strlen (locale); |
637 | m = strlen (modifiers); |
688 | m = strlen (modifiers); |
638 | |
689 | |
639 | if (!(id = (char *)malloc (l + m + 2))) |
690 | if (!(id = rxvt_temp_buf<char> (l + m + 2))) |
640 | return 0; |
691 | return 0; |
641 | |
692 | |
642 | memcpy (id, locale, l); id[l] = '\n'; |
693 | memcpy (id, locale, l); id[l] = '\n'; |
643 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
694 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
644 | |
695 | |
645 | rxvt_xim *xim = xims.get (id); |
696 | rxvt_xim *xim = xims.get (id); |
646 | |
|
|
647 | free (id); |
|
|
648 | |
697 | |
649 | return xim; |
698 | return xim; |
650 | } |
699 | } |
651 | |
700 | |
652 | void rxvt_display::put_xim (rxvt_xim *xim) |
701 | void rxvt_display::put_xim (rxvt_xim *xim) |
… | |
… | |
661 | Atom rxvt_display::atom (const char *name) |
710 | Atom rxvt_display::atom (const char *name) |
662 | { |
711 | { |
663 | return XInternAtom (dpy, name, False); |
712 | return XInternAtom (dpy, name, False); |
664 | } |
713 | } |
665 | |
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 | |
666 | ///////////////////////////////////////////////////////////////////////////// |
737 | ///////////////////////////////////////////////////////////////////////////// |
667 | |
738 | |
668 | template class refcache<rxvt_display>; |
739 | template class refcache<rxvt_display>; |
669 | refcache<rxvt_display> displays; |
740 | refcache<rxvt_display> displays; |
670 | |
741 | |
… | |
… | |
679 | |
750 | |
680 | bool |
751 | bool |
681 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
752 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
682 | { |
753 | { |
683 | //TODO: only supports 24 bit |
754 | //TODO: only supports 24 bit |
684 | int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
755 | unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
685 | |
756 | |
686 | #if XFT |
757 | #if XFT |
687 | XRenderPictFormat *format; |
758 | XRenderPictFormat *format; |
688 | |
759 | |
689 | // FUCKING Xft gets it wrong, of course, so work around it. |
760 | // FUCKING Xft gets it wrong, of course, so work around it. |
… | |
… | |
696 | c.color.red = color.r; |
767 | c.color.red = color.r; |
697 | c.color.green = color.g; |
768 | c.color.green = color.g; |
698 | c.color.blue = color.b; |
769 | c.color.blue = color.b; |
699 | c.color.alpha = alpha; |
770 | c.color.alpha = alpha; |
700 | |
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 | |
701 | c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red ) |
778 | c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red ) |
702 | | insert_component (color.g, format->direct.greenMask, format->direct.green) |
779 | | insert_component (c.color.green, format->direct.greenMask, format->direct.green) |
703 | | insert_component (color.b, format->direct.blueMask , format->direct.blue ) |
780 | | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue ) |
704 | | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); |
781 | | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); |
705 | |
782 | |
706 | return true; |
783 | return true; |
707 | } |
784 | } |
708 | else |
785 | else |
709 | { |
786 | { |
… | |
… | |
712 | d.red = color.r; |
789 | d.red = color.r; |
713 | d.green = color.g; |
790 | d.green = color.g; |
714 | d.blue = color.b; |
791 | d.blue = color.b; |
715 | d.alpha = alpha; |
792 | d.alpha = alpha; |
716 | |
793 | |
717 | return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); |
794 | if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c)) |
|
|
795 | return true; |
718 | } |
796 | } |
719 | #else |
797 | #else |
720 | c.red = color.r; |
798 | c.red = color.r; |
721 | c.green = color.g; |
799 | c.green = color.g; |
722 | c.blue = color.b; |
800 | c.blue = color.b; |
723 | |
801 | |
724 | if (screen->visual->c_class == TrueColor) |
802 | if (screen->visual->c_class == TrueColor) |
725 | { |
803 | { |
726 | c.pixel = (color.r >> (16 - rxvt_popcount (screen->visual->red_mask )) << rxvt_ctz (screen->visual->red_mask )) |
804 | c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask )) |
727 | | (color.g >> (16 - rxvt_popcount (screen->visual->green_mask)) << rxvt_ctz (screen->visual->green_mask)) |
805 | | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask)) |
728 | | (color.b >> (16 - rxvt_popcount (screen->visual->blue_mask )) << rxvt_ctz (screen->visual->blue_mask )); |
806 | | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask )); |
729 | |
807 | |
730 | return true; |
808 | return true; |
731 | } |
809 | } |
732 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
810 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
733 | return true; |
811 | return true; |
734 | else |
812 | #endif |
|
|
813 | |
|
|
814 | //TODO: set c.color* or c.* |
735 | c.pixel = (color.r + color.g + color.b) > 128*3 |
815 | c.pixel = (color.r * 2 + color.g * 3 + color.b) >= 0x8000 * 6 |
736 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
816 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
737 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
817 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
738 | #endif |
|
|
739 | |
818 | |
740 | return false; |
819 | return false; |
741 | } |
820 | } |
742 | |
821 | |
743 | bool |
822 | bool |
… | |
… | |
799 | |
878 | |
800 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
879 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
801 | // many extra optimisations. |
880 | // many extra optimisations. |
802 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
881 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
803 | |
882 | |
|
|
883 | while (cmap_size) |
|
|
884 | { |
804 | int diff = 0x7fffffffUL; |
885 | int diff = 0x7fffffffL; |
805 | XColor *best = colors; |
886 | XColor *best = colors; |
806 | |
887 | |
807 | for (int i = 0; i < cmap_size; i++) |
888 | for (int i = 0; i < cmap_size; i++) |
808 | { |
|
|
809 | int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2)) |
|
|
810 | + (squared_diff<int> (color.g >> 2, colors [i].green >> 2)) |
|
|
811 | + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2)); |
|
|
812 | |
|
|
813 | if (d < diff) |
|
|
814 | { |
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 | { |
815 | diff = d; |
897 | diff = d; |
816 | best = colors + i; |
898 | best = colors + i; |
|
|
899 | } |
817 | } |
900 | } |
818 | } |
|
|
819 | |
901 | |
820 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
902 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d,%d)\n", |
821 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
903 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff, best - colors); |
822 | |
904 | |
823 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
905 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
|
|
906 | |
|
|
907 | if (got) |
|
|
908 | break; |
|
|
909 | |
|
|
910 | *best = colors [--cmap_size]; |
|
|
911 | } |
824 | |
912 | |
825 | delete [] colors; |
913 | delete [] colors; |
826 | } |
914 | } |
827 | #endif |
915 | #endif |
828 | |
916 | |
829 | return got; |
917 | return got; |
830 | } |
918 | } |
831 | |
919 | |
832 | void |
920 | void |
833 | rxvt_color::get (rgba &color) |
921 | rxvt_color::get (rgba &color) const |
834 | { |
922 | { |
835 | #if XFT |
923 | #if XFT |
|
|
924 | //TODO premultiplied alpha?? |
836 | color.r = c.color.red; |
925 | color.r = c.color.red; |
837 | color.g = c.color.green; |
926 | color.g = c.color.green; |
838 | color.b = c.color.blue; |
927 | color.b = c.color.blue; |
839 | color.a = c.color.alpha; |
928 | color.a = c.color.alpha; |
840 | #else |
929 | #else |
… | |
… | |
844 | color.a = rgba::MAX_CC; |
933 | color.a = rgba::MAX_CC; |
845 | #endif |
934 | #endif |
846 | } |
935 | } |
847 | |
936 | |
848 | void |
937 | void |
849 | rxvt_color::get (XColor &color) |
938 | rxvt_color::get (XColor &color) const |
850 | { |
939 | { |
851 | rgba c; |
940 | rgba c; |
852 | get (c); |
941 | get (c); |
853 | |
942 | |
854 | color.red = c.r; |
943 | color.red = c.r; |
… | |
… | |
885 | lerp (c.a, to.a, percent) |
974 | lerp (c.a, to.a, percent) |
886 | ) |
975 | ) |
887 | ); |
976 | ); |
888 | } |
977 | } |
889 | |
978 | |
890 | #if TRACE_PIXMAPS |
979 | rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term) |
891 | # undef XCreatePixmap |
980 | : display (disp), request_time (tm), request_win (win), request_prop (prop), term (term) |
892 | # undef XFreePixmap |
|
|
893 | Pixmap trace_XCreatePixmap (const char *file, int line, Display *dpy, Window r, unsigned int w, unsigned int h, unsigned int d) |
|
|
894 | { |
981 | { |
895 | Pixmap res = XCreatePixmap (dpy, r, w, h, d); |
982 | assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); |
896 | fprintf (stderr, "%s:%d: XCreatePixmap (%p,%lX,%u,%u,%u) returned %lX\n", file, line, dpy, r, w, h, d, res); |
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 | } |
|
|
1081 | |
897 | return res; |
1082 | return false; |
898 | } |
1083 | } |
899 | |
1084 | |
900 | void trace_XFreePixmap (const char *file, int line, Display *dpy, Pixmap p) |
1085 | void |
|
|
1086 | rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop) |
901 | { |
1087 | { |
902 | fprintf (stderr, "%s:%d: XFreePixmap (%p,%lX)\n", file, line, dpy, p); |
1088 | Display *dpy = display->dpy; |
903 | XFreePixmap (dpy,p); |
1089 | char *data = 0; |
904 | } |
1090 | unsigned int data_len = 0; |
|
|
1091 | unsigned long bytes_after; |
|
|
1092 | XTextProperty ct; |
|
|
1093 | |
|
|
1094 | // check for failed XConvertSelection |
|
|
1095 | if (prop == None) |
|
|
1096 | { |
|
|
1097 | bool error = true; |
|
|
1098 | int selnum = selection_type & Sel_whereMask; |
|
|
1099 | |
|
|
1100 | if (selection_type & Sel_CompoundText) |
|
|
1101 | { |
|
|
1102 | selection_type = 0; |
|
|
1103 | error = !request (XA_STRING, selnum); |
|
|
1104 | } |
|
|
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 |
905 | #endif |
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 | |
|
|
1236 | void |
|
|
1237 | rxvt_selection::timer_cb (ev::timer &w, int revents) |
|
|
1238 | { |
|
|
1239 | if (selection_wait == Sel_incr) |
|
|
1240 | rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); |
|
|
1241 | |
|
|
1242 | finish (); |
|
|
1243 | } |
|
|
1244 | |
|
|
1245 | void |
|
|
1246 | rxvt_selection::x_cb (XEvent &xev) |
|
|
1247 | { |
|
|
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; |
|
|
1256 | |
|
|
1257 | case SelectionNotify: |
|
|
1258 | if (selection_wait == Sel_normal |
|
|
1259 | && xev.xselection.time == request_time) |
|
|
1260 | { |
|
|
1261 | timer_ev.stop (); |
|
|
1262 | handle_selection (xev.xselection.requestor, xev.xselection.property, true); |
|
|
1263 | } |
|
|
1264 | break; |
|
|
1265 | } |
|
|
1266 | } |