• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2009, 2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <X11/Xlib.h>
27 #include "glx_common.h"
28 
29 Display *
get_display_or_skip(void)30 get_display_or_skip(void)
31 {
32 	Display *dpy = XOpenDisplay(NULL);
33 
34 	if (!dpy) {
35 		fputs("couldn't open display\n", stderr);
36 		exit(77);
37 	}
38 
39 	return dpy;
40 }
41 
42 XVisualInfo *
get_glx_visual(Display * dpy)43 get_glx_visual(Display *dpy)
44 {
45 	XVisualInfo *visinfo;
46 	int attrib[] = {
47 		GLX_RGBA,
48 		GLX_RED_SIZE, 1,
49 		GLX_GREEN_SIZE, 1,
50 		GLX_BLUE_SIZE, 1,
51 		GLX_DOUBLEBUFFER,
52 		None
53 	};
54 	int screen = DefaultScreen(dpy);
55 
56 	visinfo = glXChooseVisual(dpy, screen, attrib);
57 	if (visinfo == NULL) {
58 		fputs("Couldn't get an RGBA, double-buffered visual\n", stderr);
59 		exit(1);
60 	}
61 
62 	return visinfo;
63 }
64 
65 Window
get_glx_window(Display * dpy,XVisualInfo * visinfo,bool map)66 get_glx_window(Display *dpy, XVisualInfo *visinfo, bool map)
67 {
68 	XSetWindowAttributes window_attr;
69 	unsigned long mask;
70 	int screen = DefaultScreen(dpy);
71 	Window root_win = RootWindow(dpy, screen);
72 	Window win;
73 
74 	window_attr.background_pixel = 0;
75 	window_attr.border_pixel = 0;
76 	window_attr.colormap = XCreateColormap(dpy, root_win,
77 					       visinfo->visual, AllocNone);
78 	window_attr.event_mask = StructureNotifyMask | ExposureMask |
79 		KeyPressMask;
80 	mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
81 	win = XCreateWindow(dpy, root_win, 0, 0,
82 			    10, 10, /* width, height */
83 			    0, visinfo->depth, InputOutput,
84 			    visinfo->visual, mask, &window_attr);
85 
86 	return win;
87 }
88 
89 void
make_glx_context_current_or_skip(Display * dpy)90 make_glx_context_current_or_skip(Display *dpy)
91 {
92 	GLXContext ctx;
93 	XVisualInfo *visinfo = get_glx_visual(dpy);
94 	Window win = get_glx_window(dpy, visinfo, false);
95 
96 	ctx = glXCreateContext(dpy, visinfo, False, True);
97 	if (ctx == None) {
98 		fputs("glXCreateContext failed\n", stderr);
99 		exit(1);
100 	}
101 
102 	glXMakeCurrent(dpy, win, ctx);
103 }
104 
105 GLXFBConfig
get_fbconfig_for_visinfo(Display * dpy,XVisualInfo * visinfo)106 get_fbconfig_for_visinfo(Display *dpy, XVisualInfo *visinfo)
107 {
108 	int i, nconfigs;
109 	GLXFBConfig ret = None, *configs;
110 
111 	configs = glXGetFBConfigs(dpy, visinfo->screen, &nconfigs);
112 	if (!configs)
113 		return None;
114 
115 	for (i = 0; i < nconfigs; i++) {
116 		int v;
117 
118 		if (glXGetFBConfigAttrib(dpy, configs[i], GLX_VISUAL_ID, &v))
119 			continue;
120 
121 		if (v == visinfo->visualid) {
122 			ret = configs[i];
123 			break;
124 		}
125 	}
126 
127 	XFree(configs);
128 	return ret;
129 }
130