From 4344bc339fb8a34027276ead75a112e5546e4e10 Mon Sep 17 00:00:00 2001 From: Alberto Fanjul Date: Mon, 13 Jun 2022 08:41:52 +0200 Subject: [PATCH] UIBC ctl interactive mode --- src/ctl/sinkctl.c | 46 ++--- src/uibc/meson.build | 6 +- src/uibc/{miracle-uibcctl.c => uibcctl.c} | 203 ++++++++++++++++++++-- src/uibc/{miracle-uibcctl.h => uibcctl.h} | 0 4 files changed, 212 insertions(+), 43 deletions(-) rename src/uibc/{miracle-uibcctl.c => uibcctl.c} (80%) rename src/uibc/{miracle-uibcctl.h => uibcctl.h} (100%) diff --git a/src/ctl/sinkctl.c b/src/ctl/sinkctl.c index d53ff51b..54a71d8f 100644 --- a/src/ctl/sinkctl.c +++ b/src/ctl/sinkctl.c @@ -371,14 +371,14 @@ static int sink_timeout_fn(sd_event_source *s, uint64_t usec, void *data) } static const struct cli_cmd cli_cmds[] = { - { "list", NULL, CLI_M, CLI_LESS, 0, cmd_list, "List all objects" }, - { "show", "", CLI_M, CLI_LESS, 1, cmd_show, "Show detailed object information" }, - { "run", "", CLI_M, CLI_EQUAL, 1, cmd_run, "Run sink on given link" }, - { "bind", "", CLI_M, CLI_EQUAL, 1, cmd_bind, "Like 'run' but bind the link name to run when it is hotplugged" }, + { "list", NULL, CLI_M, CLI_LESS, 0, cmd_list, "List all objects" }, + { "show", "", CLI_M, CLI_LESS, 1, cmd_show, "Show detailed object information" }, + { "run", "", CLI_M, CLI_EQUAL, 1, cmd_run, "Run sink on given link" }, + { "bind", "", CLI_M, CLI_EQUAL, 1, cmd_bind, "Like 'run' but bind the link name to run when it is hotplugged" }, { "set-managed", " ", CLI_M, CLI_EQUAL, 2, cmd_set_managed, "Manage or unmnage a link" }, - { "quit", NULL, CLI_Y, CLI_MORE, 0, cmd_quit, "Quit program" }, - { "exit", NULL, CLI_Y, CLI_MORE, 0, cmd_quit, NULL }, - { "help", NULL, CLI_M, CLI_MORE, 0, NULL, "Print help" }, + { "quit", NULL, CLI_Y, CLI_MORE, 0, cmd_quit, "Quit program" }, + { "exit", NULL, CLI_Y, CLI_MORE, 0, cmd_quit, NULL }, + { "help", NULL, CLI_M, CLI_MORE, 0, NULL, "Print help" }, { }, }; @@ -720,8 +720,8 @@ void cli_fn_help() " --gst-debug [cat:]lvl[,...] List of categories an level of debug\n" " --audio <0/1> Enable audio support (default %d)\n" " --scale WxH Scale to resolution\n" - " -p --port Port for rtsp (default %d)\n" - " --uibc Enables UIBC\n" + " -p --port Port for rtsp (default %d)\n" + " --uibc Enables UIBC\n" " -e --external-player Configure player to use\n" " --res Supported resolutions masks (CEA, VESA, HH)\n" " default CEA %08X\n" @@ -810,41 +810,41 @@ static int parse_argv(int argc, char *argv[]) ARG_RES, ARG_HELP_RES, ARG_UIBC, - ARG_HELP_COMMANDS, + ARG_HELP_COMMANDS, }; static const struct option options[] = { - { "help", no_argument, NULL, 'h' }, + { "help", no_argument, NULL, 'h' }, { "help-commands", no_argument, NULL, ARG_HELP_COMMANDS }, - { "version", no_argument, NULL, ARG_VERSION }, - { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, + { "version", no_argument, NULL, ARG_VERSION }, + { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, { "log-journal-level", required_argument, NULL, ARG_JOURNAL_LEVEL }, - { "gst-debug", required_argument, NULL, ARG_GST_DEBUG }, - { "audio", required_argument, NULL, ARG_AUDIO }, - { "scale", required_argument, NULL, ARG_SCALE }, - { "res", required_argument, NULL, ARG_RES }, - { "help-res", no_argument, NULL, ARG_HELP_RES }, + { "gst-debug", required_argument, NULL, ARG_GST_DEBUG }, + { "audio", required_argument, NULL, ARG_AUDIO }, + { "scale", required_argument, NULL, ARG_SCALE }, + { "res", required_argument, NULL, ARG_RES }, + { "help-res", no_argument, NULL, ARG_HELP_RES }, { "port", required_argument, NULL, 'p' }, { "uibc", no_argument, NULL, ARG_UIBC }, - { "external-player", required_argument, NULL, 'e' }, + { "external-player", required_argument, NULL, 'e' }, {} }; int c; uibc_option = false; uibc_enabled = false; - external_player = false; + external_player = false; rstp_port = DEFAULT_RSTP_PORT; while ((c = getopt_long(argc, argv, "he:p:", options, NULL)) >= 0) { switch (c) { case 'h': - cli_fn_help(); - return 0; + cli_fn_help(); + return 0; case ARG_HELP_COMMANDS: return cli_help(cli_cmds, 20); case ARG_HELP_RES: wfd_print_resolutions(""); - return 0; + return 0; case ARG_VERSION: puts(PACKAGE_STRING); return 0; diff --git a/src/uibc/meson.build b/src/uibc/meson.build index 17a06dbd..b2e97fdc 100644 --- a/src/uibc/meson.build +++ b/src/uibc/meson.build @@ -1,5 +1,7 @@ +inc = include_directories('../..') m = c_compiler.find_library('m', required: false) -executable('miracle-uibcctl', 'miracle-uibcctl.h', 'miracle-uibcctl.c', +executable('miracle-uibcctl', 'uibcctl.h', 'uibcctl.c', install: true, - dependencies: [m, libmiracle_shared_dep] + include_directories: inc, + dependencies: [m, glib2, libmiracle_shared_dep] ) diff --git a/src/uibc/miracle-uibcctl.c b/src/uibc/uibcctl.c similarity index 80% rename from src/uibc/miracle-uibcctl.c rename to src/uibc/uibcctl.c index 3aee587b..cfe699fe 100644 --- a/src/uibc/miracle-uibcctl.c +++ b/src/uibc/uibcctl.c @@ -1,34 +1,197 @@ -#include "miracle-uibcctl.h" +#include "uibcctl.h" +#include +#include +#include +#include "config.h" +#include "util.h" +//#include "ctl.h" + +int portno = -1; +gchar* host; +bool is_daemon = true; + +void usage(gchar* prgname) { + fprintf(stderr, "Usage:\n"); + fprintf(stderr, " %s \n", prgname); + fprintf(stderr, "or define host and port on ini file\n"); +} -int main(int argc, char *argv[]) { - //TODO: Add miracle TUI interface - //TODO: Add parsearg - //--daemon (read stdin) +/* + * cmd: quit/exit + */ + +//static int cmd_quit(char **args, unsigned int n) +//{ +// cli_exit(); +// return 0; +//} +// +//static const struct cli_cmd cli_cmds[] = { +// { "info", NULL, CLI_M, CLI_LESS, 1, cmd_show, "Show detailed information" }, +// { "quit", NULL, CLI_Y, CLI_MORE, 0, cmd_quit, "Quit program" }, +// { "exit", NULL, CLI_Y, CLI_MORE, 0, cmd_quit, NULL }, +// { "event", NULL, CLI_Y, CLI_MORE, 0, cmd_event, "launch an event: ,,,," }, +// { "help", NULL, CLI_M, CLI_MORE, 0, NULL, "Print help" }, +// { }, +//}; + +void cli_fn_help() +{ + /* + * 80-char barrier: + * 01234567890123456789012345678901234567890123456789012345678901234567890123456789 + */ + fprintf(stderr, "%s [OPTIONS...] \n\n" + "Manage the User Input Back Channel.\n" + " -h --help Show this help\n" + " --help-commands Show available commands\n" + " --version Show package version\n" + " --daemon Run in background\n" + " --log-level Maximum level for log messages\n" + " --host Defines the host\n" + " -p --port Defines the Port\n" + "\n" + , program_invocation_short_name); + /* + * 80-char barrier: + * 01234567890123456789012345678901234567890123456789012345678901234567890123456789 + */ +} - int portno; +static int parse_argv(int argc, char *argv[]) +{ + enum { + ARG_VERSION = 0x100, + ARG_LOG_LEVEL, + ARG_DAEMON, + ARG_HOST, + ARG_HELP_COMMANDS, + }; + static const struct option options[] = { + { "help", no_argument, NULL, 'h' }, + { "help-commands", no_argument, NULL, ARG_HELP_COMMANDS }, + { "version", no_argument, NULL, ARG_VERSION }, + { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, + { "daemon", no_argument, NULL, ARG_DAEMON }, + { "host", required_argument, NULL, ARG_HOST }, + { "port", required_argument, NULL, 'p' }, + {} + }; + int c; + + while ((c = getopt_long(argc, argv, "he:p:", options, NULL)) >= 0) { + switch (c) { + case 'h': + cli_fn_help(); + return 0; + case ARG_HELP_COMMANDS: +// return cli_help(cli_cmds, 20); + cli_fn_help(); + return 0; + case ARG_VERSION: + puts(PACKAGE_STRING); + return 0; + case ARG_LOG_LEVEL: + log_max_sev = log_parse_arg(optarg); + break; + case ARG_HOST: + host = optarg; + break; + case ARG_DAEMON: + is_daemon = true; + break; + case 'p': + portno = atoi(optarg); + break; + case '?': + return -EINVAL; + } + } + + return 1; +} +int main(int argc, char *argv[]) { + bool free_host = false; struct hostent *server; int sockfd; struct sockaddr_in serv_addr; char buffer[256]; int r; + setlocale(LC_ALL, ""); + + GKeyFile* gkf = load_ini_file(); + log_max_sev = LOG_INFO; - if (argc < 3) { - fprintf(stderr, "Usage:\n"); - fprintf(stderr, " %s \n", argv[0]); + gchar** autocmds_free = NULL; + if (gkf) { + gchar* log_level; + log_level = g_key_file_get_string (gkf, "uibcctl", "log-level", NULL); + if (log_level) { + log_max_sev = log_parse_arg(log_level); + g_free(log_level); + } + if (g_key_file_has_key (gkf, "uibcctl", "daemon", NULL)) { + is_daemon = g_key_file_get_boolean (gkf, "uibcctl", "daemon", NULL); + } + host = g_key_file_get_string (gkf, "uibcctl", "host", NULL); + if (host) { + host = log_parse_arg(log_level); + free_host = true; + } + if (g_key_file_has_key (gkf, "uibcctl", "port", NULL)) { + portno = g_key_file_get_uint64 (gkf, "uibcctl", "port", NULL); + } + } + + r = parse_argv(argc, argv); + if (r < 0) return EXIT_FAILURE; + if (!r) + return EXIT_SUCCESS; + + gchar* prgname = argv[0]; + gboolean has_port = portno != -1; + gboolean has_host = host != NULL; + if (has_host) { + if (has_port) { + //Everything defined + } else { + if (argc < 1) { + usage(prgname); + return EXIT_FAILURE; + } else { + portno = atoi(argv[1]); + } + } + } else { + if (has_port) { + if (argc < 1) { + usage(prgname); + return EXIT_FAILURE; + } else { + host = argv[1]; + } + } else { + if (argc < 2) { + usage(prgname); + return EXIT_FAILURE; + } else { + host = argv[1]; + portno = atoi(argv[1]); + } + } } - server = gethostbyname(argv[1]); - portno = atoi(argv[2]); + server = gethostbyname(host); - log_info("server %s port %d", argv[1], portno); + log_info("server %s port %d", server, portno); if (server == NULL) { - fprintf(stderr,"ERROR, no such host\n"); - exit(0); + perror("no such host"); + return EXIT_FAILURE; } sockfd = socket(AF_INET, SOCK_STREAM, 0); @@ -48,9 +211,10 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - bool daemon = true; + //TODO: Add miracle TUI interface + while(1) { - if (!daemon) { + if (!is_daemon) { printf("enter event ,,,,: "); } bzero(buffer, 256); @@ -58,7 +222,7 @@ int main(int argc, char *argv[]) { if (buffer == NULL) { break; } - if (!daemon) { + if (!is_daemon) { printf("input: %s", buffer); } char type = buffer[0]; @@ -68,7 +232,7 @@ int main(int argc, char *argv[]) { } else if (type == '3' || type == '4') { uibcmessage = buildUibcMessage(GENERIC_KEY_DOWN, buffer, 1, 1); } else { - if (!daemon) { + if (!is_daemon) { printf("unknow event type: %s", buffer); } continue; @@ -80,6 +244,9 @@ int main(int argc, char *argv[]) { } } + if (free_host) + g_free(host); + close(sockfd); return EXIT_SUCCESS; } diff --git a/src/uibc/miracle-uibcctl.h b/src/uibc/uibcctl.h similarity index 100% rename from src/uibc/miracle-uibcctl.h rename to src/uibc/uibcctl.h