From 21bebbd581183d513cd71d35cf804ea992ac2f0b Mon Sep 17 00:00:00 2001 From: Tim Kourt Date: Thu, 23 Mar 2017 16:32:10 -0700 Subject: [PATCH] client: Introduce display files The purpose of these files is to encapsulate all of the display related functionality --- Makefile.am | 1 + client/display.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ client/display.h | 36 ++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 client/display.c create mode 100644 client/display.h diff --git a/Makefile.am b/Makefile.am index f2ada88d..a4bca090 100644 --- a/Makefile.am +++ b/Makefile.am @@ -87,6 +87,7 @@ dist_sysconf_DATA = src/iwd.conf client_iwctl_SOURCES = client/main.c \ readline/readline.h readline/history.h \ + client/display.h client/display.c \ client/dbus-proxy.h client/dbus-proxy.c client_iwctl_LDADD = ell/libell-internal.la -lreadline diff --git a/client/display.c b/client/display.c new file mode 100644 index 00000000..563e40b4 --- /dev/null +++ b/client/display.c @@ -0,0 +1,98 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2017 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include "display.h" + +#define IWD_PROMPT COLOR_GREEN "[iwd]" COLOR_OFF "# " +#define LINE_LEN 81 + +static struct l_queue *display_types; +static struct l_io *io; +static char dashed_line[LINE_LEN]; +static char empty_line[LINE_LEN]; + +static void readline_callback(char *prompt) +{ + l_free(prompt); +} + +static bool read_handler(struct l_io *io, void *user_data) +{ + rl_callback_read_char(); + + return true; +} + +static void disconnect_callback(struct l_io *io, void *user_data) +{ + l_main_exit(); +} + +void display_enable_cmd_prompt(void) +{ + io = l_io_new(fileno(stdin)); + + l_io_set_read_handler(io, read_handler, NULL, NULL); + l_io_set_disconnect_handler(io, disconnect_callback, NULL, NULL); + + rl_set_prompt(IWD_PROMPT); +} + +void display_disable_cmd_prompt(void) +{ + rl_set_prompt(""); + rl_crlf(); +} + +void display_init(void) +{ + memset(&dashed_line, '-', sizeof(dashed_line) - 1); + memset(&empty_line, ' ', sizeof(empty_line) - 1); + + display_types = l_queue_new(); + + setlinebuf(stdout); + + rl_erase_empty_line = 1; + rl_callback_handler_install("Waiting for IWD to appear...", + readline_callback); + rl_redisplay(); +} + +void display_exit(void) +{ + rl_callback_handler_remove(); + + l_io_destroy(io); + + l_queue_destroy(display_types, NULL); + display_types = NULL; +} diff --git a/client/display.h b/client/display.h new file mode 100644 index 00000000..7619baad --- /dev/null +++ b/client/display.h @@ -0,0 +1,36 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2017 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#define COLOR_BOLDGRAY "\x1B[1;30m" +#define COLOR_GRAY "\x1b[37m" +#define COLOR_GREEN "\x1b[32m" +#define COLOR_RED "\x1B[0;91m" +#define COLOR_OFF "\x1B[0m" +#define CHECK "\u2714" +#define CLEAR_SCREEN "\033[2J" +#define MARGIN " " + +void display_enable_cmd_prompt(void); +void display_disable_cmd_prompt(void); + +void display_init(void); +void display_exit(void);