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.85 by root, Sun Dec 16 19:44:48 2007 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.
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 ();
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)
656static unsigned int 664static unsigned int
657insert_component (unsigned int value, unsigned int mask, unsigned int shift) 665insert_component (unsigned int value, unsigned int mask, unsigned int shift)
658{ 666{
659 return (value * (mask + 1) >> 16) << shift; 667 return (value * (mask + 1) >> 16) << shift;
660} 668}
661 669
662bool 670bool
663rxvt_color::alloc (rxvt_screen *screen, const rgba &color) 671rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
664{ 672{
665#if XFT 673#if XFT
666 XRenderPictFormat *format; 674 XRenderPictFormat *format;
700 c.green = color.g; 708 c.green = color.g;
701 c.blue = color.b; 709 c.blue = color.b;
702 710
703 if (screen->visual->c_class == TrueColor) 711 if (screen->visual->c_class == TrueColor)
704 { 712 {
705 c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) 713 c.pixel = (color.r >> (16 - popcount (screen->visual->red_mask )) << ctz (screen->visual->red_mask ))
706 / rgba::MAX_CC) << ctz (screen->visual->red_mask ) 714 | (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 )) 715 | (color.b >> (16 - popcount (screen->visual->blue_mask )) << ctz (screen->visual->blue_mask ));
710 / rgba::MAX_CC) << ctz (screen->visual->blue_mask );
711 716
712 return true; 717 return true;
713 } 718 }
714 else if (XAllocColor (screen->dpy, screen->cmap, &c)) 719 else if (XAllocColor (screen->dpy, screen->cmap, &c))
715 return true; 720 return true;
736 name += skip; 741 name += skip;
737 } 742 }
738 else 743 else
739 c.a = rgba::MAX_CC; 744 c.a = rgba::MAX_CC;
740 745
746 // parse the non-standard "#aarrggbb" format
747 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))
748 {
749 if (c.r)
750 c.r = (c.r << 8) | 0x0ff;
751 if (c.g)
752 c.g = (c.g << 8) | 0x0ff;
753 if (c.b)
754 c.b = (c.b << 8) | 0x0ff;
755 if (c.a)
756 c.a = (c.a << 8) | 0x0ff;
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 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))
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