/
spnavkbd.c
187 lines (156 loc) · 4.64 KB
/
spnavkbd.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
/*
* spnavkbd.c - 3d Connexion Space-Navigator to keyboard emulator
*
* Copyright © 2012 Mason Larobina <mason.larobina@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <time.h>
#include <X11/Xlib.h>
#include <spnav.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
XKeyEvent event;
void
sig(int s)
{
spnav_close();
exit(0);
}
bool
lua_loadrc(lua_State *L, const char *confpath)
{
printf("loading rc: %s\n", confpath);
if (!luaL_loadfile(L, confpath)) {
if (lua_pcall(L, 0, LUA_MULTRET, 0))
fprintf(stderr, "%s\n", lua_tostring(L, -1));
else
return true;
} else
fprintf(stderr, "%s\n", lua_tostring(L, -1));
return false;
}
bool
lua_dofunction(lua_State *L, const char *fname, int nargs)
{
lua_getglobal(L, fname);
if (!lua_isfunction(L, -1)) {
fprintf(stderr, "'%s' not a function", fname);
lua_pop(L, 1);
return false;
}
/* move function above args */
lua_insert(L, -(nargs+1));
if (!lua_pcall(L, nargs, 0, 0))
return true;
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
return false;
}
/* return current time with nanosecond precision (overkill) */
int
lua_gettime(lua_State *L)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
lua_pushnumber(L, ts.tv_sec + (ts.tv_nsec / 1e9));
return 1;
}
int
lua_send_keyev(lua_State *L)
{
/* event.window = currently focused window */
Window win_focus;
int revert;
XGetInputFocus(event.display, &win_focus, &revert);
event.window = win_focus;
event.keycode = luaL_checkint(L, 1);
event.state = !lua_isnoneornil(L, 2) ? luaL_checkint(L, 2) : 0;
//printf("send_keyev (%4d, %4d)\n", event.keycode, event.state);
event.type = KeyPress;
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent*)&event);
event.type = KeyRelease;
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent*)&event);
return 0;
}
void
lua_init(lua_State **L)
{
*L = luaL_newstate();
luaL_openlibs(*L);
lua_pushcfunction(*L, lua_gettime);
lua_setglobal(*L, "gettime");
lua_pushcfunction(*L, lua_send_keyev);
lua_setglobal(*L, "send_keyev");
if (!lua_loadrc(*L, "spnavkbd.lua"))
exit(1);
}
int main(void)
{
Display *dpy;
Window win;
unsigned long bpix;
spnav_event sev;
signal(SIGINT, sig);
if(!(dpy = XOpenDisplay(0))) {
fprintf(stderr, "failed to connect to the X server\n");
return 1;
}
bpix = BlackPixel(dpy, DefaultScreen(dpy));
win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, bpix, bpix);
/* This actually registers our window with the driver for receiving
* motion/button events through the 3dxsrv-compatible X11 protocol.
*/
if(spnav_x11_open(dpy, win) == -1) {
fprintf(stderr, "failed to connect to the space navigator daemon\n");
return 1;
}
/* setup key event struct */
event.display = dpy;
event.root = XDefaultRootWindow(dpy);
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = True;
event.state = 0;
/* setup lua state & load users config */
lua_State *L;
lua_init(&L);
while(spnav_wait_event(&sev)) {
if(sev.type == SPNAV_EVENT_MOTION) {
lua_pushnumber(L, sev.motion.x);
lua_pushnumber(L, sev.motion.y);
lua_pushnumber(L, sev.motion.z);
lua_pushnumber(L, sev.motion.rx);
lua_pushnumber(L, sev.motion.ry);
lua_pushnumber(L, sev.motion.rz);
lua_dofunction(L, "motion_event", 6);
} else { /* SPNAV_EVENT_BUTTON */
lua_pushstring(L, (sev.button.press ? "press" : "release"));
lua_pushnumber(L, sev.button.bnum);
lua_dofunction(L, "button_event", 2);
}
}
spnav_close();
return 0;
}