8 #include <X11/keysym.h>
20 static glut_display_func display_func
= NULL
;
21 static glut_keyboard_func keyboard_func
= NULL
;
22 static glut_reshape_func reshape_func
= NULL
;
23 static glut_idle_func idle_func
= NULL
;
26 static struct glut_window main_window
= {0,};
29 #define DEFAULT_WIN_WIDTH 400
30 #define DEFAULT_WIN_HEIGHT 300
35 void glutSwapBuffers(void)
37 glXSwapBuffers(dpy
, win
);
40 void glutInit(int *argc
, char **argv
)
42 (void)argc
; (void)argv
;
44 memset(&main_window
, 0, sizeof(main_window
));
47 void glutInitWindowSize(int width
, int height
)
49 main_window
.width
= width
;
50 main_window
.height
= height
;
53 void glutInitWindowPosition(int x
, int y
)
59 static int attributeList
[] = { GLX_RGBA
, GLX_DOUBLEBUFFER
, None
};
61 static Bool
WaitForNotify(Display
*d
, XEvent
*e
, char *arg
)
63 return (e
->type
== MapNotify
) && (e
->xmap
.window
== (Window
)arg
);
66 int glutCreateWindow(char *name
)
70 XSetWindowAttributes swa
;
77 /* get a connection */
78 dpy
= XOpenDisplay(NULL
);
80 fprintf(stderr
,"Could not open X display\n");
84 /* get an appropriate visual */
85 vi
= glXChooseVisual(dpy
, DefaultScreen(dpy
), attributeList
);
87 fprintf(stderr
, "No suitable visual for glx\n");
91 /* create a GLX context */
92 cx
= glXCreateContext(dpy
, vi
, 0, GL_TRUE
);
94 /* create a color map */
95 cmap
= XCreateColormap(dpy
, RootWindow(dpy
, vi
->screen
),
96 vi
->visual
, AllocNone
);
99 width
= main_window
.width
? : DEFAULT_WIN_WIDTH
;
100 height
= main_window
.height
? : DEFAULT_WIN_HEIGHT
;
101 main_window
.width
= width
;
102 main_window
.height
= height
;
104 hint
.x
= main_window
.x
;
105 hint
.y
= main_window
.y
;
107 hint
.height
= height
;
108 hint
.flags
= PPosition
| PSize
;
110 swa
.border_pixel
= 0;
111 swa
.event_mask
= StructureNotifyMask
;
112 win
= XCreateWindow(dpy
, RootWindow(dpy
, vi
->screen
), 0, 0, width
, height
,
113 0, vi
->depth
, InputOutput
, vi
->visual
,
114 CWBorderPixel
|CWColormap
|CWEventMask
, &swa
);
115 XSetStandardProperties (dpy
, win
, name
, name
, None
, NULL
, 0, &hint
);
117 XMapWindow(dpy
, win
);
118 XIfEvent(dpy
, &event
, WaitForNotify
, (char*)win
);
119 XSelectInput(dpy
, win
, KeyPressMask
| StructureNotifyMask
| ExposureMask
);
121 /* connect the context to the window */
122 glXMakeCurrent(dpy
, win
, cx
);
125 return 1; /* FIXME */
128 void glutDisplayFunc(void (*func
)(void))
133 void glutKeyboardFunc(void (*func
)(unsigned char key
,
136 keyboard_func
= func
;
140 void glutReshapeFunc(void (*func
)(int width
, int height
))
145 void glutIdleFunc(void (*func
)(void))
150 void glutMainLoop(void)
156 XComposeStatus status
;
159 fprintf(stderr
, "No reshape_func set!\n");
162 reshape_func(main_window
.width
, main_window
.height
);
168 if (XPending(dpy
) > 0) {
169 XNextEvent(dpy
,&xev
);
172 XLookupString((XKeyEvent
*)&xev
,buf
,80,&keysym
,&status
);
193 keyboard_func(k
, 0, 0);
195 case ConfigureNotify
:
198 width
= xev
.xconfigure
.width
;
199 height
= xev
.xconfigure
.height
;
201 reshape_func(width
, height
);