]> icculus.org git repositories - btb/d2x.git/blob - arch/ogl/glx.c
remove rcs tags
[btb/d2x.git] / arch / ogl / glx.c
1 /*
2  *
3  * opengl platform specific functions for GLX - Added 9/15/99 Matthew Mueller
4  *
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <conf.h>
10 #endif
11
12 #include <X11/Xlib.h>
13 #include <GL/glx.h>
14 #include <string.h>
15 #include "internal.h"
16 #include "vers_id.h"
17 #include "error.h"
18 #include "event.h"
19 #include "mono.h"
20 #include "u_mem.h"
21 #ifdef XFREE86_DGA
22 #include <X11/extensions/xf86dga.h>
23 #include <X11/extensions/xf86vmode.h>
24 #endif
25
26
27 #include <X11/Xatom.h>
28
29 //#define HAVE_MOTIF
30 #ifdef HAVE_MOTIF
31
32 #include <X11/Xm/MwmUtil.h>
33
34 #else
35
36 /* bit definitions for MwmHints.flags */
37 #define MWM_HINTS_FUNCTIONS (1L << 0)
38 #define MWM_HINTS_DECORATIONS (1L << 1)
39 #define MWM_HINTS_INPUT_MODE (1L << 2)
40 #define MWM_HINTS_STATUS (1L << 3)
41
42 /* bit definitions for MwmHints.functions */
43 #define MWM_FUNC_ALL            (1L << 0)
44 #define MWM_FUNC_RESIZE         (1L << 1)
45 #define MWM_FUNC_MOVE           (1L << 2)
46 #define MWM_FUNC_MINIMIZE       (1L << 3)
47 #define MWM_FUNC_MAXIMIZE       (1L << 4)
48 #define MWM_FUNC_CLOSE          (1L << 5)
49
50
51 /* bit definitions for MwmHints.decorations */
52 #define MWM_DECOR_ALL  (1L << 0)
53 #define MWM_DECOR_BORDER (1L << 1)
54 #define MWM_DECOR_RESIZEH (1L << 2)
55 #define MWM_DECOR_TITLE  (1L << 3)
56 #define MWM_DECOR_MENU  (1L << 4)
57 #define MWM_DECOR_MINIMIZE (1L << 5)
58 #define MWM_DECOR_MAXIMIZE (1L << 6)
59
60 typedef struct
61 {
62         unsigned long flags;
63         unsigned long functions;
64         unsigned long decorations;
65         long          inputMode;
66         unsigned long status;
67 } PropMotifWmHints;
68
69 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
70
71 #endif
72
73
74
75 /*
76  * Specify which Motif window manager border decorations to put on a * top-level window.  For example, you can specify that
77  a window is not * resizabe, or omit the titlebar, or completely remove all decorations. * Input:  dpy - the X display
78  *        w - the X window
79  *        flags - bitwise-OR of the MWM_DECOR_xxx symbols in
80  X11/Xm/MwmUtil.h
81  *                indicating what decoration elements to enable.  Zero would
82  *                be no decoration.
83  */
84 void set_mwm_border( Display *dpy, Window w, unsigned long dflags,unsigned long fflags ) {
85         PropMotifWmHints motif_hints;
86         Atom prop, proptype;
87
88         /* setup the property */
89         motif_hints.flags = MWM_HINTS_DECORATIONS|MWM_HINTS_FUNCTIONS;
90         motif_hints.decorations = dflags;
91         motif_hints.functions = fflags;
92
93         /* get the atom for the property */
94         prop = XInternAtom( dpy, "_MOTIF_WM_HINTS", True );   if (!prop) {
95                 mprintf((0,"set_mwm_border: prop==0\n"));
96                 /* something went wrong! */
97                 return;
98         }
99
100         /* not sure this is correct, seems to work, XA_WM_HINTS didn't work */   proptype = prop;
101
102         XChangeProperty( dpy, w, /* display, window */ prop, proptype, /* property, type */ 
103                         32, /* format: 32-bit datums */ PropModeReplace, /* mode */
104                         (unsigned char *) &motif_hints, /* data */ PROP_MOTIF_WM_HINTS_ELEMENTS /* nelements */);
105 }
106
107 int glx_erbase,glx_evbase;
108 Display *dpy;
109 XSetWindowAttributes swa;
110 Window win;
111 XVisualInfo *visinfo;
112 GLXContext glxcontext;
113 Pixmap blankpixmap=None;
114 Cursor blankcursor=None;
115
116
117 void set_wm_hints(int fullscreen){
118         XSizeHints *hints=NULL;
119                 
120         
121 //      return;//seems screwed.
122         if (!hints) hints=XAllocSizeHints();
123         hints->width=hints->min_width=hints->max_width=hints->base_width=grd_curscreen->sc_w;
124         hints->height=hints->min_height=hints->max_height=hints->base_height=grd_curscreen->sc_h;
125         hints->flags=PSize|PMinSize|PMaxSize|PBaseSize;
126 //      hints->min_width=hints->max_width=grd_curscreen->sc_w;
127 //      hints->min_height=hints->max_height=grd_curscreen->sc_h;
128 //      hints->flags=PMinSize|PMaxSize;
129         XSetWMNormalHints(dpy,win,hints);
130         XFree(hints);
131         if (fullscreen){
132                 set_mwm_border(dpy,win,0,0);
133         }else{
134                 set_mwm_border(dpy,win,MWM_DECOR_TITLE|MWM_DECOR_BORDER,MWM_FUNC_MOVE|MWM_FUNC_CLOSE);
135         }
136 }
137
138 static int attribs[]={GLX_RGBA,GLX_DOUBLEBUFFER,GLX_DEPTH_SIZE,0,GLX_STENCIL_SIZE,0,
139         GLX_ACCUM_RED_SIZE,0,GLX_ACCUM_GREEN_SIZE,0,GLX_ACCUM_BLUE_SIZE,0,GLX_ACCUM_ALPHA_SIZE,0,None};
140
141 void ogl_do_fullscreen_internal(void){
142 //      ogl_smash_texture_list_internal();//not needed
143         if (ogl_fullscreen){
144                 set_wm_hints(1);
145                 XMoveWindow(dpy,win,0,0);
146                 //                      XDefineCursor(dpy,win,blankcursor);
147                 //XGrabPointer(dpy,win,0,swa.event_mask,GrabModeAsync,GrabModeAsync,win,blankcursor,CurrentTime);
148                 XGrabPointer(dpy,win,1,ButtonPressMask | ButtonReleaseMask | PointerMotionMask,GrabModeAsync,GrabModeAsync,win,blankcursor,CurrentTime);
149                 //                      XGrabKeyboard(dpy,win,1,GrabModeAsync,GrabModeAsync,CurrentTime);//grabbing keyboard doesn't seem to do much good anyway.
150
151 #ifdef XFREE86_DGA
152                 //make ogl_fullscreen
153                 //can you even do this with DGA ?  just resizing with ctrl-alt-(-/+) caused X to die a horrible death.
154                 //might have to kill the window/context/whatever first?  HRm.
155 #endif
156         }else{
157                 set_wm_hints(0);
158                 //                      XUndefineCursor(dpy,win);
159                 XUngrabPointer(dpy,CurrentTime);
160                 //                      XUngrabKeyboard(dpy,CurrentTime);
161 #ifdef XFREE86_DGA
162                 //return to normal
163 #endif
164         }
165 }
166
167 int ogl_setbrightness_internal(void)
168 {
169         return -1; // TODO: not yet implemented
170 }
171
172 inline void ogl_swap_buffers_internal(void){
173         glXSwapBuffers(dpy,win);
174 }
175 int ogl_init_window(int x, int y){
176         if (gl_initialized){
177                 XResizeWindow(dpy,win,x,y);
178                 set_wm_hints(ogl_fullscreen);
179
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,DESCENT_VERSION);
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 }