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.55 by root, Sun Feb 19 16:55:03 2006 UTC vs.
Revision 1.82 by root, Fri Dec 14 05:25:26 2007 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*----------------------------------------------------------------------*
2 * File: rxvttoolkit.C 2 * File: rxvttoolkit.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> 6 * Copyright (c) 2003-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.
64#endif 64#endif
65#if USE_XIM 65#if USE_XIM
66 "WM_LOCALE_NAME", 66 "WM_LOCALE_NAME",
67 "XIM_SERVERS", 67 "XIM_SERVERS",
68#endif 68#endif
69#ifdef TRANSPARENT 69#ifdef ENABLE_TRANSPARENCY
70 "_XROOTPMAP_ID", 70 "_XROOTPMAP_ID",
71 "ESETROOT_PMAP_ID", 71 "ESETROOT_PMAP_ID",
72#endif 72#endif
73#if ENABLE_XEMBED 73#if ENABLE_XEMBED
74 "_XEMBED", 74 "_XEMBED",
183 183
184 XIMCallback ximcallback; 184 XIMCallback ximcallback;
185 ximcallback.client_data = (XPointer)this; 185 ximcallback.client_data = (XPointer)this;
186 ximcallback.callback = im_destroy_cb; 186 ximcallback.callback = im_destroy_cb;
187 187
188 XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0); 188 XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0);
189 189
190 return true; 190 return true;
191} 191}
192 192
193rxvt_xim::~rxvt_xim () 193rxvt_xim::~rxvt_xim ()
262 visual = DefaultVisualOfScreen (screen); 262 visual = DefaultVisualOfScreen (screen);
263 cmap = DefaultColormapOfScreen (screen); 263 cmap = DefaultColormapOfScreen (screen);
264} 264}
265 265
266void 266void
267rxvt_screen::set (rxvt_display *disp, int bitdepth) 267rxvt_screen::select_visual (int bitdepth)
268{ 268{
269 set (disp);
270
271#if XFT 269#if XFT
272 XVisualInfo vinfo; 270 XVisualInfo vinfo;
273 271
274 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) 272 if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo))
275 { 273 {
276 depth = bitdepth; 274 depth = bitdepth;
277 visual = vinfo.visual; 275 visual = vinfo.visual;
278 cmap = XCreateColormap (dpy, disp->root, visual, AllocNone); 276 cmap = XCreateColormap (dpy, display->root, visual, AllocNone);
279 } 277 }
280#endif 278#endif
281} 279}
282 280
283void 281void
297 295
298///////////////////////////////////////////////////////////////////////////// 296/////////////////////////////////////////////////////////////////////////////
299 297
300rxvt_display::rxvt_display (const char *id) 298rxvt_display::rxvt_display (const char *id)
301: refcounted (id) 299: refcounted (id)
302, x_ev (this, &rxvt_display::x_cb)
303, selection_owner (0) 300, selection_owner (0)
304{ 301{
302 x_ev .set<rxvt_display, &rxvt_display::x_cb > (this);
303 flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this);
305} 304}
306 305
307XrmDatabase 306XrmDatabase
308rxvt_display::get_resources (bool refresh) 307rxvt_display::get_resources (bool refresh)
309{ 308{
310 char *homedir = (char *)getenv ("HOME"); 309 char *homedir = getenv ("HOME");
311 char fname[1024]; 310 char fname[1024];
312 311
313 /* 312 /*
314 * get resources using the X library function 313 * get resources using the X library function
315 */ 314 */
316 char *displayResource, *xe; 315 char *displayResource, *xe;
317 XrmDatabase rdb1, database = 0; 316 XrmDatabase rdb1, database = 0;
318 317
319 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 318 // 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
320 320
321 // 6. System wide per application default file. 321 // 6. System wide per application default file.
322 322
323 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ 323 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
324 if ((xe = (char *)getenv ("XAPPLRESDIR"))) 324 if ((xe = getenv ("XAPPLRESDIR")))
325 { 325 {
326 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); 326 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
327 327
328 if ((rdb1 = XrmGetFileDatabase (fname))) 328 if ((rdb1 = XrmGetFileDatabase (fname)))
329 XrmMergeDatabases (rdb1, &database); 329 XrmMergeDatabases (rdb1, &database);
331 331
332 // 5. User's per application default file. 332 // 5. User's per application default file.
333 // none 333 // none
334 334
335 // 4. User's defaults file. 335 // 4. User's defaults file.
336 if (homedir)
337 {
338 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
339
340 if ((rdb1 = XrmGetFileDatabase (fname)))
341 XrmMergeDatabases (rdb1, &database);
342 }
343
336 /* Get any Xserver defaults */ 344 /* Get any Xserver defaults */
337 if (refresh) 345 if (refresh)
338 { 346 {
339 // fucking xlib keeps a copy of the rm string 347 // fucking xlib keeps a copy of the rm string
340 Atom actual_type; 348 Atom actual_type;
356 && actual_format == 8) 364 && actual_format == 8)
357 displayResource = val; 365 displayResource = val;
358 else 366 else
359 { 367 {
360 displayResource = 0; 368 displayResource = 0;
369
361 if (val) 370 if (val)
362 XFree(val); 371 XFree(val);
363 } 372 }
364 373
365#if XLIB_ILLEGAL_ACCESS 374#if XLIB_ILLEGAL_ACCESS
372 if (displayResource) 381 if (displayResource)
373 { 382 {
374 if ((rdb1 = XrmGetStringDatabase (displayResource))) 383 if ((rdb1 = XrmGetStringDatabase (displayResource)))
375 XrmMergeDatabases (rdb1, &database); 384 XrmMergeDatabases (rdb1, &database);
376 } 385 }
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 386
385#if !XLIB_ILLEGAL_ACCESS 387#if !XLIB_ILLEGAL_ACCESS
386 if (refresh && displayResource) 388 if (refresh && displayResource)
387 XFree (displayResource); 389 XFree (displayResource);
388#endif 390#endif
399 XFree (displayResource); 401 XFree (displayResource);
400 } 402 }
401 403
402 // 3. User's per host defaults file 404 // 3. User's per host defaults file
403 /* Add in XENVIRONMENT file */ 405 /* Add in XENVIRONMENT file */
404 if ((xe = (char *)getenv ("XENVIRONMENT")) 406 if ((xe = getenv ("XENVIRONMENT"))
405 && (rdb1 = XrmGetFileDatabase (xe))) 407 && (rdb1 = XrmGetFileDatabase (xe)))
406 XrmMergeDatabases (rdb1, &database); 408 XrmMergeDatabases (rdb1, &database);
407 else if (homedir) 409 else if (homedir)
408 { 410 {
409 struct utsname un; 411 struct utsname un;
461#endif 463#endif
462 464
463 int fd = XConnectionNumber (dpy); 465 int fd = XConnectionNumber (dpy);
464 466
465 // try to detect wether we have a local connection. 467 // try to detect wether we have a local connection.
466 // assume unix domains socket == local, everything else not 468 // assume unix domain socket == local, everything else not
467 // TODO: might want to check for inet/127.0.0.1 469 // TODO: might want to check for inet/127.0.0.1
468 is_local = 0; 470 is_local = 0;
469 sockaddr_un sa; 471 sockaddr_un sa;
470 socklen_t sl = sizeof (sa); 472 socklen_t sl = sizeof (sa);
471 473
472 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 474 if (!getsockname (fd, (sockaddr *)&sa, &sl))
473 is_local = sa.sun_family == AF_LOCAL; 475 is_local = sa.sun_family == AF_UNIX;
474 476
477 flush_ev.start ();
475 x_ev.start (fd, EVENT_READ); 478 x_ev.start (fd, ev::READ);
476 fcntl (fd, F_SETFD, FD_CLOEXEC); 479 fcntl (fd, F_SETFD, FD_CLOEXEC);
477 480
478 XSelectInput (dpy, root, PropertyChangeMask); 481 XSelectInput (dpy, root, PropertyChangeMask);
479 482
480 flush (); 483 flush ();
537 540
538 XFree (atoms); 541 XFree (atoms);
539} 542}
540#endif 543#endif
541 544
542void rxvt_display::x_cb (io_watcher &w, short revents) 545void rxvt_display::x_cb (ev::io &w, int revents)
543{ 546{
544 do 547 while (XEventsQueued (dpy, QueuedAfterReading))
545 { 548 {
546 XEvent xev; 549 XEvent xev;
547 XNextEvent (dpy, &xev); 550 XNextEvent (dpy, &xev);
548 551
549#ifdef USE_XIM 552#ifdef USE_XIM
552 if (xev.type == PropertyNotify 555 if (xev.type == PropertyNotify
553 && xev.xany.window == root 556 && xev.xany.window == root
554 && xev.xproperty.atom == xa[XA_XIM_SERVERS]) 557 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
555 im_change_check (); 558 im_change_check ();
556#endif 559#endif
560 if (xev.type == MappingNotify)
561 XRefreshKeyboardMapping (&xev.xmapping);
562
557 for (int i = xw.size (); i--; ) 563 for (int i = xw.size (); i--; )
558 { 564 {
559 if (!xw[i]) 565 if (!xw[i])
560 xw.erase_unordered (i); 566 xw.erase_unordered (i);
561 else if (xw[i]->window == xev.xany.window) 567 else if (xw[i]->window == xev.xany.window)
563 } 569 }
564#ifdef USE_XIM 570#ifdef USE_XIM
565 } 571 }
566#endif 572#endif
567 } 573 }
568 while (XEventsQueued (dpy, QueuedAlready)); 574}
569 575
576void rxvt_display::flush ()
577{
570 XFlush (dpy); 578 XFlush (dpy);
571} 579}
572 580
573void rxvt_display::flush () 581void rxvt_display::flush_cb (ev::prepare &w, int revents)
574{ 582{
575 if (XEventsQueued (dpy, QueuedAlready))
576 x_cb (x_ev, EVENT_READ);
577
578 XFlush (dpy); 583 flush ();
579} 584}
580 585
581void rxvt_display::reg (xevent_watcher *w) 586void rxvt_display::reg (xevent_watcher *w)
582{ 587{
588 if (!w->active)
589 {
583 xw.push_back (w); 590 xw.push_back (w);
584 w->active = xw.size (); 591 w->active = xw.size ();
592 }
585} 593}
586 594
587void rxvt_display::unreg (xevent_watcher *w) 595void rxvt_display::unreg (xevent_watcher *w)
588{ 596{
589 if (w->active) 597 if (w->active)
598 {
590 xw[w->active - 1] = 0; 599 xw[w->active - 1] = 0;
600 w->active = 0;
601 }
591} 602}
592 603
593void rxvt_display::set_selection_owner (rxvt_term *owner) 604void rxvt_display::set_selection_owner (rxvt_term *owner)
594{ 605{
595 if (selection_owner && selection_owner != owner) 606 if (selection_owner && selection_owner != owner)
649 660
650template class refcache<rxvt_display>; 661template class refcache<rxvt_display>;
651refcache<rxvt_display> displays; 662refcache<rxvt_display> displays;
652 663
653///////////////////////////////////////////////////////////////////////////// 664/////////////////////////////////////////////////////////////////////////////
654 665//
666
667static unsigned int
668insert_component (unsigned int value, unsigned int mask, unsigned int shift)
669{
670 return (value * (mask + 1) >> 16) << shift;
671}
672
655bool 673bool
656rxvt_color::alloc (rxvt_screen *screen, const rgba &color) 674rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
657{ 675{
658#if XFT 676#if XFT
659 XRenderPictFormat *format; 677 XRenderPictFormat *format;
668 c.color.red = color.r; 686 c.color.red = color.r;
669 c.color.green = color.g; 687 c.color.green = color.g;
670 c.color.blue = color.b; 688 c.color.blue = color.b;
671 c.color.alpha = color.a; 689 c.color.alpha = color.a;
672 690
673 c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red ) 691 c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red )
674 | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green) 692 | insert_component (color.g, format->direct.greenMask, format->direct.green)
675 | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue ) 693 | insert_component (color.b, format->direct.blueMask , format->direct.blue )
676 | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha); 694 | insert_component (color.a, format->direct.alphaMask, format->direct.alpha);
677 695
678 return true; 696 return true;
679 } 697 }
680 else 698 else
681 { 699 {
693 c.green = color.g; 711 c.green = color.g;
694 c.blue = color.b; 712 c.blue = color.b;
695 713
696 if (screen->visual->c_class == TrueColor) 714 if (screen->visual->c_class == TrueColor)
697 { 715 {
698 c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) 716 c.pixel = (color.r >> (16 - popcount (screen->visual->red_mask )) << ctz (screen->visual->red_mask ))
699 / rgba::MAX_CC) << ctz (screen->visual->red_mask ) 717 | (color.g >> (16 - popcount (screen->visual->green_mask)) << ctz (screen->visual->green_mask))
700 | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask))
701 / rgba::MAX_CC) << ctz (screen->visual->green_mask)
702 | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) 718 | (color.b >> (16 - popcount (screen->visual->blue_mask )) << ctz (screen->visual->blue_mask ));
703 / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
704 719
705 return true; 720 return true;
706 } 721 }
707 else if (XAllocColor (screen->dpy, screen->cmap, &c)) 722 else if (XAllocColor (screen->dpy, screen->cmap, &c))
708 return true; 723 return true;
729 name += skip; 744 name += skip;
730 } 745 }
731 else 746 else
732 c.a = rgba::MAX_CC; 747 c.a = rgba::MAX_CC;
733 748
749 // parse the non-standard "#aarrggbb" format
750 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))
751 {
752 if (c.r)
753 c.r = (c.r << 8) | 0x0ff;
754 if (c.g)
755 c.g = (c.g << 8) | 0x0ff;
756 if (c.b)
757 c.b = (c.b << 8) | 0x0ff;
758 if (c.a)
759 c.a = (c.a << 8) | 0x0ff;
760 }
734 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format 761 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
735 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) 762 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))
736 { 763 {
737 XColor xc, xc_exact; 764 XColor xc;
738 765
739 if (XParseColor (screen->dpy, screen->cmap, name, &xc)) 766 if (XParseColor (screen->dpy, screen->cmap, name, &xc))
740 { 767 {
741 c.r = xc.red; 768 c.r = xc.red;
742 c.g = xc.green; 769 c.g = xc.green;
769 { 796 {
770 XColor *colors = new XColor [screen->visual->map_entries]; 797 XColor *colors = new XColor [screen->visual->map_entries];
771 798
772 for (int i = 0; i < cmap_size; i++) 799 for (int i = 0; i < cmap_size; i++)
773 colors [i].pixel = i; 800 colors [i].pixel = i;
774 801
775 // many kilobytes transfer per colour, but pseudocolor isn't worth 802 // many kilobytes transfer per colour, but pseudocolor isn't worth
776 // many extra optimisations. 803 // many extra optimisations.
777 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); 804 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
778 805
779 int diff = 0x7fffffffUL; 806 int diff = 0x7fffffffUL;
792 } 819 }
793 } 820 }
794 821
795 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", 822 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n",
796 // color.r, color.g, color.b, best->red, best->green, best->blue, diff); 823 // color.r, color.g, color.b, best->red, best->green, best->blue, diff);
797 824
798 got = alloc (screen, rgba (best->red, best->green, best->blue)); 825 got = alloc (screen, rgba (best->red, best->green, best->blue));
799 826
800 delete colors; 827 delete [] colors;
801 } 828 }
802#endif 829#endif
803 830
804 return got; 831 return got;
805} 832}
830 color.green = c.g; 857 color.green = c.g;
831 color.blue = c.b; 858 color.blue = c.b;
832 color.pixel = (Pixel)*this; 859 color.pixel = (Pixel)*this;
833} 860}
834 861
835void 862void
836rxvt_color::free (rxvt_screen *screen) 863rxvt_color::free (rxvt_screen *screen)
837{ 864{
838 if (screen->visual->c_class == TrueColor) 865 if (screen->visual->c_class == TrueColor)
839 return; // nothing to do 866 return; // nothing to do
840 867
860 lerp (c.a, to.a, percent) 887 lerp (c.a, to.a, percent)
861 ) 888 )
862 ); 889 );
863} 890}
864 891
892#if TRACE_PIXMAPS
893# undef XCreatePixmap
894# undef XFreePixmap
895Pixmap trace_XCreatePixmap (const char *file, int line, Display *dpy, Window r, unsigned int w, unsigned int h, unsigned int d)
896{
897 Pixmap res = XCreatePixmap (dpy, r, w, h, d);
898 fprintf (stderr, "%s:%d: XCreatePixmap (%p,%lX,%u,%u,%u) returned %lX\n", file, line, dpy, r, w, h, d, res);
899 return res;
900}
901
902void trace_XFreePixmap (const char *file, int line, Display *dpy, Pixmap p)
903{
904 fprintf (stderr, "%s:%d: XFreePixmap (%p,%lX)\n", file, line, dpy, p);
905 XFreePixmap (dpy,p);
906}
907#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines