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.52 by root, Sat Feb 18 15:48:23 2006 UTC vs.
Revision 1.88 by root, Wed Jan 23 09:14:58 2008 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;
345#if XLIB_ILLEGAL_ACCESS 353#if XLIB_ILLEGAL_ACCESS
346 if (dpy->xdefaults) 354 if (dpy->xdefaults)
347 XFree (dpy->xdefaults); 355 XFree (dpy->xdefaults);
348#endif 356#endif
349 357
350 if (XGetWindowProperty (dpy, root, XA_RESOURCE_MANAGER, 358 if (XGetWindowProperty (dpy, RootWindow (dpy, 0), XA_RESOURCE_MANAGER,
351 0L, 100000000L, False, 359 0L, 100000000L, False,
352 XA_STRING, &actual_type, &actual_format, 360 XA_STRING, &actual_type, &actual_format,
353 &nitems, &nremaining, 361 &nitems, &nremaining,
354 (unsigned char **)&val) == Success 362 (unsigned char **)&val) == Success
355 && actual_type == XA_STRING 363 && actual_type == XA_STRING
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 ();
498 501
499#ifdef POINTER_BLANK 502#ifdef POINTER_BLANK
500 XFreeCursor (dpy, blank_cursor); 503 XFreeCursor (dpy, blank_cursor);
501#endif 504#endif
502 x_ev.stop (); 505 x_ev.stop ();
506 flush_ev.stop ();
503#ifdef USE_XIM 507#ifdef USE_XIM
504 xims.clear (); 508 xims.clear ();
505#endif 509#endif
506 XCloseDisplay (dpy); 510 XCloseDisplay (dpy);
507} 511}
537 541
538 XFree (atoms); 542 XFree (atoms);
539} 543}
540#endif 544#endif
541 545
542void rxvt_display::x_cb (io_watcher &w, short revents) 546void rxvt_display::x_cb (ev::io &w, int revents)
543{ 547{
544 do 548 while (XEventsQueued (dpy, QueuedAfterReading))
545 { 549 {
546 XEvent xev; 550 XEvent xev;
547 XNextEvent (dpy, &xev); 551 XNextEvent (dpy, &xev);
548 552
549#ifdef USE_XIM 553#ifdef USE_XIM
552 if (xev.type == PropertyNotify 556 if (xev.type == PropertyNotify
553 && xev.xany.window == root 557 && xev.xany.window == root
554 && xev.xproperty.atom == xa[XA_XIM_SERVERS]) 558 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
555 im_change_check (); 559 im_change_check ();
556#endif 560#endif
561 if (xev.type == MappingNotify)
562 XRefreshKeyboardMapping (&xev.xmapping);
563
557 for (int i = xw.size (); i--; ) 564 for (int i = xw.size (); i--; )
558 { 565 {
559 if (!xw[i]) 566 if (!xw[i])
560 xw.erase_unordered (i); 567 xw.erase_unordered (i);
561 else if (xw[i]->window == xev.xany.window) 568 else if (xw[i]->window == xev.xany.window)
563 } 570 }
564#ifdef USE_XIM 571#ifdef USE_XIM
565 } 572 }
566#endif 573#endif
567 } 574 }
568 while (XEventsQueued (dpy, QueuedAlready)); 575}
569 576
577void rxvt_display::flush_cb (ev::prepare &w, int revents)
578{
579 w.stop ();
570 XFlush (dpy); 580 XFlush (dpy);
571} 581}
572 582
573void rxvt_display::flush ()
574{
575 if (XEventsQueued (dpy, QueuedAlready))
576 x_cb (x_ev, EVENT_READ);
577
578 XFlush (dpy);
579}
580
581void rxvt_display::reg (xevent_watcher *w) 583void rxvt_display::reg (xevent_watcher *w)
582{ 584{
585 if (!w->active)
586 {
583 xw.push_back (w); 587 xw.push_back (w);
584 w->active = xw.size (); 588 w->active = xw.size ();
589 }
585} 590}
586 591
587void rxvt_display::unreg (xevent_watcher *w) 592void rxvt_display::unreg (xevent_watcher *w)
588{ 593{
589 if (w->active) 594 if (w->active)
595 {
590 xw[w->active - 1] = 0; 596 xw[w->active - 1] = 0;
597 w->active = 0;
598 }
591} 599}
592 600
593void rxvt_display::set_selection_owner (rxvt_term *owner) 601void rxvt_display::set_selection_owner (rxvt_term *owner)
594{ 602{
595 if (selection_owner && selection_owner != owner) 603 if (selection_owner && selection_owner != owner)
604 {
605 rxvt_term *owner = selection_owner;
606
596 selection_owner->selection_clear (); 607 owner->selection_clear ();
608 owner->flush ();
609 }
597 610
598 selection_owner = owner; 611 selection_owner = owner;
599} 612}
600 613
601#ifdef USE_XIM 614#ifdef USE_XIM
649 662
650template class refcache<rxvt_display>; 663template class refcache<rxvt_display>;
651refcache<rxvt_display> displays; 664refcache<rxvt_display> displays;
652 665
653///////////////////////////////////////////////////////////////////////////// 666/////////////////////////////////////////////////////////////////////////////
654 667//
668
669static unsigned int
670insert_component (unsigned int value, unsigned int mask, unsigned int shift)
671{
672 return (value * (mask + 1) >> 16) << shift;
673}
674
655bool 675bool
656rxvt_color::alloc (rxvt_screen *screen, const rgba &color) 676rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
657{ 677{
678 //TODO: only supports 24 bit truecolour
679 int alpha = color.a >= 0xff00 ? 0xffff : color.a;
680
658#if XFT 681#if XFT
659 XRenderPictFormat *format; 682 XRenderPictFormat *format;
660 683
661 // FUCKING Xft gets it wrong, of course, so work around it. 684 // FUCKING Xft gets it wrong, of course, so work around it.
662 // Transparency users should eat shit and die, and then 685 // Transparency users should eat shit and die, and then
666 { 689 {
667 // the fun lies in doing everything manually... 690 // the fun lies in doing everything manually...
668 c.color.red = color.r; 691 c.color.red = color.r;
669 c.color.green = color.g; 692 c.color.green = color.g;
670 c.color.blue = color.b; 693 c.color.blue = color.b;
671 c.color.alpha = color.a; 694 c.color.alpha = alpha;
672 695
673 c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red ) 696 c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red )
674 | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green) 697 | insert_component (color.g, format->direct.greenMask, format->direct.green)
675 | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue ) 698 | insert_component (color.b, format->direct.blueMask , format->direct.blue )
676 | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha); 699 | insert_component (alpha , format->direct.alphaMask, format->direct.alpha);
677 700
678 return true; 701 return true;
679 } 702 }
680 else 703 else
681 { 704 {
682 XRenderColor d; 705 XRenderColor d;
683 706
684 d.red = color.r; 707 d.red = color.r;
685 d.green = color.g; 708 d.green = color.g;
686 d.blue = color.b; 709 d.blue = color.b;
687 d.alpha = color.a; 710 d.alpha = alpha;
688 711
689 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); 712 return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c);
690 } 713 }
691#else 714#else
692 c.red = color.r; 715 c.red = color.r;
693 c.green = color.g; 716 c.green = color.g;
694 c.blue = color.b; 717 c.blue = color.b;
695 718
696 if (screen->visual->c_class == TrueColor) 719 if (screen->visual->c_class == TrueColor)
697 { 720 {
698 c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) 721 c.pixel = (color.r >> (16 - popcount (screen->visual->red_mask )) << ctz (screen->visual->red_mask ))
699 / rgba::MAX_CC) << ctz (screen->visual->red_mask ) 722 | (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 )) 723 | (color.b >> (16 - popcount (screen->visual->blue_mask )) << ctz (screen->visual->blue_mask ));
703 / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
704 724
705 return true; 725 return true;
706 } 726 }
707 else if (XAllocColor (screen->dpy, screen->cmap, &c)) 727 else if (XAllocColor (screen->dpy, screen->cmap, &c))
708 return true; 728 return true;
729 name += skip; 749 name += skip;
730 } 750 }
731 else 751 else
732 c.a = rgba::MAX_CC; 752 c.a = rgba::MAX_CC;
733 753
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 }
734 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format 762 // 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)) 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))
736 { 764 {
737 XColor xc, xc_exact; 765 XColor xc;
738 766
739 if (XParseColor (screen->dpy, screen->cmap, name, &xc)) 767 if (XParseColor (screen->dpy, screen->cmap, name, &xc))
740 { 768 {
741 c.r = xc.red; 769 c.r = xc.red;
742 c.g = xc.green; 770 c.g = xc.green;
769 { 797 {
770 XColor *colors = new XColor [screen->visual->map_entries]; 798 XColor *colors = new XColor [screen->visual->map_entries];
771 799
772 for (int i = 0; i < cmap_size; i++) 800 for (int i = 0; i < cmap_size; i++)
773 colors [i].pixel = i; 801 colors [i].pixel = i;
774 802
775 // many kilobytes transfer per colour, but pseudocolor isn't worth 803 // many kilobytes transfer per colour, but pseudocolor isn't worth
776 // many extra optimisations. 804 // many extra optimisations.
777 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); 805 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
778 806
779 int diff = 0x7fffffffUL; 807 int diff = 0x7fffffffUL;
792 } 820 }
793 } 821 }
794 822
795 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", 823 //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); 824 // color.r, color.g, color.b, best->red, best->green, best->blue, diff);
797 825
798 got = alloc (screen, rgba (best->red, best->green, best->blue)); 826 got = alloc (screen, rgba (best->red, best->green, best->blue));
799 827
800 delete colors; 828 delete [] colors;
801 } 829 }
802#endif 830#endif
803 831
804 return got; 832 return got;
805} 833}
830 color.green = c.g; 858 color.green = c.g;
831 color.blue = c.b; 859 color.blue = c.b;
832 color.pixel = (Pixel)*this; 860 color.pixel = (Pixel)*this;
833} 861}
834 862
835void 863void
836rxvt_color::free (rxvt_screen *screen) 864rxvt_color::free (rxvt_screen *screen)
837{ 865{
866 if (screen->visual->c_class == TrueColor)
867 return; // nothing to do
868
838#if XFT 869#if XFT
839 XftColorFree (screen->dpy, screen->visual, screen->cmap, &c); 870 XftColorFree (screen->dpy, screen->visual, screen->cmap, &c);
840#else 871#else
841 XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes); 872 XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes);
842#endif 873#endif
857 lerp (c.a, to.a, percent) 888 lerp (c.a, to.a, percent)
858 ) 889 )
859 ); 890 );
860} 891}
861 892
893#if TRACE_PIXMAPS
894# undef XCreatePixmap
895# undef XFreePixmap
896Pixmap trace_XCreatePixmap (const char *file, int line, Display *dpy, Window r, unsigned int w, unsigned int h, unsigned int d)
897{
898 Pixmap res = XCreatePixmap (dpy, r, w, h, d);
899 fprintf (stderr, "%s:%d: XCreatePixmap (%p,%lX,%u,%u,%u) returned %lX\n", file, line, dpy, r, w, h, d, res);
900 return res;
901}
902
903void trace_XFreePixmap (const char *file, int line, Display *dpy, Pixmap p)
904{
905 fprintf (stderr, "%s:%d: XFreePixmap (%p,%lX)\n", file, line, dpy, p);
906 XFreePixmap (dpy,p);
907}
908#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines