--- xclip-0.12+svn84.orig/xcprint.c +++ xclip-0.12+svn84/xcprint.c @@ -41,6 +41,7 @@ prhelp(char *name) " piping to a file or program)\n" " -l, -loops number of selection requests to " "wait for before exiting\n" + " -w -wait number of msec to wait after first past event\n" " -d, -display X display to connect to (eg " "localhost:0\")\n" " -h, -help usage information\n" --- xclip-0.12+svn84.orig/xclip.c +++ xclip-0.12+svn84/xclip.c @@ -35,13 +35,16 @@ #include "xclib.h" /* command line option table for XrmParseCommand() */ -XrmOptionDescRec opt_tab[14]; +XrmOptionDescRec opt_tab[15]; /* Options that get set on the command line */ int sloop = 0; /* number of loops */ char *sdisp = NULL; /* X display to connect to */ Atom sseln = XA_PRIMARY; /* X selection to work with */ Atom target = XA_STRING; +int wait = 0; /* wait: stop xclip after wait msec + after last 'paste event', start counting + after first 'paste event' */ /* Flags for command line options */ static int fverb = OSILENT; /* output level */ @@ -125,6 +128,13 @@ doOptMain(int argc, char *argv[]) fprintf(stderr, "Loops: %i\n", sloop); } + if (XrmGetResource(opt_db, "xclip.wait", "Xclip.Wait", &rec_typ, &rec_val) + ) { + wait = atoi(rec_val.addr); + if (fverb == OVERBOSE) /* print in verbose mode only */ + fprintf(stderr, "Timeout: %i msec\n", sloop); + } + /* Read remaining options (filenames) */ while ((fil_number + 1) < argc) { if (fil_number > 0) { @@ -209,6 +219,12 @@ doIn(Window win, const char *progname) unsigned long sel_all = 0; /* allocated size of sel_buf */ XEvent evt; /* X Event Structures */ int dloop = 0; /* done loops counter */ + int x11_fd; /* fd on which XEvents appear */ + fd_set in_fds; + struct timeval tv; + + /* ConnectionNumber is a macro, it can't fail */ + x11_fd = ConnectionNumber(dpy); /* in mode */ sel_all = 16; /* Reasonable ballpark figure */ @@ -301,10 +317,13 @@ doIn(Window win, const char *progname) /* Avoid making the current directory in use, in case it will need to be umounted */ chdir("/"); + goto start; + /* loop and wait for the expected number of * SelectionRequest events */ while (dloop < sloop || sloop < 1) { + /* print messages about what we're waiting for * if not in silent mode */ @@ -328,6 +347,20 @@ doIn(Window win, const char *progname) static Atom pty; int finished; + if (!XPending(dpy) && wait > 0) { + tv.tv_sec = wait/1000; + tv.tv_usec = (wait%1000)*1000; + + /* build fd_set */ + FD_ZERO(&in_fds); + FD_SET(x11_fd, &in_fds); + if (!select(x11_fd + 1, &in_fds, 0, 0, &tv)) { + return EXIT_SUCCESS; + } + } + +start: + XNextEvent(dpy, &evt); finished = xcin(dpy, &cwin, evt, &pty, target, sel_buf, sel_len, &sel_pos, &context); @@ -569,6 +602,12 @@ main(int argc, char *argv[]) opt_tab[12].argKind = XrmoptionSepArg; opt_tab[12].value = (XPointer) NULL; + /* wait option entry */ + opt_tab[14].option = xcstrdup("-wait"); + opt_tab[14].specifier = xcstrdup(".wait"); + opt_tab[14].argKind = XrmoptionSepArg; + opt_tab[14].value = (XPointer) NULL; + /* parse command line options */ doOptMain(argc, argv); --- xclip-0.12+svn84.orig/xclip.1 +++ xclip-0.12+svn84/xclip.1 @@ -38,6 +38,9 @@ when xclip is invoked in the in mode wit \fB\-l\fR, \fB\-loops\fR number of X selection requests (pastes into X applications) to wait for before exiting, with a value of 0 (default) causing xclip to wait for an unlimited number of requests until another application (possibly another invocation of xclip) takes ownership of the selection .TP +\fB\-w\fR, \fB\-wait\fR +number of milliseconds to wait after the first paste event before exiting +.TP \fB\-t\fR, \fB\-target\fR specify a particular data format using the given target atom. With \fB\-o\fR the special target atom name "TARGETS" can be used to get a list of valid target atoms for this selection. For more information about target atoms refer to ICCCM section 2.6.2 .TP