aboutsummaryrefslogtreecommitdiffstats
path: root/sway/main.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-08-20 08:37:09 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-08-20 08:37:24 -0400
commit579fe70ed92ce65d5a761ebdbb6c458b5f919687 (patch)
treea56ab8990f003d4408f1ef71d35c425a3545aec2 /sway/main.c
parentMerge branch 'master' of https://github.com/SirCmpwn/sway (diff)
downloadsway-579fe70ed92ce65d5a761ebdbb6c458b5f919687.tar.gz
sway-579fe70ed92ce65d5a761ebdbb6c458b5f919687.tar.zst
sway-579fe70ed92ce65d5a761ebdbb6c458b5f919687.zip
Add command line parsing
Closes #6
Diffstat (limited to 'sway/main.c')
-rw-r--r--sway/main.c78
1 files changed, 76 insertions, 2 deletions
diff --git a/sway/main.c b/sway/main.c
index 2db4604c..4a7e13c1 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -4,6 +4,7 @@
4#include <wlc/wlc.h> 4#include <wlc/wlc.h>
5#include <sys/wait.h> 5#include <sys/wait.h>
6#include <signal.h> 6#include <signal.h>
7#include <getopt.h>
7#include "layout.h" 8#include "layout.h"
8#include "config.h" 9#include "config.h"
9#include "log.h" 10#include "log.h"
@@ -12,23 +13,96 @@
12static void sigchld_handle(int signal); 13static void sigchld_handle(int signal);
13 14
14int main(int argc, char **argv) { 15int main(int argc, char **argv) {
16 static int verbose = 0, debug = 0, validate = 0;
17
18 static struct option long_options[] = {
19 {"config", required_argument, NULL, 'c'},
20 {"validate", no_argument, &validate, 1},
21 {"debug", no_argument, &debug, 1},
22 {"version", no_argument, NULL, 'v'},
23 {"verbose", no_argument, &verbose, 1},
24 {"get-socketpath", no_argument, NULL, 'p'},
25 };
26
15 /* Signal handling */ 27 /* Signal handling */
16 signal(SIGCHLD, sigchld_handle); 28 signal(SIGCHLD, sigchld_handle);
17 29
18 setenv("WLC_DIM", "0", 0); 30 setenv("WLC_DIM", "0", 0);
31
32 FILE *devnull = fopen("/dev/null", "w");
33 if (devnull) {
34 // NOTE: Does not work, see wlc issue #54
35 wlc_set_log_file(devnull);
36 }
37
19 /* Changing code earlier than this point requires detailed review */ 38 /* Changing code earlier than this point requires detailed review */
20 if (!wlc_init(&interface, argc, argv)) { 39 if (!wlc_init(&interface, argc, argv)) {
21 return 1; 40 return 1;
22 } 41 }
23 42
24 init_log(L_DEBUG); // TODO: Control this with command line arg 43 char *config_path = NULL;
44
45 int c;
46 while (1) {
47 int option_index = 0;
48 c = getopt_long(argc, argv, "CdvVpc:", long_options, &option_index);
49 if (c == -1) {
50 break;
51 }
52 switch (c) {
53 case 0: // Flag
54 break;
55 case 'c': // config
56 config_path = strdup(optarg);
57 break;
58 case 'C': // validate
59 validate = 1;
60 break;
61 case 'd': // debug
62 debug = 1;
63 break;
64 case 'v': // version
65 // todo
66 exit(0);
67 break;
68 case 'V': // verbose
69 verbose = 1;
70 break;
71 case 'p': // --get-socketpath
72 // TODO
73 break;
74 }
75 }
76
77 if (debug) {
78 init_log(L_DEBUG);
79 wlc_set_log_file(stderr);
80 fclose(devnull);
81 devnull = NULL;
82 } else if (verbose || validate) {
83 init_log(L_INFO);
84 } else {
85 init_log(L_ERROR);
86 }
87
88 if (validate) {
89 bool valid = load_config(config_path);
90 return valid ? 0 : 1;
91 }
92
25 init_layout(); 93 init_layout();
26 94
27 if (!load_config()) { 95 if (!load_config(config_path)) {
28 sway_log(L_ERROR, "Error(s) loading config!"); 96 sway_log(L_ERROR, "Error(s) loading config!");
29 } 97 }
98 if (config_path) {
99 free(config_path);
100 }
30 101
31 wlc_run(); 102 wlc_run();
103 if (devnull) {
104 fclose(devnull);
105 }
32 106
33 return 0; 107 return 0;
34} 108}