From: Michal MalĂ˝ Date: Thu, 30 Jul 2015 00:32:26 +0000 (+0200) Subject: Begin implementing SDL2 support X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=b7038ed18fea717f81abae9a05c31ae4e5907ad2;p=FFBChecker.git Begin implementing SDL2 support --- 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