summaryrefslogtreecommitdiffstats
path: root/xbmc/games/controllers/windows/GUIConfigurationWizard.h
blob: b69badf5c9a089c57e86bd05ff420dd433651db5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 *  Copyright (C) 2014-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "IConfigurationWindow.h"
#include "games/controllers/input/PhysicalFeature.h"
#include "input/XBMC_keysym.h"
#include "input/joysticks/DriverPrimitive.h"
#include "input/joysticks/interfaces/IButtonMapper.h"
#include "input/keyboard/interfaces/IKeyboardDriverHandler.h"
#include "threads/CriticalSection.h"
#include "threads/Event.h"
#include "threads/Thread.h"
#include "utils/Observer.h"

#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

namespace KODI
{
namespace KEYBOARD
{
class IActionMap;
}

namespace GAME
{
class CGUIConfigurationWizard : public IConfigurationWizard,
                                public JOYSTICK::IButtonMapper,
                                public KEYBOARD::IKeyboardDriverHandler,
                                public Observer,
                                protected CThread
{
public:
  CGUIConfigurationWizard();

  ~CGUIConfigurationWizard() override;

  // implementation of IConfigurationWizard
  void Run(const std::string& strControllerId,
           const std::vector<IFeatureButton*>& buttons) override;
  void OnUnfocus(IFeatureButton* button) override;
  bool Abort(bool bWait = true) override;
  void RegisterKey(const CPhysicalFeature& key) override;
  void UnregisterKeys() override;

  // implementation of IButtonMapper
  std::string ControllerID() const override { return m_strControllerId; }
  bool NeedsCooldown() const override { return true; }
  bool AcceptsPrimitive(JOYSTICK::PRIMITIVE_TYPE type) const override { return true; }
  bool MapPrimitive(JOYSTICK::IButtonMap* buttonMap,
                    IKeymap* keymap,
                    const JOYSTICK::CDriverPrimitive& primitive) override;
  void OnEventFrame(const JOYSTICK::IButtonMap* buttonMap, bool bMotion) override;
  void OnLateAxis(const JOYSTICK::IButtonMap* buttonMap, unsigned int axisIndex) override;

  // implementation of IKeyboardDriverHandler
  bool OnKeyPress(const CKey& key) override;
  void OnKeyRelease(const CKey& key) override {}

  // implementation of Observer
  void Notify(const Observable& obs, const ObservableMessage msg) override;

protected:
  // implementation of CThread
  void Process() override;

private:
  void InitializeState(void);

  bool IsMapping() const;
  bool IsMapping(const std::string& location) const;

  void InstallHooks(void);
  void RemoveHooks(void);

  void OnMotion(const JOYSTICK::IButtonMap* buttonMap);
  void OnMotionless(const JOYSTICK::IButtonMap* buttonMap);

  bool OnAction(unsigned int actionId);

  // Run() parameters
  std::string m_strControllerId;
  std::vector<IFeatureButton*> m_buttons;

  // State variables and mutex
  IFeatureButton* m_currentButton;
  INPUT::CARDINAL_DIRECTION m_cardinalDirection;
  JOYSTICK::WHEEL_DIRECTION m_wheelDirection;
  JOYSTICK::THROTTLE_DIRECTION m_throttleDirection;
  std::set<JOYSTICK::CDriverPrimitive> m_history; // History to avoid repeated features
  bool m_lateAxisDetected; // Set to true if an axis is detected during button mapping
  std::string m_location; // Peripheral location of device that we're mapping
  bool m_bIsKeyboard = false; // True if we're mapping keyboard keys
  CCriticalSection m_stateMutex;

  // Synchronization events
  CEvent m_inputEvent;
  CEvent m_motionlessEvent;
  CCriticalSection m_motionMutex;
  std::set<const JOYSTICK::IButtonMap*> m_bInMotion;

  // Keyboard handling
  std::unique_ptr<KEYBOARD::IActionMap> m_actionMap;
  std::map<XBMCKey, CPhysicalFeature> m_keyMap; // Keycode -> feature
};
} // namespace GAME
} // namespace KODI