summaryrefslogtreecommitdiffstats
path: root/progs/miniglx/sample_server.c
blob: 039c04fa40ae27cd6d5065b772c0c6052c3bf242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* $Id: sample_server.c,v 1.1 2003/08/06 17:47:15 keithw Exp $ */

/*
 * Sample server that just keeps first available window mapped.
 */


#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/miniglx.h>

struct client {
   struct client *next;
   Window windowid;
   int mappable;
};

struct client *clients = 0, *mapped_client = 0;


static struct client *find_client( Window id )
{
   struct client *c;

   for (c = clients ; c ; c = c->next)
      if (c->windowid == id)
	 return c;

   return 0;
}

int main( int argc, char *argv[] )
{
   Display *dpy;
   XEvent ev;

   dpy = __miniglx_StartServer(NULL);
   if (!dpy) {
      fprintf(stderr, "Error: __miniglx_StartServer failed\n");
      return 1;
   }

   while (XNextEvent( dpy, &ev )) {
      struct client *c;

      switch (ev.type) {
      case MapRequest:
	 fprintf(stderr, "MapRequest\n");
	 c = find_client(ev.xmaprequest.window);
	 if (!c) break;
	 c->mappable = True;
	 break;

      case UnmapNotify:
	 fprintf(stderr, "UnmapNotify\n");
	 c = find_client(ev.xunmap.window);
	 if (!c) break;
	 c->mappable = False;
	 if (c == mapped_client)
	    mapped_client = 0;
	 break;

      case CreateNotify: 
	 fprintf(stderr, "CreateNotify\n");
	 c = malloc(sizeof(*c));
	 c->next = clients;
	 c->windowid = ev.xcreatewindow.window;
	 c->mappable = False;
	 clients = c;
	 break;

      case DestroyNotify:
	 fprintf(stderr, "DestroyNotify\n");
	 c = find_client(ev.xdestroywindow.window);
	 if (!c) break;
	 if (c == clients)
	    clients = c->next;
	 else {
	    struct client *t;
	    for (t = clients ; t->next != c ; t = t->next)
	       ;
	    t->next = c->next;
	 }

	 if (c == mapped_client) 
	    mapped_client = 0;

	 free(c);
	 break;

      default:
	 break;
      }

      /* Search for first mappable client if none already mapped.
       */
      if (!mapped_client) {
	 for (c = clients ; c ; c = c->next) {
	    if (c->mappable) {
	       XMapWindow( dpy, c->windowid );
	       mapped_client = c;
	       break;
	    }
	 }
      }
   }

   XCloseDisplay( dpy );

   return 0;
}