3 * Copyright (C) 2010 Florian Forster <octo at verplant.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "control/haptic_manager.hpp"
20 #include "util/log.hpp"
22 #if SDL_VERSION_ATLEAST(1,3,0)
23 HapticManager::HapticManager () /* {{{ */
28 memset (_effects, 0, sizeof (_effects));
29 for (i = 0; i < ST_HAPTIC_EFFECT_MAX; i++)
31 } /* }}} HapticManager */
34 void HapticManager::addJoystick (SDL_Joystick *j) { /* {{{ */
43 if (SDL_JoystickIsHaptic (j) <= 0)
46 _device = SDL_HapticOpenFromJoystick (j);
50 supported = SDL_HapticQuery (_device);
51 if ((supported & SDL_HAPTIC_SINE) == 0)
53 SDL_HapticClose (_device);
59 _effects[ST_HAPTIC_EFFECT_BUTTJUMP].type = SDL_HAPTIC_SINE;
60 _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.direction.type = SDL_HAPTIC_POLAR;
61 _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.direction.dir[0] = 18000; /* south */
62 _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.period = 50;
63 _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.magnitude = 0x4FFF;
64 _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.length = 250;
65 _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.attack_length = 0;
66 _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.fade_length = 0;
68 _effect_ids[ST_HAPTIC_EFFECT_BUTTJUMP] = SDL_HapticNewEffect (_device,
69 &_effects[ST_HAPTIC_EFFECT_BUTTJUMP]);
70 if (_effect_ids[ST_HAPTIC_EFFECT_BUTTJUMP] < 0)
72 SDL_HapticClose (_device);
77 log_debug << "Haptic manager: Successfully added joystick." << std::endl;
79 } /* }}} void addJoystick */
81 void HapticManager::playEffect (haptic_effect_t idx) { /* {{{ */
82 if ((idx < 0) || (idx >= ST_HAPTIC_EFFECT_MAX))
88 if (_effect_ids[idx] < 0)
91 SDL_HapticRunEffect (_device, _effect_ids[idx],
92 /* iterations = */ 1);
93 } /* }}} void playEffect */
94 #else /* if SDL < 1.3 */
95 /* If the SDL version is too old, provide dummy methods that don't to anything.
96 * This avoid using defines all over the place. */
97 HapticManager::HapticManager () {
98 log_debug << "Haptic manager: Disabled because SDL version is too old." << std::endl;
101 void HapticManager::addJoystick (SDL_Joystick *j) {
105 void HapticManager::playEffect (haptic_effect_t idx) {
108 #endif /* SDL < 1.3 */
110 /* vim: set sw=2 sts=2 et fdm=marker : */