FreeStick  0.3
Cross Platform Open Sorce Joystick Lib
FSDirectInputJoystickManager.h
Go to the documentation of this file.
1 /**************************************************************************
2 Created by Alan Uthoff on 10/8/2013
3 Copyright (C) 2015.
4 
5 This Code is free software; you can redistribute it and/or
6 modify it under the terms of the zlib/libpng License as published
7 by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 This software is provided 'as-is', without any express or implied warranty.
10 
11 In no event will the authors be held liable for any damages arising from the use of this software.
12 
13 Permission is granted to anyone to use this software for any purpose,
14 including commercial applications, and to alter it and redistribute
15 it freely, subject to the following restrictions:
16 
17 1. The origin of this software must not be misrepresented;
18 you must not claim that you wrote the original software.
19 If you use this software in a product, an acknowledgment
20 in the product documentation would be appreciated but is not required.
21 
22 2. Altered source versions must be plainly marked as such,
23 and must not be misrepresented as being the original software.
24 
25 3. This notice may not be removed or altered from any source distribution.
26 **************************************************************************/
27 
28 #pragma once
31 #include "common/FSSpinlock.h"
32 #define DIRECTINPUT_VERSION 0x0800
33 #define WIN32_LEAN_AND_MEAN
34 #include <dinput.h>
35 #include <dinputd.h>
36 #include <mutex>
37 #include <memory>
38 #include <thread>
39 #include <atomic>
40 
41 inline bool operator<( const GUID & lhs, const GUID & rhs )
42 {
43  return memcmp(&lhs, &rhs, sizeof(GUID) )< 0;
44 }
45 
46 namespace freestick {
47  class FSDirectInputJoystickManager;
49  {
50  DIJOYCONFIG * joystickConfig;
51  bool isVaild;
53  std::vector<GUID> connectedLastUpdateJoysticks;
54  std::vector<GUID> joysticksConnectedThisUpdate;
55  };
56 
58  {
59  public:
61  virtual void init( );
62  virtual void update();
64  static bool IsXInputDeviceRaw( const GUID* pGuidProductFromDirectInput );
65  //Windows BOOL is type def for int
66  static BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,void* pContext );
67  static BOOL CALLBACK EnumInputObjectsCallback( const DIDEVICEOBJECTINSTANCE* pdidoi,VOID* pContext );
68 
69  private:
70 
71  LPDIRECTINPUT8 _directInput8;
72 
73  std::map<GUID,unsigned int> _directInputToDeviceIDMap;
74  DIJOYCONFIG PreferredJoyCfg;
75  DirectInput_Enum_Contex enumContext;
76  protected:
77  virtual void addDevice(FSBaseDevice * device);
78  virtual void removeDevice(FSBaseDevice * device);
79  virtual void addDevice(GUID guidDeviceInstance);
80  virtual void removeDevice(GUID guidDeviceInstance);
81  void updateJoysticksAxis(FSDirectInputJoystick & device,LONG axisValue, long int idForXAxis);
82  void updateJoysticksPOV(FSDirectInputJoystick & device,LONG axisValue, long int idForXAxis);
83 
84  void updateJoysticks();
85  private:
86  std::unordered_map<idNumber,LONG> lastPOVValue;
88  ConnctionLockType connectedJoystickLock;
89  std::vector<FSDirectInputJoystick * > joysticksToAddThisUpdate;
90  std::vector<const FSBaseDevice * > joysticksToRemoveThisUpdate;
91  std::unique_ptr<std::thread> connectedJoystickThread;
92  void updateConnectJoysticks();
93  FSSpinLock spinLock;
94  std::atomic_flag lookingForJoysticks;
95  };
96 
97 
98 }
Definition: FSDirectInputJoystickManager.h:57
static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, void *pContext)
Definition: FSDirectInputJoystickManager.cpp:314
void updateJoysticks()
Definition: FSDirectInputJoystickManager.cpp:197
std::vector< GUID > joysticksConnectedThisUpdate
Definition: FSDirectInputJoystickManager.h:54
bool operator<(const GUID &lhs, const GUID &rhs)
Definition: FSDirectInputJoystickManager.h:41
void updateJoysticksPOV(FSDirectInputJoystick &device, LONG axisValue, long int idForXAxis)
Definition: FSDirectInputJoystickManager.cpp:142
FSDirectInputJoystickManager()
Definition: FSDirectInputJoystickManager.cpp:39
bool isVaild
Definition: FSDirectInputJoystickManager.h:51
virtual void removeDevice(FSBaseDevice *device)
Definition: FSDirectInputJoystickManager.cpp:338
Definition: FSDirectInputJoystickManager.h:48
Definition: FSBaseDevice.h:32
Definition: FSDirectInputJoystick.h:36
virtual void init()
Definition: FSDirectInputJoystickManager.cpp:49
virtual void addDevice(FSBaseDevice *device)
Definition: FSDirectInputJoystickManager.cpp:332
static BOOL CALLBACK EnumInputObjectsCallback(const DIDEVICEOBJECTINSTANCE *pdidoi, VOID *pContext)
virtual ~FSDirectInputJoystickManager()
Definition: FSDirectInputJoystickManager.cpp:306
void updateJoysticksAxis(FSDirectInputJoystick &device, LONG axisValue, long int idForXAxis)
Definition: FSDirectInputJoystickManager.cpp:181
std::vector< GUID > connectedLastUpdateJoysticks
Definition: FSDirectInputJoystickManager.h:53
Definition: FSUpdatableJoystickDeviceManager.h:32
Definition: FSBaseDevice.h:35
freestick::FSDirectInputJoystickManager * manager
Definition: FSDirectInputJoystickManager.h:52
DIJOYCONFIG * joystickConfig
Definition: FSDirectInputJoystickManager.h:50
Definition: FSSpinlock.h:7
static bool IsXInputDeviceRaw(const GUID *pGuidProductFromDirectInput)
Definition: FSDirectInputJoystickManager.cpp:377
virtual void update()
Definition: FSDirectInputJoystickManager.cpp:123