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