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.

178 lines
3.1 KiB

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