add a module system

This commit is contained in:
randomuser 2022-09-20 16:29:25 -05:00
parent f44a1c3ffe
commit c97943855f
5 changed files with 45 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
tags
bspc
tstatus

View File

@ -1,8 +1,8 @@
bspc:
cc bspc.c -o bspc -lxcb -Wall -Wextra -std=c99
tstatus:
cc tstatus.c -o tstatus -lxcb -Wall -Wextra -std=c99
debug:
cc bspc.c -o bspc -lxcb -Wall -Wextra -std=c99 -ggdb
cc tstatus.c -o tstatus -lxcb -Wall -Wextra -std=c99 -ggdb
clean:
rm bspc
rm tstatus

30
bspc.c
View File

@ -27,6 +27,8 @@
#define SOCKET_ENV_VAR "BSPWM_SOCKET"
#define FAILURE_MESSAGE "\x07"
#define LENGTH(x) (sizeof(x) / sizeof(*x))
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
@ -42,6 +44,8 @@
#include <fcntl.h>
#include <ctype.h>
#include "module.h"
/* prototypes */
void warn(char *fmt, ...);
void err(char *fmt, ...);
@ -279,23 +283,22 @@ char *send_msg_to_bspwm(char *args[], int count)
return NULL;
}
int main(void) {
char final[64], current[2];
int bspwm_update(struct module *module) {
char current[2];
char *occupied[] = {"query", "-D", "-d", ".occupied", "--names"};
char *focused[] = {"query", "-D", "-d", ".focused", "--names"};
char *result;
int count = 5;
result = send_msg_to_bspwm(occupied, count);
result = send_msg_to_bspwm(occupied, LENGTH(occupied));
if(!result) {
printf("error: sending message to bspwm failed!\n");
return 1;
}
memcpy(&final, result, 64);
memcpy(&module->buffer, result, MODULE_BUFFER_LEN);
result = send_msg_to_bspwm(focused, count);
result = send_msg_to_bspwm(focused, LENGTH(focused));
if(!result) {
printf("error: sending message to bspwm failed!\n");
@ -305,19 +308,18 @@ int main(void) {
memcpy(&current, result, 2);
current[1] = '\0';
for(int i = 0; i < 64; i++) {
if(final[i] == '\0') {
for(int i = 0; i < MODULE_BUFFER_LEN; i++) {
if(module->buffer[i] == '\0') {
/* XXX: susceptible to buffer underflow */
if (i) final[i - 1] = '\0';
if(i) module->buffer[i - 1] = '\0';
break;
}
if(isdigit(final[i]) && *current == final[i])
if(isdigit(module->buffer[i]) && *current == module->buffer[i])
/* XXX: susceptible to buffer overflow */
final[i + 1] = '<';
if(final[i] == '\n')
final[i] = ' ';
if(i == MODULE_BUFFER_LEN) module->buffer[i + 1] = '<';
if(module->buffer[i] == '\n')
module->buffer[i] = ' ';
}
printf("%s", final);
return 0;
}

11
module.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef TSTATUS_MODULE_H
#define MODULE_BUFFER_LEN 64
struct module {
int update; /* update interval in seconds */
int termcode; /* update termcode */
int (*updatecallback)(struct module *); /* update function */
char buffer[MODULE_BUFFER_LEN]; /* buffer for text */
};
#endif
#define TSTATUS_MODULE_H

13
tstatus.c Normal file
View File

@ -0,0 +1,13 @@
#include "bspc.c"
#include "module.h"
struct module table[] = {
{0, 10, bspwm_update, {'\0'}},
};
int main(void) {
table[0].updatecallback(&table[0]);
printf("%s\n", table[0].buffer);
return 0;
}