]> git.tdb.fi Git - libs/gui.git/commitdiff
Add an API for other libraries and application to register device types
authorMikko Rasa <tdb@tdb.fi>
Sun, 26 Jan 2025 10:47:06 +0000 (12:47 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 26 Jan 2025 10:47:06 +0000 (12:47 +0200)
source/input/device.cpp
source/input/device.h

index 22429a0371bfcb9062e7c96d478e4a7c1d3a579f..cecbe61c7741688f56b08642de75e06a69497f8e 100644 (file)
@@ -1,4 +1,6 @@
 #include "device.h"
+#include <map>
+#include <msp/core/maputils.h>
 #include <msp/strings/format.h>
 
 using namespace std;
@@ -84,6 +86,19 @@ void Device::set_axis_value(unsigned axis, float value, bool event)
 }
 
 
+map<string, DeviceType> &get_device_types()
+{
+       static map<string, DeviceType> 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<DeviceType>(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<int>(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<int>(type)));
        }
 }
 
index e7a779af3159fececfb53acc33f79ea86332500a..3c0ee264043d71049dbdce9372a9ef667720a8c6 100644 (file)
@@ -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);