1 /* wxpaste.c- paste contents of cutbuffer to stdout
3 * Copyright (c) 1997 Alfredo K. Kojima
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #define PROG_VERSION "wxpaste (Window Maker) 0.3"
22 #include "../src/config.h"
30 #include <X11/Xatom.h>
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_SELECT_H
34 # include <sys/select.h>
38 #define MAXDATA (4*1024*1024)
44 printf("Usage: %s [OPTIONS] [FILE]\n", progn
);
45 puts("Copies data from X selection or cutbuffer to FILE or stdout.");
47 puts(" -display display display to use");
48 puts(" --cutbuffer number cutbuffer number to get data from");
49 puts(" --selection [selection] reads data from named selection instead of\n"
51 puts(" --help display this help and exit");
52 puts(" --version output version information and exit");
57 getTimestamp(Display
*dpy
, Window win
)
61 /* So we do this trickery to get a time stamp:
63 * 1. Grab the server because we are paranoid and don't want to
64 * get in a race with another instance of wxpaste being ran at the
67 * 2. Set a dummy property in our window.
69 * 3. Get the PropertyNotify event and get it's timestamp.
71 * 4. Ungrab the server.
74 XSelectInput(dpy
, win
, PropertyChangeMask
);
76 /* Generate a PropertyNotify event */
77 XStoreName(dpy
, win
, "shit");
79 /* wait for the event */
82 if (ev
.type
== PropertyNotify
)
86 return ev
.xproperty
.time
;
91 fetchSelection(Display
*dpy
, char *selection
, char *progName
)
93 Atom selatom
= XInternAtom(dpy
, selection
, False
);
94 Atom clipatom
= XInternAtom(dpy
, "CLIPBOARD", False
);
99 struct timeval timeout
;
102 win
= XCreateSimpleWindow(dpy
, DefaultRootWindow(dpy
), 0, 0, 1, 1,
105 * The ICCCM says that we can't pass CurrentTime as the timestamp
106 * for XConvertSelection(), but we don't have anything to use as
109 now
= getTimestamp(dpy
, win
);
111 XConvertSelection(dpy
, selatom
, XA_STRING
, clipatom
, win
, now
);
116 /* wait for the convertion */
120 if (XPending(dpy
)==0) {
122 FD_SET(ConnectionNumber(dpy
), &fdset
);
123 res
= select(ConnectionNumber(dpy
)+1, &fdset
, NULL
, NULL
,
130 if (res
> 0 || XPending(dpy
) > 0) {
131 XNextEvent(dpy
, &ev
);
132 if (ev
.type
== SelectionNotify
&& ev
.xany
.window
== win
) {
139 /* if success, return the data */
143 unsigned long len
, bytes
;
146 if (XGetWindowProperty(dpy
, win
, clipatom
, 0, MAXDATA
/4, False
,
147 XA_STRING
, &rtype
, &bits
, &len
, &bytes
, &data
)!=0)
150 if ((rtype
!=XA_STRING
) || (bits
!=8)) {
161 main(int argc
, char **argv
)
168 char *display_name
="";
169 char *selection_name
=NULL
;
171 for (i
=1; i
<argc
; i
++) {
172 if (argv
[i
][0]=='-') {
173 if (argv
[i
][1]=='h' || strcmp(argv
[i
], "--help")==0) {
176 } else if (strcmp(argv
[i
], "--version")==0) {
179 } else if (strcmp(argv
[i
],"-selection")==0
180 || strcmp(argv
[i
],"--selection")==0) {
182 selection_name
= argv
[++i
];
184 selection_name
= "PRIMARY";
186 } else if (strcmp(argv
[i
],"-display")==0) {
188 display_name
= argv
[++i
];
193 } else if (strcmp(argv
[i
],"-cutbuffer")==0
194 || strcmp(argv
[i
],"--cutbuffer")==0) {
197 if (sscanf(argv
[i
],"%i", &buffer
)!=1) {
198 fprintf(stderr
, "%s: could not convert \"%s\" to int\n",
202 if (buffer
<0 || buffer
> 7) {
203 fprintf(stderr
, "%s: invalid buffer number %i\n",
208 fprintf(stderr
, "%s: invalid argument '%s'\n", argv
[0],
210 fprintf(stderr
, "Try '%s --help' for more information.\n",
216 fprintf(stderr
, "%s: invalid argument '%s'\n", argv
[0], argv
[i
]);
217 fprintf(stderr
, "Try '%s --help' for more information.\n",
222 dpy
= XOpenDisplay(display_name
);
224 fprintf(stderr
, "%s: could not open display \"%s\"\n", argv
[0],
225 XDisplayName(display_name
));
229 if (selection_name
) {
230 buf
= fetchSelection(dpy
, selection_name
, argv
[0]);
236 buf
= XFetchBuffer(dpy
, &l
, buffer
);
242 if (write(STDOUT_FILENO
, buf
, l
) == -1)