~mhall119/ubuntu-app-reviews/qtranscribe

« back to all changes in this revision

Viewing changes to Joystick.hh

  • Committer: App Bot
  • Author(s): Matt Pharoah (Exüberance)
  • Date: 2012-07-09 20:14:22 UTC
  • Revision ID: appbot@holba.ch-20120709201422-cmbglasydwxlxelx
Tags: upstream-1.0
Import upstream version 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright © 2012 Matt Pharoah (mr.exuberant@gmail.com)
 
3
 *
 
4
 *  This program is free software: you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation, either version 3 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 */
 
18
 
 
19
#ifndef JOYSTICK_HH_
 
20
#define JOYSTICK_HH_
 
21
 
 
22
#include <thread>
 
23
#include <chrono>
 
24
#include <stdexcept>
 
25
#include <cstring>
 
26
 
 
27
#include <linux/joystick.h>
 
28
#include <sys/ioctl.h>
 
29
#include <sys/select.h>
 
30
#include <sys/time.h>
 
31
#include <fcntl.h>
 
32
#include <unistd.h>
 
33
#include <errno.h>
 
34
 
 
35
using namespace std;
 
36
 
 
37
struct JSEvent {
 
38
        unsigned int timestamp;
 
39
        short value;
 
40
        char type;
 
41
        unsigned char button;
 
42
};
 
43
 
 
44
 
 
45
class Joystick {
 
46
  private:
 
47
 
 
48
        thread *driver;
 
49
        char filename[15];
 
50
 
 
51
        int device;
 
52
        volatile bool alive;
 
53
        volatile bool connected;
 
54
 
 
55
        int DEB;
 
56
 
 
57
        static void start(void (*listener)(const JSEvent&, void *), void (*onDisconnect)(void *), Joystick *me, void *userdata) {
 
58
                JSEvent ev;
 
59
                const struct timespec DECISECOND = { 0, 100000000 };
 
60
                chrono::milliseconds deciSecond(100);
 
61
 
 
62
                while (me->alive) {
 
63
                        while (!me->connected) {
 
64
                                if (!me->alive) return;
 
65
                                this_thread::sleep_for(deciSecond);
 
66
                        }
 
67
 
 
68
                        int status = 0;
 
69
                        fd_set dset;
 
70
 
 
71
                        while (status == 0 && me->device >= 0) {
 
72
                                FD_ZERO(&dset);
 
73
                                FD_SET(me->device, &dset);
 
74
                                status = pselect(me->device + 1, &dset, NULL, NULL, &DECISECOND, NULL);
 
75
                                if (!me->alive) return;
 
76
                        }
 
77
 
 
78
                        if (status > 0 && errno == 0 && me->device >= 0) {
 
79
                                 if (read(me->device, (void *) &ev, sizeof(JSEvent)) == sizeof(JSEvent)) listener(ev, userdata);
 
80
                        } else if (onDisconnect != NULL) {
 
81
                                onDisconnect(userdata);
 
82
                        } else {
 
83
                                //default implementation of disconnect: wait a decisecond and try to reconnect
 
84
                                this_thread::sleep_for(deciSecond);
 
85
                                me->reconnect();
 
86
                        }
 
87
                }
 
88
        }
 
89
 
 
90
  public:
 
91
 
 
92
        static const char BUTTON_EVENT = 1;
 
93
        static const char AXIS_EVENT = 2;
 
94
 
 
95
        static const short PRESSED = 1;
 
96
        static const short RELEASED = 0;
 
97
 
 
98
        Joystick(unsigned char num, void (*listener)(const JSEvent&, void *), void (*onDisconnect)(void *) = NULL, void *userdata = NULL) {
 
99
                if (num > 9) throw invalid_argument("Invalid joystick number. Valid range is [0,9]\n");
 
100
                alive = true;
 
101
                connected = true;
 
102
                strcpy(filename, "/dev/input/js?");
 
103
                filename[13] = (char) (48 + num);
 
104
                device = open(filename, O_RDONLY);
 
105
                driver = new thread(start, listener, onDisconnect, this, userdata);
 
106
        }
 
107
 
 
108
        /*
 
109
         * NOTE: it is expected that reconnect() will be called from the same
 
110
         * thread that onDisconnect() is called from (the joystick thread).
 
111
         * Otherwise, errno will not be cleared since it is thread-specific
 
112
         */
 
113
        void reconnect() {
 
114
                close(device);
 
115
                errno = 0;
 
116
                device = open(filename, O_RDONLY);
 
117
                connected = true;
 
118
        }
 
119
 
 
120
        inline unsigned char getPort() {
 
121
                return ((unsigned char) filename[13] - 48);
 
122
        }
 
123
 
 
124
        ~Joystick() {
 
125
                alive = false;
 
126
                driver->join();
 
127
                delete driver;
 
128
                close(device);
 
129
        }
 
130
 
 
131
        static void getNameAndButtons(unsigned char num, char *str, unsigned int string_size, unsigned char *buttons = NULL) {
 
132
                if (num > 9) throw invalid_argument("Invalid joystick number. Valid range is [0,9]\n");
 
133
 
 
134
                char fileName[15];
 
135
                strcpy(fileName, "/dev/input/js?");
 
136
                fileName[13] = (char) (48 + num);
 
137
                int fd = open(fileName, O_RDONLY);
 
138
                if (fd == -1) throw invalid_argument("Joystick not connected on this port.\n");
 
139
                if (str != NULL) {
 
140
                        if (ioctl(fd, JSIOCGNAME(string_size), str) < 0) strcpy(str, "Unknown Device");
 
141
                }
 
142
                if (buttons != NULL) {
 
143
                        if (ioctl(fd, JSIOCGBUTTONS, buttons) < 0) *buttons = 8; //if the number of buttons can't be read for whatever reason, default to 8
 
144
                }
 
145
                close(fd);
 
146
        }
 
147
 
 
148
};
 
149
 
 
150
 
 
151
#endif /* JOYSTICK_HH_ */