From 934b25f8cd02a20ca78e2a1fbaa36b3dd1ba5f9f Mon Sep 17 00:00:00 2001 From: randomuser Date: Thu, 15 Sep 2022 19:53:39 -0500 Subject: [PATCH] add bspwm code for bspc --- Makefile | 78 +++++++++++++++++++++++++++++++++++++ Sourcedeps | 21 ++++++++++ bspc.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++ common.h | 33 ++++++++++++++++ helpers.h | 87 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 330 insertions(+) create mode 100644 Makefile create mode 100644 Sourcedeps create mode 100644 bspc.c create mode 100644 common.h create mode 100644 helpers.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9cbedc2 --- /dev/null +++ b/Makefile @@ -0,0 +1,78 @@ +VERCMD ?= git describe --tags 2> /dev/null +VERSION := $(shell $(VERCMD) || cat VERSION) + +CPPFLAGS += -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" +CFLAGS += -std=c99 -pedantic -Wall -Wextra -DJSMN_STRICT +LDFLAGS ?= +LDLIBS = $(LDFLAGS) -lm -lxcb -lxcb-util -lxcb-keysyms -lxcb-icccm -lxcb-ewmh -lxcb-randr -lxcb-xinerama -lxcb-shape + +PREFIX ?= /usr/local +BINPREFIX ?= $(PREFIX)/bin +MANPREFIX ?= $(PREFIX)/share/man +DOCPREFIX ?= $(PREFIX)/share/doc/bspwm +BASHCPL ?= $(PREFIX)/share/bash-completion/completions +FISHCPL ?= $(PREFIX)/share/fish/vendor_completions.d +ZSHCPL ?= $(PREFIX)/share/zsh/site-functions + +MD_DOCS = README.md doc/CHANGELOG.md doc/CONTRIBUTING.md doc/INSTALL.md doc/MISC.md doc/TODO.md +XSESSIONS ?= $(PREFIX)/share/xsessions + +WM_SRC = bspwm.c helpers.c geometry.c jsmn.c settings.c monitor.c desktop.c tree.c stack.c history.c \ + events.c pointer.c window.c messages.c parse.c query.c restore.c rule.c ewmh.c subscribe.c +WM_OBJ := $(WM_SRC:.c=.o) +CLI_SRC = bspc.c helpers.c +CLI_OBJ := $(CLI_SRC:.c=.o) + +all: bspwm bspc + +debug: CFLAGS += -O0 -g +debug: bspwm bspc + +VPATH=src + +include Sourcedeps + +$(WM_OBJ) $(CLI_OBJ): Makefile + +bspwm: $(WM_OBJ) + +bspc: $(CLI_OBJ) + +install: + mkdir -p "$(DESTDIR)$(BINPREFIX)" + cp -pf bspwm "$(DESTDIR)$(BINPREFIX)" + cp -pf bspc "$(DESTDIR)$(BINPREFIX)" + mkdir -p "$(DESTDIR)$(MANPREFIX)"/man1 + cp -p doc/bspwm.1 "$(DESTDIR)$(MANPREFIX)"/man1 + cp -Pp doc/bspc.1 "$(DESTDIR)$(MANPREFIX)"/man1 + mkdir -p "$(DESTDIR)$(BASHCPL)" + cp -p contrib/bash_completion "$(DESTDIR)$(BASHCPL)"/bspc + mkdir -p "$(DESTDIR)$(FISHCPL)" + cp -p contrib/fish_completion "$(DESTDIR)$(FISHCPL)"/bspc.fish + mkdir -p "$(DESTDIR)$(ZSHCPL)" + cp -p contrib/zsh_completion "$(DESTDIR)$(ZSHCPL)"/_bspc + mkdir -p "$(DESTDIR)$(DOCPREFIX)" + cp -p $(MD_DOCS) "$(DESTDIR)$(DOCPREFIX)" + mkdir -p "$(DESTDIR)$(DOCPREFIX)"/examples + cp -pr examples/* "$(DESTDIR)$(DOCPREFIX)"/examples + mkdir -p "$(DESTDIR)$(XSESSIONS)" + cp -p contrib/freedesktop/bspwm.desktop "$(DESTDIR)$(XSESSIONS)" + +uninstall: + rm -f "$(DESTDIR)$(BINPREFIX)"/bspwm + rm -f "$(DESTDIR)$(BINPREFIX)"/bspc + rm -f "$(DESTDIR)$(MANPREFIX)"/man1/bspwm.1 + rm -f "$(DESTDIR)$(MANPREFIX)"/man1/bspc.1 + rm -f "$(DESTDIR)$(BASHCPL)"/bspc + rm -f "$(DESTDIR)$(FISHCPL)"/bspc.fish + rm -f "$(DESTDIR)$(ZSHCPL)"/_bspc + rm -rf "$(DESTDIR)$(DOCPREFIX)" + rm -f "$(DESTDIR)$(XSESSIONS)"/bspwm.desktop + +doc: + a2x -v -d manpage -f manpage -a revnumber=$(VERSION) doc/bspwm.1.asciidoc + +clean: + rm -f $(WM_OBJ) $(CLI_OBJ) bspwm bspc + +.PHONY: all debug install uninstall doc clean diff --git a/Sourcedeps b/Sourcedeps new file mode 100644 index 0000000..ddc1030 --- /dev/null +++ b/Sourcedeps @@ -0,0 +1,21 @@ +bspc.o: bspc.c common.h helpers.h +bspwm.o: bspwm.c bspwm.h common.h desktop.h events.h ewmh.h helpers.h history.h messages.h monitor.h pointer.h rule.h settings.h subscribe.h types.h window.h +desktop.o: desktop.c bspwm.h desktop.h ewmh.h helpers.h history.h monitor.h query.h settings.h subscribe.h tree.h types.h window.h +events.o: events.c bspwm.h events.h ewmh.h helpers.h monitor.h pointer.h query.h settings.h subscribe.h tree.h types.h window.h +ewmh.o: ewmh.c bspwm.h ewmh.h helpers.h settings.h tree.h types.h +geometry.o: geometry.c geometry.h helpers.h types.h +helpers.o: helpers.c bspwm.h helpers.h types.h +history.o: history.c bspwm.h helpers.h query.h tree.h types.h +jsmn.o: jsmn.c jsmn.h +messages.o: messages.c bspwm.h common.h desktop.h helpers.h jsmn.h messages.h monitor.h parse.h pointer.h query.h restore.h rule.h settings.h subscribe.h tree.h types.h window.h +monitor.o: monitor.c bspwm.h desktop.h ewmh.h geometry.h helpers.h monitor.h pointer.h query.h settings.h subscribe.h tree.h types.h window.h +parse.o: parse.c helpers.h parse.h subscribe.h types.h +pointer.o: pointer.c bspwm.h events.h helpers.h monitor.h pointer.h query.h settings.h stack.h subscribe.h tree.h types.h window.h +query.o: query.c bspwm.h desktop.h helpers.h history.h monitor.h parse.h query.h subscribe.h tree.h types.h window.h +restore.o: restore.c bspwm.h desktop.h ewmh.h helpers.h history.h jsmn.h monitor.h parse.h pointer.h query.h restore.h settings.h stack.h subscribe.h tree.h types.h window.h +rule.o: rule.c bspwm.h ewmh.h helpers.h parse.h rule.h settings.h subscribe.h types.h window.h +settings.o: settings.c bspwm.h helpers.h settings.h types.h +stack.o: stack.c bspwm.h ewmh.h helpers.h stack.h subscribe.h tree.h types.h window.h +subscribe.o: subscribe.c bspwm.h desktop.h helpers.h settings.h subscribe.h types.h +tree.o: tree.c bspwm.h desktop.h ewmh.h geometry.h helpers.h history.h monitor.h pointer.h query.h settings.h stack.h subscribe.h tree.h types.h window.h +window.o: window.c bspwm.h ewmh.h geometry.h helpers.h monitor.h parse.h pointer.h query.h rule.h settings.h stack.h subscribe.h tree.h types.h window.h diff --git a/bspc.c b/bspc.c new file mode 100644 index 0000000..5dd807c --- /dev/null +++ b/bspc.c @@ -0,0 +1,111 @@ +/* Copyright (c) 2012, Bastien Dejean + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "helpers.h" +#include "common.h" + +int main(int argc, char *argv[]) +{ + int sock_fd; + struct sockaddr_un sock_address; + char msg[BUFSIZ], rsp[BUFSIZ]; + + if (argc < 2) { + err("No arguments given.\n"); + } + + sock_address.sun_family = AF_UNIX; + char *sp; + + if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + err("Failed to create the socket.\n"); + } + + sp = getenv(SOCKET_ENV_VAR); + if (sp != NULL) { + snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", sp); + } else { + char *host = NULL; + int dn = 0, sn = 0; + if (xcb_parse_display(NULL, &host, &dn, &sn) != 0) { + snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), SOCKET_PATH_TPL, host, dn, sn); + } + free(host); + } + + if (connect(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1) { + err("Failed to connect to the socket.\n"); + } + + argc--, argv++; + int msg_len = 0; + + for (int offset = 0, rem = sizeof(msg), n = 0; argc > 0 && rem > 0; offset += n, rem -= n, argc--, argv++) { + n = snprintf(msg + offset, rem, "%s%c", *argv, 0); + msg_len += n; + } + + if (send(sock_fd, msg, msg_len, 0) == -1) { + err("Failed to send the data.\n"); + } + + int ret = EXIT_SUCCESS, nb; + + struct pollfd fds[] = { + {sock_fd, POLLIN, 0}, + {STDOUT_FILENO, POLLHUP, 0}, + }; + + while (poll(fds, 2, -1) > 0) { + if (fds[0].revents & POLLIN) { + if ((nb = recv(sock_fd, rsp, sizeof(rsp)-1, 0)) > 0) { + rsp[nb] = '\0'; + if (rsp[0] == FAILURE_MESSAGE[0]) { + ret = EXIT_FAILURE; + fprintf(stderr, "%s", rsp + 1); + fflush(stderr); + } else { + fprintf(stdout, "%s", rsp); + fflush(stdout); + } + } else { + break; + } + } + if (fds[1].revents & (POLLERR | POLLHUP)) { + break; + } + } + + close(sock_fd); + return ret; +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..f44eef8 --- /dev/null +++ b/common.h @@ -0,0 +1,33 @@ +/* Copyright (c) 2012, Bastien Dejean + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BSPWM_COMMON_H +#define BSPWM_COMMON_H + +#define SOCKET_PATH_TPL "/tmp/bspwm%s_%i_%i-socket" +#define SOCKET_ENV_VAR "BSPWM_SOCKET" + +#define FAILURE_MESSAGE "\x07" + +#endif diff --git a/helpers.h b/helpers.h new file mode 100644 index 0000000..c65ebbe --- /dev/null +++ b/helpers.h @@ -0,0 +1,87 @@ +/* Copyright (c) 2012, Bastien Dejean + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BSPWM_HELPERS_H +#define BSPWM_HELPERS_H + +#include +#include +#include +#include +#include +#include + +#define LENGTH(x) (sizeof(x) / sizeof(*x)) +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) + +#define IS_TILED(c) (c->state == STATE_TILED || c->state == STATE_PSEUDO_TILED) +#define IS_FLOATING(c) (c->state == STATE_FLOATING) +#define IS_FULLSCREEN(c) (c->state == STATE_FULLSCREEN) +#define IS_RECEPTACLE(n) (is_leaf(n) && n->client == NULL) + +#define BOOL_STR(A) ((A) ? "true" : "false") +#define ON_OFF_STR(A) ((A) ? "on" : "off") +#define LAYOUT_STR(A) ((A) == LAYOUT_TILED ? "tiled" : "monocle") +#define LAYOUT_CHR(A) ((A) == LAYOUT_TILED ? 'T' : 'M') +#define CHILD_POL_STR(A) ((A) == FIRST_CHILD ? "first_child" : "second_child") +#define AUTO_SCM_STR(A) ((A) == SCHEME_LONGEST_SIDE ? "longest_side" : ((A) == SCHEME_ALTERNATE ? "alternate" : "spiral")) +#define TIGHTNESS_STR(A) ((A) == TIGHTNESS_HIGH ? "high" : "low") +#define SPLIT_TYPE_STR(A) ((A) == TYPE_HORIZONTAL ? "horizontal" : "vertical") +#define SPLIT_MODE_STR(A) ((A) == MODE_AUTOMATIC ? "automatic" : "manual") +#define SPLIT_DIR_STR(A) ((A) == DIR_NORTH ? "north" : ((A) == DIR_WEST ? "west" : ((A) == DIR_SOUTH ? "south" : "east"))) +#define STATE_STR(A) ((A) == STATE_TILED ? "tiled" : ((A) == STATE_FLOATING ? "floating" : ((A) == STATE_FULLSCREEN ? "fullscreen" : "pseudo_tiled"))) +#define STATE_CHR(A) ((A) == STATE_TILED ? 'T' : ((A) == STATE_FLOATING ? 'F' : ((A) == STATE_FULLSCREEN ? '=' : 'P'))) +#define LAYER_STR(A) ((A) == LAYER_BELOW ? "below" : ((A) == LAYER_NORMAL ? "normal" : "above")) + +#define XCB_CONFIG_WINDOW_X_Y (XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y) +#define XCB_CONFIG_WINDOW_WIDTH_HEIGHT (XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT) +#define XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT (XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT) + +#define MAXLEN 256 +#define SMALEN 32 +#define INIT_CAP 8 + +#define cleaned_mask(m) (m & ~(num_lock | scroll_lock | caps_lock)) +#define streq(s1, s2) (strcmp((s1), (s2)) == 0) +#define unsigned_subtract(a, b) \ + do { \ + if (b > a) { \ + a = 0; \ + } else { \ + a -= b; \ + } \ + } while (false) + + +void warn(char *fmt, ...); +void err(char *fmt, ...); +char *read_string(const char *file_path, size_t *tlen); +char *copy_string(char *str, size_t len); +char *mktempfifo(const char *template); +int asprintf(char **buf, const char *fmt, ...); +int vasprintf(char **buf, const char *fmt, va_list args); +bool is_hex_color(const char *color); + +#endif