-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinxlib_user.c
288 lines (235 loc) · 7.95 KB
/
inxlib_user.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
///////////////////////////////////////////////////////////////////////////
// This example shows how to build on top of the currrent framework by
// compiling everything together. Functions built by the user are all
// compiled together, and thus inclusion of other .c files is _expected_.
// This is not the best practice, but it is the quickest and dirtiest.
// IN 2016/06/29
// A modification and an addition were made to make this demonstration
// able to use an external "library" that renders widgets. In order to
// make the library's variables accessible we introduced an external
// function that accesses global variables.
// IN 2016/07/04
// A second external function was added. This function does a "configure"
// to the widgets that are being used.
// IN 2016/07/30
///////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "inxlib.h"
//
// Including this external file allows it to be compiled together with this
// example. This makes the function-pointer assignment really simple, because
// it can otherwise be a real burden for the user to achieve.
//
#include "app.c" // injects a whole lot of code here...
#ifdef _DEBUG_FONT_
static int global_cursor_x=0, global_cursor_y=0, do_draw_cursor=0;
#endif
//
// Generic function to be called for drawing
// (The only reason this function is introduced is so that the appropriate
// arguments are passed to the actual drawing function when it is invoked from
// the function pointer in the main loop.)
//
int user_draw( struct my_xwin_vars *xvars, void *data )
{
#ifdef _NO_GLX_WIN_
glXMakeCurrent( xvars->xdisplay, xvars->xwindow, xvars->glxc );
#else
glXMakeContextCurrent( xvars->xdisplay, xvars->glxwin,
xvars->glxwin, xvars->glxc );
#endif
#ifdef _CASE1_
// DEMO 1
// We ignore all user data for this demo (no use of the "void *data" pointer)
// we call a function of our choosing, and in this case we have compiled the
// function in this file (with all its dependencies above...)
app_draw( 0, 0,
xvars->win_width, xvars->win_height,
xvars->font_base, xvars->xdisplay, xvars->xwindow );
#endif
#ifdef _CASE2_
// DEMO 2
// We use a C++ framework to do all of our drawing
// we call the drawing frunction from the first case to draw a background
app_draw( 0, 0,
xvars->win_width, xvars->win_height,
xvars->font_base, xvars->xdisplay, xvars->xwindow );
// we pass the pointers given to us and sit back and wait...
(void) ingl_widgets_draw( xvars, data );
#endif
#ifdef _CASE3_
// DEMO 3
// this is like "case 1" but with two threads interacting
app_draw( 0, 0,
xvars->win_width, xvars->win_height,
xvars->font_base, xvars->xdisplay, xvars->xwindow );
#endif
#ifdef _DEBUG_FONT_
if( do_draw_cursor ) {
glColor3f( 0.2, 1.0, 0.2 );
glWindowPos2i( global_cursor_x, xvars->win_height - global_cursor_y );
glListBase( xvars->font_base );
glCallLists( (GLsizei) strlen( "CURSOR" ), GL_UNSIGNED_BYTE, "CURSOR" );
do_draw_cursor=0;
}
#endif
// swap the buffers to the frame we just rendered
// (make sure the current drawble's buffers are swapped!)
#ifdef _NO_GLX_WIN_
glXSwapBuffers( xvars->xdisplay, xvars->xwindow );
#else
glXSwapBuffers( xvars->xdisplay, xvars->glxwin );
#endif
return 0;
}
//
// Generic function to be called when the window is "configured"
// (The only reason this function is introduced is so that the appropriate
// arguments are passed to the actual configure function when it is invoked
// from the function pointer in the main loop.)
//
int user_configure( struct my_xwin_vars *xvars, XEvent *event )
{
XConfigureEvent xce = event->xconfigure;
xvars->win_width = xce.width;
xvars->win_height = xce.height;
#ifdef _CASE2_
// we pass the pointers given to us and sit back and wait...
(void) ingl_widgets_root_configure( xvars, (void *) event );
#endif
return 0;
}
//
// Generic function to be called when we trap and handle a KeyPress event
// (The only reason this function is introduced is so that the appropriate
// arguments are passed to the actual C++ function when it is invoked from
// the function pointer in the main loop.)
//
int user_keypress( struct my_xwin_vars *xvars, XEvent *event )
{
#ifdef _CASE2_
// we pass the pointers given to us and sit back and wait...
(void) ingl_events_handle_keypress( xvars, (void *) event );
#endif
return 0;
}
//
// Generic function to be called when we trap and handle a KeyRelease event
// (Works similarly to the keypress function.)
//
int user_keyrelease( struct my_xwin_vars *xvars, XEvent *event )
{
#ifdef _CASE2_
// we pass the pointers given to us and sit back and wait...
(void) ingl_events_handle_keyrelease( xvars, (void *) event );
#endif
return 0;
}
//
// Generic function to be called when we trap and handle a ButtonPress event
//
int user_buttonpress( struct my_xwin_vars *xvars, XEvent *event )
{
#ifdef _CASE2_
// we pass the pointers given to us and sit back and wait...
(void) ingl_events_handle_buttonpress( xvars, (void *) event );
#endif
return 0;
}
//
// Generic function to be called when we trap and handle a ButtonRelease event
//
int user_buttonrelease( struct my_xwin_vars *xvars, XEvent *event )
{
#ifdef _CASE2_
// we pass the pointers given to us and sit back and wait...
(void) ingl_events_handle_buttonrelease( xvars, (void *) event );
#endif
return 0;
}
//
// Generic function to be called when we trap and handle a MotionNotify event
//
int user_motionnotify( struct my_xwin_vars *xvars, XEvent *event )
{
#ifdef _CASE2_
// we pass the pointers given to us and sit back and wait...
(void) ingl_events_handle_motionnotify( xvars, (void *) event );
#endif
#ifdef _DEBUG_FONT_
XButtonEvent *ep = (XButtonEvent *) event;
printf("X: %d Y: %d \n", ep->x, ep->y );
global_cursor_x = ep->x + 20;
global_cursor_y = ep->y - 20;
do_draw_cursor=1;
#endif
return 0;
}
//
// Generic function to be called when we trap and handle a FocusChange event
//
int user_focuschange( struct my_xwin_vars *xvars, XEvent *event )
{
#ifdef _CASE2_
(void) ingl_events_handle_focuschange( xvars, (void *) event );
#endif
return 0;
}
//
// Function to specify callbacks (user must build this function)
// NOTE: No OpenGL related setup should be done here, as this function is
// called _before_ the OpenGL rendering context is created!
//
int xwindow_user( struct my_xwin_vars *xvars )
{
xvars->callback_FrameEntry = NULL;
xvars->callback_FrameExit = NULL;
xvars->callback_Expose = NULL;
xvars->callback_ConfigureNotify = NULL;
xvars->callback_MapNotify = NULL;
xvars->callback_KeyPress = NULL;
xvars->callback_KeyRelease = NULL;
xvars->callback_ButtonPress = NULL;
xvars->callback_ButtonRelease = NULL;
xvars->callback_MotionNotify = NULL;
xvars->callback_EnterNotify = NULL;
xvars->callback_FocusIn = NULL;
xvars->callback_FocusOut = NULL;
//
// specify the drawing function
//
xvars->callback_DrawScreen = user_draw;
//
// specify the window-configure function
//
xvars->callback_ConfigureNotify = user_configure;
//
// specify a key-press handling function
//
xvars->callback_KeyPress = user_keypress;
//
// specify a key-release handling function
//
xvars->callback_KeyRelease = user_keyrelease;
//
// specify a mouse movement handling function
//
xvars->callback_MotionNotify = user_motionnotify;
//
// specify a focus-change handling function
//
xvars->callback_FocusIn = user_focuschange;
xvars->callback_FocusOut = user_focuschange;
//
// specify a button-press handling function
//
xvars->callback_ButtonPress = user_buttonpress;
//
// specify a button-release handling function
//
xvars->callback_ButtonRelease = user_buttonrelease;
// prepare any drawing (but no GL related things!)
return 0;
}