xbmc
PeripheralCecAdapter.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #if !defined(HAVE_LIBCEC)
12 #include "Peripheral.h"
13 
14 // an empty implementation, so CPeripherals can be compiled without a bunch of #ifdef's when libCEC
15 // is not available
16 namespace PERIPHERALS
17 {
19 {
20 public:
21  bool HasAudioControl(void) { return false; }
22  void VolumeUp(void) {}
23  void VolumeDown(void) {}
24  bool IsMuted(void) { return false; }
25  void ToggleMute(void) {}
26  bool ToggleDeviceState(CecStateChange mode = STATE_SWITCH_TOGGLE, bool forceType = false)
27  {
28  return false;
29  }
30 
31  int GetButton(void) { return 0; }
32  unsigned int GetHoldTime(void) { return 0; }
33  void ResetButton(void) {}
34 };
35 } // namespace PERIPHERALS
36 
37 #else
38 
39 #include "PeripheralHID.h"
40 #include "XBDateTime.h"
41 #include "interfaces/AnnouncementManager.h"
42 #include "threads/CriticalSection.h"
43 #include "threads/Thread.h"
44 
45 #include <chrono>
46 #include <queue>
47 #include <vector>
48 
49 // undefine macro isset, it collides with function in cectypes.h
50 #ifdef isset
51 #undef isset
52 #endif
53 #include <libcec/cectypes.h>
54 
55 class CVariant;
56 
57 namespace CEC
58 {
59 class ICECAdapter;
60 };
61 
62 namespace PERIPHERALS
63 {
64 class CPeripheralCecAdapterUpdateThread;
66 
67 typedef struct
68 {
69  int iButton;
70  unsigned int iDuration;
71 } CecButtonPress;
72 
73 typedef enum
74 {
75  VOLUME_CHANGE_NONE,
76  VOLUME_CHANGE_UP,
77  VOLUME_CHANGE_DOWN,
78  VOLUME_CHANGE_MUTE
79 } CecVolumeChange;
80 
83  private CThread
84 {
85  friend class CPeripheralCecAdapterUpdateThread;
86  friend class CPeripheralCecAdapterReopenJob;
87 
88 public:
90  const PeripheralScanResult& scanResult,
91  CPeripheralBus* bus);
92  ~CPeripheralCecAdapter(void) override;
93 
94  void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
95  const std::string& sender,
96  const std::string& message,
97  const CVariant& data) override;
98 
99  // audio control
100  bool HasAudioControl(void);
101  void VolumeUp(void);
102  void VolumeDown(void);
103  void ToggleMute(void);
104  bool IsMuted(void);
105 
106  // CPeripheral callbacks
107  void OnSettingChanged(const std::string& strChangedSetting) override;
108  void OnDeviceRemoved(void) override;
109 
110  // input
111  int GetButton(void);
112  unsigned int GetHoldTime(void);
113  void ResetButton(void);
114 
115  // public CEC methods
116  void ActivateSource(void);
117  void StandbyDevices(void);
118  bool ToggleDeviceState(CecStateChange mode = STATE_SWITCH_TOGGLE, bool forceType = false);
119 
120 private:
121  bool InitialiseFeature(const PeripheralFeature feature) override;
122  void ResetMembers(void);
123  void Process(void) override;
124  bool IsRunning(void) const;
125 
126  bool OpenConnection(void);
127  bool ReopenConnection(bool bAsync = false);
128 
129  void SetConfigurationFromSettings(void);
130  void SetConfigurationFromLibCEC(const CEC::libcec_configuration& config);
131  void SetVersionInfo(const CEC::libcec_configuration& configuration);
132 
133  static void ReadLogicalAddresses(const std::string& strString,
134  CEC::cec_logical_addresses& addresses);
135  static void ReadLogicalAddresses(int iLocalisedId, CEC::cec_logical_addresses& addresses);
136  bool WriteLogicalAddresses(const CEC::cec_logical_addresses& addresses,
137  const std::string& strSettingName,
138  const std::string& strAdvancedSettingName);
139 
140  void ProcessActivateSource(void);
141  void ProcessStandbyDevices(void);
142  void ProcessVolumeChange(void);
143 
144  void PushCecKeypress(const CEC::cec_keypress& key);
145  void PushCecKeypress(const CecButtonPress& key);
146  void GetNextKey(void);
147 
148  void SetAudioSystemConnected(bool bSetTo);
149  void SetMenuLanguage(const char* strLanguage);
150  void OnTvStandby(void);
151 
152  // callbacks from libCEC
153  static void CecLogMessage(void* cbParam, const CEC::cec_log_message* message);
154  static void CecCommand(void* cbParam, const CEC::cec_command* command);
155  static void CecConfiguration(void* cbParam, const CEC::libcec_configuration* config);
156  static void CecAlert(void* cbParam,
157  const CEC::libcec_alert alert,
158  const CEC::libcec_parameter data);
159  static void CecSourceActivated(void* param,
160  const CEC::cec_logical_address address,
161  const uint8_t activated);
162  static void CecKeyPress(void* cbParam, const CEC::cec_keypress* key);
163 
164  CEC::ICECAdapter* m_cecAdapter;
165  bool m_bStarted;
166  bool m_bHasButton;
167  bool m_bIsReady;
168  bool m_bHasConnectedAudioSystem;
169  std::string m_strMenuLanguage;
170  CDateTime m_standbySent;
171  std::vector<CecButtonPress> m_buttonQueue;
172  CecButtonPress m_currentButton;
173  std::queue<CecVolumeChange> m_volumeChangeQueue;
174  std::chrono::time_point<std::chrono::steady_clock> m_lastKeypress;
175  CecVolumeChange m_lastChange;
176  int m_iExitCode;
177  bool m_bIsMuted;
178  bool m_bGoingToStandby;
179  bool m_bIsRunning;
180  bool m_bDeviceRemoved;
181  CPeripheralCecAdapterUpdateThread* m_queryThread;
182  CEC::ICECCallbacks m_callbacks;
183  mutable CCriticalSection m_critSection;
184  CEC::libcec_configuration m_configuration;
185  bool m_bActiveSourcePending;
186  bool m_bStandbyPending;
187  CDateTime m_preventActivateSourceOnPlay;
188  bool m_bActiveSourceBeforeStandby;
189  bool m_bOnPlayReceived;
190  bool m_bPlaybackPaused;
191  std::string m_strComPort;
192  bool m_bPowerOnScreensaver;
193  bool m_bUseTVMenuLanguage;
194  bool m_bSendInactiveSource;
195  bool m_bPowerOffScreensaver;
196  bool m_bShutdownOnStandby;
197 };
198 
199 class CPeripheralCecAdapterUpdateThread : public CThread
200 {
201 public:
202  CPeripheralCecAdapterUpdateThread(CPeripheralCecAdapter* adapter,
203  CEC::libcec_configuration* configuration);
204  ~CPeripheralCecAdapterUpdateThread(void) override;
205 
206  void Signal(void);
207  bool UpdateConfiguration(CEC::libcec_configuration* configuration);
208 
209 protected:
210  void UpdateMenuLanguage(void);
211  std::string UpdateAudioSystemStatus(void);
212  bool WaitReady(void);
213  bool SetInitialConfiguration(void);
214  void Process(void) override;
215 
216  CPeripheralCecAdapter* m_adapter;
217  CEvent m_event;
218  CCriticalSection m_critSection;
219  CEC::libcec_configuration m_configuration;
220  CEC::libcec_configuration m_nextConfiguration;
221  bool m_bNextConfigurationScheduled;
222  bool m_bIsUpdating;
223 };
224 } // namespace PERIPHERALS
225 
226 #endif
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Definition: PeripheralCecAdapter.h:18
Definition: deflate.c:123
Definition: PeripheralTypes.h:318
Definition: Thread.h:44
Definition: PeripheralCecAdapter.cpp:1749
Definition: RetroPlayerInput.h:15
Definition: IAnnouncer.h:70
Definition: PeripheralHID.h:16
Definition: Variant.h:29
Definition: PeripheralBusCEC.h:19
virtual bool InitialiseFeature(const PeripheralFeature feature)
Initialise one of the features of this peripheral.
Definition: Peripheral.h:120
DateTime class, which uses FileTime as it&#39;s base.
Definition: XBDateTime.h:63
Definition: PeripheralBus.h:34
virtual void OnSettingChanged(const std::string &strChangedSetting)
Called when a setting changed.
Definition: Peripheral.h:138
Definition: Peripheral.h:62
Definition: Peripherals.h:49
virtual void OnDeviceRemoved(void)
Called when this device is removed, before calling the destructor.
Definition: Peripheral.h:143