My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* @file sys_evdev.c
* @author Ambroz Bizjak <ambrop7@gmail.com>
*
* @section LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @section DESCRIPTION
*
* Linux event device module.
*
* Synopsis: sys.evdev(string device)
* Description: reports input events from a Linux event device. Transitions up when an event is
* detected, and goes down waiting for the next event when sys.evdev::nextevent() is called.
* Variables:
* string type - symbolic event type (e.g. EV_KEY, EV_REL, EV_ABS), corresponding to
* (struct input_event).type, or "unknown"
* string value - event value (signed integer), equal to (struct input_event).value
* string code_numeric - numeric event code (unsigned integer), equal to
* (struct input_event).code
* string code - symbolic event code (e.g. KEY_ESC. KEY_1, KEY_2, BTN_LEFT), corrresponding
* to (struct input_event).code, or "unknown"
*
* Synopsis: sys.evdev::nextevent()
* Description: makes the evdev module transition down in order to report the next event.
*/

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>

#include <misc/nonblocking.h>
#include <misc/debug.h>
#include <ncd/NCDModule.h>
#include <ncd/static_strings.h>
#include <ncd/extra/value_utils.h>

#include <generated/blog_channel_ncd_sys_evdev.h>

#include "linux_input_names.h"

#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
#define ModuleString(i, id) ((i)->m->group->strings[(id)])

struct instance {
NCDModuleInst *i;
int evdev_fd;
BFileDescriptor bfd;
int processing;
struct input_event event;
};

static void instance_free (struct instance *o, int is_error);

enum {STRING_VALUE, STRING_CODE_NUMERIC, STRING_CODE};

static const char *strings[] = {
"value", "code_numeric", "code", NULL
};

#define MAKE_LOOKUP_FUNC(_name_) \
static const char * evdev_##_name_##_to_str (uint16_t type) \
{ \
if (type >= (sizeof(_name_##_names) / sizeof(_name_##_names[0])) || !_name_##_names[type]) { \
return "unknown"; \
} \
return _name_##_names[type]; \
}

MAKE_LOOKUP_FUNC(type)
MAKE_LOOKUP_FUNC(key)
MAKE_LOOKUP_FUNC(rel)
MAKE_LOOKUP_FUNC(abs)
MAKE_LOOKUP_FUNC(sw)
MAKE_LOOKUP_FUNC(msc)
MAKE_LOOKUP_FUNC(led)
MAKE_LOOKUP_FUNC(rep)
MAKE_LOOKUP_FUNC(snd)
MAKE_LOOKUP_FUNC(ffstatus)

static void device_handler (struct instance *o, int events)
{
if (o->processing) {
ModuleLog(o->i, BLOG_ERROR, "device error");
instance_free(o, 1);
return;
}

int res = read(o->evdev_fd, &o->event, sizeof(o->event));
if (res < 0) {
ModuleLog(o->i, BLOG_ERROR, "read failed");
instance_free(o, 1);
return;
}
if (res != sizeof(o->event)) {
ModuleLog(o->i, BLOG_ERROR, "read wrong");
instance_free(o, 1);
return;
}

// stop reading
BReactor_SetFileDescriptorEvents(o->i->params->iparams->reactor, &o->bfd, 0);

// set processing
o->processing = 1;

// signal up
NCDModuleInst_Backend_Up(o->i);
}

static void device_nextevent (struct instance *o)
{
ASSERT(o->processing)

// start reading
BReactor_SetFileDescriptorEvents(o->i->params->iparams->reactor, &o->bfd, BREACTOR_READ);

// set not processing
o->processing = 0;

// signal down
NCDModuleInst_Backend_Down(o->i);
}

static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
{
struct instance *o = vo;
o->i = i;

// check arguments
NCDValRef device_arg;
if (!NCDVal_ListRead(params->args, 1, &device_arg)) {
ModuleLog(o->i, BLOG_ERROR, "wrong arity");
goto fail0;
}
if (!NCDVal_IsStringNoNulls(device_arg)) {
ModuleLog(o->i, BLOG_ERROR, "wrong type");
goto fail0;
}

// null terminate device
NCDValNullTermString device_nts;
if (!NCDVal_StringNullTerminate(device_arg, &device_nts)) {
ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
goto fail0;
}

// open device
o->evdev_fd = open(device_nts.data, O_RDONLY);
NCDValNullTermString_Free(&device_nts);
if (o->evdev_fd < 0) {
ModuleLog(o->i, BLOG_ERROR, "open failed");
goto fail0;
}

// set non-blocking
if (!badvpn_set_nonblocking(o->evdev_fd)) {
ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
goto fail1;
}

// init BFileDescriptor
BFileDescriptor_Init(&o->bfd, o->evdev_fd, (BFileDescriptor_handler)device_handler, o);
if (!BReactor_AddFileDescriptor(o->i->params->iparams->reactor, &o->bfd)) {
ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
goto fail1;
}
BReactor_SetFileDescriptorEvents(o->i->params->iparams->reactor, &o->bfd, BREACTOR_READ);

// set not processing
o->processing = 0;
return;

fail1:
if (close(o->evdev_fd) < 0) {
ModuleLog(o->i, BLOG_ERROR, "close failed");
}
fail0:
NCDModuleInst_Backend_DeadError(i);
}

void instance_free (struct instance *o, int is_error)
{
// free BFileDescriptor
BReactor_RemoveFileDescriptor(o->i->params->iparams->reactor, &o->bfd);

// close device.
// Ignore close error which happens if the device is removed.
if (close(o->evdev_fd) < 0) {
ModuleLog(o->i, BLOG_ERROR, "close failed");
}

if (is_error) {
NCDModuleInst_Backend_DeadError(o->i);
} else {
NCDModuleInst_Backend_Dead(o->i);
}
}

static void func_die (void *vo)
{
struct instance *o = vo;
instance_free(o, 0);
}

static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
{
struct instance *o = vo;
ASSERT(o->processing)

if (name == NCD_STRING_TYPE) {
*out = NCDVal_NewString(mem, evdev_type_to_str(o->event.type));
return 1;
}

if (name == ModuleString(o->i, STRING_VALUE)) {
char str[50];
snprintf(str, sizeof(str), "%"PRIi32, o->event.value);
*out = NCDVal_NewString(mem, str);
return 1;
}

if (name == ModuleString(o->i, STRING_CODE_NUMERIC)) {
*out = ncd_make_uintmax(mem, o->event.code);
return 1;
}

if (name == ModuleString(o->i, STRING_CODE)) {
const char *str = "unknown";

#define MAKE_CASE(_evname_, _name_) \
case _evname_: \
str = evdev_##_name_##_to_str(o->event.code); \
break;

switch (o->event.type) {
#ifdef EV_KEY
MAKE_CASE(EV_KEY, key)
#endif
#ifdef EV_REL
MAKE_CASE(EV_REL, rel)
#endif
#ifdef EV_ABS
MAKE_CASE(EV_ABS, abs)
#endif
#ifdef EV_SW
MAKE_CASE(EV_SW, sw)
#endif
#ifdef EV_MSC
MAKE_CASE(EV_MSC, msc)
#endif
#ifdef EV_LED
MAKE_CASE(EV_LED, led)
#endif
#ifdef EV_REP
MAKE_CASE(EV_REP, rep)
#endif
#ifdef EV_SND
MAKE_CASE(EV_SND, snd)
#endif
#ifdef EV_FF_STATUS
MAKE_CASE(EV_FF_STATUS, ffstatus)
#endif
}

*out = NCDVal_NewString(mem, str);
return 1;
}

return 0;
}

static void nextevent_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
{
// check arguments
if (!NCDVal_ListRead(params->args, 0)) {
ModuleLog(i, BLOG_ERROR, "wrong arity");
goto fail0;
}

// get method object
struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);

// make sure we are currently reporting an event
if (!mo->processing) {
ModuleLog(i, BLOG_ERROR, "not reporting an event");
goto fail0;
}

// signal up.
// Do it before finishing the event so our process does not advance any further if
// we would be killed the event provider going down.
NCDModuleInst_Backend_Up(i);

// wait for next event
device_nextevent(mo);
return;

fail0:
NCDModuleInst_Backend_DeadError(i);
}

static struct NCDModule modules[] = {
{
.type = "sys.evdev",
.func_new2 = func_new,
.func_die = func_die,
.func_getvar2 = func_getvar2,
.alloc_size = sizeof(struct instance)
}, {
.type = "sys.evdev::nextevent",
.func_new2 = nextevent_func_new
}, {
.type = NULL
}
};

const struct NCDModuleGroup ncdmodule_sys_evdev = {
.modules = modules,
.strings = strings
};

Change log

r1993 by ambrop7 on Dec 9, 2012   Diff
ncd: NCDModule: allow separating static
module information from generated at
runtime by the interpreter. Fix other
stuff to work with
the new design. This allows modules to
keep interpreter-wide state, and, when
modules are fixed, will allow running more
than one
NCDInterpreter in the same program.
Go to: 
Project members, sign in to write a code review

Older revisions

r1986 by ambrop7 on Dec 5, 2012   Diff
ncd: modules: use the new static
strings
r1936 by ambrop7 on Nov 29, 2012   Diff
ncd: NCDModule: remove
NCDModuleInst_Backend_SetError() and
add NCDModuleInst_Backend_DeadError()
instead. Fix modules.
r1934 by ambrop7 on Nov 29, 2012   Diff
ncd: modules: remove logging about
errors from NCDVal functions
All revisions of this file

File info

Size: 10327 bytes, 348 lines
Powered by Google Project Hosting