--- rxvt-unicode/src/rxvttoolkit.C 2007/12/14 06:17:27 1.84 +++ rxvt-unicode/src/rxvttoolkit.C 2011/02/21 07:41:03 1.121 @@ -3,7 +3,8 @@ *----------------------------------------------------------------------* * * All portions of code are copyright by their respective author/s. - * Copyright (c) 2003-2007 Marc Lehmann + * Copyright (c) 2003-2011 Marc Lehmann + * Copyright (c) 2011 Emanuele Giaquinta * * 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 +36,7 @@ # include #endif -const char *const xa_names[] = +static const char *const xa_names[] = { "TEXT", "COMPOUND_TEXT", @@ -61,6 +62,7 @@ "_NET_WM_NAME", "_NET_WM_ICON_NAME", "_NET_WM_PING", + "_NET_WM_ICON", #endif #if USE_XIM "WM_LOCALE_NAME", @@ -228,10 +230,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) { @@ -298,6 +297,7 @@ rxvt_display::rxvt_display (const char *id) : refcounted (id) , selection_owner (0) +, clipboard_owner (0) { x_ev .set (this); flush_ev.set (this); @@ -315,6 +315,11 @@ 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 @@ -362,14 +367,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; @@ -427,11 +432,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 @@ -446,7 +451,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)); @@ -464,7 +469,7 @@ int fd = XConnectionNumber (dpy); - // try to detect wether we have a local connection. + // 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; @@ -488,9 +493,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)); } @@ -503,9 +512,11 @@ XFreeCursor (dpy, blank_cursor); #endif x_ev.stop (); + flush_ev.stop (); #ifdef USE_XIM xims.clear (); #endif + XrmDestroyDatabase (XrmGetDatabase (dpy)); XCloseDisplay (dpy); } @@ -527,10 +538,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])) { @@ -544,39 +555,42 @@ void rxvt_display::x_cb (ev::io &w, int revents) { - while (XEventsQueued (dpy, QueuedAfterReading)) - { - 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)); -void rxvt_display::flush_cb (ev::prepare &w, int revents) -{ w.stop (); - XFlush (dpy); } void rxvt_display::reg (xevent_watcher *w) @@ -597,12 +611,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; + + if (cur_owner && cur_owner != owner) + { + rxvt_term *term = cur_owner; + term->selection_clear (clipboard); + term->flush (); + } - selection_owner = owner; + cur_owner = owner; } #ifdef USE_XIM @@ -625,7 +645,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'; @@ -633,8 +653,6 @@ rxvt_xim *xim = xims.get (id); - free (id); - return xim; } @@ -669,6 +687,9 @@ 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; @@ -682,12 +703,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; } @@ -698,7 +719,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); } @@ -709,9 +730,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; } @@ -733,29 +754,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; @@ -885,19 +894,280 @@ ); } -#if TRACE_PIXMAPS -# undef XCreatePixmap -# undef XFreePixmap -Pixmap trace_XCreatePixmap (const char *file, int line, Display *dpy, Window r, unsigned int w, unsigned int h, unsigned int d) +rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term, void *cb_sv) +: display (disp), request_time (tm), request_win (win), request_prop (prop), term (term), cb_sv (cb_sv) { - Pixmap res = XCreatePixmap (dpy, r, w, h, d); - fprintf (stderr, "%s:%d: XCreatePixmap (%p,%lX,%u,%u,%u) returned %lX\n", file, line, dpy, r, w, h, d, res); - return res; + assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard); + + timer_ev.set (this); + timer_ev.repeat = 10.; + x_ev.set (this); + + incr_buf = 0; + incr_buf_size = incr_buf_fill = 0; + selection_wait = Sel_normal; + selection_type = selnum; } -void trace_XFreePixmap (const char *file, int line, Display *dpy, Pixmap p) +void +rxvt_selection::stop () { - fprintf (stderr, "%s:%d: XFreePixmap (%p,%lX)\n", file, line, dpy, p); - XFreePixmap (dpy,p); + free (incr_buf); + incr_buf = 0; + timer_ev.stop (); + x_ev.stop (display); } + +rxvt_selection::~rxvt_selection () +{ + stop (); +} + +void +rxvt_selection::run () +{ + int selnum = selection_type; + +#if X_HAVE_UTF8_STRING + selection_type = Sel_UTF8String; + if (request (display->xa[XA_UTF8_STRING], selnum)) + return; +#else + selection_type = Sel_CompoundText; + if (request (display->xa[XA_COMPOUND_TEXT], selnum)) + return; #endif + + // fallback to CUT_BUFFER0 if the requested property has no owner + handle_selection (display->root, XA_CUT_BUFFER0, false); +} + +void +rxvt_selection::finish (char *data, unsigned int len) +{ + if (term) + { + if (data) + term->paste (data, len); + + term->selection_req = 0; + delete this; + } +#if ENABLE_PERL + else + { + stop (); + abort (); //TODO + } +#endif +} + +bool +rxvt_selection::request (Atom target, int selnum) +{ + Atom sel; + + selection_type |= selnum; + + if (selnum == Sel_Primary) + sel = XA_PRIMARY; + else if (selnum == Sel_Secondary) + sel = XA_SECONDARY; + else + sel = display->xa[XA_CLIPBOARD]; + + if (XGetSelectionOwner (display->dpy, sel) != None) + { + XConvertSelection (display->dpy, sel, target, request_prop, + request_win, request_time); + x_ev.start (display, request_win); + timer_ev.again (); + return true; + } + + return false; +} + +void +rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop) +{ + Display *dpy = display->dpy; + char *data = 0; + unsigned int data_len = 0; + unsigned long bytes_after; + XTextProperty ct; + + // check for failed XConvertSelection + if (prop == None) + { + bool error = true; + int selnum = selection_type & Sel_whereMask; + + if (selection_type & Sel_CompoundText) + { + selection_type = 0; + error = !request (XA_STRING, selnum); + } + + if (selection_type & Sel_UTF8String) + { + selection_type = Sel_CompoundText; + error = !request (display->xa[XA_COMPOUND_TEXT], selnum); + } + + if (error) + { + ct.value = 0; + goto bailout; + } + + return; + } + + // length == (2^31 - 1) / 4, as gdk + if (XGetWindowProperty (dpy, win, prop, + 0, 0x1fffffff, + delete_prop, AnyPropertyType, + &ct.encoding, &ct.format, + &ct.nitems, &bytes_after, + &ct.value) != Success) + { + ct.value = 0; + goto bailout; + } + + if (ct.encoding == None) + goto bailout; + + if (ct.value == 0) + goto bailout; + + if (ct.encoding == display->xa[XA_INCR]) + { + // INCR selection, start handshake + if (!delete_prop) + XDeleteProperty (dpy, win, prop); + + selection_wait = Sel_incr; + timer_ev.again (); + + goto bailout; + } + + if (ct.nitems == 0) + { + if (selection_wait == Sel_incr) + { + XFree (ct.value); + + // finally complete, now paste the whole thing + selection_wait = Sel_normal; + ct.value = (unsigned char *)incr_buf; + ct.nitems = incr_buf_fill; + incr_buf = 0; + timer_ev.stop (); + } + else + { + // avoid recursion + if (win != display->root || prop != XA_CUT_BUFFER0) + { + XFree (ct.value); + + // fallback to CUT_BUFFER0 if the requested property + // has an owner but is empty + handle_selection (display->root, XA_CUT_BUFFER0, False); + return; + } + + goto bailout; + } + } + else if (selection_wait == Sel_incr) + { + timer_ev.again (); + + while (incr_buf_fill + ct.nitems > incr_buf_size) + { + incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024; + incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size); + } + + memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems); + incr_buf_fill += ct.nitems; + + goto bailout; + } + + char **cl; + int cr; + + // we honour the first item only + +#if !ENABLE_MINIMAL + // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it + // so recode it manually + if (ct.encoding == display->xa[XA_UTF8_STRING]) + { + wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems); + data = rxvt_wcstombs (w); + free (w); + } + else +#endif + if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0 + && cl) + { + data = strdup (cl[0]); + XFreeStringList (cl); + } + else + { + // paste raw + data = strdup ((const char *)ct.value); + } + + data_len = strlen (data); + +bailout: + XFree (ct.value); + + if (selection_wait == Sel_normal) + { + finish (data, data_len); + free (data); + } +} + +void +rxvt_selection::timer_cb (ev::timer &w, int revents) +{ + if (selection_wait == Sel_incr) + rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n"); + + finish (); +} + +void +rxvt_selection::x_cb (XEvent &xev) +{ + switch (xev.type) + { + case PropertyNotify: + if (selection_wait == Sel_incr + && xev.xproperty.atom == request_prop + && xev.xproperty.state == PropertyNewValue) + handle_selection (xev.xproperty.window, xev.xproperty.atom, true); + break; + + case SelectionNotify: + if (selection_wait == Sel_normal + && xev.xselection.time == request_time + && xev.xselection.property == request_prop) + { + timer_ev.stop (); + handle_selection (xev.xselection.requestor, xev.xselection.property, true); + } + break; + } +}