direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
joystick.h
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #ifndef JOYSTICK_H
22 #define JOYSTICK_H
23 
24 #include <QtGlobal> // for Q_OS_* Makro!
25 
26 #ifdef Q_OS_LINUX // joystick support for linux:
27 #include <sys/ioctl.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <stdint.h>
35 #include <linux/input.h>
36 #include <linux/joystick.h>
37 #endif
38 
39 #ifdef Q_OS_MAC // joystick support for Mac OS:
40 #include "joyreaderMacOS.h"
41 #endif
42 
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 
47 
48 #define NAME_LENGTH 128
49 
50 #include <QThread>
51 
58 class Joystick : public QThread
59 {
60  Q_OBJECT
61 
62  public:
63  Joystick();
64  ~Joystick();
65  void stop();
66  virtual void run();
67 
72  void setPort(QString port);
73 
78  bool isConnected();
79 
80 
81  signals:
87  void message(QString text);
88 
94  void joystickMoved(int axisNumber, int axisValue);
95 
101  void joystickButtonPressed(int axisNumber, bool buttonState);
102 
115  void joystickPOVButtonPressed(int buttonsState);
116 
117 
118  private:
119  volatile bool stopped;
120  QString joystickPort;
121 
122 #ifdef Q_OS_LINUX // joystick support for linux:
123  int fd, i;
124  unsigned char axes;
125  unsigned char buttons;
126  int version;
127  char name[NAME_LENGTH];
128  uint16_t btnmap[KEY_MAX - BTN_MISC + 1];
129  uint8_t axmap[ABS_MAX + 1];
130  int *axis;
131  char *button;
132  struct js_event js;
133  short int axisButtonNumber;
134  short int axisButtonValue;
135 #endif
136 
137  // Every thread sleeps some time, for having a bit more time fo the other threads!
138  // Time in milliseconds
139  static const unsigned long THREADSLEEPTIME = 250; // Default: 25 ms
140 
144  static const int JOYSTICKAXISY2 = 2; // ok
145  static const int JOYSTICKAXISX3 = 3; // ok
146  static const int JOYSTICKAXISX4 = 4;
147  static const int JOYSTICKAXISY5 = 5;
148 #ifdef Q_OS_MAC // joystick support for Mac OS:
149  int numJoysticks; // the number of recognised joysticks
150  static const int maxNumJoystick = 4;
151  JoyReader joystick[maxNumJoystick]; // the Mac OS joystick object
152 #endif
153 };
154 
155 #endif