Add an Alert callback. bigzid: 462
[deb_libcec.git] / src / cec-config / cec-config.cpp
CommitLineData
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
45using namespace CEC;
46using namespace std;
47using namespace PLATFORM;
48
49#include <cecloader.h>
50
51CMutex g_outputMutex;
52
960f33c6 53CEvent g_responseEvent;
f57cba64
LOK
54cec_opcode g_lastCommand = CEC_OPCODE_NONE;
55
960f33c6 56CEvent g_keyEvent;
f57cba64
LOK
57cec_user_control_code g_lastKey = CEC_USER_CONTROL_CODE_UNKNOWN;
58
59ICECCallbacks g_callbacks;
c7dc6e28 60libcec_configuration g_config;
f57cba64
LOK
61ICECAdapter * g_parser;
62cec_logical_address g_primaryAddress;
63
64inline 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
79bool 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
111int 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
128int 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
135int 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
142void 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;
5ee37222 148 g_callbacks.CBCecAlert = NULL;
f57cba64
LOK
149 adapter->EnableCallbacks(NULL, &g_callbacks);
150}
151
152ICECAdapter *CreateParser(cec_device_type_list typeList)
153{
154 ICECAdapter *parser = LibCecInit("ButtonConfig", typeList);
155 if (!parser)
156 {
157 #ifdef __WINDOWS__
158 PrintToStdOut("Cannot load libcec.dll");
159 #else
160 PrintToStdOut("Cannot load libcec.so");
161 #endif
162 return NULL;
163 }
164
165 PrintToStdOut("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
166
167 return parser;
168}
169
170bool ProcessConsoleCommand(string &input)
171{
172 if (!input.empty())
173 {
174 string command;
175 if (GetWord(input, command))
176 {
177 if (command == "q" || command == "quit")
178 return false;
179 }
180 }
181 return true;
182}
183
184bool OpenConnection(cec_device_type type = CEC_DEVICE_TYPE_RECORDING_DEVICE)
185{
c7dc6e28
LOK
186 g_config.Clear();
187 snprintf(g_config.strDeviceName, 13, "CEC-config");
188 g_config.callbackParam = NULL;
3efda01a 189 g_config.clientVersion = (uint32_t)CEC_CLIENT_VERSION_1_5_0;
c7dc6e28
LOK
190 g_callbacks.CBCecLogMessage = &CecLogMessage;
191 g_callbacks.CBCecKeyPress = &CecKeyPress;
192 g_callbacks.CBCecCommand = &CecCommand;
193 g_config.callbacks = &g_callbacks;
f57cba64 194
c7dc6e28
LOK
195 g_config.deviceTypes.add(type);
196
197 g_parser = LibCecInitialise(&g_config);
f57cba64
LOK
198 if (!g_parser)
199 return false;
200
201 CStdString strPort;
202 cec_adapter devices[10];
203 uint8_t iDevicesFound = g_parser->FindAdapters(devices, 10, NULL);
204 if (iDevicesFound <= 0)
205 {
206 PrintToStdOut("autodetect FAILED");
207 UnloadLibCec(g_parser);
208 return false;
209 }
210 else
211 {
212 strPort = devices[0].comm;
213 }
214
f57cba64 215 PrintToStdOut("opening a connection to the CEC adapter...");
f57cba64
LOK
216 if (!g_parser->Open(strPort.c_str()))
217 {
218 PrintToStdOut("unable to open the device on port %s", strPort.c_str());
219 UnloadLibCec(g_parser);
220 return false;
221 }
222
223 cec_logical_addresses addr = g_parser->GetLogicalAddresses();
224 g_primaryAddress = addr.primary;
225
226 PrintToStdOut("cec device opened. using logical address %X", g_primaryAddress);
227 return true;
228}
229
230int8_t FindPhysicalAddressPortNumber(void)
231{
86393878 232 PrintToStdOut("Enter the HDMI port number to which you connected your CEC adapter, followed by <enter>. Valid ports are in the range 1-15. Anything else will cancel this wizard.");
f57cba64
LOK
233 string input;
234 getline(cin, input);
235 cin.clear();
86393878 236 if (input.empty())
f57cba64 237 return -1;
86393878 238
21d83978
LOK
239 int hdmiport = atoi(input.c_str());
240 return (hdmiport < 1 || hdmiport > 15) ? -1 : (int8_t)hdmiport;
f57cba64
LOK
241}
242
243cec_logical_address FindPhysicalAddressBaseDevice(void)
244{
3bafe353 245 PrintToStdOut("Press 1 if 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.");
f57cba64
LOK
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
260uint16_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 314bool 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 {
960f33c6 329 g_responseEvent.Wait((uint32_t)(iTarget - iNow));
f57cba64
LOK
330 if (g_lastCommand == CEC_OPCODE_REQUEST_ACTIVE_SOURCE)
331 break;
332 iNow = GetTimeMs();
333 }
334 }
335 }
336
337 currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV);
338
339 if (currentTvPower != CEC_POWER_STATUS_ON)
340 PrintToStdOut("Failed to power on the TV, or the TV does not respond properly");
341
342 return currentTvPower == CEC_POWER_STATUS_ON;
343}
344
39b1216c 345int main (int UNUSED(argc), char *UNUSED(argv[]))
f57cba64
LOK
346{
347 PrintToStdOut("=== USB-CEC Adapter Configuration ===\n");
348 if (!OpenConnection())
349 return 1;
350
351 if (!PowerOnTV())
352 return 1;
353
354 bool bAddressOk(false);
f57cba64
LOK
355 while (!bAddressOk)
356 {
d40928b5 357 uint16_t iAddress = FindPhysicalAddress();
f57cba64
LOK
358
359 PrintToStdOut("Physical address: %4X", iAddress);
360 PrintToStdOut("Is this correct (y/n)?");
361 string input;
362 getline(cin, input);
363 cin.clear();
364 bAddressOk = (input == "y" || input == "Y");
365 }
366
b0a7410c 367 g_parser->GetCurrentConfiguration(&g_config);
caca2d81 368
b0a7410c
LOK
369 {
370 cec_menu_language lang;
371 if (g_parser->GetDeviceMenuLanguage(CECDEVICE_TV, &lang))
372 {
373 PrintToStdOut("TV menu language: %s", lang.language);
374 PrintToStdOut("Do you want the application to use the menu language of the TV (y/n)?");
375 string input;
376 getline(cin, input);
377 cin.clear();
9a5fc0e8 378 g_config.bUseTVMenuLanguage = (input == "y" || input == "Y") ? 1 : 0;
b0a7410c
LOK
379 }
380 else
381 {
382 PrintToStdOut("The TV did not respond properly to the menu language request.");
383 }
384 }
385
b0a7410c 386 {
ca27e6cf 387 PrintToStdOut("Do you want to make the CEC adapter the active source when starting the application (y/n)?");
b0a7410c
LOK
388 string input;
389 getline(cin, input);
390 cin.clear();
ca27e6cf 391 g_config.bActivateSource = (input == "y" || input == "Y") ? 1 : 0;
b0a7410c
LOK
392 }
393
b0a7410c
LOK
394 {
395 PrintToStdOut("Do you want to power off CEC devices when closing the application (y/n)?");
396 string input;
397 getline(cin, input);
398 cin.clear();
ca27e6cf
LOK
399 if (input == "y" || input == "Y")
400 g_config.powerOffDevices.Set(CECDEVICE_BROADCAST);
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();
41e3372a
LOK
416 g_config.bPowerOffOnStandby = (input == "y" || input == "Y") ? 1 : 0;
417 }
418
419 {
420 PrintToStdOut("Do you want to send an inactive source message when stopping the application (y/n)?");
421 string input;
422 getline(cin, input);
423 cin.clear();
424 g_config.bSendInactiveSource = (input == "y" || input == "Y") ? 1 : 0;
b0a7410c
LOK
425 }
426
427 PrintToStdOut("\n\n=== USB-CEC Adapter Configuration Summary ===");
428 PrintToStdOut("HDMI port number: %d", g_config.iHDMIPort);
429 PrintToStdOut("Connected to HDMI device: %X", (uint8_t)g_config.baseDevice);
430 PrintToStdOut("Physical address: %4X", g_config.iPhysicalAddress);
9a5fc0e8 431 PrintToStdOut("Use the TV's language setting: %s", g_config.bUseTVMenuLanguage ? "yes" : "no");
ca27e6cf
LOK
432 PrintToStdOut("Make the adapter the active source when starting XBMC: %s", g_config.bActivateSource ? "yes" : "no");
433 PrintToStdOut("Power off devices when stopping XBMC: %s", g_config.powerOffDevices.IsSet(CECDEVICE_BROADCAST) ? "yes" : "no");
9a5fc0e8 434 PrintToStdOut("Put devices in standby mode when activating screensaver: %s", g_config.bPowerOffScreensaver ? "yes" : "no");
41e3372a
LOK
435 PrintToStdOut("Put this PC in standby mode when the TV is switched off: %s", g_config.bPowerOffOnStandby ? "yes" : "no");
436 PrintToStdOut("Seend an inactive source message when stopping XBMC: %s\n\n", g_config.bSendInactiveSource ? "yes" : "no");
9a5fc0e8
LOK
437
438 if (g_parser->CanPersistConfiguration())
439 {
440 PrintToStdOut("Do you want to store these settings in the adapter (y/n)?");
441 string input;
442 getline(cin, input);
443 cin.clear();
444 if (input == "y" || input == "Y")
445 {
446 PrintToStdOut("Storing settings ...");
447 if (g_parser->PersistConfiguration(&g_config))
448 PrintToStdOut("Settings stored.");
449 else
450 PrintToStdOut("The settings could not be stored");
451 }
452 }
453 else
454 {
455 PrintToStdOut("This adapter doesn't support settings persistence. Please set up these settings in your media player application.");
1361efd2
LOK
456
457 ofstream configOutput;
458 configOutput.open("usb_2548_1001.xml");
459 if (configOutput.is_open())
460 {
41e3372a
LOK
461 CStdString strWakeDevices;
462 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
463 if (g_config.wakeDevices[iPtr])
464 strWakeDevices.AppendFormat(" %d" + iPtr);
465 CStdString strStandbyDevices;
466 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
467 if (g_config.powerOffDevices[iPtr])
468 strStandbyDevices.AppendFormat(" %d" + iPtr);
469
1361efd2
LOK
470 configOutput <<
471 "<settings>\n" <<
41e3372a
LOK
472 "\t<setting id=\"enabled\" value=\"1\" />\n" <<
473 "\t<setting id=\"activate_source\" value=\"" << (int)g_config.bActivateSource << "\" />\n" <<
be54289b
LOK
474 "\t<setting id=\"wake_devices\" value=\"" << strWakeDevices.c_str() << "\" />\n" <<
475 "\t<setting id=\"standby_devices\" value=\"" << strStandbyDevices.c_str() << "\" />\n" <<
1361efd2
LOK
476 "\t<setting id=\"cec_standby_screensaver\" value=\"" << (int)g_config.bPowerOffScreensaver << "\" />\n" <<
477 "\t<setting id=\"standby_pc_on_tv_standby\" value=\"" << (int)g_config.bPowerOffOnStandby << "\" />\n" <<
41e3372a
LOK
478 "\t<setting id=\"use_tv_menu_language\" value=\"" << (int)g_config.bUseTVMenuLanguage << "\" />\n" <<
479 "\t<setting id=\"physical_address\" value=\"" << hex << g_config.iPhysicalAddress << "\" />\n" <<
480 "\t<setting id=\"cec_hdmi_port\" value=\"" << g_config.iHDMIPort << "\" />\n" <<
481 "\t<setting id=\"connected_device\" value=\"" << (int)g_config.baseDevice << "\" />\n" <<
1361efd2 482 "\t<setting id=\"port\" value=\"\" />\n" <<
41e3372a 483 "\t<setting id=\"send_inactive_source\" value=\"" << (int)g_config.bSendInactiveSource << "\" />\n" <<
1361efd2
LOK
484 "</settings>";
485 configOutput.close();
486
487 PrintToStdOut("The configuration has been stored in 'usb_2548_1001.xml'. Copy this file to ~/.userdata/peripheral_data to use it in XBMC");
488 }
9a5fc0e8 489 }
f57cba64
LOK
490
491 g_parser->StandbyDevices();
492 g_parser->Close();
493 UnloadLibCec(g_parser);
494
495 PrintToStdOut("Press enter to close this wizard.");
496 string input;
497 getline(cin, input);
498 return 0;
499}