/************************************** * * Copyright 2008 by Sean Conner. All Rights Reserved. * * 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 the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Comments, questions and criticisms can be sent to: sean@conman.org * **************************************/ #include #include #include #include #include #include #include /******************************************************************/ extern char *optarg; extern int optind; extern int opterr; extern int optopt; Display *gdisplay; Window gwindow; int gdebug; /*****************************************************************/ static void cleanup(void) { if (gwindow) XDestroyWindow(gdisplay,gwindow); if (gdisplay) XCloseDisplay(gdisplay); } /******************************************************************/ static void *get_data( unsigned long *pitems, int *pformat, Atom selection, Atom target, Atom property ) { XEvent event; Atom type; unsigned long bytes; unsigned char *data; int rc; *pitems = 0; XConvertSelection( gdisplay, selection, target, property, gwindow, CurrentTime ); do XNextEvent(gdisplay,&event); while (event.type != SelectionNotify); if (event.xselection.property == None) return(NULL); rc = XGetWindowProperty( event.xselection.display, event.xselection.requestor, event.xselection.property, 0, LONG_MAX / 4, True, AnyPropertyType, &type, pformat, pitems, &bytes, &data ); if (rc == 1) return(NULL); if (gdebug) { char *text; text = XGetAtomName(gdisplay,type); fprintf( stderr, "Type returned: %s\n" "Format: %d\n" "Number items: %lu\n", text, *pformat, *pitems ); XFree(text); } return((void *)data); } /*******************************************************************/ static void do_selection(Atom selection,Atom target,Atom property) { unsigned long items; unsigned long i; char *data; int format; data = get_data(&items,&format,selection,target,property); if (data == NULL) return; switch(format) { case 32: items *= 4; break; case 16: items *= 2; break; case 8: break; default: assert(0); break; } fwrite(data,1,items,stdout); } /*******************************************************************/ int main(int argc,char *argv[]) { Atom selection; Atom target; Atom property; char *name; int ftarget = 0; int c; atexit(cleanup); gdisplay = XOpenDisplay(NULL); if (gdisplay == NULL) { fprintf(stderr,"failed to open display\n"); return EXIT_FAILURE; } selection = XA_PRIMARY; target = XA_STRING; property = XInternAtom(gdisplay,"XSELECT",False); while((c = getopt(argc,argv,"pswcqdh")) != -1) { switch(c) { case 'd': gdebug = 1; break; case 'p': selection = XA_PRIMARY; break; case 's': selection = XA_SECONDARY; break; case 'c': selection = XInternAtom(gdisplay,"CLIPBOARD",False); break; case 'q': target = XInternAtom(gdisplay,"TARGETS",False); ftarget = 1; break; case 'h': default: fprintf( stderr, "usage: %s [-d] [-h] [-w] [-p] [-s] [-c] [-q] [ ... ]\n" "\t-d\tdebug information\n" "\t-h\thelp\n" "\t-p\tselect primary (default)\n" "\t-s\tselect secondary\n" "\t-c\tselect clipboard\n" "\t-w\tcreate hidden window\n" "\t-q\tquery selection for targets\n" "\t\tselection type\n", argv[0] ); return EXIT_FAILURE; } } gwindow = XCreateSimpleWindow( gdisplay, RootWindow(gdisplay,DefaultScreen(gdisplay)), 0, 0, 10, 10, 1, 0, 0 ); if (ftarget) { unsigned long items; unsigned long i; Atom *list; int format; list = get_data(&items,&format,selection,target,property); if (list == NULL) { fprintf(stderr,"current selection doesn't support querying\n"); return EXIT_FAILURE; } for (i = 0 ; i < items ; i++) { name = XGetAtomName(gdisplay,list[i]); printf("%s\n",name); XFree(name); } XFree(list); return EXIT_SUCCESS; } if (optind == argc) do_selection(selection,target,property); else for ( ; optind < argc ; optind++) { target = XInternAtom(gdisplay,argv[optind],True); if (target == None) continue; do_selection(selection,target,property); } return EXIT_SUCCESS; }