-
Notifications
You must be signed in to change notification settings - Fork 1
/
libcec-commandHandler.patch
399 lines (367 loc) · 13.9 KB
/
libcec-commandHandler.patch
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
diff --git a/include/cectypes.h b/include/cectypes.h
index e585f2f..94499e4 100644
--- a/include/cectypes.h
+++ b/include/cectypes.h
@@ -1450,6 +1450,15 @@ typedef struct ICECCallbacks
*/
void (CEC_CDECL* sourceActivated)(void* cbParam, const cec_logical_address logicalAddress, const uint8_t bActivated);
+ /*!
+ * @brief Allow the client handle a CEC command instead of libcec.
+ * @param cbparam Callback parameter provided when the callbacks were set up
+ * @param command The command to handle.
+ *
+ * @return 1 if the command has been handled and if libCEC should not take any action
+ */
+ int (CEC_CDECL* commandHandler)(void* cbparam, const cec_command* command);
+
#ifdef __cplusplus
ICECCallbacks(void) { Clear(); }
~ICECCallbacks(void) { Clear(); };
@@ -1463,6 +1472,7 @@ typedef struct ICECCallbacks
alert = nullptr;
menuStateChanged = nullptr;
sourceActivated = nullptr;
+ commandHandler = nullptr;
}
#endif
} ICECCallbacks;
diff --git a/src/cec-client/cec-client.cpp b/src/cec-client/cec-client.cpp
index b209b1e..7544bc7 100644
--- a/src/cec-client/cec-client.cpp
+++ b/src/cec-client/cec-client.cpp
@@ -227,6 +227,10 @@ void CecCommand(void *UNUSED(cbParam), const cec_command* UNUSED(command))
{
}
+int CecCommandHandler(void *UNUSED(cbParam), const cec_command* UNUSED(command))
+{
+ return 0;
+}
void CecAlert(void *UNUSED(cbParam), const libcec_alert type, const libcec_parameter UNUSED(param))
{
switch (type)
@@ -1275,6 +1279,7 @@ int main (int argc, char *argv[])
g_callbacks.keyPress = &CecKeyPress;
g_callbacks.commandReceived = &CecCommand;
g_callbacks.alert = &CecAlert;
+ g_callbacks.commandHandler = &CecCommandHandler;
g_config.callbacks = &g_callbacks;
if (!ProcessCommandLineArguments(argc, argv))
diff --git a/src/cecc-client/cecc-client.c b/src/cecc-client/cecc-client.c
index d65b77f..1db9c28 100644
--- a/src/cecc-client/cecc-client.c
+++ b/src/cecc-client/cecc-client.c
@@ -65,7 +65,8 @@ static ICECCallbacks g_callbacks = {
.configurationChanged = NULL,
.alert = NULL,
.menuStateChanged = NULL,
- .sourceActivated = NULL
+ .sourceActivated = NULL,
+ .commandHandler = NULL
};
static libcec_configuration g_config;
diff --git a/src/dotnetlib/CecSharpTypesUnmanaged.h b/src/dotnetlib/CecSharpTypesUnmanaged.h
index 53ff9bd..166547d 100644
--- a/src/dotnetlib/CecSharpTypesUnmanaged.h
+++ b/src/dotnetlib/CecSharpTypesUnmanaged.h
@@ -187,8 +187,22 @@ namespace CecSharp
}
/// <summary>
- /// Assign the callback methods in the g_cecCallbacks struct and return a pointer to it
+ /// Called by libCEC to have the client handle the command and prevent further process by libCEC
/// </summary>
+ /// <param name="cbParam">Pointer to the callback struct</param>
+ /// <param name="command">The raw CEC data</param>
+ /// <return>1 when handled by the client, 0 otherwise</return>
+ static int CecCommandHandlerCB(void* cbParam, const CEC::cec_command* command)
+ {
+ struct UnmanagedCecCallbacks* cb = static_cast<struct UnmanagedCecCallbacks*>(cbParam);
+ if (!!cb && !!cb->commandHandlerCB)
+ return cb->commandHandlerCB(command);
+ return 0;
+ }
+
+ /// <summary>
+ /// Assign the callback methods in the g_cecCallbacks struct and return a pointer to it
+ /// </summary>
static CEC::ICECCallbacks* GetLibCecCallbacks()
{
g_cecCallbacks.logMessage = CecLogMessageCB;
@@ -198,6 +212,7 @@ namespace CecSharp
g_cecCallbacks.alert = CecAlertCB;
g_cecCallbacks.menuStateChanged = CecMenuCB;
g_cecCallbacks.sourceActivated = CecSourceActivatedCB;
+ g_cecCallbacks.commandHandler = CecCommandHandlerCB;
return &g_cecCallbacks;
}
#pragma managed
diff --git a/src/libcec/CECClient.cpp b/src/libcec/CECClient.cpp
index b4167a1..445f671 100644
--- a/src/libcec/CECClient.cpp
+++ b/src/libcec/CECClient.cpp
@@ -1604,7 +1604,7 @@ void CCECClient::QueueConfigurationChanged(const libcec_configuration& config)
int CCECClient::QueueMenuStateChanged(const cec_menu_state newState)
{
- CCallbackWrap *wrapState = new CCallbackWrap(newState, true);
+ CCallbackWrap *wrapState = new CCallbackWrap(newState);
m_callbackCalls.Push(wrapState);
int result(wrapState->Result(1000));
@@ -1617,6 +1617,17 @@ void CCECClient::QueueSourceActivated(bool bActivated, const cec_logical_address
m_callbackCalls.Push(new CCallbackWrap(bActivated, logicalAddress));
}
+int CCECClient::QueueCommandHandler(const cec_command& command)
+{
+ CCallbackWrap *wrapState = new CCallbackWrap(command, true);
+ m_callbackCalls.Push(wrapState);
+ int result(wrapState->Result(10000));
+
+ if (wrapState->m_keepResult)
+ delete wrapState;
+ return result;
+}
+
void* CCECClient::Process(void)
{
CCallbackWrap* cb(NULL);
@@ -1624,6 +1635,7 @@ void* CCECClient::Process(void)
{
if (m_callbackCalls.Pop(cb, 500))
{
+ bool keepResult = cb->m_keepResult;
try
{
switch (cb->m_type)
@@ -1644,16 +1656,21 @@ void* CCECClient::Process(void)
CallbackConfigurationChanged(cb->m_config);
break;
case CCallbackWrap::CEC_CB_MENU_STATE:
- cb->Report(CallbackMenuStateChanged(cb->m_menuState));
+ keepResult = cb->Report(CallbackMenuStateChanged(cb->m_menuState));
break;
case CCallbackWrap::CEC_CB_SOURCE_ACTIVATED:
CallbackSourceActivated(cb->m_bActivated, cb->m_logicalAddress);
break;
+ case CCallbackWrap::CEC_CB_COMMAND_HANDLER:
+ keepResult = cb->Report(CallbackCommandHandler(cb->m_command));
+ if (!keepResult)
+ LIB_CEC->AddLog(CEC_LOG_WARNING, "Command callback timeout occured !");
+ break;
default:
break;
}
- if (!cb->m_keepResult)
+ if (!keepResult)
delete cb;
} catch (...)
{
@@ -1733,6 +1750,17 @@ int CCECClient::CallbackMenuStateChanged(const cec_menu_state newState)
return 0;
}
+int CCECClient::CallbackCommandHandler(const cec_command &command)
+{
+ CLockObject lock(m_cbMutex);
+ if (!!m_configuration.callbacks &&
+ !!m_configuration.callbacks->commandHandler)
+ {
+ return m_configuration.callbacks->commandHandler(m_configuration.callbackParam, &command);
+ }
+ return 0;
+}
+
bool CCECClient::AudioEnable(bool enable)
{
CCECBusDevice* device = enable ? GetPrimaryDevice() : nullptr;
diff --git a/src/libcec/CECClient.h b/src/libcec/CECClient.h
index 1c23ee1..d869df4 100644
--- a/src/libcec/CECClient.h
+++ b/src/libcec/CECClient.h
@@ -107,13 +107,13 @@ namespace CEC
m_result(0),
m_bSucceeded(false) {}
- CCallbackWrap(const cec_menu_state newState, const bool keepResult = false) :
+ CCallbackWrap(const cec_menu_state newState) :
m_type(CEC_CB_MENU_STATE),
m_alertType(CEC_ALERT_SERVICE_DEVICE),
m_menuState(newState),
m_bActivated(false),
m_logicalAddress(CECDEVICE_UNKNOWN),
- m_keepResult(keepResult),
+ m_keepResult(true),
m_result(0),
m_bSucceeded(false) {}
@@ -127,6 +127,17 @@ namespace CEC
m_result(0),
m_bSucceeded(false) {}
+ CCallbackWrap(const cec_command& command, const bool unused) :
+ m_type(CEC_CB_COMMAND_HANDLER),
+ m_command(command),
+ m_alertType(CEC_ALERT_SERVICE_DEVICE),
+ m_menuState(CEC_MENU_STATE_ACTIVATED),
+ m_bActivated(false),
+ m_logicalAddress(CECDEVICE_UNKNOWN),
+ m_keepResult(true),
+ m_result(0),
+ m_bSucceeded(false) {}
+
int Result(uint32_t iTimeout)
{
P8PLATFORM::CLockObject lock(m_mutex);
@@ -134,16 +145,18 @@ namespace CEC
bool bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout);
if (bReturn)
return m_result;
+ m_keepResult = false;
return 0;
}
- void Report(int result)
+ bool Report(int result)
{
P8PLATFORM::CLockObject lock(m_mutex);
m_result = result;
m_bSucceeded = true;
m_condition.Signal();
+ return m_keepResult;
}
enum callbackWrapType {
@@ -154,6 +167,7 @@ namespace CEC
CEC_CB_CONFIGURATION,
CEC_CB_MENU_STATE,
CEC_CB_SOURCE_ACTIVATED,
+ CEC_CB_COMMAND_HANDLER,
} m_type;
cec_command m_command;
@@ -314,6 +328,7 @@ namespace CEC
void QueueConfigurationChanged(const libcec_configuration& config);
int QueueMenuStateChanged(const cec_menu_state newState); //TODO
void QueueSourceActivated(bool bActivated, const cec_logical_address logicalAddress);
+ int QueueCommandHandler(const cec_command& command);
// callbacks
virtual void Alert(const libcec_alert type, const libcec_parameter ¶m) { QueueAlert(type, param); }
@@ -436,6 +451,7 @@ namespace CEC
virtual void SetSupportedDeviceTypes(void);
void AddCommand(const cec_command &command);
+ void AddCommandHandler(const cec_command &command);
void CallbackAddCommand(const cec_command& command);
void CallbackAddKey(const cec_keypress& key);
void CallbackAddLog(const cec_log_message_cpp& message);
@@ -443,6 +459,7 @@ namespace CEC
void CallbackConfigurationChanged(const libcec_configuration& config);
int CallbackMenuStateChanged(const cec_menu_state newState);
void CallbackSourceActivated(bool bActivated, const cec_logical_address logicalAddress);
+ int CallbackCommandHandler(const cec_command &command);
uint32_t DoubleTapTimeoutMS(void);
diff --git a/src/libcec/LibCEC.cpp b/src/libcec/LibCEC.cpp
index a814f38..1e1a66e 100644
--- a/src/libcec/LibCEC.cpp
+++ b/src/libcec/LibCEC.cpp
@@ -408,7 +408,7 @@ void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
va_end(argList);
// send the message to all clients
- CLockObject lock(m_mutex);
+ //CLockObject lock(m_mutex);
for (std::vector<CECClientPtr>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
(*it)->AddLog(message);
}
@@ -416,15 +416,25 @@ void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
void CLibCEC::AddCommand(const cec_command &command)
{
// send the command to all clients
- CLockObject lock(m_mutex);
+ //CLockObject lock(m_mutex);
for (std::vector<CECClientPtr>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
(*it)->QueueAddCommand(command);
}
+bool CLibCEC::CommandHandlerCB(const cec_command &command)
+{
+ // send the command to all clients
+ //CLockObject lock(m_mutex);
+ for (std::vector<CECClientPtr>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
+ if ((*it)->QueueCommandHandler(command))
+ return true;
+ return false;
+}
+
void CLibCEC::Alert(const libcec_alert type, const libcec_parameter ¶m)
{
// send the alert to all clients
- CLockObject lock(m_mutex);
+ //CLockObject lock(m_mutex);
for (std::vector<CECClientPtr>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
(*it)->Alert(type, param);
}
diff --git a/src/libcec/LibCEC.h b/src/libcec/LibCEC.h
index feaad33..1344c65 100644
--- a/src/libcec/LibCEC.h
+++ b/src/libcec/LibCEC.h
@@ -144,6 +144,7 @@ namespace CEC
void AddLog(const cec_log_level level, const char *strFormat, ...);
void AddCommand(const cec_command &command);
+ bool CommandHandlerCB(const cec_command &command);
uint16_t CheckKeypressTimeout(void);
void Alert(const libcec_alert type, const libcec_parameter ¶m);
@@ -169,7 +170,7 @@ namespace CEC
protected:
int64_t m_iStartTime;
- P8PLATFORM::CMutex m_mutex;
+ //P8PLATFORM::CMutex m_mutex;
CECClientPtr m_client;
std::vector<CECClientPtr> m_clients;
};
diff --git a/src/libcec/SwigHelper.h b/src/libcec/SwigHelper.h
index 9855976..8ed6915 100644
--- a/src/libcec/SwigHelper.h
+++ b/src/libcec/SwigHelper.h
@@ -58,6 +58,7 @@ namespace CEC
PYTHON_CB_MENU_STATE,
PYTHON_CB_SOURCE_ACTIVATED,
PYTHON_CB_CONFIGURATION,
+ PYTHON_CB_COMMAND_HANDLER,
NB_PYTHON_CB,
};
@@ -88,6 +89,7 @@ namespace CEC
m_configuration->callbacks->alert = CBCecAlert;
m_configuration->callbacks->menuStateChanged = CBCecMenuStateChanged;
m_configuration->callbacks->sourceActivated = CBCecSourceActivated;
+ m_configuration->callbacks->commandHandler = CBCecCommandHandler;
}
/**
@@ -220,6 +222,14 @@ namespace CEC
PyGILState_Release(gstate);
}
+ static int CBCecCommandHandler(void* param, const CEC::cec_command* command)
+ {
+ PyGILState_STATE gstate = PyGILState_Ensure();
+ int retval = CallPythonCallback(param, PYTHON_CB_COMMAND_HANDLER,
+ Py_BuildValue("(s)", CEC::CCECTypeUtils::ToString(*command).c_str()));
+ PyGILState_Release(gstate);
+ return retval;
+ }
PyObject* m_callbacks[NB_PYTHON_CB];
libcec_configuration* m_configuration;
};
diff --git a/src/libcec/implementations/CECCommandHandler.cpp b/src/libcec/implementations/CECCommandHandler.cpp
index 09087f1..5602e6e 100644
--- a/src/libcec/implementations/CECCommandHandler.cpp
+++ b/src/libcec/implementations/CECCommandHandler.cpp
@@ -77,6 +77,9 @@ bool CCECCommandHandler::HandleCommand(const cec_command &command)
LIB_CEC->AddCommand(command);
+ if (LIB_CEC->CommandHandlerCB(command))
+ return true;
+
switch(command.opcode)
{
case CEC_OPCODE_REPORT_POWER_STATUS:
diff --git a/src/libcec/libcec.i b/src/libcec/libcec.i
index 88250be..d62895e 100644
--- a/src/libcec/libcec.i
+++ b/src/libcec/libcec.i
@@ -103,6 +103,11 @@
_SetCallback(self, CEC::PYTHON_CB_CONFIGURATION, pyfunc);
}
+ void SetCommandHandlerCallback(PyObject* pyfunc)
+ {
+ _SetCallback(self, CEC::PYTHON_CB_COMMAND_HANDLER, pyfunc);
+ }
+
void ClearCallbacks(void)
{
_ClearCallbacks(self);