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