]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
Put usage-message in own function
[i3/i3] / i3bar / src / main.c
1 /*
2  * i3bar - an xcb-based status- and ws-bar for i3
3  *
4  * © 2010 Axel Wagner and contributors
5  *
6  * See file LICNSE for license information
7  *
8  */
9 #include <stdio.h>
10 #include <i3/ipc.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <ev.h>
16 #include <getopt.h>
17 #include <glob.h>
18
19 #include "common.h"
20
21 char *i3_default_sock_path = "~/.i3/ipc.sock";
22
23 char *expand_path(char *path) {
24     static glob_t globbuf;
25     if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
26         printf("glob() failed");
27         exit(EXIT_FAILURE);
28     }
29     char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
30     if (result == NULL) {
31         printf("malloc() failed");
32         exit(EXIT_FAILURE);
33     }
34     globfree(&globbuf);
35     return result;
36 }
37
38 void print_usage(char *elf_name) {
39     printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-h]\n", elf_name);
40     printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
41     printf("-c <command>\tExecute <command> to get stdin\n");
42     printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
43     printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
44     printf("\t\tand a SIGCONT on unhiding of the bars\n");
45     printf("-f <font>\tUse X-Core-Font <font> for display\n");
46     printf("-h\t\tDisplay this help-message and exit\n");
47 }
48
49 int main(int argc, char **argv) {
50     int opt;
51     int option_index = 0;
52     char *socket_path = NULL;
53     char *command = NULL;
54     char *fontname = NULL;
55
56     static struct option long_opt[] = {
57         { "socket",  required_argument, 0, 's' },
58         { "command", required_argument, 0, 'c' },
59         { "font",    required_argument, 0, 'f' },
60         { "help",    no_argument,       0, 'h' },
61         { "version", no_argument,       0, 'v' },
62         { NULL,      0,                 0, 0}
63     };
64
65     while ((opt = getopt_long(argc, argv, "s:c:f:hv", long_opt, &option_index)) != -1) {
66         switch (opt) {
67             case 's':
68                 socket_path = expand_path(optarg);
69                 break;
70             case 'c':
71                 command = strdup(optarg);
72                 break;
73             case 'f':
74                 fontname = strdup(optarg);
75                 break;
76             case 'v':
77                 printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
78                 exit(EXIT_SUCCESS);
79                 break;
80             default:
81                 print_usage(argv[0]);
82                 exit(EXIT_SUCCESS);
83                 break;
84         }
85     }
86
87     if (fontname == NULL) {
88         fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
89     }
90
91     if (socket_path == NULL) {
92         printf("No Socket Path Specified, default to %s\n", i3_default_sock_path);
93         socket_path = expand_path(i3_default_sock_path);
94     }
95
96     main_loop = ev_default_loop(0);
97
98     init_xcb(fontname);
99     init_outputs();
100     init_connection(socket_path);
101
102     FREE(socket_path);
103
104     subscribe_events();
105
106     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
107     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
108
109     start_child(command);
110
111     ev_loop(main_loop, 0);
112
113     kill_child();
114
115     FREE(statusline);
116
117     clean_xcb();
118     ev_default_destroy();
119
120     free_workspaces();
121     FREE_SLIST(outputs, i3_output);
122
123     return 0;
124 }