Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
3.3 KiB

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