Have CECEvent to actions configurable, further clean-ups

This commit is contained in:
LordGrey
2023-11-13 22:28:41 +01:00
parent c3daeef077
commit 93db1f5d6b
17 changed files with 390 additions and 151 deletions

View File

@@ -113,7 +113,7 @@ JsonAPI::JsonAPI(QString peerAddress, Logger *log, bool localConnection, QObject
Q_INIT_RESOURCE(JSONRPC_schemas);
qRegisterMetaType<Event>("Event");
qRegisterMetaType<Event>("Event");
}
void JsonAPI::initialize()

View File

@@ -13,10 +13,17 @@
#include <QFile>
/* Enable to turn on detailed CEC logs */
#define VERBOSE_CEC
#define NOVERBOSE_CEC
CECHandler::CECHandler()
: _isEnabled(false)
CECHandler::CECHandler(const QJsonDocument& config, QObject * parent)
: QObject(parent)
, _config(config)
, _isInitialised(false)
, _isEnabled(false)
, _buttonReleaseDelayMs(CEC_BUTTON_TIMEOUT)
, _buttonRepeatRateMs(0)
, _doubleTapTimeoutMs(CEC_DOUBLE_TAP_TIMEOUT_MS)
, _cecEventActionMap()
{
qRegisterMetaType<Event>("Event");
@@ -30,79 +37,149 @@ CECHandler::CECHandler()
CECHandler::~CECHandler()
{
stop();
}
void CECHandler::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
{
if(type == settings::CECEVENTS)
{
Debug( _logger, "config: [%s]", QString(QJsonDocument(config).toJson(QJsonDocument::Compact)).toUtf8().constData());
const QJsonObject& obj = config.object();
_isEnabled = obj["Enable"].toBool(false);
_isEnabled = obj["enable"].toBool(false);
Debug(_logger, "CEC Event handling is %s", _isEnabled? "enabled" : "disabled");
Debug(_logger, "_isEnabled: [%d]", _isEnabled);
_buttonReleaseDelayMs = obj["buttonReleaseDelayMs"].toInt(CEC_BUTTON_TIMEOUT);
_buttonRepeatRateMs = obj["buttonRepeatRateMs"].toInt(0);
_doubleTapTimeoutMs = obj["doubleTapTimeoutMs"].toInt(CEC_DOUBLE_TAP_TIMEOUT_MS);
_cecConfig.iButtonReleaseDelayMs = static_cast<uint32_t>(_buttonReleaseDelayMs);
_cecConfig.iButtonRepeatRateMs = static_cast<uint32_t>(_buttonRepeatRateMs);
_cecConfig.iDoubleTapTimeoutMs = static_cast<uint32_t>(_doubleTapTimeoutMs);
if (_cecAdapter->SetConfiguration(&_cecConfig))
{
Debug(_logger, "Remote button press release time : %dms",_buttonReleaseDelayMs);
Debug(_logger, "Remote button press repeat rate : %dms",_buttonRepeatRateMs);
Debug(_logger, "Remote button press delay before repeating : %dms",_doubleTapTimeoutMs);
}
else
{
Error(_logger, "Failed setting remote button press timing parameters");
}
_cecEventActionMap.clear();
const QJsonArray actionItems = obj["actions"].toArray();
if (!actionItems.isEmpty())
{
for (const QJsonValue &item : actionItems)
{
QString cecEvent = item.toObject().value("cec_event").toString();
QString action = item.toObject().value("action").toString();
_cecEventActionMap.insert(cecEvent, stringToEvent(action));
Debug(_logger, "CEC-Event : \"%s\" linked to action \"%s\"", QSTRING_CSTR(cecEvent), QSTRING_CSTR(action));
}
}
if (_isInitialised)
{
if (_isEnabled)
{
if (!_cecEventActionMap.isEmpty())
{
enable();
}
else
{
Warning(_logger, "No CEC events to listen to are configured currently.");
}
}
else
{
disable();
}
}
}
}
bool CECHandler::start()
{
if (_cecAdapter)
_isInitialised = false;
if (_cecAdapter == nullptr)
{
return true;
}
// std::string library = std::string("" CEC_LIBRARY);
// _cecAdapter = LibCecInitialise(&_cecConfig, QFile::exists(QString::fromStdString(library)) ? library.c_str() : nullptr);
// std::string library = std::string("" CEC_LIBRARY);
// _cecAdapter = LibCecInitialise(&_cecConfig, QFile::exists(QString::fromStdString(library)) ? library.c_str() :CEC_DEVICE_TYPE_PLAYBACK_DEVICE nullptr);
_cecAdapter = LibCecInitialise(&_cecConfig);
if(_cecAdapter == nullptr)
{
Error(_logger, "Failed loading libCEC library. CEC is not supported.");
return false;
}
Info(_logger, "CEC handler started");
const auto adapters = getAdapters();
if (adapters.isEmpty())
{
Error(_logger, "Failed to find any CEC adapter.");
UnloadLibCec(_cecAdapter);
_cecAdapter = nullptr;
return false;
}
Info(_logger, "Auto detecting CEC adapter");
bool opened = false;
for (const auto & adapter : adapters)
{
printAdapter(adapter);
if (!opened && openAdapter(adapter))
_cecAdapter = LibCecInitialise(&_cecConfig);
if(_cecAdapter == nullptr)
{
QObject::connect(this, &CECHandler::signalEvent, EventHandler::getInstance(), &EventHandler::handleEvent);
Info(_logger, "CEC adapter '%s', type: %s initialized." , adapter.strComName, _cecAdapter->ToString(adapter.adapterType));
opened = true;
break;
Error(_logger, "Failed loading libCEC library. CEC is not supported.");
}
else
{
_isInitialised = true;
}
}
scan();
handleSettingsUpdate(settings::CECEVENTS,_config);
}
return _isInitialised;
}
void CECHandler::stop()
{
if (_cecAdapter != nullptr)
{
Info(_logger, "Stopping CEC handler");
_cecAdapter->Close();
UnloadLibCec(_cecAdapter);
}
}
bool CECHandler::enable()
{
bool opened {false};
if (_isInitialised)
{
const auto adapters = getAdapters();
if (adapters.isEmpty())
{
Error(_logger, "Failed to find any CEC adapter.");
_cecAdapter->Close();
return false;
}
Info(_logger, "Auto detecting CEC adapter");
for (const auto & adapter : adapters)
{
printAdapter(adapter);
if (!opened && openAdapter(adapter))
{
Info(_logger, "CEC adapter '%s', type: %s initialized." , adapter.strComName, _cecAdapter->ToString(adapter.adapterType));
opened = true;
break;
}
}
#ifdef VERBOSE_CEC
std::cout << "Found Devices: " << scan().toStdString() << std::endl;
#endif
}
if (!opened)
{
Error(_logger, "Could not initialize any CEC adapter.");
UnloadLibCec(_cecAdapter);
_cecAdapter = nullptr;
_cecAdapter->Close();
}
else
{
QObject::connect(this, &CECHandler::signalEvent, EventHandler::getInstance(), &EventHandler::handleEvent);
Info(_logger, "CEC handler started");
}
return opened;
}
void CECHandler::stop()
void CECHandler::disable()
{
if (_cecAdapter != nullptr)
{
@@ -111,8 +188,6 @@ void CECHandler::stop()
QObject::disconnect(this, &CECHandler::signalEvent, EventHandler::getInstance(), &EventHandler::handleEvent);
_cecAdapter->Close();
UnloadLibCec(_cecAdapter);
_cecAdapter = nullptr;
}
}
@@ -146,12 +221,11 @@ CECCallbacks CECHandler::getCallbacks() const
QVector<CECAdapterDescriptor> CECHandler::getAdapters() const
{
if (!_cecAdapter)
if (_cecAdapter == nullptr)
return {};
QVector<CECAdapterDescriptor> descriptors(16);
//int8_t size = _cecAdapter->DetectAdapters(descriptors.data(), static_cast<uint8_t>(descriptors.size()), nullptr, true /*quickscan*/);
int8_t size = _cecAdapter->DetectAdapters(descriptors.data(), static_cast<uint8_t>(descriptors.size()), nullptr, false /*NO quickscan*/);
int8_t size = _cecAdapter->DetectAdapters(descriptors.data(), static_cast<uint8_t>(descriptors.size()), nullptr, true /*quickscan*/);
descriptors.resize(size);
return descriptors;
@@ -159,7 +233,7 @@ QVector<CECAdapterDescriptor> CECHandler::getAdapters() const
bool CECHandler::openAdapter(const CECAdapterDescriptor & descriptor)
{
if (!_cecAdapter)
if (_cecAdapter == nullptr)
return false;
if(!_cecAdapter->Open(descriptor.strComName))
@@ -186,7 +260,7 @@ void CECHandler::printAdapter(const CECAdapterDescriptor & descriptor) const
QString CECHandler::scan() const
{
if (!_cecAdapter)
if (_cecAdapter == nullptr)
return {};
Info(_logger, "Starting CEC scan");
@@ -219,17 +293,24 @@ QString CECHandler::scan() const
);
}
}
std::cout << "Devices: " << QJsonDocument(devices).toJson().toStdString() << std::endl;
return QJsonDocument(devices).toJson(QJsonDocument::Compact);
}
void CECHandler::triggerAction(const QString& cecEvent)
{
Event action = _cecEventActionMap.value(cecEvent, Event::Unknown);
Debug(_logger, "CEC-Event : \"%s\" triggers action \"%s\"", QSTRING_CSTR(cecEvent), eventToString(action) );
if ( action != Event::Unknown )
{
emit signalEvent(action);
}
}
void CECHandler::onCecLogMessage(void * context, const CECLogMessage * message)
{
#ifdef VERBOSE_CEC
CECHandler * handler = qobject_cast<CECHandler*>(static_cast<QObject*>(context));
if (!handler)
if (handler == nullptr)
return;
switch (message->level)
@@ -256,7 +337,7 @@ void CECHandler::onCecLogMessage(void * context, const CECLogMessage * message)
void CECHandler::onCecKeyPress(void * context, const CECKeyPress * key)
{
CECHandler * handler = qobject_cast<CECHandler*>(static_cast<QObject*>(context));
if (!handler)
if (handler == nullptr)
return;
CECAdapter * adapter = handler->_cecAdapter;
@@ -264,20 +345,12 @@ void CECHandler::onCecKeyPress(void * context, const CECKeyPress * key)
#ifdef VERBOSE_CEC
Debug(handler->_logger, "CECHandler::onCecKeyPress: %s", adapter->ToString(key->keycode));
#endif
switch (key->keycode) {
case CEC::CEC_USER_CONTROL_CODE_F1_BLUE:
emit handler->signalEvent(Event::ToggleIdle);
break;
case CEC::CEC_USER_CONTROL_CODE_F2_RED:
emit handler->signalEvent(Event::Suspend);
break;
case CEC::CEC_USER_CONTROL_CODE_F3_GREEN:
emit handler->signalEvent(Event::Resume);
break;
case CEC::CEC_USER_CONTROL_CODE_F4_YELLOW:
emit handler->signalEvent(Event::ToggleSuspend);
handler->triggerAction(adapter->ToString(key->keycode));
break;
default:
break;
@@ -288,7 +361,7 @@ void CECHandler::onCecAlert(void * context, const CECAlert alert, const CECParam
{
#ifdef VERBOSE_CEC
CECHandler * handler = qobject_cast<CECHandler*>(static_cast<QObject*>(context));
if (!handler)
if (handler == nullptr)
return;
Error(handler->_logger, QSTRING_CSTR(QString("CECHandler::onCecAlert: %1")
@@ -300,7 +373,7 @@ void CECHandler::onCecConfigurationChanged(void * context, const CECConfig * con
{
#ifdef VERBOSE_CEC
CECHandler * handler = qobject_cast<CECHandler*>(static_cast<QObject*>(context));
if (!handler)
if (handler == nullptr)
return;
Debug(handler->_logger, "CECHandler::onCecConfigurationChanged: %s", configuration->strDeviceName);
@@ -341,13 +414,10 @@ void CECHandler::onCecCommandReceived(void * context, const CECCommand * command
{
switch (command->opcode) {
case CEC::CEC_OPCODE_STANDBY:
Info(handler->_logger, "CEC source deactivated: %s", adapter->ToString(command->initiator));
emit handler->signalEvent(Event::Suspend);
break;
case CEC::CEC_OPCODE_SET_STREAM_PATH:
Info(handler->_logger, "'CEC source activated: %s", adapter->ToString(command->initiator));
emit handler->signalEvent(Event::Resume);
{
handler->triggerAction(adapter->ToString(command->opcode));
}
break;
default:
@@ -363,10 +433,8 @@ void CECHandler::onCecSourceActivated(void * context, const CECLogicalAddress ad
#ifdef VERBOSE_CEC
CECHandler * handler = qobject_cast<CECHandler*>(static_cast<QObject*>(context));
if (!handler)
{
if (handler == nullptr)
return;
}
CECAdapter * adapter = handler->_cecAdapter;
Debug(handler->_logger, QSTRING_CSTR(QString("CEC source %1 : %2")

View File

@@ -8,8 +8,6 @@ FILE (GLOB CEC_SOURCES "${CURRENT_HEADER_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.h"
add_library(cechandler ${CEC_SOURCES})
message(STATUS "CEC_LIBRARIES = ${CEC_LIBRARIES}")
list(GET CEC_LIBRARIES 0 CEC_LIBRARIES)
add_definitions(-DCEC_LIBRARY="${CEC_LIBRARIES}")

View File

@@ -25,6 +25,7 @@ target_include_directories(events PUBLIC
)
target_link_libraries(events
hyperion-utils
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Widgets
)

View File

@@ -145,7 +145,17 @@ void EventHandler::toggleIdle()
void EventHandler::handleEvent(Event event)
{
Debug(_log,"%s Event [%d] received", eventToString(event), event);
QObject *senderObj = QObject::sender();
QString senderObjectClass;
if (senderObj)
{
senderObjectClass = senderObj->metaObject()->className();
} else
{
senderObjectClass = "unknown sender";
}
Debug(_log,"%s Event [%d] received from %s", eventToString(event), event, QSTRING_CSTR(senderObjectClass));
switch (event) {
case Event::Suspend:
suspend();
@@ -156,7 +166,7 @@ void EventHandler::handleEvent(Event event)
break;
case Event::ToggleSuspend:
suspend();
toggleSuspend();
break;
case Event::Idle:

View File

@@ -26,7 +26,12 @@ elseif(ENABLE_V4L2)
endif()
add_library(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} hyperion ${QT_LIBRARIES})
target_link_libraries(${PROJECT_NAME}
hyperion
events
hyperion-utils
${QT_LIBRARIES}
)
if(TURBOJPEG_FOUND)
target_link_libraries(${PROJECT_NAME} ${TurboJPEG_LIBRARY})

View File

@@ -20,6 +20,7 @@ add_library(hyperion
target_link_libraries(hyperion
blackborder
events
hyperion-utils
leddevice
database

View File

@@ -1,17 +1,119 @@
{
"type" : "object",
"required" : true,
"title" : "edt_conf_cec_events_heading_title",
"properties" :
{
"enable" :
{
"type" : "boolean",
"required" : true,
"title" : "edt_conf_general_enable_title",
"default" : false,
"propertyOrder" : 1
}
},
"additionalProperties" : false
"type": "object",
"required": true,
"title": "edt_conf_cec_events_heading_title",
"properties": {
"enable": {
"type": "boolean",
"required": true,
"title": "edt_conf_general_enable_title",
"default": false,
"propertyOrder": 1
},
"buttonReleaseDelayMs": {
"type": "integer",
"format": "stepper",
"title": "edt_conf_cec_button_release_delay_ms_title",
"append": "edt_append_ms",
"minimum": 0,
"maximum": 500,
"step": 50,
"default": 0,
"required": false,
"access": "advanced",
"propertyOrder": 2
},
"buttonRepeatRateMs": {
"type": "integer",
"format": "stepper",
"title": "edt_conf_cec_button_repeat_rate_ms_title",
"append": "edt_append_ms",
"minimum": 0,
"maximum": 250,
"step": 10,
"default": 0,
"required": false,
"access": "advanced",
"propertyOrder": 3
},
"doubleTapTimeoutMs": {
"type": "integer",
"format": "stepper",
"title": "edt_conf_cec_double_tap_timeout_ms_title",
"append": "edt_append_ms",
"minimum": 50,
"maximum": 1000,
"step": 50,
"default": 200,
"required": false,
"access": "advanced",
"propertyOrder": 4
},
"actions": {
"type": "array",
"title": "edt_conf_cec_actions_header_title",
"minItems": 0,
"uniqueItems": true,
"required": false,
"propertyOrder": 5,
"items": {
"type": "object",
"required": true,
"title": "edt_conf_cec_actions_header_item_title",
"properties": {
"cec_event": {
"type": "string",
"title": "edt_conf_cec_event_title",
"enum": [
"standby",
"set stream path",
"F1(blue)",
"F2 (red)",
"F3 (green)",
"F4 (yellow)"
],
"options": {
"enum_titles": [
"edt_conf_enum_cec_opcode_standby",
"edt_conf_enum_cec_opcode_set stream path",
"edt_conf_enum_cec_key_f1_blue",
"edt_conf_enum_cec_key_f2_red",
"edt_conf_enum_cec_key_f3_green",
"edt_conf_enum_cec_key_f4_yellow"
]
},
"propertyOrder": 1
},
"action": {
"type": "string",
"title": "edt_conf_action_title",
"enum": [
"Suspend",
"Resume",
"ToggleSuspend",
"Idle",
"ResumeIdle",
"ToggleIdle",
"Restart"
],
"options": {
"enum_titles": [
"edt_conf_enum_action_suspend",
"edt_conf_enum_action_resume",
"edt_conf_enum_action_toggleSuspend",
"edt_conf_enum_action_idle",
"edt_conf_enum_action_resumeIdle",
"edt_conf_enum_action_toggleIdle",
"edt_conf_enum_action_restart"
]
},
"propertyOrder": 2
}
},
"additionalProperties": false
}
}
},
"additionalProperties": false
}

View File

@@ -212,21 +212,13 @@
"required": true,
"propertyOrder": 23
},
"cecDetection": {
"type": "boolean",
"title": "edt_conf_v4l2_cecDetection_title",
"default": false,
"required": true,
"access": "advanced",
"propertyOrder": 24
},
"signalDetection": {
"type": "boolean",
"title": "edt_conf_v4l2_signalDetection_title",
"default": false,
"required": true,
"access": "expert",
"propertyOrder": 25
"propertyOrder": 24
},
"redSignalThreshold": {
"type": "integer",
@@ -242,7 +234,7 @@
},
"access": "expert",
"required": true,
"propertyOrder": 26
"propertyOrder": 25
},
"greenSignalThreshold": {
"type": "integer",
@@ -258,7 +250,7 @@
},
"required": true,
"access": "expert",
"propertyOrder": 27
"propertyOrder": 26
},
"blueSignalThreshold": {
"type": "integer",
@@ -274,7 +266,7 @@
},
"required": true,
"access": "expert",
"propertyOrder": 28
"propertyOrder": 27
},
"noSignalCounterThreshold": {
"type": "integer",
@@ -289,7 +281,7 @@
},
"required": true,
"access": "expert",
"propertyOrder": 29
"propertyOrder": 28
},
"sDVOffsetMin": {
"type": "number",
@@ -305,7 +297,7 @@
},
"required": true,
"access": "expert",
"propertyOrder": 30
"propertyOrder": 29
},
"sDVOffsetMax": {
"type": "number",
@@ -321,7 +313,7 @@
},
"required": true,
"access": "expert",
"propertyOrder": 31
"propertyOrder": 30
},
"sDHOffsetMin": {
"type": "number",
@@ -337,7 +329,7 @@
},
"required": true,
"access": "expert",
"propertyOrder": 32
"propertyOrder": 31
},
"sDHOffsetMax": {
"type": "number",
@@ -353,7 +345,7 @@
},
"required": true,
"access": "expert",
"propertyOrder": 33
"propertyOrder": 32
}
},
"additionalProperties": true