diff --git a/CHANGELOG.md b/CHANGELOG.md index fe9925c4..ba715b4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,9 @@ release is checked here and released on by hand. about 18 MiB more free memory on every board, with the same behaviour (tcballard). +- Lock, idle and brightness commands answer faster: the power control + command is a small C program instead of Python (tcballard). + - Upstream's commands show their texts instead of raw keys like `webapp.intro`, in every language upstream has, and Install > Browser gets past its browser policy step (jannyg). diff --git a/Makefile b/Makefile index 379c109f..571024f8 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # make install DESTDIR= OMARCHY= DBUS_NEXT= # # The compiles are the on-screen keyboard (wvkbd with this repository's -# patches), the startup cursor helper (wtype), the mute-switch helper and the power daemon +# patches), the startup cursor helper (wtype), the mute-switch and power-control helpers and the power daemon # (omarchy-powerd, against libsystemd's sd-bus: the recipe's makedepends); # desktop/install-tree puts every file in place. @@ -15,7 +15,7 @@ CC ?= cc CFLAGS ?= -O2 PKG_CONFIG ?= pkg-config -all: $(BUILD)/wvkbd-mobintl $(BUILD)/wtype $(BUILD)/omarchy-powerd $(BUILD)/omarchy-mute-switch +all: $(BUILD)/wvkbd-mobintl $(BUILD)/wtype $(BUILD)/omarchy-powerd $(BUILD)/omarchy-mute-switch $(BUILD)/omarchy-powerctl $(BUILD)/omarchy-powerd: desktop/configuration/powerd.c mkdir -p $(BUILD) @@ -26,6 +26,10 @@ $(BUILD)/omarchy-mute-switch: desktop/configuration/mute-switch.c mkdir -p $(BUILD) $(CC) $(CPPFLAGS) $(CFLAGS) -Wall -Wextra -std=c99 desktop/configuration/mute-switch.c $(LDFLAGS) -o $@ +$(BUILD)/omarchy-powerctl: desktop/configuration/powerctl.c + mkdir -p $(BUILD) + $(CC) $(CPPFLAGS) $(CFLAGS) -Wall -Wextra -std=c99 desktop/configuration/powerctl.c $(LDFLAGS) -o $@ + $(BUILD)/wvkbd-mobintl: @test -d "$(WVKBD)" || { echo 'Set WVKBD to the wvkbd v0.20 source' >&2; exit 1; } @grep -q preferred_buffer_scale /usr/include/wayland-client-protocol.h || \ diff --git a/desktop/configuration/powerctl.c b/desktop/configuration/powerctl.c new file mode 100644 index 00000000..82ff098d --- /dev/null +++ b/desktop/configuration/powerctl.c @@ -0,0 +1,105 @@ +/* + * omarchy-powerctl: tell omarchy-powerd about the session (idle, wake, lock, + * dim, undim, unlocked) or query it (input, status). + * + * input: exit 0 while the panel is lit, 3 while it is dark (a dark screen + * ignores taps), 1 on unknown or error. See docs/power-and-lock.md. + */ +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEFAULT_SOCK "/run/omarchy-powerd.sock" +#define TIMEOUT_SEC 3 + +int main(int argc, char *argv[]) { + if (argc < 2 || !argv[1][0]) { + fprintf(stderr, "omarchy-powerctl: missing command\n"); + return 1; + } + + const char *sock_path = getenv("OMARCHY_POWERD_SOCK"); + if (!sock_path || !*sock_path) + sock_path = DEFAULT_SOCK; + + struct sockaddr_un addr; + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + if (strlen(sock_path) >= sizeof(addr.sun_path)) { + fprintf(stderr, "omarchy-powerctl: socket path too long\n"); + return 1; + } + memcpy(addr.sun_path, sock_path, strlen(sock_path) + 1); + + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) { + fprintf(stderr, "omarchy-powerctl: %s\n", strerror(errno)); + return 1; + } + + struct timeval tv = { .tv_sec = TIMEOUT_SEC, .tv_usec = 0 }; + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); + + if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + fprintf(stderr, "omarchy-powerctl: %s\n", strerror(errno)); + close(fd); + return 1; + } + + size_t len = strlen(argv[1]); + char msg[256]; + if (len + 1 >= sizeof(msg)) { + fprintf(stderr, "omarchy-powerctl: command too long\n"); + close(fd); + return 1; + } + memcpy(msg, argv[1], len); + msg[len] = '\n'; + + size_t sent = 0; + while (sent < len + 1) { + ssize_t n = send(fd, msg + sent, (len + 1) - sent, MSG_NOSIGNAL); + if (n < 0) { + if (errno == EINTR) + continue; + fprintf(stderr, "omarchy-powerctl: %s\n", strerror(errno)); + close(fd); + return 1; + } + sent += (size_t)n; + } + + char buf[65]; + ssize_t r; + do { + r = recv(fd, buf, 64, 0); + } while (r < 0 && errno == EINTR); + close(fd); + if (r < 0) { + fprintf(stderr, "omarchy-powerctl: %s\n", strerror(errno)); + return 1; + } + buf[r] = '\0'; + + char *start = buf; + while (*start == ' ' || *start == '\t' || *start == '\r' || *start == '\n') + start++; + char *end = start + strlen(start); + while (end > start && (end[-1] == ' ' || end[-1] == '\t' || end[-1] == '\r' || end[-1] == '\n')) + *--end = '\0'; + + printf("%s\n", start); + if (strcmp(start, "dark") == 0) + return 3; + if (strcmp(start, "unknown") == 0) + return 1; + return 0; +} diff --git a/desktop/install-tree b/desktop/install-tree index 5122ccbe..74acb345 100755 --- a/desktop/install-tree +++ b/desktop/install-tree @@ -19,7 +19,7 @@ here=$(cd "$(dirname "$0")" && pwd) project=$(dirname "$here") shell=$here/shell config=$here/configuration omarchy=$root/usr/share/omarchy -for input in "$build/wvkbd-mobintl" "$build/wtype" "$build/wtype.LICENSE" "$build/omarchy-mute-switch" "$build/omarchy-powerd" "$upstream/themes" "$upstream/bin" \ +for input in "$build/wvkbd-mobintl" "$build/wtype" "$build/wtype.LICENSE" "$build/omarchy-powerctl" "$build/omarchy-mute-switch" "$build/omarchy-powerd" "$upstream/themes" "$upstream/bin" \ "$upstream/themes/jade-horse/colors.toml" "$upstream/icons/papercraft" "$upstream/applications/foot.desktop" "$upstream/default/hypr/toggles/bar-glassy.lua" \ "$upstream/default/i18n/en/cli.json" "$upstream/install/helpers/as-root.sh" "$upstream/install/helpers/browser-policy.sh" \ "$upstream/default/chromium/policies/omarchy.json" "$upstream/default/firefox/policies.json" "$upstream/config/chromium-flags.conf" \ @@ -107,13 +107,14 @@ for set in "$omarchy"/icons/*/; do if [[ -f $set/btop.svg.tpl && ! -e $set/omactivity.svg.tpl ]]; then ln -s btop.svg.tpl "$set/omactivity.svg.tpl"; fi done -# The keyboard, the startup cursor helper, the power daemon and the hardware -# mute-switch helper. +# The keyboard, the startup cursor helper, the power daemon, and the +# power-control and hardware mute-switch helpers. put "$build/wvkbd-mobintl" "$omarchy/trials/wvkbd-mobintl-v0.20-font18-special15-state" 755 put "$build/wtype" "$root/usr/libexec/omarchy-wtype" 755 put "$build/wtype.LICENSE" "$root/usr/share/licenses/omarchy-wtype/LICENSE" put "$build/omarchy-powerd" "$root/usr/libexec/omarchy-powerd" 755 put "$build/omarchy-mute-switch" "$root/usr/libexec/omarchy-mute-switch" 755 +put "$build/omarchy-powerctl" "$root/usr/libexec/omarchy-powerctl" 755 # The Bluetooth helper's D-Bus library, from its wheel. (cd "$dbus_next" && find dbus_next -type f -name '*.py' | LC_ALL=C sort) | while read -r file; do diff --git a/release/audit.py b/release/audit.py index bda4c3b1..cac6a617 100644 --- a/release/audit.py +++ b/release/audit.py @@ -19,6 +19,7 @@ HELPERS = {'usr/libexec/omarchy-wtype': 'startup cursor helper', 'usr/libexec/omarchy-powerd': 'power, lock and sleep daemon', 'usr/libexec/omarchy-mute-switch': 'mute switch helper', + 'usr/libexec/omarchy-powerctl': 'power control client', 'usr/share/omarchy/trials/wvkbd-mobintl-v0.20-font18-special15-state': 'on-screen keyboard'} def private_firmware(orchard=None): diff --git a/system/overlay/usr/libexec/omarchy-powerctl b/system/overlay/usr/libexec/omarchy-powerctl deleted file mode 100755 index daad7ec0..00000000 --- a/system/overlay/usr/libexec/omarchy-powerctl +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/python3 -"""Tell omarchy-powerd about the session: idle, wake, lock, or ask with input/status. - -input: exit 0 while the panel is lit, 3 while it is dark (a dark screen ignores -taps). See docs/power-and-lock.md. -""" -import socket -import sys - -try: - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as connection: - connection.settimeout(3) - connection.connect('/run/omarchy-powerd.sock') - connection.sendall(sys.argv[1].encode() + b'\n') - answer = connection.recv(64).decode().strip() -except (IndexError, OSError) as error: - print(f'omarchy-powerctl: {error}', file=sys.stderr) - sys.exit(1) -print(answer) -sys.exit(3 if answer == 'dark' else 1 if answer == 'unknown' else 0) diff --git a/tests/package_tree.py b/tests/package_tree.py index b2a7bd19..07ef50a7 100644 --- a/tests/package_tree.py +++ b/tests/package_tree.py @@ -32,7 +32,7 @@ def inputs(base): """BUILD, OMARCHY and DBUS_NEXT for install-tree, under BASE.""" build, upstream, wheel = base / 'build', base / 'omarchy', base / 'wheel' build.mkdir() - for name in ('wvkbd-mobintl', 'wtype', 'omarchy-powerd', 'omarchy-mute-switch'): + for name in ('wvkbd-mobintl', 'wtype', 'omarchy-powerd', 'omarchy-mute-switch', 'omarchy-powerctl'): (build / name).write_bytes(elf()) (build / 'wtype.LICENSE').write_text('MIT\n') (upstream / 'themes/jade-horse/backgrounds').mkdir(parents=True) diff --git a/tests/test_package.py b/tests/test_package.py index b37e5e8e..b19baef3 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -378,6 +378,7 @@ def test_the_compiled_helpers_are_of_the_roots_architecture(self): self.assertEqual(set(audit.HELPERS), {'usr/libexec/omarchy-wtype', 'usr/libexec/omarchy-powerd', 'usr/libexec/omarchy-mute-switch', + 'usr/libexec/omarchy-powerctl', 'usr/share/omarchy/trials/wvkbd-mobintl-v0.20-font18-special15-state'}) from release.architecture import check_elf check_elf(elf('aarch64'), 'aarch64') diff --git a/tests/test_powerctl.py b/tests/test_powerctl.py new file mode 100644 index 00000000..fcc71b53 --- /dev/null +++ b/tests/test_powerctl.py @@ -0,0 +1,80 @@ +"""desktop/configuration/powerctl.c: native C omarchy-powerctl client tests.""" +import os +from pathlib import Path +import shutil +import socket +import subprocess +import tempfile +import threading +import unittest + +PROJECT = Path(__file__).resolve().parents[1] +SOURCE = PROJECT / 'desktop/configuration/powerctl.c' + + +@unittest.skipUnless(shutil.which('cc'), 'C compiler needed to test powerctl.c') +class PowerctlBinary(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls._tmp = tempfile.TemporaryDirectory() + cls.binary = Path(cls._tmp.name) / 'omarchy-powerctl' + subprocess.run( + ['cc', '-O2', '-Wall', '-Wextra', '-Werror', '-std=c99', str(SOURCE), '-o', str(cls.binary)], + check=True, + ) + + @classmethod + def tearDownClass(cls): + cls._tmp.cleanup() + + def run_against_reply(self, verb: str, reply: bytes): + with tempfile.TemporaryDirectory() as tmp: + sock_path = Path(tmp) / 'powerd.sock' + received = [] + srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + srv.bind(str(sock_path)) + srv.listen(1) + + def serve(): + conn, _ = srv.accept() + with conn: + received.append(conn.recv(256)) + conn.sendall(reply) + srv.close() + + t = threading.Thread(target=serve, daemon=True) + t.start() + env = dict(os.environ, OMARCHY_POWERD_SOCK=str(sock_path)) + res = subprocess.run([str(self.binary), verb], env=env, capture_output=True, text=True, timeout=5) + t.join(timeout=5) + return res, received + + def test_ok_dark_and_unknown_exit_codes(self): + res, rec = self.run_against_reply('input', b'ok\n') + self.assertEqual(res.returncode, 0) + self.assertEqual(res.stdout, 'ok\n') + self.assertEqual(rec, [b'input\n']) + + res, rec = self.run_against_reply('input', b'dark\n') + self.assertEqual(res.returncode, 3) + self.assertEqual(res.stdout, 'dark\n') + self.assertEqual(rec, [b'input\n']) + + res, rec = self.run_against_reply('bogus', b'unknown\n') + self.assertEqual(res.returncode, 1) + self.assertEqual(res.stdout, 'unknown\n') + self.assertEqual(rec, [b'bogus\n']) + + def test_missing_arg_and_missing_socket_return_1(self): + res = subprocess.run([str(self.binary)], capture_output=True, text=True, timeout=5) + self.assertEqual(res.returncode, 1) + self.assertIn('omarchy-powerctl:', res.stderr) + + env = dict(os.environ, OMARCHY_POWERD_SOCK='/tmp/nonexistent-omarchy-powerd-test.sock') + res = subprocess.run([str(self.binary), 'status'], env=env, capture_output=True, text=True, timeout=5) + self.assertEqual(res.returncode, 1) + self.assertIn('omarchy-powerctl:', res.stderr) + + +if __name__ == '__main__': + unittest.main()