2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
10 #include <sys/types.h>
13 #include <X11/Xatom.h>
18 error(char *errstr, ...) {
21 vfprintf(stderr, errstr, ap);
27 bad_malloc(unsigned int size)
29 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
35 emallocz(unsigned int size)
37 void *res = calloc(1, size);
44 emalloc(unsigned int size)
46 void *res = malloc(size);
53 erealloc(void *ptr, unsigned int size)
55 void *res = realloc(ptr, size);
62 estrdup(const char *str)
64 void *res = strdup(str);
66 bad_malloc(strlen(str));
71 failed_assert(char *a, char *file, int line)
73 fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
78 swap(void **p1, void **p2)
86 spawn(Display *dpy, char *argv[])
93 close(ConnectionNumber(dpy));
95 execvp(argv[0], argv);
96 fprintf(stderr, "gridwm: execvp %s", argv[0]);
105 pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[])
110 if(!argv || !argv[0])
113 if(pipe(pfd) == -1) {
120 close(ConnectionNumber(dpy));
122 dup2(pfd[1], STDOUT_FILENO);
125 execvp(argv[0], argv);
126 fprintf(stderr, "gridwm: execvp %s", argv[0]);
133 if((l = read(pfd[0], buf + n, len - n)) < 1)
145 getselection(unsigned long offset, unsigned long *len, unsigned long *remain)
154 unsigned char *result = NULL;
156 dpy = XOpenDisplay(0);
159 xa_clip_string = XInternAtom(dpy, "_SEL_STRING", False);
160 w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200,
161 1, CopyFromParent, CopyFromParent);
162 XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string,
165 XNextEvent(dpy, &ev);
166 if(ev.type == SelectionNotify && ev.xselection.property != None) {
167 XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False,
168 AnyPropertyType, &typeret, &format, len, remain, &data);
170 result = emalloc(sizeof(unsigned char) * *len);
171 memcpy(result, data, *len);
173 XDeleteProperty(dpy, w, ev.xselection.property);
175 XDestroyWindow(dpy, w);