feat(build): use meson so I don't end up ripping my hair out

This commit is contained in:
2025-09-21 14:15:09 -04:00
parent 961e0472f5
commit 6e075bff4a
11 changed files with 102 additions and 89 deletions

View File

@@ -1,115 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include "log.h"
#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)
{
int i, section_num, last_section_pos;
size_t section_size;
if (strlen(str) <= 1) {
log_warn("failed to parse acpid input string: it doesn't exist!");
return 1;
}
for (last_section_pos = section_num = i = 0; i <= strlen(str); i++) {
if (str[i] != ' ' && str[i] != '\n' && str[i] != '\0') {
continue;
}
/* grab correct information of the current section */
section_size = i - last_section_pos;
/* copy over the data into the new buffer */
out[section_num] = get_substring(str, last_section_pos, section_size);
/* set the last_section_pos for the next section */
last_section_pos = i + 1;
section_num++;
}
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])
{
int i;
for (i = 0; i < 4; i++) {
if (out[i]) {
free(out[i]);
}
}
}
/**
* @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)
{
int sock_fd;
struct sockaddr_un addr = { 0 };
/* create a new socket connection */
sock_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sock_fd < 0) {
log_fatal("failed to create a new socket: %s", strerror(errno));
return 1;
}
/* setup the socket */
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_file, strlen(socket_file) + 1);
/* connect */
if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
if (close(sock_fd) < 0) {
log_fatal("failed to close socket close: %s", strerror(errno));
}
log_fatal("failed to connect to the socket: %s", strerror(errno));
return 1;
}
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) {
log_fatal("failed to close socket: %s", strerror(errno));
return 1;
}
return 0;
}

View File

@@ -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);

View File

@@ -1,214 +0,0 @@
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <unistd.h>
#include "led.h"
#include "log.h"
#define Z() if (data->rate > 0) { \
usleep(1000000 / data->rate); \
}
struct Data {
char *file_name;
FILE *pfile;
double rate;
};
/* global data accessed by all threads */
static struct Data *data;
pthread_t thread;
/* mutex to lock the thread */
static pthread_mutex_t mp = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
/**
* @brief helper function to set the led rate
*
* @param value set the contents of the led file
* @param d data
*/
static inline void
led_set(char value, struct Data *d)
{
d->pfile = fopen(d->file_name, "w");
if (d->pfile == NULL) {
log_warn("failed to open %s fopen: %s", d->file_name, strerror(errno));
return;
}
if (fputc(value, d->pfile) == EOF) {
log_warn("failed to write to %s fopen: %s", d->file_name, strerror(errno));
/* According to the man page, after erroring doing anything including
* fclose may result in undefined behavior. */
return;
}
fclose(d->pfile);
}
/**
* @brief main thread loop
*
* @param args args
*/
static void
*run(void *args)
{
int err;
struct Data *d = (struct Data *)args;
for (;;) {
/* turn off the led before doing anything else */
led_set('0', d);
/* wait for the condition to change */
while(d->rate == 0) {
if ((err = pthread_cond_wait(&cv, &mp)) != 0) {
log_fatal("pthread_cond_wait: %s", strerror(err));
pthread_exit(0);
}
}
/* exit if we're told to */
if (d->rate <= -1) {
log_debug("received rate of -1, exiting");
pthread_exit(0);
}
Z();
led_set('1', d);
Z();
}
pthread_exit(0);
}
/**
* @brief run before exiting the program
* this is for cleaning up all the messes we've made
*
* @param signal signal
*/
static void
pre_exit(int signal)
{
led_set_rate(-1);
pthread_cond_destroy(&cv);
pthread_mutex_destroy(&mp);
if (data->pfile) {
data->pfile = fopen(data->file_name, "w");
}
fputc('0', data->pfile);
/* make sure to cleanup the allocated data */
fclose(data->pfile);
free(data->file_name);
free(data);
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)
{
int err;
struct sigaction act;
static bool running;
/* make sure the led thread cannot be created twice */
if (running) {
log_warn("led thread has already been created");
return 1;
}
/* zero the array, and setup sig handler and exit handler */
memset(&act, 0, sizeof(act));
act.sa_handler = &pre_exit;
if (sigaction(SIGINT, &act, NULL) < 0) {
log_fatal("failed to create sigaction: %s, memory is gonna leak",
strerror(errno));
}
/* setup data */
data = calloc(1, sizeof(struct Data));
data->file_name = malloc(sizeof(char) * (strlen(file) + 1));
if (data->file_name == NULL) {
log_fatal("failed to create led thread: malloc of led file name failed: %s",
strerror(errno));
exit(1);
}
strcpy(data->file_name, file);
data->rate = 0;
/* attempt to create the thread */
if ((err = pthread_create(&thread, NULL, run, data)) != 0) {
log_fatal("failed to create led thread: pthread_create: %s", strerror(err));
return -1;
}
/* detach the thread to make cleanup nice and easy */
if ((err = pthread_detach(thread)) != 0) {
log_fatal("failed to create led thread: pthread_detach: %s", strerror(err));
return -1;
}
log_info("started LED thread");
running = true;
return 0;
}
/**
* @brief set the led rate
*
* @param r number of blinks per second
*/
void
led_set_rate(double r)
{
int err;
static bool locked = false;
/* there's no reason to set the rate if it's already the same */
if (r == data->rate) {
return;
}
/* lock the thread, update, and unlock it */
if (!locked) {
if ((err = pthread_mutex_lock(&mp)) != 0) {
log_fatal("failed to lock the mutex: %s", strerror(errno));
}
}
/* set the rate */
data->rate = r;
log_debug("set rate to: %f", r);
pthread_cond_signal(&cv);
if (!locked) {
if ((err = pthread_mutex_unlock(&mp)) != 0) {
log_fatal("failed to lock the mutex: %s", strerror(errno));
}
}
/* make sure we don't lock the thread in an infinite loop */
locked = (r != 0);
}

View File

@@ -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);

View File

@@ -1,72 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#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)
{
int i = 0;
char *out;
if (l <= 0) {
return NULL;
}
out = malloc((l + 1) * sizeof(char));
if (out == NULL) {
log_fatal("failed to get substring malloc: %s", strerror(errno));
exit(1);
}
/* Copy substring into ss */
while (i < l) {
out[i] = s[pos + i];
i++;
}
/* Null terminate the substring */
out[i] = '\0';
return out;
}
/**
* @brief concatinate two strings
*
* @param s1 first
* @param s2 second
* @return the concatinated string
*/
char
*concat(const char *s1, const char *s2)
{
int len1, len2;
char *result;
len1 = strlen(s1);
len2 = strlen(s2);
/* attempt to allocate size for the new string */
result = malloc(len1 + len2 + 1);
if (result == NULL) {
log_fatal("malloc: %s", strerror(errno));
exit(1);
}
memcpy(result, s1, len1);
memcpy(result + len1, s2, len2 + 1);
// + 1 copies the null terminator
return result;
}

View File

@@ -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);