-
Notifications
You must be signed in to change notification settings - Fork 1
/
enjoy.c
148 lines (105 loc) · 2.47 KB
/
enjoy.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
void init() {
SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_VIDEO);
SDL_JoystickEventState(SDL_ENABLE);
}
void dumpEvent(SDL_Event e, unsigned int time) {
printf("----\n");
printf("time: %u\n", time);
printf("value: %s\n", e.type == SDL_JOYBUTTONDOWN ? "DOWN" : "UP" );
printf("number: %d\n", e.jbutton.button);
}
void handleEvents(lua_State* L)
{
while(1) {
SDL_Event event;
while(SDL_WaitEvent(&event)) {
switch(event.type) {
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
dumpEvent(event,time(0));
lua_getglobal(L,"__event_button");
lua_pushnumber(L, (double) event.jbutton.button );
lua_pushboolean(L, (int) event.type == SDL_JOYBUTTONDOWN );
if(lua_pcall(L, 2, 0, 0) != 0) {
luaL_error(L,"%s\n",lua_tostring(L, -1));
}
break;
case SDL_JOYAXISMOTION:
printf("axes\n");
break;
}
}
}
}
int openJoystick(int index) {
SDL_Joystick *joy;
if( SDL_NumJoysticks() > 0){
joy = SDL_JoystickOpen(index);
if(joy) {
printf("Using: %s\n", SDL_JoystickName(index));
printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
}
else {
printf("Problem opening joystick %d\n",index);
exit(1);
}
}
}
lua_State* openLua() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
return L;
}
void loadLuaFile(lua_State* L, const char* filename)
{
int error = luaL_dofile(L, filename);
if(error) {
printf("%s",lua_tostring(L, -1));
lua_error(L);
}
}
// returns the joy index or -1 in case of error
int selectJoy(const char* index_string) {
int index,ret;
return sscanf(index_string,"%d",&index) ? index : -1;
}
void printJoyInfo()
{
int njoys = SDL_NumJoysticks();
if ( njoys == 0) {
printf("No joystick detected.\n");
} else {
int i;
for (i=0; i< njoys; i++) {
printf("%d %s\n", i, SDL_JoystickName(i));
}
}
}
int main(int argc, char** argv) {
// TODO print usage and parameters
init();
if (argc < 2) {
printJoyInfo();
exit(0);
}
int selected = selectJoy(argv[1]);
if (selected < 0) {
exit(1);
}
openJoystick(selected);
lua_State *L = openLua();
// TODO have core.lua as a precompiled binary
loadLuaFile(L,"core.lua");
// lua_register(L, "__send_key_event" , lua_send_key_event );
handleEvents(L);
return 0;
}