]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
Add version-option
[i3/i3] / i3bar / src / main.c
1 #include <stdio.h>
2 #include <i3/ipc.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <ev.h>
8 #include <getopt.h>
9 #include <glob.h>
10
11 #include "common.h"
12
13 char *i3_default_sock_path = "~/.i3/ipc.sock";
14
15 char *expand_path(char *path) {
16     static glob_t globbuf;
17     if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
18         printf("glob() failed");
19         exit(EXIT_FAILURE);
20     }
21     char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
22     if (result == NULL) {
23         printf("malloc() failed");
24         exit(EXIT_FAILURE);
25     }
26     globfree(&globbuf);
27     return result;
28 }
29
30 int main(int argc, char **argv) {
31     int opt;
32     int option_index = 0;
33     char *socket_path = NULL;
34     char *command = NULL;
35     char *fontname = NULL;
36
37     static struct option long_opt[] = {
38         { "socket",  required_argument, 0, 's' },
39         { "command", required_argument, 0, 'c' },
40         { "font",    required_argument, 0, 'f' },
41         { "help",    no_argument,       0, 'h' },
42         { "version", no_argument,       0, 'v' },
43         { NULL,      0,                 0, 0}
44     };
45
46     while ((opt = getopt_long(argc, argv, "s:c:f:hv", long_opt, &option_index)) != -1) {
47         switch (opt) {
48             case 's':
49                 socket_path = expand_path(optarg);
50                 break;
51             case 'c':
52                 command = strdup(optarg);
53                 break;
54             case 'f':
55                 fontname = strdup(optarg);
56                 break;
57             case 'v':
58                 printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
59                 exit(EXIT_SUCCESS);
60             default:
61                 printf("Usage: %s [-s socket_path] [-c command] [-f font] [-h]\n", argv[0]);
62                 printf("-s <socket_path>: Connect to i3 via <socket_path>\n");
63                 printf("-c <command>: Execute <command> to get sdtin\n");
64                 printf("-f <font>: Use X-Core-Font <font> for display\n");
65                 printf("-h: Display this help-message and exit\n");
66                 exit(EXIT_SUCCESS);
67                 break;
68         }
69     }
70
71     if (fontname == NULL) {
72         fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
73     }
74
75     if (socket_path == NULL) {
76         printf("No Socket Path Specified, default to %s\n", i3_default_sock_path);
77         socket_path = expand_path(i3_default_sock_path);
78     }
79
80     main_loop = ev_default_loop(0);
81
82     init_xcb(fontname);
83     init_outputs();
84     init_connection(socket_path);
85
86     FREE(socket_path);
87
88     subscribe_events();
89
90     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
91     i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
92
93     start_child(command);
94
95     ev_loop(main_loop, 0);
96
97     kill_child();
98
99     FREE(statusline);
100
101     clean_xcb();
102     ev_default_destroy();
103
104     free_workspaces();
105     FREE_SLIST(outputs, i3_output);
106
107     return 0;
108 }