aewl

annotate util.c @ 14:5c078b66347b

added bar event timer
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 18:15:11 +0200
parents ea9c08ec4b48
children 00d4d52b231f
rev   line source
garbeam@2 1 /*
garbeam@2 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@2 3 * See LICENSE file for license details.
garbeam@2 4 */
garbeam@2 5
garbeam@2 6 #include <stdarg.h>
garbeam@2 7 #include <stdio.h>
garbeam@2 8 #include <stdlib.h>
garbeam@3 9 #include <string.h>
garbeam@5 10 #include <sys/types.h>
garbeam@5 11 #include <sys/wait.h>
garbeam@5 12 #include <unistd.h>
garbeam@11 13 #include <X11/Xatom.h>
garbeam@5 14
garbeam@5 15 #include "util.h"
garbeam@2 16
garbeam@2 17 void
garbeam@2 18 error(char *errstr, ...) {
garbeam@2 19 va_list ap;
garbeam@2 20 va_start(ap, errstr);
garbeam@2 21 vfprintf(stderr, errstr, ap);
garbeam@2 22 va_end(ap);
garbeam@2 23 exit(1);
garbeam@2 24 }
garbeam@2 25
garbeam@3 26 static void
garbeam@3 27 bad_malloc(unsigned int size)
garbeam@3 28 {
garbeam@3 29 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
garbeam@3 30 (int) size);
garbeam@3 31 exit(1);
garbeam@3 32 }
garbeam@3 33
garbeam@3 34 void *
garbeam@3 35 emallocz(unsigned int size)
garbeam@3 36 {
garbeam@3 37 void *res = calloc(1, size);
garbeam@3 38 if(!res)
garbeam@3 39 bad_malloc(size);
garbeam@3 40 return res;
garbeam@3 41 }
garbeam@3 42
garbeam@3 43 void *
garbeam@3 44 emalloc(unsigned int size)
garbeam@3 45 {
garbeam@3 46 void *res = malloc(size);
garbeam@3 47 if(!res)
garbeam@3 48 bad_malloc(size);
garbeam@3 49 return res;
garbeam@3 50 }
garbeam@3 51
garbeam@3 52 void *
garbeam@3 53 erealloc(void *ptr, unsigned int size)
garbeam@3 54 {
garbeam@3 55 void *res = realloc(ptr, size);
garbeam@3 56 if(!res)
garbeam@3 57 bad_malloc(size);
garbeam@3 58 return res;
garbeam@3 59 }
garbeam@3 60
garbeam@3 61 char *
garbeam@3 62 estrdup(const char *str)
garbeam@3 63 {
garbeam@3 64 void *res = strdup(str);
garbeam@3 65 if(!res)
garbeam@3 66 bad_malloc(strlen(str));
garbeam@3 67 return res;
garbeam@3 68 }
garbeam@3 69
garbeam@3 70 void
garbeam@3 71 failed_assert(char *a, char *file, int line)
garbeam@3 72 {
garbeam@3 73 fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
garbeam@3 74 abort();
garbeam@3 75 }
garbeam@3 76
garbeam@3 77 void
garbeam@3 78 swap(void **p1, void **p2)
garbeam@3 79 {
garbeam@3 80 void *tmp = *p1;
garbeam@3 81 *p1 = *p2;
garbeam@3 82 *p2 = tmp;
garbeam@3 83 }
garbeam@5 84
garbeam@5 85 void
garbeam@14 86 spawn(Display *dpy, char *argv[])
garbeam@5 87 {
garbeam@14 88 if(!argv || !argv[0])
garbeam@5 89 return;
garbeam@5 90 if(fork() == 0) {
garbeam@5 91 if(fork() == 0) {
garbeam@5 92 if(dpy)
garbeam@5 93 close(ConnectionNumber(dpy));
garbeam@9 94 setsid();
garbeam@14 95 execvp(argv[0], argv);
garbeam@14 96 fprintf(stderr, "gridwm: execvp %s", argv[0]);
garbeam@5 97 perror(" failed");
garbeam@5 98 }
garbeam@5 99 exit (0);
garbeam@5 100 }
garbeam@5 101 wait(0);
garbeam@5 102 }
garbeam@6 103
garbeam@6 104 void
garbeam@14 105 pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[])
garbeam@6 106 {
garbeam@6 107 unsigned int l, n;
garbeam@6 108 int pfd[2];
garbeam@6 109
garbeam@14 110 if(!argv || !argv[0])
garbeam@6 111 return;
garbeam@6 112
garbeam@6 113 if(pipe(pfd) == -1) {
garbeam@6 114 perror("pipe");
garbeam@6 115 exit(1);
garbeam@6 116 }
garbeam@6 117
garbeam@6 118 if(fork() == 0) {
garbeam@6 119 if(dpy)
garbeam@6 120 close(ConnectionNumber(dpy));
garbeam@9 121 setsid();
garbeam@6 122 dup2(pfd[1], STDOUT_FILENO);
garbeam@6 123 close(pfd[0]);
garbeam@6 124 close(pfd[1]);
garbeam@14 125 execvp(argv[0], argv);
garbeam@14 126 fprintf(stderr, "gridwm: execvp %s", argv[0]);
garbeam@6 127 perror(" failed");
garbeam@6 128 }
garbeam@6 129 else {
garbeam@6 130 n = 0;
garbeam@6 131 close(pfd[1]);
garbeam@6 132 while(l > n) {
garbeam@6 133 if((l = read(pfd[0], buf + n, len - n)) < 1)
garbeam@6 134 break;
garbeam@6 135 n += l;
garbeam@6 136 }
garbeam@6 137 close(pfd[0]);
garbeam@6 138 buf[n - 1] = 0;
garbeam@6 139 }
garbeam@6 140 wait(0);
garbeam@6 141 }
garbeam@11 142
garbeam@11 143
garbeam@11 144 unsigned char *
garbeam@11 145 getselection(unsigned long offset, unsigned long *len, unsigned long *remain)
garbeam@11 146 {
garbeam@11 147 Display *dpy;
garbeam@11 148 Atom xa_clip_string;
garbeam@11 149 Window w;
garbeam@11 150 XEvent ev;
garbeam@11 151 Atom typeret;
garbeam@11 152 int format;
garbeam@11 153 unsigned char *data;
garbeam@11 154 unsigned char *result = NULL;
garbeam@11 155
garbeam@11 156 dpy = XOpenDisplay(0);
garbeam@11 157 if(!dpy)
garbeam@11 158 return NULL;
garbeam@11 159 xa_clip_string = XInternAtom(dpy, "_SEL_STRING", False);
garbeam@11 160 w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200,
garbeam@11 161 1, CopyFromParent, CopyFromParent);
garbeam@11 162 XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string,
garbeam@11 163 w, CurrentTime);
garbeam@11 164 XFlush(dpy);
garbeam@11 165 XNextEvent(dpy, &ev);
garbeam@11 166 if(ev.type == SelectionNotify && ev.xselection.property != None) {
garbeam@11 167 XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False,
garbeam@11 168 AnyPropertyType, &typeret, &format, len, remain, &data);
garbeam@11 169 if(*len) {
garbeam@11 170 result = emalloc(sizeof(unsigned char) * *len);
garbeam@11 171 memcpy(result, data, *len);
garbeam@11 172 }
garbeam@11 173 XDeleteProperty(dpy, w, ev.xselection.property);
garbeam@11 174 }
garbeam@11 175 XDestroyWindow(dpy, w);
garbeam@11 176 XCloseDisplay(dpy);
garbeam@11 177 return result;
garbeam@11 178 }