summaryrefslogtreecommitdiff
path: root/wrapper/input.cpp
blob: d50f774e8169b11c413aebf9a21e1a9499b698b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "interop-exception.h"

#include <utility-rawinput.hpp>
#include <algorithm>
#include <msclr\marshal_cppstd.h>

using namespace System;
using namespace System::Collections::Generic;

struct device_info {
    std::wstring name;
    std::wstring id;
};

std::vector<device_info> get_unique_device_info() {
    std::vector<device_info> info;

    rawinput_foreach_with_interface([&](const auto& dev, const WCHAR* name) {
        info.push_back({
            L"", // get_property_wstr(name, &DEVPKEY_Device_FriendlyName), /* doesn't work */
            dev_id_from_interface(name)
            });
    });

    std::sort(info.begin(), info.end(),
        [](auto&& l, auto&& r) { return l.id < r.id; });
    auto last = std::unique(info.begin(), info.end(),
        [](auto&& l, auto&& r) { return l.id == r.id; });
    info.erase(last, info.end());
    
    return info;
}

public ref struct RawInputInterop
{
    static void AddHandlesFromID(String^ deviceID, List<IntPtr>^ rawInputHandles)
    {
        try
        {
            std::vector<HANDLE> nativeHandles = rawinput_handles_from_dev_id(
                msclr::interop::marshal_as<std::wstring>(deviceID));

            for (auto nh : nativeHandles) rawInputHandles->Add(IntPtr(nh));
        }
        catch (const std::exception& e)
        {
            throw gcnew RawInputInteropException(e);
        }
    }

    static List<ValueTuple<String^, String^>>^ GetDeviceIDs()
    {
        try
        {
            auto managed = gcnew List<ValueTuple<String^, String^>>();

            for (auto&& [name, id] : get_unique_device_info())
            {
                managed->Add(
                    ValueTuple<String^, String^>(
                        msclr::interop::marshal_as<String^>(name),
                        msclr::interop::marshal_as<String^>(id)));
            }

            return managed;
        }
        catch (const std::exception& e)
        {
            throw gcnew RawInputInteropException(e);
        }
    }

};