Angel 3.2
A 2D Game Prototyping Engine
Controller.h
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #pragma once
31 
32 #include "../Infrastructure/VecStructs.h"
33 #include "../Infrastructure/Vector2.h"
34 #include "../Infrastructure/Log.h"
35 
36 #if defined(WIN32)
37  #include <XInput.h>
38  #pragma comment(lib, "XInput.lib")
39  #include <assert.h>
40 #elif defined(__APPLE__)
41  #include <Carbon/Carbon.h>
42  #include <IOKit/HID/IOHIDKeys.h>
43  #include "HID_Utilities_External.h"
44 #elif defined(__linux__)
45  #include <fcntl.h>
46  #include <linux/joystick.h>
47  #include <string.h>
48  #include <sstream>
49  #include <unistd.h>
50  #define LINUX_CONTROLLER_1_PATH "/dev/input/js0"
51  #define LINUX_CONTROLLER_2_PATH "/dev/input/js1"
52  #define LINUX_EVENT_INTERFACE "/dev/input/event"
53  #define MAX_LINUX_EVENT_INTERFACES 32
54 #endif
55 
58 {
59  int LeftThumbstickX;
60  int LeftThumbstickY;
61  int RightThumbstickX;
62  int RightThumbstickY;
63  int LeftTriggerValue;
64  int RightTriggerValue;
65  unsigned int Buttons;
66 };
67 
68 //forward declaration for the manager class
69 class Controller;
70 
71 //Limited to two because our wrapping is a little naive at the moment
72 #define MAX_CONTROLLERS 2
73 
75 
92 {
93 public:
100  static ControllerManager& GetInstance();
101 
110  Controller& GetController(int controllerIndex=0)
111  {
112  if ( (controllerIndex >= MAX_CONTROLLERS) || (controllerIndex < 0) )
113  {
114  sysLog.Log("ERROR: Bad controller index.");
115  #if defined(ANGEL_DEBUG)
116  assert(0);
117  #endif
118  }
119  return *_controllers[controllerIndex];
120  }
121 
125  void Setup();
126 
131  void UpdateState();
132 
137 
138 protected:
139  static ControllerManager *s_ControllerManager;
140 
141 private:
143  Controller* _controllers[MAX_CONTROLLERS];
144 };
145 
146 //Accessor and Singleton shortcuts
147 #define theControllerManager ControllerManager::GetInstance()
148 #define theController ControllerManager::GetInstance().GetController()
149 #define controllerOne theController
150 #define controllerTwo ControllerManager::GetInstance().GetController(1)
151 
153 
169 {
170 public:
171  friend class ControllerManager;
172 
177  void Setup();
178 
183  void UpdateState();
184 
191  const ControllerInput GetState();
192 
199  const Vector2 GetLeftThumbVec2();
200 
207  const Vector2 GetRightThumbVec2();
208 
215  const Vec2i GetLeftThumbstick();
216 
223  const Vec2i GetRightThumbstick();
224 
231  const int GetRightTrigger();
232 
239  const int GetLeftTrigger();
240 
266  const bool IsButtonDown(int buttonMask);
267 
268 
269  //Do these really need documentation comments? Sigh. I guess so.
270 
276  const bool IsAButtonDown();
277 
283  const bool IsBButtonDown();
284 
290  const bool IsXButtonDown();
291 
297  const bool IsYButtonDown();
298 
304  const bool IsLeftThumbstickButtonDown();
305 
311  const bool IsRightThumbstickButtonDown();
312 
318  const bool IsStartButtonDown();
319 
325  const bool IsBackButtonDown();
326 
332  const bool IsLeftBumperDown();
333 
339  const bool IsRightBumperDown();
340 
348  const bool IsLeftTriggerPressed();
349 
357  const bool IsRightTriggerPressed();
358 
367  void SetLeftVibrationRaw(unsigned int vibration);
368 
377  void SetRightVibrationRaw(unsigned int vibration);
378 
387  void SetLeftVibration(unsigned int vibration);
388 
397  void SetRightVibration(unsigned int vibration);
398 
404  const unsigned int GetLeftVibration();
405 
411  const unsigned int GetRightVibration();
412 
419  const bool IsConnected() { return _connected; }
420 
426  const int GetControllerID() { return _controllerID; }
427 
428 protected:
429  Controller();
430  static Controller *s_Controller;
431 
432 private:
433  int _controllerID;
434  bool _connected;
435  ControllerInput _currentControllerInput;
436 
437  unsigned int _currentLeftVibration;
438  unsigned int _currentRightVibration;
439 
440 #if defined(WIN32)
441  DWORD _dwResult;
442  XINPUT_STATE _currentControllerRawState;
443 #elif defined(__APPLE__)
444  pRecDevice _device;
445  hashmap_ns::hash_map<unsigned int, pRecElement> _elements;
446  const long _getValueForCookie(unsigned int cookie)
447  {
448  return HIDGetElementValue(_device, _elements[cookie]);
449  }
450 #elif defined(__linux__)
451  int _deviceFD;
452  int _ffFD;
453  struct ff_effect _ffEffect;
454  struct input_event _ffPlay;
455 #endif
456 };