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