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 2 of the License, or |
11 | * (at your option) any later version. |
12 | * (at your option) any later version. |
… | |
… | |
33 | |
34 | |
34 | #if XFT |
35 | #if XFT |
35 | # include <X11/extensions/Xrender.h> |
36 | # include <X11/extensions/Xrender.h> |
36 | #endif |
37 | #endif |
37 | |
38 | |
38 | const char *const xa_names[] = |
39 | static const char *const xa_names[] = |
39 | { |
40 | { |
40 | "TEXT", |
41 | "TEXT", |
41 | "COMPOUND_TEXT", |
42 | "COMPOUND_TEXT", |
42 | "UTF8_STRING", |
43 | "UTF8_STRING", |
43 | "MULTIPLE", |
44 | "MULTIPLE", |
… | |
… | |
59 | #if ENABLE_EWMH |
60 | #if ENABLE_EWMH |
60 | "_NET_WM_PID", |
61 | "_NET_WM_PID", |
61 | "_NET_WM_NAME", |
62 | "_NET_WM_NAME", |
62 | "_NET_WM_ICON_NAME", |
63 | "_NET_WM_ICON_NAME", |
63 | "_NET_WM_PING", |
64 | "_NET_WM_PING", |
|
|
65 | "_NET_WM_ICON", |
64 | #endif |
66 | #endif |
65 | #if USE_XIM |
67 | #if USE_XIM |
66 | "WM_LOCALE_NAME", |
68 | "WM_LOCALE_NAME", |
67 | "XIM_SERVERS", |
69 | "XIM_SERVERS", |
68 | #endif |
70 | #endif |
… | |
… | |
226 | { |
228 | { |
227 | } |
229 | } |
228 | |
230 | |
229 | rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) |
231 | rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) |
230 | { |
232 | { |
231 | // it's actually faster to re-allocate every time. don't ask me |
|
|
232 | // why, but its likely no big deal there are no roundtrips |
|
|
233 | // (I think/hope). |
|
|
234 | if (!scratch_area || w > scratch_w || h > scratch_h || 1/*D*/) |
233 | if (!scratch_area || w > scratch_w || h > scratch_h) |
235 | { |
234 | { |
236 | if (scratch_area) |
235 | if (scratch_area) |
237 | { |
236 | { |
238 | XFreePixmap (dpy, scratch_area->drawable); |
237 | XFreePixmap (dpy, scratch_area->drawable); |
239 | delete scratch_area; |
238 | delete scratch_area; |
… | |
… | |
296 | ///////////////////////////////////////////////////////////////////////////// |
295 | ///////////////////////////////////////////////////////////////////////////// |
297 | |
296 | |
298 | rxvt_display::rxvt_display (const char *id) |
297 | rxvt_display::rxvt_display (const char *id) |
299 | : refcounted (id) |
298 | : refcounted (id) |
300 | , selection_owner (0) |
299 | , selection_owner (0) |
|
|
300 | , clipboard_owner (0) |
301 | { |
301 | { |
302 | x_ev .set<rxvt_display, &rxvt_display::x_cb > (this); |
302 | x_ev .set<rxvt_display, &rxvt_display::x_cb > (this); |
303 | flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this); |
303 | flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this); |
304 | } |
304 | } |
305 | |
305 | |
… | |
… | |
312 | /* |
312 | /* |
313 | * get resources using the X library function |
313 | * get resources using the X library function |
314 | */ |
314 | */ |
315 | char *displayResource, *xe; |
315 | char *displayResource, *xe; |
316 | XrmDatabase rdb1, database = 0; |
316 | XrmDatabase rdb1, database = 0; |
|
|
317 | |
|
|
318 | #if !XLIB_ILLEGAL_ACCESS |
|
|
319 | /* work around a bug in XrmSetDatabase where it frees the db, see ref_next */ |
|
|
320 | database = XrmGetStringDatabase (""); |
|
|
321 | #endif |
317 | |
322 | |
318 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
323 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
319 | // as opposed to "standard practise", we always read in ~/.Xdefaults |
324 | // as opposed to "standard practise", we always read in ~/.Xdefaults |
320 | |
325 | |
321 | // 6. System wide per application default file. |
326 | // 6. System wide per application default file. |
… | |
… | |
360 | XA_STRING, &actual_type, &actual_format, |
365 | XA_STRING, &actual_type, &actual_format, |
361 | &nitems, &nremaining, |
366 | &nitems, &nremaining, |
362 | (unsigned char **)&val) == Success |
367 | (unsigned char **)&val) == Success |
363 | && actual_type == XA_STRING |
368 | && actual_type == XA_STRING |
364 | && actual_format == 8) |
369 | && actual_format == 8) |
365 | displayResource = val; |
370 | displayResource = val; |
366 | else |
371 | else |
367 | { |
372 | { |
368 | displayResource = 0; |
373 | displayResource = 0; |
369 | |
374 | |
370 | if (val) |
375 | if (val) |
371 | XFree(val); |
376 | XFree (val); |
372 | } |
377 | } |
373 | |
378 | |
374 | #if XLIB_ILLEGAL_ACCESS |
379 | #if XLIB_ILLEGAL_ACCESS |
375 | dpy->xdefaults = displayResource; |
380 | dpy->xdefaults = displayResource; |
376 | #endif |
381 | #endif |
377 | } |
382 | } |
… | |
… | |
425 | bool rxvt_display::ref_init () |
430 | bool rxvt_display::ref_init () |
426 | { |
431 | { |
427 | #ifdef LOCAL_X_IS_UNIX |
432 | #ifdef LOCAL_X_IS_UNIX |
428 | if (id[0] == ':') |
433 | if (id[0] == ':') |
429 | { |
434 | { |
430 | val = rxvt_malloc (5 + strlen (id) + 1); |
435 | char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1); |
|
|
436 | |
431 | strcpy (val, "unix/"); |
437 | strcpy (val, "unix/"); |
432 | strcat (val, id); |
438 | strcat (val, id); |
|
|
439 | |
433 | dpy = XOpenDisplay (val); |
440 | dpy = XOpenDisplay (val); |
434 | free (val); |
|
|
435 | } |
441 | } |
436 | else |
442 | else |
437 | #endif |
443 | #endif |
438 | dpy = 0; |
444 | dpy = 0; |
439 | |
445 | |
… | |
… | |
444 | return false; |
450 | return false; |
445 | |
451 | |
446 | screen = DefaultScreen (dpy); |
452 | screen = DefaultScreen (dpy); |
447 | root = DefaultRootWindow (dpy); |
453 | root = DefaultRootWindow (dpy); |
448 | |
454 | |
449 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
455 | assert (ecb_array_length (xa_names) == NUM_XA); |
450 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
456 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
451 | |
457 | |
452 | XrmSetDatabase (dpy, get_resources (false)); |
458 | XrmSetDatabase (dpy, get_resources (false)); |
453 | |
459 | |
454 | #ifdef POINTER_BLANK |
460 | #ifdef POINTER_BLANK |
… | |
… | |
462 | XUnloadFont (dpy, f); |
468 | XUnloadFont (dpy, f); |
463 | #endif |
469 | #endif |
464 | |
470 | |
465 | int fd = XConnectionNumber (dpy); |
471 | int fd = XConnectionNumber (dpy); |
466 | |
472 | |
467 | // try to detect wether we have a local connection. |
473 | // try to detect whether we have a local connection. |
468 | // assume unix domain socket == local, everything else not |
474 | // assume unix domain socket == local, everything else not |
469 | // TODO: might want to check for inet/127.0.0.1 |
475 | // TODO: might want to check for inet/127.0.0.1 |
470 | is_local = 0; |
476 | is_local = 0; |
471 | sockaddr_un sa; |
477 | sockaddr_un sa; |
472 | socklen_t sl = sizeof (sa); |
478 | socklen_t sl = sizeof (sa); |
… | |
… | |
486 | } |
492 | } |
487 | |
493 | |
488 | void |
494 | void |
489 | rxvt_display::ref_next () |
495 | rxvt_display::ref_next () |
490 | { |
496 | { |
491 | // TODO: somehow check wether the database files/resources changed |
497 | // TODO: somehow check whether the database files/resources changed |
492 | // before affording re-loading/parsing |
498 | // before affording re-loading/parsing |
493 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
499 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
|
|
500 | #if XLIB_ILLEGAL_ACCESS |
|
|
501 | /* work around a bug in XrmSetDatabase where it frees the db */ |
|
|
502 | dpy->db = 0; |
|
|
503 | #endif |
494 | XrmSetDatabase (dpy, get_resources (true)); |
504 | XrmSetDatabase (dpy, get_resources (true)); |
495 | } |
505 | } |
496 | |
506 | |
497 | rxvt_display::~rxvt_display () |
507 | rxvt_display::~rxvt_display () |
498 | { |
508 | { |
… | |
… | |
505 | x_ev.stop (); |
515 | x_ev.stop (); |
506 | flush_ev.stop (); |
516 | flush_ev.stop (); |
507 | #ifdef USE_XIM |
517 | #ifdef USE_XIM |
508 | xims.clear (); |
518 | xims.clear (); |
509 | #endif |
519 | #endif |
|
|
520 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
510 | XCloseDisplay (dpy); |
521 | XCloseDisplay (dpy); |
511 | } |
522 | } |
512 | |
523 | |
513 | #ifdef USE_XIM |
524 | #ifdef USE_XIM |
514 | void rxvt_display::im_change_cb () |
525 | void rxvt_display::im_change_cb () |
… | |
… | |
526 | unsigned long nitems, bytes_after; |
537 | unsigned long nitems, bytes_after; |
527 | |
538 | |
528 | if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
539 | if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
529 | False, XA_ATOM, &actual_type, &actual_format, |
540 | False, XA_ATOM, &actual_type, &actual_format, |
530 | &nitems, &bytes_after, (unsigned char **)&atoms) |
541 | &nitems, &bytes_after, (unsigned char **)&atoms) |
531 | != Success ) |
542 | != Success) |
532 | return; |
543 | return; |
533 | |
544 | |
534 | if (actual_type == XA_ATOM && actual_format == 32) |
545 | if (actual_type == XA_ATOM && actual_format == 32) |
535 | for (int i = 0; i < nitems; i++) |
546 | for (int i = 0; i < nitems; i++) |
536 | if (XGetSelectionOwner (dpy, atoms[i])) |
547 | if (XGetSelectionOwner (dpy, atoms[i])) |
537 | { |
548 | { |
538 | im_change_cb (); |
549 | im_change_cb (); |
539 | break; |
550 | break; |
… | |
… | |
543 | } |
554 | } |
544 | #endif |
555 | #endif |
545 | |
556 | |
546 | void rxvt_display::x_cb (ev::io &w, int revents) |
557 | void rxvt_display::x_cb (ev::io &w, int revents) |
547 | { |
558 | { |
|
|
559 | flush_ev.start (); |
|
|
560 | } |
|
|
561 | |
|
|
562 | void rxvt_display::flush_cb (ev::prepare &w, int revents) |
|
|
563 | { |
548 | while (XEventsQueued (dpy, QueuedAfterReading)) |
564 | while (XEventsQueued (dpy, QueuedAfterFlush)) |
|
|
565 | do |
549 | { |
566 | { |
550 | XEvent xev; |
567 | XEvent xev; |
551 | XNextEvent (dpy, &xev); |
568 | XNextEvent (dpy, &xev); |
552 | |
569 | |
553 | #ifdef USE_XIM |
570 | #ifdef USE_XIM |
554 | if (!XFilterEvent (&xev, None)) |
571 | if (!XFilterEvent (&xev, None)) |
555 | { |
572 | { |
556 | if (xev.type == PropertyNotify |
573 | if (xev.type == PropertyNotify |
557 | && xev.xany.window == root |
574 | && xev.xany.window == root |
558 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
575 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
559 | im_change_check (); |
576 | im_change_check (); |
560 | #endif |
577 | #endif |
561 | if (xev.type == MappingNotify) |
578 | if (xev.type == MappingNotify) |
562 | XRefreshKeyboardMapping (&xev.xmapping); |
579 | XRefreshKeyboardMapping (&xev.xmapping); |
563 | |
580 | |
564 | for (int i = xw.size (); i--; ) |
581 | for (int i = xw.size (); i--; ) |
565 | { |
582 | { |
566 | if (!xw[i]) |
583 | if (!xw[i]) |
567 | xw.erase_unordered (i); |
584 | xw.erase_unordered (i); |
568 | else if (xw[i]->window == xev.xany.window) |
585 | else if (xw[i]->window == xev.xany.window) |
569 | xw[i]->call (xev); |
586 | xw[i]->call (xev); |
570 | } |
587 | } |
571 | #ifdef USE_XIM |
588 | #ifdef USE_XIM |
572 | } |
589 | } |
573 | #endif |
590 | #endif |
574 | } |
591 | } |
575 | } |
592 | while (XEventsQueued (dpy, QueuedAlready)); |
576 | |
593 | |
577 | void rxvt_display::flush_cb (ev::prepare &w, int revents) |
|
|
578 | { |
|
|
579 | w.stop (); |
594 | w.stop (); |
580 | XFlush (dpy); |
|
|
581 | } |
595 | } |
582 | |
596 | |
583 | void rxvt_display::reg (xevent_watcher *w) |
597 | void rxvt_display::reg (xevent_watcher *w) |
584 | { |
598 | { |
585 | if (!w->active) |
599 | if (!w->active) |
… | |
… | |
596 | xw[w->active - 1] = 0; |
610 | xw[w->active - 1] = 0; |
597 | w->active = 0; |
611 | w->active = 0; |
598 | } |
612 | } |
599 | } |
613 | } |
600 | |
614 | |
601 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
615 | void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard) |
602 | { |
616 | { |
|
|
617 | rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner; |
|
|
618 | |
603 | if (selection_owner && selection_owner != owner) |
619 | if (cur_owner && cur_owner != owner) |
604 | { |
620 | { |
605 | rxvt_term *owner = selection_owner; |
621 | rxvt_term *term = cur_owner; |
606 | |
|
|
607 | owner->selection_clear (); |
622 | term->selection_clear (clipboard); |
608 | owner->flush (); |
623 | term->flush (); |
609 | } |
624 | } |
610 | |
625 | |
611 | selection_owner = owner; |
626 | cur_owner = owner; |
612 | } |
627 | } |
613 | |
628 | |
614 | #ifdef USE_XIM |
629 | #ifdef USE_XIM |
615 | |
630 | |
616 | void rxvt_display::reg (im_watcher *w) |
631 | void rxvt_display::reg (im_watcher *w) |
… | |
… | |
629 | int l, m; |
644 | int l, m; |
630 | |
645 | |
631 | l = strlen (locale); |
646 | l = strlen (locale); |
632 | m = strlen (modifiers); |
647 | m = strlen (modifiers); |
633 | |
648 | |
634 | if (!(id = (char *)malloc (l + m + 2))) |
649 | if (!(id = rxvt_temp_buf<char> (l + m + 2))) |
635 | return 0; |
650 | return 0; |
636 | |
651 | |
637 | memcpy (id, locale, l); id[l] = '\n'; |
652 | memcpy (id, locale, l); id[l] = '\n'; |
638 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
653 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
639 | |
654 | |
640 | rxvt_xim *xim = xims.get (id); |
655 | rxvt_xim *xim = xims.get (id); |
641 | |
|
|
642 | free (id); |
|
|
643 | |
656 | |
644 | return xim; |
657 | return xim; |
645 | } |
658 | } |
646 | |
659 | |
647 | void rxvt_display::put_xim (rxvt_xim *xim) |
660 | void rxvt_display::put_xim (rxvt_xim *xim) |
… | |
… | |
673 | } |
686 | } |
674 | |
687 | |
675 | bool |
688 | bool |
676 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
689 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
677 | { |
690 | { |
678 | //TODO: only supports 24 bit truecolour |
691 | //TODO: only supports 24 bit |
679 | int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
692 | unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
680 | |
693 | |
681 | #if XFT |
694 | #if XFT |
682 | XRenderPictFormat *format; |
695 | XRenderPictFormat *format; |
683 | |
696 | |
684 | // FUCKING Xft gets it wrong, of course, so work around it. |
697 | // FUCKING Xft gets it wrong, of course, so work around it. |
… | |
… | |
691 | c.color.red = color.r; |
704 | c.color.red = color.r; |
692 | c.color.green = color.g; |
705 | c.color.green = color.g; |
693 | c.color.blue = color.b; |
706 | c.color.blue = color.b; |
694 | c.color.alpha = alpha; |
707 | c.color.alpha = alpha; |
695 | |
708 | |
|
|
709 | // Xft wants premultiplied alpha, but abuses the alpha channel |
|
|
710 | // as blend factor, and doesn't allow us to set the alpha channel |
|
|
711 | c.color.red = c.color.red * alpha / 0xffff; |
|
|
712 | c.color.green = c.color.green * alpha / 0xffff; |
|
|
713 | c.color.blue = c.color.blue * alpha / 0xffff; |
|
|
714 | |
696 | c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red ) |
715 | c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red ) |
697 | | insert_component (color.g, format->direct.greenMask, format->direct.green) |
716 | | insert_component (c.color.green, format->direct.greenMask, format->direct.green) |
698 | | insert_component (color.b, format->direct.blueMask , format->direct.blue ) |
717 | | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue ) |
699 | | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); |
718 | | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); |
700 | |
719 | |
701 | return true; |
720 | return true; |
702 | } |
721 | } |
703 | else |
722 | else |
704 | { |
723 | { |
… | |
… | |
707 | d.red = color.r; |
726 | d.red = color.r; |
708 | d.green = color.g; |
727 | d.green = color.g; |
709 | d.blue = color.b; |
728 | d.blue = color.b; |
710 | d.alpha = alpha; |
729 | d.alpha = alpha; |
711 | |
730 | |
712 | return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); |
731 | if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c)) |
|
|
732 | return true; |
713 | } |
733 | } |
714 | #else |
734 | #else |
715 | c.red = color.r; |
735 | c.red = color.r; |
716 | c.green = color.g; |
736 | c.green = color.g; |
717 | c.blue = color.b; |
737 | c.blue = color.b; |
718 | |
738 | |
719 | if (screen->visual->c_class == TrueColor) |
739 | if (screen->visual->c_class == TrueColor) |
720 | { |
740 | { |
721 | c.pixel = (color.r >> (16 - popcount (screen->visual->red_mask )) << ctz (screen->visual->red_mask )) |
741 | c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask )) |
722 | | (color.g >> (16 - popcount (screen->visual->green_mask)) << ctz (screen->visual->green_mask)) |
742 | | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask)) |
723 | | (color.b >> (16 - popcount (screen->visual->blue_mask )) << ctz (screen->visual->blue_mask )); |
743 | | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask )); |
724 | |
744 | |
725 | return true; |
745 | return true; |
726 | } |
746 | } |
727 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
747 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
728 | return true; |
748 | return true; |
729 | else |
749 | #endif |
|
|
750 | |
730 | c.pixel = (color.r + color.g + color.b) > 128*3 |
751 | c.pixel = (color.r + color.g + color.b) > 128*3 |
731 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
752 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
732 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
753 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
733 | #endif |
|
|
734 | |
754 | |
735 | return false; |
755 | return false; |
736 | } |
756 | } |
737 | |
757 | |
738 | bool |
758 | bool |
… | |
… | |
740 | { |
760 | { |
741 | rgba c; |
761 | rgba c; |
742 | char eos; |
762 | char eos; |
743 | int skip; |
763 | int skip; |
744 | |
764 | |
|
|
765 | c.a = rgba::MAX_CC; |
|
|
766 | |
745 | // parse the nonstandard "[alphapercent]" prefix |
767 | // parse the nonstandard "[alphapercent]" prefix |
746 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
768 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
747 | { |
769 | { |
748 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
770 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
749 | name += skip; |
771 | name += skip; |
750 | } |
772 | } |
751 | else |
|
|
752 | c.a = rgba::MAX_CC; |
|
|
753 | |
773 | |
754 | // parse the non-standard "#aarrggbb" format |
|
|
755 | if (name[0] == '#' && strlen (name) == 1+2+2+2+2 && 4 == sscanf (name+1, "%2hx%2hx%2hx%2hx%c", &c.a, &c.r, &c.g, &c.b, &eos)) |
|
|
756 | { |
|
|
757 | c.r <<= 8; |
|
|
758 | c.g <<= 8; |
|
|
759 | c.b <<= 8; |
|
|
760 | c.a <<= 8; |
|
|
761 | } |
|
|
762 | // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format |
774 | // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format |
763 | else if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
775 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
764 | { |
776 | { |
765 | XColor xc; |
777 | XColor xc; |
766 | |
778 | |
767 | if (XParseColor (screen->dpy, screen->cmap, name, &xc)) |
779 | if (XParseColor (screen->dpy, screen->cmap, name, &xc)) |
768 | { |
780 | { |
… | |
… | |
888 | lerp (c.a, to.a, percent) |
900 | lerp (c.a, to.a, percent) |
889 | ) |
901 | ) |
890 | ); |
902 | ); |
891 | } |
903 | } |
892 | |
904 | |
893 | #if TRACE_PIXMAPS |
905 | rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term) |
894 | # undef XCreatePixmap |
906 | : display (disp), request_time (tm), request_win (win), request_prop (prop), term (term) |
895 | # undef XFreePixmap |
|
|
896 | Pixmap trace_XCreatePixmap (const char *file, int line, Display *dpy, Window r, unsigned int w, unsigned int h, unsigned int d) |
|
|
897 | { |
907 | { |
898 | Pixmap res = XCreatePixmap (dpy, r, w, h, d); |
908 | assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); |
899 | fprintf (stderr, "%s:%d: XCreatePixmap (%p,%lX,%u,%u,%u) returned %lX\n", file, line, dpy, r, w, h, d, res); |
909 | |
|
|
910 | timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this); |
|
|
911 | timer_ev.repeat = 10.; |
|
|
912 | x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this); |
|
|
913 | |
|
|
914 | incr_buf = 0; |
|
|
915 | incr_buf_size = incr_buf_fill = 0; |
|
|
916 | selection_wait = Sel_normal; |
|
|
917 | selection_type = selnum; |
|
|
918 | cb_sv = 0; |
|
|
919 | } |
|
|
920 | |
|
|
921 | void |
|
|
922 | rxvt_selection::stop () |
|
|
923 | { |
|
|
924 | free (incr_buf); |
|
|
925 | incr_buf = 0; |
|
|
926 | timer_ev.stop (); |
|
|
927 | x_ev.stop (display); |
|
|
928 | } |
|
|
929 | |
|
|
930 | rxvt_selection::~rxvt_selection () |
|
|
931 | { |
|
|
932 | stop (); |
|
|
933 | } |
|
|
934 | |
|
|
935 | void |
|
|
936 | rxvt_selection::run () |
|
|
937 | { |
|
|
938 | int selnum = selection_type; |
|
|
939 | |
|
|
940 | #if ENABLE_FRILLS |
|
|
941 | if (selnum == Sel_Primary && display->selection_owner) |
|
|
942 | { |
|
|
943 | /* internal selection */ |
|
|
944 | char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len); |
|
|
945 | finish (str, strlen (str)); |
|
|
946 | free (str); |
|
|
947 | return; |
|
|
948 | } |
|
|
949 | #endif |
|
|
950 | |
|
|
951 | #if X_HAVE_UTF8_STRING |
|
|
952 | selection_type = Sel_UTF8String; |
|
|
953 | if (request (display->xa[XA_UTF8_STRING], selnum)) |
|
|
954 | return; |
|
|
955 | #else |
|
|
956 | selection_type = Sel_CompoundText; |
|
|
957 | if (request (display->xa[XA_COMPOUND_TEXT], selnum)) |
|
|
958 | return; |
|
|
959 | #endif |
|
|
960 | |
|
|
961 | // fallback to CUT_BUFFER0 if the requested property has no owner |
|
|
962 | handle_selection (display->root, XA_CUT_BUFFER0, false); |
|
|
963 | } |
|
|
964 | |
|
|
965 | void |
|
|
966 | rxvt_selection::finish (char *data, unsigned int len) |
|
|
967 | { |
|
|
968 | if (!cb_sv) |
|
|
969 | { |
|
|
970 | if (data) |
|
|
971 | term->paste (data, len); |
|
|
972 | |
|
|
973 | term->selection_req = 0; |
|
|
974 | delete this; |
|
|
975 | } |
|
|
976 | #if ENABLE_PERL |
|
|
977 | else |
|
|
978 | { |
|
|
979 | stop (); // we do not really trust perl callbacks |
|
|
980 | rxvt_perl.selection_finish (this, data, len); |
|
|
981 | } |
|
|
982 | #endif |
|
|
983 | } |
|
|
984 | |
|
|
985 | bool |
|
|
986 | rxvt_selection::request (Atom target, int selnum) |
|
|
987 | { |
|
|
988 | Atom sel; |
|
|
989 | |
|
|
990 | selection_type |= selnum; |
|
|
991 | |
|
|
992 | if (selnum == Sel_Primary) |
|
|
993 | sel = XA_PRIMARY; |
|
|
994 | else if (selnum == Sel_Secondary) |
|
|
995 | sel = XA_SECONDARY; |
|
|
996 | else |
|
|
997 | sel = display->xa[XA_CLIPBOARD]; |
|
|
998 | |
|
|
999 | if (XGetSelectionOwner (display->dpy, sel) != None) |
|
|
1000 | { |
|
|
1001 | XConvertSelection (display->dpy, sel, target, request_prop, |
|
|
1002 | request_win, request_time); |
|
|
1003 | x_ev.start (display, request_win); |
|
|
1004 | timer_ev.again (); |
|
|
1005 | return true; |
|
|
1006 | } |
|
|
1007 | |
900 | return res; |
1008 | return false; |
901 | } |
1009 | } |
902 | |
1010 | |
903 | void trace_XFreePixmap (const char *file, int line, Display *dpy, Pixmap p) |
1011 | void |
|
|
1012 | rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop) |
904 | { |
1013 | { |
905 | fprintf (stderr, "%s:%d: XFreePixmap (%p,%lX)\n", file, line, dpy, p); |
1014 | Display *dpy = display->dpy; |
906 | XFreePixmap (dpy,p); |
1015 | char *data = 0; |
907 | } |
1016 | unsigned int data_len = 0; |
|
|
1017 | unsigned long bytes_after; |
|
|
1018 | XTextProperty ct; |
|
|
1019 | |
|
|
1020 | // check for failed XConvertSelection |
|
|
1021 | if (prop == None) |
|
|
1022 | { |
|
|
1023 | bool error = true; |
|
|
1024 | int selnum = selection_type & Sel_whereMask; |
|
|
1025 | |
|
|
1026 | if (selection_type & Sel_CompoundText) |
|
|
1027 | { |
|
|
1028 | selection_type = 0; |
|
|
1029 | error = !request (XA_STRING, selnum); |
|
|
1030 | } |
|
|
1031 | |
|
|
1032 | if (selection_type & Sel_UTF8String) |
|
|
1033 | { |
|
|
1034 | selection_type = Sel_CompoundText; |
|
|
1035 | error = !request (display->xa[XA_COMPOUND_TEXT], selnum); |
|
|
1036 | } |
|
|
1037 | |
|
|
1038 | if (error) |
|
|
1039 | { |
|
|
1040 | ct.value = 0; |
|
|
1041 | goto bailout; |
|
|
1042 | } |
|
|
1043 | |
|
|
1044 | return; |
|
|
1045 | } |
|
|
1046 | |
|
|
1047 | // length == (2^31 - 1) / 4, as gdk |
|
|
1048 | if (XGetWindowProperty (dpy, win, prop, |
|
|
1049 | 0, 0x1fffffff, |
|
|
1050 | delete_prop, AnyPropertyType, |
|
|
1051 | &ct.encoding, &ct.format, |
|
|
1052 | &ct.nitems, &bytes_after, |
|
|
1053 | &ct.value) != Success) |
|
|
1054 | { |
|
|
1055 | ct.value = 0; |
|
|
1056 | goto bailout; |
|
|
1057 | } |
|
|
1058 | |
|
|
1059 | if (ct.encoding == None) |
|
|
1060 | goto bailout; |
|
|
1061 | |
|
|
1062 | if (ct.value == 0) |
|
|
1063 | goto bailout; |
|
|
1064 | |
|
|
1065 | if (ct.encoding == display->xa[XA_INCR]) |
|
|
1066 | { |
|
|
1067 | // INCR selection, start handshake |
|
|
1068 | if (!delete_prop) |
|
|
1069 | XDeleteProperty (dpy, win, prop); |
|
|
1070 | |
|
|
1071 | selection_wait = Sel_incr; |
|
|
1072 | timer_ev.again (); |
|
|
1073 | |
|
|
1074 | goto bailout; |
|
|
1075 | } |
|
|
1076 | |
|
|
1077 | if (ct.nitems == 0) |
|
|
1078 | { |
|
|
1079 | if (selection_wait == Sel_incr) |
|
|
1080 | { |
|
|
1081 | XFree (ct.value); |
|
|
1082 | |
|
|
1083 | // finally complete, now paste the whole thing |
|
|
1084 | selection_wait = Sel_normal; |
|
|
1085 | ct.value = (unsigned char *)incr_buf; |
|
|
1086 | ct.nitems = incr_buf_fill; |
|
|
1087 | incr_buf = 0; |
|
|
1088 | timer_ev.stop (); |
|
|
1089 | } |
|
|
1090 | else |
|
|
1091 | { |
|
|
1092 | // avoid recursion |
|
|
1093 | if (win != display->root || prop != XA_CUT_BUFFER0) |
|
|
1094 | { |
|
|
1095 | XFree (ct.value); |
|
|
1096 | |
|
|
1097 | // fallback to CUT_BUFFER0 if the requested property |
|
|
1098 | // has an owner but is empty |
|
|
1099 | handle_selection (display->root, XA_CUT_BUFFER0, False); |
|
|
1100 | return; |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | goto bailout; |
|
|
1104 | } |
|
|
1105 | } |
|
|
1106 | else if (selection_wait == Sel_incr) |
|
|
1107 | { |
|
|
1108 | timer_ev.again (); |
|
|
1109 | |
|
|
1110 | while (incr_buf_fill + ct.nitems > incr_buf_size) |
|
|
1111 | { |
|
|
1112 | incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024; |
|
|
1113 | incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size); |
|
|
1114 | } |
|
|
1115 | |
|
|
1116 | memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems); |
|
|
1117 | incr_buf_fill += ct.nitems; |
|
|
1118 | |
|
|
1119 | goto bailout; |
|
|
1120 | } |
|
|
1121 | |
|
|
1122 | char **cl; |
|
|
1123 | int cr; |
|
|
1124 | |
|
|
1125 | // we honour the first item only |
|
|
1126 | |
|
|
1127 | #if !ENABLE_MINIMAL |
|
|
1128 | // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it |
|
|
1129 | // so recode it manually |
|
|
1130 | if (ct.encoding == display->xa[XA_UTF8_STRING]) |
|
|
1131 | { |
|
|
1132 | wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems); |
|
|
1133 | data = rxvt_wcstombs (w); |
|
|
1134 | free (w); |
|
|
1135 | } |
|
|
1136 | else |
908 | #endif |
1137 | #endif |
|
|
1138 | if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0 |
|
|
1139 | && cl) |
|
|
1140 | { |
|
|
1141 | data = strdup (cl[0]); |
|
|
1142 | XFreeStringList (cl); |
|
|
1143 | } |
|
|
1144 | else |
|
|
1145 | { |
|
|
1146 | // paste raw |
|
|
1147 | data = strdup ((const char *)ct.value); |
|
|
1148 | } |
|
|
1149 | |
|
|
1150 | data_len = strlen (data); |
|
|
1151 | |
|
|
1152 | bailout: |
|
|
1153 | XFree (ct.value); |
|
|
1154 | |
|
|
1155 | if (selection_wait == Sel_normal) |
|
|
1156 | { |
|
|
1157 | finish (data, data_len); |
|
|
1158 | free (data); |
|
|
1159 | } |
|
|
1160 | } |
|
|
1161 | |
|
|
1162 | void |
|
|
1163 | rxvt_selection::timer_cb (ev::timer &w, int revents) |
|
|
1164 | { |
|
|
1165 | if (selection_wait == Sel_incr) |
|
|
1166 | rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); |
|
|
1167 | |
|
|
1168 | finish (); |
|
|
1169 | } |
|
|
1170 | |
|
|
1171 | void |
|
|
1172 | rxvt_selection::x_cb (XEvent &xev) |
|
|
1173 | { |
|
|
1174 | switch (xev.type) |
|
|
1175 | { |
|
|
1176 | case PropertyNotify: |
|
|
1177 | if (selection_wait == Sel_incr |
|
|
1178 | && xev.xproperty.atom == request_prop |
|
|
1179 | && xev.xproperty.state == PropertyNewValue) |
|
|
1180 | handle_selection (xev.xproperty.window, xev.xproperty.atom, true); |
|
|
1181 | break; |
|
|
1182 | |
|
|
1183 | case SelectionNotify: |
|
|
1184 | if (selection_wait == Sel_normal |
|
|
1185 | && xev.xselection.time == request_time) |
|
|
1186 | { |
|
|
1187 | timer_ev.stop (); |
|
|
1188 | handle_selection (xev.xselection.requestor, xev.xselection.property, true); |
|
|
1189 | } |
|
|
1190 | break; |
|
|
1191 | } |
|
|
1192 | } |