summaryrefslogtreecommitdiff
path: root/wrapper/input.cpp
blob: 3ce257a8cf2b81d345502cbd2744be176cb5b8c2 (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
75
76
77
78
79
#include "input.h"
#include "interop-exception.h"

#include <msclr\marshal_cppstd.h>
#include <algorithm>

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

std::vector<HANDLE> rawinput_handles_from_id(const std::wstring& device_id)
{
    std::vector<HANDLE> handles;

    rawinput_foreach([&](const auto& dev) {
        if (dev.id == device_id) handles.push_back(dev.handle);
    });

    return handles;
}

std::vector<std::wstring> rawinput_id_list()
{
    std::vector<std::wstring> ids;

    rawinput_foreach([&](const auto& dev) {
        ids.push_back(dev.id);
    });

    std::sort(ids.begin(), ids.end());
    ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
    return ids;
}

public ref struct RawInputInteropException : InteropException {
    RawInputInteropException(System::String^ what) :
        InteropException(what) {}
    RawInputInteropException(const char* what) :
        InteropException(what) {}
    RawInputInteropException(const std::exception& e) :
        InteropException(e) {}
};

public ref struct RawInputInterop
{
    static void AddHandlesFromID(String^ deviceID, List<IntPtr>^ rawInputHandles)
    {
        try
        {
            std::vector<HANDLE> nativeHandles = rawinput_handles_from_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<String^>^ GetDeviceIDs()
    {
        try
        {
            auto ids = gcnew List<String^>();

            for (auto&& name : rawinput_id_list())
            {
                ids->Add(msclr::interop::marshal_as<String^>(name));
            }

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

};