Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

sample_server2.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Sample server that just keeps first available window mapped.
  3. *
  4. * It also reads and echos anything that happens on stdin as an
  5. * example of tracking events from sources other than miniglx clients.
  6. *
  7. * It reads & writes without blocking, so that eg. piping a lot of
  8. * text to stdin and then hitting 'ctrl-S' on the output stream won't
  9. * cause it to stop handling miniglx events.
  10. *
  11. * See select_tut in the linux manual pages for a good overview of the
  12. * select(2) system call.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <GL/gl.h>
  19. #include <GL/miniglx.h>
  20. #include <errno.h>
  21. #include <assert.h>
  22. struct client {
  23. struct client *next;
  24. Window windowid;
  25. int mappable;
  26. };
  27. struct client *clients = 0, *mapped_client = 0;
  28. #define BUFSZ 4096
  29. char rbuf[BUFSZ];
  30. int rbuf_count;
  31. static struct client *find_client( Window id )
  32. {
  33. struct client *c;
  34. for (c = clients ; c ; c = c->next)
  35. if (c->windowid == id)
  36. return c;
  37. return 0;
  38. }
  39. int main( int argc, char *argv[] )
  40. {
  41. Display *dpy;
  42. XEvent ev;
  43. int autostart = 0;
  44. if (argc == 2 && strcmp(argv[1], "-autostart") == 0)
  45. autostart = 1;
  46. dpy = __miniglx_StartServer(NULL);
  47. if (!dpy) {
  48. fprintf(stderr, "Error: __miniglx_StartServer failed\n");
  49. return 1;
  50. }
  51. /* How is vt switching communicated through the XNextEvent interface?
  52. */
  53. while (1) {
  54. int r, n;
  55. struct timeval tv;
  56. fd_set rfds, wfds;
  57. int bored = 0;
  58. FD_ZERO(&rfds);
  59. FD_ZERO(&wfds);
  60. tv.tv_sec = 1;
  61. tv.tv_usec = 0;
  62. if (rbuf_count) {
  63. FD_SET( 1, &wfds ); /* notify when we can write out buffer */
  64. n = 1;
  65. }
  66. else {
  67. FD_SET( 0, &rfds ); /* else notify when new data to read */
  68. n = 0;
  69. }
  70. /* __miniglx_Select waits until any of these file groups becomes
  71. * readable/writable/etc (like regular select), until timeout
  72. * expires (like regular select), until a signal is received
  73. * (like regular select) or until an event is available for
  74. * XCheckMaskEvent().
  75. */
  76. r = __miniglx_Select( dpy, n+1, &rfds, &wfds, 0, &tv );
  77. /* This can happen if select() is interrupted by a signal:
  78. */
  79. if (r < 0 && errno != EINTR && errno != EAGAIN) {
  80. perror ("select()");
  81. exit (1);
  82. }
  83. if (tv.tv_sec == 0 && tv.tv_usec == 0)
  84. bored = 1;
  85. /* Check and handle events on our local file descriptors
  86. */
  87. if (FD_ISSET( 0, &rfds )) {
  88. /* Something on stdin */
  89. assert(rbuf_count == 0);
  90. r = read(0, rbuf, BUFSZ);
  91. if (r < 1) {
  92. perror("read");
  93. abort();
  94. }
  95. rbuf_count = r;
  96. }
  97. if (FD_ISSET( 1, &wfds )) {
  98. /* Can write to stdout */
  99. assert(rbuf_count > 0);
  100. r = write(1, rbuf, rbuf_count);
  101. if (r < 1) {
  102. perror("write");
  103. abort();
  104. }
  105. rbuf_count -= r;
  106. if (rbuf_count)
  107. memmove(rbuf + r, rbuf, rbuf_count);
  108. }
  109. /* Check and handle events generated by miniglx:
  110. */
  111. while (XCheckMaskEvent( dpy, ~0, &ev )) {
  112. struct client *c;
  113. bored = 0;
  114. fprintf(stderr, "Received event %d\n", ev.type);
  115. switch (ev.type) {
  116. case CreateNotify:
  117. fprintf(stderr, "CreateNotify -- new client\n");
  118. c = malloc(sizeof(*c));
  119. c->next = clients;
  120. c->windowid = ev.xcreatewindow.window;
  121. c->mappable = False;
  122. clients = c;
  123. break;
  124. case DestroyNotify:
  125. fprintf(stderr, "DestroyNotify\n");
  126. c = find_client(ev.xdestroywindow.window);
  127. if (!c) break;
  128. if (c == clients)
  129. clients = c->next;
  130. else {
  131. struct client *t;
  132. for (t = clients ; t->next != c ; t = t->next)
  133. ;
  134. t->next = c->next;
  135. }
  136. if (c == mapped_client)
  137. mapped_client = 0;
  138. free(c);
  139. break;
  140. case MapRequest:
  141. fprintf(stderr, "MapRequest\n");
  142. c = find_client(ev.xmaprequest.window);
  143. if (!c) break;
  144. c->mappable = True;
  145. break;
  146. case UnmapNotify:
  147. fprintf(stderr, "UnmapNotify\n");
  148. c = find_client(ev.xunmap.window);
  149. if (!c) break;
  150. c->mappable = False;
  151. if (c == mapped_client)
  152. mapped_client = 0;
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. /* Search for first mappable client if none already mapped.
  159. */
  160. if (!mapped_client) {
  161. struct client *c;
  162. for (c = clients ; c ; c = c->next) {
  163. if (c->mappable) {
  164. XMapWindow( dpy, c->windowid );
  165. mapped_client = c;
  166. break;
  167. }
  168. }
  169. if (!clients && autostart) {
  170. system("nohup ./texline &");
  171. system("nohup ./manytex &");
  172. }
  173. }
  174. else if (bored) {
  175. struct client *c;
  176. /* bored of mapped client now, let's try & find another one */
  177. for (c = mapped_client->next ; c && !c->mappable ; c = c->next)
  178. ;
  179. if (!c)
  180. for (c = clients ; c && !c->mappable ; c = c->next)
  181. ;
  182. if (c && c != mapped_client) {
  183. XUnmapWindow( dpy, mapped_client->windowid );
  184. XMapWindow( dpy, c->windowid );
  185. mapped_client = c;
  186. }
  187. else
  188. fprintf(stderr, "I'm bored!\n");
  189. }
  190. }
  191. XCloseDisplay( dpy );
  192. return 0;
  193. }