some improvements

This commit is contained in:
λmolinae 2025-03-12 08:31:40 -06:00
parent 39a44a3442
commit 49732861f9
6 changed files with 230 additions and 32 deletions

179
dwl.c
View file

@ -69,6 +69,14 @@
#include <xcb/xcb_icccm.h>
#endif
#ifdef __FreeBSD__
#define __BSD_VISIBLE
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#endif
#include "util.h"
#include "drwl.h"
@ -76,12 +84,13 @@
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define CLEANMASK(mask) (mask & ~WLR_MODIFIER_CAPS)
#define VISIBLEON(C, M) ((M) && (C)->mon == (M) && (((C)->tags & (M)->tagset[(M)->seltags]) || C->issticky))
#define VISIBLEON(C, M) ((M) && (C)->mon == (M) && ((C)->tags & (M)->tagset[(M)->seltags]) && !(C)->swallowedby || C->issticky)
#define LENGTH(X) (sizeof X / sizeof X[0])
#define END(A) ((A) + LENGTH(A))
#define TAGMASK ((1u << LENGTH(tags)) - 1)
#define LISTEN(E, L, H) wl_signal_add((E), ((L)->notify = (H), (L)))
#define LISTEN_STATIC(E, H) do { static struct wl_listener _l = {.notify = (H)}; wl_signal_add((E), &_l); } while (0)
#define BORDERPX(C) (borderpx + ((C)->swallowing ? (C)->swallowing->bw : 0))
#define TEXTW(mon, text) (drwl_font_getwidth(mon->drw, text) + mon->lrpad)
/* enums */
@ -112,7 +121,8 @@ typedef struct {
typedef struct Pertag Pertag;
typedef struct Monitor Monitor;
typedef struct {
typedef struct Client Client;
struct Client {
/* Must keep these three elements in this order */
unsigned int type; /* XDGShell or X11* */
int interact;
@ -148,9 +158,13 @@ typedef struct {
#endif
unsigned int bw;
uint32_t tags;
int isfloating, isurgent, isfullscreen, issticky;
uint32_t resize; /* configure serial of a pending resize */
} Client;
int isfloating, isurgent, isfullscreen, issticky;
int isterm, noswallow;
uint32_t resize; /* configure serial of a pending resize */
pid_t pid;
Client *swallowing; /* client being hidden */
Client *swallowedby;
};
typedef struct {
const char *name;
@ -267,6 +281,8 @@ typedef struct {
const char *title;
uint32_t tags;
int isfloating;
int isterm;
int noswallow;
int monitor;
} Rule;
@ -377,6 +393,7 @@ static void nextlayout(const Arg *arg);
static void outputmgrapply(struct wl_listener *listener, void *data);
static void outputmgrapplyortest(struct wlr_output_configuration_v1 *config, int test);
static void outputmgrtest(struct wl_listener *listener, void *data);
static pid_t parentpid(pid_t pid);
static void pointerfocus(Client *c, struct wlr_surface *surface,
double sx, double sy, uint32_t time);
static void powermgrsetmode(struct wl_listener *listener, void *data);
@ -401,17 +418,21 @@ static void setmon(Client *c, Monitor *m, uint32_t newtags);
static void setpsel(struct wl_listener *listener, void *data);
static void setsel(struct wl_listener *listener, void *data);
static void setup(void);
static void swallow(Client *c, Client *toswallow);
static void spawn(const Arg *arg);
static void startdrag(struct wl_listener *listener, void *data);
static int statusin(int fd, unsigned int mask, void *data);
static void switchxkbrule(const Arg *arg);
static void tag(const Arg *arg);
static void tagmon(const Arg *arg);
static Client *termforwin(Client *c);
static void tile(Monitor *m);
static void togglebar(const Arg *arg);
static void togglefloating(const Arg *arg);
static void togglesticky(const Arg *arg);
static void togglefullscreen(const Arg *arg);
static void toggleswallow(const Arg *arg);
static void toggleautoswallow(const Arg *arg);
static void togglepointer(const Arg *arg);
static void togglegaps(const Arg *arg);
static void toggletag(const Arg *arg);
@ -577,11 +598,15 @@ applyrules(Client *c)
if (!(title = client_get_title(c)))
title = broken;
c->pid = client_get_pid(c);
for (r = rules; r < END(rules); r++) {
if ((!r->title || strstr(title, r->title))
&& (!r->id || strstr(appid, r->id))) {
c->isfloating = r->isfloating;
newtags |= r->tags;
c->isterm = r->isterm;
c->noswallow = r->noswallow;
i = 0;
wl_list_for_each(m, &mons, link) {
if (r->monitor == i++)
@ -593,6 +618,12 @@ applyrules(Client *c)
c->geom.x = (mon->w.width - c->geom.width) / 2 + mon->m.x;
c->geom.y = (mon->w.height - c->geom.height) / 2 + mon->m.y;
}
if (enableautoswallow && !c->noswallow && !c->isfloating &&
!c->surface.xdg->initial_commit) {
Client *p = termforwin(c);
if (p)
swallow(c, p);
}
setmon(c, mon, newtags);
}
@ -2697,6 +2728,29 @@ outputmgrtest(struct wl_listener *listener, void *data)
outputmgrapplyortest(config, 1);
}
pid_t
parentpid(pid_t pid)
{
#ifdef __linux__
unsigned int v = 0;
FILE *f;
char buf[256];
snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)pid);
if (!(f = fopen(buf, "r")))
return 0;
fscanf(f, "%*u %*s %*c %u", &v);
fclose(f);
return (pid_t)v;
#elif defined(__FreeBSD__)
struct kinfo_proc kip;
size_t len = sizeof(struct kinfo_proc);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, p };
if (sysctl(mib, 4, &kip, &len, NULL, 0) < 0 || len == 0)
return 0;
return kip.ki_ppid;
#endif
}
void
pointerfocus(Client *c, struct wlr_surface *surface, double sx, double sy,
uint32_t time)
@ -2974,7 +3028,7 @@ setfullscreen(Client *c, int fullscreen)
c->isfullscreen = fullscreen;
if (!c->mon || !client_surface(c)->mapped)
return;
c->bw = fullscreen ? 0 : borderpx;
c->bw = fullscreen ? 0 : BORDERPX(c);
client_set_fullscreen(c, fullscreen);
wlr_scene_node_reparent(&c->scene->node, layers[c->isfullscreen
? LyrFS : c->isfloating ? LyrFloat : LyrTile]);
@ -3073,6 +3127,9 @@ setmon(Client *c, Monitor *m, uint32_t newtags)
setfloating(c, c->isfloating);
}
focusclient(focustop(selmon), 1);
if (c->swallowing)
setmon(c->swallowing, m, newtags);
}
void
@ -3170,7 +3227,8 @@ setup(void)
wlr_export_dmabuf_manager_v1_create(dpy);
wlr_screencopy_manager_v1_create(dpy);
wlr_data_control_manager_v1_create(dpy);
wlr_primary_selection_v1_device_manager_create(dpy);
if (enable_primary_selection)
wlr_primary_selection_v1_device_manager_create(dpy);
wlr_viewporter_create(dpy);
wlr_single_pixel_buffer_manager_v1_create(dpy);
wlr_fractional_scale_manager_v1_create(dpy, 1);
@ -3294,7 +3352,8 @@ setup(void)
seat = wlr_seat_create(dpy, "seat0");
LISTEN_STATIC(&seat->events.request_set_cursor, setcursor);
LISTEN_STATIC(&seat->events.request_set_selection, setsel);
LISTEN_STATIC(&seat->events.request_set_primary_selection, setpsel);
if (enable_primary_selection)
LISTEN_STATIC(&seat->events.request_set_primary_selection, setpsel);
LISTEN_STATIC(&seat->events.request_start_drag, requeststartdrag);
LISTEN_STATIC(&seat->events.start_drag, startdrag);
@ -3392,6 +3451,44 @@ switchxkbrule(const Arg *arg)
xkb_context_unref(context);
}
void
swallow(Client *c, Client *toswallow)
{
/* Do not allow a client to swallow itself */
if (c == toswallow)
return;
/* Swallow */
if (toswallow && !c->swallowing) {
c->swallowing = toswallow;
toswallow->swallowedby = c;
toswallow->mon = c->mon;
toswallow->mon = c->mon;
wl_list_remove(&c->link);
wl_list_insert(&c->swallowing->link, &c->link);
wl_list_remove(&c->flink);
wl_list_insert(&c->swallowing->flink, &c->flink);
c->bw = BORDERPX(c);
c->tags = toswallow->tags;
c->isfloating = toswallow->isfloating;
c->geom = toswallow->geom;
setfullscreen(toswallow, 0);
}
/* Unswallow */
else if (c->swallowing) {
wl_list_remove(&c->swallowing->link);
wl_list_insert(&c->link, &c->swallowing->link);
wl_list_remove(&c->swallowing->flink);
wl_list_insert(&c->flink, &c->swallowing->flink);
c->swallowing->tags = c->tags;
c->swallowing->swallowedby = NULL;
c->swallowing = NULL;
c->bw = BORDERPX(c);
setfullscreen(c, 0);
}
}
void
tag(const Arg *arg)
{
@ -3413,6 +3510,40 @@ tagmon(const Arg *arg)
setmon(sel, dirtomon(arg->i), 0);
}
Client *
termforwin(Client *c)
{
Client *p;
pid_t pid;
pid_t pids[32];
size_t i, pids_len;
if (!c->pid || c->isterm)
return NULL;
/* Get all parent pids */
pids_len = 0;
pid = c->pid;
while (pids_len < LENGTH(pids)) {
pid = parentpid(pid);
if (!pid)
break;
pids[pids_len++] = pid;
}
/* Find closest parent */
for (i = 0; i < pids_len; i++) {
wl_list_for_each(p, &clients, link) {
if (!p->pid || !p->isterm || p->swallowedby)
continue;
if (pids[i] == p->pid)
return p;
}
}
return NULL;
}
void
tile(Monitor *m)
{
@ -3501,6 +3632,32 @@ togglesticky(const Arg *arg)
setsticky(c, !c->issticky);
}
void
toggleswallow(const Arg *arg)
{
Client *c, *sel = focustop(selmon);
if (!sel)
return;
if (sel->swallowing) {
swallow(sel, NULL);
} else {
wl_list_for_each(c, &sel->flink, flink) {
if (&c->flink == &fstack)
continue; /* wrap past the sentinel node */
if (VISIBLEON(c, selmon))
break; /* found it */
}
swallow(sel, c);
}
}
void
toggleautoswallow(const Arg *arg)
{
enableautoswallow = !enableautoswallow;
}
void
toggletag(const Arg *arg)
{
@ -3581,6 +3738,12 @@ unmapnotify(struct wl_listener *listener, void *data)
grabc = NULL;
}
if (c->swallowing) {
swallow(c, NULL);
} else if (c->swallowedby) {
swallow(c->swallowedby, NULL);
}
if (client_is_unmanaged(c)) {
if (c == exclusive_focus) {
exclusive_focus = NULL;