Working on dwlb

This commit is contained in:
2025-09-19 11:44:17 -04:00
parent 24725fcdb8
commit e745c93495
18 changed files with 5504 additions and 136 deletions

View File

@@ -1,6 +1,7 @@
/*
* See LICENSE file for copyright and license details.
*/
#include <limits.h>
#include <getopt.h>
#include <libinput.h>
#include <linux/input-event-codes.h>
@@ -138,6 +139,7 @@ typedef struct {
unsigned int bw;
uint32_t tags;
int isfloating, isurgent, isfullscreen;
float opacity;
uint32_t resize; /* configure serial of a pending resize */
} Client;
@@ -197,6 +199,10 @@ struct Monitor {
struct wlr_box w; /* window area, layout-relative */
struct wl_list layers[4]; /* LayerSurface.link */
const Layout *lt[2];
int gappih; /* horizontal gap between windows */
int gappiv; /* vertical gap between windows */
int gappoh; /* horizontal outer gaps */
int gappov; /* vertical outer gaps */
unsigned int seltags;
unsigned int sellt;
uint32_t tagset[2];
@@ -227,6 +233,7 @@ typedef struct {
const char *title;
uint32_t tags;
int isfloating;
float opacity;
int monitor;
} Rule;
@@ -239,6 +246,12 @@ typedef struct {
struct wl_listener destroy;
} SessionLock;
typedef struct {
const char *cmd; /* command to run a menu */
void (*feed)(FILE *f); /* feed input to menu */
void (*action)(char *line); /* do action based on menu output */
} Menu;
/* function declarations */
static void applybounds(Client *c, struct wlr_box *bbox);
static void applyrules(Client *c);
@@ -246,6 +259,7 @@ static void arrange(Monitor *m);
static void arrangelayer(Monitor *m, struct wl_list *list,
struct wlr_box *usable_area, int exclusive);
static void arrangelayers(Monitor *m);
static void autostartexec(void);
static void axisnotify(struct wl_listener *listener, void *data);
static void buttonpress(struct wl_listener *listener, void *data);
static void chvt(const Arg *arg);
@@ -271,6 +285,7 @@ static void createpopup(struct wl_listener *listener, void *data);
static void cursorconstrain(struct wlr_pointer_constraint_v1 *constraint);
static void cursorframe(struct wl_listener *listener, void *data);
static void cursorwarptohint(void);
static void defaultgaps(const Arg *arg);
static void destroydecoration(struct wl_listener *listener, void *data);
static void destroydragicon(struct wl_listener *listener, void *data);
static void destroyidleinhibitor(struct wl_listener *listener, void *data);
@@ -285,11 +300,19 @@ static Monitor *dirtomon(enum wlr_direction dir);
static void focusclient(Client *c, int lift);
static void focusmon(const Arg *arg);
static void focusstack(const Arg *arg);
static void focusdir(const Arg *arg);
static Client *focustop(Monitor *m);
static void fullscreennotify(struct wl_listener *listener, void *data);
static void gpureset(struct wl_listener *listener, void *data);
static void handlesig(int signo);
static void incnmaster(const Arg *arg);
static void incgaps(const Arg *arg);
static void incigaps(const Arg *arg);
static void incihgaps(const Arg *arg);
static void incivgaps(const Arg *arg);
static void incogaps(const Arg *arg);
static void incohgaps(const Arg *arg);
static void incovgaps(const Arg *arg);
static void inputdevice(struct wl_listener *listener, void *data);
static int keybinding(uint32_t mods, xkb_keysym_t sym);
static void keypress(struct wl_listener *listener, void *data);
@@ -299,6 +322,12 @@ static void killclient(const Arg *arg);
static void locksession(struct wl_listener *listener, void *data);
static void mapnotify(struct wl_listener *listener, void *data);
static void maximizenotify(struct wl_listener *listener, void *data);
static void menu(const Arg *arg);
static int menuread(int fd, uint32_t mask, void *data);
static void menuwinfeed(FILE *f);
static void menuwinaction(char *line);
static void menulayoutfeed(FILE *f);
static void menulayoutaction(char *line);
static void monocle(Monitor *m);
static void motionabsolute(struct wl_listener *listener, void *data);
static void motionnotify(uint32_t time, struct wlr_input_device *device, double sx,
@@ -319,13 +348,16 @@ static void requeststartdrag(struct wl_listener *listener, void *data);
static void requestmonstate(struct wl_listener *listener, void *data);
static void resize(Client *c, struct wlr_box geo, int interact);
static void run(char *startup_cmd);
static void scenebuffersetopacity(struct wlr_scene_buffer *buffer, int sx, int sy, void *user_data);
static void setcursor(struct wl_listener *listener, void *data);
static void setcursorshape(struct wl_listener *listener, void *data);
static void setfloating(Client *c, int floating);
static void setfullscreen(Client *c, int fullscreen);
static void setgaps(int oh, int ov, int ih, int iv);
static void setlayout(const Arg *arg);
static void setmfact(const Arg *arg);
static void setmon(Client *c, Monitor *m, uint32_t newtags);
static void setopacity(const Arg *arg);
static void setpsel(struct wl_listener *listener, void *data);
static void setsel(struct wl_listener *listener, void *data);
static void setup(void);
@@ -336,6 +368,7 @@ static void tagmon(const Arg *arg);
static void tile(Monitor *m);
static void togglefloating(const Arg *arg);
static void togglefullscreen(const Arg *arg);
static void togglegaps(const Arg *arg);
static void toggletag(const Arg *arg);
static void toggleview(const Arg *arg);
static void unlocksession(struct wl_listener *listener, void *data);
@@ -405,6 +438,7 @@ static struct wlr_output_layout *output_layout;
static struct wlr_box sgeom;
static struct wl_list mons;
static Monitor *selmon;
static int enablegaps = 1; /* enables gaps, used by togglegaps */
/* global event handlers */
static struct wl_listener cursor_axis = {.notify = axisnotify};
@@ -436,6 +470,11 @@ static struct wl_listener request_start_drag = {.notify = requeststartdrag};
static struct wl_listener start_drag = {.notify = startdrag};
static struct wl_listener new_session_lock = {.notify = locksession};
static const Menu *menu_current;
static int menu_fd;
static pid_t menu_pid;
static struct wl_event_source *menu_source;
#ifdef XWAYLAND
static void activatex11(struct wl_listener *listener, void *data);
static void associatex11(struct wl_listener *listener, void *data);
@@ -455,6 +494,9 @@ static struct wlr_xwayland *xwayland;
/* attempt to encapsulate suck into one file */
#include "client.h"
static pid_t *autostart_pids;
static size_t autostart_len;
/* function implementations */
void
applybounds(Client *c, struct wlr_box *bbox)
@@ -490,6 +532,7 @@ applyrules(Client *c)
if ((!r->title || strstr(title, r->title))
&& (!r->id || strstr(appid, r->id))) {
c->isfloating = r->isfloating;
c->opacity = r->opacity;
newtags |= r->tags;
i = 0;
wl_list_for_each(m, &mons, link) {
@@ -498,6 +541,8 @@ applyrules(Client *c)
}
}
}
if (c->scene_surface)
wlr_scene_node_for_each_buffer(&c->scene_surface->node, scenebuffersetopacity, c);
c->isfloating |= client_is_float_type(c);
setmon(c, mon, newtags);
@@ -603,6 +648,27 @@ arrangelayers(Monitor *m)
}
}
void
autostartexec(void) {
const char *const *p;
size_t i = 0;
/* count entries */
for (p = autostart; *p; autostart_len++, p++)
while (*++p);
autostart_pids = calloc(autostart_len, sizeof(pid_t));
for (p = autostart; *p; i++, p++) {
if ((autostart_pids[i] = fork()) == 0) {
setsid();
execvp(*p, (char *const *)p);
die("dwl: execvp %s:", *p);
}
/* skip arguments */
while (*++p);
}
}
void
axisnotify(struct wl_listener *listener, void *data)
{
@@ -699,12 +765,23 @@ checkidleinhibitor(struct wlr_surface *exclude)
void
cleanup(void)
{
size_t i;
cleanuplisteners();
#ifdef XWAYLAND
wlr_xwayland_destroy(xwayland);
xwayland = NULL;
#endif
wl_display_destroy_clients(dpy);
/* kill child processes */
for (i = 0; i < autostart_len; i++) {
if (0 < autostart_pids[i]) {
kill(autostart_pids[i], SIGTERM);
waitpid(autostart_pids[i], NULL, 0);
}
}
if (child_pid > 0) {
kill(-child_pid, SIGTERM);
waitpid(child_pid, NULL, 0);
@@ -880,6 +957,9 @@ commitnotify(struct wl_listener *listener, void *data)
resize(c, c->geom, (c->isfloating && !c->isfullscreen));
if (c->scene_surface)
wlr_scene_node_for_each_buffer(&c->scene_surface->node, scenebuffersetopacity, c);
/* mark a pending resize as completed */
if (c->resize && c->resize <= c->surface.xdg->current.configure_serial)
c->resize = 0;
@@ -1055,6 +1135,11 @@ createmon(struct wl_listener *listener, void *data)
for (i = 0; i < LENGTH(m->layers); i++)
wl_list_init(&m->layers[i]);
m->gappih = gappih;
m->gappiv = gappiv;
m->gappoh = gappoh;
m->gappov = gappov;
wlr_output_state_init(&state);
/* Initialize monitor state using configured rules */
m->tagset[0] = m->tagset[1] = 1;
@@ -1127,6 +1212,7 @@ createnotify(struct wl_listener *listener, void *data)
c = toplevel->base->data = ecalloc(1, sizeof(*c));
c->surface.xdg = toplevel->base;
c->bw = borderpx;
c->opacity = default_opacity;
LISTEN(&toplevel->base->surface->events.commit, &c->commit, commitnotify);
LISTEN(&toplevel->base->surface->events.map, &c->map, mapnotify);
@@ -1237,6 +1323,12 @@ cursorwarptohint(void)
}
}
void
defaultgaps(const Arg *arg)
{
setgaps(gappoh, gappov, gappih, gappiv);
}
void
destroydecoration(struct wl_listener *listener, void *data)
{
@@ -1511,6 +1603,49 @@ focusstack(const Arg *arg)
focusclient(c, 1);
}
void
focusdir(const Arg *arg)
{
/* Focus the left, right, up, down client relative to the current focused client on selmon */
Client *c, *sel = focustop(selmon);
if (!sel || sel->isfullscreen)
return;
int dist=INT_MAX;
Client *newsel = NULL;
int newdist=INT_MAX;
wl_list_for_each(c, &clients, link) {
if (!VISIBLEON(c, selmon))
continue; /* skip non visible windows */
if (arg->ui == 0 && sel->geom.x <= c->geom.x) {
/* Client isn't on our left */
continue;
}
if (arg->ui == 1 && sel->geom.x >= c->geom.x) {
/* Client isn't on our right */
continue;
}
if (arg->ui == 2 && sel->geom.y <= c->geom.y) {
/* Client isn't above us */
continue;
}
if (arg->ui == 3 && sel->geom.y >= c->geom.y) {
/* Client isn't below us */
continue;
}
dist=abs(sel->geom.x-c->geom.x)+abs(sel->geom.y-c->geom.y);
if (dist < newdist){
newdist = dist;
newsel=c;
}
}
if (newsel != NULL){
focusclient(newsel, 1);
}
}
/* We probably should change the name of this: it sounds like it
* will focus the topmost client of this mon, when actually will
* only return that client */
@@ -1560,10 +1695,25 @@ gpureset(struct wl_listener *listener, void *data)
void
handlesig(int signo)
{
if (signo == SIGCHLD)
while (waitpid(-1, NULL, WNOHANG) > 0);
else if (signo == SIGINT || signo == SIGTERM)
if (signo == SIGCHLD) {
pid_t pid, *p, *lim;
while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
if (pid == child_pid)
child_pid = -1;
if (!(p = autostart_pids))
continue;
lim = &p[autostart_len];
for (; p < lim; p++) {
if (*p == pid) {
*p = -1;
break;
}
}
}
} else if (signo == SIGINT || signo == SIGTERM) {
quit(NULL);
}
}
void
@@ -1575,6 +1725,83 @@ incnmaster(const Arg *arg)
arrange(selmon);
}
void
incgaps(const Arg *arg)
{
setgaps(
selmon->gappoh + arg->i,
selmon->gappov + arg->i,
selmon->gappih + arg->i,
selmon->gappiv + arg->i
);
}
void
incigaps(const Arg *arg)
{
setgaps(
selmon->gappoh,
selmon->gappov,
selmon->gappih + arg->i,
selmon->gappiv + arg->i
);
}
void
incihgaps(const Arg *arg)
{
setgaps(
selmon->gappoh,
selmon->gappov,
selmon->gappih + arg->i,
selmon->gappiv
);
}
void
incivgaps(const Arg *arg)
{
setgaps(
selmon->gappoh,
selmon->gappov,
selmon->gappih,
selmon->gappiv + arg->i
);
}
void
incogaps(const Arg *arg)
{
setgaps(
selmon->gappoh + arg->i,
selmon->gappov + arg->i,
selmon->gappih,
selmon->gappiv
);
}
void
incohgaps(const Arg *arg)
{
setgaps(
selmon->gappoh + arg->i,
selmon->gappov,
selmon->gappih,
selmon->gappiv
);
}
void
incovgaps(const Arg *arg)
{
setgaps(
selmon->gappoh,
selmon->gappov + arg->i,
selmon->gappih,
selmon->gappiv
);
}
void
inputdevice(struct wl_listener *listener, void *data)
{
@@ -1819,6 +2046,145 @@ maximizenotify(struct wl_listener *listener, void *data)
wlr_xdg_surface_schedule_configure(c->surface.xdg);
}
void
menu(const Arg *arg)
{
FILE *f;
int fd_right[2], fd_left[2];
if (menu_current != NULL) {
wl_event_source_remove(menu_source);
close(menu_fd);
kill(menu_pid, SIGTERM);
menu_current = NULL;
if (!arg->v)
return;
}
if (pipe(fd_right) == -1 || pipe(fd_left) == -1)
return;
if ((menu_pid = fork()) == -1)
return;
if (menu_pid == 0) {
close(fd_right[1]);
close(fd_left[0]);
dup2(fd_right[0], STDIN_FILENO);
close(fd_right[0]);
dup2(fd_left[1], STDOUT_FILENO);
close(fd_left[1]);
execl("/bin/sh", "/bin/sh", "-c", ((Menu *)(arg->v))->cmd, NULL);
die("dwl: execl %s failed:", "/bin/sh");
}
close(fd_right[0]);
close(fd_left[1]);
menu_fd = fd_left[0];
if (fd_set_nonblock(menu_fd) == -1)
return;
if (!(f = fdopen(fd_right[1], "w")))
return;
menu_current = arg->v;
menu_current->feed(f);
fclose(f);
menu_source = wl_event_loop_add_fd(event_loop,
menu_fd, WL_EVENT_READABLE, menuread, NULL);
}
int
menuread(int fd, uint32_t mask, void *data)
{
char *s;
int n;
static char line[512];
static int i = 0;
if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
i = 0;
menu(&(const Arg){ .v = NULL });
return 0;
}
if ((n = read(menu_fd, line + i, LENGTH(line) - 1 - i)) == -1) {
if (errno != EAGAIN) {
i = 0;
menu(&(const Arg){ .v = NULL });
}
return 0;
}
line[i + n] = '\0';
if (!(s = strchr(line + i, '\n'))) {
i += n;
return 0;
}
i = 0;
*s = '\0';
menu_current->action(line);
return 0;
}
void
menuwinfeed(FILE *f)
{
Client *c;
const char *title, *appid;
wl_list_for_each(c, &fstack, flink) {
if (!(title = client_get_title(c)))
continue;
fprintf(f, "%s", title);
if ((appid = client_get_appid(c)))
fprintf(f, " | %s", appid);
fputc('\n', f);
}
}
void
menuwinaction(char *line)
{
Client *c;
const char *appid, *title;
static char buf[512];
wl_list_for_each(c, &fstack, flink) {
if (!(title = client_get_title(c)))
continue;
appid = client_get_appid(c);
snprintf(buf, LENGTH(buf) - 1, "%s%s%s",
title, appid ? " | " : "", appid ? appid : "");
if (strcmp(line, buf) == 0)
goto found;
}
return;
found:
if (!c->mon)
return;
wl_list_remove(&c->flink);
wl_list_insert(&fstack, &c->flink);
selmon = c->mon;
view(&(const Arg){ .ui = c->tags });
}
void
menulayoutfeed(FILE *f)
{
const Layout *l;
for (l = layouts; l < END(layouts); l++)
fprintf(f, "%s\n", l->symbol);
}
void
menulayoutaction(char *line)
{
const Layout *l;
for (l = layouts; l < END(layouts); l++)
if (strcmp(line, l->symbol) == 0)
goto found;
return;
found:
setlayout(&(const Arg){ .v = l });
}
void
monocle(Monitor *m)
{
@@ -1828,8 +2194,12 @@ monocle(Monitor *m)
wl_list_for_each(c, &clients, link) {
if (!VISIBLEON(c, m) || c->isfloating || c->isfullscreen)
continue;
resize(c, m->w, 0);
n++;
if (!monoclegaps)
resize(c, m->w, 0);
else
resize(c, (struct wlr_box){.x = m->w.x + gappoh, .y = m->w.y + gappov,
.width = m->w.width - 2 * gappoh, .height = m->w.height - 2 * gappov}, 0);
}
if (n)
snprintf(m->ltsymbol, LENGTH(m->ltsymbol), "[%d]", n);
@@ -2249,6 +2619,7 @@ run(char *startup_cmd)
die("startup: backend_start");
/* Now that the socket exists and the backend is started, run the startup command */
autostartexec();
if (startup_cmd) {
int piperw[2];
if (pipe(piperw) < 0)
@@ -2295,6 +2666,15 @@ run(char *startup_cmd)
wl_display_run(dpy);
}
void
scenebuffersetopacity(struct wlr_scene_buffer *buffer, int sx, int sy, void *data)
{
Client *c = data;
/* xdg-popups are children of Client.scene, we do not have to worry about
messing with them. */
wlr_scene_buffer_set_opacity(buffer, c->isfullscreen ? 1 : c->opacity);
}
void
setcursor(struct wl_listener *listener, void *data)
{
@@ -2363,10 +2743,21 @@ setfullscreen(Client *c, int fullscreen)
* client positions are set by the user and cannot be recalculated */
resize(c, c->prev, 0);
}
wlr_scene_node_for_each_buffer(&c->scene_surface->node, scenebuffersetopacity, c);
arrange(c->mon);
printstatus();
}
void
setgaps(int oh, int ov, int ih, int iv)
{
selmon->gappoh = MAX(oh, 0);
selmon->gappov = MAX(ov, 0);
selmon->gappih = MAX(ih, 0);
selmon->gappiv = MAX(iv, 0);
arrange(selmon);
}
void
setlayout(const Arg *arg)
{
@@ -2419,6 +2810,23 @@ setmon(Client *c, Monitor *m, uint32_t newtags)
focusclient(focustop(selmon), 1);
}
void
setopacity(const Arg *arg)
{
Client *sel = focustop(selmon);
if (!sel)
return;
sel->opacity += arg->f;
if (sel->opacity > 1.0)
sel->opacity = 1.0f;
if (sel->opacity < 0.1)
sel->opacity = 0.1f;
wlr_scene_node_for_each_buffer(&sel->scene_surface->node, scenebuffersetopacity, sel);
}
void
setpsel(struct wl_listener *listener, void *data)
{
@@ -2711,7 +3119,7 @@ tagmon(const Arg *arg)
void
tile(Monitor *m)
{
unsigned int mw, my, ty;
unsigned int mw, my, ty, h, r, oe = enablegaps, ie = enablegaps;
int i, n = 0;
Client *c;
@@ -2721,22 +3129,31 @@ tile(Monitor *m)
if (n == 0)
return;
if (smartgaps == n) {
oe = 0; // outer gaps disabled
}
if (n > m->nmaster)
mw = m->nmaster ? (int)roundf(m->w.width * m->mfact) : 0;
mw = m->nmaster ? (int)roundf((m->w.width + m->gappiv*ie) * m->mfact) : 0;
else
mw = m->w.width;
i = my = ty = 0;
mw = m->w.width - 2*m->gappov*oe + m->gappiv*ie;
i = 0;
my = ty = m->gappoh*oe;
wl_list_for_each(c, &clients, link) {
if (!VISIBLEON(c, m) || c->isfloating || c->isfullscreen)
continue;
if (i < m->nmaster) {
resize(c, (struct wlr_box){.x = m->w.x, .y = m->w.y + my, .width = mw,
.height = (m->w.height - my) / (MIN(n, m->nmaster) - i)}, 0);
my += c->geom.height;
r = MIN(n, m->nmaster) - i;
h = (m->w.height - my - m->gappoh*oe - m->gappih*ie * (r - 1)) / r;
resize(c, (struct wlr_box){.x = m->w.x + m->gappov*oe, .y = m->w.y + my,
.width = mw - m->gappiv*ie, .height = h}, 0);
my += c->geom.height + m->gappih*ie;
} else {
resize(c, (struct wlr_box){.x = m->w.x + mw, .y = m->w.y + ty,
.width = m->w.width - mw, .height = (m->w.height - ty) / (n - i)}, 0);
ty += c->geom.height;
r = n - i;
h = (m->w.height - ty - m->gappoh*oe - m->gappih*ie * (r - 1)) / r;
resize(c, (struct wlr_box){.x = m->w.x + mw + m->gappov*oe, .y = m->w.y + ty,
.width = m->w.width - mw - 2*m->gappov*oe, .height = h}, 0);
ty += c->geom.height + m->gappih*ie;
}
i++;
}
@@ -2759,6 +3176,13 @@ togglefullscreen(const Arg *arg)
setfullscreen(sel, !sel->isfullscreen);
}
void
togglegaps(const Arg *arg)
{
enablegaps = !enablegaps;
arrange(selmon);
}
void
toggletag(const Arg *arg)
{
@@ -3130,6 +3554,7 @@ createnotifyx11(struct wl_listener *listener, void *data)
c->surface.xwayland = xsurface;
c->type = X11;
c->bw = client_is_unmanaged(c) ? 0 : borderpx;
c->opacity = default_opacity;
/* Listen to the various events it can emit */
LISTEN(&xsurface->events.associate, &c->associate, associatex11);