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