]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
118c29478c84363c49dd0eeea9d4714a1c069e10
[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 /*
22  * Glob path, i.e. expand ~
23  *
24  */
25 char *expand_path(char *path) {
26     static glob_t globbuf;
27     if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
28         printf("glob() failed");
29         exit(EXIT_FAILURE);
30     }
31     char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
32     if (result == NULL) {
33         printf("malloc() failed");
34         exit(EXIT_FAILURE);
35     }
36     globfree(&globbuf);
37     return result;
38 }
39
40 void print_usage(char *elf_name) {
41     printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-h]\n", elf_name);
42     printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
43     printf("-c <command>\tExecute <command> to get stdin\n");
44     printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
45     printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
46     printf("\t\tand a SIGCONT on unhiding of the bars\n");
47     printf("-f <font>\tUse X-Core-Font <font> for display\n");
48     printf("-h\t\tDisplay this help-message and exit\n");
49 }
50
51 int main(int argc, char **argv) {
52     int opt;
53     int option_index = 0;
54     char *socket_path = NULL;
55     char *command = NULL;
56     char *fontname = NULL;
57     char *i3_default_sock_path = "~/.i3/ipc.sock";
58
59     /* Definition of the standard-config */
60     config.hide_on_modifier = 0;
61
62     static struct option long_opt[] = {
63         { "socket",  required_argument, 0, 's' },
64         { "command", required_argument, 0, 'c' },
65         { "hide",    no_argument,       0, 'm' },
66         { "font",    required_argument, 0, 'f' },
67         { "help",    no_argument,       0, 'h' },
68         { "version", no_argument,       0, 'v' },
69         { NULL,      0,                 0, 0}
70     };
71
72     while ((opt = getopt_long(argc, argv, "s:c:mf:hv", long_opt, &option_index)) != -1) {
73         switch (opt) {
74             case 's':
75                 socket_path = expand_path(optarg);
76                 break;
77             case 'c':
78                 command = strdup(optarg);
79                 break;
80             case 'm':
81                 config.hide_on_modifier = 1;
82                 break;
83             case 'f':
84                 fontname = strdup(optarg);
85                 break;
86             case 'v':
87                 printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
88                 exit(EXIT_SUCCESS);
89                 break;
90             default:
91                 print_usage(argv[0]);
92                 exit(EXIT_SUCCESS);
93                 break;
94         }
95     }
96
97     if (fontname == NULL) {
98         /* This is a very restrictive default. More sensefull would be something like
99          * "-misc-*-*-*-*--*-*-*-*-*-*-*-*". But since that produces very ugly results
100          * on my machine, let's stick with this until we have a configfile */
101         fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
102     }
103
104     if (socket_path == NULL) {
105         printf("No Socket Path Specified, default to %s\n", i3_default_sock_path);
106         socket_path = expand_path(i3_default_sock_path);
107     }
108
109     main_loop = ev_default_loop(0);
110
111     init_xcb(fontname);
112     init_outputs();
113     init_connection(socket_path);
114
115     FREE(socket_path);
116
117     /* We subscribe to the i3-events we need */
118     subscribe_events();
119
120     /* We initiate the main-function by requesting infos about the outputs and
121      * workspaces. Everything else (creating the bars, showing the right workspace-
122      * buttons and more) is taken care of by the event-driveniness of the code */
123     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
124     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
125
126     /* The name of this function is actually misleading. Even if no -c is specified,
127      * this function initiates the watchers to listen on stdin and react accordingly */
128     start_child(command);
129
130     /* From here on everything should run smooth for itself, just start listening for
131      * events. We stop simply stop the event-loop, when we are finished */
132     ev_loop(main_loop, 0);
133
134     kill_child();
135
136     FREE(statusline);
137
138     clean_xcb();
139     ev_default_destroy();
140
141     free_workspaces();
142     FREE_SLIST(outputs, i3_output);
143
144     return 0;
145 }