diff --git a/Makefile b/Makefile index a70bdb6..f68eff9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ BUILD_DIR := build SRC_DIR := src INC_DIR := include -DEBUG := 0 VERBOSE := 0 LIBS := xcb-atom @@ -27,10 +26,6 @@ ifeq ($(VERBOSE), 0) Q := @ endif -ifeq ($(DEBUG), 1) - CFLAGS += -g -endif - all: $(BUILD_DIR)/$(BIN) $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c config.h @@ -38,6 +33,7 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c config.h $(PRINTF) "CC" $@ $Q$(COMPILE.c) -o $@ $< + $(BUILD_DIR)/$(BIN): $(OBJS) $(PRINTF) "LD" $@ $Q$(LINK.o) $^ $(LDLIBS) -o $@ @@ -49,8 +45,6 @@ clean: install: $(BUILD_DIR)/$(BIN) $(PRINTF) "INSTALL" $(INSTALL_DIR)/$(BIN) $Qinstall -D -m 755 $< $(INSTALL_DIR)/$(BIN) - cp ./bat.sh /usr/local/bin - cp ./wp.sh /usr/local/bin uninstall: $(PRINTF) "RM" $(INSTALL_DIR)/$(BIN) diff --git a/README.md b/README.md index 1b389f6..69b72da 100644 --- a/README.md +++ b/README.md @@ -90,8 +90,8 @@ You can define your status bar blocks in `config.h`: ```c #define BLOCKS(X) \ ... - X(" ", "wpctl get-volume @DEFAULT_AUDIO_SINK@ | cut -d' ' -f2", 0, 5) \ - X("󰥔 ", "date '+%H:%M:%S'", 1, 1) \ + X("volume", 0, 5), \ + X("date", 1800, 1), \ ... ``` @@ -99,7 +99,6 @@ Each block has the following properties: | Property | Description | | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | -| Icon | An icon you wish to prepend to your block output. | | Command | The command you wish to execute in your block. | | Update interval | Time in seconds, after which you want the block to update. If `0`, the block will never be updated. | | Update signal | Signal to be used for triggering the block. Must be a positive integer. If `0`, a signal won't be set up for the block and it will be unclickable. | diff --git a/bat.sh b/bat.sh deleted file mode 100755 index 1e01ab5..0000000 --- a/bat.sh +++ /dev/null @@ -1,14 +0,0 @@ -status=$(cat /sys/class/power_supply/BAT0/status) -level=$(cat /sys/class/power_supply/BAT0/capacity) - -case $status in - Full) - echo $level%o - ;; - Charging) - echo $level%+ - ;; - Discharging) - echo $level%- - ;; -esac diff --git a/config.h b/config.h index 781907b..a02c0ca 100644 --- a/config.h +++ b/config.h @@ -2,13 +2,13 @@ #define CONFIG_H // String used to delimit block outputs in the status. -#define DELIMITER " " +#define DELIMITER " " // Maximum number of Unicode characters that a block can output. #define MAX_BLOCK_OUTPUT_LENGTH 45 // Control whether blocks are clickable. -#define CLICKABLE_BLOCKS 0 +#define CLICKABLE_BLOCKS 1 // Control whether a leading delimiter should be prepended to the status. #define LEADING_DELIMITER 0 @@ -16,10 +16,17 @@ // Control whether a trailing delimiter should be appended to the status. #define TRAILING_DELIMITER 0 -// Define blocks for the status feed as X(icon, cmd, interval, signal). -#define BLOCKS(X) \ - X(" ", "date +%H:%M:%S", 1, 1) \ - X(" ", "bat.sh", 1, 1) \ - X(" ", "wp.sh", 1, 1) +// Define blocks for the status feed as X(cmd, interval, signal). +#define BLOCKS(X) \ + X("sb-mail", 600, 1) \ + X("sb-music", 0, 2) \ + X("sb-disk", 1800, 3) \ + X("sb-memory", 10, 4) \ + X("sb-loadavg", 5, 5) \ + X("sb-mic", 0, 6) \ + X("sb-record", 0, 7) \ + X("sb-volume", 0, 8) \ + X("sb-battery", 5, 9) \ + X("sb-date", 1, 10) #endif // CONFIG_H diff --git a/include/block.h b/include/block.h index c4f8d54..bb87bcb 100644 --- a/include/block.h +++ b/include/block.h @@ -1,15 +1,14 @@ #ifndef BLOCK_H #define BLOCK_H +#include #include -#include #include #include "config.h" #include "util.h" typedef struct { - const char *const icon; const char *const command; const unsigned int interval; const int signal; @@ -19,11 +18,12 @@ typedef struct { pid_t fork_pid; } block; -block block_new(const char *const icon, const char *const command, - const unsigned int interval, const int signal); +block block_new(const char *const command, const unsigned int interval, + const int signal); int block_init(block *const block); int block_deinit(block *const block); int block_execute(block *const block, const uint8_t button); int block_update(block *const block); +bool block_must_run(const block *const block, const unsigned int time); #endif // BLOCK_H diff --git a/include/cli.h b/include/cli.h index 2f93f62..3d2d5a5 100644 --- a/include/cli.h +++ b/include/cli.h @@ -7,6 +7,7 @@ typedef struct { bool is_debug_mode; } cli_arguments; -cli_arguments cli_parse_arguments(const char* const argv[], const int argc); +int cli_init(cli_arguments* const args, const char* const argv[], + const int argc); #endif // CLI_H diff --git a/include/signal-handler.h b/include/signal-handler.h index da2d471..dc70433 100644 --- a/include/signal-handler.h +++ b/include/signal-handler.h @@ -1,7 +1,7 @@ #ifndef SIGNAL_HANDLER_H #define SIGNAL_HANDLER_H -#include +#include #include "block.h" #include "timer.h" diff --git a/include/timer.h b/include/timer.h index 1ec7f75..eccf4a1 100644 --- a/include/timer.h +++ b/include/timer.h @@ -2,7 +2,6 @@ #define TIMER_H #include -#include #include "block.h" @@ -16,6 +15,5 @@ typedef struct { timer timer_new(const block *const blocks, const unsigned short block_count); int timer_arm(timer *const timer); -bool timer_must_run_block(const timer *const timer, const block *const block); #endif // TIMER_H diff --git a/include/watcher.h b/include/watcher.h index ff31809..63d3283 100644 --- a/include/watcher.h +++ b/include/watcher.h @@ -1,8 +1,8 @@ #ifndef WATCHER_H #define WATCHER_H -#include #include +#include #include "block.h" #include "main.h" diff --git a/src/block.c b/src/block.c index a6c919d..c232d43 100644 --- a/src/block.c +++ b/src/block.c @@ -1,8 +1,9 @@ #include "block.h" +#include +#include #include #include -#include #include #include #include @@ -13,10 +14,9 @@ #include "config.h" #include "util.h" -block block_new(const char *const icon, const char *const command, - const unsigned int interval, const int signal) { +block block_new(const char *const command, const unsigned int interval, + const int signal) { block block = { - .icon = icon, .command = command, .interval = interval, .signal = signal, @@ -145,3 +145,15 @@ int block_update(block *const block) { return 0; } + +bool block_must_run(const block *const block, const unsigned int time) { + if (time == 0) { + return true; + } + + if (block->interval == 0) { + return false; + } + + return time % block->interval == 0; +} diff --git a/src/cli.c b/src/cli.c index 8b94cc8..5aa7df3 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1,33 +1,30 @@ #include "cli.h" -#include #include #include #include -cli_arguments cli_parse_arguments(const char* const argv[], const int argc) { - errno = 0; - cli_arguments args = { - .is_debug_mode = false, - }; +int cli_init(cli_arguments *const args, const char *const argv[], + const int argc) { + args->is_debug_mode = false; int opt = -1; opterr = 0; // Suppress getopt's built-in invalid opt message - while ((opt = getopt(argc, (char* const*)argv, "dh")) != -1) { + while ((opt = getopt(argc, (char *const *)argv, "dh")) != -1) { switch (opt) { case 'd': - args.is_debug_mode = true; + args->is_debug_mode = true; break; + case 'h': + // fall through case '?': (void)fprintf(stderr, "error: unknown option `-%c'\n", optopt); // fall through - case 'h': - // fall through default: (void)fprintf(stderr, "usage: %s [-d]\n", BINARY); - errno = 1; + return 1; } } - return args; + return 0; } diff --git a/src/main.c b/src/main.c index 747b075..a858d88 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,5 @@ #include "main.h" -#include #include #include @@ -39,10 +38,10 @@ static int deinit_blocks(block *const blocks, static int execute_blocks(block *const blocks, const unsigned short block_count, - const timer *const timer) { + const unsigned int time) { for (unsigned short i = 0; i < block_count; ++i) { block *const block = &blocks[i]; - if (!timer_must_run_block(timer, block)) { + if (!block_must_run(block, time)) { continue; } @@ -56,7 +55,7 @@ static int execute_blocks(block *const blocks, static int trigger_event(block *const blocks, const unsigned short block_count, timer *const timer) { - if (execute_blocks(blocks, block_count, timer) != 0) { + if (execute_blocks(blocks, block_count, timer->time) != 0) { return 1; } @@ -69,7 +68,7 @@ static int trigger_event(block *const blocks, const unsigned short block_count, static int refresh_callback(block *const blocks, const unsigned short block_count) { - if (execute_blocks(blocks, block_count, NULL) != 0) { + if (execute_blocks(blocks, block_count, 0) != 0) { return 1; } @@ -118,8 +117,8 @@ static int event_loop(block *const blocks, const unsigned short block_count, } int main(const int argc, const char *const argv[]) { - const cli_arguments cli_args = cli_parse_arguments(argv, argc); - if (errno != 0) { + cli_arguments cli_args; + if (cli_init(&cli_args, argv, argc) != 0) { return 1; } @@ -128,8 +127,7 @@ int main(const int argc, const char *const argv[]) { return 1; } -#define BLOCK(icon, command, interval, signal) \ - block_new(icon, command, interval, signal), +#define BLOCK(command, interval, signal) block_new(command, interval, signal), block blocks[BLOCK_COUNT] = {BLOCKS(BLOCK)}; #undef BLOCK const unsigned short block_count = LEN(blocks); diff --git a/src/signal-handler.c b/src/signal-handler.c index 7a1a966..f4f5c86 100644 --- a/src/signal-handler.c +++ b/src/signal-handler.c @@ -1,7 +1,8 @@ #include "signal-handler.h" +#include +#include #include -#include #include #include #include @@ -109,8 +110,6 @@ int signal_handler_process(signal_handler *const handler, timer *const timer) { // fall through case SIGINT: return 1; - default: - break; } for (unsigned short i = 0; i < handler->block_count; ++i) { diff --git a/src/status.c b/src/status.c index cf0911a..a3a4794 100644 --- a/src/status.c +++ b/src/status.c @@ -49,7 +49,6 @@ bool status_update(status *const status) { } #endif - (void)strncat(status->current, block->icon, LEN(block->output)); (void)strncat(status->current, block->output, LEN(block->output)); } } diff --git a/src/timer.c b/src/timer.c index 2ee555b..a9a383c 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,7 +1,6 @@ #include "timer.h" #include -#include #include #include @@ -33,12 +32,10 @@ static unsigned int compute_reset_value(const block *const blocks, } timer timer_new(const block *const blocks, const unsigned short block_count) { - const unsigned int reset_value = compute_reset_value(blocks, block_count); - timer timer = { - .time = reset_value, // Initial value to execute all blocks. + .time = 0, .tick = compute_tick(blocks, block_count), - .reset_value = reset_value, + .reset_value = compute_reset_value(blocks, block_count), }; return timer; @@ -54,19 +51,7 @@ int timer_arm(timer *const timer) { } // Wrap `time` to the interval [1, reset_value]. - timer->time = (timer->time + timer->tick) % timer->reset_value; + timer->time = (timer->time + timer->tick) % timer->reset_value + 1; return 0; } - -bool timer_must_run_block(const timer *const timer, const block *const block) { - if (timer == NULL || timer->time == timer->reset_value) { - return true; - } - - if (block->interval == 0) { - return false; - } - - return timer->time % block->interval == 0; -} diff --git a/src/watcher.c b/src/watcher.c index 71b6c52..972559a 100644 --- a/src/watcher.c +++ b/src/watcher.c @@ -1,9 +1,9 @@ #include "watcher.h" #include -#include #include #include +#include #include "block.h" #include "util.h" diff --git a/wp.sh b/wp.sh deleted file mode 100755 index a141182..0000000 --- a/wp.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -vol=$(echo "$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk -F' ' '{print $2}') * 100" | bc) -status=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | grep -q "[MUTED]") - -if wpctl get-volume @DEFAULT_AUDIO_SINK@| grep -q "[MUTED]"; then - echo "mut": $vol% - exit -fi - -echo vol: $vol%