From: Michael D. Lowis Date: Sat, 14 Mar 2020 19:07:16 +0000 (-0400) Subject: center windows on current monitor on launch X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=f19f89713af5d26b97a560454158799803e9dd7d;p=proto%2Fanvil.git center windows on current monitor on launch --- diff --git a/.gitignore b/.gitignore index b4bd510..31b6d77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ anvil tags +anvil.dSYM/ diff --git a/anvil.c b/anvil.c index 707f2c1..dcd041a 100644 --- 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 d90f47d..075f8ce 100644 --- a/anvil.h +++ b/anvil.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/build.sh b/build.sh index 2b940aa..fcdf532 100755 --- 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: *//' diff --git a/client.c b/client.c index 15530e5..0e622b7 100644 --- 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 98be4d5..321c4fe 100644 --- 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 a43080e..2d5d377 100644 --- 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 710408e..854d337 100644 --- 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);