aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornirav <nirav@teisuu.com>2018-10-21 18:24:26 +0530
committerDandelion <nirav@teisuu.com>2018-10-21 18:24:26 +0530
commit62cdffbb4d3e93d6785f3054f7fa81b7cc8b6de7 (patch)
tree821e20b2f978890f7b9e6d6a99209a85669a842e
parentf8f3159f9b98b024043bd8de2c5e15dd2adf03ca (diff)
downloadim-62cdffbb4d3e93d6785f3054f7fa81b7cc8b6de7.tar.gz
im-62cdffbb4d3e93d6785f3054f7fa81b7cc8b6de7.zip
Added command line option parsing, info bar is now visible by default
-rw-r--r--Makefile1
-rw-r--r--src/main.c36
-rw-r--r--src/option.c58
-rw-r--r--src/option.h14
-rw-r--r--src/window.c5
5 files changed, 81 insertions, 33 deletions
diff --git a/Makefile b/Makefile
index 09b3341..7970756 100644
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,7 @@ OBJECTS= $(OUTDIR)/main.o \
$(OUTDIR)/image.o \
$(OUTDIR)/input.o \
$(OUTDIR)/window.o \
+ $(OUTDIR)/option.o \
$(OUTDIR)/resources.o
all: qwe
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 <gtk-3.0/gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#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 <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "option.h"
+
+static const char version[] = "qwe version 0.01";
+static const char usage[] =
+ "Usage: qwe [options...] <file>\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 <stdbool.h>
+
+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();
}