From 62cdffbb4d3e93d6785f3054f7fa81b7cc8b6de7 Mon Sep 17 00:00:00 2001 From: nirav Date: Sun, 21 Oct 2018 18:24:26 +0530 Subject: Added command line option parsing, info bar is now visible by default --- src/main.c | 36 ++++-------------------------------- src/option.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/option.h | 14 ++++++++++++++ src/window.c | 5 ++++- 4 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 src/option.c create mode 100644 src/option.h (limited to 'src') diff --git a/src/main.c b/src/main.c index 21dd14e..9b67bf2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,53 +1,25 @@ #include #include #include -#include #include "file.h" #include "image.h" +#include "option.h" #include "window.h" -void print_usage() -{ - printf("qwe [filename]\n"); -} - -void print_version() -{ - printf("0.01\n"); -} - int main(int argc, char **argv) { gtk_init(&argc, &argv); - int opt; - while ((opt = getopt(argc, argv, "hv")) != -1) { - switch (opt) { - case '?': - print_usage(); - exit(EXIT_FAILURE); - case 'h': - print_usage(); - exit(EXIT_SUCCESS); - case 'v': - print_version(); - exit(EXIT_SUCCESS); - } - } - - if (optind >= argc) { - print_usage(); - exit(EXIT_FAILURE); - } + parse_options(argc, argv); // load the specified image - if (load_image(argv[optind])) { + if (load_image(options->file_name)) { exit(EXIT_FAILURE); } // scan other supported image file in the same dir - scan(argv[optind]); + scan(options->file_name); create_main_window(); gtk_main(); diff --git a/src/option.c b/src/option.c new file mode 100644 index 0000000..66a99f0 --- /dev/null +++ b/src/option.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "option.h" + +static const char version[] = "qwe version 0.01"; +static const char usage[] = + "Usage: qwe [options...] \n" + "\n" + " -i Hide info bar by default.\n" + " -h Show help message and quit.\n" + " -v Show the version number and quit.\n"; + +void print_usage() +{ + printf("%s\n", usage); +} + +void print_version() +{ + printf("%s\n", version); +} + +struct option _options; +const struct option *options = (const struct option *)&_options; + +void parse_options(int argc, char **argv) +{ + // default options + _options.fullscreen = false; + _options.show_info = true; + + // override options from commandline parameters + int opt; + while ((opt = getopt(argc, argv, "hvi")) != -1) { + switch (opt) { + case '?': + print_usage(); + exit(EXIT_FAILURE); + case 'h': + print_usage(); + exit(EXIT_SUCCESS); + case 'v': + print_version(); + exit(EXIT_SUCCESS); + case 'i': + _options.show_info = false; + } + } + + if (optind >= argc) { + print_usage(); + exit(EXIT_FAILURE); + } + + _options.file_name = argv[optind]; +} diff --git a/src/option.h b/src/option.h new file mode 100644 index 0000000..33b8335 --- /dev/null +++ b/src/option.h @@ -0,0 +1,14 @@ +#ifndef __OPTION_H +#define __OPTION_H + +#include + +struct option { + char *file_name; + bool fullscreen; + bool show_info; +}; +extern const struct option *options; +void parse_options(int argc, char **argv); + +#endif diff --git a/src/window.c b/src/window.c index 899d526..b50c6bc 100644 --- a/src/window.c +++ b/src/window.c @@ -3,6 +3,7 @@ #include "file.h" #include "image.h" #include "input.h" +#include "option.h" #include "window.h" GtkWidget *window; @@ -117,7 +118,9 @@ void create_main_window() gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(overlay)); gtk_widget_show_all(GTK_WIDGET(window)); - gtk_widget_hide(GTK_WIDGET(info_panel)); + if (!options->show_info) { + gtk_widget_hide(GTK_WIDGET(info_panel)); + } load_css(); } -- cgit v1.2.3