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