]> Pileus Git - wmpus/blobdiff - util.c
Add focus and enter events
[wmpus] / util.c
diff --git a/util.c b/util.c
index cfe69a9b863e1d1a1b80825c506209b7dffc3f6c..b589fd3d7247bbe5b554de84b520ec359b40d9d8 100644 (file)
--- a/util.c
+++ b/util.c
@@ -90,6 +90,41 @@ list_t *list_find(list_t *list, void *data)
        return NULL;
 }
 
+list_t *list_sort(list_t *list, int rev, int (*func)(void *a, void*b))
+{
+       if (list == NULL || list->next == NULL)
+               return list;
+
+       /* Split list */
+       list_t *sides[2] = {NULL, NULL};
+       for (int i = 0; list; i=(i+1)%2) {
+               list_t *head = list;
+               list = list->next;
+               head->next = sides[i];
+               sides[i]   = head;
+       }
+
+       /* Sort sides */
+       sides[0] = list_sort(sides[0], !rev, func);
+       sides[1] = list_sort(sides[1], !rev, func);
+
+       /* Merge sides */
+       while (sides[0] || sides[1]) {
+               int i = sides[0] == NULL ? 1 :
+                       sides[1] == NULL ? 0 :
+                       func(sides[0]->data,
+                            sides[1]->data) > 0 ? !!rev : !rev;
+               list_t *head = sides[i];
+               sides[i] = sides[i]->next;
+               head->next = list;
+               head->prev = NULL;
+               if (list)
+                       list->prev = head;
+               list = head;
+       }
+       return list;
+}
+
 /* Misc */
 int str2num(char *str, int def)
 {
@@ -98,6 +133,17 @@ int str2num(char *str, int def)
        return end && *end == '\0' ? num : def;
 }
 
+int warn(char *fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       fprintf(stderr, "Warning: ");
+       vfprintf(stderr, fmt, ap);
+       fprintf(stderr, "\n");
+       va_end(ap);
+       return 0;
+}
+
 int error(char *fmt, ...)
 {
        va_list ap;