Commit | Line | Data |
---|---|---|
f57cba64 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. | |
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 "../../include/cec.h" | |
34 | ||
35 | #include <cstdio> | |
36 | #include <fcntl.h> | |
37 | #include <iostream> | |
38 | #include <fstream> | |
39 | #include <string> | |
40 | #include <sstream> | |
41 | #include "../lib/platform/threads/mutex.h" | |
42 | #include "../lib/platform/util/timeutils.h" | |
43 | #include "../lib/implementations/CECCommandHandler.h" | |
44 | ||
45 | using namespace CEC; | |
46 | using namespace std; | |
47 | using namespace PLATFORM; | |
48 | ||
49 | #include <cecloader.h> | |
50 | ||
51 | CMutex g_outputMutex; | |
52 | ||
960f33c6 | 53 | CEvent g_responseEvent; |
f57cba64 LOK |
54 | cec_opcode g_lastCommand = CEC_OPCODE_NONE; |
55 | ||
960f33c6 | 56 | CEvent g_keyEvent; |
f57cba64 LOK |
57 | cec_user_control_code g_lastKey = CEC_USER_CONTROL_CODE_UNKNOWN; |
58 | ||
59 | ICECCallbacks g_callbacks; | |
c7dc6e28 | 60 | libcec_configuration g_config; |
f57cba64 LOK |
61 | ICECAdapter * g_parser; |
62 | cec_logical_address g_primaryAddress; | |
63 | ||
64 | inline void PrintToStdOut(const char *strFormat, ...) | |
65 | { | |
66 | CStdString strLog; | |
67 | ||
68 | va_list argList; | |
69 | va_start(argList, strFormat); | |
70 | strLog.FormatV(strFormat, argList); | |
71 | va_end(argList); | |
72 | ||
73 | CLockObject lock(g_outputMutex); | |
74 | cout << strLog << endl; | |
75 | } | |
76 | ||
77 | //get the first word (separated by whitespace) from string data and place that in word | |
78 | //then remove that word from string data | |
79 | bool GetWord(string& data, string& word) | |
80 | { | |
81 | stringstream datastream(data); | |
82 | string end; | |
83 | ||
84 | datastream >> word; | |
85 | if (datastream.fail()) | |
86 | { | |
87 | data.clear(); | |
88 | return false; | |
89 | } | |
90 | ||
91 | size_t pos = data.find(word) + word.length(); | |
92 | ||
93 | if (pos >= data.length()) | |
94 | { | |
95 | data.clear(); | |
96 | return true; | |
97 | } | |
98 | ||
99 | data = data.substr(pos); | |
100 | ||
101 | datastream.clear(); | |
102 | datastream.str(data); | |
103 | ||
104 | datastream >> end; | |
105 | if (datastream.fail()) | |
106 | data.clear(); | |
107 | ||
108 | return true; | |
109 | } | |
110 | ||
111 | int CecLogMessage(void *UNUSED(cbParam), const cec_log_message &message) | |
112 | { | |
113 | switch (message.level) | |
114 | { | |
115 | case CEC_LOG_ERROR: | |
116 | PrintToStdOut("ERROR:\t%s", message.message); | |
117 | break; | |
118 | case CEC_LOG_WARNING: | |
119 | PrintToStdOut("ERROR:\t%s", message.message); | |
120 | break; | |
121 | default: | |
122 | break; | |
123 | } | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | int CecKeyPress(void *UNUSED(cbParam), const cec_keypress &key) | |
129 | { | |
f57cba64 | 130 | g_lastKey = key.keycode; |
960f33c6 | 131 | g_keyEvent.Signal(); |
f57cba64 LOK |
132 | return 0; |
133 | } | |
134 | ||
135 | int CecCommand(void *UNUSED(cbParam), const cec_command &command) | |
136 | { | |
f57cba64 | 137 | g_lastCommand = command.opcode; |
960f33c6 | 138 | g_responseEvent.Signal(); |
f57cba64 LOK |
139 | return 0; |
140 | } | |
141 | ||
142 | void EnableCallbacks(ICECAdapter *adapter) | |
143 | { | |
144 | g_callbacks.CBCecLogMessage = &CecLogMessage; | |
145 | g_callbacks.CBCecKeyPress = &CecKeyPress; | |
146 | g_callbacks.CBCecCommand = &CecCommand; | |
32403cc3 | 147 | g_callbacks.CBCecConfigurationChanged = NULL; |
f57cba64 LOK |
148 | adapter->EnableCallbacks(NULL, &g_callbacks); |
149 | } | |
150 | ||
151 | ICECAdapter *CreateParser(cec_device_type_list typeList) | |
152 | { | |
153 | ICECAdapter *parser = LibCecInit("ButtonConfig", typeList); | |
154 | if (!parser) | |
155 | { | |
156 | #ifdef __WINDOWS__ | |
157 | PrintToStdOut("Cannot load libcec.dll"); | |
158 | #else | |
159 | PrintToStdOut("Cannot load libcec.so"); | |
160 | #endif | |
161 | return NULL; | |
162 | } | |
163 | ||
164 | PrintToStdOut("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor()); | |
165 | ||
166 | return parser; | |
167 | } | |
168 | ||
169 | bool ProcessConsoleCommand(string &input) | |
170 | { | |
171 | if (!input.empty()) | |
172 | { | |
173 | string command; | |
174 | if (GetWord(input, command)) | |
175 | { | |
176 | if (command == "q" || command == "quit") | |
177 | return false; | |
178 | } | |
179 | } | |
180 | return true; | |
181 | } | |
182 | ||
183 | bool OpenConnection(cec_device_type type = CEC_DEVICE_TYPE_RECORDING_DEVICE) | |
184 | { | |
c7dc6e28 LOK |
185 | g_config.Clear(); |
186 | snprintf(g_config.strDeviceName, 13, "CEC-config"); | |
187 | g_config.callbackParam = NULL; | |
3efda01a | 188 | g_config.clientVersion = (uint32_t)CEC_CLIENT_VERSION_1_5_0; |
c7dc6e28 LOK |
189 | g_callbacks.CBCecLogMessage = &CecLogMessage; |
190 | g_callbacks.CBCecKeyPress = &CecKeyPress; | |
191 | g_callbacks.CBCecCommand = &CecCommand; | |
192 | g_config.callbacks = &g_callbacks; | |
f57cba64 | 193 | |
c7dc6e28 LOK |
194 | g_config.deviceTypes.add(type); |
195 | ||
196 | g_parser = LibCecInitialise(&g_config); | |
f57cba64 LOK |
197 | if (!g_parser) |
198 | return false; | |
199 | ||
200 | CStdString strPort; | |
201 | cec_adapter devices[10]; | |
202 | uint8_t iDevicesFound = g_parser->FindAdapters(devices, 10, NULL); | |
203 | if (iDevicesFound <= 0) | |
204 | { | |
205 | PrintToStdOut("autodetect FAILED"); | |
206 | UnloadLibCec(g_parser); | |
207 | return false; | |
208 | } | |
209 | else | |
210 | { | |
211 | strPort = devices[0].comm; | |
212 | } | |
213 | ||
f57cba64 | 214 | PrintToStdOut("opening a connection to the CEC adapter..."); |
f57cba64 LOK |
215 | if (!g_parser->Open(strPort.c_str())) |
216 | { | |
217 | PrintToStdOut("unable to open the device on port %s", strPort.c_str()); | |
218 | UnloadLibCec(g_parser); | |
219 | return false; | |
220 | } | |
221 | ||
222 | cec_logical_addresses addr = g_parser->GetLogicalAddresses(); | |
223 | g_primaryAddress = addr.primary; | |
224 | ||
225 | PrintToStdOut("cec device opened. using logical address %X", g_primaryAddress); | |
226 | return true; | |
227 | } | |
228 | ||
229 | int8_t FindPhysicalAddressPortNumber(void) | |
230 | { | |
231 | PrintToStdOut("Enter the HDMI port number to which you connected your CEC adapter, followed by <enter>. Only port 1, 2, 3 or 4 are supported. Anything else will cancel this wizard."); | |
232 | string input; | |
233 | getline(cin, input); | |
234 | cin.clear(); | |
235 | if (input.empty() || (input != "1" && input != "2" && input != "3" && input != "4")) | |
236 | return -1; | |
237 | return (int8_t)atoi(input.c_str()); | |
238 | } | |
239 | ||
240 | cec_logical_address FindPhysicalAddressBaseDevice(void) | |
241 | { | |
242 | PrintToStdOut("Press 1 of your CEC adapter is connected to your TV or\npress 2 if it's connected to an AVR, followed by <enter>. Anything else will cancel this wizard."); | |
243 | ||
244 | string input; | |
245 | getline(cin, input); | |
246 | cin.clear(); | |
247 | if (input.empty() || (input != "1" && input != "2")) | |
248 | { | |
249 | PrintToStdOut("Exiting..."); | |
250 | return CECDEVICE_UNKNOWN; | |
251 | } | |
252 | return (input == "2") ? | |
253 | CECDEVICE_AUDIOSYSTEM : | |
254 | CECDEVICE_TV; | |
255 | } | |
256 | ||
257 | uint16_t FindPhysicalAddress(void) | |
258 | { | |
259 | PrintToStdOut("=== Physical Address Configuration ===\n"); | |
260 | uint16_t iAddress(0xFFFF); | |
261 | ||
262 | PrintToStdOut("Do you want to let libCEC try to autodetect the address (y/n)?"); | |
263 | string input; | |
264 | getline(cin, input); | |
265 | cin.clear(); | |
266 | if (input == "y" || input == "Y") | |
267 | { | |
268 | cec_logical_address baseDevice = FindPhysicalAddressBaseDevice(); | |
269 | if (baseDevice == CECDEVICE_UNKNOWN) | |
270 | return iAddress; | |
271 | ||
272 | int8_t iPortNumber = FindPhysicalAddressPortNumber(); | |
273 | if (iPortNumber == -1) | |
274 | return iAddress; | |
275 | ||
276 | PrintToStdOut("Trying to detect the physical address..."); | |
277 | if (!g_parser->SetHDMIPort(baseDevice, iPortNumber)) | |
278 | PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings."); | |
279 | else | |
280 | { | |
281 | iAddress = g_parser->GetDevicePhysicalAddress(g_primaryAddress); | |
282 | if (iAddress == 0 || iAddress == 0xFFFF) | |
283 | PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings."); | |
284 | } | |
285 | } | |
286 | ||
287 | if (iAddress == 0 || iAddress == 0xFFFF) | |
288 | { | |
289 | PrintToStdOut("Please enter the physical address (0000 - FFFF), followed by <enter>."); | |
290 | getline(cin, input); | |
291 | cin.clear(); | |
292 | ||
293 | int iAddressTmp; | |
294 | if (sscanf(input.c_str(), "%x", &iAddressTmp) == 1) | |
295 | { | |
296 | if (iAddressTmp < 0 || iAddressTmp > 0xFFFF) | |
297 | iAddressTmp = 0xFFFF; | |
298 | iAddress = (uint16_t)iAddressTmp; | |
299 | } | |
300 | } | |
301 | ||
9a5fc0e8 LOK |
302 | if (iAddress != 0) |
303 | { | |
304 | g_parser->SetPhysicalAddress(iAddress); | |
305 | g_parser->SetActiveSource(g_config.deviceTypes[0]); | |
306 | } | |
307 | ||
f57cba64 LOK |
308 | return iAddress; |
309 | } | |
310 | ||
9a5fc0e8 | 311 | bool PowerOnTV(uint64_t iTimeout = 60000) |
f57cba64 LOK |
312 | { |
313 | cec_power_status currentTvPower(CEC_POWER_STATUS_UNKNOWN); | |
314 | uint64_t iNow = GetTimeMs(); | |
315 | uint64_t iTarget = iNow + iTimeout; | |
f57cba64 | 316 | |
9a5fc0e8 | 317 | if (currentTvPower != CEC_POWER_STATUS_ON) |
f57cba64 LOK |
318 | { |
319 | currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV); | |
320 | if (currentTvPower != CEC_POWER_STATUS_ON) | |
321 | { | |
9a5fc0e8 | 322 | PrintToStdOut("Sending 'power on' command to the TV\n=== Please wait ==="); |
f57cba64 LOK |
323 | g_parser->PowerOnDevices(CECDEVICE_TV); |
324 | while (iTarget > iNow) | |
325 | { | |
960f33c6 | 326 | g_responseEvent.Wait((uint32_t)(iTarget - iNow)); |
f57cba64 LOK |
327 | if (g_lastCommand == CEC_OPCODE_REQUEST_ACTIVE_SOURCE) |
328 | break; | |
329 | iNow = GetTimeMs(); | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
334 | currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV); | |
335 | ||
336 | if (currentTvPower != CEC_POWER_STATUS_ON) | |
337 | PrintToStdOut("Failed to power on the TV, or the TV does not respond properly"); | |
338 | ||
339 | return currentTvPower == CEC_POWER_STATUS_ON; | |
340 | } | |
341 | ||
39b1216c | 342 | int main (int UNUSED(argc), char *UNUSED(argv[])) |
f57cba64 LOK |
343 | { |
344 | PrintToStdOut("=== USB-CEC Adapter Configuration ===\n"); | |
345 | if (!OpenConnection()) | |
346 | return 1; | |
347 | ||
348 | if (!PowerOnTV()) | |
349 | return 1; | |
350 | ||
351 | bool bAddressOk(false); | |
f57cba64 LOK |
352 | while (!bAddressOk) |
353 | { | |
d40928b5 | 354 | uint16_t iAddress = FindPhysicalAddress(); |
f57cba64 LOK |
355 | |
356 | PrintToStdOut("Physical address: %4X", iAddress); | |
357 | PrintToStdOut("Is this correct (y/n)?"); | |
358 | string input; | |
359 | getline(cin, input); | |
360 | cin.clear(); | |
361 | bAddressOk = (input == "y" || input == "Y"); | |
362 | } | |
363 | ||
b0a7410c | 364 | g_parser->GetCurrentConfiguration(&g_config); |
caca2d81 | 365 | |
b0a7410c LOK |
366 | { |
367 | cec_menu_language lang; | |
368 | if (g_parser->GetDeviceMenuLanguage(CECDEVICE_TV, &lang)) | |
369 | { | |
370 | PrintToStdOut("TV menu language: %s", lang.language); | |
371 | PrintToStdOut("Do you want the application to use the menu language of the TV (y/n)?"); | |
372 | string input; | |
373 | getline(cin, input); | |
374 | cin.clear(); | |
9a5fc0e8 | 375 | g_config.bUseTVMenuLanguage = (input == "y" || input == "Y") ? 1 : 0; |
b0a7410c LOK |
376 | } |
377 | else | |
378 | { | |
379 | PrintToStdOut("The TV did not respond properly to the menu language request."); | |
380 | } | |
381 | } | |
382 | ||
b0a7410c | 383 | { |
ca27e6cf | 384 | PrintToStdOut("Do you want to make the CEC adapter the active source when starting the application (y/n)?"); |
b0a7410c LOK |
385 | string input; |
386 | getline(cin, input); | |
387 | cin.clear(); | |
ca27e6cf | 388 | g_config.bActivateSource = (input == "y" || input == "Y") ? 1 : 0; |
b0a7410c LOK |
389 | } |
390 | ||
b0a7410c LOK |
391 | { |
392 | PrintToStdOut("Do you want to power off CEC devices when closing the application (y/n)?"); | |
393 | string input; | |
394 | getline(cin, input); | |
395 | cin.clear(); | |
ca27e6cf LOK |
396 | if (input == "y" || input == "Y") |
397 | g_config.powerOffDevices.Set(CECDEVICE_BROADCAST); | |
b0a7410c LOK |
398 | } |
399 | ||
b0a7410c LOK |
400 | { |
401 | PrintToStdOut("Do you want to power off CEC devices when the screensaver is activated (y/n)?"); | |
402 | string input; | |
403 | getline(cin, input); | |
404 | cin.clear(); | |
9a5fc0e8 | 405 | g_config.bPowerOffScreensaver = (input == "y" || input == "Y") ? 1 : 0; |
b0a7410c LOK |
406 | } |
407 | ||
b0a7410c LOK |
408 | { |
409 | PrintToStdOut("Do you want to put the PC in standby when the TV is put in standby mode (y/n)?"); | |
410 | string input; | |
411 | getline(cin, input); | |
412 | cin.clear(); | |
41e3372a LOK |
413 | g_config.bPowerOffOnStandby = (input == "y" || input == "Y") ? 1 : 0; |
414 | } | |
415 | ||
416 | { | |
417 | PrintToStdOut("Do you want to send an inactive source message when stopping the application (y/n)?"); | |
418 | string input; | |
419 | getline(cin, input); | |
420 | cin.clear(); | |
421 | g_config.bSendInactiveSource = (input == "y" || input == "Y") ? 1 : 0; | |
b0a7410c LOK |
422 | } |
423 | ||
424 | PrintToStdOut("\n\n=== USB-CEC Adapter Configuration Summary ==="); | |
425 | PrintToStdOut("HDMI port number: %d", g_config.iHDMIPort); | |
426 | PrintToStdOut("Connected to HDMI device: %X", (uint8_t)g_config.baseDevice); | |
427 | PrintToStdOut("Physical address: %4X", g_config.iPhysicalAddress); | |
9a5fc0e8 | 428 | PrintToStdOut("Use the TV's language setting: %s", g_config.bUseTVMenuLanguage ? "yes" : "no"); |
ca27e6cf LOK |
429 | PrintToStdOut("Make the adapter the active source when starting XBMC: %s", g_config.bActivateSource ? "yes" : "no"); |
430 | PrintToStdOut("Power off devices when stopping XBMC: %s", g_config.powerOffDevices.IsSet(CECDEVICE_BROADCAST) ? "yes" : "no"); | |
9a5fc0e8 | 431 | PrintToStdOut("Put devices in standby mode when activating screensaver: %s", g_config.bPowerOffScreensaver ? "yes" : "no"); |
41e3372a LOK |
432 | PrintToStdOut("Put this PC in standby mode when the TV is switched off: %s", g_config.bPowerOffOnStandby ? "yes" : "no"); |
433 | PrintToStdOut("Seend an inactive source message when stopping XBMC: %s\n\n", g_config.bSendInactiveSource ? "yes" : "no"); | |
9a5fc0e8 LOK |
434 | |
435 | if (g_parser->CanPersistConfiguration()) | |
436 | { | |
437 | PrintToStdOut("Do you want to store these settings in the adapter (y/n)?"); | |
438 | string input; | |
439 | getline(cin, input); | |
440 | cin.clear(); | |
441 | if (input == "y" || input == "Y") | |
442 | { | |
443 | PrintToStdOut("Storing settings ..."); | |
444 | if (g_parser->PersistConfiguration(&g_config)) | |
445 | PrintToStdOut("Settings stored."); | |
446 | else | |
447 | PrintToStdOut("The settings could not be stored"); | |
448 | } | |
449 | } | |
450 | else | |
451 | { | |
452 | PrintToStdOut("This adapter doesn't support settings persistence. Please set up these settings in your media player application."); | |
1361efd2 LOK |
453 | |
454 | ofstream configOutput; | |
455 | configOutput.open("usb_2548_1001.xml"); | |
456 | if (configOutput.is_open()) | |
457 | { | |
41e3372a LOK |
458 | CStdString strWakeDevices; |
459 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) | |
460 | if (g_config.wakeDevices[iPtr]) | |
461 | strWakeDevices.AppendFormat(" %d" + iPtr); | |
462 | CStdString strStandbyDevices; | |
463 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) | |
464 | if (g_config.powerOffDevices[iPtr]) | |
465 | strStandbyDevices.AppendFormat(" %d" + iPtr); | |
466 | ||
1361efd2 LOK |
467 | configOutput << |
468 | "<settings>\n" << | |
41e3372a LOK |
469 | "\t<setting id=\"enabled\" value=\"1\" />\n" << |
470 | "\t<setting id=\"activate_source\" value=\"" << (int)g_config.bActivateSource << "\" />\n" << | |
be54289b LOK |
471 | "\t<setting id=\"wake_devices\" value=\"" << strWakeDevices.c_str() << "\" />\n" << |
472 | "\t<setting id=\"standby_devices\" value=\"" << strStandbyDevices.c_str() << "\" />\n" << | |
1361efd2 LOK |
473 | "\t<setting id=\"cec_standby_screensaver\" value=\"" << (int)g_config.bPowerOffScreensaver << "\" />\n" << |
474 | "\t<setting id=\"standby_pc_on_tv_standby\" value=\"" << (int)g_config.bPowerOffOnStandby << "\" />\n" << | |
41e3372a LOK |
475 | "\t<setting id=\"use_tv_menu_language\" value=\"" << (int)g_config.bUseTVMenuLanguage << "\" />\n" << |
476 | "\t<setting id=\"physical_address\" value=\"" << hex << g_config.iPhysicalAddress << "\" />\n" << | |
477 | "\t<setting id=\"cec_hdmi_port\" value=\"" << g_config.iHDMIPort << "\" />\n" << | |
478 | "\t<setting id=\"connected_device\" value=\"" << (int)g_config.baseDevice << "\" />\n" << | |
1361efd2 | 479 | "\t<setting id=\"port\" value=\"\" />\n" << |
41e3372a | 480 | "\t<setting id=\"send_inactive_source\" value=\"" << (int)g_config.bSendInactiveSource << "\" />\n" << |
1361efd2 LOK |
481 | "</settings>"; |
482 | configOutput.close(); | |
483 | ||
484 | PrintToStdOut("The configuration has been stored in 'usb_2548_1001.xml'. Copy this file to ~/.userdata/peripheral_data to use it in XBMC"); | |
485 | } | |
9a5fc0e8 | 486 | } |
f57cba64 LOK |
487 | |
488 | g_parser->StandbyDevices(); | |
489 | g_parser->Close(); | |
490 | UnloadLibCec(g_parser); | |
491 | ||
492 | PrintToStdOut("Press enter to close this wizard."); | |
493 | string input; | |
494 | getline(cin, input); | |
495 | return 0; | |
496 | } |