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