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