Showing the changes to interface/src/scripting/JoystickScriptingInterface.cpp (1 of 13 files). Show all
Unified · Split
| @@ -0,0 +1,82 @@ | ||
| 1 | +// | |
| 2 | +// JoystickManager.cpp | |
| 3 | +// interface/src/devices | |
| 4 | +// | |
| 5 | +// Created by Andrzej Kapolka on 5/15/14. | |
| 6 | +// Copyright 2014 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include <QtDebug> | |
| 13 | + | |
| 14 | +#ifdef HAVE_SDL | |
| 15 | +#include <SDL.h> | |
| 16 | +#undef main | |
| 17 | +#endif | |
| 18 | + | |
| 19 | +#include <PerfStat.h> | |
| 20 | + | |
| 21 | +#include "JoystickScriptingInterface.h" | |
| 22 | + | |
| 23 | +JoystickScriptingInterface& JoystickScriptingInterface::getInstance() { | |
| 24 | + static JoystickScriptingInterface sharedInstance; | |
| 25 | + return sharedInstance; | |
| 26 | +} | |
| 27 | + | |
| 28 | +JoystickScriptingInterface::JoystickScriptingInterface() : | |
| 29 | + _openJoysticks(), | |
| 30 | + _availableDeviceNames() | |
| 31 | +{ | |
| 32 | +#ifdef HAVE_SDL | |
| 33 | + SDL_Init(SDL_INIT_JOYSTICK); | |
| 34 | + | |
| 35 | + int joystickCount = SDL_NumJoysticks(); | |
| 36 | + | |
| 37 | + for (int i = 0; i < joystickCount; i++) { | |
| 38 | + _availableDeviceNames << SDL_JoystickName(i); | |
| 39 | + } | |
| 40 | +#endif | |
| 41 | +} | |
| 42 | + | |
| 43 | +JoystickScriptingInterface::~JoystickScriptingInterface() { | |
| 44 | + qDeleteAll(_openJoysticks); | |
| 45 | + | |
| 46 | +#ifdef HAVE_SDL | |
| 47 | + SDL_Quit(); | |
| 48 | +#endif | |
| 49 | +} | |
| 50 | + | |
| 51 | +void JoystickScriptingInterface::update() { | |
| 52 | +#ifdef HAVE_SDL | |
| 53 | + PerformanceTimer perfTimer("JoystickScriptingInterface::update"); | |
| 54 | + SDL_JoystickUpdate(); | |
| 55 | + | |
| 56 | + foreach(Joystick* joystick, _openJoysticks) { | |
| 57 | + joystick->update(); | |
| 58 | + } | |
| 59 | + | |
| 60 | +#endif | |
| 61 | +} | |
| 62 | + | |
| 63 | +Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { | |
| 64 | + Joystick* matchingJoystick = _openJoysticks.value(name); | |
| 65 | + if (!matchingJoystick) { | |
| 66 | + // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists | |
| 67 | + int joystickCount = SDL_NumJoysticks(); | |
| 68 | + | |
| 69 | + for (int i = 0; i < joystickCount; i++) { | |
| 70 | + if (SDL_JoystickName(i) == name) { | |
| 71 | + matchingJoystick = _openJoysticks.insert(name, new Joystick(name, SDL_JoystickOpen(i))).value(); | |
| 72 | + break; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer."; | |
| 77 | + } | |
| 78 | + | |
| 79 | + return matchingJoystick; | |
| 80 | +} | |
| 81 | + | |
| 82 | + | |