~mhall119/ubuntu-app-reviews/qtranscribe

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
/*
 *  Copyright © 2012 Matt Pharoah (mr.exuberant@gmail.com)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef JOYSTICK_HH_
#define JOYSTICK_HH_

#include <thread>
#include <chrono>
#include <stdexcept>
#include <cstring>

#include <linux/joystick.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

using namespace std;

struct JSEvent {
	unsigned int timestamp;
	short value;
	char type;
	unsigned char button;
};


class Joystick {
  private:

	thread *driver;
	char filename[15];

	int device;
	volatile bool alive;
	volatile bool connected;

	int DEB;

	static void start(void (*listener)(const JSEvent&, void *), void (*onDisconnect)(void *), Joystick *me, void *userdata) {
		JSEvent ev;
		const struct timespec DECISECOND = { 0, 100000000 };
		chrono::milliseconds deciSecond(100);

		while (me->alive) {
			while (!me->connected) {
				if (!me->alive) return;
				this_thread::sleep_for(deciSecond);
			}

			int status = 0;
			fd_set dset;

			while (status == 0 && me->device >= 0) {
				FD_ZERO(&dset);
				FD_SET(me->device, &dset);
				status = pselect(me->device + 1, &dset, NULL, NULL, &DECISECOND, NULL);
				if (!me->alive) return;
			}

			if (status > 0 && errno == 0 && me->device >= 0) {
				 if (read(me->device, (void *) &ev, sizeof(JSEvent)) == sizeof(JSEvent)) listener(ev, userdata);
			} else if (onDisconnect != NULL) {
				onDisconnect(userdata);
			} else {
				//default implementation of disconnect: wait a decisecond and try to reconnect
				this_thread::sleep_for(deciSecond);
				me->reconnect();
			}
		}
	}

  public:

	static const char BUTTON_EVENT = 1;
	static const char AXIS_EVENT = 2;

	static const short PRESSED = 1;
	static const short RELEASED = 0;

	Joystick(unsigned char num, void (*listener)(const JSEvent&, void *), void (*onDisconnect)(void *) = NULL, void *userdata = NULL) {
		if (num > 9) throw invalid_argument("Invalid joystick number. Valid range is [0,9]\n");
		alive = true;
		connected = true;
		strcpy(filename, "/dev/input/js?");
		filename[13] = (char) (48 + num);
		device = open(filename, O_RDONLY);
		driver = new thread(start, listener, onDisconnect, this, userdata);
	}

	/*
	 * NOTE: it is expected that reconnect() will be called from the same
	 * thread that onDisconnect() is called from (the joystick thread).
	 * Otherwise, errno will not be cleared since it is thread-specific
	 */
	void reconnect() {
		close(device);
		errno = 0;
		device = open(filename, O_RDONLY);
		connected = true;
	}

	inline unsigned char getPort() {
		return ((unsigned char) filename[13] - 48);
	}

	~Joystick() {
		alive = false;
		driver->join();
		delete driver;
		close(device);
	}

	static void getNameAndButtons(unsigned char num, char *str, unsigned int string_size, unsigned char *buttons = NULL) {
		if (num > 9) throw invalid_argument("Invalid joystick number. Valid range is [0,9]\n");

		char fileName[15];
		strcpy(fileName, "/dev/input/js?");
		fileName[13] = (char) (48 + num);
		int fd = open(fileName, O_RDONLY);
		if (fd == -1) throw invalid_argument("Joystick not connected on this port.\n");
		if (str != NULL) {
			if (ioctl(fd, JSIOCGNAME(string_size), str) < 0) strcpy(str, "Unknown Device");
		}
		if (buttons != NULL) {
			if (ioctl(fd, JSIOCGBUTTONS, buttons) < 0) *buttons = 8; //if the number of buttons can't be read for whatever reason, default to 8
		}
		close(fd);
	}

};


#endif /* JOYSTICK_HH_ */