summaryrefslogtreecommitdiff
path: root/common/utility-install.hpp
blob: 55ae9d6832ce37b281d24f13c38f1e3bbc522826 (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
#pragma once

#include <filesystem>
#include <Windows.h>

#include "external/WinReg.hpp"

namespace fs = std::filesystem;
namespace wr = winreg;

inline const std::wstring DRIVER_NAME = L"rawaccel";
inline const std::wstring DRIVER_FILE_NAME = DRIVER_NAME + L".sys";
inline const std::wstring DRIVER_ENV_PATH = L"%systemroot%\\system32\\drivers\\" + DRIVER_FILE_NAME;

inline const auto sys_error = [](auto what, DWORD code = GetLastError()) {
    return std::system_error(code, std::system_category(), what);
};

inline std::wstring expand(const std::wstring& env_path) {
    std::wstring path(MAX_PATH, L'\0');

    auto len = ExpandEnvironmentStringsW(env_path.c_str(), &path[0], MAX_PATH);
    if (len == 0) throw sys_error("ExpandEnvironmentStrings failed");
    path.resize(len - 1);
    return path;
}

inline fs::path make_temp_path(const fs::path& p) {
    auto tmp_path = p;
    tmp_path.concat(L".tmp");
    return tmp_path;
}

template<typename Func>
void modify_upper_filters(Func fn) {
    const std::wstring FILTERS_NAME = L"UpperFilters";
    wr::RegKey key(
        HKEY_LOCAL_MACHINE,
        L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e96f-e325-11ce-bfc1-08002be10318}"
    );
 
    std::vector<std::wstring> filters = key.GetMultiStringValue(FILTERS_NAME);
    fn(filters);
    key.SetMultiStringValue(FILTERS_NAME, filters);
}