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>
17 static char *shell = NULL;
20 error(char *errstr, ...) {
23 vfprintf(stderr, errstr, ap);
29 bad_malloc(unsigned int size)
31 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
37 emallocz(unsigned int size)
39 void *res = calloc(1, size);
46 emalloc(unsigned int size)
48 void *res = malloc(size);
55 erealloc(void *ptr, unsigned int size)
57 void *res = realloc(ptr, size);
64 estrdup(const char *str)
66 void *res = strdup(str);
68 bad_malloc(strlen(str));
73 failed_assert(char *a, char *file, int line)
75 fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
80 swap(void **p1, void **p2)
88 spawn(Display *dpy, const char *cmd)
90 if(!shell && !(shell = getenv("SHELL")))
98 close(ConnectionNumber(dpy));
100 fprintf(stderr, "gridwm: execlp %s %s -c %s", shell, shell, cmd);
101 execlp(shell, shell, "-c", cmd, NULL);
102 fprintf(stderr, "gridwm: execlp %s", cmd);
111 pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd)
116 if(!shell && !(shell = getenv("SHELL")))
122 if(pipe(pfd) == -1) {
129 close(ConnectionNumber(dpy));
131 dup2(pfd[1], STDOUT_FILENO);
134 execlp(shell, shell, "-c", cmd, NULL);
135 fprintf(stderr, "gridwm: execlp %s", cmd);
142 if((l = read(pfd[0], buf + n, len - n)) < 1)
154 getselection(unsigned long offset, unsigned long *len, unsigned long *remain)
163 unsigned char *result = NULL;
165 dpy = XOpenDisplay(0);
168 xa_clip_string = XInternAtom(dpy, "_SEL_STRING", False);
169 w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200,
170 1, CopyFromParent, CopyFromParent);
171 XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string,
174 XNextEvent(dpy, &ev);
175 if(ev.type == SelectionNotify && ev.xselection.property != None) {
176 XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False,
177 AnyPropertyType, &typeret, &format, len, remain, &data);
179 result = emalloc(sizeof(unsigned char) * *len);
180 memcpy(result, data, *len);
182 XDeleteProperty(dpy, w, ev.xselection.property);
184 XDestroyWindow(dpy, w);