+/*
+ * SuperTux
+ * Copyright (C) 2010 Florian Forster <octo at verplant.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "control/haptic_manager.hpp"
+#include "util/log.hpp"
+
+HapticManager::HapticManager () /* {{{ */
+{
+ int i;
+
+ _device = NULL;
+ memset (_effects, 0, sizeof (_effects));
+ for (i = 0; i < ST_HAPTIC_EFFECT_MAX; i++)
+ _effect_ids[i] = -1;
+} /* }}} HapticManager */
+
+/* Public methods */
+void HapticManager::addJoystick (SDL_Joystick *j) { /* {{{ */
+ int supported;
+
+ if (j == NULL)
+ return;
+
+ if (_device != NULL)
+ return;
+
+ if (SDL_JoystickIsHaptic (j) <= 0)
+ return;
+
+ _device = SDL_HapticOpenFromJoystick (j);
+ if (_device == NULL)
+ return;
+
+ supported = SDL_HapticQuery (_device);
+ if ((supported & SDL_HAPTIC_SINE) == 0)
+ {
+ SDL_HapticClose (_device);
+ _device = NULL;
+ return;
+ }
+
+ /* Setup buttjump */
+ _effects[ST_HAPTIC_EFFECT_BUTTJUMP].type = SDL_HAPTIC_SINE;
+ _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.direction.type = SDL_HAPTIC_POLAR;
+ _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.direction.dir[0] = 18000; /* south */
+ _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.period = 50;
+ _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.magnitude = 0x4FFF;
+ _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.length = 250;
+ _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.attack_length = 0;
+ _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.fade_length = 0;
+
+ _effect_ids[ST_HAPTIC_EFFECT_BUTTJUMP] = SDL_HapticNewEffect (_device,
+ &_effects[ST_HAPTIC_EFFECT_BUTTJUMP]);
+ if (_effect_ids[ST_HAPTIC_EFFECT_BUTTJUMP] < 0)
+ {
+ SDL_HapticClose (_device);
+ _device = NULL;
+ return;
+ }
+
+ log_debug << "Haptic manager: Successfully added joystick." << std::endl;
+ return;
+} /* }}} void addJoystick */
+
+void HapticManager::playEffect (haptic_effect_t idx) { /* {{{ */
+ if ((idx < 0) || (idx >= ST_HAPTIC_EFFECT_MAX))
+ return;
+
+ if (_device == NULL)
+ return;
+
+ if (_effect_ids[idx] < 0)
+ return;
+
+ SDL_HapticRunEffect (_device, _effect_ids[idx],
+ /* iterations = */ 1);
+} /* }}} void playEffect */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */