ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
(Generate patch)

Comparing rxvt-unicode/src/rxvttoolkit.C (file contents):
Revision 1.59 by root, Wed Jul 5 20:31:48 2006 UTC vs.
Revision 1.103 by root, Fri Sep 4 15:40:13 2009 UTC

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-2006 Marc Lehmann <pcg@goof.com> 6 * Copyright (c) 2003-2007 Marc Lehmann <pcg@goof.com>
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * 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 9 * 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 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
59#if ENABLE_EWMH 59#if ENABLE_EWMH
60 "_NET_WM_PID", 60 "_NET_WM_PID",
61 "_NET_WM_NAME", 61 "_NET_WM_NAME",
62 "_NET_WM_ICON_NAME", 62 "_NET_WM_ICON_NAME",
63 "_NET_WM_PING", 63 "_NET_WM_PING",
64 "_NET_WM_ICON",
64#endif 65#endif
65#if USE_XIM 66#if USE_XIM
66 "WM_LOCALE_NAME", 67 "WM_LOCALE_NAME",
67 "XIM_SERVERS", 68 "XIM_SERVERS",
68#endif 69#endif
69#ifdef TRANSPARENT 70#ifdef ENABLE_TRANSPARENCY
70 "_XROOTPMAP_ID", 71 "_XROOTPMAP_ID",
71 "ESETROOT_PMAP_ID", 72 "ESETROOT_PMAP_ID",
72#endif 73#endif
73#if ENABLE_XEMBED 74#if ENABLE_XEMBED
74 "_XEMBED", 75 "_XEMBED",
183 184
184 XIMCallback ximcallback; 185 XIMCallback ximcallback;
185 ximcallback.client_data = (XPointer)this; 186 ximcallback.client_data = (XPointer)this;
186 ximcallback.callback = im_destroy_cb; 187 ximcallback.callback = im_destroy_cb;
187 188
188 XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0); 189 XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0);
189 190
190 return true; 191 return true;
191} 192}
192 193
193rxvt_xim::~rxvt_xim () 194rxvt_xim::~rxvt_xim ()
295 296
296///////////////////////////////////////////////////////////////////////////// 297/////////////////////////////////////////////////////////////////////////////
297 298
298rxvt_display::rxvt_display (const char *id) 299rxvt_display::rxvt_display (const char *id)
299: refcounted (id) 300: refcounted (id)
300, x_ev (this, &rxvt_display::x_cb)
301, selection_owner (0) 301, selection_owner (0)
302{ 302{
303 x_ev .set<rxvt_display, &rxvt_display::x_cb > (this);
304 flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this);
303} 305}
304 306
305XrmDatabase 307XrmDatabase
306rxvt_display::get_resources (bool refresh) 308rxvt_display::get_resources (bool refresh)
307{ 309{
308 char *homedir = (char *)getenv ("HOME"); 310 char *homedir = getenv ("HOME");
309 char fname[1024]; 311 char fname[1024];
310 312
311 /* 313 /*
312 * get resources using the X library function 314 * get resources using the X library function
313 */ 315 */
314 char *displayResource, *xe; 316 char *displayResource, *xe;
315 XrmDatabase rdb1, database = 0; 317 XrmDatabase rdb1, database = 0;
316 318
317 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 319 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
320 // as opposed to "standard practise", we always read in ~/.Xdefaults
318 321
319 // 6. System wide per application default file. 322 // 6. System wide per application default file.
320 323
321 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ 324 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
322 if ((xe = (char *)getenv ("XAPPLRESDIR"))) 325 if ((xe = getenv ("XAPPLRESDIR")))
323 { 326 {
324 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); 327 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
325 328
326 if ((rdb1 = XrmGetFileDatabase (fname))) 329 if ((rdb1 = XrmGetFileDatabase (fname)))
327 XrmMergeDatabases (rdb1, &database); 330 XrmMergeDatabases (rdb1, &database);
329 332
330 // 5. User's per application default file. 333 // 5. User's per application default file.
331 // none 334 // none
332 335
333 // 4. User's defaults file. 336 // 4. User's defaults file.
337 if (homedir)
338 {
339 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
340
341 if ((rdb1 = XrmGetFileDatabase (fname)))
342 XrmMergeDatabases (rdb1, &database);
343 }
344
334 /* Get any Xserver defaults */ 345 /* Get any Xserver defaults */
335 if (refresh) 346 if (refresh)
336 { 347 {
337 // fucking xlib keeps a copy of the rm string 348 // fucking xlib keeps a copy of the rm string
338 Atom actual_type; 349 Atom actual_type;
354 && actual_format == 8) 365 && actual_format == 8)
355 displayResource = val; 366 displayResource = val;
356 else 367 else
357 { 368 {
358 displayResource = 0; 369 displayResource = 0;
370
359 if (val) 371 if (val)
360 XFree(val); 372 XFree(val);
361 } 373 }
362 374
363#if XLIB_ILLEGAL_ACCESS 375#if XLIB_ILLEGAL_ACCESS
370 if (displayResource) 382 if (displayResource)
371 { 383 {
372 if ((rdb1 = XrmGetStringDatabase (displayResource))) 384 if ((rdb1 = XrmGetStringDatabase (displayResource)))
373 XrmMergeDatabases (rdb1, &database); 385 XrmMergeDatabases (rdb1, &database);
374 } 386 }
375 else if (homedir)
376 {
377 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
378
379 if ((rdb1 = XrmGetFileDatabase (fname)))
380 XrmMergeDatabases (rdb1, &database);
381 }
382 387
383#if !XLIB_ILLEGAL_ACCESS 388#if !XLIB_ILLEGAL_ACCESS
384 if (refresh && displayResource) 389 if (refresh && displayResource)
385 XFree (displayResource); 390 XFree (displayResource);
386#endif 391#endif
397 XFree (displayResource); 402 XFree (displayResource);
398 } 403 }
399 404
400 // 3. User's per host defaults file 405 // 3. User's per host defaults file
401 /* Add in XENVIRONMENT file */ 406 /* Add in XENVIRONMENT file */
402 if ((xe = (char *)getenv ("XENVIRONMENT")) 407 if ((xe = getenv ("XENVIRONMENT"))
403 && (rdb1 = XrmGetFileDatabase (xe))) 408 && (rdb1 = XrmGetFileDatabase (xe)))
404 XrmMergeDatabases (rdb1, &database); 409 XrmMergeDatabases (rdb1, &database);
405 else if (homedir) 410 else if (homedir)
406 { 411 {
407 struct utsname un; 412 struct utsname un;
459#endif 464#endif
460 465
461 int fd = XConnectionNumber (dpy); 466 int fd = XConnectionNumber (dpy);
462 467
463 // try to detect wether we have a local connection. 468 // try to detect wether we have a local connection.
464 // assume unix domains socket == local, everything else not 469 // assume unix domain socket == local, everything else not
465 // TODO: might want to check for inet/127.0.0.1 470 // TODO: might want to check for inet/127.0.0.1
466 is_local = 0; 471 is_local = 0;
467 sockaddr_un sa; 472 sockaddr_un sa;
468 socklen_t sl = sizeof (sa); 473 socklen_t sl = sizeof (sa);
469 474
470 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 475 if (!getsockname (fd, (sockaddr *)&sa, &sl))
471 is_local = sa.sun_family == AF_LOCAL; 476 is_local = sa.sun_family == AF_UNIX;
472 477
478 flush_ev.start ();
473 x_ev.start (fd, EVENT_READ); 479 x_ev.start (fd, ev::READ);
474 fcntl (fd, F_SETFD, FD_CLOEXEC); 480 fcntl (fd, F_SETFD, FD_CLOEXEC);
475 481
476 XSelectInput (dpy, root, PropertyChangeMask); 482 XSelectInput (dpy, root, PropertyChangeMask);
477 483
478 flush (); 484 flush ();
496 502
497#ifdef POINTER_BLANK 503#ifdef POINTER_BLANK
498 XFreeCursor (dpy, blank_cursor); 504 XFreeCursor (dpy, blank_cursor);
499#endif 505#endif
500 x_ev.stop (); 506 x_ev.stop ();
507 flush_ev.stop ();
501#ifdef USE_XIM 508#ifdef USE_XIM
502 xims.clear (); 509 xims.clear ();
503#endif 510#endif
504 XCloseDisplay (dpy); 511 XCloseDisplay (dpy);
505} 512}
535 542
536 XFree (atoms); 543 XFree (atoms);
537} 544}
538#endif 545#endif
539 546
540void rxvt_display::x_cb (io_watcher &w, short revents) 547void rxvt_display::x_cb (ev::io &w, int revents)
541{ 548{
549 flush_ev.start ();
550}
551
552void rxvt_display::flush_cb (ev::prepare &w, int revents)
553{
554 while (XEventsQueued (dpy, QueuedAfterFlush))
542 do 555 do
543 { 556 {
544 XEvent xev; 557 XEvent xev;
545 XNextEvent (dpy, &xev); 558 XNextEvent (dpy, &xev);
546 559
547#ifdef USE_XIM 560#ifdef USE_XIM
548 if (!XFilterEvent (&xev, None)) 561 if (!XFilterEvent (&xev, None))
549 { 562 {
550 if (xev.type == PropertyNotify 563 if (xev.type == PropertyNotify
551 && xev.xany.window == root 564 && xev.xany.window == root
552 && xev.xproperty.atom == xa[XA_XIM_SERVERS]) 565 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
553 im_change_check (); 566 im_change_check ();
554#endif 567#endif
568 if (xev.type == MappingNotify)
569 XRefreshKeyboardMapping (&xev.xmapping);
570
555 for (int i = xw.size (); i--; ) 571 for (int i = xw.size (); i--; )
556 { 572 {
557 if (!xw[i]) 573 if (!xw[i])
558 xw.erase_unordered (i); 574 xw.erase_unordered (i);
559 else if (xw[i]->window == xev.xany.window) 575 else if (xw[i]->window == xev.xany.window)
560 xw[i]->call (xev); 576 xw[i]->call (xev);
561 } 577 }
562#ifdef USE_XIM 578#ifdef USE_XIM
563 } 579 }
564#endif 580#endif
565 } 581 }
566 while (XEventsQueued (dpy, QueuedAlready)); 582 while (XEventsQueued (dpy, QueuedAlready));
567 583
568 XFlush (dpy); 584 w.stop ();
569}
570
571void rxvt_display::flush ()
572{
573 if (XEventsQueued (dpy, QueuedAlready))
574 x_cb (x_ev, EVENT_READ);
575
576 XFlush (dpy);
577} 585}
578 586
579void rxvt_display::reg (xevent_watcher *w) 587void rxvt_display::reg (xevent_watcher *w)
580{ 588{
589 if (!w->active)
590 {
581 xw.push_back (w); 591 xw.push_back (w);
582 w->active = xw.size (); 592 w->active = xw.size ();
593 }
583} 594}
584 595
585void rxvt_display::unreg (xevent_watcher *w) 596void rxvt_display::unreg (xevent_watcher *w)
586{ 597{
587 if (w->active) 598 if (w->active)
599 {
588 xw[w->active - 1] = 0; 600 xw[w->active - 1] = 0;
601 w->active = 0;
602 }
589} 603}
590 604
591void rxvt_display::set_selection_owner (rxvt_term *owner) 605void rxvt_display::set_selection_owner (rxvt_term *owner)
592{ 606{
593 if (selection_owner && selection_owner != owner) 607 if (selection_owner && selection_owner != owner)
608 {
609 rxvt_term *owner = selection_owner;
610
594 selection_owner->selection_clear (); 611 owner->selection_clear ();
612 owner->flush ();
613 }
595 614
596 selection_owner = owner; 615 selection_owner = owner;
597} 616}
598 617
599#ifdef USE_XIM 618#ifdef USE_XIM
654static unsigned int 673static unsigned int
655insert_component (unsigned int value, unsigned int mask, unsigned int shift) 674insert_component (unsigned int value, unsigned int mask, unsigned int shift)
656{ 675{
657 return (value * (mask + 1) >> 16) << shift; 676 return (value * (mask + 1) >> 16) << shift;
658} 677}
659 678
660bool 679bool
661rxvt_color::alloc (rxvt_screen *screen, const rgba &color) 680rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
662{ 681{
682 //TODO: only supports 24 bit
683 int alpha = color.a >= 0xff00 ? 0xffff : color.a;
684
663#if XFT 685#if XFT
664 XRenderPictFormat *format; 686 XRenderPictFormat *format;
665 687
666 // FUCKING Xft gets it wrong, of course, so work around it. 688 // FUCKING Xft gets it wrong, of course, so work around it.
667 // Transparency users should eat shit and die, and then 689 // Transparency users should eat shit and die, and then
671 { 693 {
672 // the fun lies in doing everything manually... 694 // the fun lies in doing everything manually...
673 c.color.red = color.r; 695 c.color.red = color.r;
674 c.color.green = color.g; 696 c.color.green = color.g;
675 c.color.blue = color.b; 697 c.color.blue = color.b;
676 c.color.alpha = color.a; 698 c.color.alpha = alpha;
677 699
678 c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red ) 700 c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red )
679 | insert_component (color.g, format->direct.greenMask, format->direct.green) 701 | insert_component (color.g, format->direct.greenMask, format->direct.green)
680 | insert_component (color.b, format->direct.blueMask , format->direct.blue ) 702 | insert_component (color.b, format->direct.blueMask , format->direct.blue )
681 | insert_component (color.a, format->direct.alphaMask, format->direct.alpha); 703 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha);
682 704
683 return true; 705 return true;
684 } 706 }
685 else 707 else
686 { 708 {
687 XRenderColor d; 709 XRenderColor d;
688 710
689 d.red = color.r; 711 d.red = color.r;
690 d.green = color.g; 712 d.green = color.g;
691 d.blue = color.b; 713 d.blue = color.b;
692 d.alpha = color.a; 714 d.alpha = alpha;
693 715
694 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); 716 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c);
695 } 717 }
696#else 718#else
697 c.red = color.r; 719 c.red = color.r;
698 c.green = color.g; 720 c.green = color.g;
699 c.blue = color.b; 721 c.blue = color.b;
700 722
701 if (screen->visual->c_class == TrueColor) 723 if (screen->visual->c_class == TrueColor)
702 { 724 {
703 c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) 725 c.pixel = (color.r >> (16 - rxvt_popcount (screen->visual->red_mask )) << rxvt_ctz (screen->visual->red_mask ))
704 / rgba::MAX_CC) << ctz (screen->visual->red_mask ) 726 | (color.g >> (16 - rxvt_popcount (screen->visual->green_mask)) << rxvt_ctz (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 )) 727 | (color.b >> (16 - rxvt_popcount (screen->visual->blue_mask )) << rxvt_ctz (screen->visual->blue_mask ));
708 / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
709 728
710 return true; 729 return true;
711 } 730 }
712 else if (XAllocColor (screen->dpy, screen->cmap, &c)) 731 else if (XAllocColor (screen->dpy, screen->cmap, &c))
713 return true; 732 return true;
725{ 744{
726 rgba c; 745 rgba c;
727 char eos; 746 char eos;
728 int skip; 747 int skip;
729 748
749 c.a = rgba::MAX_CC;
750
730 // parse the nonstandard "[alphapercent]" prefix 751 // parse the nonstandard "[alphapercent]" prefix
731 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) 752 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
732 { 753 {
733 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); 754 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a);
734 name += skip; 755 name += skip;
735 } 756 }
736 else
737 c.a = rgba::MAX_CC;
738 757
739 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format 758 // 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)) 759 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 { 760 {
742 XColor xc, xc_exact; 761 XColor xc;
743 762
744 if (XParseColor (screen->dpy, screen->cmap, name, &xc)) 763 if (XParseColor (screen->dpy, screen->cmap, name, &xc))
745 { 764 {
746 c.r = xc.red; 765 c.r = xc.red;
747 c.g = xc.green; 766 c.g = xc.green;
774 { 793 {
775 XColor *colors = new XColor [screen->visual->map_entries]; 794 XColor *colors = new XColor [screen->visual->map_entries];
776 795
777 for (int i = 0; i < cmap_size; i++) 796 for (int i = 0; i < cmap_size; i++)
778 colors [i].pixel = i; 797 colors [i].pixel = i;
779 798
780 // many kilobytes transfer per colour, but pseudocolor isn't worth 799 // many kilobytes transfer per colour, but pseudocolor isn't worth
781 // many extra optimisations. 800 // many extra optimisations.
782 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); 801 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
783 802
784 int diff = 0x7fffffffUL; 803 int diff = 0x7fffffffUL;
797 } 816 }
798 } 817 }
799 818
800 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", 819 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n",
801 // color.r, color.g, color.b, best->red, best->green, best->blue, diff); 820 // color.r, color.g, color.b, best->red, best->green, best->blue, diff);
802 821
803 got = alloc (screen, rgba (best->red, best->green, best->blue)); 822 got = alloc (screen, rgba (best->red, best->green, best->blue));
804 823
805 delete colors; 824 delete [] colors;
806 } 825 }
807#endif 826#endif
808 827
809 return got; 828 return got;
810} 829}
835 color.green = c.g; 854 color.green = c.g;
836 color.blue = c.b; 855 color.blue = c.b;
837 color.pixel = (Pixel)*this; 856 color.pixel = (Pixel)*this;
838} 857}
839 858
840void 859void
841rxvt_color::free (rxvt_screen *screen) 860rxvt_color::free (rxvt_screen *screen)
842{ 861{
843 if (screen->visual->c_class == TrueColor) 862 if (screen->visual->c_class == TrueColor)
844 return; // nothing to do 863 return; // nothing to do
845 864
865 lerp (c.a, to.a, percent) 884 lerp (c.a, to.a, percent)
866 ) 885 )
867 ); 886 );
868} 887}
869 888
889#if TRACE_PIXMAPS
890# undef XCreatePixmap
891# undef XFreePixmap
892Pixmap trace_XCreatePixmap (const char *file, int line, Display *dpy, Window r, unsigned int w, unsigned int h, unsigned int d)
893{
894 Pixmap res = XCreatePixmap (dpy, r, w, h, d);
895 fprintf (stderr, "%s:%d: XCreatePixmap (%p,%lX,%u,%u,%u) returned %lX\n", file, line, dpy, r, w, h, d, res);
896 return res;
897}
898
899void trace_XFreePixmap (const char *file, int line, Display *dpy, Pixmap p)
900{
901 fprintf (stderr, "%s:%d: XFreePixmap (%p,%lX)\n", file, line, dpy, p);
902 XFreePixmap (dpy,p);
903}
904#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines