summaryrefslogtreecommitdiffstats
path: root/xbmc/peripherals/Peripherals.h
blob: d9931d76990346b44633d382660d325d0ba2bdb5 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 *  Copyright (C) 2005-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 "IEventScannerCallback.h"
#include "bus/PeripheralBus.h"
#include "devices/Peripheral.h"
#include "interfaces/IAnnouncer.h"
#include "messaging/IMessageTarget.h"
#include "settings/lib/ISettingCallback.h"
#include "threads/CriticalSection.h"
#include "threads/Thread.h"
#include "utils/Observer.h"

#include <memory>
#include <vector>

class CFileItemList;
class CInputManager;
class CSetting;
class CSettingsCategory;
class TiXmlElement;
class CAction;
class CKey;

namespace KODI
{
namespace GAME
{
class CControllerManager;
}

namespace JOYSTICK
{
class IButtonMapper;
}
} // namespace KODI

namespace PERIPHERALS
{
class CEventScanner;

class CPeripherals : public ISettingCallback,
                     public Observable,
                     public KODI::MESSAGING::IMessageTarget,
                     public IEventScannerCallback,
                     public ANNOUNCEMENT::IAnnouncer
{
public:
  explicit CPeripherals(CInputManager& inputManager,
                        KODI::GAME::CControllerManager& controllerProfiles);

  ~CPeripherals() override;

  /*!
   * @brief Initialise the peripherals manager.
   */
  void Initialise();

  /*!
   * @brief Clear all data known by the peripherals manager.
   */
  void Clear();

  /*!
   * @brief Get the instance of the peripheral at the given location.
   * @param strLocation The location.
   * @param busType The bus to query. Default (PERIPHERAL_BUS_UNKNOWN) searches all busses.
   * @return The peripheral or NULL if it wasn't found.
   */
  PeripheralPtr GetPeripheralAtLocation(const std::string& strLocation,
                                        PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;

  /*!
   * @brief Check whether a peripheral is present at the given location.
   * @param strLocation The location.
   * @param busType The bus to query. Default (PERIPHERAL_BUS_UNKNOWN) searches all busses.
   * @return True when a peripheral was found, false otherwise.
   */
  bool HasPeripheralAtLocation(const std::string& strLocation,
                               PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;

  /*!
   * @brief Get the bus that holds the device with the given location.
   * @param strLocation The location.
   * @return The bus or NULL if no device was found.
   */
  PeripheralBusPtr GetBusWithDevice(const std::string& strLocation) const;

  /*!
   * @brief Check if any busses support the given feature
   * @param feature The feature to check for
   * @return True if a bus supports the feature, false otherwise
   */
  bool SupportsFeature(PeripheralFeature feature) const;

  /*!
   * @brief Get all peripheral instances that have the given feature.
   * @param results The list of results.
   * @param feature The feature to search for.
   * @param busType The bus to query. Default (PERIPHERAL_BUS_UNKNOWN) searches all busses.
   * @return The number of devices that have been found.
   */
  int GetPeripheralsWithFeature(PeripheralVector& results,
                                const PeripheralFeature feature,
                                PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;

  size_t GetNumberOfPeripherals() const;

  /*!
   * @brief Check whether there is at least one device present with the given feature.
   * @param feature The feature to check for.
   * @param busType The bus to query. Default (PERIPHERAL_BUS_UNKNOWN) searches all busses.
   * @return True when at least one device was found with this feature, false otherwise.
   */
  bool HasPeripheralWithFeature(const PeripheralFeature feature,
                                PeripheralBusType busType = PERIPHERAL_BUS_UNKNOWN) const;

  /*!
   * @brief Called when a device has been added to a bus.
   * @param bus The bus the device was added to.
   * @param peripheral The peripheral that has been added.
   */
  void OnDeviceAdded(const CPeripheralBus& bus, const CPeripheral& peripheral);

  /*!
   * @brief Called when a device has been deleted from a bus.
   * @param bus The bus from which the device removed.
   * @param peripheral The peripheral that has been removed.
   */
  void OnDeviceDeleted(const CPeripheralBus& bus, const CPeripheral& peripheral);

  /*!
   * @brief Creates a new instance of a peripheral.
   * @param bus The bus on which this peripheral is present.
   * @param result The scan result from the device scanning code.
   * @return The new peripheral or NULL if it could not be created.
   */
  void CreatePeripheral(CPeripheralBus& bus, const PeripheralScanResult& result);

  /*!
   * @brief Add the settings that are defined in the mappings file to the peripheral (if there is
   * anything defined).
   * @param peripheral The peripheral to get the settings for.
   */
  void GetSettingsFromMapping(CPeripheral& peripheral) const;

  /*!
   * @brief Trigger a device scan on all known busses
   */
  void TriggerDeviceScan(const PeripheralBusType type = PERIPHERAL_BUS_UNKNOWN);

  /*!
   * @brief Get the instance of a bus given it's type.
   * @param type The bus type.
   * @return The bus or NULL if it wasn't found.
   */
  PeripheralBusPtr GetBusByType(const PeripheralBusType type) const;

  /*!
   * @brief Get all fileitems for a path.
   * @param strPath The path to the directory to get the items from.
   * @param items The item list.
   */
  void GetDirectory(const std::string& strPath, CFileItemList& items) const;

  /*!
   * @brief Get the instance of a peripheral given it's path.
   * @param strPath The path to the peripheral.
   * @return The peripheral or NULL if it wasn't found.
   */
  PeripheralPtr GetByPath(const std::string& strPath) const;

  /*!
   * @brief Try to let one of the peripherals handle an action.
   * @param action The change to handle.
   * @return True when this change was handled by a peripheral (and should not be handled by
   * anything else), false otherwise.
   */
  bool OnAction(const CAction& action);

  /*!
   * @brief Check whether there's a peripheral that reports to be muted.
   * @return True when at least one peripheral reports to be muted, false otherwise.
   */
  bool IsMuted();

  /*!
   * @brief Try to toggle the mute status via a peripheral.
   * @return True when this change was handled by a peripheral (and should not be handled by
   * anything else), false otherwise.
   */
  bool ToggleMute();

  /*!
   * @brief Try to toggle the playing device state via a peripheral.
   * @param mode Whether to activate, put on standby or toggle the source.
   * @return True when the playing device has been switched on, false otherwise.
   */
  bool ToggleDeviceState(const CecStateChange mode = STATE_SWITCH_TOGGLE);

  /*!
   * @brief Try to mute the audio via a peripheral.
   * @return True when this change was handled by a peripheral (and should not be handled by
   * anything else), false otherwise.
   */
  bool Mute()
  {
    return ToggleMute();
  } //! @todo CEC only supports toggling the mute status at this time

  /*!
   * @brief Try to unmute the audio via a peripheral.
   * @return True when this change was handled by a peripheral (and should not be handled by
   * anything else), false otherwise.
   */
  bool UnMute()
  {
    return ToggleMute();
  } //! @todo CEC only supports toggling the mute status at this time

  /*!
   * @brief Try to get a keypress from a peripheral.
   * @param frameTime The current frametime.
   * @param key The fetched key.
   * @return True when a keypress was fetched, false otherwise.
   */
  bool GetNextKeypress(float frameTime, CKey& key);

  /*!
   * @brief Register with the event scanner to control scan timing
   * @return A handle that unregisters itself when expired
   */
  EventPollHandlePtr RegisterEventPoller();

  /*!
   * @brief Register with the event scanner to disable event processing
   * @return A handle that unregisters itself when expired
   */
  EventLockHandlePtr RegisterEventLock();

  /*!
   *
   */
  void OnUserNotification();

  /*!
   * @brief Request peripherals with the specified feature to perform a quick test
   * @return true if any peripherals support the feature, false otherwise
   */
  void TestFeature(PeripheralFeature feature);

  /*!
   * \brief Request all devices with power-off support to power down
   */
  void PowerOffDevices();

  bool SupportsCEC() const
  {
#if defined(HAVE_LIBCEC)
    return true;
#else
    return false;
#endif
  }

  // implementation of IEventScannerCallback
  void ProcessEvents(void) override;

  /*!
   * \brief Initialize button mapping
   *
   * This command enables button mapping on all busses. Button maps allow
   * connect events from the driver to the higher-level features used by
   * controller profiles.
   *
   * If user input is required, a blocking dialog may be shown.
   */
  void EnableButtonMapping();

  /*!
   * \brief Get an add-on that can provide button maps for a device
   * \return An add-on that provides button maps, or empty if no add-on is found
   */
  PeripheralAddonPtr GetAddonWithButtonMap(const CPeripheral* device);

  /*!
   * \brief Reset all button maps to the defaults for all devices and the given controller
   * \param controllerId The controller profile to reset
   * @todo Add a device parameter to allow resetting button maps per-device
   */
  void ResetButtonMaps(const std::string& controllerId);

  /*!
   * \brief Register a button mapper interface
   * \param mapper The button mapper
   *
   * Clients implementing the IButtonMapper interface call
   * \ref CPeripherals::RegisterJoystickButtonMapper to register themselves
   * as eligible for button mapping commands.
   *
   * When registering the mapper is forwarded to all peripherals. See
   * \ref CPeripheral::RegisterJoystickButtonMapper for what is done to the
   * mapper after being given to the peripheral.
   */
  void RegisterJoystickButtonMapper(KODI::JOYSTICK::IButtonMapper* mapper);

  /*!
   * \brief Unregister a button mapper interface
   * \param mapper The button mapper
   */
  void UnregisterJoystickButtonMapper(KODI::JOYSTICK::IButtonMapper* mapper);

  // implementation of ISettingCallback
  void OnSettingChanged(const std::shared_ptr<const CSetting>& setting) override;
  void OnSettingAction(const std::shared_ptr<const CSetting>& setting) override;

  // implementation of IMessageTarget
  void OnApplicationMessage(KODI::MESSAGING::ThreadMessage* pMsg) override;
  int GetMessageMask() override;

  // implementation of IAnnouncer
  void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
                const std::string& sender,
                const std::string& message,
                const CVariant& data) override;

  /*!
   * \brief Access the input manager passed to the constructor
   */
  CInputManager& GetInputManager() { return m_inputManager; }

  /*!
   * \brief Access controller profiles through the construction parameter
   */
  KODI::GAME::CControllerManager& GetControllerProfiles() { return m_controllerProfiles; }

  /*!
   * \brief Get a mutex that allows for add-on install tasks to block on each other
   */
  CCriticalSection& GetAddonInstallMutex() { return m_addonInstallMutex; }

private:
  bool LoadMappings();
  bool GetMappingForDevice(const CPeripheralBus& bus, PeripheralScanResult& result) const;
  static void GetSettingsFromMappingsFile(
      TiXmlElement* xmlNode, std::map<std::string, PeripheralDeviceSetting>& m_settings);

  void OnDeviceChanged();

  // Construction parameters
  CInputManager& m_inputManager;
  KODI::GAME::CControllerManager& m_controllerProfiles;

#if !defined(HAVE_LIBCEC)
  bool m_bMissingLibCecWarningDisplayed = false;
#endif
  std::vector<PeripheralBusPtr> m_busses;
  std::vector<PeripheralDeviceMapping> m_mappings;
  std::unique_ptr<CEventScanner> m_eventScanner;
  mutable CCriticalSection m_critSectionBusses;
  mutable CCriticalSection m_critSectionMappings;
  CCriticalSection m_addonInstallMutex;
};
} // namespace PERIPHERALS