diff -r 90f0e34e7f11 main.c --- a/main.c Thu Feb 08 14:10:17 2007 +0100 +++ b/main.c Sun Jun 17 14:10:16 2007 +0200 @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include @@ -43,6 +45,52 @@ static Item *curr = NULL; static Item *curr = NULL; static Window root; static Window win; + +static void * +emallocz(unsigned int size) { + void *res = calloc(1, size); + + if(!res) { + fprintf(stderr, "fatal: could not malloc() %u bytes\n", size); + exit(EXIT_FAILURE); + } + return res; +} + +static unsigned char * +getselection(unsigned long offset, unsigned long *len, unsigned long *remain) { + Display *dpy; + Atom xa_clip_string; + Window w; + XEvent ev; + Atom typeret; + int format; + unsigned char *data; + unsigned char *result = NULL; + + dpy = XOpenDisplay(NULL); + if(!dpy) + return NULL; + xa_clip_string = XInternAtom(dpy, "BLITZ_SEL_STRING", False); + w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200, + 1, CopyFromParent, CopyFromParent); + XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string, + w, CurrentTime); + XFlush(dpy); + XNextEvent(dpy, &ev); + if(ev.type == SelectionNotify && ev.xselection.property != None) { + XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False, + AnyPropertyType, &typeret, &format, len, remain, &data); + if(*len) { + result = emallocz(sizeof(unsigned char) * *len); + memcpy(result, data, *len); + } + XDeleteProperty(dpy, w, ev.xselection.property); + } + XDestroyWindow(dpy, w); + XCloseDisplay(dpy); + return result; +} static void calcoffsets(void) { @@ -162,6 +210,25 @@ kpress(XKeyEvent * e) { || IsMiscFunctionKey(ksym) || IsPFKey(ksym) || IsPrivateKeypadKey(ksym)) return; + + /* handle clipboard */ + if ((e->state & ControlMask) && (ksym == XK_i)) { + int len, offset, remain; + char *data; + len = offset = remain = 0; + do { + data = getselection(offset, &len, &remain); + for(i = 0; i < len; i++) + text[i] = data[i]; + offset += len; + free(data); + } + while(remain); + text[len]='\0'; + match(text); + drawmenu(); + return; + } /* first check if a control mask is omitted */ if(e->state & ControlMask) { switch (ksym) { @@ -489,3 +556,4 @@ UninitializedEnd: XCloseDisplay(dpy); return ret; } +