1
0
forked from extern/nix-config

PKGBUILDs: Add tari-util-xeventbind

xeventbind is a useful program that lets you run any command on
resolution change, similar to how GNOME on Wayland is able to change
scaling on on resolution change, but not exactly.

Since we're still using X, existing programs are not affected; they
have to be restarted instead.
This commit is contained in:
Donovan Glover 2018-11-28 20:41:02 -05:00
parent 361abf7c98
commit 74b6975ba5
No known key found for this signature in database
GPG Key ID: EA7408A77AE1BE65
8 changed files with 389 additions and 0 deletions

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 Olaf Tomalka
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.

View File

@ -0,0 +1,17 @@
CC=gcc
CFLAGS=-O2
LIBS=-lX11
OBJ=main.o xeb_handler.o
.PHONY: all clean
all: xeventbind
xeventbind: $(OBJ)
$(CC) -o $@ $^ $(LIBS) $(CFLAGS)
%.o: %.c
$(CC) -c -o $@ $< $(CLAGS)
clean:
rm -f xeventbind *.o

View File

@ -0,0 +1,24 @@
pkgname=tari-util-xeventbind
pkgver=0.1.0
pkgrel=1
pkgdesc="Run a script on resolution change"
arch=('any')
provides=('xeventbind')
conflicts=('xeventbind')
depends=('libx11')
source=('xeb_event_types.h' 'xeb_handler.c' 'xeb_handler.h' 'main.c' 'Makefile' 'LICENSE')
sha256sums=('5e55b56bc1fc281d2549bb892a1a7e93f5eb7cb7ff88d3216345907456d02801'
'9a26c895d79244c76e3c9b5842dab6e6d4b628842f6f63a3001d4563c253a28b'
'b57396a210fde7858928c1e44febd1cecc2aa715b58ca11590afddc5aa3d7a75'
'86352dc57e084a0e3ddf1b31a06b57d9d267f39618d6de077150da720cc3728c'
'420ee5dc1349c226423bf009a71a3f0b0b33456ffdf35d682fc12ee5b4dd207f'
'e219b161877df1f1986c4e79794fde8b6cb209da7bc4b4442b925da0d7fd93ee')
build() {
make
}
package() {
install -Dm 755 xeventbind "$pkgdir"/usr/bin/xeventbind
install -Dm 644 LICENSE "$pkgdir"/usr/share/licenses/xeventbind/LICENSE
}

View File

@ -0,0 +1,13 @@
# tari-util-xeventbind
[xeventbind][xeventbind] is used to run a script on resolution change.
xeventbind can be used to:
- Change the X DPI for specific resolutions, useful, for example, if you switch between traditional and HiDPI displays often
You should not use xeventbind if:
- You don't change resolutions often or don't need to run anything on resolution change
[xeventbind]: https://github.com/ritave/xeventbind

View File

@ -0,0 +1,108 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Olaf Tomalka
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "xeb_event_types.h"
#include "xeb_handler.h"
struct arguments {
xeb_event_type event_type;
char *script_path;
};
struct event_map {
char *str;
xeb_event_type event;
};
static struct event_map mapping[] = {
{"resolution", ResolutionChange}
};
void print_usage(char *path) {
fprintf(stderr, "Usage: %s event_type script_path\n", path);
fprintf(stderr, "Event types:\n");
size_t mapping_size = sizeof(mapping) / sizeof(mapping[0]);
for (int i = 0; i < mapping_size; i++) {
fprintf(stderr, "\t%s\n", mapping[i].str);
}
exit(EXIT_FAILURE);
}
void parse_args(int argc, char** argv, struct arguments *args) {
if (argc != 3)
print_usage(argv[0]);
size_t mapping_size = sizeof(mapping) / sizeof(mapping[0]);
int found = 0;
for (int i = 0; i < mapping_size; i++) {
if (strcmp(argv[1], mapping[i].str) == 0) {
found = 1;
args->event_type = mapping[i].event;
break;
}
}
if (!found)
print_usage(argv[0]);
args->script_path = argv[2];
}
int handle_callback(xeb_event_type event, void *data) {
assert(data);
struct arguments *args = data;
assert(args->event_type == event);
pid_t pid = fork();
int err;
switch (pid) {
case -1:
perror("Failed to fork\n");
exit(EXIT_FAILURE);
break;
case 0: // Child
err = execlp(args->script_path, NULL);
if (err == -1) {
perror("Failed to open callback script\n");
exit(EXIT_FAILURE);
}
break;
}
}
int main(int argc, char **argv) {
struct arguments args;
int err;
parse_args(argc, argv, &args);
xeb_add_callback(args.event_type, handle_callback, &args);
return xeb_loop() ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -0,0 +1,31 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Olaf Tomalka
*
* 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.
*/
#ifndef _XEVENTBIND_EVENT_TYPES_H
#define _XEVENTBIND_EVENT_TYPES_H
typedef enum {
ResolutionChange
} xeb_event_type;
#endif

View File

@ -0,0 +1,131 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Olaf Tomalka
*
* 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.
*/
#include <assert.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xeb_handler.h"
struct callback_data {
xeb_callback func;
void *data;
xeb_event_type event;
};
struct dimensions {
int width, height;
};
static struct callback_data *callbacks = NULL;
static int callbacks_count = 0;
static int callbacks_capacity = 0;
static void ensure_enough_callback_space(void) {
if (callbacks_count == 0)
return;
if (callbacks_capacity == 0) {
callbacks = malloc(callbacks_count * sizeof(struct callback_data));
callbacks_capacity = callbacks_count;
} else if (callbacks_count > callbacks_capacity) {
int new_size = callbacks_capacity;
while (new_size < callbacks_count)
new_size *= 2;
callbacks_capacity = new_size;
struct callback_dat *new_call = realloc(
callbacks,
callbacks_capacity * sizeof(struct callback_data));
if (new_call == NULL) {
fprintf(stderr, "Failed to realloc callback_data array\n");
exit(EXIT_FAILURE);
}
}
}
void xeb_add_callback(xeb_event_type event, xeb_callback func, void* data) {
assert(func != NULL);
callbacks_count++;
ensure_enough_callback_space();
struct callback_data *row = &callbacks[callbacks_count - 1];
row->event = event;
row->func = func;
row->data = data;
}
static void call_callbacks(xeb_event_type event) {
for (int i = 0; i < callbacks_count; i++) {
struct callback_data callback = callbacks[i];
if (callback.event == event) {
callback.func(event, callback.data);
}
}
}
int xeb_loop(void) {
Display *display = XOpenDisplay(NULL);
if (!display) {
fprintf(stderr, "Failed to open display\n");
return -1;
}
int screen_count = ScreenCount(display);
Window roots[screen_count];
struct dimensions dimens[screen_count];
for (int i = 0; i < screen_count; i++) {
roots[i] = RootWindow(display, i);
XWindowAttributes attrs;
Status status = XGetWindowAttributes(display, roots[i], &attrs);
if (!status) {
fprintf(stderr, "Failed to get root window attributes for screen: %d\n", i);
return -1;
}
dimens[i].width = attrs.width;
dimens[i].height = attrs.height;
XSelectInput(display, roots[i], StructureNotifyMask);
}
for (;;) {
XEvent e;
XNextEvent(display, &e);
if (e.type == ConfigureNotify) {
XConfigureEvent xce = e.xconfigure;
for (int i = 0; i < screen_count; i++) {
if (xce.window == roots[i]) {
if (xce.width != dimens[i].width || xce.height != dimens[i].height) {
dimens[i].width = xce.width;
dimens[i].height = xce.height;
call_callbacks(ResolutionChange);
}
break;
}
}
}
}
}

View File

@ -0,0 +1,44 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Olaf Tomalka
*
* 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.
*/
#ifndef _XEVENTBIND_HANDLER_H
#define _XEVENTBIND_HANDLER_H
#include "xeb_event_types.h"
typedef int xeb_callback_id;
/**
* @param event An event that this function was called for
* @param data A pointer to, optional, user defined input for the callback function
* @return 0 on success, anything else for error */
typedef int(*xeb_callback)(xeb_event_type event, void* data);
/**
* \brief Starts the event loop and blocks the thread until xeb_stop is called
* @return 0 on sucess, -1 on error
*/
int xeb_loop(void);
void xeb_add_callback(xeb_event_type event, xeb_callback func, void* data);
#endif