2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
34 #include "../include/cec.h"
43 #include "../lib/platform/threads/mutex.h"
44 #include "../lib/platform/util/timeutils.h"
45 #include "../lib/implementations/CECCommandHandler.h"
46 #include "../lib/platform/util/StdString.h"
50 using namespace PLATFORM
;
52 #include <cecloader.h>
56 CEvent g_responseEvent
;
57 cec_opcode g_lastCommand
= CEC_OPCODE_NONE
;
60 cec_user_control_code g_lastKey
= CEC_USER_CONTROL_CODE_UNKNOWN
;
62 ICECCallbacks g_callbacks
;
63 libcec_configuration g_config
;
64 ICECAdapter
* g_parser
;
66 inline void PrintToStdOut(const char *strFormat
, ...)
71 va_start(argList
, strFormat
);
72 strLog
.FormatV(strFormat
, argList
);
75 CLockObject
lock(g_outputMutex
);
76 cout
<< strLog
<< endl
;
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
)
83 stringstream
datastream(data
);
87 if (datastream
.fail())
93 size_t pos
= data
.find(word
) + word
.length();
95 if (pos
>= data
.length())
101 data
= data
.substr(pos
);
104 datastream
.str(data
);
107 if (datastream
.fail())
113 int CecLogMessage(void *UNUSED(cbParam
), const cec_log_message
&message
)
115 switch (message
.level
)
118 PrintToStdOut("ERROR:\t%s", message
.message
);
120 case CEC_LOG_WARNING
:
121 PrintToStdOut("ERROR:\t%s", message
.message
);
130 int CecKeyPress(void *UNUSED(cbParam
), const cec_keypress
&key
)
132 g_lastKey
= key
.keycode
;
137 int CecCommand(void *UNUSED(cbParam
), const cec_command
&command
)
139 g_lastCommand
= command
.opcode
;
140 g_responseEvent
.Signal();
144 bool ProcessConsoleCommand(string
&input
)
149 if (GetWord(input
, command
))
151 if (command
== "q" || command
== "quit")
158 bool OpenConnection(cec_device_type type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
)
161 snprintf(g_config
.strDeviceName
, 13, "CEC-config");
162 g_config
.callbackParam
= NULL
;
163 g_config
.clientVersion
= (uint32_t)CEC_CLIENT_VERSION_1_9_0
;
164 g_callbacks
.CBCecLogMessage
= &CecLogMessage
;
165 g_callbacks
.CBCecKeyPress
= &CecKeyPress
;
166 g_callbacks
.CBCecCommand
= &CecCommand
;
167 g_config
.callbacks
= &g_callbacks
;
169 g_config
.deviceTypes
.add(type
);
171 g_parser
= LibCecInitialise(&g_config
);
175 // init video on targets that need this
176 g_parser
->InitVideoStandalone();
179 cec_adapter devices
[10];
180 uint8_t iDevicesFound
= g_parser
->FindAdapters(devices
, 10, NULL
);
181 if (iDevicesFound
<= 0)
183 PrintToStdOut("autodetect FAILED");
184 UnloadLibCec(g_parser
);
189 strPort
= devices
[0].comm
;
192 PrintToStdOut("opening a connection to the CEC adapter...");
193 if (!g_parser
->Open(strPort
.c_str()))
195 PrintToStdOut("unable to open the device on port %s", strPort
.c_str());
196 UnloadLibCec(g_parser
);
200 g_parser
->GetCurrentConfiguration(&g_config
);
201 PrintToStdOut("CEC Parser created - libCEC version %s", g_parser
->ToString((cec_server_version
)g_config
.serverVersion
));
206 int8_t FindPhysicalAddressPortNumber(void)
208 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.");
215 int hdmiport
= atoi(input
.c_str());
216 return (hdmiport
< 1 || hdmiport
> 15) ? -1 : (int8_t)hdmiport
;
219 cec_logical_address
FindPhysicalAddressBaseDevice(void)
221 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.");
226 if (input
.empty() || (input
!= "1" && input
!= "2"))
228 PrintToStdOut("Exiting...");
229 return CECDEVICE_UNKNOWN
;
231 return (input
== "2") ?
232 CECDEVICE_AUDIOSYSTEM
:
236 uint16_t FindPhysicalAddress(void)
238 PrintToStdOut("=== Physical Address Configuration ===\n");
239 uint16_t iAddress(CEC_INVALID_PHYSICAL_ADDRESS
);
241 PrintToStdOut("Do you want to let libCEC try to autodetect the address (y/n)?");
245 if (input
== "y" || input
== "Y")
247 cec_logical_address baseDevice
= FindPhysicalAddressBaseDevice();
248 if (baseDevice
== CECDEVICE_UNKNOWN
)
251 int8_t iPortNumber
= FindPhysicalAddressPortNumber();
252 if (iPortNumber
== -1)
255 PrintToStdOut("Trying to detect the physical address...");
256 if (!g_parser
->SetHDMIPort(baseDevice
, iPortNumber
))
257 PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings.");
260 g_parser
->GetCurrentConfiguration(&g_config
);
261 iAddress
= g_parser
->GetDevicePhysicalAddress(g_config
.logicalAddresses
.primary
);
262 if (iAddress
== 0 || iAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
263 PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings.");
267 if (iAddress
== 0 || iAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
269 PrintToStdOut("Please enter the physical address (0001 - FFFE), followed by <enter>.");
274 if (sscanf(input
.c_str(), "%x", &iAddressTmp
) == 1)
276 if (iAddressTmp
<= CEC_PHYSICAL_ADDRESS_TV
|| iAddressTmp
> CEC_MAX_PHYSICAL_ADDRESS
)
277 iAddressTmp
= CEC_INVALID_PHYSICAL_ADDRESS
;
278 iAddress
= (uint16_t)iAddressTmp
;
284 g_parser
->SetPhysicalAddress(iAddress
);
285 g_parser
->SetActiveSource(g_config
.deviceTypes
[0]);
291 bool PowerOnTV(uint64_t iTimeout
= 60000)
293 cec_power_status
currentTvPower(CEC_POWER_STATUS_UNKNOWN
);
294 uint64_t iNow
= GetTimeMs();
295 uint64_t iTarget
= iNow
+ iTimeout
;
297 if (currentTvPower
!= CEC_POWER_STATUS_ON
)
299 currentTvPower
= g_parser
->GetDevicePowerStatus(CECDEVICE_TV
);
300 if (currentTvPower
!= CEC_POWER_STATUS_ON
)
302 PrintToStdOut("Sending 'power on' command to the TV\n=== Please wait ===");
303 g_parser
->PowerOnDevices(CECDEVICE_TV
);
304 while (iTarget
> iNow
)
306 g_responseEvent
.Wait((uint32_t)(iTarget
- iNow
));
307 if (g_lastCommand
== CEC_OPCODE_REQUEST_ACTIVE_SOURCE
)
314 currentTvPower
= g_parser
->GetDevicePowerStatus(CECDEVICE_TV
);
316 if (currentTvPower
!= CEC_POWER_STATUS_ON
)
317 PrintToStdOut("Failed to power on the TV, or the TV does not respond properly");
319 return currentTvPower
== CEC_POWER_STATUS_ON
;
322 void sighandler(int iSignal
)
324 PrintToStdOut("signal caught: %d - exiting", iSignal
);
327 UnloadLibCec(g_parser
);
332 int main (int UNUSED(argc
), char *UNUSED(argv
[]))
334 if (signal(SIGINT
, sighandler
) == SIG_ERR
)
336 PrintToStdOut("can't register sighandler");
342 PrintToStdOut("=== USB-CEC Adapter Configuration ===\n");
343 if (!OpenConnection())
349 bool bAddressOk(false);
352 uint16_t iAddress
= FindPhysicalAddress();
354 PrintToStdOut("Physical address: %4X", iAddress
);
355 PrintToStdOut("Is this correct (y/n)?");
359 bAddressOk
= (input
== "y" || input
== "Y");
362 g_parser
->GetCurrentConfiguration(&g_config
);
365 cec_menu_language lang
;
366 if (g_parser
->GetDeviceMenuLanguage(CECDEVICE_TV
, &lang
))
368 PrintToStdOut("TV menu language: %s", lang
.language
);
369 PrintToStdOut("Do you want the application to use the menu language of the TV (y/n)?");
373 g_config
.bUseTVMenuLanguage
= (input
== "y" || input
== "Y") ? 1 : 0;
377 PrintToStdOut("The TV did not respond properly to the menu language request.");
382 PrintToStdOut("Do you want to make the CEC adapter the active source when starting the application (y/n)?");
386 g_config
.bActivateSource
= (input
== "y" || input
== "Y") ? 1 : 0;
390 PrintToStdOut("Do you want to power on the TV when starting the application (y/n)?");
394 if (input
== "y" || input
== "Y")
395 g_config
.wakeDevices
.Set(CECDEVICE_TV
);
399 PrintToStdOut("Do you want to power off CEC devices when closing the application (y/n)?");
403 if (input
== "y" || input
== "Y")
404 g_config
.powerOffDevices
.Set(CECDEVICE_TV
);
408 PrintToStdOut("Do you want to power off CEC devices when the screensaver is activated (y/n)?");
412 g_config
.bPowerOffScreensaver
= (input
== "y" || input
== "Y") ? 1 : 0;
416 PrintToStdOut("Do you want to put the PC in standby when the TV is put in standby mode (y/n)?");
420 g_config
.bPowerOffOnStandby
= (input
== "y" || input
== "Y") ? 1 : 0;
424 PrintToStdOut("Do you want to send an inactive source message when stopping the application (y/n)?");
428 g_config
.bSendInactiveSource
= (input
== "y" || input
== "Y") ? 1 : 0;
431 PrintToStdOut("\n\n=== USB-CEC Adapter Configuration Summary ===");
432 PrintToStdOut("HDMI port number: %d", g_config
.iHDMIPort
);
433 PrintToStdOut("Connected to HDMI device: %X", (uint8_t)g_config
.baseDevice
);
434 PrintToStdOut("Physical address: %4X", g_config
.iPhysicalAddress
);
435 PrintToStdOut("Use the TV's language setting: %s", g_config
.bUseTVMenuLanguage
? "yes" : "no");
436 PrintToStdOut("Make the adapter the active source when starting XBMC: %s", g_config
.bActivateSource
? "yes" : "no");
437 PrintToStdOut("Power on the TV when starting XBMC: %s", g_config
.wakeDevices
.IsSet(CECDEVICE_BROADCAST
) ? "yes" : "no");
438 PrintToStdOut("Power off devices when stopping XBMC: %s", g_config
.powerOffDevices
.IsSet(CECDEVICE_BROADCAST
) ? "yes" : "no");
439 PrintToStdOut("Put devices in standby mode when activating screensaver: %s", g_config
.bPowerOffScreensaver
? "yes" : "no");
440 PrintToStdOut("Put this PC in standby mode when the TV is switched off: %s", g_config
.bPowerOffOnStandby
? "yes" : "no");
441 PrintToStdOut("Send an inactive source message when stopping XBMC: %s\n\n", g_config
.bSendInactiveSource
? "yes" : "no");
443 PrintToStdOut("Storing settings ...");
444 if (g_parser
->PersistConfiguration(&g_config
))
445 PrintToStdOut("Settings stored.");
447 PrintToStdOut("The settings could not be stored");
449 ofstream configOutput
;
450 configOutput
.open("usb_2548_1001.xml");
451 if (configOutput
.is_open())
453 CStdString strWakeDevices
;
454 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
455 if (g_config
.wakeDevices
[iPtr
])
456 strWakeDevices
.AppendFormat(" %d", iPtr
);
457 CStdString strStandbyDevices
;
458 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
459 if (g_config
.powerOffDevices
[iPtr
])
460 strStandbyDevices
.AppendFormat(" %d", iPtr
);
464 "\t<setting id=\"enabled\" value=\"1\" />\n" <<
465 "\t<setting id=\"activate_source\" value=\"" << (int)g_config
.bActivateSource
<< "\" />\n" <<
466 "\t<setting id=\"wake_devices\" value=\"" << strWakeDevices
.c_str() << "\" />\n" <<
467 "\t<setting id=\"standby_devices\" value=\"" << strStandbyDevices
.c_str() << "\" />\n" <<
468 "\t<setting id=\"cec_standby_screensaver\" value=\"" << (int)g_config
.bPowerOffScreensaver
<< "\" />\n" <<
469 "\t<setting id=\"standby_pc_on_tv_standby\" value=\"" << (int)g_config
.bPowerOffOnStandby
<< "\" />\n" <<
470 "\t<setting id=\"use_tv_menu_language\" value=\"" << (int)g_config
.bUseTVMenuLanguage
<< "\" />\n" <<
471 "\t<setting id=\"physical_address\" value=\"" << hex
<< g_config
.iPhysicalAddress
<< "\" />\n" <<
472 "\t<setting id=\"cec_hdmi_port\" value=\"" << g_config
.iHDMIPort
<< "\" />\n" <<
473 "\t<setting id=\"connected_device\" value=\"" << (int)g_config
.baseDevice
<< "\" />\n" <<
474 "\t<setting id=\"port\" value=\"\" />\n" <<
475 "\t<setting id=\"send_inactive_source\" value=\"" << (int)g_config
.bSendInactiveSource
<< "\" />\n" <<
477 configOutput
.close();
479 PrintToStdOut("The configuration has been stored in 'usb_2548_1001.xml'. Copy this file to ~/.userdata/peripheral_data to use it in XBMC");
482 g_parser
->StandbyDevices();
484 UnloadLibCec(g_parser
);
486 PrintToStdOut("Press enter to close this wizard.");