summaryrefslogtreecommitdiffhomepage
path: root/tracker-linux-joystick/ftnoir_tracker_linux_joystick.cpp
blob: 54d9b059b7ffbe927ab4703553f0d490da8fe017 (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
/* Copyright (c) 2013 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 "ftnoir_tracker_linux_joystick.h"
#include "api/plugin-api.hpp"
#include "compat/math.hpp"
#include <QMutexLocker>

joystick::joystick()
{
    QString device = getJoystickDevice(s.guid);
    joy_fd = open(device.toUtf8().data(), O_RDONLY | O_NONBLOCK);
}


joystick::~joystick() {
    if (joy_fd > 0) close(joy_fd);
}

module_status joystick::start_tracker(QFrame *)
{
    if (joy_fd == -1) return error("Couldn't open joystick");
    return status_ok();
}


void joystick::data(double *data)
{
    int map[6] = {
        s.joy_1 - 1,
        s.joy_2 - 1,
        s.joy_3 - 1,
        s.joy_4 - 1,
        s.joy_5 - 1,
        s.joy_6 - 1,
    };

    const double limits[] = {
        100,
        100,
        100,
        180,
        180,
        180
    };

    const QString guid = s.guid;
    int axes[8];
    struct js_event event;
    bool ret = true;
    if (read(joy_fd, &event, sizeof(event)) > 0)
    {
        switch (event.type)
        {
        case JS_EVENT_AXIS:
            if (event.number >= 8) break;
            axes_state[event.number] = event.value;

            break;
        default:
            /* Ignore init/button events. */
            break;
        }
    }

    for (int i = 0; i < 6; i++)
    {
        axes[i] = axes_state[i];
    }
    if (ret)
    {
        for (int i = 0; i < 6; i++)
        {
            int k = map[i];
            if (k < 0 || k >= 8)
                data[i] = 0;
            else
                data[i] = std::clamp(axes[k] * limits[i] / AXIS_MAX,
                                     -limits[i], limits[i]);
        }
    }
}

OPENTRACK_DECLARE_TRACKER(joystick, dialog_joystick, joystickDll)