Commit | Line | Data |
---|---|---|
2abe74eb LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
b492c10e | 4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
2abe74eb LOK |
5 | * libCEC(R) is an original work, containing original code. |
6 | * | |
7 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
8 | * | |
9 | * This program is dual-licensed; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | * | |
23 | * | |
24 | * Alternatively, you can license this library under a commercial license, | |
25 | * please contact Pulse-Eight Licensing for more information. | |
26 | * | |
27 | * For more information contact: | |
28 | * Pulse-Eight Licensing <license@pulse-eight.com> | |
29 | * http://www.pulse-eight.com/ | |
30 | * http://www.pulse-eight.net/ | |
31 | */ | |
32 | ||
33 | #include "LibCEC.h" | |
34 | ||
7bb4ed43 | 35 | #include "adapter/USBCECAdapterDetection.h" |
08d80226 | 36 | #include "adapter/USBCECAdapterCommunication.h" |
2abe74eb | 37 | #include "CECProcessor.h" |
0f23c85c | 38 | #include "devices/CECBusDevice.h" |
ba65909d LOK |
39 | #include "platform/util/timeutils.h" |
40 | #include "platform/util/StdString.h" | |
2abe74eb LOK |
41 | |
42 | using namespace std; | |
43 | using namespace CEC; | |
f00ff009 | 44 | using namespace PLATFORM; |
2abe74eb | 45 | |
4f362964 | 46 | CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types, uint16_t iPhysicalAddress /* = 0 */) : |
f8513317 LOK |
47 | m_iStartTime(GetTimeMs()), |
48 | m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), | |
fa4798bd | 49 | m_buttontime(0), |
547b390d LOK |
50 | m_callbacks(NULL), |
51 | m_cbParam(NULL) | |
f8513317 | 52 | { |
7bdfd76c | 53 | m_cec = new CCECProcessor(this, strDeviceName, types, iPhysicalAddress); |
caca2d81 LOK |
54 | } |
55 | ||
3efda01a | 56 | CLibCEC::CLibCEC(libcec_configuration *configuration) : |
caca2d81 LOK |
57 | m_iStartTime(GetTimeMs()), |
58 | m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), | |
59 | m_buttontime(0), | |
60 | m_callbacks(configuration->callbacks), | |
61 | m_cbParam(configuration->callbackParam) | |
62 | { | |
63 | m_cec = new CCECProcessor(this, configuration); | |
f8513317 LOK |
64 | } |
65 | ||
2abe74eb LOK |
66 | CLibCEC::~CLibCEC(void) |
67 | { | |
68 | delete m_cec; | |
2abe74eb LOK |
69 | } |
70 | ||
25701fa6 | 71 | bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */) |
2abe74eb | 72 | { |
1113cb7d | 73 | if (m_cec->IsRunning()) |
2abe74eb LOK |
74 | { |
75 | AddLog(CEC_LOG_ERROR, "connection already open"); | |
76 | return false; | |
77 | } | |
78 | ||
1113cb7d | 79 | if (!m_cec->Start(strPort, 38400, iTimeoutMs)) |
2abe74eb LOK |
80 | { |
81 | AddLog(CEC_LOG_ERROR, "could not start CEC communications"); | |
82 | return false; | |
83 | } | |
84 | ||
85 | return true; | |
86 | } | |
87 | ||
88 | void CLibCEC::Close(void) | |
89 | { | |
25701fa6 | 90 | if (m_cec) |
eca71746 | 91 | m_cec->Close(); |
2abe74eb LOK |
92 | } |
93 | ||
547b390d | 94 | bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks) |
fa4798bd | 95 | { |
f00ff009 | 96 | CLockObject lock(m_mutex); |
fa4798bd | 97 | if (m_cec) |
547b390d LOK |
98 | { |
99 | m_cbParam = cbParam; | |
fa4798bd | 100 | m_callbacks = callbacks; |
547b390d | 101 | } |
fa4798bd LOK |
102 | return false; |
103 | } | |
104 | ||
25701fa6 | 105 | int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */) |
2abe74eb LOK |
106 | { |
107 | CStdString strDebug; | |
108 | if (strDevicePath) | |
109 | strDebug.Format("trying to autodetect the com port for device path '%s'", strDevicePath); | |
110 | else | |
111 | strDebug.Format("trying to autodetect all CEC adapters"); | |
112 | AddLog(CEC_LOG_DEBUG, strDebug); | |
113 | ||
7bb4ed43 | 114 | return CUSBCECAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath); |
2abe74eb LOK |
115 | } |
116 | ||
117 | bool CLibCEC::PingAdapter(void) | |
118 | { | |
1113cb7d | 119 | return m_cec ? m_cec->PingAdapter() : false; |
2abe74eb LOK |
120 | } |
121 | ||
122 | bool CLibCEC::StartBootloader(void) | |
123 | { | |
1113cb7d | 124 | return m_cec ? m_cec->StartBootloader() : false; |
2abe74eb LOK |
125 | } |
126 | ||
2abe74eb LOK |
127 | bool CLibCEC::GetNextLogMessage(cec_log_message *message) |
128 | { | |
25701fa6 | 129 | return (m_logBuffer.Pop(*message)); |
2abe74eb LOK |
130 | } |
131 | ||
132 | bool CLibCEC::GetNextKeypress(cec_keypress *key) | |
133 | { | |
134 | return m_keyBuffer.Pop(*key); | |
135 | } | |
136 | ||
137 | bool CLibCEC::GetNextCommand(cec_command *command) | |
138 | { | |
139 | return m_commandBuffer.Pop(*command); | |
140 | } | |
141 | ||
8d84e2c0 | 142 | bool CLibCEC::Transmit(const cec_command &data) |
2abe74eb | 143 | { |
8d84e2c0 | 144 | return m_cec ? m_cec->Transmit(data) : false; |
2abe74eb LOK |
145 | } |
146 | ||
147 | bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress) | |
148 | { | |
149 | return m_cec ? m_cec->SetLogicalAddress(iLogicalAddress) : false; | |
150 | } | |
151 | ||
16b1e052 | 152 | bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) |
2492216a LOK |
153 | { |
154 | return m_cec ? m_cec->SetPhysicalAddress(iPhysicalAddress) : false; | |
155 | } | |
156 | ||
d2f1c157 | 157 | bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */) |
16b1e052 | 158 | { |
d2f1c157 | 159 | return m_cec ? m_cec->SetHDMIPort(iBaseDevice, iPort) : false; |
16b1e052 LOK |
160 | } |
161 | ||
2dbd78f8 LOK |
162 | bool CLibCEC::EnablePhysicalAddressDetection(void) |
163 | { | |
164 | return m_cec ? m_cec->EnablePhysicalAddressDetection() : false; | |
165 | } | |
166 | ||
2abe74eb LOK |
167 | bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */) |
168 | { | |
ca27e6cf | 169 | return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->PowerOnDevices(address) : false; |
2abe74eb LOK |
170 | } |
171 | ||
172 | bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */) | |
173 | { | |
ca27e6cf | 174 | return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->StandbyDevices(address) : false; |
2abe74eb LOK |
175 | } |
176 | ||
18203d17 LOK |
177 | bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */) |
178 | { | |
179 | return m_cec ? m_cec->SetActiveSource(type) : false; | |
180 | } | |
181 | ||
2abe74eb LOK |
182 | bool CLibCEC::SetActiveView(void) |
183 | { | |
184 | return m_cec ? m_cec->SetActiveView() : false; | |
185 | } | |
186 | ||
28fa6c97 | 187 | bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */) |
a9232a79 | 188 | { |
28fa6c97 | 189 | return m_cec ? m_cec->SetDeckControlMode(mode, bSendUpdate) : false; |
a9232a79 LOK |
190 | } |
191 | ||
28fa6c97 | 192 | bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */) |
a9232a79 | 193 | { |
28fa6c97 | 194 | return m_cec ? m_cec->SetDeckInfo(info, bSendUpdate) : false; |
a9232a79 LOK |
195 | } |
196 | ||
2abe74eb LOK |
197 | bool CLibCEC::SetInactiveView(void) |
198 | { | |
8fb8355c | 199 | return m_cec ? m_cec->TransmitInactiveSource() : false; |
2abe74eb LOK |
200 | } |
201 | ||
28fa6c97 LOK |
202 | bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */) |
203 | { | |
204 | return m_cec ? m_cec->SetMenuState(state, bSendUpdate) : false; | |
205 | } | |
206 | ||
1969b140 LOK |
207 | bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage) |
208 | { | |
38bdb943 LOK |
209 | return m_cec && iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST ? |
210 | m_cec->m_busDevices[m_cec->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress, duration, strMessage) : | |
211 | false; | |
1969b140 LOK |
212 | } |
213 | ||
8b7e5ff6 LOK |
214 | bool CLibCEC::SwitchMonitoring(bool bEnable) |
215 | { | |
216 | return m_cec ? m_cec->SwitchMonitoring(bEnable) : false; | |
217 | } | |
218 | ||
6a1c0009 LOK |
219 | cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress) |
220 | { | |
221 | if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST) | |
222 | return m_cec->GetDeviceCecVersion(iAddress); | |
223 | return CEC_VERSION_UNKNOWN; | |
224 | } | |
225 | ||
a3269a0a LOK |
226 | bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language) |
227 | { | |
228 | if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST) | |
229 | return m_cec->GetDeviceMenuLanguage(iAddress, language); | |
230 | return false; | |
231 | } | |
232 | ||
44c74256 LOK |
233 | uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress) |
234 | { | |
235 | if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST) | |
236 | return m_cec->GetDeviceVendorId(iAddress); | |
237 | return 0; | |
238 | } | |
239 | ||
eab72c40 LOK |
240 | uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress) |
241 | { | |
242 | if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST) | |
243 | return m_cec->GetDevicePhysicalAddress(iAddress); | |
244 | return 0; | |
245 | } | |
246 | ||
b4b1b49b LOK |
247 | cec_logical_address CLibCEC::GetActiveSource(void) |
248 | { | |
249 | return m_cec ? m_cec->GetActiveSource() : CECDEVICE_UNKNOWN; | |
250 | } | |
251 | ||
252 | bool CLibCEC::IsActiveSource(cec_logical_address iAddress) | |
253 | { | |
254 | if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST) | |
255 | return m_cec->IsActiveSource(iAddress); | |
256 | return false; | |
257 | } | |
258 | ||
e55f3f70 LOK |
259 | cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress) |
260 | { | |
261 | if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST) | |
262 | return m_cec->GetDevicePowerStatus(iAddress); | |
263 | return CEC_POWER_STATUS_UNKNOWN; | |
264 | } | |
44c74256 | 265 | |
57f45e6c LOK |
266 | bool CLibCEC::PollDevice(cec_logical_address iAddress) |
267 | { | |
268 | if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST) | |
269 | return m_cec->PollDevice(iAddress); | |
270 | return false; | |
271 | } | |
272 | ||
6d858ba4 LOK |
273 | cec_logical_addresses CLibCEC::GetActiveDevices(void) |
274 | { | |
275 | cec_logical_addresses addresses; | |
988de7b9 | 276 | addresses.Clear(); |
6d858ba4 LOK |
277 | if (m_cec) |
278 | addresses = m_cec->GetActiveDevices(); | |
279 | return addresses; | |
280 | } | |
281 | ||
282 | bool CLibCEC::IsActiveDevice(cec_logical_address iAddress) | |
283 | { | |
284 | if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST) | |
37b0c572 | 285 | return m_cec->IsPresentDevice(iAddress); |
6d858ba4 LOK |
286 | return false; |
287 | } | |
288 | ||
289 | bool CLibCEC::IsActiveDeviceType(cec_device_type type) | |
290 | { | |
291 | if (m_cec && type >= CEC_DEVICE_TYPE_TV && type <= CEC_DEVICE_TYPE_AUDIO_SYSTEM) | |
37b0c572 | 292 | return m_cec->IsPresentDeviceType(type); |
6d858ba4 LOK |
293 | return false; |
294 | } | |
295 | ||
5c73f7f7 | 296 | uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */) |
04e637f9 LOK |
297 | { |
298 | if (m_cec) | |
5c73f7f7 | 299 | return m_cec->VolumeUp(bSendRelease); |
04e637f9 LOK |
300 | return 0; |
301 | } | |
302 | ||
5c73f7f7 | 303 | uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */) |
04e637f9 LOK |
304 | { |
305 | if (m_cec) | |
5c73f7f7 | 306 | return m_cec->VolumeDown(bSendRelease); |
04e637f9 LOK |
307 | return 0; |
308 | } | |
309 | ||
310 | ||
5c73f7f7 | 311 | uint8_t CLibCEC::MuteAudio(bool bSendRelease /* = true */) |
04e637f9 LOK |
312 | { |
313 | if (m_cec) | |
5c73f7f7 | 314 | return m_cec->MuteAudio(bSendRelease); |
04e637f9 LOK |
315 | return 0; |
316 | } | |
317 | ||
4bec9d79 | 318 | bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */) |
a33794d8 LOK |
319 | { |
320 | if (m_cec) | |
4bec9d79 | 321 | return m_cec->TransmitKeypress(iDestination, key, bWait); |
a33794d8 LOK |
322 | return false; |
323 | } | |
324 | ||
4bec9d79 | 325 | bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */) |
a33794d8 LOK |
326 | { |
327 | if (m_cec) | |
4bec9d79 | 328 | return m_cec->TransmitKeyRelease(iDestination, bWait); |
a33794d8 LOK |
329 | return false; |
330 | } | |
331 | ||
f71a1df9 | 332 | cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress) |
ed21be2a LOK |
333 | { |
334 | cec_osd_name retVal; | |
335 | retVal.device = iAddress; | |
336 | retVal.name[0] = 0; | |
337 | ||
338 | if (m_cec) | |
339 | retVal = m_cec->GetDeviceOSDName(iAddress); | |
340 | ||
341 | return retVal; | |
342 | } | |
343 | ||
32403cc3 | 344 | void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...) |
2abe74eb | 345 | { |
5477a250 | 346 | CStdString strLog; |
fa4798bd | 347 | |
5477a250 LOK |
348 | va_list argList; |
349 | va_start(argList, strFormat); | |
350 | strLog.FormatV(strFormat, argList); | |
351 | va_end(argList); | |
352 | ||
353 | CLibCEC *instance = CLibCEC::GetInstance(); | |
a2198e5e LOK |
354 | if (!instance) |
355 | return; | |
a8559e01 | 356 | CLockObject lock(instance->m_logMutex); |
5477a250 LOK |
357 | |
358 | cec_log_message message; | |
359 | message.level = level; | |
360 | message.time = GetTimeMs() - instance->m_iStartTime; | |
361 | snprintf(message.message, sizeof(message.message), "%s", strLog.c_str()); | |
362 | ||
8a7c0a07 | 363 | if (instance->m_callbacks && instance->m_callbacks->CBCecLogMessage) |
5477a250 LOK |
364 | instance->m_callbacks->CBCecLogMessage(instance->m_cbParam, message); |
365 | else | |
366 | instance->m_logBuffer.Push(message); | |
2abe74eb LOK |
367 | } |
368 | ||
32403cc3 | 369 | void CLibCEC::AddKey(const cec_keypress &key) |
95ba7a09 | 370 | { |
02e7043e | 371 | CLibCEC *instance = CLibCEC::GetInstance(); |
a2198e5e LOK |
372 | if (!instance) |
373 | return; | |
02e7043e LOK |
374 | CLockObject lock(instance->m_mutex); |
375 | ||
376 | AddLog(CEC_LOG_DEBUG, "key pressed: %1x", key.keycode); | |
377 | ||
8a7c0a07 | 378 | if (instance->m_callbacks && instance->m_callbacks->CBCecKeyPress) |
02e7043e | 379 | instance->m_callbacks->CBCecKeyPress(instance->m_cbParam, key); |
fa4798bd | 380 | else |
02e7043e LOK |
381 | instance->m_keyBuffer.Push(key); |
382 | ||
383 | instance->m_iCurrentButton = key.duration > 0 ? CEC_USER_CONTROL_CODE_UNKNOWN : key.keycode; | |
384 | instance->m_buttontime = key.duration > 0 ? 0 : GetTimeMs(); | |
385 | } | |
386 | ||
32403cc3 LOK |
387 | void CLibCEC::ConfigurationChanged(const libcec_configuration &config) |
388 | { | |
389 | CLibCEC *instance = CLibCEC::GetInstance(); | |
390 | CLockObject lock(instance->m_mutex); | |
391 | ||
392 | if (instance->m_callbacks && | |
393 | config.clientVersion >= CEC_CLIENT_VERSION_1_5_0 && | |
8a7c0a07 | 394 | instance->m_callbacks->CBCecConfigurationChanged && |
468d786e | 395 | instance->m_cec->IsInitialised()) |
32403cc3 LOK |
396 | instance->m_callbacks->CBCecConfigurationChanged(instance->m_cbParam, config); |
397 | } | |
398 | ||
02e7043e LOK |
399 | void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode) |
400 | { | |
401 | /* push keypress to the keybuffer with 0 duration. | |
402 | push another press to the keybuffer with the duration set when the button is released */ | |
403 | cec_keypress key; | |
404 | key.duration = 0; | |
405 | key.keycode = iButtonCode; | |
406 | ||
407 | AddKey(key); | |
95ba7a09 LOK |
408 | } |
409 | ||
2abe74eb LOK |
410 | void CLibCEC::AddKey(void) |
411 | { | |
02e7043e | 412 | CLibCEC *instance = CLibCEC::GetInstance(); |
a2198e5e LOK |
413 | if (!instance) |
414 | return; | |
02e7043e LOK |
415 | CLockObject lock(instance->m_mutex); |
416 | ||
417 | if (instance->m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN) | |
2abe74eb LOK |
418 | { |
419 | cec_keypress key; | |
25701fa6 | 420 | |
02e7043e LOK |
421 | key.duration = (unsigned int) (GetTimeMs() - instance->m_buttontime); |
422 | key.keycode = instance->m_iCurrentButton; | |
423 | AddLog(CEC_LOG_DEBUG, "key released: %1x", key.keycode); | |
fa4798bd | 424 | |
8a7c0a07 | 425 | if (instance->m_callbacks && instance->m_callbacks->CBCecKeyPress) |
02e7043e | 426 | instance->m_callbacks->CBCecKeyPress(instance->m_cbParam, key); |
fa4798bd | 427 | else |
02e7043e LOK |
428 | instance->m_keyBuffer.Push(key); |
429 | instance->m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; | |
2abe74eb | 430 | } |
02e7043e | 431 | instance->m_buttontime = 0; |
2abe74eb LOK |
432 | } |
433 | ||
e9de9629 | 434 | void CLibCEC::AddCommand(const cec_command &command) |
2abe74eb | 435 | { |
02e7043e | 436 | CLibCEC *instance = CLibCEC::GetInstance(); |
a2198e5e LOK |
437 | if (!instance) |
438 | return; | |
02e7043e LOK |
439 | CLockObject lock(instance->m_mutex); |
440 | ||
441 | AddLog(CEC_LOG_NOTICE, ">> %s (%X) -> %s (%X): %s (%2X)", instance->m_cec->ToString(command.initiator), command.initiator, instance->m_cec->ToString(command.destination), command.destination, instance->m_cec->ToString(command.opcode), command.opcode); | |
442 | ||
8a7c0a07 | 443 | if (instance->m_callbacks && instance->m_callbacks->CBCecCommand) |
02e7043e LOK |
444 | instance->m_callbacks->CBCecCommand(instance->m_cbParam, command); |
445 | else if (!instance->m_commandBuffer.Push(command)) | |
2abe74eb | 446 | AddLog(CEC_LOG_WARNING, "command buffer is full"); |
2abe74eb LOK |
447 | } |
448 | ||
b1c47f9d LOK |
449 | void CLibCEC::Alert(const libcec_alert type, const libcec_parameter ¶m) |
450 | { | |
451 | CLibCEC *instance = CLibCEC::GetInstance(); | |
452 | if (!instance) | |
453 | return; | |
454 | CLockObject lock(instance->m_mutex); | |
455 | ||
456 | libcec_configuration config; | |
457 | instance->GetCurrentConfiguration(&config); | |
458 | ||
459 | if (instance->m_callbacks && | |
460 | config.clientVersion >= CEC_CLIENT_VERSION_1_6_0 && | |
0b714871 | 461 | instance->m_cec->IsInitialised() && |
8a7c0a07 | 462 | instance->m_callbacks->CBCecAlert) |
b1c47f9d | 463 | instance->m_callbacks->CBCecAlert(instance->m_cbParam, type, param); |
0b714871 LOK |
464 | |
465 | if (type == CEC_ALERT_CONNECTION_LOST) | |
466 | instance->Close(); | |
b1c47f9d LOK |
467 | } |
468 | ||
2abe74eb LOK |
469 | void CLibCEC::CheckKeypressTimeout(void) |
470 | { | |
471 | if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT) | |
472 | { | |
473 | AddKey(); | |
474 | m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; | |
475 | } | |
476 | } | |
477 | ||
e1804a4e LOK |
478 | int CLibCEC::MenuStateChanged(const cec_menu_state newState) |
479 | { | |
480 | int iReturn(0); | |
481 | ||
482 | CLibCEC *instance = CLibCEC::GetInstance(); | |
483 | if (!instance) | |
484 | return iReturn; | |
485 | CLockObject lock(instance->m_mutex); | |
486 | ||
487 | AddLog(CEC_LOG_NOTICE, ">> %s: %s", instance->m_cec->ToString(CEC_OPCODE_MENU_REQUEST), instance->m_cec->ToString(newState)); | |
488 | ||
489 | libcec_configuration config; | |
490 | instance->GetCurrentConfiguration(&config); | |
491 | ||
492 | if (instance->m_callbacks && | |
493 | config.clientVersion >= CEC_CLIENT_VERSION_1_6_2 && | |
c3ce6b47 LOK |
494 | instance->m_callbacks->CBCecMenuStateChanged) |
495 | iReturn = instance->m_callbacks->CBCecMenuStateChanged(instance->m_cbParam, newState); | |
e1804a4e LOK |
496 | |
497 | return iReturn; | |
498 | } | |
499 | ||
f42d3e0f LOK |
500 | bool CLibCEC::SetStreamPath(cec_logical_address iAddress) |
501 | { | |
502 | uint16_t iPhysicalAddress = GetDevicePhysicalAddress(iAddress); | |
503 | if (iPhysicalAddress != 0xFFFF) | |
504 | return SetStreamPath(iPhysicalAddress); | |
505 | return false; | |
506 | } | |
507 | ||
508 | bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress) | |
509 | { | |
510 | return m_cec->SetStreamPath(iPhysicalAddress); | |
511 | } | |
512 | ||
80b72250 LOK |
513 | cec_logical_addresses CLibCEC::GetLogicalAddresses(void) |
514 | { | |
515 | cec_logical_addresses addr = m_cec->GetLogicalAddresses(); | |
516 | return addr; | |
517 | } | |
518 | ||
5477a250 LOK |
519 | static CLibCEC *g_libCEC_instance(NULL); |
520 | CLibCEC *CLibCEC::GetInstance(void) | |
521 | { | |
522 | return g_libCEC_instance; | |
523 | } | |
524 | ||
525 | void CLibCEC::SetInstance(CLibCEC *instance) | |
526 | { | |
527 | if (g_libCEC_instance) | |
528 | delete g_libCEC_instance; | |
529 | g_libCEC_instance = instance; | |
530 | } | |
531 | ||
81b78e4a | 532 | void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types, uint16_t UNUSED(iPhysicalAddress) /* = 0 */) |
f8513317 | 533 | { |
5477a250 LOK |
534 | CLibCEC *lib = new CLibCEC(strDeviceName, types); |
535 | CLibCEC::SetInstance(lib); | |
536 | return static_cast< void* > (lib); | |
f8513317 LOK |
537 | } |
538 | ||
3efda01a | 539 | void * CECInitialise(libcec_configuration *configuration) |
caca2d81 LOK |
540 | { |
541 | CLibCEC *lib = new CLibCEC(configuration); | |
542 | CLibCEC::SetInstance(lib); | |
543 | return static_cast< void* > (lib); | |
544 | } | |
545 | ||
a2198e5e LOK |
546 | bool CECStartBootloader(void) |
547 | { | |
a2198e5e LOK |
548 | bool bReturn(false); |
549 | cec_adapter deviceList[1]; | |
550 | if (CUSBCECAdapterDetection::FindAdapters(deviceList, 1) > 0) | |
551 | { | |
08d80226 LOK |
552 | CUSBCECAdapterCommunication comm(NULL, deviceList[0].comm); |
553 | CTimeout timeout(10000); | |
3c5b4d05 | 554 | while (timeout.TimeLeft() > 0 && (bReturn = comm.Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false) |
08d80226 LOK |
555 | { |
556 | comm.Close(); | |
b76619e8 | 557 | CEvent::Sleep(500); |
08d80226 LOK |
558 | } |
559 | if (comm.IsOpen()) | |
560 | bReturn = comm.StartBootloader(); | |
a2198e5e | 561 | } |
08d80226 | 562 | |
a2198e5e LOK |
563 | return bReturn; |
564 | } | |
565 | ||
5477a250 | 566 | void CECDestroy(CEC::ICECAdapter *UNUSED(instance)) |
25701fa6 | 567 | { |
5477a250 | 568 | CLibCEC::SetInstance(NULL); |
25701fa6 | 569 | } |
03ae897d LOK |
570 | |
571 | const char *CLibCEC::ToString(const cec_menu_state state) | |
572 | { | |
573 | return m_cec->ToString(state); | |
574 | } | |
575 | ||
576 | const char *CLibCEC::ToString(const cec_version version) | |
577 | { | |
578 | return m_cec->ToString(version); | |
579 | } | |
580 | ||
581 | const char *CLibCEC::ToString(const cec_power_status status) | |
582 | { | |
583 | return m_cec->ToString(status); | |
584 | } | |
585 | ||
586 | const char *CLibCEC::ToString(const cec_logical_address address) | |
587 | { | |
588 | return m_cec->ToString(address); | |
589 | } | |
590 | ||
591 | const char *CLibCEC::ToString(const cec_deck_control_mode mode) | |
592 | { | |
593 | return m_cec->ToString(mode); | |
594 | } | |
595 | ||
596 | const char *CLibCEC::ToString(const cec_deck_info status) | |
597 | { | |
598 | return m_cec->ToString(status); | |
599 | } | |
600 | ||
601 | const char *CLibCEC::ToString(const cec_opcode opcode) | |
602 | { | |
603 | return m_cec->ToString(opcode); | |
604 | } | |
605 | ||
606 | const char *CLibCEC::ToString(const cec_system_audio_status mode) | |
607 | { | |
608 | return m_cec->ToString(mode); | |
609 | } | |
610 | ||
611 | const char *CLibCEC::ToString(const cec_audio_status status) | |
612 | { | |
613 | return m_cec->ToString(status); | |
614 | } | |
615 | ||
616 | const char *CLibCEC::ToString(const cec_vendor_id vendor) | |
617 | { | |
618 | return m_cec->ToString(vendor); | |
619 | } | |
caca2d81 LOK |
620 | |
621 | const char *CLibCEC::ToString(const cec_client_version version) | |
622 | { | |
623 | return m_cec->ToString(version); | |
624 | } | |
d40928b5 | 625 | |
3efda01a LOK |
626 | const char *CLibCEC::ToString(const cec_server_version version) |
627 | { | |
628 | return m_cec->ToString(version); | |
629 | } | |
630 | ||
a75e3a5a LOK |
631 | const char *CLibCEC::ToString(const cec_device_type type) |
632 | { | |
633 | return m_cec->ToString(type); | |
634 | } | |
635 | ||
d40928b5 LOK |
636 | bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration) |
637 | { | |
0b714871 | 638 | return m_cec->GetCurrentConfiguration(configuration); |
224ea877 LOK |
639 | } |
640 | ||
30b4aac0 LOK |
641 | bool CLibCEC::SetConfiguration(const libcec_configuration *configuration) |
642 | { | |
643 | return m_cec->SetConfiguration(configuration); | |
644 | } | |
645 | ||
224ea877 LOK |
646 | bool CLibCEC::CanPersistConfiguration(void) |
647 | { | |
648 | return m_cec->CanPersistConfiguration(); | |
649 | } | |
650 | ||
651 | bool CLibCEC::PersistConfiguration(libcec_configuration *configuration) | |
652 | { | |
653 | return m_cec->PersistConfiguration(configuration); | |
654 | } | |
3efda01a LOK |
655 | |
656 | void CLibCEC::RescanActiveDevices(void) | |
657 | { | |
658 | return m_cec->RescanActiveDevices(); | |
659 | } | |
c9549d35 LOK |
660 | |
661 | bool CLibCEC::IsLibCECActiveSource(void) | |
662 | { | |
a2f4c09c LOK |
663 | bool bReturn(false); |
664 | if (m_cec) | |
665 | { | |
666 | cec_logical_address activeSource = m_cec->GetActiveSource(); | |
667 | if (activeSource != CECDEVICE_UNKNOWN) | |
668 | bReturn = m_cec->m_busDevices[activeSource]->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC; | |
669 | } | |
670 | return bReturn; | |
c9549d35 | 671 | } |
9878069e LOK |
672 | |
673 | cec_device_type CLibCEC::GetType(cec_logical_address address) | |
674 | { | |
675 | switch (address) | |
676 | { | |
677 | case CECDEVICE_AUDIOSYSTEM: | |
678 | return CEC_DEVICE_TYPE_AUDIO_SYSTEM; | |
679 | case CECDEVICE_PLAYBACKDEVICE1: | |
680 | case CECDEVICE_PLAYBACKDEVICE2: | |
681 | case CECDEVICE_PLAYBACKDEVICE3: | |
682 | return CEC_DEVICE_TYPE_PLAYBACK_DEVICE; | |
683 | case CECDEVICE_RECORDINGDEVICE1: | |
684 | case CECDEVICE_RECORDINGDEVICE2: | |
685 | case CECDEVICE_RECORDINGDEVICE3: | |
686 | return CEC_DEVICE_TYPE_RECORDING_DEVICE; | |
687 | case CECDEVICE_TUNER1: | |
688 | case CECDEVICE_TUNER2: | |
689 | case CECDEVICE_TUNER3: | |
690 | case CECDEVICE_TUNER4: | |
691 | return CEC_DEVICE_TYPE_TUNER; | |
692 | case CECDEVICE_TV: | |
693 | return CEC_DEVICE_TYPE_TV; | |
694 | default: | |
695 | return CEC_DEVICE_TYPE_RESERVED; | |
696 | } | |
697 | } | |
209b7a5c LOK |
698 | |
699 | uint16_t CLibCEC::GetMaskForType(cec_logical_address address) | |
700 | { | |
701 | return GetMaskForType(GetType(address)); | |
702 | } | |
703 | ||
704 | uint16_t CLibCEC::GetMaskForType(cec_device_type type) | |
705 | { | |
706 | switch (type) | |
707 | { | |
708 | case CEC_DEVICE_TYPE_AUDIO_SYSTEM: | |
709 | { | |
710 | cec_logical_addresses addr; | |
711 | addr.Clear(); | |
712 | addr.Set(CECDEVICE_AUDIOSYSTEM); | |
713 | return addr.AckMask(); | |
714 | } | |
715 | case CEC_DEVICE_TYPE_PLAYBACK_DEVICE: | |
716 | { | |
717 | cec_logical_addresses addr; | |
718 | addr.Clear(); | |
719 | addr.Set(CECDEVICE_PLAYBACKDEVICE1); | |
720 | addr.Set(CECDEVICE_PLAYBACKDEVICE2); | |
721 | addr.Set(CECDEVICE_PLAYBACKDEVICE3); | |
722 | return addr.AckMask(); | |
723 | } | |
724 | case CEC_DEVICE_TYPE_RECORDING_DEVICE: | |
725 | { | |
726 | cec_logical_addresses addr; | |
727 | addr.Clear(); | |
728 | addr.Set(CECDEVICE_RECORDINGDEVICE1); | |
729 | addr.Set(CECDEVICE_RECORDINGDEVICE2); | |
730 | addr.Set(CECDEVICE_RECORDINGDEVICE3); | |
731 | return addr.AckMask(); | |
732 | } | |
733 | case CEC_DEVICE_TYPE_TUNER: | |
734 | { | |
735 | cec_logical_addresses addr; | |
736 | addr.Clear(); | |
737 | addr.Set(CECDEVICE_TUNER1); | |
738 | addr.Set(CECDEVICE_TUNER2); | |
739 | addr.Set(CECDEVICE_TUNER3); | |
740 | addr.Set(CECDEVICE_TUNER4); | |
741 | return addr.AckMask(); | |
742 | } | |
743 | case CEC_DEVICE_TYPE_TV: | |
744 | { | |
745 | cec_logical_addresses addr; | |
746 | addr.Clear(); | |
747 | addr.Set(CECDEVICE_TV); | |
748 | return addr.AckMask(); | |
749 | } | |
750 | default: | |
751 | return 0; | |
752 | } | |
753 | } | |
f80cd208 LOK |
754 | |
755 | bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = 10000 */) | |
756 | { | |
757 | if (m_cec->IsRunning()) | |
758 | return false; | |
759 | ||
760 | return m_cec->GetDeviceInformation(strPort, config, iTimeoutMs); | |
a75e3a5a | 761 | } |