From 6e075bff4a475570b7a2f931ee26236f40cc008d Mon Sep 17 00:00:00 2001 From: Squibid Date: Sun, 21 Sep 2025 14:15:09 -0400 Subject: [PATCH] feat(build): use meson so I don't end up ripping my hair out --- Makefile | 20 -------------------- config.h | 3 +++ config.mk | 8 -------- include/acpi.h | 33 +++++++++++++++++++++++++++++++-- include/led.h | 14 ++++++++++++++ include/utils.h | 18 ++++++++++++++++++ meson.build | 29 +++++++++++++++++++++++++++++ {include => src}/acpi.c | 24 ------------------------ {include => src}/led.c | 11 ----------- main.c => src/main.c | 14 ++++++-------- {include => src}/utils.c | 17 +---------------- 11 files changed, 102 insertions(+), 89 deletions(-) delete mode 100644 Makefile delete mode 100644 config.mk create mode 100644 meson.build rename {include => src}/acpi.c (79%) rename {include => src}/led.c (95%) rename main.c => src/main.c (89%) rename {include => src}/utils.c (76%) diff --git a/Makefile b/Makefile deleted file mode 100644 index 2d9890a..0000000 --- a/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -include config.mk - -PROG ?= eh -OBJS = main.o\ - lib/log.c/src/log.o\ - include/led.o\ - include/acpi.o\ - include/utils.o - -all: $(PROG) - -# main build task -.c.h.o: - $(CC) $(CFLAGS) -o $@ $< - -$(PROG): $(OBJS) - $(CC) $(CFLAGS) -o $@ $(OBJS) - -clean: - rm $(PROG) $(OBJS) diff --git a/config.h b/config.h index dfe86bf..a983d46 100644 --- a/config.h +++ b/config.h @@ -2,6 +2,9 @@ static const char *notification_name = "Event Handler"; /* name to use in notifications */ static const char *battery_dir = "/sys/class/power_supply/BAT1"; /* path to the batteries directory */ +static const char *linefile = "/sys/class/power_supply/ACAD/online"; /* path to the file which tells us if we are online (plugged in) */ +static const char *ledfile = "/sys/class/leds/input0::capslock/brightness"; /* path to the file which lights up the led */ +static const char *acpid_sock = "/run/acpid.socket"; /* acpid socket file */ static const int blink_start = 15; /* percentage to start blinking at */ const double blink_formula(int x) { /* formula used to set the blinking rate, this function takes in one argument: the battery percentage */ return 1.7 + 18.5 * exp(-0.29 * x); diff --git a/config.mk b/config.mk deleted file mode 100644 index b35e790..0000000 --- a/config.mk +++ /dev/null @@ -1,8 +0,0 @@ -PKG_CONFIG = pkg-config -CC = cc -VERSION = 0.1 - -# flags and incs -INCLUDES = -I./lib/log.c/src -PKGS = -CFLAGS = -DVERSION=\"$(VERSION)\" $(INCLUDES) -DLOG_USE_COLOR -Wall -Og -g -lm diff --git a/include/acpi.h b/include/acpi.h index 399abd7..255effb 100644 --- a/include/acpi.h +++ b/include/acpi.h @@ -1,4 +1,33 @@ -int acpi_create_socket(char *socket_file); +#pragma once + +/** + * @brief parse output of acpid + * + * @param str input from acpi socket + * @param out the parsed version of it + * @return 0 on success + */ int acpi_parse(char *str, char **out); -int acpi_close_socket(int sockfd); + +/** + * @brief cleanup the results of acpi_parse + * + * @param out the pointer to the output from acpi_parse + */ void acpi_clean_parse(char *out[4]); + +/** + * @brief create a new acpi socket connection + * + * @param socket_file path to the socket file + * @return the socket fd + */ +int acpi_create_socket(char *socket_file); + +/** + * @brief close the socket connection + * + * @param sockfd the sockets fd + * @return 0 on success 1 on error + */ +int acpi_close_socket(int sockfd); diff --git a/include/led.h b/include/led.h index a42ae6d..085f8f9 100644 --- a/include/led.h +++ b/include/led.h @@ -1,2 +1,16 @@ +#pragma once + +/** + * @brief create the led thread + * + * @param file file to send blinking signals to + * @return 0 on success + */ int led_create_thread(char *file); + +/** + * @brief set the led rate + * + * @param r number of blinks per second + */ void led_set_rate(double rate); diff --git a/include/utils.h b/include/utils.h index a16d9fc..5fb0430 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,2 +1,20 @@ +#pragma once + +/** + * @brief get a substring of a string + * + * @param s string + * @param pos first pos + * @param l length + * @return the substring + */ char *get_substring(char *s, int pos, int l); + +/** + * @brief concatinate two strings + * + * @param s1 first + * @param s2 second + * @return the concatinated string + */ char *concat(const char *s1, const char *s2); diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..c012265 --- /dev/null +++ b/meson.build @@ -0,0 +1,29 @@ +project('acpi-event-handler', 'c') + +add_project_arguments([ + '-DLOG_USE_COLOR' # enable colored logs +], language: 'c') + +srcfiles = files( + 'src/main.c', + 'src/led.c', + 'src/acpi.c', + 'src/utils.c' +) + +cc = meson.get_compiler('c') +math_dep = cc.find_library('m', required: true) + +executable('eh', srcfiles, + dependencies: [ + math_dep + ], + include_directories: [ + 'lib/log.c/src', + 'include' + ], + link_with: [ + static_library('log.c', 'lib/log.c/src/log.c', + include_directories: 'lib/log.c/src') + ] +) diff --git a/include/acpi.c b/src/acpi.c similarity index 79% rename from include/acpi.c rename to src/acpi.c index 1abdbee..bb2bbd1 100644 --- a/include/acpi.c +++ b/src/acpi.c @@ -9,13 +9,6 @@ #include "acpi.h" #include "utils.h" -/** - * @brief parse output of acpid - * - * @param str input from acpi socket - * @param out the parsed version of it - * @return 0 on success - */ int acpi_parse(char *str, char **out) { @@ -46,11 +39,6 @@ acpi_parse(char *str, char **out) return 0; } -/** - * @brief cleanup the results of acpi_parse - * - * @param out the pointer to the output from acpi_parse - */ void acpi_clean_parse(char *out[4]) { @@ -63,12 +51,6 @@ acpi_clean_parse(char *out[4]) } } -/** - * @brief create a new acpi socket connection - * - * @param socket_file path to the socket file - * @return the socket fd - */ int acpi_create_socket(char *socket_file) { @@ -98,12 +80,6 @@ acpi_create_socket(char *socket_file) return sock_fd; } -/** - * @brief close the socket connection - * - * @param sockfd the sockets fd - * @return 0 on success 1 on error - */ int acpi_close_socket(int sockfd) { if (close(sockfd) < 0) { diff --git a/include/led.c b/src/led.c similarity index 95% rename from include/led.c rename to src/led.c index d414a2d..76b1a61 100644 --- a/include/led.c +++ b/src/led.c @@ -118,12 +118,6 @@ pre_exit(int signal) exit(0); } -/** - * @brief create the led thread - * - * @param file file to send blinking signals to - * @return 0 on success - */ int led_create_thread(char *file) { @@ -175,11 +169,6 @@ led_create_thread(char *file) return 0; } -/** - * @brief set the led rate - * - * @param r number of blinks per second - */ void led_set_rate(double r) { diff --git a/main.c b/src/main.c similarity index 89% rename from main.c rename to src/main.c index 11c2499..d9fa49b 100644 --- a/main.c +++ b/src/main.c @@ -13,14 +13,12 @@ #include #include "log.h" -#include "include/led.h" -#include "include/acpi.h" -#include "include/utils.h" +#include "led.h" +#include "acpi.h" +#include "utils.h" #include "config.h" -#define SOCKET_FILE "/run/acpid.socket" -#define LED_FILE "/sys/class/leds/input0::capslock/brightness" #define MAX_BUFLEN 1024 char @@ -98,7 +96,7 @@ on_battery_event(char *percent, bool plugged) int main(int argc, char *argv[]) { - bool plugged = plugged_in("/sys/class/power_supply/ACAD/online"); + bool plugged = plugged_in((char *)linefile); char buffer[MAX_BUFLEN], *out[4]; int sock_fd; @@ -106,10 +104,10 @@ main(int argc, char *argv[]) log_set_level(LOG_DEBUG); /* open up socket address */ - sock_fd = acpi_create_socket(SOCKET_FILE); + sock_fd = acpi_create_socket((char *)acpid_sock); /* create the led thread */ - led_create_thread(LED_FILE); + led_create_thread((char *)ledfile); /* run events that need to be run on start to ensure that the current state * inside the program reflects that that is outside the program diff --git a/include/utils.c b/src/utils.c similarity index 76% rename from include/utils.c rename to src/utils.c index 600f336..dda66ee 100644 --- a/include/utils.c +++ b/src/utils.c @@ -5,14 +5,6 @@ #include "utils.h" #include "log.h" -/** - * @brief get a substring of a string - * - * @param s string - * @param pos first pos - * @param l length - * @return the substring - */ char *get_substring(char *s, int pos, int l) { @@ -41,13 +33,6 @@ char return out; } -/** - * @brief concatinate two strings - * - * @param s1 first - * @param s2 second - * @return the concatinated string - */ char *concat(const char *s1, const char *s2) { @@ -58,7 +43,7 @@ char len2 = strlen(s2); /* attempt to allocate size for the new string */ - result = malloc(len1 + len2 + 1); + result = malloc((len1 + len2 + 1) * sizeof(char)); if (result == NULL) { log_fatal("malloc: %s", strerror(errno)); exit(1);