summaryrefslogtreecommitdiffhomepage
path: root/proto-wine/opentrack-wrapper-wine-windows.cxx
blob: c1d552e18af8a1d2450201389beded195712d921 (plain)
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
#ifndef __WIN32
#   error "bad cross"
#endif

#define shm_wrapper ShmWine
#define __WINE_OLE2_H
// OSX sdk 10.8 build error otherwise
#undef _LIBCPP_MSVCRT

#include "compat/shm.h"
#include "compat/shm.cpp"
#include "wine-shm.h"
#include "compat/library-path.hpp"
#include <cstdlib>
#include <cstring>
#include <sysexits.h>

using std::strcat;

static void write_path(const char* key, const char* subkey, bool path)
{
    char dir[8192] {};

    if (GetCurrentDirectoryA(8192, dir) < 8190)
    {
        HKEY hkpath;
        if (RegCreateKeyExA(HKEY_CURRENT_USER,
                            key,
                            0,
                            NULL,
                            0,
                            KEY_ALL_ACCESS,
                            NULL,
                            &hkpath,
                            NULL) == ERROR_SUCCESS)
        {
            for (int i = 0; dir[i]; i++)
                if (dir[i] == '\\')
                    dir[i] = '/';
            // there's always a leading and trailing slash
            strcat(dir, OPENTRACK_LIBRARY_PATH);
            //strcat(dir, "/");
            if (!path)
                dir[0] = '\0';
            (void) RegSetValueExA(hkpath, subkey, 0, REG_SZ, (BYTE*) dir, strlen(dir) + 1);
            RegCloseKey(hkpath);
        }
    }
}

void create_registry_key(void)
{
    bool use_freetrack, use_npclient;
    const char* env = getenv("OTR_WINE_PROTO");
    char* endptr;
    if (!env) env = "";
    int selection = strtol(env, &endptr, 10);
    if (*endptr)
        selection = 0;

    switch (selection)
    {
    default: std::exit(EX_USAGE);
    case 1: use_freetrack = true, use_npclient = false; break;
    case 2: use_freetrack = false, use_npclient = true; break;
    case 3: use_freetrack = true, use_npclient = true; break;
    }

    write_path("Software\\NaturalPoint\\NATURALPOINT\\NPClient Location", "Path", use_npclient);
    write_path("Software\\Freetrack\\FreeTrackClient", "Path", use_freetrack);
}