- Commit
- c88e368b14682071e899b552ae30c7457b2fb1ad
- Parent
- 50b8e64aaf48c60685e1312a3fe6790e1610ff91
- Author
- Pablo <pablo-pie@riseup.net>
- Date
Added a small header for integers and floats
Simple interface to set PulseAudio's volume, with graphical feedback! 🔊
Added a small header for integers and floats
5 files changed, 200 insertions, 158 deletions
| Status | Name | Changes | Insertions | Deletions |
| Modified | nob.c | 2 files changed | 4 | 2 |
| Modified | src/configure.c | 2 files changed | 91 | 92 |
| Added | src/ints.h | 1 file changed | 41 | 0 |
| Modified | src/main.c | 2 files changed | 59 | 59 |
| Modified | src/pcm.h | 2 files changed | 5 | 5 |
diff --git a/nob.c b/nob.c @@ -10,6 +10,8 @@ #define BUILD_SRC_DIR BUILD_DIR "src/" #define ASSETS_DIR "./assets/" +#include "src/ints.h" + #define SRC_PATH SRC_DIR "main.c" #define OUTPUT_PATH BUILD_DIR "chvol" @@ -32,7 +34,7 @@ const char* srcs[] = { CONFIGURE_SRC_PATH, }; -int main(int argc, char** argv) +i32 main(i32 argc, char** argv) { NOB_GO_REBUILD_URSELF(argc, argv); shift(argv, argc); // discard program name @@ -62,7 +64,7 @@ int main(int argc, char** argv) if (argc > 0 && (strcmp(shift(argv, argc), "--") == 0)) { cmd_append(&cmd, OUTPUT_PATH); - for (int i = 0; i < argc; i++) cmd_append(&cmd, argv[i]); + for (i32 i = 0; i < argc; i++) cmd_append(&cmd, argv[i]); if (!cmd_run(&cmd)) return EXIT_FAILURE; }
diff --git a/src/configure.c b/src/configure.c @@ -1,6 +1,5 @@ #include <stdlib.h> #include <stdio.h> -#include <stdint.h> #include <assert.h> #include <ctype.h> #include <errno.h> @@ -14,6 +13,7 @@ #define ARENA_IMPLEMENTATION #include "arena.h" #include "pcm.h" +#include "ints.h" #define OUTPUT_PATH "./build/src/config.h" @@ -28,27 +28,27 @@ ##__VA_ARGS__) typedef struct { - size_t window_width; - size_t window_height; - size_t window_progress_height; - size_t window_padding; - size_t window_border; - size_t window_timeout_ms; - - size_t volume_inc; - size_t volume_cap; + u64 window_width; + u64 window_height; + u64 window_progress_height; + u64 window_padding; + u64 window_border; + u64 window_timeout_ms; + + u64 volume_inc; + u64 volume_cap; const char* volume_audio_sample; - size_t color_background; - size_t color_light_background; - size_t color_accent; - size_t color_warning; + u64 color_background; + u64 color_light_background; + u64 color_accent; + u64 color_warning; } Config; -bool parse_config(const char* path, Config* config); -bool parse_sample(const char* path, PCM* pcm); +b8 parse_config(const char* path, Config* config); +b8 parse_sample(const char* path, PCM* pcm); -int main(int argc, char** argv) +i32 main(i32 argc, char** argv) { if (argc < 2) { report("No config file provided"); @@ -88,7 +88,7 @@ int main(int argc, char** argv) fprintf(f, "int16_t volume_change_samples_buff[%zu] = {", sample.samples_len); - for (size_t i = 0; i < sample.samples_len; i++) { + for (u64 i = 0; i < sample.samples_len; i++) { if (i % 8 == 0) fprintf(f, "\n "); else fprintf(f, " "); if (sample.samples[i] >= 0) fprintf(f, " 0x%04X,", sample.samples[i]); @@ -107,9 +107,9 @@ int main(int argc, char** argv) return EXIT_SUCCESS; } -size_t log_floor(size_t n) +u64 log_floor(u64 n) { - size_t result; + u64 result; if (n == 0) return 1; for (result = 0; n > 0; result++, n /= 10); return result; @@ -119,7 +119,7 @@ size_t log_floor(size_t n) #define VORBIS_READ_CHUNK 4096 // TODO: verify this -const char *ov_error_str(int e){ +const char *ov_error_str(i32 e){ switch(e){ case 0: return "OK"; case OV_EREAD: return "read error"; @@ -132,10 +132,10 @@ const char *ov_error_str(int e){ } -bool parse_sample(const char* path, PCM* pcm) +b8 parse_sample(const char* path, PCM* pcm) { OggVorbis_File f; - int ret = ov_fopen(path, &f); + i32 ret = ov_fopen(path, &f); if (ret < 0) { report("Could not open \"%s\": %s", path, ov_error_str(ret)); return false; @@ -148,29 +148,29 @@ bool parse_sample(const char* path, PCM* pcm) return false; } - int channels = info->channels; - long sample_rate = info->rate; + i32 channels = info->channels; + i64 sample_rate = info->rate; assert(sample_rate); - assert(channels && channels < UINT8_MAX); - pcm->sample_rate = (size_t)sample_rate; - pcm->channels = (uint8_t)channels; + assert(channels && channels < U8_MAX); + pcm->sample_rate = (u64)sample_rate; + pcm->channels = (u8)channels; - int16_t* buff = malloc(5 * VORBIS_READ_CHUNK); - size_t buff_len = 0; + i16* buff = malloc(5 * VORBIS_READ_CHUNK); + u64 buff_len = 0; - int section; + i32 section; char read_buff[VORBIS_READ_CHUNK]; for (;;) { - long ret = ov_read(&f, read_buff, VORBIS_READ_CHUNK, 0, 2, 1, §ion); + i64 ret = ov_read(&f, read_buff, VORBIS_READ_CHUNK, 0, 2, 1, §ion); if (ret == 0) break; if (ret < 0) { report("Failed to decode Vorbis: %s", ov_error_str(ret)); return false; } - size_t new_samples = ret / sizeof(buff[0]); + u64 new_samples = ret / sizeof(buff[0]); buff = realloc(buff, (buff_len + new_samples) * sizeof(buff[0])); assert(buff && "buy more RAM!"); @@ -189,26 +189,26 @@ bool parse_sample(const char* path, PCM* pcm) // = string view & string builder ============================================= typedef struct { const char* data; - size_t len; + u64 len; } SV; typedef struct { - char* data; - size_t len; - size_t capacity; + char* data; + u64 len; + u64 capacity; } SB; #define SB_CHUNCK_CAP 512 -bool sv_equals(SV str1, const char* str2) +b8 sv_equals(SV str1, const char* str2) { if (str1.len != strlen(str2)) return false; return memcmp(str1.data, str2, str1.len) == 0; } -bool sv_starts_with(SV str, const char* prefix) +b8 sv_starts_with(SV str, const char* prefix) { - size_t prefix_len = strlen(prefix); + u64 prefix_len = strlen(prefix); if (str.len < prefix_len) return false; return memcmp(str.data, prefix, prefix_len) == 0; @@ -239,11 +239,11 @@ typedef struct { const char* src_path; const char* data; - size_t cursor; - size_t len; + u64 cursor; + u64 len; - size_t current_line; - size_t current_line_start; + u64 current_line; + u64 current_line_start; SB sb; Arena* allocator; @@ -270,8 +270,8 @@ const char* token_kind_str[TOKEN_KIND_COUNT] = { typedef struct { const char* src_path; - size_t line; - size_t offset; + u64 line; + u64 offset; SV text_start; } Location; @@ -281,7 +281,7 @@ typedef struct { union { SV as_text; - size_t as_int; + u64 as_int; const char* as_str; }; TokenKind kind; @@ -316,13 +316,13 @@ void location_report_end(Location loc, LogLevel level) { SV text = loc.text_start; - int len; - for (len = 0; len < (int)text.len && text.data[len] != '\n'; len++); + i32 len; + for (len = 0; len < (i32)text.len && text.data[len] != '\n'; len++); fprintf(stderr, " %zu | %.*s\n ", loc.line, len, text.data); - for (size_t i = 0; i < log_floor(loc.line); i++) fprintf(stderr, " "); + for (u64 i = 0; i < log_floor(loc.line); i++) fprintf(stderr, " "); fprintf(stderr, " | "); - for (size_t i = 0; i < loc.offset; i++) fprintf(stderr, " "); + for (u64 i = 0; i < loc.offset; i++) fprintf(stderr, " "); switch (level) { case LOG_ERROR: fprintf(stderr, BOLD_RED "^\n" RESET); break; @@ -332,7 +332,7 @@ void location_report_end(Location loc, LogLevel level) } // TODO: support negative numbers! -bool parse_int(SV text, Location loc, size_t* val) +b8 parse_int(SV text, Location loc, u64* val) { *val = 0; @@ -340,23 +340,23 @@ bool parse_int(SV text, Location loc, size_t* val) text.data += 2; text.len -= 2; - for (size_t i = 0; i < text.len; i++) { + for (u64 i = 0; i < text.len; i++) { char c = text.data[i]; *val *= 16; if (isdigit(c)) { - *val += (size_t)(text.data[i] - '0'); + *val += (u64)(text.data[i] - '0'); } else if (c == '_') { continue; } else if (c >= 'A' && c <= 'F') { - *val += (size_t)(c - 'A' + 10); + *val += (u64)(c - 'A' + 10); } else if (c >= 'a' && c <= 'f') { - *val += (size_t)(c - 'a' + 10); + *val += (u64)(c - 'a' + 10); } else { loc.offset += i + 2; location_report(loc, LOG_ERROR, "Could not parse integer value \"%.*s\"", - (int)text.len, text.data); + (i32)text.len, text.data); return false; } } @@ -364,7 +364,7 @@ bool parse_int(SV text, Location loc, size_t* val) return true; } - for (size_t i = 0; i < text.len; i++) { + for (u64 i = 0; i < text.len; i++) { char c = text.data[i]; if (c == '_') continue; @@ -372,12 +372,12 @@ bool parse_int(SV text, Location loc, size_t* val) loc.offset += i; location_report(loc, LOG_ERROR, "Could not parse integer value \"%.*s\"", - (int)text.len, text.data); + (i32)text.len, text.data); return false; } *val *= 10; - *val += (size_t)(c - '0'); + *val += (u64)(c - '0'); } return true; @@ -405,13 +405,13 @@ void lexer_skip_whitespace(Lexer* l) } } -bool lexer_new(const char* path, Lexer* l, Arena* a) +b8 lexer_new(const char* path, Lexer* l, Arena* a) { memset(l, 0, sizeof(*l)); l->src_path = path; l->allocator = a; - int fd = open(path, O_RDONLY); + i32 fd = open(path, O_RDONLY); if (fd < 0) { report("Could not open \"%s\": %s", path, strerror(errno)); return false; @@ -424,13 +424,13 @@ bool lexer_new(const char* path, Lexer* l, Arena* a) return false; } - size_t len = st.st_size; + u64 len = st.st_size; if (len == 0) return true; const char* buff = malloc(len); assert(buff && "buy more RAM!"); - if (read(fd, (void*)buff, len) != (ssize_t)len) { + if (read(fd, (void*)buff, len) != (i64)len) { report("Could not read \"%s\"", path); free((char*)buff); close(fd); @@ -438,7 +438,7 @@ bool lexer_new(const char* path, Lexer* l, Arena* a) } l->data = buff; - l->len = (size_t)len; + l->len = (u64)len; close(fd); // NOTE: need to clear whitespaces at startup since @@ -447,12 +447,12 @@ bool lexer_new(const char* path, Lexer* l, Arena* a) return true; } -bool lexer_finished(Lexer* l) +b8 lexer_finished(Lexer* l) { return l->cursor >= l->len; } -bool lexer_next(Lexer* l, Token* token) +b8 lexer_next(Lexer* l, Token* token) { Location loc; loc.src_path = l->src_path; @@ -487,13 +487,13 @@ bool lexer_next(Lexer* l, Token* token) text.len++; } - for (size_t i = 0; i < text.len; i++) { + for (u64 i = 0; i < text.len; i++) { char c = text.data[i]; if (!(isalnum(c) || c == ' ' || c == '.')) { loc.offset += i + 1; location_report(loc, LOG_ERROR, "Invalid section header \"%.*s\"", - (int)text.len, text.data); + (i32)text.len, text.data); return false; } } @@ -523,7 +523,7 @@ bool lexer_next(Lexer* l, Token* token) } sb_clear(&l->sb); - bool loop = true; + b8 loop = true; while (loop) { if (lexer_finished(l)) { loc.offset += l->sb.len; @@ -593,7 +593,7 @@ bool lexer_next(Lexer* l, Token* token) token->kind = TOKEN_KEY; token->as_text.data = l->data + l->cursor; - size_t len = 0; + u64 len = 0; for (; l->cursor < l->len; l->cursor++) { char c = l->data[l->cursor]; if (!(isalnum(c) || c == '_')) break; @@ -602,7 +602,7 @@ bool lexer_next(Lexer* l, Token* token) token->as_text.len = len; } else if (isdigit(c)) { - SV text; size_t len = 0; + SV text; u64 len = 0; text.data = l->data + l->cursor; for (; l->cursor < l->len; l->cursor++) { if (!isalnum(l->data[l->cursor])) break; @@ -610,7 +610,7 @@ bool lexer_next(Lexer* l, Token* token) } text.len = len; - size_t val; + u64 val; if (!parse_int(text, loc, &val)) return false; token->kind = TOKEN_INT; @@ -649,11 +649,11 @@ typedef enum { typedef struct { const char* name; const char* section; - size_t offset; + u64 offset; - TokenKind kind; - size_t min_val; - size_t max_val; + TokenKind kind; + u64 min_val; + u64 max_val; } KeyInfo; static_assert(KEYS_COUNT == 13); @@ -678,12 +678,11 @@ KeyInfo key_infos[KEYS_COUNT] = { fprintf(stderr, BOLD_WHITE "%s: " BOLD_RED "error: " RESET \ fmt "\n", (path), ##__VA_ARGS__) -bool check_bounds(const char* path, Key k, Config* config, - size_t min, size_t max) +b8 check_bounds(const char* path, Key k, Config* config, u64 min, u64 max) { KeyInfo info = key_infos[k]; assert(info.kind == TOKEN_INT); - size_t val = *(size_t*)((uint8_t*)config + info.offset); + u64 val = *(u64*)((u8*)config + info.offset); if (val < min) { file_report(path, "Value of field \"%s\" is too small: %zu < %zu", @@ -700,13 +699,13 @@ bool check_bounds(const char* path, Key k, Config* config, return true; } -bool parse_config(const char* path, Config* config) +b8 parse_config(const char* path, Config* config) { Arena allocator = {0}; Lexer l; if (!lexer_new(path, &l, &allocator)) return false; // = parsing ================================================================ - Token token; uint16_t key_set_flag = 0; + Token token; u16 key_set_flag = 0; while (!lexer_finished(&l)) { if (!lexer_next(&l, &token)) return false; if (token.kind == TOKEN_SECTION) continue; // skip section headers @@ -734,8 +733,8 @@ bool parse_config(const char* path, Config* config) return false; } - bool key_found = false; - for (size_t k = 0; k < KEYS_COUNT; k++) { + b8 key_found = false; + for (u64 k = 0; k < KEYS_COUNT; k++) { KeyInfo info = key_infos[k]; if (!sv_equals(key, info.name)) continue; @@ -749,8 +748,8 @@ bool parse_config(const char* path, Config* config) key_set_flag |= 1 << k; - uint8_t* field_ptr = (uint8_t*)config + info.offset; - if (info.kind == TOKEN_INT) *(size_t*) field_ptr = token.as_int; + u8* field_ptr = (u8*)config + info.offset; + if (info.kind == TOKEN_INT) *(u64*) field_ptr = token.as_int; else *(const char**)field_ptr = token.as_str; key_found = true; @@ -759,12 +758,12 @@ bool parse_config(const char* path, Config* config) if (!key_found) { location_report(loc, LOG_WARNING, "Ignoring unknown field \"%.*s\"", - (int)key.len, key.data); + (i32)key.len, key.data); } } // = validation ============================================================= - for (size_t k = 0; k < KEYS_COUNT; k++) { + for (u64 k = 0; k < KEYS_COUNT; k++) { KeyInfo info = key_infos[k]; if (!(key_set_flag & (1 << k))) { @@ -777,7 +776,7 @@ bool parse_config(const char* path, Config* config) if (!check_bounds(path, KEY_WINDOW_WIDTH, config, 1, 0)) return false; if (!check_bounds(path, KEY_WINDOW_HEIGHT, config, 1, 0)) return false; - size_t size = config->window_width; + u64 size = config->window_width; if (config->window_height > size) size = config->window_height; if (!check_bounds(path, KEY_WINDOW_PROGRESS_HEIGHT, @@ -787,10 +786,10 @@ bool parse_config(const char* path, Config* config) if (!check_bounds(path, KEY_WINDOW_TIMEOUT_MS, config, 1, 0)) return false; - if (!check_bounds(path, KEY_VOLUME_INC, config, 1, UINT8_MAX)) return false; - if (!check_bounds(path, KEY_VOLUME_CAP, config, 1, UINT8_MAX)) return false; + if (!check_bounds(path, KEY_VOLUME_INC, config, 1, U8_MAX)) return false; + if (!check_bounds(path, KEY_VOLUME_CAP, config, 1, U8_MAX)) return false; - for (size_t k = KEY_COLOR_BACKGROUND; k <= KEY_COLOR_WARNING; k++) { + for (u64 k = KEY_COLOR_BACKGROUND; k <= KEY_COLOR_WARNING; k++) { if (!check_bounds(path, k, config, 0, 0xFFFFFF)) return false; }
diff --git /dev/null b/src/ints.h @@ -0,0 +1,41 @@ +#ifndef INTS_H_ +#define INTS_H_ + +#include <assert.h> + +typedef char i8; +typedef short i16; +typedef int i32; +typedef long i64; + +static_assert(sizeof(i8) == 1); +static_assert(sizeof(i16) == 2); +static_assert(sizeof(i32) == 4); +static_assert(sizeof(i64) == 8); + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long u64; + +static_assert(sizeof(u8) == 1); +static_assert(sizeof(u16) == 2); +static_assert(sizeof(u32) == 4); +static_assert(sizeof(u64) == 8); + +#define U8_MAX 0xFF +#define U16_MAX 0xFFFF +#define U32_MAX 0xFFFFFFFF +#define U64_MAX 0xFFFFFFFFFFFFFFFF + +typedef float f32; +typedef double f64; + +static_assert(sizeof(f32) == 4); +static_assert(sizeof(f64) == 8); + +typedef u8 b8; +#define true 1 +#define false 0 + +#endif // INTS_H_
diff --git a/src/main.c b/src/main.c @@ -17,9 +17,10 @@ #include <pulse/simple.h> #include <pulse/error.h> +#include "ints.h" #include "config.h" -static_assert(VOLUME_CAP <= UINT8_MAX); +//static_assert(VOLUME_CAP <= U8_MAX); typedef enum { CMD_VOL_GET = 0, @@ -32,19 +33,19 @@ typedef enum { typedef struct { CmdKind kind; union { - uint8_t vol_percent; - bool is_dec; + u8 vol_percent; + b8 is_dec; }; - bool show_graphics; + b8 show_graphics; } Cmd; typedef struct { - size_t width; - size_t height; - uint32_t* pixels; + u64 width; + u64 height; + u32* pixels; } Canvas; -bool pa_run_cmd(Cmd cmd); +b8 pa_run_cmd(Cmd cmd); void usage(char* program_name) { @@ -52,7 +53,7 @@ void usage(char* program_name) fprintf(stderr, " %s [up|down|mute|mute-mic] [-g]\n", program_name); } -bool parse_percent(char* str, uint32_t* result) +b8 parse_percent(char* str, u32* result) { char* view = str; *result = 0; @@ -62,7 +63,7 @@ bool parse_percent(char* str, uint32_t* result) if (!isdigit(c)) break; *result *= 10; - *result += (uint32_t)(c - '0'); + *result += (u32)(c - '0'); } if (*view == '%') view++; @@ -73,13 +74,13 @@ bool parse_percent(char* str, uint32_t* result) #define shift_args(argc, argv) (assert((argc) > 0), (argc)--, *(argv)++) -int main(int argc, char** argv) +i32 main(i32 argc, char** argv) { char* program_name = shift_args(argc, argv); Cmd cmd = {0}; char* arg; - bool cmd_set = false; + b8 cmd_set = false; while (argc > 0) { arg = shift_args(argc, argv); @@ -112,7 +113,7 @@ int main(int argc, char** argv) return EXIT_SUCCESS; } else { // usuming the user wants to set the volume - uint32_t percent; + u32 percent; if (!parse_percent(arg, &percent)) { fprintf(stderr, "ERROR: unknown argument \"%s\"\n", arg); usage(program_name); @@ -121,7 +122,7 @@ int main(int argc, char** argv) if (percent > VOLUME_CAP) percent = VOLUME_CAP; cmd.kind = CMD_VOL_SET; - cmd.vol_percent = (uint8_t)percent; + cmd.vol_percent = (u8)percent; } cmd_set = true; @@ -131,52 +132,52 @@ int main(int argc, char** argv) return EXIT_SUCCESS; } -void draw_progress_bar(Canvas* canvas, uint8_t percent) +void draw_progress_bar(Canvas* canvas, u8 percent) { assert(percent <= VOLUME_CAP); - for (size_t i = 0; i < canvas->width * canvas->height; i++) { + for (u64 i = 0; i < canvas->width * canvas->height; i++) { canvas->pixels[i] = COLOR_LIGHT_BACKGROUND; } // = borders ================================================================ - for (size_t y = 0; y < WIN_BORDER; y++) { - for (size_t x = 0; x < canvas->width; x++) { + for (u64 y = 0; y < WIN_BORDER; y++) { + for (u64 x = 0; x < canvas->width; x++) { canvas->pixels[y * canvas->width + x] = COLOR_BACKGROUND; } } - for (size_t y = canvas->height - 1; y >= canvas->height - WIN_BORDER; y--) { - for (size_t x = 0; x < canvas->width; x++) { + for (u64 y = canvas->height - 1; y >= canvas->height - WIN_BORDER; y--) { + for (u64 x = 0; x < canvas->width; x++) { canvas->pixels[y * canvas->width + x] = COLOR_BACKGROUND; } } - for (size_t y = WIN_BORDER; y < canvas->height - WIN_BORDER; y++) { - for (size_t x = 0; x < WIN_BORDER; x++) { + for (u64 y = WIN_BORDER; y < canvas->height - WIN_BORDER; y++) { + for (u64 x = 0; x < WIN_BORDER; x++) { canvas->pixels[y * canvas->width + x] = COLOR_BACKGROUND; } - for (size_t x = canvas->width - WIN_BORDER; x <= canvas->width; x++) { + for (u64 x = canvas->width - WIN_BORDER; x <= canvas->width; x++) { canvas->pixels[y * canvas->width + x] = COLOR_BACKGROUND; } } //// = progress bar =========================================================== - uint32_t bar_color = COLOR_ACCENT; + u32 bar_color = COLOR_ACCENT; if (percent > 100) { bar_color = COLOR_WARNING; percent /= 2; } - size_t progress_start = WIN_PADDING; - size_t progress_end = percent * (canvas->width - 2 * WIN_PADDING)/100 + WIN_PADDING; - size_t progress_full = canvas->width - WIN_PADDING; + u64 progress_start = WIN_PADDING; + u64 progress_end = percent * (canvas->width - 2 * WIN_PADDING)/100 + WIN_PADDING; + u64 progress_full = canvas->width - WIN_PADDING; - for (size_t y = (canvas->height - WIN_PROGRESS_HEIGHT)/2; + for (u64 y = (canvas->height - WIN_PROGRESS_HEIGHT)/2; y < (canvas->height + WIN_PROGRESS_HEIGHT)/2; y++) { - size_t x; + u64 x; for (x = progress_start; x < progress_end; x++) { canvas->pixels[y * canvas->width + x] = bar_color; } @@ -189,11 +190,10 @@ void draw_progress_bar(Canvas* canvas, uint8_t percent) // = X11 bullshit ============================================================= -// TODO: also play sound /* * Draws the progress bar window and closes it after [timeout_ms] millisecs * */ -void x11_launch_window(uint8_t percent, uint32_t timeout_ms) +void x11_launch_window(u8 percent, u32 timeout_ms) { // NOTE: forking is just a cheap way to making the graphics non-blocking pid_t pid = fork(); @@ -210,13 +210,13 @@ void x11_launch_window(uint8_t percent, uint32_t timeout_ms) return; } - int screen = DefaultScreen(d); + i32 screen = DefaultScreen(d); Window root = RootWindow(d, screen); - int screen_width = DisplayWidth(d, screen); - int screen_height = DisplayHeight(d, screen); + i32 screen_width = DisplayWidth(d, screen); + i32 screen_height = DisplayHeight(d, screen); - int depth = DefaultDepth(d, screen); + i32 depth = DefaultDepth(d, screen); Visual* visual = DefaultVisual(d, screen); XSetWindowAttributes swa; @@ -225,8 +225,8 @@ void x11_launch_window(uint8_t percent, uint32_t timeout_ms) swa.event_mask = ExposureMask | ButtonPressMask; // ========================================================================== - int x_offset = (screen_width - WIN_WIDTH )/2; - int y_offset = (screen_height - WIN_HEIGHT)/2; + i32 x_offset = (screen_width - WIN_WIDTH )/2; + i32 y_offset = (screen_height - WIN_HEIGHT)/2; Window win = XCreateWindow( d, root, x_offset, y_offset, WIN_WIDTH, WIN_HEIGHT, 0, depth, InputOutput, @@ -238,8 +238,8 @@ void x11_launch_window(uint8_t percent, uint32_t timeout_ms) XMapWindow(d, win); XFlush(d); - size_t bpp = (depth <= 8) ? 1 : 4; - uint32_t* pixels = malloc(WIN_WIDTH * WIN_HEIGHT * bpp); + u64 bpp = (depth <= 8) ? 1 : 4; + u32* pixels = malloc(WIN_WIDTH * WIN_HEIGHT * bpp); if (!pixels) { fprintf(stderr, "ERROR: couldn't create pixel buffer: buy more RAM\n"); return; @@ -287,11 +287,11 @@ pa_context* ctx = NULL; pa_mainloop* m = NULL; pa_mainloop_api* mainloop_api = NULL; -size_t n_cmds = 0; +u64 n_cmds = 0; void pa_ctx_state_cb(pa_context* ctx, void* data); -bool pa_run_cmd(Cmd cmd) +b8 pa_run_cmd(Cmd cmd) { m = pa_mainloop_new(); mainloop_api = pa_mainloop_get_api(m); @@ -304,7 +304,7 @@ bool pa_run_cmd(Cmd cmd) return EXIT_FAILURE; } - int ret_value = 0; + i32 ret_value = 0; pa_mainloop_run(m, &ret_value); pa_context_disconnect(ctx); @@ -313,7 +313,7 @@ bool pa_run_cmd(Cmd cmd) return ret_value == 0; } -bool pa_play_sample(PCM sample) +b8 pa_play_sample(PCM sample) { assert(sample.channels); if (!sample.samples_len) return true; @@ -323,7 +323,7 @@ bool pa_play_sample(PCM sample) ss.channels = sample.channels; ss.rate = sample.sample_rate; - int pa_err; + i32 pa_err; pa_simple* simple_api = pa_simple_new(NULL, "audio-volume-change", PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &pa_err); @@ -332,11 +332,11 @@ bool pa_play_sample(PCM sample) return false; } - size_t bytes = sample.samples_len * sizeof(sample.samples[0]); - size_t written = 0; - const uint8_t* ptr = (const uint8_t *)sample.samples; + u64 bytes = sample.samples_len * sizeof(sample.samples[0]); + u64 written = 0; + const u8* ptr = (const u8 *)sample.samples; while (written < bytes) { - size_t chunk = bytes - written; + u64 chunk = bytes - written; if (chunk > 65536) chunk = 65536; if (pa_simple_write(simple_api, ptr + written, chunk, &pa_err) < 0) { fprintf(stderr, "ERROR: pa_simple_write failed: %s\n", @@ -365,7 +365,7 @@ void ctx_drain_complete(pa_context* ctx, void* data) pa_context_disconnect(ctx); } -void mainloop_quit(int ret_value) +void mainloop_quit(i32 ret_value) { assert(mainloop_api); mainloop_api->quit(mainloop_api, ret_value); @@ -386,7 +386,7 @@ void pa_complete_cmd(void) } } -void dummy_cb(pa_context* ctx, int success, void* data) +void dummy_cb(pa_context* ctx, i32 success, void* data) { (void)ctx; (void)data; @@ -400,7 +400,7 @@ void dummy_cb(pa_context* ctx, int success, void* data) pa_complete_cmd(); } -void vol_get_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) +void vol_get_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) { (void)ctx; @@ -418,7 +418,7 @@ void vol_get_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) assert(info->volume.channels > 0); #define DIV_CEIL(p, q) (((p) + (q) - 1)/(q)) - uint32_t vol_percent = DIV_CEIL(pa_cvolume_avg(&info->volume)*100, + u32 vol_percent = DIV_CEIL(pa_cvolume_avg(&info->volume)*100, PA_VOLUME_NORM); printf("%d%%\n", vol_percent); #undef DIV_CEIL @@ -429,7 +429,7 @@ void vol_get_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) pa_complete_cmd(); } -void vol_inc_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) +void vol_inc_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) { (void)ctx; Cmd cmd = *(Cmd*)data; @@ -448,7 +448,7 @@ void vol_inc_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) // clamp percent to the closest multiple of VOLUME_INC // between 0 and VOLUME_CAP - uint8_t vol_percent = (uint8_t)(pa_cvolume_avg(&vol)*100/PA_VOLUME_NORM); + u8 vol_percent = (u8)(pa_cvolume_avg(&vol)*100/PA_VOLUME_NORM); if (!cmd.is_dec) { vol_percent += VOLUME_INC; if (vol_percent > VOLUME_CAP) vol_percent = VOLUME_CAP; @@ -458,7 +458,7 @@ void vol_inc_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) } vol_percent = (vol_percent + (VOLUME_INC/2))/VOLUME_INC*VOLUME_INC; - for (size_t i = 0; i < vol.channels; i++) + for (u64 i = 0; i < vol.channels; i++) vol.values[i] = vol_percent*PA_VOLUME_NORM/100; pa_operation* op = pa_context_set_sink_volume_by_name(ctx, PA_SINK_NAME, @@ -482,7 +482,7 @@ void vol_inc_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) pa_operation_unref(op); } -void vol_set_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) +void vol_set_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) { (void)ctx; Cmd cmd = *(Cmd*)data; @@ -499,7 +499,7 @@ void vol_set_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) assert(info->volume.channels > 0); pa_cvolume vol = info->volume; - for (size_t i = 0; i < vol.channels; i++) { + for (u64 i = 0; i < vol.channels; i++) { vol.values[i] = PA_VOLUME_NORM * cmd.vol_percent/100; } @@ -524,7 +524,7 @@ void vol_set_cb(pa_context* ctx, const pa_sink_info* info, int eol, void* data) pa_operation_unref(op); } -void vol_mute_cb(pa_context* ctx, const pa_sink_info* info, int eol, +void vol_mute_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) { (void)ctx; @@ -545,7 +545,7 @@ void vol_mute_cb(pa_context* ctx, const pa_sink_info* info, int eol, pa_operation_unref(op); } -void mic_mute_cb(pa_context* ctx, const pa_source_info* info, int eol, +void mic_mute_cb(pa_context* ctx, const pa_source_info* info, i32 eol, void* data) { (void)data;
diff --git a/src/pcm.h b/src/pcm.h @@ -1,14 +1,14 @@ #ifndef PCM_H_ #define PCM_H_ -#include <stdint.h> +#include "ints.h" typedef struct { - int16_t* samples; - size_t samples_len; + i16* samples; + u64 samples_count; - size_t sample_rate; - uint8_t channels; + u64 sample_rate; + u8 channels; } PCM; #endif // PCM_H_