summaryrefslogtreecommitdiff
path: root/platform/x11/joypad_linux.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/x11/joypad_linux.cpp')
-rw-r--r--platform/x11/joypad_linux.cpp156
1 files changed, 78 insertions, 78 deletions
diff --git a/platform/x11/joypad_linux.cpp b/platform/x11/joypad_linux.cpp
index bdbd2912a6..62ece58f58 100644
--- a/platform/x11/joypad_linux.cpp
+++ b/platform/x11/joypad_linux.cpp
@@ -31,21 +31,21 @@
#include "joypad_linux.h"
+#include <errno.h>
+#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
#ifdef UDEV_ENABLED
#include <libudev.h>
#endif
-#define LONG_BITS (sizeof(long) * 8)
-#define test_bit(nr, addr) (((1UL << ((nr) % LONG_BITS)) & ((addr)[(nr) / LONG_BITS])) != 0)
-#define NBITS(x) ((((x)-1)/LONG_BITS)+1)
+#define LONG_BITS (sizeof(long) * 8)
+#define test_bit(nr, addr) (((1UL << ((nr) % LONG_BITS)) & ((addr)[(nr) / LONG_BITS])) != 0)
+#define NBITS(x) ((((x)-1) / LONG_BITS) + 1)
#ifdef UDEV_ENABLED
-static const char* ignore_str = "/dev/input/js";
+static const char *ignore_str = "/dev/input/js";
#endif
JoypadLinux::Joypad::Joypad() {
@@ -67,20 +67,19 @@ JoypadLinux::Joypad::~Joypad() {
}
void JoypadLinux::Joypad::reset() {
- dpad = 0;
- fd = -1;
+ dpad = 0;
+ fd = -1;
InputDefault::JoyAxis jx;
jx.min = -1;
jx.value = 0.0f;
- for (int i=0; i < MAX_ABS; i++) {
+ for (int i = 0; i < MAX_ABS; i++) {
abs_map[i] = -1;
curr_axis[i] = jx;
}
}
-JoypadLinux::JoypadLinux(InputDefault *in)
-{
+JoypadLinux::JoypadLinux(InputDefault *in) {
exit_udev = false;
input = in;
joy_mutex = Mutex::create();
@@ -98,7 +97,7 @@ JoypadLinux::~JoypadLinux() {
void JoypadLinux::joy_thread_func(void *p_user) {
if (p_user) {
- JoypadLinux* joy = (JoypadLinux*) p_user;
+ JoypadLinux *joy = (JoypadLinux *)p_user;
joy->run_joypad_thread();
}
return;
@@ -124,16 +123,16 @@ void JoypadLinux::enumerate_joypads(udev *p_udev) {
udev_device *dev;
enumerate = udev_enumerate_new(p_udev);
- udev_enumerate_add_match_subsystem(enumerate,"input");
+ udev_enumerate_add_match_subsystem(enumerate, "input");
udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYPAD", "1");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices) {
- const char* path = udev_list_entry_get_name(dev_list_entry);
+ const char *path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(p_udev, path);
- const char* devnode = udev_device_get_devnode(dev);
+ const char *devnode = udev_device_get_devnode(dev);
if (devnode) {
@@ -168,7 +167,7 @@ void JoypadLinux::monitor_joypads(udev *p_udev) {
tv.tv_sec = 0;
tv.tv_usec = 0;
- ret = select(fd+1, &fds, NULL, NULL, &tv);
+ ret = select(fd + 1, &fds, NULL, NULL, &tv);
/* Check if our file descriptor has received data. */
if (ret > 0 && FD_ISSET(fd, &fds)) {
@@ -180,7 +179,7 @@ void JoypadLinux::monitor_joypads(udev *p_udev) {
joy_mutex->lock();
String action = udev_device_get_action(dev);
- const char* devnode = udev_device_get_devnode(dev);
+ const char *devnode = udev_device_get_devnode(dev);
if (devnode) {
String devnode_str = devnode;
@@ -232,13 +231,13 @@ int JoypadLinux::get_joy_from_path(String p_path) const {
void JoypadLinux::close_joypad(int p_id) {
if (p_id == -1) {
- for (int i=0; i<JOYPADS_MAX; i++) {
+ for (int i = 0; i < JOYPADS_MAX; i++) {
close_joypad(i);
};
return;
- }
- else if (p_id < 0) return;
+ } else if (p_id < 0)
+ return;
Joypad &joy = joypads[p_id];
@@ -253,11 +252,11 @@ void JoypadLinux::close_joypad(int p_id) {
static String _hex_str(uint8_t p_byte) {
- static const char* dict = "0123456789abcdef";
+ static const char *dict = "0123456789abcdef";
char ret[3];
ret[2] = 0;
- ret[0] = dict[p_byte>>4];
+ ret[0] = dict[p_byte >> 4];
ret[1] = dict[p_byte & 0xF];
return ret;
@@ -265,7 +264,7 @@ static String _hex_str(uint8_t p_byte) {
void JoypadLinux::setup_joypad_properties(int p_id) {
- Joypad* joy = &joypads[p_id];
+ Joypad *joy = &joypads[p_id];
unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
@@ -274,7 +273,7 @@ void JoypadLinux::setup_joypad_properties(int p_id) {
int num_axes = 0;
if ((ioctl(joy->fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
- (ioctl(joy->fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
+ (ioctl(joy->fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
return;
}
for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
@@ -332,8 +331,8 @@ void JoypadLinux::open_joypad(const char *p_path) {
attached_devices.push_back(String(p_path));
if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
- (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
- (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
+ (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
+ (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
close(fd);
return;
}
@@ -341,10 +340,10 @@ void JoypadLinux::open_joypad(const char *p_path) {
//check if the device supports basic gamepad events, prevents certain keyboards from
//being detected as joypads
if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
- (test_bit(ABS_X, absbit) || test_bit(ABS_Y, absbit) || test_bit(ABS_HAT0X, absbit) ||
- test_bit(ABS_GAS, absbit) || test_bit(ABS_RUDDER, absbit)) &&
- (test_bit(BTN_A, keybit) || test_bit(BTN_THUMBL, keybit) ||
- test_bit(BTN_TRIGGER, keybit) || test_bit(BTN_1, keybit)))) {
+ (test_bit(ABS_X, absbit) || test_bit(ABS_Y, absbit) || test_bit(ABS_HAT0X, absbit) ||
+ test_bit(ABS_GAS, absbit) || test_bit(ABS_RUDDER, absbit)) &&
+ (test_bit(BTN_A, keybit) || test_bit(BTN_THUMBL, keybit) ||
+ test_bit(BTN_TRIGGER, keybit) || test_bit(BTN_1, keybit)))) {
close(fd);
return;
}
@@ -375,13 +374,12 @@ void JoypadLinux::open_joypad(const char *p_path) {
uint16_t product = __bswap_16(inpid.product);
uint16_t version = __bswap_16(inpid.version);
- sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor,0,product,0,version,0);
+ sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor, 0, product, 0, version, 0);
input->joy_connection_changed(joy_num, true, name, uid);
- }
- else {
+ } else {
String uidname = uid;
int uidlen = MIN(name.length(), 11);
- for (int i=0; i<uidlen; i++) {
+ for (int i = 0; i < uidlen; i++) {
uidname = uidname + _hex_str(name[i]);
}
@@ -391,9 +389,8 @@ void JoypadLinux::open_joypad(const char *p_path) {
}
}
-void JoypadLinux::joypad_vibration_start(int p_id, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp)
-{
- Joypad& joy = joypads[p_id];
+void JoypadLinux::joypad_vibration_start(int p_id, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
+ Joypad &joy = joypads[p_id];
if (!joy.force_feedback || joy.fd == -1 || p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
return;
}
@@ -417,15 +414,14 @@ void JoypadLinux::joypad_vibration_start(int p_id, float p_weak_magnitude, float
play.type = EV_FF;
play.code = effect.id;
play.value = 1;
- write(joy.fd, (const void*)&play, sizeof(play));
+ write(joy.fd, (const void *)&play, sizeof(play));
joy.ff_effect_id = effect.id;
joy.ff_effect_timestamp = p_timestamp;
}
-void JoypadLinux::joypad_vibration_stop(int p_id, uint64_t p_timestamp)
-{
- Joypad& joy = joypads[p_id];
+void JoypadLinux::joypad_vibration_stop(int p_id, uint64_t p_timestamp) {
+ Joypad &joy = joypads[p_id];
if (!joy.force_feedback || joy.fd == -1 || joy.ff_effect_id == -1) {
return;
}
@@ -447,13 +443,13 @@ InputDefault::JoyAxis JoypadLinux::axis_correct(const input_absinfo *p_abs, int
if (min < 0) {
jx.min = -1;
if (p_value < 0) {
- jx.value = (float) -p_value / min;
+ jx.value = (float)-p_value / min;
}
- jx.value = (float) p_value / max;
+ jx.value = (float)p_value / max;
}
if (min == 0) {
jx.min = 0;
- jx.value = 0.0f + (float) p_value / max;
+ jx.value = 0.0f + (float)p_value / max;
}
return jx;
}
@@ -463,12 +459,12 @@ uint32_t JoypadLinux::process_joypads(uint32_t p_event_id) {
if (joy_mutex->try_lock() != OK) {
return p_event_id;
}
- for (int i=0; i<JOYPADS_MAX; i++) {
+ for (int i = 0; i < JOYPADS_MAX; i++) {
if (joypads[i].fd == -1) continue;
input_event events[32];
- Joypad* joy = &joypads[i];
+ Joypad *joy = &joypads[i];
int len;
@@ -480,45 +476,49 @@ uint32_t JoypadLinux::process_joypads(uint32_t p_event_id) {
// ev may be tainted and out of MAX_KEY range, which will cause
// joy->key_map[ev.code] to crash
- if( ev.code < 0 || ev.code >= MAX_KEY )
+ if (ev.code < 0 || ev.code >= MAX_KEY)
return p_event_id;
switch (ev.type) {
- case EV_KEY:
- p_event_id = input->joy_button(p_event_id, i, joy->key_map[ev.code], ev.value);
- break;
-
- case EV_ABS:
-
- switch (ev.code) {
- case ABS_HAT0X:
- if (ev.value != 0) {
- if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_LEFT;
- else joy->dpad |= InputDefault::HAT_MASK_RIGHT;
- }
- else joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT);
-
- p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
+ case EV_KEY:
+ p_event_id = input->joy_button(p_event_id, i, joy->key_map[ev.code], ev.value);
break;
- case ABS_HAT0Y:
- if (ev.value != 0) {
- if (ev.value < 0) joy->dpad |= InputDefault::HAT_MASK_UP;
- else joy->dpad |= InputDefault::HAT_MASK_DOWN;
+ case EV_ABS:
+
+ switch (ev.code) {
+ case ABS_HAT0X:
+ if (ev.value != 0) {
+ if (ev.value < 0)
+ joy->dpad |= InputDefault::HAT_MASK_LEFT;
+ else
+ joy->dpad |= InputDefault::HAT_MASK_RIGHT;
+ } else
+ joy->dpad &= ~(InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_RIGHT);
+
+ p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
+ break;
+
+ case ABS_HAT0Y:
+ if (ev.value != 0) {
+ if (ev.value < 0)
+ joy->dpad |= InputDefault::HAT_MASK_UP;
+ else
+ joy->dpad |= InputDefault::HAT_MASK_DOWN;
+ } else
+ joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN);
+
+ p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
+ break;
+
+ default:
+ if (joy->abs_map[ev.code] != -1 && joy->abs_info[ev.code]) {
+ InputDefault::JoyAxis value = axis_correct(joy->abs_info[ev.code], ev.value);
+ joy->curr_axis[joy->abs_map[ev.code]] = value;
+ }
+ break;
}
- else joy->dpad &= ~(InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_DOWN);
-
- p_event_id = input->joy_hat(p_event_id, i, joy->dpad);
break;
-
- default:
- if (joy->abs_map[ev.code] != -1 && joy->abs_info[ev.code]) {
- InputDefault::JoyAxis value = axis_correct(joy->abs_info[ev.code], ev.value);
- joy->curr_axis[joy->abs_map[ev.code]] = value;
- }
- break;
- }
- break;
}
}
}