]> git.mdlowis.com Git - proto/anvil.git/commitdiff
center windows on current monitor on launch
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 14 Mar 2020 19:07:16 +0000 (15:07 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 14 Mar 2020 19:07:16 +0000 (15:07 -0400)
.gitignore
anvil.c
anvil.h
build.sh
client.c
error.c
keys.c
mons.c

index b4bd510b0d3da77aa78448dc373dde2e4513ae09..31b6d778af66f4d5398dd8af7b1485255d49a1d6 100644 (file)
@@ -1,2 +1,3 @@
 anvil
 tags
+anvil.dSYM/
diff --git a/anvil.c b/anvil.c
index 707f2c192fdb1ee05ea991939ee2cfd0a798f026..dcd041a6c6dd4ebe4b106dfdfb6fe0dfbb8d62fc 100644 (file)
--- a/anvil.c
+++ b/anvil.c
@@ -144,8 +144,6 @@ static void xclientmsg(XEvent* e)
 
 static void xpropnotify(XEvent* e)
 {
-    (void)e;
-    /* TODO: update window titles */
     XPropertyEvent* ev = &(e->xproperty);
     Client* c = client_get(ev->window);
     if (c)
@@ -182,20 +180,9 @@ static void xkeypress(XEvent* e)
     keys_run(&(e->xkey));
 }
 
-int main(void)
+static void init_cursors(void)
 {
-    XColor color, red, white, exact;
-    /* Initialize X server*/
-    check( (X.disp = XOpenDisplay(0)) != NULL,
-        "could not open display");
-    X.root = DefaultRootWindow(X.disp);
-    X.screen = DefaultScreen(X.disp);
-    X.black = BlackPixel(X.disp, X.screen);
-    X.white = WhitePixel(X.disp, X.screen);
-    XAllocNamedColor(X.disp, DefaultColormap(X.disp, X.screen), "DimGray", &color, &exact);
-    X.gray = color.pixel;
-
-    /* configure mouse cursors */
+    XColor red, white, exact;
     Colormap cmap = DefaultColormap(X.disp, X.screen);
     X.csr_root = XCreateFontCursor(X.disp, XC_left_ptr);
     XAllocNamedColor(X.disp, cmap, "red", &red, &exact);
@@ -203,7 +190,10 @@ int main(void)
     X.csr_point = XCreateFontCursor(X.disp, XC_left_ptr);
     XRecolorCursor(X.disp, X.csr_point, &red, &white);
     XDefineCursor(X.disp, X.root, X.csr_root);
+}
 
+static void init_font(void)
+{
     /* load the font */
     char **miss, *def;
     int nmiss;
@@ -214,7 +204,10 @@ int main(void)
     }
     check(X.font != NULL, "unable to create font set for title font");
     X.font_ext = XExtentsOfFontSet(X.font);
+}
 
+static void init_graphics(void)
+{
     XGCValues gv;
     /* Set up root (frame) GC's. */
     gv.foreground = X.black ^ X.white;
@@ -226,9 +219,26 @@ int main(void)
     X.graphics = XCreateGC(X.disp, X.root,
         GCForeground | GCBackground | GCFunction |
         GCLineWidth | GCSubwindowMode, &gv);
+}
 
+int main(void)
+{
+    XColor color, exact;
+    /* Initialize X server*/
+    check( (X.disp = XOpenDisplay(0)) != NULL,
+        "could not open display");
+    X.root = DefaultRootWindow(X.disp);
+    X.screen = DefaultScreen(X.disp);
+    X.black = BlackPixel(X.disp, X.screen);
+    X.white = WhitePixel(X.disp, X.screen);
+    XAllocNamedColor(X.disp, DefaultColormap(X.disp, X.screen), "DimGray", &color, &exact);
+    X.gray = color.pixel;
 
+    /* initialize anvil-specific stuff */
     check_for_wm();
+    init_cursors();
+    init_font();
+    init_graphics();
     mons_init();
     client_initall();
     keys_init();
diff --git a/anvil.h b/anvil.h
index d90f47d273fcb3fd5b50fad178ba4dc1eb74cc5e..075f8cec09466f1a346d83d54b0260aae7d28856 100644 (file)
--- a/anvil.h
+++ b/anvil.h
@@ -4,6 +4,7 @@
 #include <X11/XKBlib.h>
 #include <X11/Xatom.h>
 #include <X11/cursorfont.h>
+#include <X11/Xproto.h>
 #include <X11/extensions/Xinerama.h>
 #include <stdlib.h>
 #include <stdint.h>
index 2b940aa835c12c03603cb7dccc80969d7c61bb09..fcdf532889bd8573a756f7a17a0b3f930ff0c395 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -1,4 +1,4 @@
 #!/bin/sh
-cc -g -Wall -Wextra -Werror -o anvil *.c -lX11 -lXinerama -lm
+cc -g -Wall -Wextra -Werror -I/usr/X11R6/include -L/usr/X11R6/lib -o anvil *.c -lX11 -lXinerama -lm
 ctags *
-grep -n 'TODO' *.c | sed -e 's/: \+/: /' -e 's/\/\* *//g' -e 's/ *\*\/$//' -e 's/TODO: *//'
\ No newline at end of file
+grep -n 'TODO' *.c | sed -e 's/: \+/: /' -e 's/\/\* *//g' -e 's/ *\*\/$//' -e 's/TODO: *//'
index 15530e59fdb353c9f424a4cd943332bae5340499..0e622b7fa9e90c22ac148c82295c490b8d128c7b 100644 (file)
--- a/client.c
+++ b/client.c
@@ -30,8 +30,6 @@ Client* client_add(Window win, XWindowAttributes* attr)
     c->h = attr->height;
     mons_addclient(c);
 
-    /* TODO: place window on current monitor centered by default */
-
     /* get window name */
     client_readprops(c);
 
diff --git a/error.c b/error.c
index 98be4d57c5cad5a2fe595b762e1882a14c2c3ba1..321c4fe5ce0230c9f304446711cb5f023e07038b 100644 (file)
--- a/error.c
+++ b/error.c
@@ -11,7 +11,11 @@ int error_init(Display* disp, XErrorEvent* ev)
 
 int error_panic(Display* disp, XErrorEvent* ev)
 {
-    if (ev->error_code == BadWindow)
+    int ignore_error = (
+           (ev->error_code == BadWindow)
+        || (ev->request_code == X_SetInputFocus && ev->error_code == BadMatch)
+    );
+    if (ignore_error)
     {
         return 0;
     }
diff --git a/keys.c b/keys.c
index a43080e7ce67aab59b9feb21540c88e9d42c072f..2d5d377d9b02ec233bd26eacc188ba1b5656f9c6 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -10,7 +10,11 @@ static void to_workspace(Arg* arg)
     mons_towspace(Focused, arg->i);
 }
 
+#ifdef __APPLE__
+#define MODKEY Mod1Mask
+#else
 #define MODKEY Mod4Mask
+#endif
 
 static Key keys[] = {
     { MODKEY,   XK_1,   set_workspace,  {.i = 0 } },
@@ -23,7 +27,6 @@ static Key keys[] = {
     { MODKEY,   XK_8,   set_workspace,  {.i = 7 } },
     { MODKEY,   XK_9,   set_workspace,  {.i = 8 } },
     { MODKEY,   XK_0,   set_workspace,  {.i = 9 } },
-
     { MODKEY|ShiftMask,   XK_1,   to_workspace,  {.i = 0 } },
     { MODKEY|ShiftMask,   XK_2,   to_workspace,  {.i = 1 } },
     { MODKEY|ShiftMask,   XK_3,   to_workspace,  {.i = 2 } },
diff --git a/mons.c b/mons.c
index 710408eaf8cb1b22fede9e4a7de5147ea1d00a20..854d337accd8c02da3ad2c87255388e4174a909a 100644 (file)
--- a/mons.c
+++ b/mons.c
@@ -38,11 +38,13 @@ void mons_init(void)
 /* adds a new client to the most appropriate monitor */
 void mons_addclient(Client* c)
 {
-    /* for now just add it to the first monitor in the list */
-    c->next = Monitors->cspace->floating;
-    Monitors->cspace->floating = c;
-    /* now find the best one */
-    mons_place(c);
+    Monitor* mon = pickmon();
+    c->next = mon->cspace->floating;
+    mon->cspace->floating = c;
+    int cw = c->w + 2*BORDER_WIDTH;
+    int ch = c->h + 2*BORDER_WIDTH + TITLE_HEIGHT;
+    c->x = mon->midx - cw/2;
+    c->y = mon->midy - ch/2;
 }
 
 void mons_delclient(Client* c)
@@ -106,7 +108,7 @@ void mons_place(Client* c)
                 }
             }
         }
-        /* if we changed monitiors, make sure we update accordingly */
+        /* if we changed monitors, make sure we update accordingly */
         if (closest && loc.monitor != closest)
         {
             loc.workspace->floating = delclient(loc.workspace->floating, c);