]> git.lizzy.rs Git - bspwm.git/blobdiff - helpers.c
Implement ICCCM's WM_TAKE_FOCUS behavior
[bspwm.git] / helpers.c
index 28bed8e500a4382fd96654daaf5115097651808b..6b8a8444e1f8f5e00d090fcae5a99d9f5bfac60e 100644 (file)
--- a/helpers.c
+++ b/helpers.c
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 #include <xcb/xcb.h>
 #include <xcb/xcb_event.h>
 #include "bspwm.h"
@@ -23,10 +24,13 @@ void err(char *fmt, ...)
     exit(EXIT_FAILURE);
 }
 
-uint32_t get_color(char *col)
+bool get_color(char *col, xcb_window_t win, uint32_t *pxl)
 {
     xcb_colormap_t map = screen->default_colormap;
-    uint32_t pxl = 0;
+    xcb_get_window_attributes_reply_t *reply = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
+    if (reply != NULL)
+        map = reply->colormap;
+    free(reply);
 
     if (col[0] == '#') {
         unsigned int red, green, blue;
@@ -37,17 +41,24 @@ uint32_t get_color(char *col)
             blue *= 0x101;
             xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(dpy, xcb_alloc_color(dpy, map, red, green, blue), NULL);
             if (reply != NULL) {
-                pxl = reply->pixel;
+                *pxl = reply->pixel;
                 free(reply);
+                return true;
             }
         }
     } else {
         xcb_alloc_named_color_reply_t *reply = xcb_alloc_named_color_reply(dpy, xcb_alloc_named_color(dpy, map, strlen(col), col), NULL);
         if (reply != NULL) {
-            pxl = reply->pixel;
+            *pxl = reply->pixel;
             free(reply);
+            return true;
         }
     }
+    *pxl = 0;
+    return false;
+}
 
-    return pxl;
+double distance(xcb_point_t a, xcb_point_t b)
+{
+    return hypot(a.x - b.x, a.y - b.y);
 }