diff --git a/src/linux_uinput.cpp b/src/linux_uinput.cpp index 678afbc..2446b45 100644 --- a/src/linux_uinput.cpp +++ b/src/linux_uinput.cpp @@ -32,7 +32,8 @@ #include "force_feedback_handler.hpp" #include "linux_uinput.hpp" -LinuxUinput::LinuxUinput(const std::string& name_, uint16_t vendor_, uint16_t product_) : +LinuxUinput::LinuxUinput(DeviceType device_type, const std::string& name_, uint16_t vendor_, uint16_t product_) : + m_device_type(device_type), name(name_), vendor(vendor_), product(product_), @@ -183,6 +184,37 @@ LinuxUinput::set_ff_callback(const boost::function& cal void LinuxUinput::finish() { + // Create some mandatory events that are needed for the kernel/Xorg + // to register the device as its proper type + switch(m_device_type) + { + case kGenericDevice: + // nothing to be done + break; + + case kMouseDevice: + add_rel(REL_X); + add_rel(REL_Y); + add_key(BTN_LEFT); + break; + + case kJoystickDevice: + if (!key_lst[BTN_JOYSTICK] && + !key_lst[BTN_GAMEPAD] && + !key_lst[BTN_TRIGGER_HAPPY] && + + !abs_lst[ABS_THROTTLE] && + !abs_lst[ABS_WHEEL] && + !abs_lst[ABS_X]) + { + // using BTN_TRIGGER_HAPPY instead of more common BTN_JOYSTICK + // as it should end up as last joystick button instead of + // first + add_key(BTN_TRIGGER_HAPPY); + } + break; + } + strncpy(user_dev.name, name.c_str(), UINPUT_MAX_NAME_SIZE); user_dev.id.version = 0x114; // FIXME: whats this for? user_dev.id.bustype = BUS_USB; diff --git a/src/linux_uinput.hpp b/src/linux_uinput.hpp index 2171e8f..b60e2cb 100644 --- a/src/linux_uinput.hpp +++ b/src/linux_uinput.hpp @@ -28,7 +28,11 @@ class ForceFeedbackHandler; class LinuxUinput { +public: + enum DeviceType { kGenericDevice, kMouseDevice, kJoystickDevice }; + private: + DeviceType m_device_type; std::string name; uint16_t vendor; uint16_t product; @@ -52,7 +56,7 @@ private: bool needs_sync; public: - LinuxUinput(const std::string& name, uint16_t vendor, uint16_t product); + LinuxUinput(DeviceType device_type, const std::string& name, uint16_t vendor, uint16_t product); ~LinuxUinput(); /*@{*/ diff --git a/src/uinput.cpp b/src/uinput.cpp index f7784fd..1008efb 100644 --- a/src/uinput.cpp +++ b/src/uinput.cpp @@ -99,20 +99,23 @@ uInput::create_uinput_device(int device_id) } else { + LinuxUinput::DeviceType device_type = LinuxUinput::kGenericDevice; std::ostringstream dev_name; dev_name << cfg.device_name; switch (device_id) { case DEVICEID_JOYSTICK: - dev_name << " - Mouse Emulation"; + device_type = LinuxUinput::kJoystickDevice; break; case DEVICEID_MOUSE: + device_type = LinuxUinput::kMouseDevice; dev_name << " - Mouse Emulation"; break; case DEVICEID_KEYBOARD: + device_type = LinuxUinput::kGenericDevice; dev_name << " - Keyboard Emulation"; break; @@ -121,32 +124,9 @@ uInput::create_uinput_device(int device_id) break; } - boost::shared_ptr dev(new LinuxUinput(dev_name.str(), m_vendor_id, m_product_id)); + boost::shared_ptr dev(new LinuxUinput(device_type, dev_name.str(), m_vendor_id, m_product_id)); uinput_devs.insert(std::pair >(device_id, dev)); - // Create some mandatory events that are needed for the kernel/Xorg - // to register the device as its proper type - switch(device_id) - { - case DEVICEID_KEYBOARD: - // do nothing, detection seems to be fine without - break; - - case DEVICEID_MOUSE: - dev->add_rel(REL_X); - dev->add_rel(REL_Y); - dev->add_key(BTN_LEFT); - break; - - case DEVICEID_JOYSTICK: - // FIXME: do nothing, as we don't know yet the range of abs, - // unmapped buttons might also confuse some games - break; - - default: - break; - } - std::cout << "Creating uinput device: device_id: " << device_id << ", dev_name: " << dev_name.str() << std::endl; } }