xf86ctrl

mplay control using xf86 media keys
git clone https://git.sinitax.com/sinitax/xf86ctrl
Log | Files | Refs | LICENSE | sfeed.txt

commit 41e6d4cfba1d8528fead5885004318d43d451ac5
parent 7e7c44fe8f574379a62a2c406462b9aaaeda87ba
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri, 23 Jun 2023 02:28:30 +0200

Add MIT license and cleanup

Diffstat:
M.gitignore | 2+-
ALICENSE | 21+++++++++++++++++++++
MMakefile | 26+++++++++++++++++++++++++-
Mmain.c | 61++++++++++++++++++++++++++++++++++---------------------------
4 files changed, 81 insertions(+), 29 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1 @@ -main +xf86ctrl diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Louis Burda + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile @@ -1,3 +1,27 @@ +PREFIX ?= /usr/local +BINDIR ?= /bin + LDLIBS = -lX11 -lutil +CFLAGS = -Wunused-function -Wunused-variable -Wconversion + +ifeq ($(DEBUG),1) +CFLAGS += -Og -g +else +CFLAGS += -O2 +endif + +all: xf86ctrl + +clean: + rm -f main + +xf86ctrl: main.c + $(CC) -o $@ $^ $(CFLAGS) $(LDLIBS) + +install: + install -m755 xf86ctrl -t "$(DESTDIR)$(PREFIX)$(BINDIR)" + +uninstall: + rm -f "$(DESTDIR)$(PREFIX)$(BINDIR)/xf86ctrl" -main: main.c +.PHONY: all clea install uninstall diff --git a/main.c b/main.c @@ -1,34 +1,41 @@ #include <X11/X.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <stdarg.h> +#include <X11/XKBlib.h> +#include <X11/Xlib.h> +#include <X11/keysym.h> +#include <X11/XF86keysym.h> + +#include <sys/wait.h> #include <signal.h> #include <unistd.h> #include <termios.h> #include <pty.h> +#include <string.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> -#include <sys/wait.h> - -#include <X11/Xlib.h> -#include <X11/keysym.h> -#include <X11/XF86keysym.h> - -#define ARRSIZE(a) (sizeof(a)/sizeof(*(a))) +#define ARRLEN(a) (sizeof(a)/sizeof(*(a))) -void -die(const char *fmtstr, ...) +static void +die(const char *fmt, ...) { va_list ap; - va_start(ap, fmtstr); - vfprintf(stderr, fmtstr, ap); + va_start(ap, fmt); + fputs("xf86ctrl: ", stderr); + vfprintf(stderr, fmt, ap); + if (*fmt && fmt[strlen(fmt) - 1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } va_end(ap); exit(EXIT_FAILURE); } -void +static void sigchild(int sig) { exit(0); @@ -42,17 +49,15 @@ main(int argc, char **argv) Window win; XEvent e; unsigned int modifiers; - int i, key, childio; struct termios childterm; struct winsize ws; + KeySym key; + int childio; - if (argc < 2) - die("USAGE: skipkill CMD [ARGS..]\n"); - - //if (tcgetattr(STDIN_FILENO, &childterm)) - // die("Failed to get term attrs\n"); - //if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != 0) - // die("Failed to get term size\n"); + if (argc != 2) { + fprintf(stderr, "Usage: xf86ctrl CMD [ARGS..]\n"); + return 1; + } cfmakeraw(&childterm); @@ -60,7 +65,7 @@ main(int argc, char **argv) ws.ws_row = 40; if ((childpid = forkpty(&childio, NULL, &childterm, &ws)) < 0) - die("Failed to run fork\n"); + die("forkpty:"); fclose(stdin); @@ -68,7 +73,7 @@ main(int argc, char **argv) execvp(argv[1], argv + 1); } else { if (!(d = XOpenDisplay(NULL))) - die("Failed to open display\n"); + die("XOpenDisplay(NULL)"); win = DefaultRootWindow(d); modifiers = ShiftMask; @@ -82,7 +87,9 @@ main(int argc, char **argv) while (1) { XNextEvent(d, &e); if (e.type == KeyPress) { - key = XKeycodeToKeysym(d, e.xkey.keycode, 0); + key = XkbKeycodeToKeysym(d, + (KeyCode) e.xkey.keycode, 0, + !!(e.xkey.state & ShiftMask)); switch (key) { case XF86XK_AudioNext: goto exit;