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