Move code to separate modules

src/layouts.c
src/interaction.c
This commit is contained in:
Alex Kotov 2022-09-10 01:09:32 +04:00
parent 7861f0ccd6
commit 2f735200f3
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
16 changed files with 217 additions and 137 deletions

2
.gitignore vendored
View file

@ -3,7 +3,7 @@
/polytreewm /polytreewm
# C # C
/src/*.o /src/**/*.o
/tests/*.o /tests/*.o
/tests/*.test /tests/*.test

View file

@ -27,6 +27,9 @@ RUST_APIS = \
src/settings.h src/settings.h
MODULES_SRC = \ MODULES_SRC = \
src/config/keys.c \
src/config/buttons.c \
src/config/layouts.c \
src/drw.c \ src/drw.c \
src/dwm.c \ src/dwm.c \
src/geom.c \ src/geom.c \
@ -52,7 +55,7 @@ TEST_SRC = \
MAIN_SRC = $(MODULES_SRC) src/main.c 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 DWM_HDR = $(DWM_SRC:.c=.h) src/dwm/types.h
MAIN_HDR = $(MODULES_HDR) src/main.h MAIN_HDR = $(MODULES_HDR) src/main.h

19
src/config/buttons.c Normal file
View file

@ -0,0 +1,19 @@
#include "buttons.h"
#include "../dwm.h"
#include <X11/keysym.h>
#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]);
}

22
src/config/buttons.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef _CONFIG_BUTTONS_H
#define _CONFIG_BUTTONS_H
#include "common.h"
#include <stddef.h>
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

11
src/config/common.h Normal file
View file

@ -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

56
src/config/keys.c Normal file
View file

@ -0,0 +1,56 @@
#include "keys.h"
#include "../dwm.h"
#include "layouts.h"
#include <X11/keysym.h>
#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]);
}

21
src/config/keys.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef _CONFIG_KEYS_H
#define _CONFIG_KEYS_H
#include "common.h"
#include <stddef.h>
#include <X11/Xlib.h>
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

17
src/config/layouts.c Normal file
View file

@ -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]);
}

14
src/config/layouts.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef _LAYOUTS_H
#define _LAYOUTS_H
#include <stddef.h>
typedef struct {
void (*arrange)(void*);
} Layout;
extern const Layout layouts[];
size_t layouts_count();
#endif // _LAYOUTS_H

View file

@ -137,12 +137,9 @@ static void updatewmhints(Client *c);
static Client *wintoclient(Window w); static Client *wintoclient(Window w);
static Monitor *wintomon(Window w); static Monitor *wintomon(Window w);
extern const Layout layouts[];
#include "dwm/spaghetti/bar.h" #include "dwm/spaghetti/bar.h"
#include "dwm/spaghetti/handlers.h" #include "dwm/spaghetti/handlers.h"
#include "dwm/spaghetti/interaction.h" #include "dwm/spaghetti/interaction.h"
#include "dwm/spaghetti/layouts.h"
#include "dwm/spaghetti/wmcheckwin.h" #include "dwm/spaghetti/wmcheckwin.h"
#include "dwm/spaghetti/xerror.h" #include "dwm/spaghetti/xerror.h"
@ -191,15 +188,6 @@ static const char *colors[][3] = {
[SchemeSel] = { col_gray4, col_cyan, "#d9b01c" }, [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 * * Private function implementations *
************************************/ ************************************/
@ -748,7 +736,7 @@ void grabbuttons(Client *c, int focused)
{ {
updatenumlockmask(); updatenumlockmask();
{ {
unsigned int i, j; unsigned int j;
unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
XUngrabButton(xbase->x_display, AnyButton, AnyModifier, c->x_window); 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) { if (buttons[i].click == ClkClientWin) {
for (j = 0; j < LENGTH(modifiers); j++) { for (j = 0; j < LENGTH(modifiers); j++) {
XGrabButton( XGrabButton(
@ -792,13 +780,13 @@ void grabkeys()
{ {
updatenumlockmask(); updatenumlockmask();
{ {
unsigned int i, j; unsigned int j;
unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
KeyCode code; KeyCode code;
XUngrabKey(xbase->x_display, AnyKey, AnyModifier, xbase->x_root); 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))) { if ((code = XKeysymToKeycode(xbase->x_display, keys[i].keysym))) {
for (j = 0; j < LENGTH(modifiers); j++) { for (j = 0; j < LENGTH(modifiers); j++) {
XGrabKey( XGrabKey(
@ -998,7 +986,7 @@ Monitor *monitor_create()
m->nmaster = settings_get_default_clients_in_master(); m->nmaster = settings_get_default_clients_in_master();
m->lt[0] = &layouts[0]; m->lt[0] = &layouts[0];
m->lt[1] = &layouts[1 % LENGTH(layouts)]; m->lt[1] = &layouts[1 % layouts_count()];
return m; return m;

View file

@ -1,6 +1,35 @@
#ifndef _DWM_H #ifndef _DWM_H
#define _DWM_H #define _DWM_H
#include "config/buttons.h"
#include "config/keys.h"
#include "config/layouts.h"
int dwm_main(const char *program_title); 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 #endif // _DWM_H

View file

@ -3,7 +3,7 @@
void on_button_press(XEvent *e) void on_button_press(XEvent *e)
{ {
unsigned int i, click; unsigned int click;
Client *c; Client *c;
Monitor *m; Monitor *m;
XButtonPressedEvent *ev = &e->xbutton; XButtonPressedEvent *ev = &e->xbutton;
@ -27,7 +27,7 @@ void on_button_press(XEvent *e)
XAllowEvents(xbase->x_display, ReplayPointer, CurrentTime); XAllowEvents(xbase->x_display, ReplayPointer, CurrentTime);
click = ClkClientWin; click = ClkClientWin;
} }
for (i = 0; i < LENGTH(buttons); i++) for (size_t i = 0; i < buttons_count(); ++i)
if ( if (
click == buttons[i].click click == buttons[i].click
&& &&
@ -208,13 +208,12 @@ void on_focus_in(XEvent *e)
void on_key_press(XEvent *e) void on_key_press(XEvent *e)
{ {
unsigned int i;
KeySym keysym; KeySym keysym;
XKeyEvent *ev; XKeyEvent *ev;
ev = &e->xkey; ev = &e->xkey;
keysym = XKeycodeToKeysym(xbase->x_display, (KeyCode)ev->keycode, 0); 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 ( if (
keysym == keys[i].keysym keysym == keys[i].keysym
&& &&

View file

@ -1,100 +1,6 @@
#ifndef _DWM_INTERACTION_H #ifndef _DWM_INTERACTION_H
#define _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(); 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 #endif // _DWM_INTERACTION_H

View file

@ -1,8 +1,10 @@
#ifndef _DWM_LAYOUTS_C #ifndef _DWM_LAYOUTS_C
#define _DWM_LAYOUTS_C #define _DWM_LAYOUTS_C
void centeredmaster(Monitor *m) void centeredmaster(void *const arg)
{ {
Monitor *const m = arg;
unsigned int n = 0; unsigned int n = 0;
for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n); for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n);
if (n == 0) return; 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(); const int border_width = settings_get_border_width();
for (Client *c = m->clients; c; c = c->next) { 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; unsigned int n = 0;
for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n); for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n);
if (n == 0) return; 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; bool any_is_fullscreen = false;
for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next)) { for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next)) {
any_is_fullscreen = any_is_fullscreen || c->state.is_fullscreen; 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; unsigned int n = 0;
for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n); for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n);
if (n == 0) return; if (n == 0) return;

View file

@ -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

View file

@ -3,7 +3,6 @@
enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
enum { SchemeNorm, SchemeSel }; /* color schemes */ enum { SchemeNorm, SchemeSel }; /* color schemes */
enum { ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
typedef struct Monitor Monitor; typedef struct Monitor Monitor;
typedef struct Client Client; typedef struct Client Client;
@ -18,10 +17,6 @@ struct Client {
Window x_window; Window x_window;
}; };
typedef struct {
void (*arrange)(Monitor *);
} Layout;
struct Monitor { struct Monitor {
struct BasicGeom screen_geom; struct BasicGeom screen_geom;
struct BasicGeom window_area_geom; struct BasicGeom window_area_geom;