[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / input / windows / WINJoystick.h
1 #pragma once
2 /*
3 *      Copyright (C) 2012-2013 Team XBMC
4 *      http://www.xbmc.org
5 *
6 *  This Program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2, or (at your option)
9 *  any later version.
10 *
11 *  This Program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with XBMC; see the file COPYING.  If not, see
18 *  <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include <vector>
23 #include <string>
24 #include "threads/CriticalSection.h"
25
26 #define JACTIVE_BUTTON 0x00000001
27 #define JACTIVE_AXIS   0x00000002
28 #define JACTIVE_HAT    0x00000004
29 #define JACTIVE_NONE   0x00000000
30 #define JACTIVE_HAT_UP    0x01
31 #define JACTIVE_HAT_RIGHT 0x02
32 #define JACTIVE_HAT_DOWN  0x04
33 #define JACTIVE_HAT_LEFT  0x08
34
35 #define MAX_AXES 8
36
37 // Class to manage all connected joysticks
38
39 class CJoystick
40 {
41 public:
42   CJoystick();
43   ~CJoystick();
44
45   void Initialize();
46   void Reset(bool axis=false);
47   void ResetAxis(int axisId) { m_Amount[axisId] = 0; }
48   void Update();
49   bool GetButton (int& id, bool consider_repeat=true);
50   bool GetAxis (int &id);
51   bool GetHat (int &id, int &position, bool consider_repeat=true);
52   std::string GetJoystick() { return (m_JoyId>-1)?m_JoystickNames[m_JoyId]:""; }
53   int GetAxisWithMaxAmount();
54   float GetAmount(int axis);
55   float GetAmount() { return GetAmount(m_AxisId); }
56   bool IsEnabled() const { return m_joystickEnabled; }
57   void SetEnabled(bool enabled = true);
58   float SetDeadzone(float val);
59   bool Reinitialize();
60   void Acquire();
61
62 private:
63   void SetAxisActive(bool active=true) { m_ActiveFlags = active?(m_ActiveFlags|JACTIVE_AXIS):(m_ActiveFlags&(~JACTIVE_AXIS)); }
64   void SetButtonActive(bool active=true) { m_ActiveFlags = active?(m_ActiveFlags|JACTIVE_BUTTON):(m_ActiveFlags&(~JACTIVE_BUTTON)); }
65   void SetHatActive(bool active=true) { m_ActiveFlags = active?(m_ActiveFlags|JACTIVE_HAT):(m_ActiveFlags&(~JACTIVE_HAT)); }
66   bool IsButtonActive() { return (m_ActiveFlags & JACTIVE_BUTTON) == JACTIVE_BUTTON; }
67   bool IsAxisActive() { return (m_ActiveFlags & JACTIVE_AXIS) == JACTIVE_AXIS; }
68   bool IsHatActive() { return (m_ActiveFlags & JACTIVE_HAT) == JACTIVE_HAT; }
69
70   void ReleaseJoysticks();
71   static BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext );
72   static BOOL CALLBACK EnumObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext );
73
74   int m_Amount[MAX_AXES];
75   int m_AxisId;
76   int m_ButtonId;
77   uint8_t m_HatState;
78   int m_HatId;
79   int m_JoyId;
80   int m_NumAxes;
81   int m_DeadzoneRange;
82   bool m_joystickEnabled;
83   uint32_t m_pressTicksButton;
84   uint32_t m_pressTicksHat;
85   uint8_t m_ActiveFlags;
86   uint32_t m_lastPressTicks;
87   uint32_t m_lastTicks;
88   CCriticalSection m_critSection;
89
90   LPDIRECTINPUT8  m_pDI;
91   std::vector<LPDIRECTINPUTDEVICE8> m_pJoysticks;
92   std::vector<std::string> m_JoystickNames;
93   std::vector<DIDEVCAPS> m_devCaps;
94 };
95
96 extern CJoystick g_Joystick;