Updating to version 0.20.2
[wmaker-crm.git] / util / wxpaste.c
blobd66bfbe187f1df6691d314566dbf9608dcf5f060
1 /* wxpaste.c- paste contents of cutbuffer to stdout
2 *
3 * Copyright (c) 1997 Alfredo K. Kojima
4 *
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 "../src/config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xatom.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SELECT_H
31 # include <sys/select.h>
32 #endif
35 #define MAXDATA (4*1024*1024)
38 void
39 help(char *progn)
41 fprintf(stderr,
42 "usage: %s [options]\n"
43 " -display display-name\n"
44 " -cutbuffer number\n"
45 " -selection [selection-name]\n"
46 , progn);
50 Time
51 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;
84 char*
85 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,
97 0, 0, 0);
98 /*
99 * The ICCCM says that we can't pass CurrentTime as the timestamp
100 * for XConvertSelection(), but we don't have anything to use as
101 * a timestamp...
103 now = getTimestamp(dpy, win);
105 XConvertSelection(dpy, selatom, XA_STRING, clipatom, win, now);
107 timeout.tv_sec = 1;
108 timeout.tv_usec = 0;
110 /* wait for the convertion */
111 while (0) {
112 int res;
114 if (XPending(dpy)==0) {
115 FD_ZERO(&fdset);
116 FD_SET(ConnectionNumber(dpy), &fdset);
117 res = select(ConnectionNumber(dpy)+1, &fdset, NULL, NULL,
118 &timeout);
119 if (res <= 0) {
120 ok = 0;
121 break;
124 if (res > 0 || XPending(dpy) > 0) {
125 XNextEvent(dpy, &ev);
126 if (ev.type == SelectionNotify && ev.xany.window == win) {
127 ok = 1;
128 break;
133 /* if success, return the data */
134 if (ok) {
135 Atom rtype;
136 int bits;
137 unsigned long len, bytes;
138 unsigned char *data;
140 if (XGetWindowProperty(dpy, win, clipatom, 0, MAXDATA/4, False,
141 XA_STRING, &rtype, &bits, &len, &bytes, &data)!=0)
142 return NULL;
144 if ((rtype!=XA_STRING) || (bits!=8)) {
145 return NULL;
146 } else {
147 return (char*)data;
150 return NULL;
155 main(int argc, char **argv)
157 Display *dpy;
158 int i, l;
159 int buffer=0;
160 char *buf;
161 int status;
162 char *display_name="";
163 char *selection_name=NULL;
165 for (i=1; i<argc; i++) {
166 if (argv[i][0]=='-') {
167 if (argv[i][1]=='h') {
168 help(argv[0]);
169 exit(0);
170 } else if (strcmp(argv[i],"-selection")==0) {
171 if (i<argc-1) {
172 selection_name = argv[++i];
173 } else {
174 selection_name = "PRIMARY";
176 } else if (strcmp(argv[i],"-display")==0) {
177 if (i<argc-1) {
178 display_name = argv[++i];
179 } else {
180 help(argv[0]);
181 exit(0);
183 } else if (strcmp(argv[i],"-cutbuffer")==0) {
184 if (i<argc-1) {
185 i++;
186 if (sscanf(argv[i],"%i", &buffer)!=1) {
187 fprintf(stderr, "%s: could not convert \"%s\" to int\n",
188 argv[0], argv[i]);
189 exit(1);
191 if (buffer<0 || buffer > 7) {
192 fprintf(stderr, "%s: invalid buffer number %i\n",
193 argv[0], buffer);
194 exit(1);
196 } else {
197 help(argv[0]);
198 exit(1);
201 } else {
202 help(argv[0]);
203 exit(1);
206 dpy = XOpenDisplay(display_name);
207 if (!dpy) {
208 fprintf(stderr, "%s: could not open display \"%s\"\n", argv[0],
209 XDisplayName(display_name));
210 exit(1);
213 if (selection_name) {
214 buf = fetchSelection(dpy, selection_name, argv[0]);
215 } else {
216 buf = NULL;
219 if (buf == NULL) {
220 buf = XFetchBuffer(dpy, &l, buffer);
223 if (buf == NULL) {
224 status = 1;
225 } else {
226 if (write(STDIN_FILENO, buf, l) == -1)
227 status = errno;
228 else
229 status = 0;
231 XCloseDisplay(dpy);
232 exit(status);