]> git.cworth.org Git - fips/blob - test/common.c
test: Rename handle-events.c to common.c
[fips] / test / common.c
1 /* Copyright © 2013, Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 /* This is C code intended to be included (yes, included) by test
23  * programs in the fips test suite.
24  *
25  * It defines two functions for use by the test:
26  *
27  *      static void
28  *      common_create_context (Display *dpy, GLXContext *ctx, XVisualInfo **vi);
29  * and:
30  *      static void
31  *      common_handle_events (Display *dpy, Window window);
32  *
33  * Before including this file, the test program must define a macro
34  * COMMON_GL_PREFIX to some prefix. This macro can be empty (to
35  * directly call functions in an OpenGL-library directly linked) or
36  * can be some custom prefix to call through symbols defined in the
37  * test program.
38  */
39
40 #ifndef COMMON_GL_PREFIX
41 #error Code including common.c must define COMMON_GL_PREFIX
42 #endif
43
44 #define concat_(a,b) a ## b
45 #define concat(a,b) concat_(a,b)
46 #define _(func) concat(COMMON_GL_PREFIX, func)
47
48 static void
49 set_2d_projection (int width, int height)
50 {
51         _(glMatrixMode) (GL_PROJECTION);
52         _(glLoadIdentity) ();
53         _(glOrtho) (0, width, height, 0, 0, 1);
54         _(glMatrixMode) (GL_MODELVIEW);
55 }
56
57 static void
58 paint_rgb_using_clear (double r, double g, double b)
59 {
60         _(glClearColor) (r, g, b, 1.0);
61         _(glClear) (GL_COLOR_BUFFER_BIT);
62 }
63
64 static void
65 common_create_context (Display *dpy,
66                        GLXContext *ctx_ret, XVisualInfo **visual_info_ret)
67 {
68         int visual_attr[] = {
69                 GLX_RGBA,
70                 GLX_RED_SIZE,           8,
71                 GLX_GREEN_SIZE,         8,
72                 GLX_BLUE_SIZE,          8,
73                 GLX_ALPHA_SIZE,         8,
74                 GLX_DOUBLEBUFFER,
75                 GLX_DEPTH_SIZE,         24,
76                 GLX_STENCIL_SIZE,       8,
77                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
78                 None
79         };
80
81         /* Window and context setup. */
82         *visual_info_ret = _(glXChooseVisual) (dpy, 0, visual_attr);
83         *ctx_ret = _(glXCreateContext) (dpy, *visual_info_ret, NULL, True);
84 }
85
86 static void
87 draw (Display *dpy, GLXContext ctx, Window window, int width, int height)
88 {
89         int i;
90         _(glXMakeCurrent) (dpy, window, ctx);
91
92         _(glViewport) (0, 0, width, height);
93
94         set_2d_projection (width, height);
95
96 /* Simply count through some colors, frame by frame. */
97 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
98
99         int frame = 0;
100         for (i = 0; i < 2; i++) {
101                 /* Frame: Draw a solid (magenta) frame */
102                 paint_rgb_using_clear (RGB(frame));
103                 _(glXSwapBuffers) (dpy, window);
104                 frame++;
105
106                 /* Frame: Draw a solid (yellow) frame */
107                 paint_rgb_using_clear (RGB(frame));
108                 _(glXSwapBuffers) (dpy, window);
109                 frame++;
110
111                 /* Frame: Draw a solid (cyan) frame */
112                 paint_rgb_using_clear (RGB(frame));
113                 _(glXSwapBuffers) (dpy, window);
114                 frame++;
115         }
116
117         /* Cleanup */
118         _(glXDestroyContext) (dpy, ctx);
119 }
120
121 static void
122 common_handle_events(Display *dpy, GLXContext ctx, Window window)
123 {
124         XEvent xev;
125         int width = 0;
126         int height = 0;
127
128         XNextEvent (dpy, &xev);
129
130         while (1) {
131                 XNextEvent (dpy, &xev);
132                 switch (xev.type) {
133                 case ConfigureNotify:
134                         width = xev.xconfigure.width;
135                         height = xev.xconfigure.height;
136                         break;
137                 case Expose:
138                         if (xev.xexpose.count == 0) {
139                                 draw (dpy, ctx, window, width, height);
140                                 return;
141                         }
142                         break;
143                 }
144         }
145 }