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., 51 Franklin St, Fifth Floor, Boston,
21 #include "../config.h"
29 #include <X11/Xatom.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SELECT_H
33 # include <sys/select.h>
36 #define MAXDATA (4*1024*1024)
38 static const char *prog_name
;
40 static void print_help(void)
42 printf("Usage: %s [OPTIONS]\n", prog_name
);
43 puts("Copies data from X selection or cutbuffer to stdout.");
45 puts(" -display display display to use");
46 puts(" --cutbuffer number cutbuffer number to get data from");
47 puts(" --selection [selection] reads data from named selection instead of cutbuffer");
48 puts(" -h, --help display this help and exit");
49 puts(" -v, --version output version information and exit");
52 static Time
getTimestamp(Display
* dpy
, Window win
)
56 /* So we do this trickery to get a time stamp:
58 * 1. Grab the server because we are paranoid and don't want to
59 * get in a race with another instance of wxpaste being ran at the
62 * 2. Set a dummy property in our window.
64 * 3. Get the PropertyNotify event and get its timestamp.
66 * 4. Ungrab the server.
69 XSelectInput(dpy
, win
, PropertyChangeMask
);
71 /* Generate a PropertyNotify event */
72 XStoreName(dpy
, win
, "shit");
74 /* wait for the event */
77 if (ev
.type
== PropertyNotify
)
81 return ev
.xproperty
.time
;
84 static char *fetchSelection(Display
*dpy
, const char *selection
)
86 Atom selatom
= XInternAtom(dpy
, selection
, False
);
87 Atom clipatom
= XInternAtom(dpy
, "CLIPBOARD", False
);
92 struct timeval timeout
;
95 win
= XCreateSimpleWindow(dpy
, DefaultRootWindow(dpy
), 0, 0, 1, 1, 0, 0, 0);
97 * The ICCCM says that we can't pass CurrentTime as the timestamp
98 * for XConvertSelection(), but we don't have anything to use as
101 now
= getTimestamp(dpy
, win
);
103 XConvertSelection(dpy
, selatom
, XA_STRING
, clipatom
, win
, now
);
108 /* wait for the convertion */
112 if (XPending(dpy
) == 0) {
114 FD_SET(ConnectionNumber(dpy
), &fdset
);
115 res
= select(ConnectionNumber(dpy
) + 1, &fdset
, NULL
, NULL
, &timeout
);
121 if (res
> 0 || XPending(dpy
) > 0) {
122 XNextEvent(dpy
, &ev
);
123 if (ev
.type
== SelectionNotify
&& ev
.xany
.window
== win
) {
130 /* if success, return the data */
134 unsigned long len
, bytes
;
137 if (XGetWindowProperty(dpy
, win
, clipatom
, 0, MAXDATA
/ 4, False
,
138 XA_STRING
, &rtype
, &bits
, &len
, &bytes
, &data
) != 0)
141 if ((rtype
!= XA_STRING
) || (bits
!= 8)) {
150 int main(int argc
, char **argv
)
157 char *display_name
= "";
158 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) {
166 } else if (argv
[i
][1] == 'v' || strcmp(argv
[i
], "--version") == 0) {
167 printf("%s (Window Maker %s)\n", prog_name
, VERSION
);
169 } else if (strcmp(argv
[i
], "-selection") == 0 || strcmp(argv
[i
], "--selection") == 0) {
171 selection_name
= argv
[++i
];
173 selection_name
= "PRIMARY";
175 } else if (strcmp(argv
[i
], "-display") == 0) {
177 display_name
= argv
[++i
];
182 } else if (strcmp(argv
[i
], "-cutbuffer") == 0 || strcmp(argv
[i
], "--cutbuffer") == 0) {
185 if (sscanf(argv
[i
], "%i", &buffer
) != 1) {
186 fprintf(stderr
, "%s: could not convert \"%s\" to int\n",
190 if (buffer
< 0 || buffer
> 7) {
191 fprintf(stderr
, "%s: invalid buffer number %i\n", prog_name
, buffer
);
195 fprintf(stderr
, "%s: invalid argument '%s'\n", prog_name
, argv
[i
]);
196 fprintf(stderr
, "Try '%s --help' for more information.\n", prog_name
);
201 fprintf(stderr
, "%s: invalid argument '%s'\n", prog_name
, argv
[i
]);
202 fprintf(stderr
, "Try '%s --help' for more information.\n", prog_name
);
206 dpy
= XOpenDisplay(display_name
);
208 fprintf(stderr
, "%s: could not open display \"%s\"\n", prog_name
, XDisplayName(display_name
));
212 if (selection_name
) {
213 buf
= fetchSelection(dpy
, selection_name
);
219 buf
= XFetchBuffer(dpy
, &l
, buffer
);
225 if (write(STDOUT_FILENO
, buf
, l
) == -1)