File: joystick.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (162 lines) | stat: -rw-r--r-- 4,405 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Joystick management.
 *****************************************************************************/

#include "interface/joystick.h"
#include <SDL.h>

Joystick::Joystick()
  : ManMachineInterface()
  , init(false)
  , previous_x_value(0)
  , previous_y_value(0)
  , previous_x_axis(KEY_NONE)
  , previous_y_axis(KEY_NONE)
{
  if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
    Error(Format("Unable to initialize SDL joystick: %s", SDL_GetError()));

    // Don't crash for this, just ignore further request
    return;
  }
  init = true;

  SetDefaultConfig();

  // Registring SDL event
  RegisterEvent(SDL_JOYAXISMOTION);
  RegisterEvent(SDL_JOYBALLMOTION);
  RegisterEvent(SDL_JOYHATMOTION);
  RegisterEvent(SDL_JOYBUTTONDOWN);
  RegisterEvent(SDL_JOYBUTTONUP);
}

Joystick::~Joystick()
{
  if (init)
    SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  init = false;
}

void Joystick::SetDefaultConfig()
{
  int i = 0;
  SetKeyAction(i++, ManMachineInterface::KEY_SHOOT);
  SetKeyAction(i++, ManMachineInterface::KEY_JUMP);
  SetKeyAction(i++, ManMachineInterface::KEY_HIGH_JUMP);
  SetKeyAction(i++, ManMachineInterface::KEY_BACK_JUMP);
  SetKeyAction(i++, ManMachineInterface::KEY_NEXT_CHARACTER);
  SetKeyAction(i++, ManMachineInterface::KEY_CHANGE_WEAPON);
  SetKeyAction(i++, ManMachineInterface::KEY_WEAPON_LESS);
  SetKeyAction(i++, ManMachineInterface::KEY_WEAPON_MORE);
}

int Joystick::GetNumberOfJoystick() const
{
  return (init) ? 0 : SDL_NumJoysticks();
}

bool Joystick::HandleKeyEvent(const SDL_Event& evnt)
{
  // Not a registred event
  if (!init || !IsRegistredEvent(evnt.type))
    return false;

  if (GetNumberOfJoystick() == 0)
    return false;

  Key_Event_t event_type;
  switch (evnt.type) {
  case SDL_JOYAXISMOTION:
    if(evnt.jaxis.axis == 0) {
      event_type = X_AXIS_MOTION;
    } else {
      event_type = Y_AXIS_MOTION;
    }
    break;
  case SDL_JOYBUTTONDOWN:
    event_type = KEY_PRESSED;
    break;
  case SDL_JOYBUTTONUP:
    event_type = KEY_RELEASED;
    break;

  default:
    return false;
  }

  if (event_type == X_AXIS_MOTION) {
    if (evnt.jaxis.value > -100 && evnt.jaxis.value < 100) {
      if (previous_x_axis != KEY_NONE)
        HandleKeyReleased(previous_x_axis);
    } else {
      if (evnt.jaxis.value > 0)
        previous_x_axis = KEY_MOVE_RIGHT;
      else
        previous_x_axis = KEY_MOVE_LEFT;
      HandleKeyPressed(previous_x_axis);
    }
    return true;
  } else if (event_type == Y_AXIS_MOTION) {
    if (evnt.jaxis.value > -100 && evnt.jaxis.value < 100) {
      if(previous_y_axis != KEY_NONE)
        HandleKeyReleased(previous_y_axis);
    } else {
      if (evnt.jaxis.value > 0)
        previous_y_axis = KEY_DOWN;
      else
        previous_y_axis = KEY_UP;
      HandleKeyPressed(previous_y_axis);
    }
    return true;
  }

  std::map<int, std::vector<Key_t> >::iterator it = layout.find(evnt.jbutton.button);

  if (it == layout.end())
    return false;

  std::vector<Key_t> keys = it->second;
  std::vector<Key_t>::const_iterator itv;

  for (itv = keys.begin(); itv != keys.end() ; itv++) {
    if (event_type == KEY_PRESSED) {
      HandleKeyPressed(*itv);
      return true;
    }

    if (event_type == KEY_RELEASED) {
      HandleKeyReleased(*itv);
      return true;
    }
  }

  return false;
}

void Joystick::Reset()
{
  ManMachineInterface::Reset();

  if (init) {
    SDL_JoystickEventState(SDL_ENABLE);
    SDL_JoystickOpen(0);
  }
}