]> git.tdb.fi Git - libs/gui.git/blob - source/input/windows/gamecontroller.cpp
Add a type enumeration for devices
[libs/gui.git] / source / input / windows / gamecontroller.cpp
1 #include <msp/core/application.h>
2 #include <msp/strings/format.h>
3 #include "gamecontroller.h"
4 #include "gamecontroller_platform.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace Input {
10
11 vector<unsigned> GameController::Private::detected_controllers;
12
13 GameController::GameController(unsigned index):
14         Device(GAME_CONTROLLER)
15 {
16         if(!detect_done)
17                 detect();
18         if(index>=Private::detected_controllers.size())
19                 throw device_not_available(format("GameController(%d)", index));
20
21         unsigned user_index = Private::detected_controllers[index];
22
23         XINPUT_STATE state;
24         DWORD err = XInputGetState(user_index, &state);
25         if(err!=ERROR_SUCCESS)
26                 throw device_not_available(format("GameController(%d)", index));
27
28         priv = new Private;
29         priv->index = user_index;
30         priv->update_state(*this, state, false);
31
32         name = format("Controller %d", user_index);
33 }
34
35 GameController::~GameController()
36 {
37         use_event_dispatcher(0);
38
39         delete priv;
40 }
41
42 unsigned GameController::detect()
43 {
44         Private::detected_controllers.clear();
45
46         XINPUT_STATE state;
47         for(unsigned i=0; i<XUSER_MAX_COUNT; ++i)
48                 if(XInputGetState(i, &state)==ERROR_SUCCESS)
49                         Private::detected_controllers.push_back(i);
50
51         detect_done = true;
52         n_detected_controllers = Private::detected_controllers.size();
53
54         return Private::detected_controllers.size();
55 }
56
57 void GameController::use_event_dispatcher(IO::EventDispatcher *ed)
58 {
59         if(event_disp)
60                 event_disp->remove(*priv->event_pipe);
61
62         event_disp = ed;
63         if(event_disp)
64         {
65                 if(!priv->event_pipe)
66                 {
67                         priv->event_pipe = new IO::Pipe;
68                         priv->event_pipe->signal_data_available.connect(sigc::mem_fun(this, static_cast<void (GameController::*)()>(&GameController::tick)));
69                         priv->timer_slot = &GameControllerTimerThread::add_slot();
70                         priv->timer_slot->signal_timeout.connect(sigc::mem_fun(priv, &Private::generate_event));
71                 }
72
73                 event_disp->add(*priv->event_pipe);
74         }
75         else if(priv->event_pipe)
76         {
77                 GameControllerTimerThread::remove_slot(*priv->timer_slot);
78                 priv->timer_slot = 0;
79                 delete priv->event_pipe;
80                 priv->event_pipe = 0;
81         }
82 }
83
84 void GameController::tick()
85 {
86         if(priv->event_pipe)
87         {
88                 char buf[64];
89                 priv->event_pipe->read(buf, sizeof(buf));
90         }
91
92         XINPUT_STATE state;
93         DWORD err = XInputGetState(priv->index, &state);
94         if(err==ERROR_SUCCESS)
95                 priv->update_state(*this, state, true);
96 }
97
98 void GameController::tick(const Time::TimeDelta &)
99 {
100         tick();
101 }
102
103
104 GameController::Private::Private():
105         index(0),
106         last_packet_number(0),
107         event_pipe(0),
108         timer_slot(0)
109 { }
110
111 bool GameController::Private::generate_event()
112 {
113         event_pipe->put(1);
114         return true;
115 }
116
117 void GameController::Private::update_state(GameController &ctrl, const XINPUT_STATE &state, bool event)
118 {
119         if(state.dwPacketNumber==last_packet_number)
120                 return;
121         last_packet_number = state.dwPacketNumber;
122
123         ctrl.set_axis_value(0, state.Gamepad.sThumbLX/32768.0, event);
124         ctrl.set_axis_value(1, -state.Gamepad.sThumbLY/32768.0, event);
125         ctrl.set_axis_value(3, state.Gamepad.sThumbRX/32768.0, event);
126         ctrl.set_axis_value(4, -state.Gamepad.sThumbRY/32768.0, event);
127         WORD dpad_x = state.Gamepad.wButtons&(XINPUT_GAMEPAD_DPAD_LEFT|XINPUT_GAMEPAD_DPAD_RIGHT);
128         WORD dpad_y = state.Gamepad.wButtons&(XINPUT_GAMEPAD_DPAD_UP|XINPUT_GAMEPAD_DPAD_DOWN);
129         ctrl.set_axis_value(6, (dpad_x==XINPUT_GAMEPAD_DPAD_LEFT ? -1 : dpad_x==XINPUT_GAMEPAD_DPAD_RIGHT ? 1 : 0), event);
130         ctrl.set_axis_value(7, (dpad_y==XINPUT_GAMEPAD_DPAD_UP ? -1 : dpad_y==XINPUT_GAMEPAD_DPAD_DOWN ? 1 : 0), event);
131         ctrl.set_button_state(0, state.Gamepad.wButtons&XINPUT_GAMEPAD_A, event);
132         ctrl.set_button_state(1, state.Gamepad.wButtons&XINPUT_GAMEPAD_B, event);
133         ctrl.set_button_state(2, state.Gamepad.wButtons&XINPUT_GAMEPAD_X, event);
134         ctrl.set_button_state(3, state.Gamepad.wButtons&XINPUT_GAMEPAD_Y, event);
135         ctrl.set_button_state(4, state.Gamepad.wButtons&XINPUT_GAMEPAD_LEFT_SHOULDER, event);
136         ctrl.set_button_state(5, state.Gamepad.wButtons&XINPUT_GAMEPAD_RIGHT_SHOULDER, event);
137         ctrl.set_button_state(6, state.Gamepad.wButtons&XINPUT_GAMEPAD_BACK, event);
138         ctrl.set_button_state(7, state.Gamepad.wButtons&XINPUT_GAMEPAD_START, event);
139         ctrl.set_button_state(8, state.Gamepad.wButtons&XINPUT_GAMEPAD_LEFT_THUMB, event);
140         ctrl.set_button_state(9, state.Gamepad.wButtons&XINPUT_GAMEPAD_RIGHT_THUMB, event);
141 }
142
143
144 GameControllerTimerThread *GameControllerTimerThread::thread = 0;
145
146 GameControllerTimerThread::GameControllerTimerThread():
147         Thread("GameController"),
148         n_users(0)
149 {
150         launch();
151 }
152
153 GameControllerTimerThread::~GameControllerTimerThread()
154 {
155         timer.add(Time::zero);
156         join();
157 }
158
159 Time::Timer::Slot &GameControllerTimerThread::add_slot()
160 {
161         if(!thread)
162                 thread = new GameControllerTimerThread;
163         ++thread->n_users;
164         return thread->timer.add(100*Time::msec);
165 }
166
167 void GameControllerTimerThread::remove_slot(Time::Timer::Slot &slot)
168 {
169         thread->timer.cancel(slot);
170         if(!--thread->n_users)
171         {
172                 thread->timer.add(Time::zero);
173                 delete thread;
174                 thread = 0;
175         }
176 }
177
178 void GameControllerTimerThread::main()
179 {
180         while(1)
181         {
182                 timer.tick();
183                 if(!n_users)
184                         break;
185         }
186 }
187
188 } // namespace Input
189 } // namespace Msp