2 This file is part of 3dengfx, realtime visualization system.
3 Copyright (C) 2004, 2005, 2006 John Tsiombikas <nuclear@siggraph.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /* OpenGL through GLX (X Window System)
22 * Author: John Tsiombikas 2005
25 #include "3dengfx_config.h"
27 #if GFX_LIBRARY == NATIVE && NATIVE_LIB == NATIVE_X11
31 #include "gfx_library.h"
32 #include "3dengfx/3denginefx.hpp"
33 #include "common/err_msg.h"
35 #ifdef USE_XF86VIDMODE
36 #include <X11/extensions/xf86vmode.h>
38 static XF86VidModeModeInfo orig_mode;
39 #endif // USE_XF86VIDMODE
43 static GLXContext glx_ctx;
44 static bool fullscreen = false;
46 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
49 info("Initializing GLX");
51 if(!(dpy = XOpenDisplay(0))) {
52 error("Could not connect to the X server");
56 int screen = DefaultScreen(dpy);
57 Window root_win = RootWindow(dpy, screen);
59 info("Trying to set video mode %dx%dx%d, d:%d s:%d %s", gparams->x, gparams->y, gparams->bpp, gparams->depth_bits, gparams->stencil_bits, gparams->fullscreen ? "fullscreen" : "windowed");
61 // determine color bits
63 if(!(gparams->dont_care_flags & DONT_CARE_BPP)) {
64 switch(gparams->bpp) {
80 error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
84 // determine stencil bits
85 int stencil_bits = gparams->stencil_bits;
86 if(gparams->dont_care_flags & DONT_CARE_STENCIL) {
90 // determine zbuffer bits
91 int zbits = gparams->depth_bits == 32 ? 24 : gparams->depth_bits;
92 if(gparams->dont_care_flags & DONT_CARE_BPP) {
97 GLX_RGBA, GLX_DOUBLEBUFFER,
98 GLX_RED_SIZE, color_bits,
99 GLX_GREEN_SIZE, color_bits,
100 GLX_BLUE_SIZE, color_bits,
101 GLX_DEPTH_SIZE, zbits,
102 GLX_STENCIL_SIZE, stencil_bits,
106 XVisualInfo *vis_info;
107 if(!(vis_info = glXChooseVisual(dpy, screen, glx_attrib))) {
108 error("%s: Could not set requested video mode", __func__);
113 // check the video mode we got
114 int arbits, agbits, abbits, azbits, astencilbits;
115 glXGetConfig(dpy, vis_info, GLX_RED_SIZE, &arbits);
116 glXGetConfig(dpy, vis_info, GLX_GREEN_SIZE, &agbits);
117 glXGetConfig(dpy, vis_info, GLX_BLUE_SIZE, &abbits);
118 glXGetConfig(dpy, vis_info, GLX_DEPTH_SIZE, &azbits);
119 glXGetConfig(dpy, vis_info, GLX_STENCIL_SIZE, &astencilbits);
121 info("Initialized video mode:");
122 info(" bpp: %d (%d%d%d)", arbits + agbits + abbits, arbits, agbits, abbits);
123 info("zbuffer: %d", azbits);
124 info("stencil: %d", astencilbits);
126 /* if the dont_care_flags does not contain DONT_CARE_BPP and our color bits
127 * does not match, we should return failure, however we test against
128 * the difference allowing a +/-1 difference in order to allow for 16bpp
129 * formats of either 565 or 555 and consider them equal.
131 if(!(gparams->dont_care_flags & DONT_CARE_BPP) && abs(arbits - color_bits) > 1 && abs(agbits - color_bits) > 1 && abs(abbits - color_bits) > 1) {
132 error("%s: Could not set requested exact bpp mode", __func__);
138 // now if we don't have DONT_CARE_DEPTH in the dont_care_flags check for
139 // exact depth buffer format, however consider 24 and 32 bit the same
140 if(!(gparams->dont_care_flags & DONT_CARE_DEPTH) && azbits != zbits) {
141 if(!(zbits == 32 && azbits == 24 || zbits == 24 && azbits == 32)) {
142 error("%s: Could not set requested exact zbuffer depth", __func__);
149 // if we don't have DONT_CARE_STENCIL make sure we have the stencil format
151 if(!(gparams->dont_care_flags & DONT_CARE_STENCIL) && astencilbits != gparams->stencil_bits) {
152 error("%s: Could not set exact stencil format", __func__);
158 // everything is ok, create the context
159 if(!(glx_ctx = glXCreateContext(dpy, vis_info, 0, True))) {
160 error("%s: Failed to create GLX context", __func__);
166 XSetWindowAttributes xattr;
167 xattr.background_pixel = xattr.border_pixel = BlackPixel(dpy, screen);
168 xattr.colormap = XCreateColormap(dpy, root_win, vis_info->visual, AllocNone);
170 if(gparams->fullscreen) {
171 // TODO: also test for "XFree86-VidModeExtension"
172 #ifdef USE_XF86VIDMODE
173 info("Using XF86VidMode extension for fullscreen resolution switch.");
175 XF86VidModeModeInfo **modes;
176 XF86VidModeModeInfo *vid_mode = 0;
179 XF86VidModeGetAllModeLines(dpy, screen, &mode_count, &modes);
180 orig_mode = *modes[0];
182 for(int i=0; i<mode_count; i++) {
183 if(modes[i]->hdisplay == gparams->x && modes[i]->vdisplay == gparams->y) {
188 error("Could not set requested video mode");
195 XF86VidModeSwitchToMode(dpy, screen, vid_mode);
196 XF86VidModeSetViewPort(dpy, screen, 0, 0);
199 xattr.override_redirect = True;
200 win = XCreateWindow(dpy, root_win, 0, 0, gparams->x, gparams->y, 0, vis_info->depth,
201 InputOutput, vis_info->visual, CWColormap | CWBackPixel | CWBorderPixel | CWOverrideRedirect, &xattr);
203 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
204 XMapRaised(dpy, win);
205 XGrabKeyboard(dpy, win, True, GrabModeAsync, GrabModeAsync, CurrentTime);
206 XGrabPointer(dpy, win, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
208 info("Resolution switching is not compiled or not supported by the X server, using a full-screen window.");
210 XWindowAttributes root_attr;
211 XGetWindowAttributes(dpy, root_win, &root_attr);
213 gparams->x = root_attr.width;
214 gparams->y = root_attr.height;
215 xattr.override_redirect = True;
216 win = XCreateWindow(dpy, root_win, 0, 0, gparams->x, gparams->y, 0, vis_info->depth,
217 InputOutput, vis_info->visual, CWColormap | CWBackPixel | CWBorderPixel | CWOverrideRedirect, &xattr);
219 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
220 XMapRaised(dpy, win);
221 XGrabKeyboard(dpy, win, True, GrabModeAsync, GrabModeAsync, CurrentTime);
222 XGrabPointer(dpy, win, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
223 #endif // USE_XF86VIDMODE
227 win = XCreateWindow(dpy, root_win, 0, 0, gparams->x, gparams->y, 0, vis_info->depth,
228 InputOutput, vis_info->visual, CWColormap | CWBackPixel | CWBorderPixel, &xattr);
231 long events = ExposureMask | StructureNotifyMask | KeyPressMask; // expose and key events
232 events |= ButtonPressMask | ButtonReleaseMask | PointerMotionMask; // mouse events
233 XSelectInput(dpy, win, events);
235 // set WM cooperation settings
236 Atom wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", True);
237 XSetWMProtocols(dpy, win, &wm_delete, 1);
239 XTextProperty tp_wname;
240 static char *win_title = "3dengfx/X";
241 XStringListToTextProperty(&win_title, 1, &tp_wname);
242 XSetWMName(dpy, win, &tp_wname);
243 XFree(tp_wname.value);
245 XClassHint class_hint;
246 class_hint.res_name = "3dengfx";
247 class_hint.res_class = "3dengfx_graphics";
248 XSetClassHint(dpy, win, &class_hint);
252 if(glXMakeCurrent(dpy, win, glx_ctx) == False) {
253 error("%s: Failed to make the GLX context current", __func__);
254 glXDestroyContext(dpy, glx_ctx);
255 XDestroyWindow(dpy, win);
260 if(!glXIsDirect(dpy, glx_ctx)) {
261 warning("using indirect rendering, which might be slow...");
264 XMapWindow(dpy, win);
273 void fxwt::destroy_graphics() {
274 info("Shutting down GLX");
275 glXDestroyContext(fxwt_x_dpy, glx_ctx);
276 XDestroyWindow(fxwt_x_dpy, fxwt_x_win);
278 #ifdef USE_XF86VIDMODE
280 XF86VidModeSwitchToMode(fxwt_x_dpy, DefaultScreen(fxwt_x_dpy), &orig_mode);
281 XF86VidModeSetViewPort(fxwt_x_dpy, DefaultScreen(fxwt_x_dpy), 0, 0);
283 #endif // USE_XF86VIDMODE
285 XCloseDisplay(fxwt_x_dpy);
288 #ifndef GLX_ARB_get_proc_address
291 void *glXGetProcAddress(const char *name) {
294 void *so = dlopen("libGL.so", RTLD_LAZY);
296 perror("dlopen failed");
301 sym = dlsym(so, name);
302 if((err_str = dlerror())) {
303 fprintf(stderr, "dlsym failed: %s\n", err_str);
312 #endif // GFX_LIBRARY == NATIVE && NATIVE_LIB == NATIVE_X11