]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
Add licensing information
[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 int main(int argc, char **argv) {
39     int opt;
40     int option_index = 0;
41     char *socket_path = NULL;
42     char *command = NULL;
43     char *fontname = NULL;
44
45     static struct option long_opt[] = {
46         { "socket",  required_argument, 0, 's' },
47         { "command", required_argument, 0, 'c' },
48         { "font",    required_argument, 0, 'f' },
49         { "help",    no_argument,       0, 'h' },
50         { "version", no_argument,       0, 'v' },
51         { NULL,      0,                 0, 0}
52     };
53
54     while ((opt = getopt_long(argc, argv, "s:c:f:hv", long_opt, &option_index)) != -1) {
55         switch (opt) {
56             case 's':
57                 socket_path = expand_path(optarg);
58                 break;
59             case 'c':
60                 command = strdup(optarg);
61                 break;
62             case 'f':
63                 fontname = strdup(optarg);
64                 break;
65             case 'v':
66                 printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
67                 exit(EXIT_SUCCESS);
68             default:
69                 printf("Usage: %s [-s socket_path] [-c command] [-f font] [-h]\n", argv[0]);
70                 printf("-s <socket_path>: Connect to i3 via <socket_path>\n");
71                 printf("-c <command>: Execute <command> to get sdtin\n");
72                 printf("-f <font>: Use X-Core-Font <font> for display\n");
73                 printf("-h: Display this help-message and exit\n");
74                 exit(EXIT_SUCCESS);
75                 break;
76         }
77     }
78
79     if (fontname == NULL) {
80         fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
81     }
82
83     if (socket_path == NULL) {
84         printf("No Socket Path Specified, default to %s\n", i3_default_sock_path);
85         socket_path = expand_path(i3_default_sock_path);
86     }
87
88     main_loop = ev_default_loop(0);
89
90     init_xcb(fontname);
91     init_outputs();
92     init_connection(socket_path);
93
94     FREE(socket_path);
95
96     subscribe_events();
97
98     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
99     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
100
101     start_child(command);
102
103     ev_loop(main_loop, 0);
104
105     kill_child();
106
107     FREE(statusline);
108
109     clean_xcb();
110     ev_default_destroy();
111
112     free_workspaces();
113     FREE_SLIST(outputs, i3_output);
114
115     return 0;
116 }