]> Devoid-pointer.net GitWeb - FFBChecker.git/commitdiff
Begin implementing SDL2 support
authorMichal Malý <madcatxster@devoid-pointer.net>
Thu, 30 Jul 2015 00:32:26 +0000 (02:32 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Thu, 30 Jul 2015 00:32:26 +0000 (02:32 +0200)
CMakeLists.txt
mainwindow.cpp
sdl2deviceprober.cpp [new file with mode: 0644]
sdl2deviceprober.h [new file with mode: 0644]

index 4cf7a25779ba1381e11ce2bbfb8e41cdbdce0250..b6231d80836abc3fb76b0b6cfa8e05ab87b7354d 100644 (file)
@@ -49,7 +49,8 @@ set(FFBChecker_SRCS
     mainwindow.cpp
     periodiceffectsettings.cpp
     rampeffectsettings.cpp
-    rumbleeffectsettings.cpp)
+    rumbleeffectsettings.cpp
+    sdl2deviceprober.cpp)
 
 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
 if (SDL2_FOUND)
index 17041ae93e019aa565c432ebfe73ea304b5b4759..8668fe813aa1e5de13574302a9a926cf2f9c64d1 100644 (file)
@@ -1,5 +1,6 @@
 #include "globalsettings.h"
 #include "linuxdeviceprober.h"
+#include "sdl2deviceprober.h"
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 #include <QtWidgets/QMessageBox>
@@ -35,7 +36,8 @@ MainWindow::MainWindow(const QString& title, QWidget* parent) :
   /* Fill the list of available interfaces */
   ui->cbox_interfaces->addItem("Linux API", static_cast<std::underlying_type<DeviceProber::DeviceInterfaces>::type>(DeviceProber::DeviceInterfaces::LINUX));
 #ifdef FFBC_HAVE_SDL2
-  ui->cbox_interfaces->addItem("SDL2", static_cast<std::underlying_type<DeviceProber::DeviceInterfaces>::type>(DeviceProber::DeviceInterfaces::SDL2));
+  if (SDL2DeviceProber::initializeSDL())
+    ui->cbox_interfaces->addItem("SDL2", static_cast<std::underlying_type<DeviceProber::DeviceInterfaces>::type>(DeviceProber::DeviceInterfaces::SDL2));
 #endif
 
   ui->cbox_interfaces->setCurrentIndex(0);
@@ -68,6 +70,11 @@ bool MainWindow::createDeviceProber(const DeviceProber::DeviceInterfaces iface)
   case DeviceProber::DeviceInterfaces::LINUX:
     prober = std::make_shared<LinuxDeviceProber>();
     break;
+#ifdef FFBC_HAVE_SDL2
+  case DeviceProber::DeviceInterfaces::SDL2:
+    prober = std::make_shared<SDL2DeviceProber>();
+    break;
+#endif
   default:
     QMessageBox::critical(this, "Cannot probe devices", "Selected interface is not supported yet.");
     break;
diff --git a/sdl2deviceprober.cpp b/sdl2deviceprober.cpp
new file mode 100644 (file)
index 0000000..0934463
--- /dev/null
@@ -0,0 +1,77 @@
+#include "sdl2deviceprober.h"
+#include <QtCore/QDebug>
+#include <QtWidgets/QMessageBox>
+
+bool SDL2DeviceProber::s_SDLInited = false;
+
+bool SDL2DeviceProber::initializeSDL()
+{
+  int ret;
+
+  if (s_SDLInited)
+    return true;
+
+  ret = SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC);
+  if (ret == 0) {
+    s_SDLInited = true;
+    return true;
+  }
+
+  QMessageBox::warning(nullptr, "SDL2 error",
+                      QString("Unable to initialize SDL2 (%1). SDL2 interface will not be available.").arg(SDL_GetError()));
+  return false;
+}
+
+void SDL2DeviceProber::closeAllDevices()
+{
+  if (!s_SDLInited)
+    return;
+
+  return;
+}
+
+
+DeviceProber::DeviceList SDL2DeviceProber::listDevices()
+{
+  DeviceProber::DeviceList devList;
+  int numJoy;
+
+  if (!s_SDLInited)
+    return devList;
+
+  numJoy = SDL_NumJoysticks();
+
+  for (int idx = 0; idx < numJoy; idx++) {
+    DeviceProber::DeviceInfo dinfo;
+
+    SDL_Joystick* joystick = SDL_JoystickOpen(idx);
+
+    if (joystick == nullptr) {
+      qDebug() << "SDL2: Cannot open joystick at idx" << idx;
+      continue;
+    }
+
+    if (SDL_JoystickIsHaptic(joystick) != 1) {
+      qDebug() << "SDL2: Joystick at idx" << idx << "does not support force feedback";
+      SDL_JoystickClose(joystick);
+      continue;
+    }
+
+    dinfo.name = QString(SDL_JoystickName(joystick));
+    dinfo.id = QVariant(idx);
+
+    devList.push_back(dinfo);
+    SDL_JoystickClose(joystick);
+  }
+
+  return devList;
+}
+
+std::shared_ptr<FFBDevice> SDL2DeviceProber::openDevice(const QString& id)
+{
+  if (!s_SDLInited)
+    return nullptr;
+
+  return nullptr;
+}
+
diff --git a/sdl2deviceprober.h b/sdl2deviceprober.h
new file mode 100644 (file)
index 0000000..ae10f4d
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef SDL2DEVICEPROBER_H
+#define SDL2DEVICEPROBER_H
+
+#include "deviceprober.h"
+#include "SDL.h"
+
+class SDL2DeviceProber : public DeviceProber
+{
+public:
+  static bool initializeSDL();
+
+  explicit SDL2DeviceProber() : DeviceProber(DeviceInterfaces::SDL2) {}
+  void closeAllDevices();
+  DeviceList listDevices();
+  std::shared_ptr<FFBDevice> openDevice(const QString& id);
+
+private:
+  static bool s_SDLInited;
+
+
+};
+
+#endif // SDL2DEVICEPROBER_H
\ No newline at end of file