From 2f735200f31f598d144d65e12940c64cd5aeaeca Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 10 Sep 2022 01:09:32 +0400 Subject: [PATCH] Move code to separate modules src/layouts.c src/interaction.c --- .gitignore | 2 +- Makefile | 5 +- src/config/buttons.c | 19 +++++++ src/config/buttons.h | 22 ++++++++ src/config/common.h | 11 ++++ src/config/keys.c | 56 ++++++++++++++++++++ src/config/keys.h | 21 ++++++++ src/config/layouts.c | 17 ++++++ src/config/layouts.h | 14 +++++ src/dwm.c | 22 ++------ src/dwm.h | 29 ++++++++++ src/dwm/spaghetti/handlers.c | 7 ++- src/dwm/spaghetti/interaction.h | 94 --------------------------------- src/dwm/spaghetti/layouts.c | 20 +++++-- src/dwm/spaghetti/layouts.h | 10 ---- src/dwm/types.h | 5 -- 16 files changed, 217 insertions(+), 137 deletions(-) create mode 100644 src/config/buttons.c create mode 100644 src/config/buttons.h create mode 100644 src/config/common.h create mode 100644 src/config/keys.c create mode 100644 src/config/keys.h create mode 100644 src/config/layouts.c create mode 100644 src/config/layouts.h delete mode 100644 src/dwm/spaghetti/layouts.h diff --git a/.gitignore b/.gitignore index 5695fcc..a80e092 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ /polytreewm # C -/src/*.o +/src/**/*.o /tests/*.o /tests/*.test diff --git a/Makefile b/Makefile index 773170b..19a289b 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,9 @@ RUST_APIS = \ src/settings.h MODULES_SRC = \ + src/config/keys.c \ + src/config/buttons.c \ + src/config/layouts.c \ src/drw.c \ src/dwm.c \ src/geom.c \ @@ -52,7 +55,7 @@ TEST_SRC = \ MAIN_SRC = $(MODULES_SRC) src/main.c -MODULES_HDR = $(MODULES_SRC:.c=.h) $(RUST_APIS) +MODULES_HDR = $(MODULES_SRC:.c=.h) $(RUST_APIS) src/config/common.h DWM_HDR = $(DWM_SRC:.c=.h) src/dwm/types.h MAIN_HDR = $(MODULES_HDR) src/main.h diff --git a/src/config/buttons.c b/src/config/buttons.c new file mode 100644 index 0000000..fbb5311 --- /dev/null +++ b/src/config/buttons.c @@ -0,0 +1,19 @@ +#include "buttons.h" + +#include "../dwm.h" + +#include + +#define MODKEY Mod4Mask + +const Button buttons[] = { + /* click event mask button function argument */ + { ClkClientWin, MODKEY, Button1, movemouse, {0} }, + { ClkClientWin, MODKEY, Button2, togglefloating, {0} }, + { ClkClientWin, MODKEY, Button3, resizemouse, {0} }, +}; + +size_t buttons_count() +{ + return sizeof(buttons) / sizeof(buttons[0]); +} diff --git a/src/config/buttons.h b/src/config/buttons.h new file mode 100644 index 0000000..5483c6f --- /dev/null +++ b/src/config/buttons.h @@ -0,0 +1,22 @@ +#ifndef _CONFIG_BUTTONS_H +#define _CONFIG_BUTTONS_H + +#include "common.h" + +#include + +typedef struct { + unsigned int click; + unsigned int mask; + unsigned int button; + void (*func)(const Arg *arg); + const Arg arg; +} Button; + +enum { ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ + +extern const Button buttons[]; + +size_t buttons_count(); + +#endif // _CONFIG_BUTTONS_H diff --git a/src/config/common.h b/src/config/common.h new file mode 100644 index 0000000..1d63f87 --- /dev/null +++ b/src/config/common.h @@ -0,0 +1,11 @@ +#ifndef _CONFIG_COMMON_H +#define _CONFIG_COMMON_H + +typedef union { + int i; + unsigned int ui; + float f; + const void *v; +} Arg; + +#endif // _CONFIG_COMMON_H diff --git a/src/config/keys.c b/src/config/keys.c new file mode 100644 index 0000000..e7eaf28 --- /dev/null +++ b/src/config/keys.c @@ -0,0 +1,56 @@ +#include "keys.h" + +#include "../dwm.h" +#include "layouts.h" + +#include + +#define MODKEY Mod4Mask + +const Key keys[] = { + // WM + { MODKEY|ControlMask|ShiftMask, XK_q, quit, {0} }, + { MODKEY|ControlMask|ShiftMask, XK_r, dorestart, {0} }, + // Monitor + { MODKEY, XK_bracketleft, focusmon, {.i = -1 } }, + { MODKEY, XK_bracketright, focusmon, {.i = +1 } }, + { MODKEY|ShiftMask, XK_bracketleft, tagmon, {.i = -1 } }, + { MODKEY|ShiftMask, XK_bracketright, tagmon, {.i = +1 } }, + // Layout + { MODKEY, XK_m, setlayout, {.v = &layouts[0]} }, // Monocle + { MODKEY, XK_f, setlayout, {.v = &layouts[1]} }, // Floating + { MODKEY, XK_t, setlayout, {.v = &layouts[2]} }, // Tile + { MODKEY|ShiftMask, XK_t, setlayout, {.v = &layouts[3]} }, // Horizontile + { MODKEY, XK_u, setlayout, {.v = &layouts[4]} }, // Centeredmaster + { MODKEY, XK_space, setlayout, {0} }, + { MODKEY, XK_i, incnmaster, {.i = +1 } }, + { MODKEY|ShiftMask, XK_i, resetnmaster, {.i = 1 } }, + { MODKEY, XK_d, incnmaster, {.i = -1 } }, + { MODKEY|ShiftMask, XK_d, resetnmaster, {.i = 0 } }, + // Stack + { MODKEY, XK_j, focusstack, {.i = +1 } }, + { MODKEY, XK_k, focusstack, {.i = -1 } }, + { MODKEY|ShiftMask, XK_j, movestack, {.i = +1 } }, + { MODKEY|ShiftMask, XK_k, movestack, {.i = -1 } }, + { MODKEY, XK_Return, zoom, {0} }, + // Window + { MODKEY|ShiftMask, XK_x, killclient, {0} }, + { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, + // Appearance + { MODKEY, XK_h, setmfact, {.f = -0.05} }, + { MODKEY, XK_l, setmfact, {.f = +0.05} }, + { MODKEY|Mod1Mask, XK_b, configborder, {.i = -1 } }, + { MODKEY|Mod1Mask|ShiftMask, XK_b, configborder, {.i = +1 } }, + { MODKEY|Mod1Mask, XK_g, configgap, {.i = -1 } }, + { MODKEY|Mod1Mask|ShiftMask, XK_g, configgap, {.i = +1 } }, + // Starting applications + { MODKEY, XK_z, spawn, {.v = "lock" } }, + { MODKEY, XK_slash, spawn, {.v = "menu" } }, + { MODKEY|ShiftMask, XK_slash, spawn, {.v = "term" } }, + { MODKEY|ShiftMask, XK_f, spawn, {.v = "firefox" } }, +}; + +size_t keys_count() +{ + return sizeof(keys) / sizeof(keys[0]); +} diff --git a/src/config/keys.h b/src/config/keys.h new file mode 100644 index 0000000..f779d4d --- /dev/null +++ b/src/config/keys.h @@ -0,0 +1,21 @@ +#ifndef _CONFIG_KEYS_H +#define _CONFIG_KEYS_H + +#include "common.h" + +#include + +#include + +typedef struct { + unsigned int mod; + KeySym keysym; + void (*func)(const Arg *); + const Arg arg; +} Key; + +extern const Key keys[]; + +size_t keys_count(); + +#endif // _CONFIG_KEYS_H diff --git a/src/config/layouts.c b/src/config/layouts.c new file mode 100644 index 0000000..0d0aa2b --- /dev/null +++ b/src/config/layouts.c @@ -0,0 +1,17 @@ +#include "layouts.h" + +#include "../dwm.h" + +const Layout layouts[] = { + /* arrange function */ + { monocle }, /* first entry is default */ + { NULL }, /* no layout function means floating behavior */ + { tile }, + { horizontile }, + { centeredmaster }, +}; + +size_t layouts_count() +{ + return sizeof(layouts) / sizeof(layouts[0]); +} diff --git a/src/config/layouts.h b/src/config/layouts.h new file mode 100644 index 0000000..7894cbb --- /dev/null +++ b/src/config/layouts.h @@ -0,0 +1,14 @@ +#ifndef _LAYOUTS_H +#define _LAYOUTS_H + +#include + +typedef struct { + void (*arrange)(void*); +} Layout; + +extern const Layout layouts[]; + +size_t layouts_count(); + +#endif // _LAYOUTS_H diff --git a/src/dwm.c b/src/dwm.c index af873a1..0597e4d 100644 --- a/src/dwm.c +++ b/src/dwm.c @@ -137,12 +137,9 @@ static void updatewmhints(Client *c); static Client *wintoclient(Window w); static Monitor *wintomon(Window w); -extern const Layout layouts[]; - #include "dwm/spaghetti/bar.h" #include "dwm/spaghetti/handlers.h" #include "dwm/spaghetti/interaction.h" -#include "dwm/spaghetti/layouts.h" #include "dwm/spaghetti/wmcheckwin.h" #include "dwm/spaghetti/xerror.h" @@ -191,15 +188,6 @@ static const char *colors[][3] = { [SchemeSel] = { col_gray4, col_cyan, "#d9b01c" }, }; -const Layout layouts[] = { - /* arrange function */ - { monocle }, /* first entry is default */ - { NULL }, /* no layout function means floating behavior */ - { tile }, - { horizontile }, - { centeredmaster }, -}; - /************************************ * Private function implementations * ************************************/ @@ -748,7 +736,7 @@ void grabbuttons(Client *c, int focused) { updatenumlockmask(); { - unsigned int i, j; + unsigned int j; unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; XUngrabButton(xbase->x_display, AnyButton, AnyModifier, c->x_window); @@ -767,7 +755,7 @@ void grabbuttons(Client *c, int focused) ); } - for (i = 0; i < LENGTH(buttons); i++) { + for (size_t i = 0; i < buttons_count(); ++i) { if (buttons[i].click == ClkClientWin) { for (j = 0; j < LENGTH(modifiers); j++) { XGrabButton( @@ -792,13 +780,13 @@ void grabkeys() { updatenumlockmask(); { - unsigned int i, j; + unsigned int j; unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; KeyCode code; XUngrabKey(xbase->x_display, AnyKey, AnyModifier, xbase->x_root); - for (i = 0; i < LENGTH(keys); i++) { + for (size_t i = 0; i < keys_count(); ++i) { if ((code = XKeysymToKeycode(xbase->x_display, keys[i].keysym))) { for (j = 0; j < LENGTH(modifiers); j++) { XGrabKey( @@ -998,7 +986,7 @@ Monitor *monitor_create() m->nmaster = settings_get_default_clients_in_master(); m->lt[0] = &layouts[0]; - m->lt[1] = &layouts[1 % LENGTH(layouts)]; + m->lt[1] = &layouts[1 % layouts_count()]; return m; diff --git a/src/dwm.h b/src/dwm.h index 39c3451..de08f91 100644 --- a/src/dwm.h +++ b/src/dwm.h @@ -1,6 +1,35 @@ #ifndef _DWM_H #define _DWM_H +#include "config/buttons.h" +#include "config/keys.h" +#include "config/layouts.h" + int dwm_main(const char *program_title); +void centeredmaster(void *arg); +void floating(void *arg); +void horizontile(void *arg); +void monocle(void *arg); +void tile(void *arg); + +void configborder(const Arg *arg); +void configgap(const Arg *arg); +void dorestart(const Arg *arg); +void focusmon(const Arg *arg); +void focusstack(const Arg *arg); +void incnmaster(const Arg *arg); +void killclient(const Arg *arg); +void movemouse(const Arg *arg); +void movestack(const Arg *arg); +void quit(const Arg *arg); +void resetnmaster(const Arg *arg); +void resizemouse(const Arg *arg); +void setlayout(const Arg *arg); +void setmfact(const Arg *arg); +void spawn(const Arg *arg); +void tagmon(const Arg *arg); +void togglefloating(const Arg *arg); +void zoom(const Arg *arg); + #endif // _DWM_H diff --git a/src/dwm/spaghetti/handlers.c b/src/dwm/spaghetti/handlers.c index 146ea76..c52c7c5 100644 --- a/src/dwm/spaghetti/handlers.c +++ b/src/dwm/spaghetti/handlers.c @@ -3,7 +3,7 @@ void on_button_press(XEvent *e) { - unsigned int i, click; + unsigned int click; Client *c; Monitor *m; XButtonPressedEvent *ev = &e->xbutton; @@ -27,7 +27,7 @@ void on_button_press(XEvent *e) XAllowEvents(xbase->x_display, ReplayPointer, CurrentTime); click = ClkClientWin; } - for (i = 0; i < LENGTH(buttons); i++) + for (size_t i = 0; i < buttons_count(); ++i) if ( click == buttons[i].click && @@ -208,13 +208,12 @@ void on_focus_in(XEvent *e) void on_key_press(XEvent *e) { - unsigned int i; KeySym keysym; XKeyEvent *ev; ev = &e->xkey; keysym = XKeycodeToKeysym(xbase->x_display, (KeyCode)ev->keycode, 0); - for (i = 0; i < LENGTH(keys); i++) { + for (size_t i = 0; i < keys_count(); ++i) { if ( keysym == keys[i].keysym && diff --git a/src/dwm/spaghetti/interaction.h b/src/dwm/spaghetti/interaction.h index 102363d..b429168 100644 --- a/src/dwm/spaghetti/interaction.h +++ b/src/dwm/spaghetti/interaction.h @@ -1,100 +1,6 @@ #ifndef _DWM_INTERACTION_H #define _DWM_INTERACTION_H -typedef union { - int i; - unsigned int ui; - float f; - const void *v; -} Arg; - -typedef struct { - unsigned int click; - unsigned int mask; - unsigned int button; - void (*func)(const Arg *arg); - const Arg arg; -} Button; - -typedef struct { - unsigned int mod; - KeySym keysym; - void (*func)(const Arg *); - const Arg arg; -} Key; - -static void configborder(const Arg *arg); -static void configgap(const Arg *arg); -static void dorestart(const Arg *arg); -static void focusmon(const Arg *arg); -static void focusstack(const Arg *arg); -static void incnmaster(const Arg *arg); -static void killclient(const Arg *arg); -static void movemouse(const Arg *arg); -static void movestack(const Arg *arg); -static void quit(const Arg *arg); -static void resetnmaster(const Arg *arg); -static void resizemouse(const Arg *arg); -static void setlayout(const Arg *arg); -static void setmfact(const Arg *arg); -static void spawn(const Arg *arg); -static void tagmon(const Arg *arg); -static void togglefloating(const Arg *arg); -static void zoom(const Arg *arg); - static void spawn_callback(); -#define MODKEY Mod4Mask - -static Key keys[] = { - // WM - { MODKEY|ControlMask|ShiftMask, XK_q, quit, {0} }, - { MODKEY|ControlMask|ShiftMask, XK_r, dorestart, {0} }, - // Monitor - { MODKEY, XK_bracketleft, focusmon, {.i = -1 } }, - { MODKEY, XK_bracketright, focusmon, {.i = +1 } }, - { MODKEY|ShiftMask, XK_bracketleft, tagmon, {.i = -1 } }, - { MODKEY|ShiftMask, XK_bracketright, tagmon, {.i = +1 } }, - // Layout - { MODKEY, XK_m, setlayout, {.v = &layouts[0]} }, // Monocle - { MODKEY, XK_f, setlayout, {.v = &layouts[1]} }, // Floating - { MODKEY, XK_t, setlayout, {.v = &layouts[2]} }, // Tile - { MODKEY|ShiftMask, XK_t, setlayout, {.v = &layouts[3]} }, // Horizontile - { MODKEY, XK_u, setlayout, {.v = &layouts[4]} }, // Centeredmaster - { MODKEY, XK_space, setlayout, {0} }, - { MODKEY, XK_i, incnmaster, {.i = +1 } }, - { MODKEY|ShiftMask, XK_i, resetnmaster, {.i = 1 } }, - { MODKEY, XK_d, incnmaster, {.i = -1 } }, - { MODKEY|ShiftMask, XK_d, resetnmaster, {.i = 0 } }, - // Stack - { MODKEY, XK_j, focusstack, {.i = +1 } }, - { MODKEY, XK_k, focusstack, {.i = -1 } }, - { MODKEY|ShiftMask, XK_j, movestack, {.i = +1 } }, - { MODKEY|ShiftMask, XK_k, movestack, {.i = -1 } }, - { MODKEY, XK_Return, zoom, {0} }, - // Window - { MODKEY|ShiftMask, XK_x, killclient, {0} }, - { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, - // Appearance - { MODKEY, XK_h, setmfact, {.f = -0.05} }, - { MODKEY, XK_l, setmfact, {.f = +0.05} }, - { MODKEY|Mod1Mask, XK_b, configborder, {.i = -1 } }, - { MODKEY|Mod1Mask|ShiftMask, XK_b, configborder, {.i = +1 } }, - { MODKEY|Mod1Mask, XK_g, configgap, {.i = -1 } }, - { MODKEY|Mod1Mask|ShiftMask, XK_g, configgap, {.i = +1 } }, - // Starting applications - { MODKEY, XK_z, spawn, {.v = "lock" } }, - { MODKEY, XK_slash, spawn, {.v = "menu" } }, - { MODKEY|ShiftMask, XK_slash, spawn, {.v = "term" } }, - { MODKEY|ShiftMask, XK_f, spawn, {.v = "firefox" } }, -}; - -// click can be ClkClientWin, or ClkRootWin -static Button buttons[] = { - /* click event mask button function argument */ - { ClkClientWin, MODKEY, Button1, movemouse, {0} }, - { ClkClientWin, MODKEY, Button2, togglefloating, {0} }, - { ClkClientWin, MODKEY, Button3, resizemouse, {0} }, -}; - #endif // _DWM_INTERACTION_H diff --git a/src/dwm/spaghetti/layouts.c b/src/dwm/spaghetti/layouts.c index acb1e58..a2bd035 100644 --- a/src/dwm/spaghetti/layouts.c +++ b/src/dwm/spaghetti/layouts.c @@ -1,8 +1,10 @@ #ifndef _DWM_LAYOUTS_C #define _DWM_LAYOUTS_C -void centeredmaster(Monitor *m) +void centeredmaster(void *const arg) { + Monitor *const m = arg; + unsigned int n = 0; for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n); if (n == 0) return; @@ -124,8 +126,10 @@ void centeredmaster(Monitor *m) } } -void floating(Monitor *m) +void floating(void *const arg) { + Monitor *const m = arg; + const int border_width = settings_get_border_width(); for (Client *c = m->clients; c; c = c->next) { @@ -145,8 +149,10 @@ void floating(Monitor *m) } } -void horizontile(Monitor *m) +void horizontile(void *const arg) { + Monitor *const m = arg; + unsigned int n = 0; for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n); if (n == 0) return; @@ -232,8 +238,10 @@ void horizontile(Monitor *m) } } -void monocle(Monitor *m) +void monocle(void *const arg) { + Monitor *const m = arg; + bool any_is_fullscreen = false; for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next)) { any_is_fullscreen = any_is_fullscreen || c->state.is_fullscreen; @@ -259,8 +267,10 @@ void monocle(Monitor *m) } } -void tile(Monitor *m) +void tile(void *const arg) { + Monitor *const m = arg; + unsigned int n = 0; for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n); if (n == 0) return; diff --git a/src/dwm/spaghetti/layouts.h b/src/dwm/spaghetti/layouts.h deleted file mode 100644 index eb9f6bb..0000000 --- a/src/dwm/spaghetti/layouts.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef _DWM_LAYOUTS_H -#define _DWM_LAYOUTS_H - -static void centeredmaster(Monitor *m); -static void floating(Monitor *m); -static void horizontile(Monitor *); -static void monocle(Monitor *m); -static void tile(Monitor *); - -#endif // _DWM_LAYOUTS_H diff --git a/src/dwm/types.h b/src/dwm/types.h index 6ad4c69..2a39c39 100644 --- a/src/dwm/types.h +++ b/src/dwm/types.h @@ -3,7 +3,6 @@ enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ enum { SchemeNorm, SchemeSel }; /* color schemes */ -enum { ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ typedef struct Monitor Monitor; typedef struct Client Client; @@ -18,10 +17,6 @@ struct Client { Window x_window; }; -typedef struct { - void (*arrange)(Monitor *); -} Layout; - struct Monitor { struct BasicGeom screen_geom; struct BasicGeom window_area_geom;