swift
joystickwindows.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #ifndef SWIFT_INPUT_JOYSTICKWINDOWS_H
5 #define SWIFT_INPUT_JOYSTICKWINDOWS_H
6 
8 
9 #include <memory>
10 
11 #include <QVector>
12 
13 #include "input/joystick.h"
14 #include "input/swiftinputexport.h"
15 #include "misc/collection.h"
17 
18 #ifndef NOMINMAX
19 # define NOMINMAX
20 #endif
21 
22 #include <dinput.h>
23 
24 namespace swift::input
25 {
27  using DirectInput8Ptr = std::shared_ptr<IDirectInput8>;
28 
31  {
32  int m_offset;
34  };
35 
37  class CJoystickDevice : public QObject
38  {
39  Q_OBJECT
40 
41  public:
43  CJoystickDevice(DirectInput8Ptr directInputPtr, const DIDEVICEINSTANCE *pdidInstance,
44  QObject *parent = nullptr);
45 
47  bool init(HWND helperWindow);
48 
51 
53  GUID getDeviceGuid() const { return m_guidDevice; }
54 
55  signals:
57  void buttonChanged(const swift::misc::input::CJoystickButton &joystickButton, bool isPressed);
58 
60  void connectionLost(const GUID &guid);
61 
62  protected:
64  virtual void timerEvent(QTimerEvent *event) override;
65 
66  private:
67  friend bool operator==(const CJoystickDevice &lhs, const CJoystickDevice &rhs);
68 
69  struct DirectInputDevice8Deleter
70  {
71  void operator()(IDirectInputDevice8 *obj)
72  {
73  if (obj)
74  {
75  obj->Unacquire();
76  obj->Release();
77  }
78  }
79  };
80 
81  using DirectInputDevice8Ptr = std::unique_ptr<IDirectInputDevice8, DirectInputDevice8Deleter>;
82 
84  HRESULT pollDeviceState();
85 
87  static QString hrString(HRESULT hr);
88 
90  static BOOL CALLBACK enumObjectsCallback(const DIDEVICEOBJECTINSTANCE *dev, LPVOID pvRef);
91 
92  GUID m_guidDevice;
93  GUID m_guidProduct;
94  QString m_deviceName;
95  QString m_productName;
96  DirectInput8Ptr m_directInput;
97  DirectInputDevice8Ptr m_directInputDevice;
98  QVector<CJoystickDeviceInput> m_joystickDeviceInputs;
99  };
100 
102  bool operator==(CJoystickDevice const &lhs, CJoystickDevice const &rhs);
103 
106  {
107  Q_OBJECT
108 
109  public:
112 
115 
117  virtual ~CJoystickWindows() override;
118 
120  virtual swift::misc::input::CJoystickButtonList getAllAvailableJoystickButtons() const override;
121 
122  private:
123  friend class IJoystick;
124 
126  CJoystickWindows(QObject *parent = nullptr);
127 
129  HRESULT initDirectInput();
130 
132  HRESULT enumJoystickDevices();
133 
135  int createHelperWindow();
136 
139  void requestDeviceNotification();
140 
142  void destroyHelperWindow();
143 
145  void addJoystickDevice(const DIDEVICEINSTANCE *pdidInstance);
146 
148  void removeJoystickDevice(const GUID &guid);
149 
151  bool isJoystickAlreadyAdded(const DIDEVICEINSTANCE *pdidInstance) const;
152 
153  void joystickButtonChanged(const swift::misc::input::CJoystickButton &joystickButton, bool isPressed);
154 
155  static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
156 
158  static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, VOID *pContext);
159 
160  HWND helperWindow = nullptr;
161  HDEVNOTIFY hDevNotify = nullptr;
162 
163  const TCHAR *helperWindowClassName = TEXT("HelperWindow");
164  const TCHAR *helperWindowName = TEXT("JoystickCatcherWindow");
165 
166  bool m_coInitializeSucceeded = false;
167  DirectInput8Ptr m_directInput;
168  QVector<CJoystickDevice *> m_joystickDevices;
169 
170  swift::misc::input::CHotkeyCombination m_buttonCombination;
171  };
172 } // namespace swift::input
173 
174 #endif // SWIFT_INPUT_JOYSTICKWINDOWS_H
Linux Joystick device.
Definition: joysticklinux.h:24
swift::misc::input::CJoystickButtonList getDeviceButtons() const
Get all available device buttons.
bool init(const IOHIDDeviceRef device)
Initialize device.
void connectionLost(const GUID &guid)
Connection to joystick lost. Probably unplugged.
friend bool operator==(const CJoystickDevice &lhs, const CJoystickDevice &rhs)
Equal operator.
virtual void timerEvent(QTimerEvent *event)
Timer based updates.
void buttonChanged(const swift::misc::input::CJoystickButton &joystickButton, bool isPressed)
Joystick button changed.
GUID getDeviceGuid() const
Get device GUID.
CJoystickDevice(const QString &path, QFile *fd, QObject *parent)
Constructor.
Windows implemenation of IJoystick with DirectInput.
CJoystickWindows & operator=(CJoystickWindows const &)=delete
Assignment operator.
CJoystickWindows(CJoystickWindows const &)=delete
Copy Constructor.
Abstract interface for native joystick handling.
Definition: joystick.h:22
Value object representing hotkey sequence.
Value object representing a joystick button.
Value object encapsulating a list of joystick buttons.
std::shared_ptr< IDirectInput8 > DirectInput8Ptr
Shared IDirectInput8 ptr.
Joystick device input/button.
swift::misc::input::CJoystickButton m_button
Joystick button.
#define SWIFT_INPUT_EXPORT
Export a class or function from the library.