]> git.cworth.org Git - fips/blob - test/glx-dlopen-dlsym.c
test: Reduce code duplication in test-suite programs
[fips] / test / glx-dlopen-dlsym.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 /* Perform some simple drawing via OpenGL as follows:
23  *
24  *      1. Using GLX to construct an OpenGL context
25  *      2. By using dlopen to dynamically load libGL.so
26  *      3. By using dlsym to lookup OpenGL functions
27  */
28
29 #define GL_GLEXT_PROTOTYPES
30 #include <GL/gl.h>
31 #include <GL/glx.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <dlfcn.h>
36
37 #include "util.h"
38
39 void (*my_glClear) (GLbitfield);
40 void (*my_glClearColor) (GLclampf, GLclampf, GLclampf, GLclampf);
41 void (*my_glLoadIdentity) (void);
42 void (*my_glMatrixMode) (GLenum);
43 void (*my_glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble);
44 void (*my_glViewport) (GLint, GLint, GLsizei, GLsizei);
45 XVisualInfo * (*my_glXChooseVisual) (Display *, int, int *);
46 GLXContext (*my_glXCreateContext) (Display *, XVisualInfo *, GLXContext, Bool);
47 void (*my_glXDestroyContext) (Display *, GLXContext);
48 Bool (*my_glXMakeCurrent) (Display *, GLXDrawable, GLXContext);
49 void (*my_glXSwapBuffers) (Display *, GLXDrawable);
50
51 #define HANDLE_EVENTS_GL_PREFIX my_
52 #include "handle-events.c"
53
54 static void
55 resolve_symbols (void)
56 {
57         void *gl_handle;
58         char *error;
59
60         gl_handle = dlopen ("libGL.so", RTLD_LAZY);
61         if (gl_handle == NULL) {
62                 fprintf (stderr, "Error: Failed to open libGL.so\n");
63                 exit (1);
64         }
65
66         /* Clear errors. */
67         dlerror();
68
69         my_glClear = dlsym (gl_handle, "glClear");
70         error = dlerror ();
71         if (error) {
72                 fprintf (stderr, "Failed to dlsym glClear: %s\n", error);
73                 exit (1);
74         }
75
76         my_glClearColor = dlsym (gl_handle, "glClearColor");
77         error = dlerror ();
78         if (error) {
79                 fprintf (stderr, "Failed to dlsym glClearColor: %s\n", error);
80                 exit (1);
81         }
82
83         my_glLoadIdentity = dlsym (gl_handle, "glLoadIdentity");
84         error = dlerror ();
85         if (error) {
86                 fprintf (stderr, "Failed to dlsym glLoadIdentity: %s\n", error);
87                 exit (1);
88         }
89
90         my_glMatrixMode = dlsym (gl_handle, "glMatrixMode");
91         error = dlerror ();
92         if (error) {
93                 fprintf (stderr, "Failed to dlsym glMatrixMode: %s\n", error);
94                 exit (1);
95         }
96
97         my_glOrtho = dlsym (gl_handle, "glOrtho");
98         error = dlerror ();
99         if (error) {
100                 fprintf (stderr, "Failed to dlsym glOrtho: %s\n", error);
101                 exit (1);
102         }
103
104         my_glViewport = dlsym (gl_handle, "glViewport");
105         error = dlerror ();
106         if (error) {
107                 fprintf (stderr, "Failed to dlsym glViewport: %s\n", error);
108                 exit (1);
109         }
110
111         my_glXChooseVisual = dlsym (gl_handle, "glXChooseVisual");
112         error = dlerror ();
113         if (error) {
114                 fprintf (stderr, "Failed to dlsym glXChooseVisual: %s\n", error);
115                 exit (1);
116         }
117
118         my_glXCreateContext = dlsym (gl_handle, "glXCreateContext");
119         error = dlerror ();
120         if (error) {
121                 fprintf (stderr, "Failed to dlsym glXCreateContext: %s\n", error);
122                 exit (1);
123         }
124
125         my_glXDestroyContext = dlsym (gl_handle, "glXDestroyContext");
126         error = dlerror ();
127         if (error) {
128                 fprintf (stderr, "Failed to dlsym glXDestroyContext: %s\n", error);
129                 exit (1);
130         }
131
132         my_glXMakeCurrent = dlsym (gl_handle, "glXMakeCurrent");
133         error = dlerror ();
134         if (error) {
135                 fprintf (stderr, "Failed to dlsym glXMakeCurrent: %s\n", error);
136                 exit (1);
137         }
138
139         my_glXSwapBuffers = dlsym (gl_handle, "glXSwapBuffers");
140         error = dlerror ();
141         if (error) {
142                 fprintf (stderr, "Failed to dlsym glXSwapBuffers: %s\n", error);
143                 exit (1);
144         }
145 }
146
147 int
148 main (void)
149 {
150         Display *dpy;
151         Window window;
152
153         util_init_display_window (&dpy, &window);
154
155         resolve_symbols ();
156
157         handle_events (dpy, window);
158
159         util_fini_display_window (dpy, window);
160
161         return 0;
162 }