]> git.sur5r.net Git - i3/i3/blobdiff - i3bar/src/main.c
make i3bar use i3’s common.mk
[i3/i3] / i3bar / src / main.c
index 9b6f9e12074b6af13a73eb3bd7aca1785d8961b4..0def305754c767806ee80986b36197d8a3659cce 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * i3bar - an xcb-based status- and ws-bar for i3
  *
- * © 2010 Axel Wagner and contributors
+ * © 2010-2011 Axel Wagner and contributors
  *
  * See file LICNSE for license information
  *
@@ -30,7 +30,7 @@ char *expand_path(char *path) {
     }
     char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
     if (result == NULL) {
-        ELOG("malloc() failed\n");
+        ELOG("malloc() failed: %s\n", strerror(errno));
         exit(EXIT_FAILURE);
     }
     globfree(&globbuf);
@@ -75,38 +75,66 @@ static void free_colors(struct xcb_color_strings_t *colors) {
     FREE_COLOR(inactive_ws_bg);
     FREE_COLOR(urgent_ws_fg);
     FREE_COLOR(urgent_ws_bg);
+    FREE_COLOR(focus_ws_fg);
+    FREE_COLOR(focus_ws_bg);
 #undef FREE_COLOR
 }
 
 void print_usage(char *elf_name) {
-    printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-V] [-h]\n", elf_name);
+    printf("Usage: %s [-s sock_path] [-c command] [-m|-d[pos]] [-f font] [-V] [-h]\n", elf_name);
     printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
     printf("-c <command>\tExecute <command> to get stdin\n");
     printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
+    printf("-d[<pos>]\tEnable dockmode. <pos> is \"top\" or \"bottom\". Default is bottom\n");
     printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
     printf("\t\tand a SIGCONT on unhiding of the bars\n");
     printf("-f <font>\tUse X-Core-Font <font> for display\n");
+    printf("-w\t\tDisable workspace-buttons\n");
     printf("-V\t\tBe (very) verbose with the debug-output\n");
     printf("-h\t\tDisplay this help-message and exit\n");
 }
 
+/*
+ * We watch various signals, that are there to make our application stop.
+ * If we get one of those, we ev_unloop() and invoke the cleanup-routines
+ * in main() with that
+ *
+ */
+void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
+    switch (watcher->signum) {
+        case SIGTERM:
+            DLOG("Got a SIGTERM, stopping\n");
+            break;
+        case SIGINT:
+            DLOG("Got a SIGINT, stopping\n");
+            break;
+        case SIGHUP:
+            DLOG("Got a SIGHUP, stopping\n");
+    }
+    ev_unloop(main_loop, EVUNLOOP_ALL);
+}
+
 int main(int argc, char **argv) {
     int opt;
     int option_index = 0;
-    char *socket_path = NULL;
+    char *socket_path = getenv("I3SOCK");
     char *command = NULL;
     char *fontname = NULL;
-    char *i3_default_sock_path = "~/.i3/ipc.sock";
+    char *i3_default_sock_path = "/tmp/i3-ipc.sock";
     struct xcb_color_strings_t colors = { NULL, };
 
     /* Definition of the standard-config */
     config.hide_on_modifier = 0;
+    config.dockpos = DOCKPOS_NONE;
+    config.disable_ws = 0;
 
     static struct option long_opt[] = {
         { "socket",               required_argument, 0, 's' },
         { "command",              required_argument, 0, 'c' },
         { "hide",                 no_argument,       0, 'm' },
+        { "dock",                 optional_argument, 0, 'd' },
         { "font",                 required_argument, 0, 'f' },
+        { "nows",                 no_argument,       0, 'w' },
         { "help",                 no_argument,       0, 'h' },
         { "version",              no_argument,       0, 'v' },
         { "verbose",              no_argument,       0, 'V' },
@@ -118,10 +146,12 @@ int main(int argc, char **argv) {
         { "color-inactive-ws-bg", required_argument, 0, 'F' },
         { "color-urgent-ws-bg",   required_argument, 0, 'G' },
         { "color-urgent-ws-fg",   required_argument, 0, 'H' },
+        { "color-focus-ws-bg",    required_argument, 0, 'I' },
+        { "color-focus-ws-fg",    required_argument, 0, 'J' },
         { NULL,                   0,                 0, 0}
     };
 
-    while ((opt = getopt_long(argc, argv, "s:c:mf:hvV:A:B:C:D:E:F:G:H:", long_opt, &option_index)) != -1) {
+    while ((opt = getopt_long(argc, argv, "s:c:d::mf:whvVA:B:C:D:E:F:G:H:I:J:", long_opt, &option_index)) != -1) {
         switch (opt) {
             case 's':
                 socket_path = expand_path(optarg);
@@ -132,11 +162,29 @@ int main(int argc, char **argv) {
             case 'm':
                 config.hide_on_modifier = 1;
                 break;
+            case 'd':
+                config.hide_on_modifier = 0;
+                if (optarg == NULL) {
+                    config.dockpos = DOCKPOS_BOT;
+                    break;
+                }
+                if (!strcmp(optarg, "top")) {
+                    config.dockpos = DOCKPOS_TOP;
+                } else if (!strcmp(optarg, "bottom")) {
+                    config.dockpos = DOCKPOS_BOT;
+                } else {
+                    print_usage(argv[0]);
+                    exit(EXIT_FAILURE);
+                }
+                break;
             case 'f':
                 fontname = strdup(optarg);
                 break;
+            case 'w':
+                config.disable_ws = 1;
+                break;
             case 'v':
-                printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
+                printf("i3bar version " I3_VERSION " © 2010-2011 Axel Wagner and contributors\n");
                 exit(EXIT_SUCCESS);
                 break;
             case 'V':
@@ -161,10 +209,16 @@ int main(int argc, char **argv) {
                 read_color(&colors.inactive_ws_bg);
                 break;
             case 'G':
-                read_color(&colors.urgent_ws_fg);
+                read_color(&colors.urgent_ws_bg);
                 break;
             case 'H':
-                read_color(&colors.urgent_ws_bg);
+                read_color(&colors.urgent_ws_fg);
+                break;
+            case 'I':
+                read_color(&colors.focus_ws_bg);
+                break;
+            case 'J':
+                read_color(&colors.focus_ws_fg);
                 break;
             default:
                 print_usage(argv[0]);
@@ -180,35 +234,69 @@ int main(int argc, char **argv) {
         fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
     }
 
-    if (socket_path == NULL) {
-        ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
-        socket_path = expand_path(i3_default_sock_path);
+    if (config.dockpos != DOCKPOS_NONE) {
+        if (config.hide_on_modifier) {
+            ELOG("--dock and --hide are mutually exclusive!\n");
+            exit(EXIT_FAILURE);
+        }
+    } else {
+        config.hide_on_modifier = 1;
     }
 
     main_loop = ev_default_loop(0);
 
     init_colors(&colors);
-    init_xcb(fontname);
+    char *atom_sock_path = init_xcb(fontname);
 
-    free_colors(&colors);
+    if (socket_path == NULL) {
+        socket_path = atom_sock_path;
+    }
 
-    init_outputs();
-    init_connection(socket_path);
+    if (socket_path == NULL) {
+        ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
+        socket_path = expand_path(i3_default_sock_path);
+    }
 
-    FREE(socket_path);
+    free_colors(&colors);
 
-    /* We subscribe to the i3-events we need */
-    subscribe_events();
+    init_outputs();
+    if (init_connection(socket_path)) {
+        /* We subscribe to the i3-events we need */
+        subscribe_events();
 
-    /* We initiate the main-function by requesting infos about the outputs and
-     * workspaces. Everything else (creating the bars, showing the right workspace-
-     * buttons and more) is taken care of by the event-driveniness of the code */
-    i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
-    i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
+        /* We initiate the main-function by requesting infos about the outputs and
+         * workspaces. Everything else (creating the bars, showing the right workspace-
+         * buttons and more) is taken care of by the event-driveniness of the code */
+        i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
+        if (!config.disable_ws) {
+            i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
+        }
+    }
 
     /* The name of this function is actually misleading. Even if no -c is specified,
      * this function initiates the watchers to listen on stdin and react accordingly */
     start_child(command);
+    FREE(command);
+
+    /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
+     * We only need those watchers on the stack, so putting them on the stack saves us
+     * some calls to free() */
+    ev_signal *sig_term = malloc(sizeof(ev_signal));
+    ev_signal *sig_int = malloc(sizeof(ev_signal));
+    ev_signal *sig_hup = malloc(sizeof(ev_signal));
+
+    if (sig_term == NULL || sig_int == NULL || sig_hup == NULL) {
+        ELOG("malloc() failed: %s\n", strerror(errno));
+        exit(EXIT_FAILURE);
+    }
+
+    ev_signal_init(sig_term, &sig_cb, SIGTERM);
+    ev_signal_init(sig_int, &sig_cb, SIGINT);
+    ev_signal_init(sig_hup, &sig_cb, SIGHUP);
+
+    ev_signal_start(main_loop, sig_term);
+    ev_signal_start(main_loop, sig_int);
+    ev_signal_start(main_loop, sig_hup);
 
     /* From here on everything should run smooth for itself, just start listening for
      * events. We stop simply stop the event-loop, when we are finished */
@@ -216,13 +304,12 @@ int main(int argc, char **argv) {
 
     kill_child();
 
-    FREE(statusline);
+    FREE(statusline_buffer);
 
     clean_xcb();
     ev_default_destroy();
 
     free_workspaces();
-    FREE_SLIST(outputs, i3_output);
 
     return 0;
 }