]> git.sur5r.net Git - i3/i3/blobdiff - include/util.h
Merge branch 'master' into next
[i3/i3] / include / util.h
index 064a4792d8cb4a4635352a5bfae91ac51ee10e90..edc51d812de74580d6387aca300a70a5e4c597df 100644 (file)
@@ -8,7 +8,6 @@
  * See file LICENSE for license information.
  *
  */
-#include <xcb/xcb.h>
 #include <err.h>
 
 #include "data.h"
 #define FOR_TABLE(workspace) \
                         for (int cols = 0; cols < (workspace)->cols; cols++) \
                                 for (int rows = 0; rows < (workspace)->rows; rows++)
+
+#define NODES_FOREACH(head) \
+    for (Con *child = (Con*)-1; (child == (Con*)-1) && ((child = 0), true);) \
+        TAILQ_FOREACH(child, &((head)->nodes_head), nodes)
+
+#define NODES_FOREACH_REVERSE(head) \
+    for (Con *child = (Con*)-1; (child == (Con*)-1) && ((child = 0), true);) \
+        TAILQ_FOREACH_REVERSE(child, &((head)->nodes_head), nodes_head, nodes)
+
+/* greps the ->nodes of the given head and returns the first node that matches the given condition */
+#define GREP_FIRST(dest, head, condition) \
+    NODES_FOREACH(head) { \
+        if (!(condition)) \
+            continue; \
+        \
+        (dest) = child; \
+        break; \
+    }
+
 #define FREE(pointer) do { \
         if (pointer != NULL) { \
                 free(pointer); \
@@ -88,6 +106,23 @@ char *sstrdup(const char *str);
  */
 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,
+ * then falls back to the dirname() of the i3 executable path and then falls
+ * back to the dirname() of the target of /proc/self/exe (on linux).
+ *
+ * This function should be called after fork()ing.
+ *
+ * The first argument of the given argv vector will be overwritten with the
+ * executable name, so pass NULL.
+ *
+ * If the utility cannot be found in any of these locations, it exits with
+ * return code 2.
+ *
+ */
+void exec_i3_utility(char *name, char *argv[]);
+
 /**
  * Checks a generic cookie for errors and quits with the given message if
  * there was an error.
@@ -105,7 +140,7 @@ void check_error(xcb_connection_t *conn, xcb_void_cookie_t cookie,
  */
 char *convert_utf8_to_ucs2(char *input, int *real_strlen);
 
-/*
+/**
  * This function resolves ~ in pathnames.
  * It may resolve wildcards in the first part of the path, but if no match
  * or multiple matches are found, it just returns a copy of path as given.
@@ -113,17 +148,47 @@ char *convert_utf8_to_ucs2(char *input, int *real_strlen);
  */
 char *resolve_tilde(const char *path);
 
-/*
+/**
  * Checks if the given path exists by calling stat().
  *
  */
 bool path_exists(const char *path);
 
-/*
+
+/**
+ * Returns the name of a temporary file with the specified prefix.
+ *
+ */
+char *get_process_filename(const char *prefix);
+
+/**
  * Restart i3 in-place
  * appends -a to argument list to disable autostart
  *
  */
 void i3_restart(bool forget_layout);
 
+#if defined(__OpenBSD__) || defined(__APPLE__)
+
+/*
+ * Taken from FreeBSD
+ * Find the first occurrence of the byte string s in byte string l.
+ *
+ */
+void *memmem(const void *l, size_t l_len, const void *s, size_t s_len);
+
+#endif
+
+#if defined(__APPLE__)
+
+/*
+ * Taken from FreeBSD
+ * Returns a pointer to a new string which is a duplicate of the
+ * string, but only copies at most n characters.
+ *
+ */
+char *strndup(const char *str, size_t n);
+
+#endif
+
 #endif