added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / fxwt / init_x.cpp
1 /*
2 This file is part of 3dengfx, realtime visualization system.
3 Copyright (C) 2004, 2005, 2006 John Tsiombikas <nuclear@siggraph.org>
4
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.
9
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.
14
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
18 */
19
20 /* OpenGL through GLX (X Window System)
21  *
22  * Author: John Tsiombikas 2005
23  */
24
25 #include "3dengfx_config.h"
26
27 #if GFX_LIBRARY == NATIVE && NATIVE_LIB == NATIVE_X11
28
29 #include <stdlib.h>
30 #include "init.hpp"
31 #include "gfx_library.h"
32 #include "3dengfx/3denginefx.hpp"
33 #include "common/err_msg.h"
34
35 #ifdef USE_XF86VIDMODE
36 #include <X11/extensions/xf86vmode.h>
37
38 static XF86VidModeModeInfo orig_mode;
39 #endif  // USE_XF86VIDMODE
40
41 Display *fxwt_x_dpy;
42 Window fxwt_x_win;
43 static GLXContext glx_ctx;
44 static bool fullscreen = false;
45
46 bool fxwt::init_graphics(GraphicsInitParameters *gparams) {
47         Display *dpy;
48         Window win;
49         info("Initializing GLX");
50
51         if(!(dpy = XOpenDisplay(0))) {
52                 error("Could not connect to the X server");
53                 return false;
54         }
55
56         int screen = DefaultScreen(dpy);
57         Window root_win = RootWindow(dpy, screen);
58
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");
60         
61         // determine color bits
62         int color_bits = 1;
63         if(!(gparams->dont_care_flags & DONT_CARE_BPP)) {
64                 switch(gparams->bpp) {
65                 case 32:
66                 case 24:
67                         color_bits = 8;
68                         break;
69
70                 case 16:
71                 case 15:
72                         color_bits = 5;
73                         break;
74
75                 case 12:
76                         color_bits = 4;
77                         break;
78
79                 default:
80                         error("%s: Tried to set unsupported pixel format: %d bpp", __func__, gparams->bpp);
81                 }
82         }
83
84         // determine stencil bits
85         int stencil_bits = gparams->stencil_bits;
86         if(gparams->dont_care_flags & DONT_CARE_STENCIL) {
87                 stencil_bits = 1;
88         }
89
90         // determine zbuffer bits
91         int zbits = gparams->depth_bits == 32 ? 24 : gparams->depth_bits;
92         if(gparams->dont_care_flags & DONT_CARE_BPP) {
93                 zbits = 1;
94         }
95         
96         int glx_attrib[] = {
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,
103                 None
104         };
105
106         XVisualInfo *vis_info;
107         if(!(vis_info = glXChooseVisual(dpy, screen, glx_attrib))) {
108                 error("%s: Could not set requested video mode", __func__);
109                 XCloseDisplay(dpy);
110                 return false;
111         }
112
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);
120
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);
125
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.
130          */
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__);
133                 XFree(vis_info);
134                 XCloseDisplay(dpy);
135                 return false;
136         }
137
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__);
143                         XFree(vis_info);
144                         XCloseDisplay(dpy);
145                         return false;
146                 }
147         }
148
149         // if we don't have DONT_CARE_STENCIL make sure we have the stencil format
150         // that was asked.
151         if(!(gparams->dont_care_flags & DONT_CARE_STENCIL) && astencilbits != gparams->stencil_bits) {
152                 error("%s: Could not set exact stencil format", __func__);
153                 XFree(vis_info);
154                 XCloseDisplay(dpy);
155                 return false;
156         }
157
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__);
161                 XFree(vis_info);
162                 XCloseDisplay(dpy);
163                 return false;
164         }
165
166         XSetWindowAttributes xattr;
167         xattr.background_pixel = xattr.border_pixel = BlackPixel(dpy, screen);
168         xattr.colormap = XCreateColormap(dpy, root_win, vis_info->visual, AllocNone);
169
170         if(gparams->fullscreen) {
171                 // TODO: also test for "XFree86-VidModeExtension"
172 #ifdef USE_XF86VIDMODE
173                 info("Using XF86VidMode extension for fullscreen resolution switch.");
174                 
175                 XF86VidModeModeInfo **modes;
176                 XF86VidModeModeInfo *vid_mode = 0;
177                 int mode_count;
178                 
179                 XF86VidModeGetAllModeLines(dpy, screen, &mode_count, &modes);
180                 orig_mode = *modes[0];
181
182                 for(int i=0; i<mode_count; i++) {
183                         if(modes[i]->hdisplay == gparams->x && modes[i]->vdisplay == gparams->y) {
184                                 vid_mode = modes[i];
185                         }
186                 }
187                 if(!vid_mode) {
188                         error("Could not set requested video mode");
189                         XFree(modes);
190                         XFree(vis_info);
191                         XCloseDisplay(dpy);
192                         return -1;
193                 }
194                 
195                 XF86VidModeSwitchToMode(dpy, screen, vid_mode);
196                 XF86VidModeSetViewPort(dpy, screen, 0, 0);
197                 XFree(modes);
198
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);
202
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);
207 #else
208                 info("Resolution switching is not compiled or not supported by the X server, using a full-screen window.");
209
210                 XWindowAttributes root_attr;
211                 XGetWindowAttributes(dpy, root_win, &root_attr);
212
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);
218
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
224
225                 fullscreen = true;
226         } else {
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);
229         }
230
231         long events = ExposureMask | StructureNotifyMask | KeyPressMask;        // expose and key events
232         events |= ButtonPressMask | ButtonReleaseMask | PointerMotionMask;      // mouse events
233         XSelectInput(dpy, win, events);
234                 
235         // set WM cooperation settings
236         Atom wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", True);
237         XSetWMProtocols(dpy, win, &wm_delete, 1);
238
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);
244
245         XClassHint class_hint;
246         class_hint.res_name = "3dengfx";
247         class_hint.res_class = "3dengfx_graphics";
248         XSetClassHint(dpy, win, &class_hint);
249
250         XFree(vis_info);
251
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);
256                 XCloseDisplay(dpy);
257                 return false;
258         }
259
260         if(!glXIsDirect(dpy, glx_ctx)) {
261                 warning("using indirect rendering, which might be slow...");
262         }
263
264         XMapWindow(dpy, win);
265         XFlush(dpy);
266
267         fxwt_x_dpy = dpy;
268         fxwt_x_win = win;
269         
270         return true;
271 }
272
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);
277
278 #ifdef USE_XF86VIDMODE
279         if(fullscreen) {
280                 XF86VidModeSwitchToMode(fxwt_x_dpy, DefaultScreen(fxwt_x_dpy), &orig_mode);
281                 XF86VidModeSetViewPort(fxwt_x_dpy, DefaultScreen(fxwt_x_dpy), 0, 0);
282         }
283 #endif  // USE_XF86VIDMODE
284         
285         XCloseDisplay(fxwt_x_dpy);
286 }
287
288 #ifndef GLX_ARB_get_proc_address
289 #include <dlfcn.h>
290
291 void *glXGetProcAddress(const char *name) {
292         char *err_str;
293         void *sym;
294         void *so = dlopen("libGL.so", RTLD_LAZY);
295         if(!so) {
296                 perror("dlopen failed");
297                 return 0;
298         }
299         
300         dlerror();
301         sym = dlsym(so, name);
302         if((err_str = dlerror())) {
303                 fprintf(stderr, "dlsym failed: %s\n", err_str);
304                 sym = 0;
305         }
306         
307         dlclose(so);
308         return sym;
309 }
310 #endif
311
312 #endif  // GFX_LIBRARY == NATIVE && NATIVE_LIB == NATIVE_X11