- Commit
- fd008b33a1c365c9b780aa5946201f9a784f1ae9
- Parent
- ce6ec7916e6e96f67ac404a1590c1d75e70aee1d
- Author
- Pablo <pablo-pie@riseup.net>
- Date
Cleaned-up error messages
Simple interface to set PulseAudio's volume, with graphical feedback! 🔊
Cleaned-up error messages
1 files changed, 88 insertions, 68 deletions
| Status | Name | Changes | Insertions | Deletions |
| Modified | src/main.c | 2 files changed | 88 | 68 |
diff --git a/src/main.c b/src/main.c @@ -43,34 +43,24 @@ typedef struct { u32* pixels; } Canvas; -b8 pa_run_cmd(Cmd cmd); - -void usage(char* program_name) -{ - fprintf(stderr, "USAGE: %s VOLUME%%\n", program_name); - fprintf(stderr, " %s [up|down|mute|mute-mic] [-g]\n", program_name); -} - -b8 parse_percent(char* str, u32* result) -{ - char* view = str; - *result = 0; +#define BOLD_RED "\x1b[1;31m" +#define BOLD_YELLOW "\x1b[1;33m" +#define RESET "\x1b[0m" - while (*view != '\0') { - char c = *view++; - if (!isdigit(c)) break; +// NOTE: using GNU extension for ##__VA_ARGS__ +#define report(fmt, ...) \ + fprintf(stderr, BOLD_RED "ERROR: " RESET fmt, ##__VA_ARGS__) - *result *= 10; - *result += (u32)(c - '0'); - } - - if (*view == '%') view++; - if (*view != '\0') return false; +#define shift_args(argc, argv) (assert((argc) > 0), (argc)--, *(argv)++) - return true; +void usage(char* program_name) +{ + fprintf(stderr, BOLD_YELLOW "USAGE: " RESET "%s VOLUME%%\n", program_name); + fprintf(stderr, " %s [up|down|mute|mute-mic] [-g]\n", program_name); } -#define shift_args(argc, argv) (assert((argc) > 0), (argc)--, *(argv)++) +b8 parse_percent(char* str, u32* result); +b8 pa_run_cmd(Cmd cmd); i32 main(i32 argc, char** argv) { @@ -89,7 +79,7 @@ i32 main(i32 argc, char** argv) } if (cmd_set) { - fprintf(stderr, "ERROR: too many arguments\n"); + report("too many arguments\n"); usage(program_name); return EXIT_FAILURE; } @@ -113,7 +103,7 @@ i32 main(i32 argc, char** argv) // usuming the user wants to set the volume u32 percent; if (!parse_percent(arg, &percent)) { - fprintf(stderr, "ERROR: unknown argument \"%s\"\n", arg); + report("unknown argument \"%s\"\n", arg); usage(program_name); return EXIT_FAILURE; } @@ -130,6 +120,25 @@ i32 main(i32 argc, char** argv) return EXIT_SUCCESS; } +b8 parse_percent(char* str, u32* result) +{ + char* view = str; + *result = 0; + + while (*view != '\0') { + char c = *view++; + if (!isdigit(c)) break; + + *result *= 10; + *result += (u32)(c - '0'); + } + + if (*view == '%') view++; + if (*view != '\0') return false; + + return true; +} + void draw_progress_bar(Canvas* canvas, u8 percent) { assert(percent <= VOLUME_CAP); @@ -170,7 +179,8 @@ void draw_progress_bar(Canvas* canvas, u8 percent) } u64 progress_start = WIN_PADDING; - u64 progress_end = percent * (canvas->width - 2 * WIN_PADDING)/100 + WIN_PADDING; + u64 progress_end = percent * (canvas->width - 2 * WIN_PADDING)/100 + + WIN_PADDING; u64 progress_full = canvas->width - WIN_PADDING; for (u64 y = (canvas->height - WIN_PROGRESS_HEIGHT)/2; @@ -188,6 +198,8 @@ void draw_progress_bar(Canvas* canvas, u8 percent) // = X11 bullshit ============================================================= +#define BPP 4 + /* * Draws the progress bar window and closes it after [timeout_ms] millisecs * */ @@ -196,20 +208,21 @@ 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(); if (pid < 0) { - fprintf(stderr, "ERROR: failed to fork process to display graphics\n"); + report("failed to fork X11 process\n"); return; } if (pid != 0) return; - Display *d = XOpenDisplay(NULL); + Display* d = XOpenDisplay(NULL); if (!d) { - fprintf(stderr, "ERROR: cannot open graphical display\n"); + report("cannot open X11 display\n"); return; } i32 screen = DefaultScreen(d); Window root = RootWindow(d, screen); + assert(root && "could not get X11 root window"); i32 screen_width = DisplayWidth(d, screen); i32 screen_height = DisplayHeight(d, screen); @@ -232,19 +245,24 @@ void x11_launch_window(u8 percent, u32 timeout_ms) CWOverrideRedirect | CWBackPixel | CWEventMask, &swa ); + if (!win) { + report("couldn't create X11 window\n"); + return; + } XMapWindow(d, win); XFlush(d); - 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"); + assert(depth == 24 || depth == 32); + u32* pixels = malloc(WIN_WIDTH * WIN_HEIGHT * BPP); + assert(pixels && "buy more RAM!\n"); + XImage* img = XCreateImage(d, visual, depth, ZPixmap, 0, + (char*)pixels, WIN_WIDTH, WIN_HEIGHT, 32, 0); + if (!img) { + report("couldn't create X11 image\n"); return; } - XImage* img = XCreateImage(d, visual, depth, ZPixmap, 0, - (char*)pixels, WIN_WIDTH, WIN_HEIGHT, 32, 0); Canvas canvas = { .width = WIN_WIDTH, .height = WIN_HEIGHT, @@ -254,13 +272,13 @@ void x11_launch_window(u8 percent, u32 timeout_ms) // ========================================================================== struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - long deadline_ns = now.tv_sec * 1000000000LL + now.tv_nsec - + (long)timeout_ms * 1000000LL; + i64 deadline_ns = now.tv_sec * 1000000000LL + now.tv_nsec + + (i64)timeout_ms * 1000000LL; draw_progress_bar(&canvas, percent); XEvent e; - for (long now_ns = 0; + for (i64 now_ns = 0; now_ns <= deadline_ns; clock_gettime(CLOCK_MONOTONIC, &now), now_ns = now.tv_sec * 1000000000LL + now.tv_nsec) { @@ -281,6 +299,8 @@ void x11_launch_window(u8 percent, u32 timeout_ms) #define PA_SINK_NAME "@DEFAULT_SINK@" #define PA_SOURCE_NAME "@DEFAULT_SOURCE@" +#define PA_CHUNK_SIZE 65536 + pa_context* ctx = NULL; pa_mainloop* m = NULL; pa_mainloop_api* mainloop_api = NULL; @@ -298,8 +318,8 @@ b8 pa_run_cmd(Cmd cmd) pa_context_set_state_callback(ctx, pa_ctx_state_cb, &cmd); if (pa_context_connect(ctx, NULL, 0, NULL) < 0) { - fprintf(stderr, "ERROR: could not connect to PulseAudio context\n"); - return EXIT_FAILURE; + report("could not connect to PulseAudio context\n"); + return false; } i32 ret_value = 0; @@ -326,7 +346,7 @@ b8 pa_play_sample(PCM sample) PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &pa_err); if (!simple_api) { - fprintf(stderr, "ERROR: pa_simple_new failed: %s\n", pa_strerror(pa_err)); + report("pa_simple_new failed: %s\n", pa_strerror(pa_err)); return false; } @@ -335,9 +355,9 @@ b8 pa_play_sample(PCM sample) const u8* ptr = (const u8 *)sample.samples; while (written < bytes) { u64 chunk = bytes - written; - if (chunk > 65536) chunk = 65536; + if (chunk > PA_CHUNK_SIZE) chunk = PA_CHUNK_SIZE; if (pa_simple_write(simple_api, ptr + written, chunk, &pa_err) < 0) { - fprintf(stderr, "ERROR: pa_simple_write failed: %s\n", + report("pa_simple_write failed: %s\n", pa_strerror(pa_err)); pa_simple_free(simple_api); return false; @@ -347,7 +367,7 @@ b8 pa_play_sample(PCM sample) } if (pa_simple_drain(simple_api, &pa_err) < 0) { - fprintf(stderr, "ERROR: pa_simple_drain failed: %s\n", + report("pa_simple_drain failed: %s\n", pa_strerror(pa_err)); pa_simple_free(simple_api); return false; @@ -363,10 +383,10 @@ void ctx_drain_complete(pa_context* ctx, void* data) pa_context_disconnect(ctx); } -void mainloop_quit(i32 ret) +void mainloop_abort(void) { assert(mainloop_api); - mainloop_api->quit(mainloop_api, ret); + mainloop_api->quit(mainloop_api, EXIT_FAILURE); } void pa_complete_cmd(void) @@ -390,8 +410,8 @@ void dummy_cb(pa_context* ctx, i32 success, void* data) (void)data; if (!success) { - fprintf(stderr, "ERROR: dummy_cb failed\n"); - mainloop_quit(EXIT_FAILURE); + report("dummy_cb failed\n"); + mainloop_abort(); return; } @@ -405,8 +425,8 @@ void vol_get_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) Cmd cmd = *(Cmd*)data; if (eol < 0) { - fprintf(stderr, "ERROR: vol_get_cb failed\n"); - mainloop_quit(EXIT_FAILURE); + report("vol_get_cb failed\n"); + mainloop_abort(); return; } @@ -433,8 +453,8 @@ void vol_inc_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) Cmd cmd = *(Cmd*)data; if (eol < 0) { - fprintf(stderr, "ERROR: vol_inc_cb failed\n"); - mainloop_quit(EXIT_FAILURE); + report("vol_inc_cb failed\n"); + mainloop_abort(); return; } @@ -463,16 +483,16 @@ void vol_inc_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) &vol, dummy_cb, NULL); if (!op) { - fprintf(stderr, "ERROR: failed to set default sink volume\n"); + report("failed to set default sink volume\n"); pa_complete_cmd(); - mainloop_quit(EXIT_FAILURE); + mainloop_abort(); return; } if (cmd.show_graphics) { if (!pa_play_sample(volume_change_sample)) { - fprintf(stderr, "ERROR: failed to play audio sample\n"); - mainloop_quit(EXIT_FAILURE); + report("failed to play audio sample\n"); + mainloop_abort(); } x11_launch_window(vol_percent, WIN_TIMEOUT_MS); } @@ -486,8 +506,8 @@ void vol_set_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) Cmd cmd = *(Cmd*)data; if (eol < 0) { - fprintf(stderr, "ERROR: vol_set_cb failed\n"); - mainloop_quit(EXIT_FAILURE); + report("vol_set_cb failed\n"); + mainloop_abort(); return; } @@ -505,16 +525,16 @@ void vol_set_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, void* data) &vol, dummy_cb, NULL); if (!op) { - fprintf(stderr, "ERROR: failed to set default sink volume\n"); + report("failed to set default sink volume\n"); pa_complete_cmd(); - mainloop_quit(EXIT_FAILURE); + mainloop_abort(); return; } if (cmd.show_graphics) { if (!pa_play_sample(volume_change_sample)) { - fprintf(stderr, "ERROR: failed to play audio sample\n"); - mainloop_quit(EXIT_FAILURE); + report("failed to play audio sample\n"); + mainloop_abort(); } x11_launch_window(cmd.vol_percent, WIN_TIMEOUT_MS); } @@ -529,8 +549,8 @@ void vol_mute_cb(pa_context* ctx, const pa_sink_info* info, i32 eol, (void)data; if (eol < 0) { - fprintf(stderr, "ERROR: vol_mute_cb failed\n"); - mainloop_quit(EXIT_FAILURE); + report("vol_mute_cb failed\n"); + mainloop_abort(); return; } @@ -549,8 +569,8 @@ void mic_mute_cb(pa_context* ctx, const pa_source_info* info, i32 eol, (void)data; if (eol < 0) { - fprintf(stderr, "ERROR: mic_mute_cb failed\n"); - mainloop_quit(EXIT_FAILURE); + report("mic_mute_cb failed\n"); + mainloop_abort(); return; } @@ -600,10 +620,10 @@ void pa_ctx_state_cb(pa_context* ctx, void* data) if (op) { pa_operation_unref(op); n_cmds++; } break; case PA_CONTEXT_TERMINATED: - mainloop_quit(EXIT_SUCCESS); + mainloop_abort(); break; case PA_CONTEXT_FAILED: default: - mainloop_quit(EXIT_FAILURE); + mainloop_abort(); } }