void cec_init() { libcecc_reset_configuration(&g_config); g_config.clientVersion = LIBCEC_VERSION_CURRENT; g_config.bActivateSource = 0; g_callbacks.CBCecKeyPress = &on_cec_keypress; g_config.callbacks = &g_callbacks; snprintf(g_config.strDeviceName, sizeof(g_config.strDeviceName), "Moonlight"); g_config.callbacks = &g_callbacks; g_config.deviceTypes.types[0] = CEC_DEVICE_TYPE_PLAYBACK_DEVICE; if (libcecc_initialise(&g_config, &g_iface, NULL) != 1) { fprintf(stderr, "Failed to initialize libcec interface\n"); fflush(stderr); return; } g_iface.init_video_standalone(g_iface.connection); cec_adapter devices[10]; int8_t iDevicesFound = g_iface.find_adapters(g_iface.connection, devices, sizeof(devices) / sizeof(devices), NULL); if (iDevicesFound <= 0) { fprintf(stderr, "No CEC devices found\n"); fflush(stderr); libcecc_destroy(&g_iface); return; } strcpy(g_strPort, devices[0].comm); if (!g_iface.open(g_iface.connection, g_strPort, 5000)) { fprintf(stderr, "Unable to open the device on port %s\n", g_strPort); fflush(stderr); libcecc_destroy(&g_iface); return; } g_iface.set_active_source(g_iface.connection, g_config.deviceTypes.types[0]); }
static int cec_process_command_line_arguments(int argc, char *argv[]) { int bReturn = 1; int iArgPtr = 1; while (iArgPtr < argc && bReturn) { if (argc >= iArgPtr + 1) { if (!strcmp(argv[iArgPtr], "-d") || !strcmp(argv[iArgPtr], "--log-level")) { if (argc >= iArgPtr + 2) { int iNewLevel = atoi(argv[iArgPtr + 1]); if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL) { g_cecLogLevel = iNewLevel; if (!g_bSingleCommand) printf("log level set to %s\n", argv[iArgPtr + 1]); } else { printf("== skipped log-level parameter: invalid level %s' ==\n", argv[iArgPtr + 1]); } iArgPtr += 2; } else { printf("== skipped log-level parameter: no level given ==\n"); ++iArgPtr; } } else if (!strcmp(argv[iArgPtr], "-t") || !strcmp(argv[iArgPtr], "--type")) { if (argc >= iArgPtr + 2) { if (!strcmp(argv[iArgPtr + 1], "p")) { if (!g_bSingleCommand) printf("== using device type 'playback device'\n"); g_config.deviceTypes.types[0] = CEC_DEVICE_TYPE_PLAYBACK_DEVICE; } else if (!strcmp(argv[iArgPtr + 1], "r")) { if (!g_bSingleCommand) printf("== using device type 'recording device'\n"); g_config.deviceTypes.types[0] = CEC_DEVICE_TYPE_RECORDING_DEVICE; } else if (!strcmp(argv[iArgPtr + 1], "t")) { if (!g_bSingleCommand) printf("== using device type 'tuner'\n"); g_config.deviceTypes.types[0] = CEC_DEVICE_TYPE_TUNER; } else if (!strcmp(argv[iArgPtr + 1], "a")) { if (!g_bSingleCommand) printf("== using device type 'audio system'\n"); g_config.deviceTypes.types[0] = CEC_DEVICE_TYPE_AUDIO_SYSTEM; } else if (!strcmp(argv[iArgPtr + 1], "x")) { if (!g_bSingleCommand) printf("== using device type 'tv'\n"); g_config.deviceTypes.types[0] = CEC_DEVICE_TYPE_TV; } else { printf("== skipped invalid device type %s'\n", argv[iArgPtr + 1]); } ++iArgPtr; } ++iArgPtr; } else if (!strcmp(argv[iArgPtr], "--info") || !strcmp(argv[iArgPtr], "-i")) { if (g_cecLogLevel == -1) g_cecLogLevel = CEC_LOG_WARNING + CEC_LOG_ERROR; if (libcecc_initialise(&g_config, &g_iface, NULL) == 1) { char verbuf[10]; g_iface.version_to_string(g_config.serverVersion, verbuf, sizeof(verbuf)); printf("libCEC version: %s %s\n", verbuf, g_iface.get_lib_info(g_iface.connection)); libcecc_destroy(&g_iface); } bReturn = 0; } else if (!strcmp(argv[iArgPtr], "--list-devices") || !strcmp(argv[iArgPtr], "-l")) { if (g_cecLogLevel == -1) g_cecLogLevel = CEC_LOG_WARNING + CEC_LOG_ERROR; if (libcecc_initialise(&g_config, &g_iface, NULL) == 1) { cec_list_devices(); libcecc_destroy(&g_iface); } bReturn = 0; } else if (!strcmp(argv[iArgPtr], "--single-command") || !strcmp(argv[iArgPtr], "-s")) { g_bSingleCommand = 1; ++iArgPtr; } else if (!strcmp(argv[iArgPtr], "--help") || !strcmp(argv[iArgPtr], "-h")) { if (g_cecLogLevel == -1) g_cecLogLevel = CEC_LOG_WARNING + CEC_LOG_ERROR; // TODO ShowHelpCommandLine(argv[0]); return 0; } else if (!strcmp(argv[iArgPtr], "-b") || !strcmp(argv[iArgPtr], "--base")) { if (argc >= iArgPtr + 2) { g_config.baseDevice = (cec_logical_address)atoi(argv[iArgPtr + 1]); printf("using base device '%d'\n", (int)g_config.baseDevice); ++iArgPtr; } ++iArgPtr; } else if (!strcmp(argv[iArgPtr], "-p") || !strcmp(argv[iArgPtr], "--port")) { if (argc >= iArgPtr + 2) { uint8_t hdmiport = (int8_t)atoi(argv[iArgPtr + 1]); if (hdmiport < 1) hdmiport = 1; if (hdmiport > 15) hdmiport = 15; g_config.iHDMIPort = hdmiport; printf("using HDMI port '%d'\n", (int)g_config.iHDMIPort); ++iArgPtr; } ++iArgPtr; } else if (!strcmp(argv[iArgPtr], "-r") || !strcmp(argv[iArgPtr], "--rom")) { printf("using settings from EEPROM\n"); g_config.bGetSettingsFromROM = 1; ++iArgPtr; } else if (!strcmp(argv[iArgPtr], "-o") || !strcmp(argv[iArgPtr], "--osd-name")) { if (argc >= iArgPtr + 2) { snprintf(g_config.strDeviceName, 13, "%s", argv[iArgPtr + 1]); printf("using osd name '%s'\n", g_config.strDeviceName); ++iArgPtr; } ++iArgPtr; } else if (!strcmp(argv[iArgPtr], "-m") || !strcmp(argv[iArgPtr], "--monitor")) { printf("starting a monitor-only client. use 'mon 0' to switch to normal mode\n"); g_config.bMonitorOnly = 1; ++iArgPtr; } else { strcpy(g_strPort, argv[iArgPtr++]); } } } return bReturn; }