]> icculus.org git repositories - btb/d2x.git/blob - video/ogl_glx.c
OpenGL now uses SDL-GL instead of gii
[btb/d2x.git] / video / ogl_glx.c
1 //opengl platform specific functions for GLX - Added 9/15/99 Matthew Mueller
2 #include <conf.h>
3 #ifdef GLX_GL_VIDEO
4
5 #include <X11/Xlib.h>
6 #include <GL/glx.h>
7 #include <string.h>
8 #include "u_mem.h"
9 #include "ogl_init.h"
10 #include "vers_id.h"
11 #include "error.h"
12 #include "event.h"
13 #include "mono.h"
14 #ifdef XFREE86_DGA
15 #include <X11/extensions/xf86dga.h>
16 #include <X11/extensions/xf86vmode.h>
17 #endif
18    
19
20 #include <X11/Xatom.h>
21
22 //#define HAVE_MOTIF
23 #ifdef HAVE_MOTIF
24
25 #include <X11/Xm/MwmUtil.h>
26
27 #else
28
29 /* bit definitions for MwmHints.flags */
30 #define MWM_HINTS_FUNCTIONS (1L << 0)
31 #define MWM_HINTS_DECORATIONS (1L << 1)
32 #define MWM_HINTS_INPUT_MODE (1L << 2)
33 #define MWM_HINTS_STATUS (1L << 3)
34
35 /* bit definitions for MwmHints.functions */
36 #define MWM_FUNC_ALL            (1L << 0)
37 #define MWM_FUNC_RESIZE         (1L << 1)
38 #define MWM_FUNC_MOVE           (1L << 2)
39 #define MWM_FUNC_MINIMIZE       (1L << 3)
40 #define MWM_FUNC_MAXIMIZE       (1L << 4)
41 #define MWM_FUNC_CLOSE          (1L << 5)
42
43
44 /* bit definitions for MwmHints.decorations */
45 #define MWM_DECOR_ALL  (1L << 0)
46 #define MWM_DECOR_BORDER (1L << 1)
47 #define MWM_DECOR_RESIZEH (1L << 2)
48 #define MWM_DECOR_TITLE  (1L << 3)
49 #define MWM_DECOR_MENU  (1L << 4)
50 #define MWM_DECOR_MINIMIZE (1L << 5)
51 #define MWM_DECOR_MAXIMIZE (1L << 6)
52
53 typedef struct
54 {
55         unsigned long flags;
56         unsigned long functions;
57         unsigned long decorations;
58         long          inputMode;
59         unsigned long status;
60 } PropMotifWmHints;
61
62 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
63
64 #endif
65
66
67
68 /*
69  * Specify which Motif window manager border decorations to put on a * top-level window.  For example, you can specify that
70  a window is not * resizabe, or omit the titlebar, or completely remove all decorations. * Input:  dpy - the X display
71  *        w - the X window
72  *        flags - bitwise-OR of the MWM_DECOR_xxx symbols in
73  X11/Xm/MwmUtil.h
74  *                indicating what decoration elements to enable.  Zero would
75  *                be no decoration.
76  */
77 void set_mwm_border( Display *dpy, Window w, unsigned long dflags,unsigned long fflags ) {
78         PropMotifWmHints motif_hints;
79         Atom prop, proptype;
80
81         /* setup the property */
82         motif_hints.flags = MWM_HINTS_DECORATIONS|MWM_HINTS_FUNCTIONS;
83         motif_hints.decorations = dflags;
84         motif_hints.functions = fflags;
85
86         /* get the atom for the property */
87         prop = XInternAtom( dpy, "_MOTIF_WM_HINTS", True );   if (!prop) {
88                 mprintf((0,"set_mwm_border: prop==0\n"));
89                 /* something went wrong! */
90                 return;
91         }
92
93         /* not sure this is correct, seems to work, XA_WM_HINTS didn't work */   proptype = prop;
94
95         XChangeProperty( dpy, w, /* display, window */ prop, proptype, /* property, type */ 
96                         32, /* format: 32-bit datums */ PropModeReplace, /* mode */
97                         (unsigned char *) &motif_hints, /* data */ PROP_MOTIF_WM_HINTS_ELEMENTS /* nelements */);
98 }
99
100 int glx_erbase,glx_evbase;
101 Display *dpy;
102 XSetWindowAttributes swa;
103 Window win;
104 XVisualInfo *visinfo;
105 GLXContext glxcontext;
106 Pixmap blankpixmap=None;
107 Cursor blankcursor=None;
108
109
110 void set_wm_hints(int fullscreen){
111         XSizeHints *hints=NULL;
112                 
113         
114 //      return;//seems screwed.
115         if (!hints) hints=XAllocSizeHints();
116         hints->width=hints->min_width=hints->max_width=hints->base_width=grd_curscreen->sc_w;
117         hints->height=hints->min_height=hints->max_height=hints->base_height=grd_curscreen->sc_h;
118         hints->flags=PSize|PMinSize|PMaxSize|PBaseSize;
119 //      hints->min_width=hints->max_width=grd_curscreen->sc_w;
120 //      hints->min_height=hints->max_height=grd_curscreen->sc_h;
121 //      hints->flags=PMinSize|PMaxSize;
122         XSetWMNormalHints(dpy,win,hints);
123         XFree(hints);
124         if (fullscreen){
125                 set_mwm_border(dpy,win,0,0);
126         }else{
127                 set_mwm_border(dpy,win,MWM_DECOR_TITLE|MWM_DECOR_BORDER,MWM_FUNC_MOVE|MWM_FUNC_CLOSE);
128         }
129 }
130
131 static int attribs[]={GLX_RGBA,GLX_DOUBLEBUFFER,GLX_DEPTH_SIZE,0,GLX_STENCIL_SIZE,0,
132         GLX_ACCUM_RED_SIZE,0,GLX_ACCUM_GREEN_SIZE,0,GLX_ACCUM_BLUE_SIZE,0,GLX_ACCUM_ALPHA_SIZE,0,None};
133
134 void ogl_do_fullscreen_internal(void){
135 //      ogl_smash_texture_list_internal();//not needed
136         if (ogl_fullscreen){
137                 set_wm_hints(1);
138                 XMoveWindow(dpy,win,0,0);
139                 //                      XDefineCursor(dpy,win,blankcursor);
140                 //XGrabPointer(dpy,win,0,swa.event_mask,GrabModeAsync,GrabModeAsync,win,blankcursor,CurrentTime);
141                 XGrabPointer(dpy,win,1,ButtonPressMask | ButtonReleaseMask | PointerMotionMask,GrabModeAsync,GrabModeAsync,win,blankcursor,CurrentTime);
142                 //                      XGrabKeyboard(dpy,win,1,GrabModeAsync,GrabModeAsync,CurrentTime);//grabbing keyboard doesn't seem to do much good anyway.
143
144 #ifdef XFREE86_DGA
145                 //make ogl_fullscreen
146                 //can you even do this with DGA ?  just resizing with ctrl-alt-(-/+) caused X to die a horrible death.
147                 //might have to kill the window/context/whatever first?  HRm.
148 #endif
149         }else{
150                 set_wm_hints(0);
151                 //                      XUndefineCursor(dpy,win);
152                 XUngrabPointer(dpy,CurrentTime);
153                 //                      XUngrabKeyboard(dpy,CurrentTime);
154 #ifdef XFREE86_DGA
155                 //return to normal
156 #endif
157         }
158 }
159
160 inline void ogl_swap_buffers_internal(void){
161         glXSwapBuffers(dpy,win);
162 }
163 int ogl_init_window(int x, int y){
164         if (gl_initialized){
165                 XResizeWindow(dpy,win,x,y);
166         }else {
167                 glxcontext=glXCreateContext(dpy,visinfo,0,GL_TRUE);
168
169                 //create colormap
170                 swa.colormap=XCreateColormap(dpy,RootWindow(dpy,visinfo->screen),visinfo->visual,AllocNone);
171                 //create window
172                 swa.border_pixel=0;
173                 swa.event_mask=ExposureMask | StructureNotifyMask | KeyPressMask | KeyReleaseMask| ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
174                 //win = XCreateWindow(dpy,RootWindow(dpy,visinfo->screen),0,0,x,y,0,visinfo->depth,InputOutput,visinfo->visual,CWBorderPixel|CWColormap|CWEventMask,&swa);
175                 win = XCreateWindow(dpy,RootWindow(dpy,visinfo->screen),0,0,x,y,0,visinfo->depth,InputOutput,visinfo->visual,CWColormap|CWEventMask,&swa);
176         
177                 XStoreName(dpy,win,"D2X");
178 //              XStoreName(dpy,win,"agry");
179                 
180                 XMapWindow(dpy,win);
181                 
182                 glXMakeCurrent(dpy,win,glxcontext);
183                 
184                 set_wm_hints(ogl_fullscreen);
185
186                 gl_initialized=1;
187
188                 {
189                         XColor blankcolor;
190                         unsigned char *blankdata;
191                         int w,h;
192                         XQueryBestCursor(dpy,win,1,1,&w,&h);
193 //                      mprintf((0,"bestcursor %ix%i\n",w,h));
194                         blankdata=d_malloc(w*h/8);
195                         memset(blankdata,0,w*h/8);
196                         memset(&blankcolor,0,sizeof(XColor));
197                         blankpixmap=XCreateBitmapFromData(dpy,win,blankdata,w,h);
198                         blankcursor=XCreatePixmapCursor(dpy,blankpixmap,blankpixmap,&blankcolor,&blankcolor,w,h);
199                         d_free(blankdata);
200 //                      sleep(1);
201                 }
202                 
203                 if (ogl_fullscreen)
204                         ogl_do_fullscreen_internal();
205 //                      gr_do_fullscreen(ogl_fullscreen);
206         }
207 #ifdef GII_XWIN
208         init_gii_xwin(dpy,win);
209 #endif
210         return 0;
211 }
212 void ogl_destroy_window(void){
213         if (gl_initialized){
214                 glXDestroyContext(dpy,glxcontext);
215                 XDestroyWindow(dpy,win);
216                 XFreeColormap(dpy,swa.colormap);
217                 gl_initialized=0;
218         }
219         return;
220 }
221 void ogl_init(void){
222         dpy=XOpenDisplay(0);
223         if(!dpy)
224                 Error("no display\n");
225         else if (glXQueryExtension(dpy,&glx_erbase,&glx_evbase)==False)
226                 Error("no glx\n");
227         else if (!(visinfo = glXChooseVisual(dpy,DefaultScreen(dpy),attribs)))
228                 Error("no visual\n");
229 }
230 void ogl_close(void){
231         if (ogl_fullscreen){
232                 ogl_fullscreen=0;
233                 ogl_do_fullscreen_internal();
234         }
235         ogl_destroy_window();
236         if (blankcursor!=None){
237                 XFreeCursor(dpy,blankcursor);
238                 XFreePixmap(dpy,blankpixmap);
239         }
240         XCloseDisplay(dpy);
241 }
242
243 #endif //OGL