Massive code refactor

This commit is contained in:
2026-07-02 01:24:16 -04:00
parent c10722aa69
commit b5ccb2621b
22 changed files with 3277 additions and 2760 deletions
+97 -395
View File
@@ -1,404 +1,106 @@
/*
* Attempt to consolidate unavoidable suck into one file, away from peachwm.c. This
* file is not meant to be pretty. We use a .h file with static inline
* functions instead of a separate .c module, or function pointers like sway, so
* that they will simply compile out if the chosen #defines leave them unused.
*/
#ifndef PEACHWM_CLIENT_H
#define PEACHWM_CLIENT_H
/* Leave these functions first; they're used in the others */
static inline int
client_is_x11(Client *c)
{
#include <wayland-server-core.h>
#include <wlr/util/box.h>
#include "monitor.h"
/* Forward declarations for pointer-only types */
struct wlr_surface;
struct wlr_keyboard;
struct wlr_scene_tree;
struct wlr_scene_rect;
struct wlr_scene_layer_surface_v1;
struct wlr_layer_surface_v1;
struct wlr_xdg_surface;
struct wlr_xwayland_surface;
struct wlr_xdg_toplevel_decoration_v1;
/* Client types */
enum { XDGShell, LayerShell, X11 };
typedef struct Client {
/* Must keep this field first */
unsigned int type; /* XDGShell or X11* */
Monitor *mon;
struct wlr_scene_tree *scene;
struct wlr_scene_rect *border[4]; /* top, bottom, left, right */
struct wlr_scene_tree *scene_surface;
struct wl_list link;
struct wl_list flink;
struct wlr_box geom; /* layout-relative, includes border */
struct wlr_box prev; /* layout-relative, includes border */
struct wlr_box bounds; /* only width and height are used */
union {
struct wlr_xdg_surface *xdg;
struct wlr_xwayland_surface *xwayland;
} surface;
struct wlr_xdg_toplevel_decoration_v1 *decoration;
struct wl_listener commit;
struct wl_listener map;
struct wl_listener maximize;
struct wl_listener unmap;
struct wl_listener destroy;
struct wl_listener set_title;
struct wl_listener fullscreen;
struct wl_listener set_decoration_mode;
struct wl_listener destroy_decoration;
#ifdef XWAYLAND
return c->type == X11;
struct wl_listener activate;
struct wl_listener associate;
struct wl_listener dissociate;
struct wl_listener configure;
struct wl_listener set_hints;
#endif
return 0;
}
unsigned int bw;
uint32_t tags;
int isfloating, isurgent, isfullscreen, isscratchpad;
uint32_t resize; /* configure serial of a pending resize */
} Client;
static inline struct wlr_surface *
client_surface(Client *c)
{
#ifdef XWAYLAND
if (client_is_x11(c))
return c->surface.xwayland->surface;
#endif
return c->surface.xdg->surface;
}
typedef struct {
/* Must keep this field first */
unsigned int type; /* LayerShell */
static inline int
toplevel_from_wlr_surface(struct wlr_surface *s, Client **pc, LayerSurface **pl)
{
struct wlr_xdg_surface *xdg_surface, *tmp_xdg_surface;
struct wlr_surface *root_surface;
Monitor *mon;
struct wlr_scene_tree *scene;
struct wlr_scene_tree *popups;
struct wlr_scene_layer_surface_v1 *scene_layer;
struct wl_list link;
int mapped;
struct wlr_layer_surface_v1 *layer_surface;
Client *c = NULL;
LayerSurface *l = NULL;
int type = -1;
#ifdef XWAYLAND
struct wlr_xwayland_surface *xsurface;
#endif
if (!s)
return -1;
root_surface = wlr_surface_get_root_surface(s);
struct wl_listener destroy;
struct wl_listener unmap;
struct wl_listener surface_commit;
} LayerSurface;
#ifdef XWAYLAND
if ((xsurface = wlr_xwayland_surface_try_from_wlr_surface(root_surface))) {
c = xsurface->data;
type = c->type;
goto end;
}
#endif
/* Function declarations */
int client_is_x11(Client *c);
struct wlr_surface *client_surface(Client *c);
int toplevel_from_wlr_surface(struct wlr_surface *s, Client **pc, LayerSurface **pl);
void client_activate_surface(struct wlr_surface *s, int activated);
uint32_t client_set_bounds(Client *c, int32_t width, int32_t height);
const char *client_get_appid(Client *c);
void client_get_clip(Client *c, struct wlr_box *clip);
void client_get_geometry(Client *c, struct wlr_box *geom);
Client *client_get_parent(Client *c);
int client_has_children(Client *c);
const char *client_get_title(Client *c);
int client_is_float_type(Client *c);
int client_is_rendered_on_mon(Client *c, Monitor *m);
int client_is_stopped(Client *c);
int client_is_unmanaged(Client *c);
void client_notify_enter(struct wlr_surface *s, struct wlr_keyboard *kb);
void client_send_close(Client *c);
void client_set_border_color(Client *c, const float color[static 4]);
void client_set_fullscreen(Client *c, int fullscreen);
void client_set_scale(struct wlr_surface *s, float scale);
uint32_t client_set_size(Client *c, uint32_t width, uint32_t height);
void client_set_tiled(Client *c, uint32_t edges);
void client_set_suspended(Client *c, int suspended);
int client_wants_focus(Client *c);
int client_wants_fullscreen(Client *c);
if ((layer_surface = wlr_layer_surface_v1_try_from_wlr_surface(root_surface))) {
l = layer_surface->data;
type = LayerShell;
goto end;
}
xdg_surface = wlr_xdg_surface_try_from_wlr_surface(root_surface);
while (xdg_surface) {
tmp_xdg_surface = NULL;
switch (xdg_surface->role) {
case WLR_XDG_SURFACE_ROLE_POPUP:
if (!xdg_surface->popup || !xdg_surface->popup->parent)
return -1;
tmp_xdg_surface = wlr_xdg_surface_try_from_wlr_surface(xdg_surface->popup->parent);
if (!tmp_xdg_surface)
return toplevel_from_wlr_surface(xdg_surface->popup->parent, pc, pl);
xdg_surface = tmp_xdg_surface;
break;
case WLR_XDG_SURFACE_ROLE_TOPLEVEL:
c = xdg_surface->data;
type = c->type;
goto end;
case WLR_XDG_SURFACE_ROLE_NONE:
return -1;
}
}
end:
if (pl)
*pl = l;
if (pc)
*pc = c;
return type;
}
/* The others */
static inline void
client_activate_surface(struct wlr_surface *s, int activated)
{
struct wlr_xdg_toplevel *toplevel;
#ifdef XWAYLAND
struct wlr_xwayland_surface *xsurface;
if ((xsurface = wlr_xwayland_surface_try_from_wlr_surface(s))) {
wlr_xwayland_surface_activate(xsurface, activated);
return;
}
#endif
if ((toplevel = wlr_xdg_toplevel_try_from_wlr_surface(s)))
wlr_xdg_toplevel_set_activated(toplevel, activated);
}
static inline uint32_t
client_set_bounds(Client *c, int32_t width, int32_t height)
{
#ifdef XWAYLAND
if (client_is_x11(c))
return 0;
#endif
if (wl_resource_get_version(c->surface.xdg->toplevel->resource) >=
XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE && width >= 0 && height >= 0
&& (c->bounds.width != width || c->bounds.height != height)) {
c->bounds.width = width;
c->bounds.height = height;
return wlr_xdg_toplevel_set_bounds(c->surface.xdg->toplevel, width, height);
}
return 0;
}
static inline const char *
client_get_appid(Client *c)
{
#ifdef XWAYLAND
if (client_is_x11(c))
return c->surface.xwayland->class ? c->surface.xwayland->class : "broken";
#endif
return c->surface.xdg->toplevel->app_id ? c->surface.xdg->toplevel->app_id : "broken";
}
static inline void
client_get_clip(Client *c, struct wlr_box *clip)
{
*clip = (struct wlr_box){
.x = 0,
.y = 0,
.width = c->geom.width - c->bw,
.height = c->geom.height - c->bw,
};
#ifdef XWAYLAND
if (client_is_x11(c))
return;
#endif
clip->x = c->surface.xdg->geometry.x;
clip->y = c->surface.xdg->geometry.y;
}
static inline void
client_get_geometry(Client *c, struct wlr_box *geom)
{
#ifdef XWAYLAND
if (client_is_x11(c)) {
geom->x = c->surface.xwayland->x;
geom->y = c->surface.xwayland->y;
geom->width = c->surface.xwayland->width;
geom->height = c->surface.xwayland->height;
return;
}
#endif
*geom = c->surface.xdg->geometry;
}
static inline Client *
client_get_parent(Client *c)
{
Client *p = NULL;
#ifdef XWAYLAND
if (client_is_x11(c)) {
if (c->surface.xwayland->parent)
toplevel_from_wlr_surface(c->surface.xwayland->parent->surface, &p, NULL);
return p;
}
#endif
if (c->surface.xdg->toplevel->parent)
toplevel_from_wlr_surface(c->surface.xdg->toplevel->parent->base->surface, &p, NULL);
return p;
}
static inline int
client_has_children(Client *c)
{
#ifdef XWAYLAND
if (client_is_x11(c))
return !wl_list_empty(&c->surface.xwayland->children);
#endif
/* surface.xdg->link is never empty because it always contains at least the
* surface itself. */
return wl_list_length(&c->surface.xdg->link) > 1;
}
static inline const char *
client_get_title(Client *c)
{
#ifdef XWAYLAND
if (client_is_x11(c))
return c->surface.xwayland->title ? c->surface.xwayland->title : "broken";
#endif
return c->surface.xdg->toplevel->title ? c->surface.xdg->toplevel->title : "broken";
}
static inline int
client_is_float_type(Client *c)
{
struct wlr_xdg_toplevel *toplevel;
struct wlr_xdg_toplevel_state state;
#ifdef XWAYLAND
if (client_is_x11(c)) {
struct wlr_xwayland_surface *surface = c->surface.xwayland;
xcb_size_hints_t *size_hints = surface->size_hints;
if (surface->modal)
return 1;
if (wlr_xwayland_surface_has_window_type(surface, WLR_XWAYLAND_NET_WM_WINDOW_TYPE_DIALOG)
|| wlr_xwayland_surface_has_window_type(surface, WLR_XWAYLAND_NET_WM_WINDOW_TYPE_SPLASH)
|| wlr_xwayland_surface_has_window_type(surface, WLR_XWAYLAND_NET_WM_WINDOW_TYPE_TOOLBAR)
|| wlr_xwayland_surface_has_window_type(surface, WLR_XWAYLAND_NET_WM_WINDOW_TYPE_UTILITY)) {
return 1;
}
return size_hints && size_hints->min_width > 0 && size_hints->min_height > 0
&& (size_hints->max_width == size_hints->min_width
|| size_hints->max_height == size_hints->min_height);
}
#endif
toplevel = c->surface.xdg->toplevel;
state = toplevel->current;
return toplevel->parent || (state.min_width != 0 && state.min_height != 0
&& (state.min_width == state.max_width
|| state.min_height == state.max_height));
}
static inline int
client_is_rendered_on_mon(Client *c, Monitor *m)
{
/* This is needed for when you don't want to check formal assignment,
* but rather actual displaying of the pixels.
* Usually VISIBLEON suffices and is also faster. */
struct wlr_surface_output *s;
int unused_lx, unused_ly;
if (!wlr_scene_node_coords(&c->scene->node, &unused_lx, &unused_ly))
return 0;
wl_list_for_each(s, &client_surface(c)->current_outputs, link)
if (s->output == m->wlr_output)
return 1;
return 0;
}
static inline int
client_is_stopped(Client *c)
{
int pid;
siginfo_t in = {0};
#ifdef XWAYLAND
if (client_is_x11(c))
return 0;
#endif
wl_client_get_credentials(c->surface.xdg->client->client, &pid, NULL, NULL);
if (waitid(P_PID, pid, &in, WNOHANG|WCONTINUED|WSTOPPED|WNOWAIT) < 0) {
/* This process is not our child process, while is very unlikely that
* it is stopped, in order to do not skip frames, assume that it is. */
if (errno == ECHILD)
return 1;
} else if (in.si_pid) {
if (in.si_code == CLD_STOPPED || in.si_code == CLD_TRAPPED)
return 1;
if (in.si_code == CLD_CONTINUED)
return 0;
}
return 0;
}
static inline int
client_is_unmanaged(Client *c)
{
#ifdef XWAYLAND
if (client_is_x11(c))
return c->surface.xwayland->override_redirect;
#endif
return 0;
}
static inline void
client_notify_enter(struct wlr_surface *s, struct wlr_keyboard *kb)
{
if (kb)
wlr_seat_keyboard_notify_enter(seat, s, kb->keycodes,
kb->num_keycodes, &kb->modifiers);
else
wlr_seat_keyboard_notify_enter(seat, s, NULL, 0, NULL);
}
static inline void
client_send_close(Client *c)
{
#ifdef XWAYLAND
if (client_is_x11(c)) {
wlr_xwayland_surface_close(c->surface.xwayland);
return;
}
#endif
wlr_xdg_toplevel_send_close(c->surface.xdg->toplevel);
}
static inline void
client_set_border_color(Client *c, const float color[static 4])
{
int i;
for (i = 0; i < 4; i++)
wlr_scene_rect_set_color(c->border[i], color);
}
static inline void
client_set_fullscreen(Client *c, int fullscreen)
{
#ifdef XWAYLAND
if (client_is_x11(c)) {
wlr_xwayland_surface_set_fullscreen(c->surface.xwayland, fullscreen);
return;
}
#endif
wlr_xdg_toplevel_set_fullscreen(c->surface.xdg->toplevel, fullscreen);
}
static inline void
client_set_scale(struct wlr_surface *s, float scale)
{
wlr_fractional_scale_v1_notify_scale(s, scale);
wlr_surface_set_preferred_buffer_scale(s, (int32_t)ceilf(scale));
}
static inline uint32_t
client_set_size(Client *c, uint32_t width, uint32_t height)
{
#ifdef XWAYLAND
if (client_is_x11(c)) {
wlr_xwayland_surface_configure(c->surface.xwayland,
c->geom.x + c->bw, c->geom.y + c->bw, width, height);
return 0;
}
#endif
if ((int32_t)width == c->surface.xdg->toplevel->current.width
&& (int32_t)height == c->surface.xdg->toplevel->current.height)
return 0;
return wlr_xdg_toplevel_set_size(c->surface.xdg->toplevel, (int32_t)width, (int32_t)height);
}
static inline void
client_set_tiled(Client *c, uint32_t edges)
{
#ifdef XWAYLAND
if (client_is_x11(c)) {
wlr_xwayland_surface_set_maximized(c->surface.xwayland,
edges != WLR_EDGE_NONE, edges != WLR_EDGE_NONE);
return;
}
#endif
if (wl_resource_get_version(c->surface.xdg->toplevel->resource)
>= XDG_TOPLEVEL_STATE_TILED_RIGHT_SINCE_VERSION) {
wlr_xdg_toplevel_set_tiled(c->surface.xdg->toplevel, edges);
} else {
wlr_xdg_toplevel_set_maximized(c->surface.xdg->toplevel, edges != WLR_EDGE_NONE);
}
}
static inline void
client_set_suspended(Client *c, int suspended)
{
#ifdef XWAYLAND
if (client_is_x11(c))
return;
#endif
wlr_xdg_toplevel_set_suspended(c->surface.xdg->toplevel, suspended);
}
static inline int
client_wants_focus(Client *c)
{
#ifdef XWAYLAND
return client_is_unmanaged(c)
&& wlr_xwayland_surface_override_redirect_wants_focus(c->surface.xwayland)
&& wlr_xwayland_surface_icccm_input_model(c->surface.xwayland) != WLR_ICCCM_INPUT_MODEL_NONE;
#endif
return 0;
}
static inline int
client_wants_fullscreen(Client *c)
{
#ifdef XWAYLAND
if (client_is_x11(c))
return c->surface.xwayland->fullscreen;
#endif
return c->surface.xdg->toplevel->requested.fullscreen;
}
#endif /* PEACHWM_CLIENT_H */
-210
View File
@@ -1,210 +0,0 @@
/* My shitty port of Mango's ext-workspace.h for my compositor;
* thanks DreamMaoMao for doing most of the heavy lifting
* by actually writing the thing, it didn't work the first
* few tries and I barely have any idea how this code works anyway.
* To say that it's the eighth wonder of the world
* would be an understatement
*/
#include "wlr_ext_workspace_v1.h"
#define EXT_WORKSPACE_CAPS \
(EXT_WORKSPACE_HANDLE_V1_WORKSPACE_CAPABILITIES_ACTIVATE | \
EXT_WORKSPACE_HANDLE_V1_WORKSPACE_CAPABILITIES_DEACTIVATE)
/* types */
struct ext_workspace {
struct wl_list link;
uint32_t tag;
Monitor *m;
struct wlr_ext_workspace_handle_v1 *handle;
};
/* global */
static struct wlr_ext_workspace_manager_v1 *ext_workspace_manager;
static struct wl_list ext_workspaces;
static struct wl_listener ext_commit_listener;
static struct wl_listener ext_destroy_listener;
/* helpers */
static int
get_tag_status(uint32_t tag, Monitor *m)
{
Client *c;
uint32_t mask = 1 << (tag - 1);
wl_list_for_each(c, &clients, link) {
if (c->mon != m)
continue;
if (!(c->tags & mask))
continue;
if (c->isurgent)
return 2;
return 1;
}
return 0;
}
static const char *
tag_name(uint32_t tag)
{
static const char *names[] = {
"1", "2", "3", "4", "5", "6", "7", "8", "9",
};
if (tag >= 1 && tag <= LENGTH(names))
return names[tag - 1];
return "?";
}
/* workspace lifecycle */
static void
ext_workspace_destroy(struct ext_workspace *ws)
{
wlr_ext_workspace_handle_v1_destroy(ws->handle);
wl_list_remove(&ws->link);
free(ws);
}
static void
ext_workspace_create(uint32_t tag, Monitor *m)
{
const char *name = tag_name(tag);
struct ext_workspace *ws = ecalloc(1, sizeof(*ws));
ws->tag = tag;
ws->m = m;
ws->handle = wlr_ext_workspace_handle_v1_create(
ext_workspace_manager, name, EXT_WORKSPACE_CAPS);
ws->handle->data = ws;
wlr_ext_workspace_handle_v1_set_group(ws->handle, m->ext_group);
wlr_ext_workspace_handle_v1_set_name(ws->handle, name);
wl_list_insert(ext_workspaces.prev, &ws->link);
}
/* monitor integration */
static void
ext_workspace_createmon(Monitor *m)
{
uint32_t i;
m->ext_group = wlr_ext_workspace_group_handle_v1_create(
ext_workspace_manager, 0);
wlr_ext_workspace_group_handle_v1_output_enter(
m->ext_group, m->wlr_output);
for (i = 1; i <= TAGCOUNT; i++)
ext_workspace_create(i, m);
}
static void
ext_workspace_cleanupmon(Monitor *m)
{
struct ext_workspace *ws, *tmp;
wl_list_for_each_safe(ws, tmp, &ext_workspaces, link)
if (ws->m == m)
ext_workspace_destroy(ws);
wlr_ext_workspace_group_handle_v1_output_leave(
m->ext_group, m->wlr_output);
wlr_ext_workspace_group_handle_v1_destroy(m->ext_group);
m->ext_group = NULL;
}
/* status */
static void
ext_workspace_printstatus(Monitor *m)
{
struct ext_workspace *ws;
wl_list_for_each(ws, &ext_workspaces, link) {
if (ws->m != m)
continue;
int status = get_tag_status(ws->tag, m);
int active = !!(m->tagset[m->seltags] & (1 << (ws->tag - 1)) & TAGMASK);
wlr_ext_workspace_handle_v1_set_urgent(ws->handle, status == 2);
wlr_ext_workspace_handle_v1_set_hidden(ws->handle, 0);
wlr_ext_workspace_handle_v1_set_active(ws->handle, active);
}
}
/* commit handler */
static void
handle_ext_commit(struct wl_listener *listener, void *data)
{
struct wlr_ext_workspace_v1_commit_event *event = data;
struct wlr_ext_workspace_v1_request *req;
wl_list_for_each(req, event->requests, link) {
struct ext_workspace *ws = NULL;
struct ext_workspace *w;
switch (req->type) {
case WLR_EXT_WORKSPACE_V1_REQUEST_ACTIVATE:
if (!req->activate.workspace)
break;
wl_list_for_each(w, &ext_workspaces, link) {
if (w->handle == req->activate.workspace) {
ws = w;
break;
}
}
if (!ws)
break;
view(&(Arg){.ui = 1 << (ws->tag - 1)});
break;
case WLR_EXT_WORKSPACE_V1_REQUEST_DEACTIVATE:
if (!req->deactivate.workspace)
break;
wl_list_for_each(w, &ext_workspaces, link) {
if (w->handle == req->deactivate.workspace) {
ws = w;
break;
}
}
if (!ws)
break;
toggleview(&(Arg){.ui = 1 << (ws->tag - 1)});
break;
default:
break;
}
}
}
/* init */
static void
handle_ext_destroy(struct wl_listener *listener, void *data)
{
wl_list_remove(&ext_commit_listener.link);
wl_list_remove(&ext_destroy_listener.link);
}
static void
workspaces_init(void)
{
ext_workspace_manager = wlr_ext_workspace_manager_v1_create(dpy, 1);
wl_list_init(&ext_workspaces);
ext_commit_listener.notify = handle_ext_commit;
wl_signal_add(&ext_workspace_manager->events.commit,
&ext_commit_listener);
ext_destroy_listener.notify = handle_ext_destroy;
wl_signal_add(&ext_workspace_manager->events.destroy,
&ext_destroy_listener);
}
+23
View File
@@ -0,0 +1,23 @@
#ifndef PEACHWM_EXT_WORKSPACE_H
#define PEACHWM_EXT_WORKSPACE_H
#include "monitor.h"
#define EXT_WORKSPACE_CAPS \
(EXT_WORKSPACE_HANDLE_V1_WORKSPACE_CAPABILITIES_ACTIVATE | \
EXT_WORKSPACE_HANDLE_V1_WORKSPACE_CAPABILITIES_DEACTIVATE)
struct ext_workspace {
struct wl_list link;
uint32_t tag;
Monitor *m;
struct wlr_ext_workspace_handle_v1 *handle;
};
/* Functions */
void ext_workspace_createmon(Monitor *m);
void ext_workspace_cleanupmon(Monitor *m);
void ext_workspace_printstatus(Monitor *m);
void workspaces_init(void);
#endif /* PEACHWM_EXT_WORKSPACE_H */
+30 -272
View File
@@ -1,18 +1,36 @@
#pragma once
#include "peachwm-ipc-unstable-v2-protocol.h"
#include "monitor.h"
/* Forward declarations for peachwm.c static functions */
/* macros needed by IPC */
#ifndef TAGCOUNT
#define TAGCOUNT 9
#endif
#define LENGTH(X) (sizeof X / sizeof X[0])
#define TAGMASK ((1u << TAGCOUNT) - 1)
static void arrange(Monitor *m);
static void focusclient(Client *c, int lift);
static Client *focustop(Monitor *m);
static void printstatus(void);
/* Forward declarations from peachwm.c */
/* globals */
extern struct wl_display *dpy;
extern struct wl_list mons;
extern Monitor *selmon;
extern struct wl_list clients;
extern const Layout layouts[];
extern const unsigned int layout_count;
static struct wl_global *ipc_global = NULL;
/* All bound manager objects */
static struct wl_list ipc_managers; /* IpcManager.link */
/* Functions from peachwm.c called by IPC */
void arrange(Monitor *m);
void focusclient(Client *c, int lift);
Client *focustop(Monitor *m);
void printstatus(void);
const Layout *curlayout(Monitor *m);
int current_tag_idx(Monitor *m);
/* IPC-specific globals */
extern struct wl_global *ipc_global;
extern struct wl_list ipc_managers;
/* Types */
typedef struct {
struct wl_resource *resource;
struct wl_list link; /* ipc_managers */
@@ -25,266 +43,6 @@ typedef struct {
struct wl_list link; /* IpcManager.outputs */
} IpcOutput;
/* helpers */
static IpcOutput *
ipc_output_for_mon(IpcManager *mgr, Monitor *m)
{
IpcOutput *o;
wl_list_for_each(o, &mgr->outputs, link)
if (o->mon == m)
return o;
return NULL;
}
/* Send the full state of monitor m to one IpcOutput resource */
static void
ipc_send_output_state(IpcOutput *ipc_out)
{
Monitor *m = ipc_out->mon;
struct wl_resource *res = ipc_out->resource;
Client *c;
uint32_t occ = 0, urg = 0, sel_tags = 0;
int tag, focused;
/* Compute occupancy/urgency (same logic as printstatus, I think) */
wl_list_for_each(c, &clients, link) {
if (c->mon != m)
continue;
occ |= c->tags;
if (c->isurgent)
urg |= c->tags;
}
c = focustop(m);
sel_tags = c ? c->tags : 0;
/* active */
zpeachwm_ipc_output_v2_send_active(res, m == selmon ? 1 : 0);
/* per-tag state */
for (tag = 0; tag < TAGCOUNT; tag++) {
uint32_t mask = 1u << tag;
uint32_t state = ZPEACHWM_IPC_OUTPUT_V2_TAG_STATE_NONE;
uint32_t clients_on_tag = 0;
if (m->tagset[m->seltags] & mask)
state = ZPEACHWM_IPC_OUTPUT_V2_TAG_STATE_ACTIVE;
if (urg & mask)
state = ZPEACHWM_IPC_OUTPUT_V2_TAG_STATE_URGENT;
if (occ & mask)
clients_on_tag = 1; /* report ≥1, not exact count */
focused = (sel_tags & mask) ? 1 : 0;
zpeachwm_ipc_output_v2_send_tag(res, tag, state, clients_on_tag, focused);
}
/* layout index */
{
uint32_t i;
for (i = 0; i < LENGTH(layouts); i++)
if (&layouts[i] == curlayout(m))
break;
zpeachwm_ipc_output_v2_send_layout(res, i);
}
/* layout symbol */
zpeachwm_ipc_output_v2_send_layout_symbol(res, m->ltsymbol[current_tag_idx(m)]);
/* title/appid/fullscreen/floating */
if (c) {
zpeachwm_ipc_output_v2_send_title(res, client_get_title(c));
zpeachwm_ipc_output_v2_send_appid(res, client_get_appid(c));
if (wl_resource_get_version(res) >= 2) {
zpeachwm_ipc_output_v2_send_fullscreen(res, c->isfullscreen ? 1 : 0);
zpeachwm_ipc_output_v2_send_floating(res, c->isfloating ? 1 : 0);
}
} else {
zpeachwm_ipc_output_v2_send_title(res, "");
zpeachwm_ipc_output_v2_send_appid(res, "");
if (wl_resource_get_version(res) >= 2) {
zpeachwm_ipc_output_v2_send_fullscreen(res, 0);
zpeachwm_ipc_output_v2_send_floating(res, 0);
}
}
/* frame — signals end of this batch of events */
zpeachwm_ipc_output_v2_send_frame(res);
}
/* Called instead of (or alongside) the old printstatus() */
static void
ipc_printstatus(void)
{
IpcManager *mgr;
Monitor *m;
wl_list_for_each(mgr, &ipc_managers, link) {
wl_list_for_each(m, &mons, link) {
IpcOutput *out = ipc_output_for_mon(mgr, m);
if (out)
ipc_send_output_state(out);
}
}
}
/* zpeachwm_ipc_output_v2 requests */
static void
ipc_output_handle_release(struct wl_client *client, struct wl_resource *resource)
{
wl_resource_destroy(resource);
}
static void
ipc_output_handle_set_tags(struct wl_client *client, struct wl_resource *resource,
uint32_t tagmask, uint32_t toggle_tagset)
{
IpcOutput *out = wl_resource_get_user_data(resource);
if (!out) return;
Monitor *m = out->mon;
if (!m) return;
if (toggle_tagset)
m->seltags ^= 1;
m->tagset[m->seltags] = tagmask & TAGMASK;
focusclient(focustop(m), 1);
arrange(m);
printstatus();
}
static void
ipc_output_handle_set_client_tags(struct wl_client *client,
struct wl_resource *resource, uint32_t and_tags, uint32_t xor_tags)
{
IpcOutput *out = wl_resource_get_user_data(resource);
if (!out) return;
Monitor *m = out->mon;
Client *sel;
uint32_t newtags;
if (!m) return;
sel = focustop(m);
if (!sel) return;
newtags = (sel->tags & and_tags) ^ xor_tags;
if (!newtags) return;
sel->tags = newtags;
focusclient(focustop(m), 1);
arrange(m);
printstatus();
}
static void
ipc_output_handle_set_layout(struct wl_client *client,
struct wl_resource *resource, uint32_t idx)
{
IpcOutput *out = wl_resource_get_user_data(resource);
if (!out) return;
Monitor *m = out->mon;
if (!m || idx >= LENGTH(layouts)) return;
int ti = current_tag_idx(m);
if (m->lt[ti][m->sellt[ti]] != &layouts[idx])
m->sellt[ti] ^= 1;
m->lt[ti][m->sellt[ti]] = &layouts[idx];
strncpy(m->ltsymbol[ti], layouts[idx].symbol, LENGTH(m->ltsymbol[ti]));
arrange(m);
printstatus();
}
static const struct zpeachwm_ipc_output_v2_interface ipc_output_impl = {
.release = ipc_output_handle_release,
.set_tags = ipc_output_handle_set_tags,
.set_client_tags = ipc_output_handle_set_client_tags,
.set_layout = ipc_output_handle_set_layout,
};
static void
ipc_output_destroy(struct wl_resource *resource)
{
IpcOutput *out = wl_resource_get_user_data(resource);
wl_list_remove(&out->link);
free(out);
}
/* zpeachwm_ipc_manager_v2 requests */
static void
ipc_manager_handle_release(struct wl_client *client, struct wl_resource *resource)
{
wl_resource_destroy(resource);
}
static void
ipc_manager_handle_get_output(struct wl_client *client, struct wl_resource *resource,
uint32_t id, struct wl_resource *output_resource)
{
IpcManager *mgr = wl_resource_get_user_data(resource);
struct wlr_output *wlr_out = wlr_output_from_resource(output_resource);
Monitor *m = (wlr_out && wlr_out->data) ? wlr_out->data : NULL;
IpcOutput *out = ecalloc(1, sizeof(*out));
out->mon = m;
out->resource = wl_resource_create(client, &zpeachwm_ipc_output_v2_interface,
wl_resource_get_version(resource), id);
if (!out->resource) {
free(out);
wl_client_post_no_memory(client);
return;
}
wl_resource_set_implementation(out->resource, &ipc_output_impl,
out, ipc_output_destroy);
wl_list_insert(&mgr->outputs, &out->link);
if (m)
ipc_send_output_state(out);
}
static const struct zpeachwm_ipc_manager_v2_interface ipc_manager_impl = {
.release = ipc_manager_handle_release,
.get_output = ipc_manager_handle_get_output,
};
static void
ipc_manager_destroy(struct wl_resource *resource)
{
IpcManager *mgr = wl_resource_get_user_data(resource);
IpcOutput *out, *tmp;
wl_list_for_each_safe(out, tmp, &mgr->outputs, link)
wl_resource_destroy(out->resource);
wl_list_remove(&mgr->link);
free(mgr);
}
/* global bind */
static void
ipc_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
IpcManager *mgr = ecalloc(1, sizeof(*mgr));
wl_list_init(&mgr->outputs);
mgr->resource = wl_resource_create(client, &zpeachwm_ipc_manager_v2_interface,
version, id);
if (!mgr->resource) {
free(mgr);
wl_client_post_no_memory(client);
return;
}
wl_resource_set_implementation(mgr->resource, &ipc_manager_impl,
mgr, ipc_manager_destroy);
wl_list_insert(&ipc_managers, &mgr->link);
/* Announce tags and layouts */
zpeachwm_ipc_manager_v2_send_tags(mgr->resource, TAGCOUNT);
for (uint32_t i = 0; i < LENGTH(layouts); i++)
zpeachwm_ipc_manager_v2_send_layout(mgr->resource, layouts[i].symbol);
}
static void
ipc_init(void)
{
wl_list_init(&ipc_managers);
ipc_global = wl_global_create(dpy, &zpeachwm_ipc_manager_v2_interface,
2, NULL, ipc_bind);
}
/* Public IPC API */
void ipc_printstatus(void);
void ipc_init(void);
+14 -1159
View File
File diff suppressed because it is too large Load Diff
+58
View File
@@ -0,0 +1,58 @@
#ifndef PEACHWM_MONITOR_H
#define PEACHWM_MONITOR_H
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_session_lock_v1.h>
#include "wlr_ext_workspace_v1.h"
/* forward declarations */
struct Client;
struct LayerSurface;
typedef struct DwindleNode DwindleNode;
#ifndef TAGCOUNT
#define TAGCOUNT 9
#endif
typedef struct Monitor Monitor;
typedef struct {
const char *symbol;
void (*arrange)(Monitor *);
} Layout;
struct Monitor {
struct wl_list link;
struct wlr_output *wlr_output;
struct wlr_scene_output *scene_output;
struct wlr_scene_rect *fullscreen_bg; /* See createmon() for info */
struct wl_listener frame;
struct wl_listener destroy;
struct wl_listener request_state;
struct wl_listener destroy_lock_surface;
struct wlr_session_lock_surface_v1 *lock_surface;
struct wlr_box m; /* monitor area, layout-relative */
struct wlr_box w; /* window area, layout-relative */
struct wl_list layers[4]; /* LayerSurface.link */
const Layout *lt[TAGCOUNT][2]; /* per-tag layout slots */
int gaps;
unsigned int seltags;
struct wlr_ext_workspace_group_handle_v1 *ext_group;
unsigned int sellt[TAGCOUNT]; /* per-tag layout toggle index */
uint32_t tagset[2];
float mfact;
int nmaster;
char ltsymbol[TAGCOUNT][16]; /* per-tag layout symbol */
int asleep;
DwindleNode *dwindle_root[TAGCOUNT]; /* one tree per tag */
struct Client *dwindle_focus[TAGCOUNT]; /* insertion anchor, one per tag */
/* master/stack layout state */
struct Client *master_master[TAGCOUNT]; /* the master client (NULL if none) */
int master_side[TAGCOUNT]; /* 0 = master on left, 1 = master on right */
int scratchpad_visible; /* whether scratchpad is shown */
struct Client *scratchpad_prev_focus; /* client focused before scratchpad opened */
struct Client *scratchpad_current; /* currently visible scratchpad client */
};
#endif /* PEACHWM_MONITOR_H */