1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
21 #define _WM_EXPOSE_ALL
29 int xkey_to_char(uint32_t *ch
, XEvent
*event
)
35 l
= XwcLookupString(wm_data
.ic
,&event
->xkey
,buff
,4,&keysym
,NULL
);
39 kc
= XKeysymToString(keysym
);
40 if (kc
&& utf8_strlen(kc
) == 1)
41 *ch
= utf8_toutf32(kc
,-1);
47 int xevent_to_sym(sym_t
*s
, XEvent
*event
)
52 sym
= XLookupKeysym(&event
->xkey
, 0);
57 s
->type
= SYM_TYPE_MOD
;
58 s
->sym
= SYM_MOD_SHIFT
;
62 s
->type
= SYM_TYPE_MOD
;
63 s
->sym
= SYM_MOD_CTRL
;
67 s
->type
= SYM_TYPE_MOD
;
72 s
->type
= SYM_TYPE_MOD
;
73 s
->sym
= SYM_MOD_SUPER
;
77 s
->type
= SYM_TYPE_SKEY
;
78 s
->sym
= SYM_KEY_SPACE
;
81 s
->type
= SYM_TYPE_SKEY
;
82 s
->sym
= SYM_KEY_ESCAPE
;
86 s
->type
= SYM_TYPE_SKEY
;
90 s
->type
= SYM_TYPE_SKEY
;
94 s
->type
= SYM_TYPE_SKEY
;
98 s
->type
= SYM_TYPE_SKEY
;
102 s
->type
= SYM_TYPE_SKEY
;
103 s
->sym
= SYM_KEY_KP3
;
106 s
->type
= SYM_TYPE_SKEY
;
107 s
->sym
= SYM_KEY_KP4
;
110 s
->type
= SYM_TYPE_SKEY
;
111 s
->sym
= SYM_KEY_KP5
;
114 s
->type
= SYM_TYPE_SKEY
;
115 s
->sym
= SYM_KEY_KP6
;
118 s
->type
= SYM_TYPE_SKEY
;
119 s
->sym
= SYM_KEY_KP7
;
122 s
->type
= SYM_TYPE_SKEY
;
123 s
->sym
= SYM_KEY_KP8
;
126 s
->type
= SYM_TYPE_SKEY
;
127 s
->sym
= SYM_KEY_KP9
;
131 s
->type
= SYM_TYPE_SKEY
;
132 s
->sym
= SYM_KEY_LEFT
;
136 s
->type
= SYM_TYPE_SKEY
;
141 s
->type
= SYM_TYPE_SKEY
;
142 s
->sym
= SYM_KEY_RIGHT
;
146 s
->type
= SYM_TYPE_SKEY
;
147 s
->sym
= SYM_KEY_DOWN
;
151 s
->type
= SYM_TYPE_SKEY
;
156 s
->type
= SYM_TYPE_SKEY
;
161 s
->type
= SYM_TYPE_SKEY
;
166 s
->type
= SYM_TYPE_SKEY
;
170 s
->type
= SYM_TYPE_SKEY
;
174 s
->type
= SYM_TYPE_SKEY
;
178 s
->type
= SYM_TYPE_SKEY
;
182 s
->type
= SYM_TYPE_SKEY
;
186 s
->type
= SYM_TYPE_SKEY
;
190 s
->type
= SYM_TYPE_SKEY
;
191 s
->sym
= SYM_KEY_F10
;
194 s
->type
= SYM_TYPE_SKEY
;
195 s
->sym
= SYM_KEY_F11
;
198 s
->type
= SYM_TYPE_SKEY
;
199 s
->sym
= SYM_KEY_F12
;
202 s
->type
= SYM_TYPE_SKEY
;
203 s
->sym
= SYM_KEY_BCKSPC
;
207 s
->type
= SYM_TYPE_SKEY
;
208 s
->sym
= SYM_KEY_RETURN
;
212 s
->type
= SYM_TYPE_SKEY
;
213 s
->sym
= SYM_KEY_DELETE
;
216 s
->type
= SYM_TYPE_SKEY
;
217 s
->sym
= SYM_KEY_KPEQ
;
220 s
->type
= SYM_TYPE_SKEY
;
221 s
->sym
= SYM_KEY_KPMUL
;
224 s
->type
= SYM_TYPE_SKEY
;
225 s
->sym
= SYM_KEY_KPADD
;
228 s
->type
= SYM_TYPE_SKEY
;
229 s
->sym
= SYM_KEY_KPSUB
;
232 s
->type
= SYM_TYPE_SKEY
;
233 s
->sym
= SYM_KEY_KPDOT
;
236 s
->type
= SYM_TYPE_SKEY
;
237 s
->sym
= SYM_KEY_KPDIV
;
239 /* stuff we don't care about */
247 /* case XK_KP_Prior: same as non-kp
248 case XK_Page_Up: same as Prior
249 case XK_KP_Page_Up: same as non-kp */
251 /* case XK_KP_Next: same as non-kp
252 case XK_Page_Down: same as Next
253 case XK_KP_Page_Down: same as non-kp */
255 /* case XK_script_switch: same as Mode_switch */
276 s
->type
= SYM_TYPE_KEY
;
277 kc
= XKeysymToString(sym
);
278 if (kc
&& utf8_strlen(kc
) == 1) {
279 s
->sym
= utf8_toutf32(kc
,-1);
280 if (!iswgraph(s
->sym
))
289 l
= XLookupString(&event
->xkey
, buff
, 5, &keysym
, NULL
);
290 if (l
>0 && utf8_strlen(buff
) == 1)
291 s
->sym
= utf8_toutf32(buff
,-1);
297 /* read and act on events */
303 while (XPending(wm_data
.dpy
) > 0) {
304 XNextEvent(wm_data
.dpy
, &event
);
305 if (XFilterEvent(&event
,None
) == True
)
308 e
.sym
.type
= SYM_TYPE_NONE
;
311 switch (event
.type
) {
313 if (event
.xexpose
.count
!= 0)
316 case ConfigureNotify
:
317 if ((event
.xconfigure
.width
!= wm_data
.size
.width
) || (event
.xconfigure
.height
!= wm_data
.size
.height
)) {
318 wm_data
.size
.width
= event
.xconfigure
.width
;
319 wm_data
.size
.height
= event
.xconfigure
.height
;
324 e
.type
= EVENT_BUTTON_DOWN
;
325 e
.sym
.type
= SYM_TYPE_MOUSE
;
326 if (event
.xbutton
.button
== 1) {
327 e
.sym
.sym
= MOUSE_BUTTON_LEFT
;
329 }else if (event
.xbutton
.button
== 2) {
330 e
.sym
.sym
= MOUSE_BUTTON_CENTRE
;
332 }else if (event
.xbutton
.button
== 3) {
333 e
.sym
.sym
= MOUSE_BUTTON_RIGHT
;
335 }else if (event
.xbutton
.button
== 4) {
336 e
.sym
.sym
= MOUSE_BUTTON_UP
;
338 }else if (event
.xbutton
.button
== 5) {
339 e
.sym
.sym
= MOUSE_BUTTON_DOWN
;
344 e
.type
= EVENT_BUTTON_UP
;
345 e
.sym
.type
= SYM_TYPE_MOUSE
;
346 if (event
.xbutton
.button
== 1) {
347 e
.sym
.sym
= MOUSE_BUTTON_LEFT
;
349 }else if (event
.xbutton
.button
== 2) {
350 e
.sym
.sym
= MOUSE_BUTTON_CENTRE
;
352 }else if (event
.xbutton
.button
== 3) {
353 e
.sym
.sym
= MOUSE_BUTTON_RIGHT
;
358 e
.type
= EVENT_MOUSE_MOTION
;
359 e
.x
= event
.xmotion
.x
;
360 e
.y
= event
.xmotion
.y
;
361 events_get_mouse(rm
);
364 if (e
.rx
> -100 && e
.rx
< 100 && e
.ry
> -100 && e
.ry
< 100) {
366 if (events_get_mousegrab()) {
367 if (e
.x
< 5 || e
.x
> (wm_data
.size
.width
-5))
368 XWarpPointer(wm_data
.dpy
, None
, wm_data
.win
, 0, 0, 0, 0, wm_data
.size
.width
/2, e
.y
);
369 if (e
.y
< 5 || e
.y
> (wm_data
.size
.height
-5))
370 XWarpPointer(wm_data
.dpy
, None
, wm_data
.win
, 0, 0, 0, 0, e
.x
, wm_data
.size
.height
/2);
375 e
.type
= EVENT_KEY_DOWN
;
376 xevent_to_sym(&e
.sym
,&event
);
377 xkey_to_char(&e
.sym
.ch
,&event
);
381 utf8_fromutf32(buffs
,6,e
.sym
.sym
);
382 utf8_fromutf32(buffc
,6,e
.sym
.ch
);
383 vlprintf(CN_INFO
,"P %u %u '%s' '%s'\n",e
.sym
.sym
,e
.sym
.ch
,buffs
,buffc
);
389 if (XEventsQueued(wm_data
.dpy
, QueuedAfterReading
)) {
391 XPeekEvent(wm_data
.dpy
, &nev
);
394 && nev
.xkey
.time
== event
.xkey
.time
395 && nev
.xkey
.keycode
== event
.xkey
.keycode
400 e
.type
= EVENT_KEY_DOWN
;
401 xevent_to_sym(&e
.sym
,&event
);
405 utf8_fromutf32(buffs
,6,e
.sym
.sym
);
406 utf8_fromutf32(buffc
,6,e
.sym
.ch
);
407 vlprintf(CN_INFO
,"R %u %u '%s' '%s'\n",e
.sym
.sym
,e
.sym
.ch
,buffs
,buffc
);
413 XRefreshKeyboardMapping(&event
.xmapping
);
416 if (strstr(XGetAtomName(wm_data
.dpy
, event
.xclient
.message_type
),"WM_PROTOCOLS"))
417 client_state(VLSTATE_EXIT
);
422 events_trigger_active();