forked from zvxryb/Linux-Virtual-Joystick
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevice.h
121 lines (87 loc) · 2.44 KB
/
device.h
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
#ifndef DEVICE_H
#define DEVICE_H
#include <linux/input.h>
#include <linux/uinput.h>
#include <string>
#include <ostream>
#include <iostream>
#include "python_include.h"
typedef struct _vjoy_info {
char name[UINPUT_MAX_NAME_SIZE];
int relaxis[REL_CNT];
int relaxiscount;
int absaxis[ABS_CNT];
int absaxiscount;
int feedback[FF_CNT];
int feedbackcount;
int maxeffects;
int buttons[KEY_CNT];
int buttoncount;
} vjoy_info;
#define DEFUALT_INPUT_RATE 60 // default loop input frequency in Hertz
class device {
public:
static device* dev;
enum dev_state {
UNINITIALIZED = 0, LIVE, TO_STALL, STALLED
};
char* devPath;
int uifd; // UInput File Descriptor
uinput_user_dev uidev; // UInput Device Info
PyObject* pymodule; // The Python script that operates this device
vjoy_info devinfo; // The parsed device info
pthread_t inputThread; // pthread for device input loop
std::string name; //sadly this is a duplicate
int delay; //milliseconds
bool enableFF; //use force feedback
//this is called from inputLoop
dev_state getState() {
pthread_mutex_lock(&devMutex);
dev_state ret = currentStatus;
pthread_mutex_unlock(&devMutex);
return ret;
}
void setState(const dev_state& s) {
pthread_mutex_lock(&devMutex);
currentStatus = s;
pthread_mutex_unlock(&devMutex);
}
device(std::string devName, int freq) :
name(devName) {
if (freq == 0) freq = DEFUALT_INPUT_RATE;
delay = 1000000 / freq;
currentStatus = UNINITIALIZED;
pause_callback = NULL;
resume_callback = NULL;
kill_callback = NULL;
}
~device() {
}
//this can be called from one of the threads
std::ostream& err() {
pthread_mutex_lock(&devMutex);
return std::cerr << name << ": ";
pthread_mutex_unlock(&devMutex);
}
void load();
//this can also be called from the event thread
void kill();
//these are to be called only from main thread
void pause();
void resume();
private:
//these are module functions called when appropriate, if they exist
PyObject* pause_callback;
PyObject* resume_callback;
PyObject* kill_callback;
//thread
static void* inputLoop(void* arg);
void killEventLoop();
void killInputLoop();
static pthread_mutex_t devMutex; //this is for indirectly communicating with the main loop
static PyObject* moduleKillSelf(PyObject* self, PyObject* args); //this is a python callback
void parseInfo();
pthread_cond_t stallCond; //this waited for in the case of stall
dev_state currentStatus;
};
#endif /* DEVICE_H */