Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/post/src/tk/event.c
3203 views
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <X11/keysym.h>
4
#include "tk.h"
5
#include "private.h"
6
7
/******************************************************************************/
8
9
void (*ExposeFunc)(int, int) = 0;
10
void (*ReshapeFunc)(int, int) = 0;
11
void (*DisplayFunc)(void) = 0;
12
GLenum (*KeyDownFunc)(int, GLenum) = 0;
13
GLenum (*MouseDownFunc)(int, int, GLenum) = 0;
14
GLenum (*MouseUpFunc)(int, int, GLenum) = 0;
15
GLenum (*MouseMoveFunc)(int, int, GLenum) = 0;
16
void (*IdleFunc)(void) = 0;
17
int lastEventType = -1;
18
GLenum drawAllowFlag;
19
20
/******************************************************************************/
21
22
static GLenum DoNextEvent(void)
23
{
24
XEvent current, ahead;
25
char buf[1000];
26
KeySym ks;
27
int key;
28
29
XNextEvent(xDisplay, &current);
30
switch (current.type) {
31
case MappingNotify:
32
XRefreshKeyboardMapping((XMappingEvent *)&current);
33
lastEventType = MappingNotify;
34
return GL_FALSE;
35
36
case MapNotify:
37
lastEventType = MapNotify;
38
drawAllowFlag = GL_TRUE;
39
return GL_FALSE;
40
41
case UnmapNotify:
42
lastEventType = UnmapNotify;
43
drawAllowFlag = GL_FALSE;
44
return GL_FALSE;
45
46
case ClientMessage:
47
lastEventType = ClientMessage;
48
if (current.xclient.data.l[0] == deleteWindowAtom) {
49
exit(0);
50
}
51
return GL_FALSE;
52
53
case Expose:
54
while (XEventsQueued(current.xexpose.display, QueuedAfterReading) > 0) {
55
XPeekEvent(current.xexpose.display, &ahead);
56
if (ahead.xexpose.window != current.xexpose.window ||
57
ahead.type != Expose) {
58
break;
59
}
60
XNextEvent(xDisplay, &current);
61
}
62
if (current.xexpose.count == 0) {
63
if (ExposeFunc) {
64
(*ExposeFunc)(w.w, w.h);
65
if (lastEventType == ConfigureNotify &&
66
drawAllowFlag == GL_TRUE) {
67
lastEventType = Expose;
68
return GL_FALSE;
69
} else {
70
lastEventType = Expose;
71
drawAllowFlag = GL_TRUE;
72
return GL_TRUE;
73
}
74
}
75
}
76
return GL_FALSE;
77
78
case ConfigureNotify:
79
lastEventType = ConfigureNotify;
80
w.w = current.xconfigure.width;
81
w.h = current.xconfigure.height;
82
if (TK_HAS_OVERLAY(w.type)) {
83
XResizeWindow(xDisplay, w.wOverlay, w.w, w.h);
84
}
85
if (ReshapeFunc) {
86
(*ReshapeFunc)(w.w, w.h);
87
return GL_TRUE;
88
} else {
89
return GL_FALSE;
90
}
91
92
case MotionNotify:
93
lastEventType = MotionNotify;
94
if (MouseMoveFunc) {
95
GLenum mask;
96
97
mask = 0;
98
if (current.xmotion.state & Button1Mask) {
99
mask |= TK_LEFTBUTTON;
100
}
101
if (current.xmotion.state & Button2Mask) {
102
mask |= TK_MIDDLEBUTTON;
103
}
104
if (current.xmotion.state & Button3Mask) {
105
mask |= TK_RIGHTBUTTON;
106
}
107
return (*MouseMoveFunc)(current.xmotion.x, current.xmotion.y, mask);
108
} else {
109
return GL_FALSE;
110
}
111
112
case ButtonPress:
113
lastEventType = ButtonPress;
114
if (MouseDownFunc) {
115
GLenum mask;
116
117
mask = 0;
118
if (current.xbutton.button == 1) {
119
mask |= TK_LEFTBUTTON;
120
}
121
if (current.xbutton.button == 2) {
122
mask |= TK_MIDDLEBUTTON;
123
}
124
if (current.xbutton.button == 3) {
125
mask |= TK_RIGHTBUTTON;
126
}
127
return (*MouseDownFunc)(current.xbutton.x, current.xbutton.y, mask);
128
} else {
129
return GL_FALSE;
130
}
131
case ButtonRelease:
132
lastEventType = ButtonRelease;
133
if (MouseUpFunc) {
134
GLenum mask;
135
136
mask = 0;
137
if (current.xbutton.button == 1) {
138
mask |= TK_LEFTBUTTON;
139
}
140
if (current.xbutton.button == 2) {
141
mask |= TK_MIDDLEBUTTON;
142
}
143
if (current.xbutton.button == 3) {
144
mask |= TK_RIGHTBUTTON;
145
}
146
return (*MouseUpFunc)(current.xbutton.x, current.xbutton.y, mask);
147
} else {
148
return GL_FALSE;
149
}
150
151
case KeyPress:
152
lastEventType = KeyPress;
153
XLookupString(&current.xkey, buf, sizeof(buf), &ks, 0);
154
switch (ks) {
155
case XK_0: key = TK_0; break;
156
case XK_1: key = TK_1; break;
157
case XK_2: key = TK_2; break;
158
case XK_3: key = TK_3; break;
159
case XK_4: key = TK_4; break;
160
case XK_5: key = TK_5; break;
161
case XK_6: key = TK_6; break;
162
case XK_7: key = TK_7; break;
163
case XK_8: key = TK_8; break;
164
case XK_9: key = TK_9; break;
165
case XK_A: key = TK_A; break;
166
case XK_B: key = TK_B; break;
167
case XK_C: key = TK_C; break;
168
case XK_D: key = TK_D; break;
169
case XK_E: key = TK_E; break;
170
case XK_F: key = TK_F; break;
171
case XK_G: key = TK_G; break;
172
case XK_H: key = TK_H; break;
173
case XK_I: key = TK_I; break;
174
case XK_J: key = TK_J; break;
175
case XK_K: key = TK_K; break;
176
case XK_L: key = TK_L; break;
177
case XK_M: key = TK_M; break;
178
case XK_N: key = TK_N; break;
179
case XK_O: key = TK_O; break;
180
case XK_P: key = TK_P; break;
181
case XK_Q: key = TK_Q; break;
182
case XK_R: key = TK_R; break;
183
case XK_S: key = TK_S; break;
184
case XK_T: key = TK_T; break;
185
case XK_U: key = TK_U; break;
186
case XK_V: key = TK_V; break;
187
case XK_W: key = TK_W; break;
188
case XK_X: key = TK_X; break;
189
case XK_Y: key = TK_Y; break;
190
case XK_Z: key = TK_Z; break;
191
case XK_a: key = TK_a; break;
192
case XK_b: key = TK_b; break;
193
case XK_c: key = TK_c; break;
194
case XK_d: key = TK_d; break;
195
case XK_e: key = TK_e; break;
196
case XK_f: key = TK_f; break;
197
case XK_g: key = TK_g; break;
198
case XK_h: key = TK_h; break;
199
case XK_i: key = TK_i; break;
200
case XK_j: key = TK_j; break;
201
case XK_k: key = TK_k; break;
202
case XK_l: key = TK_l; break;
203
case XK_m: key = TK_m; break;
204
case XK_n: key = TK_n; break;
205
case XK_o: key = TK_o; break;
206
case XK_p: key = TK_p; break;
207
case XK_q: key = TK_q; break;
208
case XK_r: key = TK_r; break;
209
case XK_s: key = TK_s; break;
210
case XK_t: key = TK_t; break;
211
case XK_u: key = TK_u; break;
212
case XK_v: key = TK_v; break;
213
case XK_w: key = TK_w; break;
214
case XK_x: key = TK_x; break;
215
case XK_y: key = TK_y; break;
216
case XK_z: key = TK_z; break;
217
case XK_space: key = TK_SPACE; break;
218
case XK_Return: key = TK_RETURN; break;
219
case XK_Escape: key = TK_ESCAPE; break;
220
case XK_Left: key = TK_LEFT; break;
221
case XK_Up: key = TK_UP; break;
222
case XK_Right: key = TK_RIGHT; break;
223
case XK_Down: key = TK_DOWN; break;
224
default: key = GL_FALSE; break;
225
}
226
if (key && KeyDownFunc) {
227
GLenum mask;
228
229
mask = 0;
230
if (current.xkey.state & ControlMask) {
231
mask |= TK_CONTROL;
232
}
233
if (current.xkey.state & ShiftMask) {
234
mask |= TK_SHIFT;
235
}
236
return (*KeyDownFunc)(key, mask);
237
} else {
238
return GL_FALSE;
239
}
240
}
241
return GL_FALSE;
242
}
243
244
void tkExec(int i)
245
{
246
GLenum flag;
247
248
while (GL_TRUE) {
249
if (IdleFunc) {
250
(*IdleFunc)();
251
flag = GL_TRUE;
252
while /*if*/ (XPending(xDisplay)) {
253
flag |= DoNextEvent();
254
}
255
} else {
256
flag = DoNextEvent();
257
}
258
if (drawAllowFlag && DisplayFunc && flag) {
259
(*DisplayFunc)();
260
}
261
}
262
}
263
264
/******************************************************************************/
265
266
void tkExposeFunc(void (*Func)(int, int))
267
{
268
269
ExposeFunc = Func;
270
}
271
272
/******************************************************************************/
273
274
void tkReshapeFunc(void (*Func)(int, int))
275
{
276
277
ReshapeFunc = Func;
278
}
279
280
/******************************************************************************/
281
282
void tkDisplayFunc(void (*Func)(void))
283
{
284
285
DisplayFunc = Func;
286
}
287
288
/******************************************************************************/
289
290
void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
291
{
292
293
KeyDownFunc = Func;
294
}
295
296
/******************************************************************************/
297
298
void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
299
{
300
301
MouseDownFunc = Func;
302
}
303
304
/******************************************************************************/
305
306
void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
307
{
308
309
MouseUpFunc = Func;
310
}
311
312
/******************************************************************************/
313
314
void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
315
{
316
317
MouseMoveFunc = Func;
318
}
319
320
/******************************************************************************/
321
322
void tkIdleFunc(void (*Func)(void))
323
{
324
325
IdleFunc = Func;
326
}
327
328
/******************************************************************************/
329
330