From 60425efb991819697c510a0235766fc2a4d1ee63 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 26 Jan 2025 12:47:06 +0200 Subject: [PATCH] Add an API for other libraries and application to register device types --- source/input/device.cpp | 33 +++++++++++++++++++++++++++++++-- source/input/device.h | 4 +++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/source/input/device.cpp b/source/input/device.cpp index 22429a0..cecbe61 100644 --- a/source/input/device.cpp +++ b/source/input/device.cpp @@ -1,4 +1,6 @@ #include "device.h" +#include +#include #include using namespace std; @@ -84,6 +86,19 @@ void Device::set_axis_value(unsigned axis, float value, bool event) } +map &get_device_types() +{ + static map types; + return types; +} + +DeviceType register_device_type(const char *name) +{ + static unsigned next_type = EXTENSION_DEVICE+1; + auto i = insert_unique(get_device_types(), name, static_cast(next_type++)); + return i->second; +} + void operator>>(const LexicalConverter &conv, DeviceType &type) { if(conv.get()=="UNSPECIFIED") @@ -97,7 +112,14 @@ void operator>>(const LexicalConverter &conv, DeviceType &type) else if(conv.get()=="GAME_CONTROLLER") type = GAME_CONTROLLER; else - throw lexical_error(format("conversion of '%s' to DeviceType", conv.get())); + { + auto &ext_types = get_device_types(); + auto i = ext_types.find(conv.get()); + if(i!=ext_types.end()) + type = i->second; + else + throw lexical_error(format("conversion of '%s' to DeviceType", conv.get())); + } } void operator<<(LexicalConverter &conv, DeviceType type) @@ -109,7 +131,14 @@ void operator<<(LexicalConverter &conv, DeviceType type) case MOUSE: conv.result("MOUSE"); break; case TOUCH_SURFACE: conv.result("TOUCH_SURFACE"); break; case GAME_CONTROLLER: conv.result("GAME_CONTROLLER"); break; - default: conv.result(format("DeviceType(%#x)", static_cast(type))); + default: + for(const auto &kvp: get_device_types()) + if(kvp.second==type) + { + conv.result(kvp.first); + return; + } + conv.result(format("DeviceType(%#x)", static_cast(type))); } } diff --git a/source/input/device.h b/source/input/device.h index e7a779a..3c0ee26 100644 --- a/source/input/device.h +++ b/source/input/device.h @@ -24,7 +24,8 @@ enum DeviceType KEYBOARD, MOUSE, TOUCH_SURFACE, - GAME_CONTROLLER + GAME_CONTROLLER, + EXTENSION_DEVICE }; @@ -82,6 +83,7 @@ protected: }; +MSPGUI_API DeviceType register_device_type(const char *); MSPGUI_API void operator>>(const LexicalConverter &, DeviceType &); MSPGUI_API void operator<<(LexicalConverter &, DeviceType); -- 2.45.2