Compare commits

...

4 commits

Author SHA1 Message Date
ca7e8e5e44 meta: Refactoring to make room for other parts of the project 2023-07-13 14:22:52 +02:00
86bc9dc363 meta: Renamed project to convertablet 2023-07-13 14:18:17 +02:00
c66c11a2b4 usb_device_detection: Replaced deprecated libusb function 2023-07-13 13:54:49 +02:00
ca74d51f05 usb_device_detection+main: Added hooks on basestation state change
A script will now run whenever the basesation is plugged/unplugged.
2023-07-13 13:53:44 +02:00
11 changed files with 108 additions and 14 deletions

8
.gitignore vendored
View file

@ -1,5 +1,5 @@
protocol/*.c
protocol/*.h
protocol/*.xml
wayland-protos/*.c
wayland-protos/*.h
wayland-protos/*.xml
*~
miix-wlr
convertablet

View file

@ -2,9 +2,13 @@
set -ex;
PROTOCOL_IMPLEMENTATIONS="wayland-protos/wlr-output-management-unstable-v1.c"
SOURCES="main.c output_manager.c accel_monitor.c usb_device_detection.c"
CFLAGS="-std=c2x -Wall -Wextra -pedantic $CFLAGS"
LIBS="-lwayland-client -lusb-1.0 protocol/wlr-output-management-unstable-v1.c"
OUTPUT=miix-wlr
CFLAGS="-std=c2x -Wall -Wextra -pedantic $CFLAGS -Iwayland-protos"
LIBS="-lwayland-client -lusb-1.0 $PROTOCOL_IMPLEMENTATIONS"
OUTPUT=convertablet
clang $CFLAGS $LIBS $SOURCES -o $OUTPUT
SOURCES_PATH=""
for file in $SOURCES; do SOURCES_PATH="$SOURCES_PATH daemon/$file"; done;
clang $CFLAGS $LIBS $SOURCES_PATH -o $OUTPUT

View file

@ -1,5 +1,10 @@
// This is for asprintf support.
// TODO: Don't use asprintf and remove this.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libusb-1.0/libusb.h>
@ -21,6 +26,77 @@ static char const* const DEFAULT_MOTION_SENSOR =
static uint16_t const BASESTATION_VENDOR_ID = 0x048d;
static uint16_t const BASESTATION_PRODUCT_ID = 0x8911;
static char* get_hook_path(char const* const hook_name)
{
char* path;
char* xdg_config_dir = getenv("XDG_CONFIG_HOME");
if (xdg_config_dir)
asprintf(&path, "%s/convertablet/hooks/%s-hook", xdg_config_dir, hook_name);
else
asprintf(&path,
"%s/.config/convertablet/hooks/%s-hook",
getenv("HOME"),
hook_name);
return path;
}
static void on_basestation_connected()
{
printf("Basestation connected\n");
char* hook_path = get_hook_path("basestation-connected");
if (access(hook_path, X_OK) != 0) {
fprintf(stderr, "Couldn't read or execute hook at %s\n", hook_path);
free(hook_path);
return;
}
pid_t pid = fork();
if (pid < 0) {
// Error
fprintf(stderr, "Unable to run hook script!\n");
perror("fork");
} else if (pid == 0) {
// In the child
char* argv[] = {hook_path, 0};
execve(hook_path, argv, environ);
exit(-1);
} else {
free(hook_path);
}
}
static void on_basestation_disconnected()
{
printf("Basestation disconnected\n");
char* hook_path = get_hook_path("basestation-disconnected");
if (access(hook_path, X_OK) != 0) {
// Hook is innaccessible
fprintf(stderr, "Couldn't read or execute hook at %s\n", hook_path);
free(hook_path);
return;
}
pid_t pid = fork();
if (pid < 0) {
// Error
fprintf(stderr, "Unable to run hook script!\n");
perror("fork");
} else if (pid == 0) {
// In the child
char* argv[] = {hook_path, 0};
execve(hook_path, argv, environ);
exit(-1);
} else {
free(hook_path);
}
}
static enum accel_rotation last_accel_rotation = ACCEL_ROTATION_NO_CHANGE;
int main(int, char**)
@ -35,6 +111,10 @@ int main(int, char**)
struct usb_detector* base_station_detector =
create_detector(BASESTATION_VENDOR_ID, BASESTATION_PRODUCT_ID);
base_station_detector->on_connected = &on_basestation_connected;
base_station_detector->on_disconnected = &on_basestation_disconnected;
base_station_detector->has_hotplug_callbacks = true;
struct miix_wlr_state* state = miix_wlr_init();
for (;;) {

View file

@ -9,7 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include "protocol/wlr-output-management-unstable-v1.h"
#include "wlr-output-management-unstable-v1.h"
static void head_handle_name(void* data,
struct zwlr_output_head_v1*,

View file

@ -4,7 +4,7 @@
#include <wayland-client.h>
#include "protocol/wlr-output-management-unstable-v1.h"
#include "wlr-output-management-unstable-v1.h"
struct miix_wlr_head {
struct zwlr_output_head_v1* wlr_head;

View file

@ -10,11 +10,13 @@ int hotplug_callback(struct libusb_context*,
{
struct usb_detector* detector = user_data;
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) {
detector->is_connected = true;
else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
if (detector->has_hotplug_callbacks) detector->on_connected();
} else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) {
detector->is_connected = false;
else
if (detector->has_hotplug_callbacks) detector->on_disconnected();
} else
printf("Unexpected USB device event %d\n", event);
return 0;
@ -26,7 +28,7 @@ static int libusb_event_handler(void* data)
while (!detector->event_handler_should_exit) {
thrd_sleep(&(struct timespec){.tv_sec = 1}, NULL);
libusb_handle_events(detector->usb_context);
libusb_handle_events_completed(detector->usb_context, NULL);
}
return 0;
@ -49,6 +51,8 @@ struct usb_detector* create_detector(uint16_t vendor_id, uint16_t product_id)
detector->is_connected = false;
detector->event_handler_should_exit = false;
detector->has_hotplug_callbacks = false;
if (libusb_hotplug_register_callback(detector->usb_context,
LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,

View file

@ -6,6 +6,8 @@
#include <libusb-1.0/libusb.h>
typedef void (*hotplug_callback_f)();
struct usb_detector {
atomic_bool is_connected;
@ -14,6 +16,10 @@ struct usb_detector {
struct libusb_context* usb_context;
libusb_hotplug_callback_handle hotplug_handle;
atomic_bool has_hotplug_callbacks;
hotplug_callback_f on_connected;
hotplug_callback_f on_disconnected;
};
struct usb_detector* create_detector(uint16_t vendor_id, uint16_t product_id);