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.58 by root, Wed Jul 5 19:22:15 2006 UTC vs.
Revision 1.102 by root, Tue Nov 4 11:03:29 2008 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 ()
262 visual = DefaultVisualOfScreen (screen); 263 visual = DefaultVisualOfScreen (screen);
263 cmap = DefaultColormapOfScreen (screen); 264 cmap = DefaultColormapOfScreen (screen);
264} 265}
265 266
266void 267void
267rxvt_screen::set (rxvt_display *disp, int bitdepth) 268rxvt_screen::select_visual (int bitdepth)
268{ 269{
269 set (disp);
270
271#if XFT 270#if XFT
272 XVisualInfo vinfo; 271 XVisualInfo vinfo;
273 272
274 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) 273 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo))
275 { 274 {
276 depth = bitdepth; 275 depth = bitdepth;
277 visual = vinfo.visual; 276 visual = vinfo.visual;
278 cmap = XCreateColormap (dpy, disp->root, visual, AllocNone); 277 cmap = XCreateColormap (dpy, display->root, visual, AllocNone);
279 } 278 }
280#endif 279#endif
281} 280}
282 281
283void 282void
297 296
298///////////////////////////////////////////////////////////////////////////// 297/////////////////////////////////////////////////////////////////////////////
299 298
300rxvt_display::rxvt_display (const char *id) 299rxvt_display::rxvt_display (const char *id)
301: refcounted (id) 300: refcounted (id)
302, x_ev (this, &rxvt_display::x_cb)
303, selection_owner (0) 301, selection_owner (0)
304{ 302{
303 x_ev .set<rxvt_display, &rxvt_display::x_cb > (this);
304 flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this);
305} 305}
306 306
307XrmDatabase 307XrmDatabase
308rxvt_display::get_resources (bool refresh) 308rxvt_display::get_resources (bool refresh)
309{ 309{
310 char *homedir = (char *)getenv ("HOME"); 310 char *homedir = getenv ("HOME");
311 char fname[1024]; 311 char fname[1024];
312 312
313 /* 313 /*
314 * get resources using the X library function 314 * get resources using the X library function
315 */ 315 */
316 char *displayResource, *xe; 316 char *displayResource, *xe;
317 XrmDatabase rdb1, database = 0; 317 XrmDatabase rdb1, database = 0;
318 318
319 // 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
320 321
321 // 6. System wide per application default file. 322 // 6. System wide per application default file.
322 323
323 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ 324 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
324 if ((xe = (char *)getenv ("XAPPLRESDIR"))) 325 if ((xe = getenv ("XAPPLRESDIR")))
325 { 326 {
326 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); 327 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
327 328
328 if ((rdb1 = XrmGetFileDatabase (fname))) 329 if ((rdb1 = XrmGetFileDatabase (fname)))
329 XrmMergeDatabases (rdb1, &database); 330 XrmMergeDatabases (rdb1, &database);
331 332
332 // 5. User's per application default file. 333 // 5. User's per application default file.
333 // none 334 // none
334 335
335 // 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
336 /* Get any Xserver defaults */ 345 /* Get any Xserver defaults */
337 if (refresh) 346 if (refresh)
338 { 347 {
339 // fucking xlib keeps a copy of the rm string 348 // fucking xlib keeps a copy of the rm string
340 Atom actual_type; 349 Atom actual_type;
356 && actual_format == 8) 365 && actual_format == 8)
357 displayResource = val; 366 displayResource = val;
358 else 367 else
359 { 368 {
360 displayResource = 0; 369 displayResource = 0;
370
361 if (val) 371 if (val)
362 XFree(val); 372 XFree(val);
363 } 373 }
364 374
365#if XLIB_ILLEGAL_ACCESS 375#if XLIB_ILLEGAL_ACCESS
372 if (displayResource) 382 if (displayResource)
373 { 383 {
374 if ((rdb1 = XrmGetStringDatabase (displayResource))) 384 if ((rdb1 = XrmGetStringDatabase (displayResource)))
375 XrmMergeDatabases (rdb1, &database); 385 XrmMergeDatabases (rdb1, &database);
376 } 386 }
377 else if (homedir)
378 {
379 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
380
381 if ((rdb1 = XrmGetFileDatabase (fname)))
382 XrmMergeDatabases (rdb1, &database);
383 }
384 387
385#if !XLIB_ILLEGAL_ACCESS 388#if !XLIB_ILLEGAL_ACCESS
386 if (refresh && displayResource) 389 if (refresh && displayResource)
387 XFree (displayResource); 390 XFree (displayResource);
388#endif 391#endif
399 XFree (displayResource); 402 XFree (displayResource);
400 } 403 }
401 404
402 // 3. User's per host defaults file 405 // 3. User's per host defaults file
403 /* Add in XENVIRONMENT file */ 406 /* Add in XENVIRONMENT file */
404 if ((xe = (char *)getenv ("XENVIRONMENT")) 407 if ((xe = getenv ("XENVIRONMENT"))
405 && (rdb1 = XrmGetFileDatabase (xe))) 408 && (rdb1 = XrmGetFileDatabase (xe)))
406 XrmMergeDatabases (rdb1, &database); 409 XrmMergeDatabases (rdb1, &database);
407 else if (homedir) 410 else if (homedir)
408 { 411 {
409 struct utsname un; 412 struct utsname un;
461#endif 464#endif
462 465
463 int fd = XConnectionNumber (dpy); 466 int fd = XConnectionNumber (dpy);
464 467
465 // try to detect wether we have a local connection. 468 // try to detect wether we have a local connection.
466 // assume unix domains socket == local, everything else not 469 // assume unix domain socket == local, everything else not
467 // TODO: might want to check for inet/127.0.0.1 470 // TODO: might want to check for inet/127.0.0.1
468 is_local = 0; 471 is_local = 0;
469 sockaddr_un sa; 472 sockaddr_un sa;
470 socklen_t sl = sizeof (sa); 473 socklen_t sl = sizeof (sa);
471 474
472 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 475 if (!getsockname (fd, (sockaddr *)&sa, &sl))
473 is_local = sa.sun_family == AF_LOCAL; 476 is_local = sa.sun_family == AF_UNIX;
474 477
478 flush_ev.start ();
475 x_ev.start (fd, EVENT_READ); 479 x_ev.start (fd, ev::READ);
476 fcntl (fd, F_SETFD, FD_CLOEXEC); 480 fcntl (fd, F_SETFD, FD_CLOEXEC);
477 481
478 XSelectInput (dpy, root, PropertyChangeMask); 482 XSelectInput (dpy, root, PropertyChangeMask);
479 483
480 flush (); 484 flush ();
498 502
499#ifdef POINTER_BLANK 503#ifdef POINTER_BLANK
500 XFreeCursor (dpy, blank_cursor); 504 XFreeCursor (dpy, blank_cursor);
501#endif 505#endif
502 x_ev.stop (); 506 x_ev.stop ();
507 flush_ev.stop ();
503#ifdef USE_XIM 508#ifdef USE_XIM
504 xims.clear (); 509 xims.clear ();
505#endif 510#endif
506 XCloseDisplay (dpy); 511 XCloseDisplay (dpy);
507} 512}
537 542
538 XFree (atoms); 543 XFree (atoms);
539} 544}
540#endif 545#endif
541 546
542void rxvt_display::x_cb (io_watcher &w, short revents) 547void rxvt_display::x_cb (ev::io &w, int revents)
543{ 548{
549 flush_ev.start ();
550}
551
552void rxvt_display::flush_cb (ev::prepare &w, int revents)
553{
554 while (XEventsQueued (dpy, QueuedAfterFlush))
544 do 555 do
545 { 556 {
546 XEvent xev; 557 XEvent xev;
547 XNextEvent (dpy, &xev); 558 XNextEvent (dpy, &xev);
548 559
549#ifdef USE_XIM 560#ifdef USE_XIM
550 if (!XFilterEvent (&xev, None)) 561 if (!XFilterEvent (&xev, None))
551 { 562 {
552 if (xev.type == PropertyNotify 563 if (xev.type == PropertyNotify
553 && xev.xany.window == root 564 && xev.xany.window == root
554 && xev.xproperty.atom == xa[XA_XIM_SERVERS]) 565 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
555 im_change_check (); 566 im_change_check ();
556#endif 567#endif
568 if (xev.type == MappingNotify)
569 XRefreshKeyboardMapping (&xev.xmapping);
570
557 for (int i = xw.size (); i--; ) 571 for (int i = xw.size (); i--; )
558 { 572 {
559 if (!xw[i]) 573 if (!xw[i])
560 xw.erase_unordered (i); 574 xw.erase_unordered (i);
561 else if (xw[i]->window == xev.xany.window) 575 else if (xw[i]->window == xev.xany.window)
562 xw[i]->call (xev); 576 xw[i]->call (xev);
563 } 577 }
564#ifdef USE_XIM 578#ifdef USE_XIM
565 } 579 }
566#endif 580#endif
567 } 581 }
568 while (XEventsQueued (dpy, QueuedAlready)); 582 while (XEventsQueued (dpy, QueuedAlready));
569 583
570 XFlush (dpy); 584 w.stop ();
571}
572
573void rxvt_display::flush ()
574{
575 if (XEventsQueued (dpy, QueuedAlready))
576 x_cb (x_ev, EVENT_READ);
577
578 XFlush (dpy);
579} 585}
580 586
581void rxvt_display::reg (xevent_watcher *w) 587void rxvt_display::reg (xevent_watcher *w)
582{ 588{
589 if (!w->active)
590 {
583 xw.push_back (w); 591 xw.push_back (w);
584 w->active = xw.size (); 592 w->active = xw.size ();
593 }
585} 594}
586 595
587void rxvt_display::unreg (xevent_watcher *w) 596void rxvt_display::unreg (xevent_watcher *w)
588{ 597{
589 if (w->active) 598 if (w->active)
599 {
590 xw[w->active - 1] = 0; 600 xw[w->active - 1] = 0;
601 w->active = 0;
602 }
591} 603}
592 604
593void rxvt_display::set_selection_owner (rxvt_term *owner) 605void rxvt_display::set_selection_owner (rxvt_term *owner)
594{ 606{
595 if (selection_owner && selection_owner != owner) 607 if (selection_owner && selection_owner != owner)
608 {
609 rxvt_term *owner = selection_owner;
610
596 selection_owner->selection_clear (); 611 owner->selection_clear ();
612 owner->flush ();
613 }
597 614
598 selection_owner = owner; 615 selection_owner = owner;
599} 616}
600 617
601#ifdef USE_XIM 618#ifdef USE_XIM
656static unsigned int 673static unsigned int
657insert_component (unsigned int value, unsigned int mask, unsigned int shift) 674insert_component (unsigned int value, unsigned int mask, unsigned int shift)
658{ 675{
659 return (value * (mask + 1) >> 16) << shift; 676 return (value * (mask + 1) >> 16) << shift;
660} 677}
661 678
662bool 679bool
663rxvt_color::alloc (rxvt_screen *screen, const rgba &color) 680rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
664{ 681{
682 //TODO: only supports 24 bit
683 int alpha = color.a >= 0xff00 ? 0xffff : color.a;
684
665#if XFT 685#if XFT
666 XRenderPictFormat *format; 686 XRenderPictFormat *format;
667 687
668 // FUCKING Xft gets it wrong, of course, so work around it. 688 // FUCKING Xft gets it wrong, of course, so work around it.
669 // Transparency users should eat shit and die, and then 689 // Transparency users should eat shit and die, and then
673 { 693 {
674 // the fun lies in doing everything manually... 694 // the fun lies in doing everything manually...
675 c.color.red = color.r; 695 c.color.red = color.r;
676 c.color.green = color.g; 696 c.color.green = color.g;
677 c.color.blue = color.b; 697 c.color.blue = color.b;
678 c.color.alpha = color.a; 698 c.color.alpha = alpha;
679 699
680 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 )
681 | insert_component (color.g, format->direct.greenMask, format->direct.green) 701 | insert_component (color.g, format->direct.greenMask, format->direct.green)
682 | insert_component (color.b, format->direct.blueMask , format->direct.blue ) 702 | insert_component (color.b, format->direct.blueMask , format->direct.blue )
683 | insert_component (color.a, format->direct.alphaMask, format->direct.alpha); 703 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha);
684 704
685 return true; 705 return true;
686 } 706 }
687 else 707 else
688 { 708 {
689 XRenderColor d; 709 XRenderColor d;
690 710
691 d.red = color.r; 711 d.red = color.r;
692 d.green = color.g; 712 d.green = color.g;
693 d.blue = color.b; 713 d.blue = color.b;
694 d.alpha = color.a; 714 d.alpha = alpha;
695 715
696 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); 716 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c);
697 } 717 }
698#else 718#else
699 c.red = color.r; 719 c.red = color.r;
700 c.green = color.g; 720 c.green = color.g;
701 c.blue = color.b; 721 c.blue = color.b;
702 722
703 if (screen->visual->c_class == TrueColor) 723 if (screen->visual->c_class == TrueColor)
704 { 724 {
705 c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) 725 c.pixel = (color.r >> (16 - popcount (screen->visual->red_mask )) << ctz (screen->visual->red_mask ))
706 / rgba::MAX_CC) << ctz (screen->visual->red_mask ) 726 | (color.g >> (16 - popcount (screen->visual->green_mask)) << ctz (screen->visual->green_mask))
707 | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask))
708 / rgba::MAX_CC) << ctz (screen->visual->green_mask)
709 | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) 727 | (color.b >> (16 - popcount (screen->visual->blue_mask )) << ctz (screen->visual->blue_mask ));
710 / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
711 728
712 return true; 729 return true;
713 } 730 }
714 else if (XAllocColor (screen->dpy, screen->cmap, &c)) 731 else if (XAllocColor (screen->dpy, screen->cmap, &c))
715 return true; 732 return true;
727{ 744{
728 rgba c; 745 rgba c;
729 char eos; 746 char eos;
730 int skip; 747 int skip;
731 748
749 c.a = rgba::MAX_CC;
750
732 // parse the nonstandard "[alphapercent]" prefix 751 // parse the nonstandard "[alphapercent]" prefix
733 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) 752 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
734 { 753 {
735 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);
736 name += skip; 755 name += skip;
737 } 756 }
738 else
739 c.a = rgba::MAX_CC;
740 757
741 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format 758 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
742 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))
743 { 760 {
744 XColor xc, xc_exact; 761 XColor xc;
745 762
746 if (XParseColor (screen->dpy, screen->cmap, name, &xc)) 763 if (XParseColor (screen->dpy, screen->cmap, name, &xc))
747 { 764 {
748 c.r = xc.red; 765 c.r = xc.red;
749 c.g = xc.green; 766 c.g = xc.green;
776 { 793 {
777 XColor *colors = new XColor [screen->visual->map_entries]; 794 XColor *colors = new XColor [screen->visual->map_entries];
778 795
779 for (int i = 0; i < cmap_size; i++) 796 for (int i = 0; i < cmap_size; i++)
780 colors [i].pixel = i; 797 colors [i].pixel = i;
781 798
782 // many kilobytes transfer per colour, but pseudocolor isn't worth 799 // many kilobytes transfer per colour, but pseudocolor isn't worth
783 // many extra optimisations. 800 // many extra optimisations.
784 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); 801 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
785 802
786 int diff = 0x7fffffffUL; 803 int diff = 0x7fffffffUL;
799 } 816 }
800 } 817 }
801 818
802 //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",
803 // 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);
804 821
805 got = alloc (screen, rgba (best->red, best->green, best->blue)); 822 got = alloc (screen, rgba (best->red, best->green, best->blue));
806 823
807 delete colors; 824 delete [] colors;
808 } 825 }
809#endif 826#endif
810 827
811 return got; 828 return got;
812} 829}
837 color.green = c.g; 854 color.green = c.g;
838 color.blue = c.b; 855 color.blue = c.b;
839 color.pixel = (Pixel)*this; 856 color.pixel = (Pixel)*this;
840} 857}
841 858
842void 859void
843rxvt_color::free (rxvt_screen *screen) 860rxvt_color::free (rxvt_screen *screen)
844{ 861{
845 if (screen->visual->c_class == TrueColor) 862 if (screen->visual->c_class == TrueColor)
846 return; // nothing to do 863 return; // nothing to do
847 864
867 lerp (c.a, to.a, percent) 884 lerp (c.a, to.a, percent)
868 ) 885 )
869 ); 886 );
870} 887}
871 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