]> git.defcon.no Git - rctxduino/blobdiff - tools/jstt/jstt.cpp
Added some code for the controllers testing tool. The code is still not functional.
[rctxduino] / tools / jstt / jstt.cpp
index 5698a1adbdf4a4010d19d572aa7544cfbf0db827..4e197db831608a5d9ae01e8d223a97ef23c5ab91 100644 (file)
@@ -5,31 +5,79 @@ jstt::jstt(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::jstt)
 {
-    int i;
-
     ui->setupUi(this);
 
-    if( SDL_Init(SDL_INIT_JOYSTICK) == 0 ) {
-        for(i = 0; i < SDL_NumJoysticks(); i++) {
-            this->jsNames.append(SDL_JoystickName(i));
-        }
-        ui->Joysticks->addItems(this->jsNames);
-    }
+    this->joystick = NULL;
+    this->init();
 }
 
 jstt::~jstt()
 {
     delete ui;
+
+    if( jsIsOpen() ) jsClose();
+
+    SDL_Quit();
+}
+
+void jstt::init()
+{
+    QStringList jsNames;
+
+    if( SDL_Init(SDL_INIT_JOYSTICK) == 0 ) {
+
+        for(int i = 0; i < SDL_NumJoysticks(); i++) {
+            jsNames.append(SDL_JoystickName(i));
+        }
+        connect(&jsPullTimer, SIGNAL(timeout()), this, SLOT(processJs()));
+
+        ui->Joysticks->addItems(jsNames);
+        ui->Joysticks->setCurrentIndex(0);          // This will trigger on_Joysticks_currentIndexChanged(int)
+    }
 }
 
-void jstt::changeEvent(QEvent *e)
+void jstt::jsOpen(int js)
 {
-    QMainWindow::changeEvent(e);
-    switch (e->type()) {
-    case QEvent::LanguageChange:
-        ui->retranslateUi(this);
-        break;
-    default:
-        break;
+    if( jsIsOpen() ) jsClose();
+
+    joystick = SDL_JoystickOpen(js);
+    if( joystick ) {
+        jsPullTimer.start(eventTimeout);
+        /*
+
+        TODO:
+
+        numAxes = SDL_JoystickNumAxes(joystick);
+        numButtons = SDL_JoystickNumButtons(joystick);
+        numHats = SDL_JoystickNumHats(joystick);
+        numTrackballs = SDL_JoystickNumBalls(joystick);
+        JoystickTimer.start(eventTimeout);
+
+        When we have found out how many axes, buttons etc, we can expand the GUI further
+        */
+        return;
+    } else {
+        /*
+          One should make sure the program will react on this situation instead of just
+          returning.
+          */
+        return;
     }
 }
+
+void jstt::jsClose()
+{
+    this->jsPullTimer.stop();
+    if( joystick ) SDL_JoystickClose(joystick);
+    joystick = NULL;
+}
+
+void jstt::processJs()
+{
+    ui->Chan_1_bar->setValue(30);
+}
+
+void jstt::on_Joysticks_currentIndexChanged(int index)
+{
+    this->jsOpen(index);
+}