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