From b7038ed18fea717f81abae9a05c31ae4e5907ad2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Thu, 30 Jul 2015 02:32:26 +0200 Subject: [PATCH] Begin implementing SDL2 support --- CMakeLists.txt | 3 +- mainwindow.cpp | 9 +++++- sdl2deviceprober.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++ sdl2deviceprober.h | 23 +++++++++++++ 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 sdl2deviceprober.cpp create mode 100644 sdl2deviceprober.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4cf7a25..b6231d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/mainwindow.cpp b/mainwindow.cpp index 17041ae..8668fe8 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,5 +1,6 @@ #include "globalsettings.h" #include "linuxdeviceprober.h" +#include "sdl2deviceprober.h" #include "mainwindow.h" #include "ui_mainwindow.h" #include @@ -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::type>(DeviceProber::DeviceInterfaces::LINUX)); #ifdef FFBC_HAVE_SDL2 - ui->cbox_interfaces->addItem("SDL2", static_cast::type>(DeviceProber::DeviceInterfaces::SDL2)); + if (SDL2DeviceProber::initializeSDL()) + ui->cbox_interfaces->addItem("SDL2", static_cast::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(); break; +#ifdef FFBC_HAVE_SDL2 + case DeviceProber::DeviceInterfaces::SDL2: + prober = std::make_shared(); + 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 index 0000000..0934463 --- /dev/null +++ b/sdl2deviceprober.cpp @@ -0,0 +1,77 @@ +#include "sdl2deviceprober.h" +#include +#include + +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 SDL2DeviceProber::openDevice(const QString& id) +{ + if (!s_SDLInited) + return nullptr; + + return nullptr; +} + diff --git a/sdl2deviceprober.h b/sdl2deviceprober.h new file mode 100644 index 0000000..ae10f4d --- /dev/null +++ b/sdl2deviceprober.h @@ -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 openDevice(const QString& id); + +private: + static bool s_SDLInited; + + +}; + +#endif // SDL2DEVICEPROBER_H \ No newline at end of file -- 2.43.5