aboutsummaryrefslogblamecommitdiffstats
path: root/libgamepad.h
blob: 627589a9a12215616a947e882d5d8366bf47fbe0 (plain) (tree)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566





















































































































































































































































































































































































































































































































































































                                                                                                                                 
/* See LICENSE file for copyright and license details. */
#ifndef LIBGAMEPAD_H
#define LIBGAMEPAD_H

#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stddef.h>
#include <time.h>

#include <libevdev/libevdev.h>


/**
 * Opaque structure for monitoring device attachment changes
 */
typedef struct libgamepad_attachment_monitor LIBGAMEPAD_ATTACHMENT_MONITOR;


/**
 * Device attachment event type
 */
enum libgamepad_attachment_event_type {
	/**
	 * Device has been added
	 */
	LIBGAMEPAD_ADDED,

	/**
	 * Device has been removed
	 */
	LIBGAMEPAD_REMOVED
};


/**
 * Device type
 *
 * A device can have any number of these applied to
 * it, it may also have unknown types beyond these
 */
enum libgamepad_type {
	/**
	 * Gamepad, joystick, steering wheel, or similar
	 */
	LIBGAMEPAD_GAMEPAD = 0x0001,

	/**
	 * Computer mouse
	 */
	LIBGAMEPAD_MOUSE = 0x0002
};


/**
 * Gamepad input type
 */
enum libgamepad_input_type {
	/**
	 * Button/key
	 */
	LIBGAMEPAD_BUTTON,

	/**
	 * Absolute axis
	 */
	LIBGAMEPAD_ABSOLUTE_AXIS,

	/**
	 * Relative axis
	 */
	LIBGAMEPAD_RELATIVE_AXIS
};


/**
 * Subdevice input event structure
 */
struct libgamepad_input_event {
	/**
	 * The affect input type
	 */
	enum libgamepad_input_type type;

	/**
	 * Whether the event was polled in a
	 * synchronisation read
	 */
	short int sync_event;

	/**
	 * The button/key or axis affect by the event
	 */
	uint16_t code;

	/**
	 * The new value on the button/key or axis,
	 * or the delta if on a relative axis
	 */
	int32_t value;

	/**
	 * Event timestamp
	 */
	struct timeval time;
};


/**
 * Subdevice on a device
 * 
 * For example a modern gamepad may be split into
 * a gamepad, computer mouse, and motion sensor
 *
 * Structure is always deallocated by the library
 * using free(3)
 */
struct libgamepad_subdevice {
	/**
	 * Device type
	 */
	enum libgamepad_type type;

	/**
	 * Device path
	 */
	char path[];
};


/**
 * Physical device
 * 
 * Contents of structure is deallocated with
 * `libgamepad_close_superdevice`
 */
struct libgamepad_superdevice {
	/**
	 * Device path in /sys
	 */
	char *syspath;

	/**
	 * Number of subdevices
	 */
	size_t ndevices;

	/**
	 * Subdevices
	 */
	struct libgamepad_subdevice **devices;

	/**
	 * Number of LEDs
	 */
	size_t nleds;

	/**
	 * Paths to LEDs
	 */
	char **leds;

	/**
	 * Number of power supplies (batteries)
	 */
	size_t npower_supplies;

	/**
	 * Paths of power supplies (batteries)
	 */
	char **power_supplies;
};


/**
 * Subdevice structure for detailed information listening on input events
 */
struct libgamepad_device {
	/**
	 * File descriptor to the device, the application
	 * may use it to poll for read-readyness
	 */
	int fd;

	/**
	 * Bus type the device is connect via, see BUS_-prefixed
	 * constants in <linux/input.h>
	 */
	int bus_type;

	/**
	 * Vendor ID for the device (sub- or superdevice)
	 */
	int vendor;

	/**
	 * Product ID for the device (sub- or superdevice)
	 */
	int product;

	/**
	 * Product version ID for the device (sub- or superdevice)
	 */
	int version;

	/**
	 * FOR INTERNAL USE
	 * 
	 * Specifies whether `.fd` shall be closed with the device
	 */
	int close_fd;

	/**
	 * FOR INTERNAL USE
	 * 
	 * Whether the device must be synchronised
	 */
	int require_sync;

	/**
	 * Human-readable device (sub- or superdevice) name
	 */
	const char *name;

	/**
	 * ID that is supposted to be unique to the device
	 * (sub- or superdevice)
	 */
	const char *unique_id;

	/**
	 * The location if the device
	 */
	const char *physical_location;

	/**
	 * libevdev instance for the device
	 */
	struct libevdev *dev;

	/**
	 * Number of (digital) buttons/keys present
	 * on the device
	 */
	size_t nbuttons;

	/**
	 * Number of absolute axes present on the device
	 */
	size_t nabsolute_axes;

	/**
	 * Number of relative axes present on the device
	 */
	size_t nrelative_axes;

	/**
	 * Maps from button/key codes to button indices,
	 * non-present buttons/keys map to -1, other
	 * values are in [0, `.nbuttons`[.
	 */
	int16_t button_map[KEY_CNT];

	/**
	 * Maps from absolute axis codes to absolute axis
	 * indices, non-present axes map to -1, other
	 * values are in [0, `.nabsolute_axes`[.
	 */
	int16_t absolute_axis_map[ABS_CNT];

	/**
	 * Maps from relative axis codes to absolute axis
	 * indices, non-present axes map to -1, other
	 * values are in [0, `.nrelative_axes`[.
	 */
	int16_t relative_axis_map[REL_CNT];

	/**
	 * Map from button/key indices to button/key codes
	 */
	uint16_t *buttons;

	/**
	 * Map from absolute axis indices to absolute axis codes
	 */
	uint16_t *absolute_axes;

	/**
	 * Map from relative axis indices to relative axis codes
	 */
	uint16_t *relative_axes;
};


/**
 * Get a list of all available physical devices
 * 
 * @param   devicesp   Output parameter for the list of devices
 * @param   ndevicesp  Output parameter for the number of listed devices
 * @return             0 on success, -1 on failure
 * 
 * This function may fail for any reason specified for
 * realloc(3), open(3), fdopendir(3), or readdir(3)
 */
int libgamepad_list_superdevices(char ***, size_t *);

/**
 * Get information about a physical device
 * 
 * @param   devicep  Output parameter for the device information,
 *                   allocated memory shall be deallocated with
 *                   `libgamepad_close_superdevice(devicep)`
 * @param   syspath  The path of the device, in /sys
 * @return           0 on success, -1 on failure
 * 
 * This function may fail for any reason specified for
 * realloc(3), openat(3), fdopendir(3), or readdir(3)
 */
int libgamepad_open_superdevice(struct libgamepad_superdevice *, const char *);

/**
 * Deallocate the contents of a `struct libgamepad_superdevice`
 * 
 * @param  device  The structure whose nested memory allocations
 *                 shall be deallocated, may be `NULL`
 */
void libgamepad_close_superdevice(struct libgamepad_superdevice *);


/**
 * Create a device attachment monitor
 * 
 * The user shall poll the returned file descriptor
 * for read-readiness, and whenever it is ready,
 * call `libgamepad_get_attachment_event`
 * 
 * @param   monitorp  Output parameter for the monitor, shall deallocated with
 *                    `libgamepad_destroy_attachment_monitor` when no longer used
 * @return            A file descriptor on successful completion, -1 on failure
 *
 * This function may fail for any reason specified by
 * malloc(3), udev_new(3), udev_monitor_new_from_netlink(3), or
 * udev_monitor_enable_receiving(3)
 */
int libgamepad_create_attachment_monitor(LIBGAMEPAD_ATTACHMENT_MONITOR **);

/**
 * Deallocate a device attachment monitor
 * 
 * @param  monitor  The monitor to deallocate; the file descriptor returned
 *                  with it will also be closed and become invalid, may be `NULL`
 */
void libgamepad_destroy_attachment_monitor(LIBGAMEPAD_ATTACHMENT_MONITOR *);

/**
 * Get the next device attachment event
 * 
 * @param   monitor   Monitor created with `libgamepad_create_attachment_monitor`
 * @param   syspathp  Pointer to output buffer for the device's path in /sys;
 *                    the buffer may be reallocated by the function
 * @param   sizep     Pointer to the allocation size of `*syspath`; may be
 *                    updated by the function
 * @param   typep     Output parameter for the attachment event type; in the
 *                    event that this value is set to `LIBGAMEPAD_REMOVED`, the
 *                    device is not necessarily on supported by this library
 * @return            1 on success, 0 if the received event was suppressed
 *                    by the library, -1 on failure
 * 
 * This function may fail for any reason specified for
 * realloc(3) or udev_monitor_receive_device(3); notable,
 * this function may set `errno` to `EAGAIN` or `EINTR`
 */
int libgamepad_get_attachment_event(LIBGAMEPAD_ATTACHMENT_MONITOR *, char **, size_t *, enum libgamepad_attachment_event_type *);


/**
 * Open a subdevice
 * 
 * @param   devicep  Output parameter for the device information and handles;
 *                   deallocated with `libgamepad_close_device`
 * @param   dirfd    File descriptor to path that `path` is relative to
 *                   (unless it is an absolute path), or `AT_FDCWD` for the
 *                   current working directory
 * @param   path     The path to the device, if `NULL` or empty, `dirfd`
 *                   will be used as the file descriptor to the device,
 *                   in which case the application must keep it open until
 *                   the device is closed, and then close it manually, and
 *                   `mode` is ignored
 * @param   mode     Mode to open the device in, normally `O_RDONLY`,
 *                   `O_RDONLY|O_NONBLOCK`, `O_RDWR`, or `O_RDWR|O_NONBLOCK`
 * @return           0 on success, -1 on failure
 * 
 * This function may fail for any reason specified for
 * malloc(3), openat(3) or libevdev_new_from_fd(3)
 */
int libgamepad_open_device(struct libgamepad_device *, int, const char *, int);

/**
 * Close a subdevice
 * 
 * @param  device  Device information and handles, may be `NULL`
 */
void libgamepad_close_device(struct libgamepad_device *);

/**
 * Get the next event on a subdevice
 * 
 * NB! events may be queued internally (by libevdev), therefore
 * an application must either call this function in a loop in
 * its own thread, or have the device opened in non-blocking
 * mode and even the poll the device's file descriptor for
 * read-readyness and when it is ready clal this function in
 * a loop until it sets `errno` to `EAGAIN`.
 * 
 * @param   device  Device to read an event from
 * @param   eventp  Output parameter for the event
 * @return          1 if an event was returned,
 *                  0 if no event was returned,
 *                  -1 on failure
 * 
 * This function may fail for any reason specified for
 * libevdev_next_event(3), including:
 * -  EAGAIN  no more data currently available (non-blocking mode)
 * -  EINTR   system call was interrupted, try again
 * -  ENODEV  device has been unplugged
 */
int libgamepad_next_event(struct libgamepad_device *, struct libgamepad_input_event *);


/**
 * Get the name of a button/key
 * 
 * @param   code  The button/key
 * @return        The button/key's name
 */
inline const char *
libgamepad_get_button_name(unsigned int code)
{
	return libevdev_event_code_get_name(EV_KEY, code);
}

/**
 * Get the name of an absolute axis
 * 
 * @param   code  The axis
 * @return        The axis' name
 */
inline const char *
libgamepad_get_absolute_axis_name(unsigned int code)
{
	return libevdev_event_code_get_name(EV_ABS, code);
}

/**
 * Get the name of a relative axis
 * 
 * @param   code  The axis
 * @return        The axis' name
 */
inline const char *
libgamepad_get_relative_axis_name(unsigned int code)
{
	return libevdev_event_code_get_name(EV_REL, code);
}

/**
 * Get whether a button/key is pressed down or not
 *
 * @param   device  The device to retrieve the information for
 * @param   code    The button/key
 * @return          1 if the button/key is pressed down,
 *                  0 otherwise
 */
inline int
libgamepad_get_button_is_pressed(struct libgamepad_device *device, unsigned int code)
{
	return libevdev_get_event_value(device->dev, EV_KEY, code);
}

/**
 * Get information about an absolute axis
 *
 * @param   device  The device to retrieve the information for
 * @param   code    The axis
 * @return          Information about the axis
 */
inline const struct input_absinfo * /* `struct input_absinfo` is defined in <linux/input.h> */
libgamepad_get_absolute_axis_info(struct libgamepad_device *device, unsigned int code)
{
	return libevdev_get_abs_info(device->dev, code);
}


/**
 * Grab a subdevice, if not already grabbed
 *
 * This is supposed to block out clients from
 * retrieving events for the device, but it
 * does not necessarily stop them from reading
 * the device state
 * 
 * @param   device  The device to grab
 * @return          0 on success, -1 on failure
 * 
 * May fail for any reason specified for libevdev_grab(3)
 * with LIBEVDEV_GRAB applied
 * 
 * In the event that the device is already grabbed by
 * via another file descriptor (duplicates do not share
 * grab), this function will return -1 and set `errno`
 * to `EBUSY`
 */
inline int
libgamepad_grab(struct libgamepad_device *device)
{
	int err = libevdev_grab(device->dev, LIBEVDEV_GRAB);
	if (err < 0) {
		errno = -err;
		return -1;
	}
	return 0;
}

/**
 * Ungrab a subdevice, unless not currently grabbed
 * 
 * @param   device  The device to ungrab
 * @return          0 on success, -1 on failure
 * 
 * May fail for any reason specified for libevdev_grab(3)
 * with LIBEVDEV_UNGRAB applied
 */
inline int
libgamepad_ungrab(struct libgamepad_device *device)
{
	int err = libevdev_grab(device->dev, LIBEVDEV_UNGRAB);
	if (err < 0) {
		errno = -err;
		return -1;
	}
	return 0;
}


/**
 * Set the clock that event timestamps shall be reported in
 * 
 * @param   device   The device to configure
 * @param   clockid  The clock to use on event timestamps
 * @return           0 on success, -1 on failure
 * 
 * May fail for any reason specified for libevdev_set_clock_id(3)
 */
inline int
libgamepad_set_clock(struct libgamepad_device *device, clockid_t clockid)
{
	int err = libevdev_set_clock_id(device->dev, clockid);
	if (err < 0) {
		errno = -err;
		return -1;
	}
	return 0;
}


#endif