aboutsummaryrefslogtreecommitdiffstats
path: root/window_wait_event.c
blob: 9c06d725622720a265d23b9fd12ba7ccd6d4f39f (plain)
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
#include "xlua.h"

/* window:wait_event() -> event, data */
static int wait_event(lua_State *L)
{
	static const struct key mousebutton_keys[] = {
		KEY_STR(button, VT_STRING),
		KEY_STR(down,   VT_BOOLEAN),
		KEY_STR(x,      VT_NUMBER),
		KEY_STR(y,      VT_NUMBER),
		KEY_END,
	};
	static const struct key mousemotion_keys[] = {
		KEY_STR(x,    VT_NUMBER),
		KEY_STR(y,    VT_NUMBER),
		KEY_STR(xrel, VT_NUMBER),
		KEY_STR(yrel, VT_NUMBER),
		KEY_END,
	};
	static const struct key key_keys[] = {
		KEY_STR(down,   VT_BOOLEAN),
		KEY_STR(repeat, VT_BOOLEAN),
		KEY_END,
	};
	static const struct key keysym_keys[] = {
		KEY_STR(scancode, VT_STRING),
		KEY_STR(sym,      VT_STRING),
		KEY_END,
	};
	struct window *w;
	SDL_Event e;

	w = luaL_checkudata(L, 1, UD_WINDOW);

	while (1) {
		if (SDL_WaitEvent(&e) == 0) return sdl_error(L, "SDL_WaitEvent");

		switch (e.type) {
		case SDL_QUIT:
			lua_pushstring(L, "QUIT");
			lua_pushnil(L);
			break;
		case SDL_WINDOWEVENT:
			if (e.window.event != SDL_WINDOWEVENT_EXPOSED)
				continue;
			lua_pushstring(L, "REDRAW");
			lua_pushnil(L);
			break;
		case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:
			lua_pushstring(L, "MOUSEBUTTON");
			pushtable(L, mousebutton_keys,
				  strbutton(e.button.button),
				  e.button.state == SDL_PRESSED,
				  (lua_Number)e.button.x,
				  (lua_Number)e.button.y);
			break;
		case SDL_MOUSEMOTION:
			lua_pushstring(L, "MOUSEMOTION");
			pushtable(L, mousemotion_keys,
				  (lua_Number)e.motion.x,
				  (lua_Number)e.motion.y,
				  (lua_Number)e.motion.xrel,
				  (lua_Number)e.motion.yrel);
			lua_pushstring(L, "state");
			lua_createtable(L, 0, 5);
			pushsetmember(L, -1, "LEFT",   e.motion.state & SDL_BUTTON_LMASK);
			pushsetmember(L, -1, "MIDDLE", e.motion.state & SDL_BUTTON_MMASK);
			pushsetmember(L, -1, "RIGHT",  e.motion.state & SDL_BUTTON_RMASK);
			pushsetmember(L, -1, "X1",     e.motion.state & SDL_BUTTON_X1MASK);
			pushsetmember(L, -1, "X2",     e.motion.state & SDL_BUTTON_X2MASK);
			lua_settable(L, -3);
			break;
		case SDL_KEYDOWN: case SDL_KEYUP:
			lua_pushstring(L, "KEY");
			pushtable(L, key_keys,
				  e.key.state == SDL_PRESSED,
				  e.key.repeat);
			lua_pushstring(L, "keysym");
			pushtable(L, keysym_keys,
				  strscancode(e.key.keysym.scancode),
				  strsym(e.key.keysym.sym));
			lua_pushstring(L, "mod");
			lua_createtable(L, 0, 11);
			pushsetmember(L, -1, "LSHIFT", e.key.keysym.mod & KMOD_LSHIFT);
			pushsetmember(L, -1, "RSHIFT", e.key.keysym.mod & KMOD_RSHIFT);
			pushsetmember(L, -1, "LCTRL", e.key.keysym.mod & KMOD_LCTRL);
			pushsetmember(L, -1, "RCTRL", e.key.keysym.mod & KMOD_RCTRL);
			pushsetmember(L, -1, "LALT", e.key.keysym.mod & KMOD_LALT);
			pushsetmember(L, -1, "RALT", e.key.keysym.mod & KMOD_RALT);
			pushsetmember(L, -1, "LGUI", e.key.keysym.mod & KMOD_LGUI);
			pushsetmember(L, -1, "RGUI", e.key.keysym.mod & KMOD_RGUI);
			pushsetmember(L, -1, "NUM", e.key.keysym.mod & KMOD_NUM);
			pushsetmember(L, -1, "CAPS", e.key.keysym.mod & KMOD_CAPS);
			pushsetmember(L, -1, "MODE", e.key.keysym.mod & KMOD_MODE);
			lua_settable(L, -3);
			lua_settable(L, -3);
			break;
		default: continue;
		}
		return 2;
	}
}