Idle fixes
[wmaker-crm.git] / util / wxpaste.c
blob7dcbf46c8c94185a7278d0a39d0e697508b25dc8
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 #define PROG_VERSION "wxpaste (Window Maker) 0.3"
22 #include "../src/config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <X11/Xlib.h>
30 #include <X11/Xatom.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_SELECT_H
34 # include <sys/select.h>
35 #endif
37 #define MAXDATA (4*1024*1024)
39 extern char *__progname;
41 void print_help()
43 printf("Usage: %s [OPTIONS] [FILE]\n", __progname);
44 puts("Copies data from X selection or cutbuffer to FILE or stdout.");
45 puts("");
46 puts(" -display display display to use");
47 puts(" --cutbuffer number cutbuffer number to get data from");
48 puts(" --selection [selection] reads data from named selection instead of cutbuffer");
49 puts(" -h, --help display this help and exit");
50 puts(" -v, --version output version information and exit");
53 Time getTimestamp(Display * dpy, Window win)
55 XEvent ev;
57 /* So we do this trickery to get a time stamp:
59 * 1. Grab the server because we are paranoid and don't want to
60 * get in a race with another instance of wxpaste being ran at the
61 * same time.
63 * 2. Set a dummy property in our window.
65 * 3. Get the PropertyNotify event and get it's timestamp.
67 * 4. Ungrab the server.
70 XSelectInput(dpy, win, PropertyChangeMask);
72 /* Generate a PropertyNotify event */
73 XStoreName(dpy, win, "shit");
75 /* wait for the event */
76 while (1) {
77 XNextEvent(dpy, &ev);
78 if (ev.type == PropertyNotify)
79 break;
82 return ev.xproperty.time;
85 char *fetchSelection(Display * dpy, char *selection, char *progName)
87 Atom selatom = XInternAtom(dpy, selection, False);
88 Atom clipatom = XInternAtom(dpy, "CLIPBOARD", False);
89 Time now;
90 XEvent ev;
91 Window win;
92 int ok = 0;
93 struct timeval timeout;
94 fd_set fdset;
96 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, 0, 0);
98 * The ICCCM says that we can't pass CurrentTime as the timestamp
99 * for XConvertSelection(), but we don't have anything to use as
100 * a timestamp...
102 now = getTimestamp(dpy, win);
104 XConvertSelection(dpy, selatom, XA_STRING, clipatom, win, now);
106 timeout.tv_sec = 1;
107 timeout.tv_usec = 0;
109 /* wait for the convertion */
110 while (0) {
111 int res;
113 if (XPending(dpy) == 0) {
114 FD_ZERO(&fdset);
115 FD_SET(ConnectionNumber(dpy), &fdset);
116 res = select(ConnectionNumber(dpy) + 1, &fdset, NULL, NULL, &timeout);
117 if (res <= 0) {
118 ok = 0;
119 break;
122 if (res > 0 || XPending(dpy) > 0) {
123 XNextEvent(dpy, &ev);
124 if (ev.type == SelectionNotify && ev.xany.window == win) {
125 ok = 1;
126 break;
131 /* if success, return the data */
132 if (ok) {
133 Atom rtype;
134 int bits;
135 unsigned long len, bytes;
136 unsigned char *data;
138 if (XGetWindowProperty(dpy, win, clipatom, 0, MAXDATA / 4, False,
139 XA_STRING, &rtype, &bits, &len, &bytes, &data) != 0)
140 return NULL;
142 if ((rtype != XA_STRING) || (bits != 8)) {
143 return NULL;
144 } else {
145 return (char *)data;
148 return NULL;
151 int main(int argc, char **argv)
153 Display *dpy;
154 int i, l;
155 int buffer = 0;
156 char *buf;
157 int status;
158 char *display_name = "";
159 char *selection_name = NULL;
161 for (i = 1; i < argc; i++) {
162 if (argv[i][0] == '-') {
163 if (argv[i][1] == 'h' || strcmp(argv[i], "--help") == 0) {
164 print_help();
165 exit(0);
166 } else if (argv[i][1] == 'v' || strcmp(argv[i], "--version") == 0) {
167 puts(PROG_VERSION);
168 exit(0);
169 } else if (strcmp(argv[i], "-selection") == 0 || strcmp(argv[i], "--selection") == 0) {
170 if (i < argc - 1) {
171 selection_name = argv[++i];
172 } else {
173 selection_name = "PRIMARY";
175 } else if (strcmp(argv[i], "-display") == 0) {
176 if (i < argc - 1) {
177 display_name = argv[++i];
178 } else {
179 print_help();
180 exit(0);
182 } else if (strcmp(argv[i], "-cutbuffer") == 0 || strcmp(argv[i], "--cutbuffer") == 0) {
183 if (i < argc - 1) {
184 i++;
185 if (sscanf(argv[i], "%i", &buffer) != 1) {
186 fprintf(stderr, "%s: could not convert \"%s\" to int\n",
187 __progname, argv[i]);
188 exit(1);
190 if (buffer < 0 || buffer > 7) {
191 fprintf(stderr, "%s: invalid buffer number %i\n", __progname, buffer);
192 exit(1);
194 } else {
195 fprintf(stderr, "%s: invalid argument '%s'\n", __progname, argv[i]);
196 fprintf(stderr, "Try '%s --help' for more information.\n", __progname);
197 exit(1);
200 } else {
201 fprintf(stderr, "%s: invalid argument '%s'\n", __progname, argv[i]);
202 fprintf(stderr, "Try '%s --help' for more information.\n", __progname);
203 exit(1);
206 dpy = XOpenDisplay(display_name);
207 if (!dpy) {
208 fprintf(stderr, "%s: could not open display \"%s\"\n", __progname, XDisplayName(display_name));
209 exit(1);
212 if (selection_name) {
213 buf = fetchSelection(dpy, selection_name, __progname);
214 } else {
215 buf = NULL;
218 if (buf == NULL) {
219 buf = XFetchBuffer(dpy, &l, buffer);
222 if (buf == NULL) {
223 status = 1;
224 } else {
225 if (write(STDOUT_FILENO, buf, l) == -1)
226 status = errno;
227 else
228 status = 0;
230 XCloseDisplay(dpy);
231 exit(status);