1 //this code by Peter Semiletov is the public domain 2 3 #include <QtGlobal> 4 #include <QObject> 5 6 #ifndef MYJOYSTICK_H 7 #define MYJOYSTICK_H 8 9 #if defined(JOYSTICK_SUPPORTED) 10 11 12 #include <iostream> 13 #include <unistd.h> 14 #include <fcntl.h> 15 #include <errno.h> 16 #include <linux/joystick.h> 17 18 #include <QEvent> 19 20 21 const QEvent::Type evtJoystickAxis = QEvent::Type (QEvent::User + 1); 22 const QEvent::Type evtJoystickButton = QEvent::Type (QEvent::User + 2); 23 24 25 class CJoystickButtonEvent: public QEvent 26 { 27 public: 28 29 uint button; 30 bool pressed; 31 CJoystickButtonEvent(QEvent::Type type)32 CJoystickButtonEvent (QEvent::Type type): QEvent (type), button (0), pressed (false) {} 33 }; 34 35 36 class CJoystickAxisEvent: public QEvent 37 { 38 public: 39 40 uint axis; 41 qint16 value; 42 CJoystickAxisEvent(QEvent::Type type)43 CJoystickAxisEvent (QEvent::Type type): QEvent (type), axis (0), value (0) {} 44 }; 45 46 47 class CJoystick: public QObject 48 { 49 Q_OBJECT 50 51 public: 52 53 QObject *receiver; //link to the object that handle joystick events 54 55 int fd; //joystick file descriptor 56 uint id; //joystick id 57 58 int etype; 59 bool axis_pressed; 60 61 QString description; //joystick text description 62 bool initialized; 63 64 uint number_of_axis; 65 uint number_of_buttons; 66 67 CJoystick (uint idn, QObject *upper_link); 68 ~CJoystick(); 69 70 void process_event (js_event e); 71 72 public slots: 73 74 void read_joystick(); 75 76 }; 77 78 #endif 79 #endif 80 81