]> git.sur5r.net Git - i3/i3/commitdiff
implement a startup monitor, move code to src/startup.c
authorMichael Stapelberg <michael@stapelberg.de>
Mon, 10 Oct 2011 10:36:21 +0000 (11:36 +0100)
committerMichael Stapelberg <michael@stapelberg.de>
Mon, 10 Oct 2011 14:54:17 +0000 (15:54 +0100)
include/all.h
include/startup.h [new file with mode: 0644]
include/util.h
src/handlers.c
src/startup.c [new file with mode: 0644]
src/util.c

index 38cda89c53824285c521329893e9312998917755..fd25629ecaf0bdd09e232313d6a2a2f52b1152db 100644 (file)
@@ -66,5 +66,6 @@
 #include "assignments.h"
 #include "regex.h"
 #include "libi3.h"
+#include "startup.h"
 
 #endif
diff --git a/include/startup.h b/include/startup.h
new file mode 100644 (file)
index 0000000..9e13101
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * vim:ts=4:sw=4:expandtab
+ *
+ * i3 - an improved dynamic tiling window manager
+ *
+ * © 2009-2011 Michael Stapelberg and contributors
+ *
+ * See file LICENSE for license information.
+ *
+ */
+#ifndef _STARTUP_H
+#define _STARTUP_H
+
+#define SN_API_NOT_YET_FROZEN 1
+#include <libsn/sn-monitor.h>
+
+/**
+ * Starts the given application by passing it through a shell. We use double
+ * fork to avoid zombie processes. As the started application’s parent exits
+ * (immediately), the application is reparented to init (process-id 1), which
+ * correctly handles childs, so we don’t have to do it :-).
+ *
+ * The shell is determined by looking for the SHELL environment variable. If
+ * it does not exist, /bin/sh is used.
+ *
+ */
+void start_application(const char *command);
+
+/**
+ * Called by libstartup-notification when something happens
+ *
+ */
+void startup_monitor_event(SnMonitorEvent *event, void *userdata);
+
+#endif
index 7c7b819a97269c6e8fcd5c0080839d156c3aae96..efef0bd089a6fa803a817afe815f3bfc2bdd992a 100644 (file)
@@ -66,18 +66,6 @@ Rect rect_add(Rect a, Rect b);
  */
 bool update_if_necessary(uint32_t *destination, const uint32_t new_value);
 
-/**
- * Starts the given application by passing it through a shell. We use double
- * fork to avoid zombie processes. As the started application’s parent exits
- * (immediately), the application is reparented to init (process-id 1), which
- * correctly handles childs, so we don’t have to do it :-).
- *
- * The shell is determined by looking for the SHELL environment variable. If
- * it does not exist, /bin/sh is used.
- *
- */
-void start_application(const char *command);
-
 /**
  * exec()s an i3 utility, for example the config file migration script or
  * i3-nagbar. This function first searches $PATH for the given utility named,
index d81d7ecc459641d2f3e833774c8bde16774f8f1f..340e24aa6c25f9b66ba0d21ed25469146a33ab65 100644 (file)
@@ -11,6 +11,9 @@
 
 #include <X11/XKBlib.h>
 
+#define SN_API_NOT_YET_FROZEN 1
+#include <libsn/sn-monitor.h>
+
 #include "all.h"
 
 int randr_base = -1;
@@ -628,6 +631,11 @@ static int handle_expose_event(xcb_expose_event_t *event) {
  *
  */
 static void handle_client_message(xcb_client_message_event_t *event) {
+    /* If this is a startup notification ClientMessage, the library will handle
+     * it and call our monitor_event() callback. */
+    if (sn_xcb_display_process_event(sndisplay, (xcb_generic_event_t*)event))
+        return;
+
     LOG("ClientMessage for window 0x%08x\n", event->window);
     if (event->type == A__NET_WM_STATE) {
         if (event->format != 32 || event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN) {
@@ -984,6 +992,9 @@ static struct property_handler_t property_handlers[] = {
  *
  */
 void property_handlers_init() {
+
+    sn_monitor_context_new(sndisplay, conn_screen, startup_monitor_event, NULL, NULL);
+
     property_handlers[0].atom = A__NET_WM_NAME;
     property_handlers[1].atom = XCB_ATOM_WM_HINTS;
     property_handlers[2].atom = XCB_ATOM_WM_NAME;
diff --git a/src/startup.c b/src/startup.c
new file mode 100644 (file)
index 0000000..dd32735
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * vim:ts=4:sw=4:expandtab
+ *
+ * i3 - an improved dynamic tiling window manager
+ *
+ * © 2009-2011 Michael Stapelberg and contributors
+ *
+ * See file LICENSE for license information.
+ *
+ * startup.c: Startup notification code
+ *
+ */
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#define SN_API_NOT_YET_FROZEN 1
+#include <libsn/sn-launcher.h>
+
+#include "all.h"
+
+/*
+ * Starts the given application by passing it through a shell. We use double fork
+ * to avoid zombie processes. As the started application’s parent exits (immediately),
+ * the application is reparented to init (process-id 1), which correctly handles
+ * childs, so we don’t have to do it :-).
+ *
+ * The shell is determined by looking for the SHELL environment variable. If it
+ * does not exist, /bin/sh is used.
+ *
+ */
+void start_application(const char *command) {
+    /* Create a startup notification context to monitor the progress of this
+     * startup. */
+    SnLauncherContext *context;
+    context = sn_launcher_context_new(sndisplay, conn_screen);
+    sn_launcher_context_set_name(context, "i3");
+    sn_launcher_context_set_description(context, "exec command in i3");
+    /* Chop off everything starting from the first space (if there are any
+     * spaces in the command), since we don’t want the parameters. */
+    char *first_word = sstrdup(command);
+    char *space = strchr(first_word, ' ');
+    if (space)
+        *space = '\0';
+    sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
+    free(first_word);
+
+    LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
+
+    LOG("executing: %s\n", command);
+    if (fork() == 0) {
+        /* Child process */
+        setsid();
+        if (fork() == 0) {
+            /* Setup the environment variable(s) */
+            sn_launcher_context_setup_child_process(context);
+
+            /* Stores the path of the shell */
+            static const char *shell = NULL;
+
+            if (shell == NULL)
+                if ((shell = getenv("SHELL")) == NULL)
+                    shell = "/bin/sh";
+
+            /* This is the child */
+            execl(shell, shell, "-c", command, (void*)NULL);
+            /* not reached */
+        }
+        exit(0);
+    }
+    wait(0);
+}
+
+/*
+ * Called by libstartup-notification when something happens
+ *
+ */
+void startup_monitor_event(SnMonitorEvent *event, void *userdata) {
+    SnStartupSequence *sequence;
+
+    DLOG("something happened\n");
+    sequence = sn_monitor_event_get_startup_sequence(event);
+
+    switch (sn_monitor_event_get_type(event)) {
+        case SN_MONITOR_EVENT_COMPLETED:
+            DLOG("startup sequence %s completed\n", sn_startup_sequence_get_id(sequence));
+            break;
+        default:
+            /* ignore */
+            break;
+    }
+}
index d0e8e947992adedb33c952221a33c56b0cd870d1..036dce9ab616c09bb425e4700a0bc04f6bdeeca7 100644 (file)
@@ -61,58 +61,6 @@ bool update_if_necessary(uint32_t *destination, const uint32_t new_value) {
     return ((*destination = new_value) != old_value);
 }
 
-/*
- * Starts the given application by passing it through a shell. We use double fork
- * to avoid zombie processes. As the started application’s parent exits (immediately),
- * the application is reparented to init (process-id 1), which correctly handles
- * childs, so we don’t have to do it :-).
- *
- * The shell is determined by looking for the SHELL environment variable. If it
- * does not exist, /bin/sh is used.
- *
- */
-void start_application(const char *command) {
-    /* Create a startup notification context to monitor the progress of this
-     * startup. */
-    SnLauncherContext *context;
-    context = sn_launcher_context_new(sndisplay, conn_screen);
-    sn_launcher_context_set_name(context, "i3");
-    sn_launcher_context_set_description(context, "exec command in i3");
-    /* Chop off everything starting from the first space (if there are any
-     * spaces in the command), since we don’t want the parameters. */
-    char *first_word = sstrdup(command);
-    char *space = strchr(first_word, ' ');
-    if (space)
-        *space = '\0';
-    sn_launcher_context_initiate(context, "i3", first_word, last_timestamp);
-    free(first_word);
-
-    LOG("startup id = %s\n", sn_launcher_context_get_startup_id(context));
-
-    LOG("executing: %s\n", command);
-    if (fork() == 0) {
-        /* Child process */
-        setsid();
-        if (fork() == 0) {
-            /* Setup the environment variable(s) */
-            sn_launcher_context_setup_child_process(context);
-
-            /* Stores the path of the shell */
-            static const char *shell = NULL;
-
-            if (shell == NULL)
-                if ((shell = getenv("SHELL")) == NULL)
-                    shell = "/bin/sh";
-
-            /* This is the child */
-            execl(shell, shell, "-c", command, (void*)NULL);
-            /* not reached */
-        }
-        exit(0);
-    }
-    wait(0);
-}
-
 /*
  * exec()s an i3 utility, for example the config file migration script or
  * i3-nagbar. This function first searches $PATH for the given utility named,