Commit Diff


commit - 636fecc8d40f400c4f0dd34335d879b59a02eacc
commit + 78a11878fa94c94014b7a0364319fb5025821916
blob - f2036ceee0d2a17f9060e28eec8ee71c00c908af
blob + 1b19669b48acd0d65bfc6423ef3f8ff04efbe10b
--- bxwm.c
+++ bxwm.c
@@ -1,10 +1,13 @@
 /*
+ * bxwm
+ *
  * A very basic X window manager, a.k.a. Brett's X window manager.
  * See LICENSE.md and README.md for details.
  */
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <signal.h>
 #include <X11/Xlib.h>
@@ -15,7 +18,6 @@
 
 #include "config.h"
 
-
 /* Layout types */
 enum { FLOAT, CENTER, LEFT_HALF, RIGHT_HALF, SMALL, MAXIMIZE };
 
@@ -51,7 +53,8 @@ static Atom net_wm_window_type, net_wm_window_type_doc
 /* ICCCM atoms */
 static Atom wm_protocols, wm_delete_window;
 
-/* Function declarations */
+/* Forward function declarations */
+static void logmsg(const char *fmt, ...);
 static void run(void);
 static void cleanup(int status);
 static void setup(void);
@@ -73,7 +76,8 @@ static void movetows(int ws);
 static void unmanage(Window w);
 static void update_client_list(void);
 
-int main(void) {
+int 
+main(void) {
     signal(SIGTERM, cleanup);
     dpy = XOpenDisplay(NULL);
     if (!dpy) {
@@ -87,7 +91,25 @@ int main(void) {
     return 0;
 }
 
-static void setup(void) {
+/* Uncomment to enable debug logging */
+/* #define DEBUG */
+
+#ifdef DEBUG
+static void 
+logmsg(const char *fmt, ...) {
+    va_list ap;
+    va_start(ap, fmt);
+    fprintf(stderr, "bxwm: ");
+    vfprintf(stderr, fmt, ap);
+    va_end(ap);
+    fprintf(stderr, "\n");
+}
+#else
+static void logmsg(const char *fmt, ...) { (void)fmt; }
+#endif
+
+static void 
+setup(void) {
     XSetWindowAttributes wa;
     Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy));
     XColor color;
@@ -221,22 +243,17 @@ static void setup(void) {
     update_workarea();
 
     spawn(STATUSBAR);
-
-    #ifdef DEBUG
-        fprintf(stderr, "bxwm: setup complete\n");
-    #endif
 }
 
-static void manage(Window w) {
+static void 
+manage(Window w) {
     XWindowAttributes wa;
     XSetWindowAttributes attrs;
     unsigned int win_w, win_h;
     int win_x, win_y;
     Client *c;
 
-    #ifdef DEBUG
-        fprintf(stderr, "bxwm: managing 0x%lx\n", w);
-    #endif
+    logmsg("managing 0x%lx", w);
 
     if (!XGetWindowAttributes(dpy, w, &wa))
         return;
@@ -308,7 +325,8 @@ static void manage(Window w) {
     update_client_list();
 }
 
-static void focus_client(Client *c) {
+static void 
+focus_client(Client *c) {
     Client *prev = focused_client;
 
     if (prev) {
@@ -325,13 +343,13 @@ static void focus_client(Client *c) {
         long active = c->win;
         XChangeProperty(dpy, root, net_active_window, XA_WINDOW, 32,
                         PropModeReplace, (unsigned char *)&active, 1);
-        #ifdef DEBUG
-            fprintf(stderr, "bxwm: focused 0x%lx\n", c->win);
-        #endif
+
+        logmsg("focused 0x%lx", c->win);
     }
 }
 
-static void center_window(Client *c) {
+static void 
+center_window(Client *c) {
     unsigned int win_w = (wa_w / 2) - (2 * BORDER_WIDTH);
     int win_x = wa_x + (wa_w - (win_w + 2 * BORDER_WIDTH)) / 2;
     int win_y = wa_y;
@@ -339,10 +357,6 @@ static void center_window(Client *c) {
     if (!c) return;
     c->layout = CENTER;
 
-    #ifdef DEBUG
-        fprintf(stderr, "bxwm: center 0x%lx\n", c->win);
-    #endif
-
     {
         XWindowChanges wc = {
             .x = win_x,
@@ -357,7 +371,8 @@ static void center_window(Client *c) {
     }
 }
 
-static void left_half_window(Client *c) {
+static void 
+left_half_window(Client *c) {
     unsigned int win_w = (wa_w / 2) - (2 * BORDER_WIDTH);
     int win_x = wa_x;
     int win_y = wa_y;
@@ -365,10 +380,6 @@ static void left_half_window(Client *c) {
     if (!c) return;
     c->layout = LEFT_HALF;
 
-    #ifdef DEBUG
-        fprintf(stderr, "bxwm: left 0x%lx\n", c->win);
-    #endif
-
     {
         XWindowChanges wc = {
             .x = win_x,
@@ -383,7 +394,8 @@ static void left_half_window(Client *c) {
     }
 }
 
-static void right_half_window(Client *c) {
+static void 
+right_half_window(Client *c) {
     unsigned int win_w = (wa_w / 2) - (2 * BORDER_WIDTH);
     int win_x = wa_x + wa_w - win_w - (2 * BORDER_WIDTH);
     int win_y = wa_y;
@@ -391,10 +403,6 @@ static void right_half_window(Client *c) {
     if (!c) return;
     c->layout = RIGHT_HALF;
  
-    #ifdef DEBUG
-        fprintf(stderr, "bxwm: right 0x%lx\n", c->win);
-    #endif
-
     {
         XWindowChanges wc = {
             .x = win_x,
@@ -409,7 +417,8 @@ static void right_half_window(Client *c) {
     }
 }
 
-static void small_window(Client *c) {
+static void 
+small_window(Client *c) {
     unsigned int win_w, win_h;
     int win_x, win_y;
 
@@ -436,11 +445,6 @@ static void small_window(Client *c) {
        (screen height - total window height including borders) / 2 */
     win_y = wa_y + (wa_h - (win_h + 2 * BORDER_WIDTH)) / 2;
 
-
-    #ifdef DEBUG
-        fprintf(stderr, "bxwm: small 0x%lx\n", c->win);
-    #endif
-
     {
         XWindowChanges wc = {
             .x = win_x,
@@ -455,7 +459,8 @@ static void small_window(Client *c) {
     }
 }
 
-static void maximize_window(Client *c) {
+static void 
+maximize_window(Client *c) {
     unsigned int win_w = wa_w - (2 * BORDER_WIDTH);
     unsigned int win_h = wa_h - (2 * BORDER_WIDTH);
     int win_x = wa_x;
@@ -464,10 +469,6 @@ static void maximize_window(Client *c) {
     if (!c) return;
     c->layout = MAXIMIZE;
  
-    #ifdef DEBUG
-        fprintf(stderr, "bxwm: maximize 0x%lx\n", c->win);
-    #endif
-
     {
         XWindowChanges wc = {
             .x = win_x,
@@ -482,7 +483,8 @@ static void maximize_window(Client *c) {
     }
 }
 
-static void close_window(Client *c) {
+static void 
+close_window(Client *c) {
     if (!c) return;
 
     int n;
@@ -507,7 +509,8 @@ static void close_window(Client *c) {
     XKillClient(dpy, c->win);
 }
 
-static void focus_next(void) {
+static void 
+focus_next(void) {
     unsigned int i, idx, start;
     int found = 0;
     
@@ -546,7 +549,8 @@ static void focus_next(void) {
     }
 }
 
-static void focus_prev(void) {
+static void 
+focus_prev(void) {
     unsigned int i, idx, start;
     int found = 0;
     
@@ -584,7 +588,8 @@ static void focus_prev(void) {
     }
 }
 
-static void view(int ws) {
+static void 
+view(int ws) {
     unsigned int i;
 
     if (ws < 0 || ws >= NUM_WORKSPACES || ws == curws)
@@ -620,7 +625,8 @@ static void view(int ws) {
         focus_next();
 }
 
-static void movetows(int ws) {
+static void 
+movetows(int ws) {
     Client *c = focused_client;
     int old_ws;
     
@@ -651,13 +657,12 @@ static void movetows(int ws) {
     }
 }
 
-static void unmanage(Window w) {
+static void 
+unmanage(Window w) {
     unsigned int i;
     Client *c = NULL;
 
-    #ifdef DEBUG
-        fprintf(stderr, "bxwm: unmanage 0x%lx\n", w);
-    #endif
+    logmsg("unmanage 0x%lx", w);
 
     for (i = 0; i < num_clients; i++) {
         if (clients[i].win == w) {
@@ -707,19 +712,22 @@ static void unmanage(Window w) {
     }
 }
 
-static void spawn(const char *cmd) {
+static void 
+spawn(const char *cmd) {
     if (fork() == 0)
         execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
 }
 
-static int xerror(Display *dpy, XErrorEvent *ee) {
+static int 
+xerror(Display *dpy, XErrorEvent *ee) {
     char buf[1024];
     XGetErrorText(dpy, ee->error_code, buf, sizeof(buf));
     fprintf(stderr, "bxwm: X error: %s\n", buf);
     return 0;
 }
 
-static void run(void) {
+static void 
+run(void) {
     XEvent ev;
     KeyCode kcodes[11];
     KeyCode ws_codes[10];
@@ -830,7 +838,8 @@ static void run(void) {
     }
 }
 
-static void update_client_list(void) {
+static void 
+update_client_list(void) {
     Window *wins = NULL;
     unsigned int i;
 
@@ -845,7 +854,8 @@ static void update_client_list(void) {
         free(wins);
 }
 
-static void arrange(void) {
+static void 
+arrange(void) {
     unsigned int i;
     for (i = 0; i < num_clients; i++) {
         if (clients[i].ws == curws && !clients[i].is_dock) {
@@ -860,7 +870,8 @@ static void arrange(void) {
     }
 }
 
-static void update_workarea(void) {
+static void 
+update_workarea(void) {
     unsigned long left = 0, right = 0, top = 0, bottom = 0;
     unsigned int i;
     Atom actual_type;
@@ -921,7 +932,8 @@ static void update_workarea(void) {
 }
 
 
-static void cleanup(int status) {
+static void 
+cleanup(int status) {
     if (dpy) {
         if (clients)
             free(clients);