From e4e7e092c225dc4c6767ad7267bf2b668357009d Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Wed, 19 Aug 2015 03:18:12 +0200 Subject: [PATCH] linux-dist/evdev: Add controller axis value autoconversion --- core/linux-dist/evdev.cpp | 58 +++++++++++++++++++++++++++------------ core/linux-dist/evdev.h | 18 ++++++++++-- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/core/linux-dist/evdev.cpp b/core/linux-dist/evdev.cpp index 326bd2c0a..fb76fbf98 100644 --- a/core/linux-dist/evdev.cpp +++ b/core/linux-dist/evdev.cpp @@ -8,6 +8,39 @@ #include #if defined(USE_EVDEV) + s8 AxisData::convert(s32 value) + { + return (((value - min) * 255) / range); + } + + void AxisData::init(int fd, int code) + { + struct input_absinfo abs; + if(code < 0 || ioctl(fd, EVIOCGABS(code), &abs)) + { + if(code >= 0) + { + perror("evdev ioctl"); + } + this->range = 255; + this->min = 0; + return; + } + s32 min = abs.minimum; + s32 max = abs.maximum; + printf("evdev: range of axis %d is from %d to %d\n", code, min, max); + this->range = (max - min); + this->min = min; + } + + void Controller::init() + { + this->data_x.init(this->fd, this->mapping->Axis_Analog_X); + this->data_y.init(this->fd, this->mapping->Axis_Analog_Y); + this->data_trigger_left.init(this->fd, this->mapping->Axis_Trigger_Left); + this->data_trigger_right.init(this->fd, this->mapping->Axis_Trigger_Right); + } + std::map loaded_mappings; int load_keycode(ConfigFile* cfg, string section, string dc_key) @@ -144,6 +177,7 @@ printf("evdev: reading mapping file: '%s'\n", mapping_fname); loaded_mappings.insert(std::make_pair(string(mapping_fname), load_mapping(mapping_fd))); fclose(mapping_fd); + } else { @@ -153,6 +187,8 @@ } controller->mapping = &loaded_mappings[string(mapping_fname)]; printf("evdev: Using '%s' mapping\n", controller->mapping->name); + controller->init(); + return 0; } } @@ -175,10 +211,6 @@ while(read(controller->fd, &ie, sizeof(ie)) == sizeof(ie)) { - if(ie.type != EV_SYN && ie.type != EV_MSC) - { - printf("type %i key %i state %i\n", ie.type, ie.code, ie.value); - } switch(ie.type) { case EV_KEY: @@ -297,32 +329,22 @@ } else if (ie.code == controller->mapping->Axis_Analog_X) { - printf("%d", ie.value); - joyx[port] = (s8)(ie.value/256); + joyx[port] = (controller->data_x.convert(ie.value) + 128); } else if (ie.code == controller->mapping->Axis_Analog_Y) { - joyy[port] = (s8)(ie.value/256); + joyy[port] = (controller->data_y.convert(ie.value) + 128); } else if (ie.code == controller->mapping->Axis_Trigger_Left) { - lt[port] = (s8)ie.value; + lt[port] = controller->data_trigger_left.convert(ie.value); } else if (ie.code == controller->mapping->Axis_Trigger_Right) { - rt[port] = (s8)ie.value; + rt[port] = controller->data_trigger_right.convert(ie.value); } break; } } } #endif - - - - - - - - - diff --git a/core/linux-dist/evdev.h b/core/linux-dist/evdev.h index 6dc2ccd75..4351c8c18 100644 --- a/core/linux-dist/evdev.h +++ b/core/linux-dist/evdev.h @@ -5,14 +5,25 @@ #pragma once -struct s_controller +struct AxisData +{ + s32 range; // smaller size than 32 bit might cause integer overflows + s32 min; + void init(int fd, int code); + s8 convert(int value); +}; + +struct Controller { int fd; ControllerMapping* mapping; + AxisData data_x; + AxisData data_y; + AxisData data_trigger_left; + AxisData data_trigger_right; + void init(); }; -typedef struct s_controller Controller; - #define EVDEV_DEVICE_CONFIG_KEY "evdev_device_id_%d" #define EVDEV_MAPPING_CONFIG_KEY "evdev_mapping_%d" #define EVDEV_DEVICE_STRING "/dev/input/event%d" @@ -30,3 +41,4 @@ extern int input_evdev_init(Controller* controller, const char* device, const ch extern bool input_evdev_handle(Controller* controller, u32 port); +