diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-07-22 16:24:26 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-07-22 16:24:26 +0200 |
| commit | 896bb1e6576fe32b754341ddf0b49fbcfd1c116a (patch) | |
| tree | dff8e0e16a344a0a27153e3f6b7b23aad4e96227 | |
| parent | 1b9769db524afe3deb987e95db9fe6651104ffc0 (diff) | |
make it also working on other compositors
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | compositor.c | 51 | ||||
| -rw-r--r-- | compositor.h | 6 | ||||
| -rw-r--r-- | config.h | 4 | ||||
| -rw-r--r-- | dmenu-wl.c | 33 |
5 files changed, 90 insertions, 6 deletions
@@ -4,7 +4,7 @@ include config.mk WLR_LAYER_SHELL = protocol/wlr-layer-shell-unstable.xml DWL_IPC = protocol/dwl-bar-ipc-unstable-v1.xml PROTOCOLS = wlr-layer-shell-protocol.c xdg-shell-protocol.c dwl-bar-ipc-protocol.c -SRC = dmenu-wl.c drw.c util.c wayland.c +SRC = dmenu-wl.c drw.c util.c wayland.c compositor.c OBJ = ${SRC:.c=.o} ${PROTOCOLS:.c=.o} default_target: options dmenu-wl diff --git a/compositor.c b/compositor.c new file mode 100644 index 0000000..6470611 --- /dev/null +++ b/compositor.c @@ -0,0 +1,51 @@ +#define _GNU_SOURCE + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wayland-client.h> +#include <sys/socket.h> +#include <errno.h> + +#define PROCESS_NAME_MAX_LENGTH 1024 + +char name[PROCESS_NAME_MAX_LENGTH] = ""; + +static pid_t +pid_from_fd(int fd) +{ + struct ucred ucred; + socklen_t len = sizeof(struct ucred); + if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { + perror("getsockopt failed"); + exit(-1); + } + return ucred.pid; +} + +static char * +process_name_from_pid(const pid_t pid) { + char proc_buf[64]; + sprintf(proc_buf, "/proc/%d/comm", pid); + FILE *fp; + if ((fp = fopen(proc_buf, "r")) == NULL) { + fprintf(stderr, "opening '%s' failed: %s\n", proc_buf, strerror(errno)); + exit(-1); + } + if (fgets(name, PROCESS_NAME_MAX_LENGTH, fp) == NULL) { + fprintf(stderr, "reading '%s' failed\n", proc_buf); + exit(-1); + } + name[strcspn(name, "\n")] = 0; + fclose(fp); + return name; +} + +char * +process_name_from_fd(struct wl_display *display) +{ + int fd = wl_display_get_fd(display); + pid_t pid = pid_from_fd(fd); + char *process_name = process_name_from_pid(pid); + return process_name; +} diff --git a/compositor.h b/compositor.h new file mode 100644 index 0000000..0b7216a --- /dev/null +++ b/compositor.h @@ -0,0 +1,6 @@ +#ifndef COMPOSITOR_H +#define COMPOSITOR_H + +char * process_name_from_fd(struct wl_display *display); + +#endif @@ -9,8 +9,10 @@ static unsigned lines = 20; static unsigned padding = 10; static unsigned borderwidth = 2; static unsigned fontsize = 11; -static char fontpath[] = "/usr/share/fonts/TTF/Sauce Code Pro Nerd Font Complete Mono.ttf"; +static char fontpath[] = "/usr/share/fonts/TTF/SauceCodeProNerdFont-Regular.ttf"; static unsigned cursor_width = 1; static unsigned cursor_vertical_offset = 2; static unsigned cursor_horizontal_offset = 1; + +static unsigned monitor_offset = 1; @@ -17,6 +17,7 @@ #include "drw.h" #include "util.h" +#include "compositor.h" /* macro definitions */ #define MAX_LINE_LENGTH 1024 @@ -548,7 +549,9 @@ readstdin() void setup() { + unsigned count = 0; Monitor *mon; + char *compositor; char namespace[] = "dmenu-wl"; uint32_t layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP; uint32_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; @@ -563,15 +566,37 @@ setup() client.repeat.timer = timerfd_create(CLOCK_MONOTONIC, 0); client.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); client.display = wl_display_connect(0); + + if (client.display == 0) + die("could not open display:"); + client.registry = wl_display_get_registry(client.display); + + if (client.registry == 0) + die("could not open display:"); + wl_registry_add_listener(client.registry, ®istry_listnener, &client); wl_display_roundtrip(client.display); - for (mon = monitors; mon; mon = mon->next) { - mon->dwl_output = zdwl_manager_v1_get_output(client.dwl_manager, mon->output); - zdwl_output_v1_add_listener(mon->dwl_output, &dwl_output_listener, mon); + for (mon = monitors; mon && count < monitor_offset;) { + mon = mon->next; + count += 1; + } + + if (mon) { + active_monitor = mon; + } else { + active_monitor = monitors; + } + + compositor = process_name_from_fd(client.display); + if (strcmp(compositor, "dwl") == 0) { + for (mon = monitors; mon; mon = mon->next) { + mon->dwl_output = zdwl_manager_v1_get_output(client.dwl_manager, mon->output); + zdwl_output_v1_add_listener(mon->dwl_output, &dwl_output_listener, mon); + } + wl_display_roundtrip(client.display); } - wl_display_roundtrip(client.display); active_monitor->font = create_font(fontpath, fontsize * 1.5); |