summaryrefslogtreecommitdiffhomepage
path: root/proto-vjoystick/vjoystick.cpp
blob: d781d8e1afd0e275955e26c5d4d78d29ef733297 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Copyright (c) 2016, Stanislaw Halik <sthalik@misaki.pl>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */
#include "vjoystick.h"
#include "api/plugin-api.hpp"
#include "compat/math.hpp"

#include <cstring>
#include <QDebug>

#include <QPushButton>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>

// required for api headers
#include <windows.h>

#undef PPJOY_MODE
#include <public.h>
#include <vjoyinterface.h>

#define OPENTRACK_VJOYSTICK_ID 1

const unsigned char handle::axis_ids[6] =
{
    HID_USAGE_X,
    HID_USAGE_Y,
    HID_USAGE_Z,
    HID_USAGE_RX,
    HID_USAGE_RY,
    HID_USAGE_RZ,
//    HID_USAGE_SL0,
//    HID_USAGE_SL1,
//    HID_USAGE_WHL,
};

static constexpr double val_minmax[6] =
{
    50,
    50,
    50,
    180,
    180,
    180
};

bool handle::init()
{
    if (!AcquireVJD(OPENTRACK_VJOYSTICK_ID))
        return false;

    unsigned cnt = 0;
    bool status = true;

    for (unsigned i = 0; i < axis_count; i++)
    {
        if (!GetVJDAxisExist(OPENTRACK_VJOYSTICK_ID, axis_ids[i]))
            continue;
        cnt++;
        status &= !!GetVJDAxisMin(OPENTRACK_VJOYSTICK_ID, axis_ids[i], &axis_min[i]);
        status &= !!GetVJDAxisMax(OPENTRACK_VJOYSTICK_ID, axis_ids[i], &axis_max[i]);
    }
    //(void)ResetVJD(OPENTRACK_VJOYSTICK_ID);

    return status && cnt;
}

handle::handle()
{
    if (!isVJDExists(OPENTRACK_VJOYSTICK_ID))
        joy_state = state::notent;
    else if (init())
        joy_state = state::success;
    else
        joy_state = state::fail;
}

handle::~handle()
{
    if (joy_state == state::success)
        RelinquishVJD(OPENTRACK_VJOYSTICK_ID);
}

bool handle::to_axis_value(unsigned axis_id, double val, int& ret) const
{
    if (!axis_min[axis_id] && !axis_max[axis_id])
        return false;

    const double minmax = val_minmax[axis_id];
    const double min = axis_min[axis_id];
    const double max = axis_max[axis_id];

    ret = int(clamp((val+minmax) * max / (2*minmax) - min, min, max));
    return true;
}

vjoystick_proto::vjoystick_proto() = default;
vjoystick_proto::~vjoystick_proto() = default;

module_status vjoystick_proto::initialize()
{
    h = handle{};

    if (h->get_state() != state::success)
    {
        QMessageBox msgbox;
        msgbox.setIcon(QMessageBox::Critical);
        msgbox.setText(tr("vjoystick driver missing"));
        msgbox.setInformativeText(tr("vjoystick won't work without the driver installed."));

        QPushButton* driver_button = msgbox.addButton(tr("Download the driver"), QMessageBox::ActionRole);
        QPushButton* project_site_button = msgbox.addButton(tr("Visit project site"), QMessageBox::ActionRole);
        msgbox.addButton(QMessageBox::Close);

        (void) msgbox.exec();

        if (msgbox.clickedButton() == driver_button)
        {
            static const char* download_driver_url = "https://sourceforge.net/projects/vjoystick/files/latest/download";
            QDesktopServices::openUrl(QUrl(download_driver_url, QUrl::StrictMode));
        }
        else if (msgbox.clickedButton() == project_site_button)
        {
            static const char* project_site_url = "http://vjoystick.sourceforge.net/site/";
            QDesktopServices::openUrl(QUrl(project_site_url, QUrl::StrictMode));
        }
    }

    switch (h->get_state())
    {
    default:
    case state::notent:
        return error(tr("vjoystick not installed or disabled"));
    case state::fail:
        return error(tr("can't initialize vjoystick"));
    case state::success:
        return status_ok();
    }
}

void vjoystick_proto::pose(const double *pose)
{
    if (h->get_state() != state::success)
        return;

    for (unsigned i = 0; i < handle::axis_count; i++)
    {
        int val;
        if (!h->to_axis_value(i, pose[i], val))
            continue;
        SetAxis(val, OPENTRACK_VJOYSTICK_ID, handle::axis_ids[i]);
    }
}

OPENTRACK_DECLARE_PROTOCOL(vjoystick_proto, vjoystick_dialog, vjoystick_metadata)