WindowMaker: New function wGetWorkspaceNumber
[wmaker-crm.git] / util / wxpaste.c
blob87fc8920d63d1908998e4d4cde49935b28ff260c
1 /* wxpaste.c- paste contents of cutbuffer to stdout
3 * Copyright (c) 1997-2003 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 #include "../config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xatom.h>
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_SELECT_H
32 # include <sys/select.h>
33 #endif
35 #define MAXDATA (4*1024*1024)
37 extern char *__progname;
39 void print_help()
41 printf("Usage: %s [OPTIONS] [FILE]\n", __progname);
42 puts("Copies data from X selection or cutbuffer to FILE or stdout.");
43 puts("");
44 puts(" -display display display to use");
45 puts(" --cutbuffer number cutbuffer number to get data from");
46 puts(" --selection [selection] reads data from named selection instead of cutbuffer");
47 puts(" -h, --help display this help and exit");
48 puts(" -v, --version output version information and exit");
51 Time getTimestamp(Display * dpy, Window win)
53 XEvent ev;
55 /* So we do this trickery to get a time stamp:
57 * 1. Grab the server because we are paranoid and don't want to
58 * get in a race with another instance of wxpaste being ran at the
59 * same time.
61 * 2. Set a dummy property in our window.
63 * 3. Get the PropertyNotify event and get it's timestamp.
65 * 4. Ungrab the server.
68 XSelectInput(dpy, win, PropertyChangeMask);
70 /* Generate a PropertyNotify event */
71 XStoreName(dpy, win, "shit");
73 /* wait for the event */
74 while (1) {
75 XNextEvent(dpy, &ev);
76 if (ev.type == PropertyNotify)
77 break;
80 return ev.xproperty.time;
83 char *fetchSelection(Display * dpy, char *selection, char *progName)
85 Atom selatom = XInternAtom(dpy, selection, False);
86 Atom clipatom = XInternAtom(dpy, "CLIPBOARD", False);
87 Time now;
88 XEvent ev;
89 Window win;
90 int ok = 0;
91 struct timeval timeout;
92 fd_set fdset;
94 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, 0, 0);
96 * The ICCCM says that we can't pass CurrentTime as the timestamp
97 * for XConvertSelection(), but we don't have anything to use as
98 * a timestamp...
100 now = getTimestamp(dpy, win);
102 XConvertSelection(dpy, selatom, XA_STRING, clipatom, win, now);
104 timeout.tv_sec = 1;
105 timeout.tv_usec = 0;
107 /* wait for the convertion */
108 while (0) {
109 int res;
111 if (XPending(dpy) == 0) {
112 FD_ZERO(&fdset);
113 FD_SET(ConnectionNumber(dpy), &fdset);
114 res = select(ConnectionNumber(dpy) + 1, &fdset, NULL, NULL, &timeout);
115 if (res <= 0) {
116 ok = 0;
117 break;
120 if (res > 0 || XPending(dpy) > 0) {
121 XNextEvent(dpy, &ev);
122 if (ev.type == SelectionNotify && ev.xany.window == win) {
123 ok = 1;
124 break;
129 /* if success, return the data */
130 if (ok) {
131 Atom rtype;
132 int bits;
133 unsigned long len, bytes;
134 unsigned char *data;
136 if (XGetWindowProperty(dpy, win, clipatom, 0, MAXDATA / 4, False,
137 XA_STRING, &rtype, &bits, &len, &bytes, &data) != 0)
138 return NULL;
140 if ((rtype != XA_STRING) || (bits != 8)) {
141 return NULL;
142 } else {
143 return (char *)data;
146 return NULL;
149 int main(int argc, char **argv)
151 Display *dpy;
152 int i, l;
153 int buffer = 0;
154 char *buf;
155 int status;
156 char *display_name = "";
157 char *selection_name = NULL;
159 for (i = 1; i < argc; i++) {
160 if (argv[i][0] == '-') {
161 if (argv[i][1] == 'h' || strcmp(argv[i], "--help") == 0) {
162 print_help();
163 exit(0);
164 } else if (argv[i][1] == 'v' || strcmp(argv[i], "--version") == 0) {
165 printf("%s (Window Maker %s)\n", __progname, VERSION);
166 exit(0);
167 } else if (strcmp(argv[i], "-selection") == 0 || strcmp(argv[i], "--selection") == 0) {
168 if (i < argc - 1) {
169 selection_name = argv[++i];
170 } else {
171 selection_name = "PRIMARY";
173 } else if (strcmp(argv[i], "-display") == 0) {
174 if (i < argc - 1) {
175 display_name = argv[++i];
176 } else {
177 print_help();
178 exit(0);
180 } else if (strcmp(argv[i], "-cutbuffer") == 0 || strcmp(argv[i], "--cutbuffer") == 0) {
181 if (i < argc - 1) {
182 i++;
183 if (sscanf(argv[i], "%i", &buffer) != 1) {
184 fprintf(stderr, "%s: could not convert \"%s\" to int\n",
185 __progname, argv[i]);
186 exit(1);
188 if (buffer < 0 || buffer > 7) {
189 fprintf(stderr, "%s: invalid buffer number %i\n", __progname, buffer);
190 exit(1);
192 } else {
193 fprintf(stderr, "%s: invalid argument '%s'\n", __progname, argv[i]);
194 fprintf(stderr, "Try '%s --help' for more information.\n", __progname);
195 exit(1);
198 } else {
199 fprintf(stderr, "%s: invalid argument '%s'\n", __progname, argv[i]);
200 fprintf(stderr, "Try '%s --help' for more information.\n", __progname);
201 exit(1);
204 dpy = XOpenDisplay(display_name);
205 if (!dpy) {
206 fprintf(stderr, "%s: could not open display \"%s\"\n", __progname, XDisplayName(display_name));
207 exit(1);
210 if (selection_name) {
211 buf = fetchSelection(dpy, selection_name, __progname);
212 } else {
213 buf = NULL;
216 if (buf == NULL) {
217 buf = XFetchBuffer(dpy, &l, buffer);
220 if (buf == NULL) {
221 status = 1;
222 } else {
223 if (write(STDOUT_FILENO, buf, l) == -1)
224 status = errno;
225 else
226 status = 0;
228 XCloseDisplay(dpy);
229 exit(status);