--- rxvt-unicode/src/rxvttoolkit.C 2007/08/27 18:31:00 1.72 +++ rxvt-unicode/src/rxvttoolkit.C 2011/01/27 02:03:30 1.114 @@ -3,7 +3,7 @@ *----------------------------------------------------------------------* * * All portions of code are copyright by their respective author/s. - * Copyright (c) 2003-2006 Marc Lehmann + * Copyright (c) 2003-2007 Marc Lehmann * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ # include #endif -const char *const xa_names[] = +static const char *const xa_names[] = { "TEXT", "COMPOUND_TEXT", @@ -61,6 +61,7 @@ "_NET_WM_NAME", "_NET_WM_ICON_NAME", "_NET_WM_PING", + "_NET_WM_ICON", #endif #if USE_XIM "WM_LOCALE_NAME", @@ -185,7 +186,7 @@ ximcallback.client_data = (XPointer)this; ximcallback.callback = im_destroy_cb; - XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0); + XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0); return true; } @@ -228,10 +229,7 @@ rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h) { - // it's actually faster to re-allocate every time. don't ask me - // why, but its likely no big deal there are no roundtrips - // (I think/hope). - if (!scratch_area || w > scratch_w || h > scratch_h || 1/*D*/) + if (!scratch_area || w > scratch_w || h > scratch_h) { if (scratch_area) { @@ -297,15 +295,17 @@ rxvt_display::rxvt_display (const char *id) : refcounted (id) -, x_ev (this, &rxvt_display::x_cb) , selection_owner (0) +, clipboard_owner (0) { + x_ev .set (this); + flush_ev.set (this); } XrmDatabase rxvt_display::get_resources (bool refresh) { - char *homedir = (char *)getenv ("HOME"); + char *homedir = getenv ("HOME"); char fname[1024]; /* @@ -314,13 +314,18 @@ char *displayResource, *xe; XrmDatabase rdb1, database = 0; +#if !XLIB_ILLEGAL_ACCESS + /* work around a bug in XrmSetDatabase where it frees the db, see ref_next */ + database = XrmGetStringDatabase (""); +#endif + // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 // as opposed to "standard practise", we always read in ~/.Xdefaults // 6. System wide per application default file. /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ - if ((xe = (char *)getenv ("XAPPLRESDIR"))) + if ((xe = getenv ("XAPPLRESDIR"))) { snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); @@ -361,14 +366,14 @@ (unsigned char **)&val) == Success && actual_type == XA_STRING && actual_format == 8) - displayResource = val; - else - { - displayResource = 0; - - if (val) - XFree(val); - } + displayResource = val; + else + { + displayResource = 0; + + if (val) + XFree (val); + } #if XLIB_ILLEGAL_ACCESS dpy->xdefaults = displayResource; @@ -402,7 +407,7 @@ // 3. User's per host defaults file /* Add in XENVIRONMENT file */ - if ((xe = (char *)getenv ("XENVIRONMENT")) + if ((xe = getenv ("XENVIRONMENT")) && (rdb1 = XrmGetFileDatabase (xe))) XrmMergeDatabases (rdb1, &database); else if (homedir) @@ -426,11 +431,11 @@ #ifdef LOCAL_X_IS_UNIX if (id[0] == ':') { - val = rxvt_malloc (5 + strlen (id) + 1); + if (!(val = rxvt_temp_buf (5 + strlen (id) + 1))) + return false; strcpy (val, "unix/"); strcat (val, id); dpy = XOpenDisplay (val); - free (val); } else #endif @@ -445,7 +450,7 @@ screen = DefaultScreen (dpy); root = DefaultRootWindow (dpy); - assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); + assert (ARRAY_LENGTH(xa_names) == NUM_XA); XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); XrmSetDatabase (dpy, get_resources (false)); @@ -463,8 +468,8 @@ int fd = XConnectionNumber (dpy); - // try to detect wether we have a local connection. - // assume unix domains socket == local, everything else not + // try to detect whether we have a local connection. + // assume unix domain socket == local, everything else not // TODO: might want to check for inet/127.0.0.1 is_local = 0; sockaddr_un sa; @@ -473,7 +478,8 @@ if (!getsockname (fd, (sockaddr *)&sa, &sl)) is_local = sa.sun_family == AF_UNIX; - x_ev.start (fd, EVENT_READ); + flush_ev.start (); + x_ev.start (fd, ev::READ); fcntl (fd, F_SETFD, FD_CLOEXEC); XSelectInput (dpy, root, PropertyChangeMask); @@ -486,9 +492,13 @@ void rxvt_display::ref_next () { - // TODO: somehow check wether the database files/resources changed + // TODO: somehow check whether the database files/resources changed // before affording re-loading/parsing XrmDestroyDatabase (XrmGetDatabase (dpy)); +#if XLIB_ILLEGAL_ACCESS + /* work around a bug in XrmSetDatabase where it frees the db */ + dpy->db = 0; +#endif XrmSetDatabase (dpy, get_resources (true)); } @@ -501,9 +511,11 @@ XFreeCursor (dpy, blank_cursor); #endif x_ev.stop (); + flush_ev.stop (); #ifdef USE_XIM xims.clear (); #endif + XrmDestroyDatabase (XrmGetDatabase (dpy)); XCloseDisplay (dpy); } @@ -525,10 +537,10 @@ if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, False, XA_ATOM, &actual_type, &actual_format, &nitems, &bytes_after, (unsigned char **)&atoms) - != Success ) + != Success) return; - if (actual_type == XA_ATOM && actual_format == 32) + if (actual_type == XA_ATOM && actual_format == 32) for (int i = 0; i < nitems; i++) if (XGetSelectionOwner (dpy, atoms[i])) { @@ -540,46 +552,44 @@ } #endif -void rxvt_display::x_cb (io_watcher &w, short revents) +void rxvt_display::x_cb (ev::io &w, int revents) { - do - { - XEvent xev; - XNextEvent (dpy, &xev); + flush_ev.start (); +} -#ifdef USE_XIM - if (!XFilterEvent (&xev, None)) - { - if (xev.type == PropertyNotify - && xev.xany.window == root - && xev.xproperty.atom == xa[XA_XIM_SERVERS]) - im_change_check (); -#endif - if (xev.type == MappingNotify) - XRefreshKeyboardMapping (&xev.xmapping); +void rxvt_display::flush_cb (ev::prepare &w, int revents) +{ + while (XEventsQueued (dpy, QueuedAfterFlush)) + do + { + XEvent xev; + XNextEvent (dpy, &xev); - for (int i = xw.size (); i--; ) - { - if (!xw[i]) - xw.erase_unordered (i); - else if (xw[i]->window == xev.xany.window) - xw[i]->call (xev); - } #ifdef USE_XIM - } + if (!XFilterEvent (&xev, None)) + { + if (xev.type == PropertyNotify + && xev.xany.window == root + && xev.xproperty.atom == xa[XA_XIM_SERVERS]) + im_change_check (); +#endif + if (xev.type == MappingNotify) + XRefreshKeyboardMapping (&xev.xmapping); + + for (int i = xw.size (); i--; ) + { + if (!xw[i]) + xw.erase_unordered (i); + else if (xw[i]->window == xev.xany.window) + xw[i]->call (xev); + } +#ifdef USE_XIM + } #endif - } - while (XEventsQueued (dpy, QueuedAlready)); - - XFlush (dpy); -} - -void rxvt_display::flush () -{ - if (XEventsQueued (dpy, QueuedAlready)) - x_cb (x_ev, EVENT_READ); + } + while (XEventsQueued (dpy, QueuedAlready)); - XFlush (dpy); + w.stop (); } void rxvt_display::reg (xevent_watcher *w) @@ -600,12 +610,18 @@ } } -void rxvt_display::set_selection_owner (rxvt_term *owner) +void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard) { - if (selection_owner && selection_owner != owner) - selection_owner->selection_clear (); + rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner; - selection_owner = owner; + if (cur_owner && cur_owner != owner) + { + rxvt_term *term = cur_owner; + term->selection_clear (clipboard); + term->flush (); + } + + cur_owner = owner; } #ifdef USE_XIM @@ -628,7 +644,7 @@ l = strlen (locale); m = strlen (modifiers); - if (!(id = (char *)malloc (l + m + 2))) + if (!(id = rxvt_temp_buf (l + m + 2))) return 0; memcpy (id, locale, l); id[l] = '\n'; @@ -636,8 +652,6 @@ rxvt_xim *xim = xims.get (id); - free (id); - return xim; } @@ -668,10 +682,13 @@ { return (value * (mask + 1) >> 16) << shift; } - + bool rxvt_color::alloc (rxvt_screen *screen, const rgba &color) { + //TODO: only supports 24 bit + int alpha = color.a >= 0xff00 ? 0xffff : color.a; + #if XFT XRenderPictFormat *format; @@ -685,12 +702,12 @@ c.color.red = color.r; c.color.green = color.g; c.color.blue = color.b; - c.color.alpha = color.a; + c.color.alpha = alpha; c.pixel = insert_component (color.r, format->direct.redMask , format->direct.red ) | insert_component (color.g, format->direct.greenMask, format->direct.green) | insert_component (color.b, format->direct.blueMask , format->direct.blue ) - | insert_component (color.a, format->direct.alphaMask, format->direct.alpha); + | insert_component (alpha , format->direct.alphaMask, format->direct.alpha); return true; } @@ -701,7 +718,7 @@ d.red = color.r; d.green = color.g; d.blue = color.b; - d.alpha = color.a; + d.alpha = alpha; return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); } @@ -712,9 +729,9 @@ if (screen->visual->c_class == TrueColor) { - c.pixel = (color.r >> (16 - popcount (screen->visual->red_mask )) << ctz (screen->visual->red_mask )) - | (color.g >> (16 - popcount (screen->visual->green_mask)) << ctz (screen->visual->green_mask)) - | (color.b >> (16 - popcount (screen->visual->blue_mask )) << ctz (screen->visual->blue_mask )); + c.pixel = (color.r >> (16 - rxvt_popcount (screen->visual->red_mask )) << rxvt_ctz (screen->visual->red_mask )) + | (color.g >> (16 - rxvt_popcount (screen->visual->green_mask)) << rxvt_ctz (screen->visual->green_mask)) + | (color.b >> (16 - rxvt_popcount (screen->visual->blue_mask )) << rxvt_ctz (screen->visual->blue_mask )); return true; } @@ -736,29 +753,17 @@ char eos; int skip; + c.a = rgba::MAX_CC; + // parse the nonstandard "[alphapercent]" prefix if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) { c.a = lerp (0, rgba::MAX_CC, c.a); name += skip; } - else - c.a = rgba::MAX_CC; - // parse the non-standard "#aarrggbb" format - 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)) - { - if (c.r) - c.r = (c.r << 8) | 0x0ff; - if (c.g) - c.g = (c.g << 8) | 0x0ff; - if (c.b) - c.b = (c.b << 8) | 0x0ff; - if (c.a) - c.a = (c.a << 8) | 0x0ff; - } // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format - 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)) + if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) { XColor xc; @@ -797,7 +802,7 @@ for (int i = 0; i < cmap_size; i++) colors [i].pixel = i; - + // many kilobytes transfer per colour, but pseudocolor isn't worth // many extra optimisations. XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); @@ -820,7 +825,7 @@ //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", // color.r, color.g, color.b, best->red, best->green, best->blue, diff); - + got = alloc (screen, rgba (best->red, best->green, best->blue)); delete [] colors; @@ -858,7 +863,7 @@ color.pixel = (Pixel)*this; } -void +void rxvt_color::free (rxvt_screen *screen) { if (screen->visual->c_class == TrueColor)