progs/perf: make teximage results easier to read, more reproducible
[mesa.git] / progs / miniglx / sample_server.c
1
2 /*
3 * Sample server that just keeps first available window mapped.
4 */
5
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <GL/gl.h>
10 #include <GL/miniglx.h>
11
12 struct client {
13 struct client *next;
14 Window windowid;
15 int mappable;
16 };
17
18 struct client *clients = 0, *mapped_client = 0;
19
20
21 static struct client *find_client( Window id )
22 {
23 struct client *c;
24
25 for (c = clients ; c ; c = c->next)
26 if (c->windowid == id)
27 return c;
28
29 return 0;
30 }
31
32 int main( int argc, char *argv[] )
33 {
34 Display *dpy;
35 XEvent ev;
36
37 dpy = __miniglx_StartServer(NULL);
38 if (!dpy) {
39 fprintf(stderr, "Error: __miniglx_StartServer failed\n");
40 return 1;
41 }
42
43 while (XNextEvent( dpy, &ev )) {
44 struct client *c;
45
46 switch (ev.type) {
47 case MapRequest:
48 fprintf(stderr, "MapRequest\n");
49 c = find_client(ev.xmaprequest.window);
50 if (!c) break;
51 c->mappable = True;
52 break;
53
54 case UnmapNotify:
55 fprintf(stderr, "UnmapNotify\n");
56 c = find_client(ev.xunmap.window);
57 if (!c) break;
58 c->mappable = False;
59 if (c == mapped_client)
60 mapped_client = 0;
61 break;
62
63 case CreateNotify:
64 fprintf(stderr, "CreateNotify\n");
65 c = malloc(sizeof(*c));
66 c->next = clients;
67 c->windowid = ev.xcreatewindow.window;
68 c->mappable = False;
69 clients = c;
70 break;
71
72 case DestroyNotify:
73 fprintf(stderr, "DestroyNotify\n");
74 c = find_client(ev.xdestroywindow.window);
75 if (!c) break;
76 if (c == clients)
77 clients = c->next;
78 else {
79 struct client *t;
80 for (t = clients ; t->next != c ; t = t->next)
81 ;
82 t->next = c->next;
83 }
84
85 if (c == mapped_client)
86 mapped_client = 0;
87
88 free(c);
89 break;
90
91 default:
92 break;
93 }
94
95 /* Search for first mappable client if none already mapped.
96 */
97 if (!mapped_client) {
98 for (c = clients ; c ; c = c->next) {
99 if (c->mappable) {
100 XMapWindow( dpy, c->windowid );
101 mapped_client = c;
102 break;
103 }
104 }
105 }
106 }
107
108 XCloseDisplay( dpy );
109
110 return 0;
111 }