swift
joystickmacos.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #ifndef SWIFT_INPUT_JOYSTICKMACOS_H
5 #define SWIFT_INPUT_JOYSTICKMACOS_H
6 
8 
9 #include <IOKit/hid/IOHIDManager.h>
10 
11 #include <QHash>
12 
13 #include "input/joystick.h"
15 
16 namespace swift::input
17 {
18 
20  class CJoystickDevice : public QObject
21  {
22  Q_OBJECT
23 
24  public:
26  CJoystickDevice(QObject *parent = nullptr);
27 
29  virtual ~CJoystickDevice() override;
30 
32  bool init(const IOHIDDeviceRef device);
33 
36 
38  IOHIDDeviceRef getNativeDevice() const { return m_deviceRef; }
39 
40  signals:
42  void buttonChanged(const swift::misc::input::CJoystickButton &joystickButton, bool isPressed);
43 
44  private:
45  void processButtonEvent(IOHIDValueRef value);
46 
47  static void valueCallback(void *context, IOReturn result, void *sender, IOHIDValueRef value);
48 
49  QString m_deviceName = "unknown";
50  // IOHIDDeviceRef is owned by IOHIDManager. Do not release it.
51  IOHIDDeviceRef m_deviceRef = nullptr;
53  };
54 
56  class CJoystickMacOS : public IJoystick
57  {
58  Q_OBJECT
59 
60  public:
62  CJoystickMacOS(CJoystickMacOS const &) = delete;
63 
66 
68  virtual ~CJoystickMacOS() override;
69 
72 
73  protected:
74  virtual bool init() override;
75 
76  private:
77  friend class IJoystick;
78 
80  CJoystickMacOS(QObject *parent = nullptr);
81 
83  void addJoystickDevice(const IOHIDDeviceRef device);
84 
86  void removeJoystickDevice(const IOHIDDeviceRef device);
87 
88  void joystickButtonChanged(const swift::misc::input::CJoystickButton &joystickButton, bool isPressed);
89 
90  static void matchCallback(void *context, IOReturn result, void *sender, IOHIDDeviceRef device);
91  static void removeCallback(void *context, IOReturn result, void *sender, IOHIDDeviceRef device);
92 
93  IOHIDManagerRef m_hidManager = nullptr;
94  QVector<CJoystickDevice *> m_joystickDevices;
95 
96  swift::misc::input::CHotkeyCombination m_buttonCombination;
97  };
98 
99 } // namespace swift::input
100 
101 #endif // SWIFT_INPUT_JOYSTICKMACOS_H
bool init(const IOHIDDeviceRef device)
Initialize device.
IOHIDDeviceRef getNativeDevice() const
Return the native IOHIDDeviceRef.
Definition: joystickmacos.h:38
virtual ~CJoystickDevice()
Destructor.
swift::misc::input::CJoystickButtonList getDeviceButtons() const
Get all available device buttons.
void buttonChanged(const swift::misc::input::CJoystickButton &joystickButton, bool isPressed)
Joystick button changed.
CJoystickDevice(const QString &path, QFile *fd, QObject *parent)
Constructor.
MacOS implemenation of IJoystick.
Definition: joystickmacos.h:57
CJoystickMacOS & operator=(CJoystickMacOS const &)=delete
Assignment operator.
CJoystickMacOS(CJoystickMacOS const &)=delete
Copy Constructor.
virtual bool init()
Initializes the platform joystick devices.
virtual ~CJoystickMacOS()
Destructor.
virtual swift::misc::input::CJoystickButtonList getAllAvailableJoystickButtons() const
Get all available joystick buttons.
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.