-
Notifications
You must be signed in to change notification settings - Fork 49
/
mevent.c
448 lines (375 loc) · 10.5 KB
/
mevent.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* This file is part of the Advance project.
*
* Copyright (C) 2003, 2004, 2005 Andrea Mazzoleni
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* In addition, as a special exception, Andrea Mazzoleni
* gives permission to link the code of this program with
* the MAME library (or with modified versions of MAME that use the
* same license as MAME), and distribute linked combinations including
* the two. You must obey the GNU General Public License in all
* respects for all of the code used other than MAME. If you modify
* this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*/
#include "portable.h"
#include "mevent.h"
#include "log.h"
#include "oslinux.h"
#include "error.h"
#include "snstring.h"
#include "event.h"
#if defined(USE_SVGALIB)
#include <vga.h>
#include <vgamouse.h>
#endif
#include <linux/input.h>
#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#define EVENT_MOUSE_MAX 8
#define EVENT_MOUSE_DEVICE_MAX 32
#define EVENT_MOUSE_AXE_MAX 8
#define EVENT_MOUSE_BUTTON_MAX 16
#define EVENT_MOUSE_NAME_MAX 128
struct mouse_axe_context {
unsigned code;
int value;
char name[EVENT_MOUSE_NAME_MAX];
};
struct mouse_button_context {
unsigned code;
int revbind;
adv_bool state;
char name[EVENT_MOUSE_NAME_MAX];
};
struct mouse_item_context {
int f;
unsigned vendor;
unsigned product;
unsigned version;
unsigned bus;
unsigned char evtype_bitmask[EV_MAX / 8 + 1];
unsigned axe_mac;
struct mouse_axe_context axe_map[EVENT_MOUSE_AXE_MAX];
unsigned button_mac;
struct mouse_button_context button_map[EVENT_MOUSE_BUTTON_MAX];
int bind_map[MOUSEB_MAX];
};
struct mouseb_event_context {
unsigned mac;
struct mouse_item_context map[EVENT_MOUSE_MAX];
};
static struct mouseb_event_context event_state;
static adv_device DEVICE[] = {
{ "auto", -1, "Linux input-event mouse" },
{ 0, 0, 0 }
};
static adv_error mouseb_setup(struct mouse_item_context* item, int f)
{
unsigned char key_bitmask[KEY_MAX / 8 + 1];
unsigned char rel_bitmask[REL_MAX / 8 + 1];
unsigned i;
struct button_entry {
int code;
const char* name;
int bind;
} button_map[] = {
#ifdef BTN_LEFT
{ BTN_LEFT, "left", MOUSEB_LEFT },
#endif
#ifdef BTN_RIGHT
{ BTN_RIGHT, "right", MOUSEB_RIGHT },
#endif
#ifdef BTN_MIDDLE
{ BTN_MIDDLE, "middle", MOUSEB_MIDDLE },
#endif
#ifdef BTN_SIDE
{ BTN_SIDE, "side", MOUSEB_SIDE },
#endif
#ifdef BTN_EXTRA
{ BTN_EXTRA, "extra", MOUSEB_EXTRA },
#endif
#ifdef BTN_FORWARD
{ BTN_FORWARD, "forward", MOUSEB_FORWARD },
#endif
#ifdef BTN_BACK
{ BTN_BACK, "back", MOUSEB_BACK }
#endif
};
/* WARNING: It must be syncronized with the list in event.c */
struct axe_entry {
int code;
const char* name;
} axe_map[] = {
#ifdef REL_X
{ REL_X, "x" },
#endif
#ifdef REL_Y
{ REL_Y, "y" },
#endif
#ifdef REL_Z
{ REL_Z, "z" },
#endif
#ifdef REL_WHEEL
{ REL_WHEEL, "wheel" },
#endif
#ifdef REL_HWHEEL
{ REL_HWHEEL, "hwheel" },
#endif
#ifdef REL_DIAL
{ REL_DIAL, "dial" },
#endif
#ifdef REL_MISC
{ REL_MISC, "misc" }
#endif
};
item->f = f;
memset(key_bitmask, 0, sizeof(key_bitmask));
if (event_test_bit(EV_KEY, item->evtype_bitmask)) {
if (ioctl(f, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) < 0) {
log_std(("event: error in ioctl(EVIOCGBIT(EV_KEY,%d))\n", (int)KEY_MAX));
return -1;
}
}
for (i = 0; i < MOUSEB_MAX; ++i)
item->bind_map[i] = -1;
item->button_mac = 0;
for (i = 0; i < sizeof(button_map) / sizeof(button_map[0]); ++i) {
if (event_test_bit(button_map[i].code, key_bitmask)) {
if (item->button_mac < EVENT_MOUSE_BUTTON_MAX) {
item->button_map[item->button_mac].code = button_map[i].code;
item->button_map[item->button_mac].state = 0;
sncpy(item->button_map[item->button_mac].name, sizeof(item->button_map[item->button_mac].name), button_map[i].name);
if (button_map[i].bind) {
item->bind_map[button_map[i].bind] = item->button_mac;
item->button_map[item->button_mac].revbind = button_map[i].bind;
} else {
item->button_map[item->button_mac].revbind = -1;
}
item->bind_map[item->button_mac] = item->button_mac;
++item->button_mac;
}
}
}
memset(rel_bitmask, 0, sizeof(rel_bitmask));
if (event_test_bit(EV_REL, item->evtype_bitmask)) {
if (ioctl(f, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) < 0) {
log_std(("event: error in ioctl(EVIOCGBIT(EV_REL,%d))\n", (int)REL_MAX));
return -1;
}
}
item->axe_mac = 0;
for (i = 0; i < sizeof(axe_map) / sizeof(axe_map[0]); ++i) {
if (event_test_bit(axe_map[i].code, rel_bitmask)) {
if (item->axe_mac < EVENT_MOUSE_AXE_MAX) {
item->axe_map[item->axe_mac].code = axe_map[i].code;
item->axe_map[item->axe_mac].value = 0;
sncpy(item->axe_map[item->axe_mac].name, sizeof(item->axe_map[item->axe_mac].name), axe_map[i].name);
++item->axe_mac;
}
}
}
return 0;
}
adv_error mouseb_event_init(int mouseb_id)
{
unsigned i;
adv_bool eacces = 0;
struct event_location map[EVENT_MOUSE_DEVICE_MAX];
unsigned mac;
log_std(("mouseb:event: mouseb_event_init(id:%d)\n", mouseb_id));
#if defined(USE_SVGALIB)
/* close the SVGALIB mouse device. SVGALIB always call mouse_init(), also */
/* if mouse input is not requested */
if (os_internal_svgalib_get()) {
mouse_close();
}
#endif
log_std(("mouseb:event: opening mouse from 0 to %d\n", EVENT_MOUSE_DEVICE_MAX));
mac = event_locate(map, EVENT_MOUSE_DEVICE_MAX, "event", &eacces);
event_state.mac = 0;
for (i = 0; i < mac; ++i) {
int f;
struct mouse_item_context* item = &event_state.map[event_state.mac];
if (event_state.mac >= EVENT_MOUSE_MAX)
continue;
f = event_open(map[i].file, item->evtype_bitmask, sizeof(item->evtype_bitmask));
if (f == -1) {
if (errno == EACCES) {
eacces = 1;
}
continue;
}
if (!event_is_mouse(f, item->evtype_bitmask)) {
log_std(("mouseb:event: not a mouse on device %s\n", map[i].file));
event_close(f);
continue;
}
if (mouseb_setup(item, f) != 0) {
event_close(f);
continue;
}
item->vendor = map[i].vendor;
item->product = map[i].product;
item->version = map[i].version;
item->bus = map[i].bus;
++event_state.mac;
}
if (!event_state.mac) {
if (eacces)
error_set("No mouse found. Check the /dev/input/event* permissions.\n");
else
error_set("No mouse found.\n");
return -1;
}
return 0;
}
void mouseb_event_done(void)
{
unsigned i;
log_std(("mouseb:event: mouseb_event_done()\n"));
for (i = 0; i < event_state.mac; ++i)
event_close(event_state.map[i].f);
event_state.mac = 0;
}
unsigned mouseb_event_count_get(void)
{
log_debug(("mouseb:event: mouseb_event_count_get()\n"));
return event_state.mac;
}
int mouseb_event_device_name_get(unsigned mouse, char* name)
{
log_debug(("mouseb:event: mouseb_event_device_name_get(%u)\n", mouse));
if (event_state.map[mouse].vendor == 0)
return -1;
snprintf(name, DEVICE_NAME_MAX, "%04x_%04x", event_state.map[mouse].vendor, event_state.map[mouse].product);
return 0;
}
unsigned mouseb_event_axe_count_get(unsigned mouse)
{
log_debug(("mouseb:event: mouseb_event_axe_count_get()\n"));
return event_state.map[mouse].axe_mac;
}
const char* mouseb_event_axe_name_get(unsigned mouse, unsigned axe)
{
log_debug(("mouseb:event: mouseb_event_button_axe_get()\n"));
return event_state.map[mouse].axe_map[axe].name;
}
unsigned mouseb_event_button_count_get(unsigned mouse)
{
log_debug(("mouseb:event: mouseb_event_button_count_get()\n"));
return event_state.map[mouse].button_mac;
}
const char* mouseb_event_button_name_get(unsigned mouse, unsigned button)
{
log_debug(("mouseb:event: mouseb_event_button_name_get()\n"));
return event_state.map[mouse].button_map[button].name;
}
int mouseb_event_axe_get(unsigned mouse, unsigned axe)
{
int r;
log_debug(("mouseb:event: mouseb_event_axe_get()\n"));
r = event_state.map[mouse].axe_map[axe].value;
event_state.map[mouse].axe_map[axe].value = 0;
return r;
}
unsigned mouseb_event_button_get(unsigned mouse, unsigned button)
{
log_debug(("mouseb:event: mouseb_event_button_get()\n"));
return event_state.map[mouse].button_map[button].state;
}
int mouseb_event_bind(unsigned mouse, unsigned code)
{
log_debug(("mouseb:event: mouseb_event_bind_map()\n"));
return event_state.map[mouse].bind_map[code];
}
int mouseb_event_revbind(unsigned mouse, unsigned button)
{
log_debug(("mouseb:event: mouseb_event_revbind()\n"));
return event_state.map[mouse].button_map[button].revbind;
}
int mouseb_event_poll(void)
{
unsigned i;
int type, code, value;
int error = 0;
log_debug(("mouseb:event: mouseb_event_poll()\n"));
for (i = 0; i < event_state.mac; ++i) {
struct mouse_item_context* item = event_state.map + i;
int ret;
while ((ret = event_read(item->f, &type, &code, &value)) == 1) {
if (type == EV_KEY) {
unsigned j;
for (j = 0; j < item->button_mac; ++j) {
if (code == item->button_map[j].code) {
item->button_map[j].state = value != 0;
break;
}
}
} else if (type == EV_REL) {
unsigned j;
for (j = 0; j < item->axe_mac; ++j) {
if (code == item->axe_map[j].code) {
item->axe_map[j].value += value;
break;
}
}
}
}
/* keep track of any error */
if (ret < 0)
error = -1;
}
return error;
}
unsigned mouseb_event_flags(void)
{
return 0;
}
adv_error mouseb_event_load(adv_conf* context)
{
return 0;
}
void mouseb_event_reg(adv_conf* context)
{
}
/***************************************************************************/
/* Driver */
mouseb_driver mouseb_event_driver = {
"event",
DEVICE,
mouseb_event_load,
mouseb_event_reg,
mouseb_event_init,
mouseb_event_done,
0,
0,
mouseb_event_flags,
mouseb_event_count_get,
mouseb_event_axe_count_get,
mouseb_event_axe_name_get,
mouseb_event_button_count_get,
mouseb_event_button_name_get,
mouseb_event_axe_get,
mouseb_event_button_get,
mouseb_event_poll,
mouseb_event_device_name_get,
mouseb_event_bind,
mouseb_event_revbind
};