Skip to content

Commit

Permalink
Add runtime configuration of stack width
Browse files Browse the repository at this point in the history
  • Loading branch information
tleino committed Oct 4, 2021
1 parent 372cf97 commit 3bc2e2c
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 28 deletions.
17 changes: 13 additions & 4 deletions Makefile.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SHELL = /bin/sh
CFLAGS = -g -Wall -pedantic -std=c99 @PKGS_CFLAGS@ -DTRACE -DWANT_MAXWIDTH
LDFLAGS = @PKGS_LDFLAGS@
CFLAGS = -g -Wall -std=c99 @PKGS_CFLAGS@ -DTRACE -DWANT_MAXWIDTH \
-DWANT_CTLSOCKET @SYSTEM_CFLAGS@
LDFLAGS = @PKGS_LDFLAGS@ @SYSTEM_LDFLAGS@

prefix = @prefix@
exec_prefix = $(prefix)
Expand All @@ -12,12 +13,15 @@ mandir = $(datarootdir)/man
INSTALL ?= install
INSTALLFLAGS ?=

SRCS=mxswm.c stack.c client.c event.c menu.c keyboard.c
SRCS=mxswm.c stack.c client.c event.c menu.c keyboard.c ctlsocket.c ctl.c
PROG=mxswm

OBJS=$(SRCS:.c=.o)

all: $(PROG)
all: $(PROG) mxswmctl

mxswmctl:
make -C mxswmctl

$(PROG): $(OBJS)
$(CC) -o$@ $(OBJS) $(LDFLAGS)
Expand All @@ -27,12 +31,17 @@ $(PROG): $(OBJS)

clean:
rm -f $(OBJS) $(PROG)
make -C mxswmctl clean

install: $(PROG)
if [ ! -x $(DESTDIR)$(bindir) ] ; then \
mkdir -p $(DESTDIR)$(bindir) ; fi
$(INSTALL) $(INSTALLFLAGS) $(PROG) $(DESTDIR)$(bindir)
make -C mxswmctl install

uninstall:
if [ -e $(DESTDIR)$(bindir)/$(PROG) ] ; then \
rm $(DESTDIR)$(bindir)/$(PROG) ; fi
make -C mxswmctl uninstall

.PHONY: mxswmctl
22 changes: 18 additions & 4 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ for most purposes when working with programs that were designed for
the full screen view.

At the moment mostly works, but is not flawless. Also, requires manual
customization from mxswm.h for e.g. setting the fonts and borders.
customization from mxswm.h for e.g. setting the fonts and borders. There
is some initial support for runtime configuration through a Unix domain
socket using 'mxswmctl' command.

Feature TODO
============
Expand Down Expand Up @@ -76,12 +78,24 @@ Right Move selected item to a stack on the right side,
Build
=====

./configure ~
make
make install
$ ./configure ~
$ make
$ make install

Runtime configuration
=====================

Set first stack's width to 200px:

$ mxswmctl stack 1 width 200

Dependencies
============

- Practically none on a standard Unix/Linux system that uses
X11 Window System.

Files
=====

~/.mxswm_socket For runtime configuration using mxswmctl
27 changes: 27 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ prefix=/usr/local
if [ "$#" -eq 1 ] ; then prefix=$1 ; fi
echo "prefix=${prefix}"

SYSTEM_CFLAGS=
case $(uname) in
Linux )
SYSTEM_CFLAGS=-D_POSIX_C_SOURCE=200809L
SYSTEM_LDFLAGS=
;;
OpenBSD )
SYSTEM_CFLAGS=
SYSTEM_LDFLAGS=
;;
esac
echo "system: $(uname)"
echo "SYSTEM_CFLAGS=" ${SYSTEM_CFLAGS}

PKGS="x11 xinerama"
for a in ${PKGS} ; do
check_pkg $a
Expand All @@ -42,4 +56,17 @@ sed \
-e "s|@prefix@|${prefix}|g" \
-e "s|@PKGS_CFLAGS@|${PKGS_CFLAGS}|g" \
-e "s|@PKGS_LDFLAGS@|${PKGS_LDFLAGS}|g" \
-e "s|@SYSTEM_CFLAGS@|${SYSTEM_CFLAGS}|g" \
-e "s|@SYSTEM_LDFLAGS@|${SYSTEM_LDFLAGS}|g" \
Makefile.in >>Makefile
echo "create: mxswmctl/Makefile"
echo '# Automatically generated from Makefile.in by configure' \
>mxswmctl/Makefile
echo >>mxswmctl/Makefile
sed \
-e "s|@prefix@|${prefix}|g" \
-e "s|@PKGS_CFLAGS@|${PKGS_CFLAGS}|g" \
-e "s|@PKGS_LDFLAGS@|${PKGS_LDFLAGS}|g" \
-e "s|@SYSTEM_CFLAGS@|${SYSTEM_CFLAGS}|g" \
-e "s|@SYSTEM_LDFLAGS@|${SYSTEM_LDFLAGS}|g" \
mxswmctl/Makefile.in >>mxswmctl/Makefile
17 changes: 17 additions & 0 deletions ctl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "mxswm.h"
#include <stdio.h>

void
run_ctl_line(const char *str)
{
int stackno, width;
struct stack *stack;

if (sscanf(str, "stack %d width %d", &stackno, &width) == 2) {
stack = find_stack(stackno);
if (stack != NULL)
stack->prefer_width = width;
resize_stacks();
focus_stack(stack);
}
}
148 changes: 148 additions & 0 deletions ctlsocket.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include "mxswm.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/select.h>
#include <string.h>

int
listen_ctlsocket()
{
char *home;
struct stat sb;
struct sockaddr_un addr;
int fd;

home = getenv("HOME");
if (home == NULL)
home = "/";

memset(&addr, '\0', sizeof(addr));

if (snprintf(addr.sun_path, sizeof(addr.sun_path),
"%s/.mxswm_socket", home) >= sizeof(addr.sun_path)) {
warnx("socket path truncated");
return -1;
}

if (stat(addr.sun_path, &sb) == 0) {
if (!S_ISSOCK(sb.st_mode)) {
warnx("%s is not socket", addr.sun_path);
return -1;
}
warnx("removing stale socket %s", addr.sun_path);
if (unlink(addr.sun_path) == -1) {
warn("unlink %s", addr.sun_path);
return -1;
}
}

fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
warn("socket");
return -1;
}

addr.sun_family = AF_UNIX;
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
warn("bind");
close(fd);
return -1;
}

if (listen(fd, 128) == -1) {
warn("listen");
close(fd);
return -1;
}

return fd;
}

void
run_ctlsocket_event_loop(int ctlfd)
{
int clientfd[128];
char buf[4096];
fd_set rfds;
int i, nready, nclients, j;
ssize_t n;
int fd;
int maxfd;
int xfd;
Display *dpy;
XEvent event;

dpy = display();

XSync(dpy, False);
xfd = ConnectionNumber(dpy);
nclients = 0;
for (;;) {
maxfd = 0;
FD_ZERO(&rfds);
if (xfd > maxfd)
maxfd = xfd;
FD_SET(xfd, &rfds);
if (ctlfd > maxfd)
maxfd = ctlfd;
FD_SET(ctlfd, &rfds);
for (i = 0; i < nclients; i++) {
if (clientfd[i] > maxfd)
maxfd = clientfd[i];
FD_SET(clientfd[i], &rfds);
}

nready = select(maxfd + 1, &rfds, NULL, NULL, NULL);
if (nready == -1 || nready == 0)
err(1, "select");

for (i = 0; i < nready; i++) {
if (FD_ISSET(xfd, &rfds)) {
j = XPending(dpy) + 1;
while (j--) {
XNextEvent(dpy, &event);
handle_event(&event);
XSync(dpy, False);
}
} else if (FD_ISSET(ctlfd, &rfds)) {
fd = accept(ctlfd, NULL, NULL);
if (nclients == 128)
close(fd);
else if (fd != -1)
clientfd[nclients++] = fd;
else
warn("accept");
} else for (j = 0; j < nclients; j++) {
if (FD_ISSET(clientfd[j],
&rfds)) {
n = read(clientfd[j], buf,
sizeof(buf));
if (n == -1)
err(1, "read");
else if (n == 0) {
nclients--;
close(clientfd[j]);
memmove(
&clientfd[i],
&clientfd[i+1],
(nclients - j) *
sizeof(int));
} else {
buf[n] = '\0';
run_ctl_line(buf);
XSync(dpy, False);
}
}
}
}
}
}
20 changes: 15 additions & 5 deletions mxswm.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ main(int argc, char *argv[])
int running;
XEvent event;
Display *dpy;
int ctlfd;

setlocale(LC_ALL, "");

Expand All @@ -153,11 +154,20 @@ main(int argc, char *argv[])

bind_keys();

running = 1;
while (running) {
XNextEvent(dpy, &event);
if (handle_event(&event) <= 0)
break;
#if WANT_CTLSOCKET
ctlfd = listen_ctlsocket();
#else
ctlfd = -1;
#endif
if (ctlfd != -1) {
run_ctlsocket_event_loop(ctlfd);
} else {
running = 1;
while (running) {
XNextEvent(dpy, &event);
if (handle_event(&event) <= 0)
break;
}
}

XSync(dpy, False);
Expand Down
17 changes: 16 additions & 1 deletion mxswm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
#define ARRLEN(_x) sizeof((_x)) / sizeof((_x)[0])
#endif

#ifndef MIN
#define MIN(_x, _y) ((_x) < (_y) ? (_x) : (_y))
#endif

#ifndef MAX
#define MAX(_x, _y) ((_x) > (_y) ? (_x) : (_y))
#endif

/*
* These are sane defaults for a 2560x1440 screen.
* Modify freely locally.
Expand Down Expand Up @@ -64,6 +72,7 @@ struct stack {
struct stack *prev;
Window window;
int maxwidth_override;
int prefer_width;
};

struct client {
Expand Down Expand Up @@ -92,7 +101,7 @@ void focus_stack_forward(void);
void focus_stack_backward(void);
struct stack *current_stack(void);
void resize_stack(struct stack *, unsigned short);
struct stack *find_stack(struct client *);
struct stack *find_stack(int);
void resize_client(struct client *);
void toggle_stacks_maxwidth_override(void);

Expand Down Expand Up @@ -141,6 +150,12 @@ void do_keyaction(XKeyEvent *);
void unbind_keys();
void bind_keys();

#if WANT_CTLSOCKET
int listen_ctlsocket(void);
void run_ctlsocket_event_loop(int);
void run_ctl_line(const char *);
#endif

#if TRACE
void dump_client(struct client *);
void dump_clients(void);
Expand Down
Loading

0 comments on commit 3bc2e2c

Please sign in to comment.