aboutsummaryrefslogtreecommitdiffstats
path: root/libgamepad.h
blob: 5b6c46c0633f1199ef8375d686ab0052e014df64 (plain) (blame)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
/* See LICENSE file for copyright and license details. */
#ifndef LIBGAMEPAD_H
#define LIBGAMEPAD_H

#include <linux/input.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <time.h>
#include <unistd.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>
	 */
	unsigned int bus_type;

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

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

	/**
	 * Product version ID for the device (sub- or superdevice)
	 */
	unsigned 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;

	/**
	 * 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;

	/**
	 * 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];

	/**
	 * Bitmap of supported force feedback effects
	 */
	uint8_t force_feedback_support[(FF_CNT + 7) / 8];
};


/* FOR INTERNAL USE */
void libgamepad_construct_force_feedback_effect__(struct ff_effect *, const struct ff_effect *, double, uint16_t, uint16_t);


/**
 * 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 or access has been revoked
 */
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(uint16_t code)
{
	return libevdev_event_code_get_name(EV_KEY, (unsigned int)code);
}

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

/**
 * Get the name of a relative axis
 * 
 * @param   code  The axis
 * @return        The axis' name
 */
inline const char *
libgamepad_get_relative_axis_name(uint16_t code)
{
	return libevdev_event_code_get_name(EV_REL, (unsigned int)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, uint16_t code)
{
	return libevdev_get_event_value(device->dev, EV_KEY, (unsigned int)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, uint16_t code)
{
	return libevdev_get_abs_info(device->dev, (unsigned int)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
 * 
 * In the event that the device is already grabbed by
 * via another open file descriptor (duplicates share
 * grab), this function will return -1 and set `errno`
 * to `EBUSY`
 */
inline int
libgamepad_grab(struct libgamepad_device *device)
{
	return ioctl(device->fd, EVIOCGRAB, (void *)1);
}

/**
 * Ungrab a subdevice, unless not currently grabbed
 * 
 * @param   device  The device to ungrab
 * @return          0 on success, -1 on failure
 */
inline int
libgamepad_ungrab(struct libgamepad_device *device)
{
	return ioctl(device->fd, EVIOCGRAB, (void *)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
 */
inline int
libgamepad_set_clock(struct libgamepad_device *device, clockid_t clockid)
{
	return ioctl(device->fd, EVIOCSCLOCKID, &clockid);
}


/**
 * Install a force feedback effect that can be played back later
 * 
 * @param   device  The device to configure
 * @param   effect  The effect to install; will be edited, specifically the ID
 *                  will be set (need not be set before calling this function)
 * @return          0 on success, -1 on failure
 */
inline int /* `struct ff_effect` is defined in <linux/input.h> */
libgamepad_install_force_feedback_effect(struct libgamepad_device *device, struct ff_effect *effect)
{
	effect->id = -1;
	return ioctl(device->fd, EVIOCSFF, effect);
}

/**
 * Reconfigure an already installed force feedback effect
 * 
 * @param   device  The device to configure
 * @param   effect  The effect to update, the ID must be set to the ID of a
 *                  previously installed effect
 * @return          0 on success, -1 on failure
 */
inline int
libgamepad_update_force_feedback_effect(struct libgamepad_device *device, struct ff_effect *effect)
{
	return ioctl(device->fd, EVIOCSFF, effect);
}

/**
 * Uninstall an installed force feedback effect
 * 
 * @param   device  The device to configure
 * @param   effect  The effect to remove, the ID must be set to the ID of a
 *                  previously installed effect and will be reset
 * @return          0 on success, -1 on failure
 */
inline int
libgamepad_uninstall_force_feedback_effect(struct libgamepad_device *device, struct ff_effect *effect)
{
	int ret = ioctl(device->fd, EVIOCRMFF, effect);
	effect->id = -1;
	return ret;
}

/**
 * Get the number of force feedback effects that can be played concurrently
 * 
 * @param   device  The device to configure
 * @return          The number of maximum number concurrent effects, -1 on failure
 */
inline int
libgamepad_get_force_feedback_max_concurrency(struct libgamepad_device *device)
{
	int num;
	return ioctl(device->fd, EVIOCGEFFECTS, &num) ? -1 : num;
}

/**
 * Play a force feedback effect
 * 
 * @param   device  The device to play the force feedback effect on
 * @param   effect  The force feedback effect to play, must already be installed
 *                  with `libgamepad_install_force_feedback_effect`
 * @return          0 on success, -1 on failure
 */
inline int
libgamepad_play_force_feedback_effect(struct libgamepad_device *device, const struct ff_effect *effect)
{
	struct input_event message;
	message.type = EV_FF;
	message.code = (uint16_t)effect->id;
	message.value = 1;
	return write(device->fd, &message, sizeof(message)) < 0 ? -1 : 0;
}

/**
 * Stop playing a force feedback effect
 * 
 * @param   device  The device playing the force feedback effect on
 * @param   effect  The force feedback effect to play, must already be installed
 *                  with `libgamepad_install_force_feedback_effect`
 * @return          0 on success, -1 on failure
 */
inline int
libgamepad_stop_force_feedback_effect(struct libgamepad_device *device, const struct ff_effect *effect)
{
	struct input_event message;
	message.type = EV_FF;
	message.code = (uint16_t)effect->id;
	message.value = 0;
	return write(device->fd, &message, sizeof(message)) < 0 ? -1 : 0;
}

/**
 * Check if a force feedback effect or waveform is supported
 *
 * @param   device  The device to check force feedback support on
 * @param   effect  The force feedback effect or waveform
 * @return          1 if the effect/waveform is supported, 0 otherwise
 */
inline int
libgamepad_is_force_feedback_effect_supported(struct libgamepad_device *device, uint16_t effect)
{
	return effect < sizeof(device->force_feedback_support) * 8 &&
	       ((device->force_feedback_support[effect / 8] >> (effect % 8)) & 1);
}

/**
 * Set the force feedback master gain on a device
 * 
 * Requires support for `FF_GAIN` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * @param   device  The device to configure
 * @param   gain    The master gain, shall be a value in [0, 0xFFFF]
 * @return          0 on success, -1 on failure
 */
inline int
libgamepad_set_force_feedback_master_gain(struct libgamepad_device *device, uint16_t gain)
{
	struct input_event message;
	message.type = EV_FF;
	message.code = FF_GAIN;
	message.value = (int16_t)gain;
	return write(device->fd, &message, sizeof(message)) < 0 ? -1 : 0;
}

/**
 * Set the autocenter force feedback on a device
 * 
 * Requires support for `FF_AUTOCENTER` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * @param   device      The device to configure
 * @param   autocenter  Autocenter strength, shall be a value in [0, 0xFFFF], select 0 to
 *                      disable (you can also use `libgamepad_disable_force_feedback_autocenter`)
 * @return              0 on success, -1 on failure
 */
inline int
libgamepad_set_force_feedback_autocenter(struct libgamepad_device *device, uint16_t autocenter)
{
	struct input_event message;
	message.type = EV_FF;
	message.code = FF_AUTOCENTER;
	message.value = (int16_t)autocenter;
	return write(device->fd, &message, sizeof(message)) < 0 ? -1 : 0;
}

/**
 * Disable the autocenter force feedback on a device
 * 
 * Requires support for `FF_AUTOCENTER` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * @param   device  device to configure
 * @return          0 on success, -1 on failure
 */
inline int
libgamepad_disable_force_feedback_autocenter(struct libgamepad_device *device)
{
	struct input_event message;
	message.type = EV_FF;
	message.code = FF_AUTOCENTER;
	message.value = 0;
	return write(device->fd, &message, sizeof(message)) < 0 ? -1 : 0;
}

/**
 * Construct a rumble force-feedback effect
 * 
 * Requires support for `FF_RUMBLE` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * This function will set the following values to
 * zero if `from` is `NULL`, see <linux/input.h>
 * for more details:
 * -  effectp->trigger.button
 * -  effectp->trigger.interval
 * -  effectp->replay.delay
 * 
 * @param  effectp    Output parameter for the effect
 * @param  from       Effect to reconfigure, `NULL` to create a new effect
 * @param  direction  The direction of the force feedback (if directional), where
 *                    values are measure clockwise and both 0 and 1 are upwards
 * @param  length     The number of milliseconds the effect is played
 * @param  effect     Effect specific details:
 *                    - .strong_magnitude  Heavy motor strength, unsigned 16-bit integer
 *                    - .weak_magnitude    Light motor strength, unsigned 16-bit integer
 */
inline void
libgamepad_construct_rumble_force_feedback_effect(struct ff_effect *effectp, const struct ff_effect *from,
                                                  double direction, uint16_t length, const struct ff_rumble_effect *effect)
{
	libgamepad_construct_force_feedback_effect__(effectp, from, direction, length, FF_RUMBLE);
	memcpy(&effectp->u.rumble, effect, sizeof(effectp->u.rumble));
}

/**
 * Construct a constant force-feedback effect
 * 
 * Requires support for `FF_CONSTANT` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * This function will set the following values to
 * zero if `from` is `NULL`, see <linux/input.h>
 * for more details:
 * -  effectp->trigger.button
 * -  effectp->trigger.interval
 * -  effectp->replay.delay
 * 
 * @param  effectp    Output parameter for the effect
 * @param  from       Effect to reconfigure, `NULL` to create a new effect
 * @param  direction  The direction of the force feedback (if directional), where
 *                    values are measure clockwise and both 0 and 1 are upwards
 * @param  length     The number of milliseconds the effect is played
 * @param  effect     Effect specific details:
 *                    - .level                     Strength, signed 16-bit integer
 *                    - .envelope.attack_strength  Strength at beginning, unsigned 16-bit integer
 *                    - .envelope.fade_strength    Strength at end, unsigned 16-bit integer
 *                    - .envelope.attack_length    The number of milliseconds the strength shall
 *                                                 transition from `.envelope.attack_strength`
 *                                                 to `.level`, unsigned 16-bit integer
 *                    - .envelope.fade_length      The number of milliseconds the strength shall
 *                                                 transition to `.envelope.fade_strength`
 *                                                 from `.level`, unsigned 16-bit integer
 */
inline void
libgamepad_construct_constant_force_feedback_effect(struct ff_effect *effectp, const struct ff_effect *from,
                                                    double direction, uint16_t length, const struct ff_constant_effect *effect)
{
	libgamepad_construct_force_feedback_effect__(effectp, from, direction, length, FF_CONSTANT);
	memcpy(&effectp->u.constant, effect, sizeof(effectp->u.constant));
}

/**
 * Construct a ramp force-feedback effect
 * 
 * Requires support for `FF_RAMP` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * This function will set the following values to
 * zero if `from` is `NULL`, see <linux/input.h>
 * for more details:
 * -  effectp->trigger.button
 * -  effectp->trigger.interval
 * -  effectp->replay.delay
 * 
 * @param  effectp    Output parameter for the effect
 * @param  from       Effect to reconfigure, `NULL` to create a new effect
 * @param  direction  The direction of the force feedback (if directional), where
 *                    values are measure clockwise and both 0 and 1 are upwards
 * @param  length     The number of milliseconds the effect is played
 * @param  effect     Effect specific details:
 *                    - .start_level               Strength at beginning, signed 16-bit integer
 *                    - .end_level                 Strength at end, signed 16-bit integer
 *                    - .envelope.attack_strength  Strength at beginning, unsigned 16-bit integer
 *                    - .envelope.fade_strength    Strength at end, unsigned 16-bit integer
 *                    - .envelope.attack_length    The number of milliseconds the strength shall
 *                                                 transition from `.envelope.attack_strength`,
 *                                                 unsigned 16-bit integer
 *                    - .envelope.fade_length      The number of milliseconds the strength shall
 *                                                 transition to `.envelope.fade_strength`,
 *                                                 unsigned 16-bit integer
 */
inline void
libgamepad_construct_ramp_force_feedback_effect(struct ff_effect *effectp, const struct ff_effect *from,
                                                double direction, uint16_t length, const struct ff_ramp_effect *effect)
{
	libgamepad_construct_force_feedback_effect__(effectp, from, direction, length, FF_RUMBLE);
	memcpy(&effectp->u.ramp, effect, sizeof(effectp->u.ramp));
}

/**
 * Construct a periodic force-feedback effect
 * 
 * Requires support for `FF_PERIODIC` and the selected waveform
 * (check with `libgamepad_is_force_feedback_effect_supported`)
 * 
 * This function will set the following values to
 * zero if `from` is `NULL`, see <linux/input.h>
 * for more details:
 * -  effectp->trigger.button
 * -  effectp->trigger.interval
 * -  effectp->replay.delay
 * 
 * @param  effectp    Output parameter for the effect
 * @param  from       Effect to reconfigure, `NULL` to create a new effect
 * @param  direction  The direction of the force feedback (if directional), where
 *                    values are measure clockwise and both 0 and 1 are upwards
 * @param  length     The number of milliseconds the effect is played
 * @param  effect     Effect specific details:
 *                    - .waveform                  `FF_SQUARE`, `FF_TRIANGLE`, `FF_SINE`, `FF_SAW_UP`,
 *                                                 `FF_SAW_DOWN`, or `FF_CUSTOM`
 *                    - .period                    The period of the wave, in milliseconds (unsigned 16-bit integer)
 *                    - .magnitude                 The peak value, signed 16-bit integer
 *                    - .offset                    The rough mean value, signed 16-bit integer
 *                    - .phase                     Horiztonal wave-shift, in milliseconds (unsigned 16-bit integer)
 *                    - .envelope.attack_strength  Strength at beginning, unsigned 16-bit integer
 *                    - .envelope.fade_strength    Strength at end, unsigned 16-bit integer
 *                    - .envelope.attack_length    The number of milliseconds the strength shall
 *                                                 transition from `.envelope.attack_strength`,
 *                                                 unsigned 16-bit integer
 *                    - .envelope.fade_length      The number of milliseconds the strength shall
 *                                                 transition to `.envelope.fade_strength`,
 *                                                 unsigned 16-bit integer
 *                    - .custom_len                Set to zero unless you are using (`FF_CUSTOM`)
 *                    - .custom_data               Set to `NULL` unless you are using (`FF_CUSTOM`)
 */
inline void
libgamepad_construct_periodic_force_feedback_effect(struct ff_effect *effectp, const struct ff_effect *from,
                                                   double direction, uint16_t length, const struct ff_periodic_effect *effect)
{
	libgamepad_construct_force_feedback_effect__(effectp, from, direction, length, FF_PERIODIC);
	memcpy(&effectp->u.periodic, effect, sizeof(effectp->u.periodic));
}

/**
 * Construct a spring force-feedback effect
 * 
 * Requires support for `FF_SPRING` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * This function will set the following values to
 * zero if `from` is `NULL`, see <linux/input.h>
 * for more details:
 * -  effectp->trigger.button
 * -  effectp->trigger.interval
 * -  effectp->replay.delay
 * 
 * @param  effectp    Output parameter for the effect
 * @param  from       Effect to reconfigure, `NULL` to create a new effect
 * @param  direction  The direction of the force feedback (if directional), where
 *                    values are measure clockwise and both 0 and 1 are upwards
 * @param  length     The number of milliseconds the effect is played
 * @param  effect     Effect specific details (one of each axis):
 *                    - .right_saturation  Maximum level at end of axis, unsigned 16-bit integer
 *                    - .left_saturation   Maximum level at beginning of axis, unsigned 16-bit integer
 *                    - .right_coeff       Growth coefficient after the dead zone, signed 16-bit integer
 *                    - .left_coeff        Growth coefficient before the dead zone, signed 16-bit integer
 *                    - .deadband          Size of the dead zone
 *                    - .center            The position of the center of the dead zone
 */
inline void
libgamepad_construct_spring_force_feedback_effect(struct ff_effect *effectp, const struct ff_effect *from,
                                                  double direction, uint16_t length, const struct ff_condition_effect effect[2])
{
	libgamepad_construct_force_feedback_effect__(effectp, from, direction, length, FF_SPRING);
	memcpy(effectp->u.condition, effect, sizeof(effectp->u.condition));
}

/**
 * Construct a friction force-feedback effect
 * 
 * Requires support for `FF_FRICTION` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * This function will set the following values to
 * zero if `from` is `NULL`, see <linux/input.h>
 * for more details:
 * -  effectp->trigger.button
 * -  effectp->trigger.interval
 * -  effectp->replay.delay
 * 
 * @param  effectp    Output parameter for the effect
 * @param  from       Effect to reconfigure, `NULL` to create a new effect
 * @param  direction  The direction of the force feedback (if directional), where
 *                    values are measure clockwise and both 0 and 1 are upwards
 * @param  length     The number of milliseconds the effect is played
 * @param  effect     Effect specific details (one of each axis):
 *                    - .right_saturation  Maximum level at end of axis, unsigned 16-bit integer
 *                    - .left_saturation   Maximum level at beginning of axis, unsigned 16-bit integer
 *                    - .right_coeff       Growth coefficient after the dead zone, signed 16-bit integer
 *                    - .left_coeff        Growth coefficient before the dead zone, signed 16-bit integer
 *                    - .deadband          Size of the dead zone
 *                    - .center            The position of the center of the dead zone
 */
inline void
libgamepad_construct_friction_force_feedback_effect(struct ff_effect *effectp, const struct ff_effect *from,
                                                    double direction, uint16_t length, const struct ff_condition_effect effect[2])
{
	libgamepad_construct_force_feedback_effect__(effectp, from, direction, length, FF_FRICTION);
	memcpy(effectp->u.condition, effect, sizeof(effectp->u.condition));
}

/**
 * Construct a damper force-feedback effect
 * 
 * Requires support for `FF_DAMPER` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * This function will set the following values to
 * zero if `from` is `NULL`, see <linux/input.h>
 * for more details:
 * -  effectp->trigger.button
 * -  effectp->trigger.interval
 * -  effectp->replay.delay
 * 
 * @param  effectp    Output parameter for the effect
 * @param  from       Effect to reconfigure, `NULL` to create a new effect
 * @param  direction  The direction of the force feedback (if directional), where
 *                    values are measure clockwise and both 0 and 1 are upwards
 * @param  length     The number of milliseconds the effect is played
 * @param  effect     Effect specific details (one of each axis):
 *                    - .right_saturation  Maximum level at end of axis, unsigned 16-bit integer
 *                    - .left_saturation   Maximum level at beginning of axis, unsigned 16-bit integer
 *                    - .right_coeff       Growth coefficient after the dead zone, signed 16-bit integer
 *                    - .left_coeff        Growth coefficient before the dead zone, signed 16-bit integer
 *                    - .deadband          Size of the dead zone
 *                    - .center            The position of the center of the dead zone
 */
inline void
libgamepad_construct_damper_force_feedback_effect(struct ff_effect *effectp, const struct ff_effect *from,
                                                  double direction, uint16_t length, const struct ff_condition_effect effect[2])
{
	libgamepad_construct_force_feedback_effect__(effectp, from, direction, length, FF_DAMPER);
	memcpy(effectp->u.condition, effect, sizeof(effectp->u.condition));
}

/**
 * Construct a inertia force-feedback effect
 * 
 * Requires support for `FF_INERTIA` (check with
 * `libgamepad_is_force_feedback_effect_supported`)
 * 
 * This function will set the following values to
 * zero if `from` is `NULL`, see <linux/input.h>
 * for more details:
 * -  effectp->trigger.button
 * -  effectp->trigger.interval
 * -  effectp->replay.delay
 * 
 * @param  effectp    Output parameter for the effect
 * @param  from       Effect to reconfigure, `NULL` to create a new effect
 * @param  direction  The direction of the force feedback (if directional), where
 *                    values are measure clockwise and both 0 and 1 are upwards
 * @param  length     The number of milliseconds the effect is played
 * @param  effect     Effect specific details (one of each axis):
 *                    - .right_saturation  Maximum level at end of axis, unsigned 16-bit integer
 *                    - .left_saturation   Maximum level at beginning of axis, unsigned 16-bit integer
 *                    - .right_coeff       Growth coefficient after the dead zone, signed 16-bit integer
 *                    - .left_coeff        Growth coefficient before the dead zone, signed 16-bit integer
 *                    - .deadband          Size of the dead zone
 *                    - .center            The position of the center of the dead zone
 */
inline void
libgamepad_construct_inertia_force_feedback_effect(struct ff_effect *effectp, const struct ff_effect *from,
                                                   double direction, uint16_t length, const struct ff_condition_effect effect[2])
{
	libgamepad_construct_force_feedback_effect__(effectp, from, direction, length, FF_INERTIA);
	memcpy(effectp->u.condition, effect, sizeof(effectp->u.condition));
}


#endif