]> git.defcon.no Git - rctxduino/blob - tools/jstt/jstt.cpp
4e197db831608a5d9ae01e8d223a97ef23c5ab91
[rctxduino] / tools / jstt / jstt.cpp
1 #include "jstt.h"
2 #include "ui_jstt.h"
3
4 jstt::jstt(QWidget *parent) :
5 QMainWindow(parent),
6 ui(new Ui::jstt)
7 {
8 ui->setupUi(this);
9
10 this->joystick = NULL;
11 this->init();
12 }
13
14 jstt::~jstt()
15 {
16 delete ui;
17
18 if( jsIsOpen() ) jsClose();
19
20 SDL_Quit();
21 }
22
23 void jstt::init()
24 {
25 QStringList jsNames;
26
27 if( SDL_Init(SDL_INIT_JOYSTICK) == 0 ) {
28
29 for(int i = 0; i < SDL_NumJoysticks(); i++) {
30 jsNames.append(SDL_JoystickName(i));
31 }
32 connect(&jsPullTimer, SIGNAL(timeout()), this, SLOT(processJs()));
33
34 ui->Joysticks->addItems(jsNames);
35 ui->Joysticks->setCurrentIndex(0); // This will trigger on_Joysticks_currentIndexChanged(int)
36 }
37 }
38
39 void jstt::jsOpen(int js)
40 {
41 if( jsIsOpen() ) jsClose();
42
43 joystick = SDL_JoystickOpen(js);
44 if( joystick ) {
45 jsPullTimer.start(eventTimeout);
46 /*
47
48 TODO:
49
50 numAxes = SDL_JoystickNumAxes(joystick);
51 numButtons = SDL_JoystickNumButtons(joystick);
52 numHats = SDL_JoystickNumHats(joystick);
53 numTrackballs = SDL_JoystickNumBalls(joystick);
54 JoystickTimer.start(eventTimeout);
55
56 When we have found out how many axes, buttons etc, we can expand the GUI further
57 */
58 return;
59 } else {
60 /*
61 One should make sure the program will react on this situation instead of just
62 returning.
63 */
64 return;
65 }
66 }
67
68 void jstt::jsClose()
69 {
70 this->jsPullTimer.stop();
71 if( joystick ) SDL_JoystickClose(joystick);
72 joystick = NULL;
73 }
74
75 void jstt::processJs()
76 {
77 ui->Chan_1_bar->setValue(30);
78 }
79
80 void jstt::on_Joysticks_currentIndexChanged(int index)
81 {
82 this->jsOpen(index);
83 }