]> git.defcon.no Git - joysticktest/blob - joystickthread.cpp
Working test with Qt GUI. Threaded, with no thread safety.
[joysticktest] / joystickthread.cpp
1 #include "joystickthread.h"
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 int JoystickThread::open_joystick(char *joystick_device)
12 {
13 joystick_fd = open(joystick_device, O_RDONLY | O_NONBLOCK);
14
15 return joystick_fd;
16 }
17 int JoystickThread::read_joystick_event(struct js_event *jse)
18 {
19 int bytes;
20
21 bytes = read(joystick_fd, jse, sizeof(*jse));
22
23 if (bytes == -1)
24 return 0;
25
26 if (bytes == sizeof(*jse))
27 return 1;
28
29 return -1;
30 }
31
32 JoystickThread::JoystickThread(JoystickData *data, QString device)
33 {
34 this->device = device;
35 this->jd = data;
36 this->joystick_fd = -1;
37 this->done = 0;
38
39 }
40
41 void JoystickThread::run()
42 {
43 int fd, rc;
44
45 struct js_event jse;
46 fd = open_joystick(this->device.toLatin1().data());
47 if (fd < 0) {
48 return;
49 }
50 while (!done) {
51 rc = read_joystick_event(&jse);
52 usleep(1000);
53 if (rc == 1) {
54 if (jse.type == JS_EVENT_AXIS)
55 {
56 if ( jse.number <= 5)
57 {
58 jd->setAxis(jse.number, jse.value);
59 }
60 }
61 if (jse.type == JS_EVENT_BUTTON)
62 {
63 if ( jse.number <= 9)
64 {
65 jd->setButton(jse.number, jse.value);
66 }
67 }
68 }
69 }
70 close(joystick_fd);
71 joystick_fd = -1;
72 done = 0;
73 return;
74 }
75 void JoystickThread::stop()
76 {
77 this->done = 1;
78 this->wait();
79 }