chvol

Simple interface to set PulseAudio's volume, with graphical feedback! 🔊

Commit
3239af26fd1dcd6cc5fbfb6ab2e36beb88392be3
Parent
be7d7b2728e9916a8d519beb0466035cba203c7f
Author
Pablo <pablo-pie@riseup.net>
Date

Added support for negative numbers to the INI parser

Diffstats

1 files changed, 75 insertions, 48 deletions

Status Name Changes Insertions Deletions
Modified src/configure.c 2 files changed 75 48
diff --git a/src/configure.c b/src/configure.c
@@ -28,21 +28,21 @@
                   ##__VA_ARGS__)
 
 typedef struct {
-  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;
+  i64 window_width;
+  i64 window_height;
+  i64 window_progress_height;
+  i64 window_padding;
+  i64 window_border;
+  i64 window_timeout_ms;
+
+  i64         volume_inc;
+  i64         volume_cap;
   const char* volume_audio_sample;
 
-  u64 color_background;
-  u64 color_light_background;
-  u64 color_accent;
-  u64 color_warning;
+  i64 color_background;
+  i64 color_light_background;
+  i64 color_accent;
+  i64 color_warning;
 } Config;
 
 b8 parse_config(const char* path, Config* config);
@@ -69,24 +69,25 @@ i32 main(i32 argc, char** argv)
     return EXIT_FAILURE;
   }
 
+  fprintf(f, "#include \"ints.h\"\n");
   fprintf(f, "#include \"pcm.h\"\n\n");
 
-  fprintf(f, "#define WIN_WIDTH           %zu\n",   config.window_width);
-  fprintf(f, "#define WIN_HEIGHT          %zu\n",   config.window_height);
-  fprintf(f, "#define WIN_PROGRESS_HEIGHT %zu\n",   config.window_progress_height);
-  fprintf(f, "#define WIN_PADDING         %zu\n",   config.window_padding);
-  fprintf(f, "#define WIN_BORDER          %zu\n",   config.window_border);
-  fprintf(f, "#define WIN_TIMEOUT_MS      %zu\n\n", config.window_timeout_ms);
+  fprintf(f, "#define WIN_WIDTH           %ld\n",   config.window_width);
+  fprintf(f, "#define WIN_HEIGHT          %ld\n",   config.window_height);
+  fprintf(f, "#define WIN_PROGRESS_HEIGHT %ld\n",   config.window_progress_height);
+  fprintf(f, "#define WIN_PADDING         %ld\n",   config.window_padding);
+  fprintf(f, "#define WIN_BORDER          %ld\n",   config.window_border);
+  fprintf(f, "#define WIN_TIMEOUT_MS      %ld\n\n", config.window_timeout_ms);
 
-  fprintf(f, "#define VOLUME_INC %zu\n",   config.volume_inc);
-  fprintf(f, "#define VOLUME_CAP %zu\n\n", config.volume_cap);
+  fprintf(f, "#define VOLUME_INC %ld\n",   config.volume_inc);
+  fprintf(f, "#define VOLUME_CAP %ld\n\n", config.volume_cap);
 
   fprintf(f, "#define COLOR_BACKGROUND       0xFF%lX\n",   config.color_background);
   fprintf(f, "#define COLOR_LIGHT_BACKGROUND 0xFF%lX\n",   config.color_light_background);
   fprintf(f, "#define COLOR_ACCENT           0xFF%lX\n",   config.color_accent);
   fprintf(f, "#define COLOR_WARNING          0xFF%lX\n\n", config.color_warning);
 
-  fprintf(f, "int16_t volume_change_samples_buff[%zu] = {",
+  fprintf(f, "i16 volume_change_samples_buff[%zu] = {",
              sample.samples_count);
   for (u64 i = 0; i < sample.samples_count; i++) {
     if (i % 8 == 0) fprintf(f, "\n  ");
@@ -119,7 +120,8 @@ u64 log_floor(u64 n)
 #define VORBIS_READ_CHUNK 4096
 
 // TODO: verify this
-const char *ov_error_str(i32 e){
+const char* ov_error_str(i32 e)
+{
   switch(e){
   case 0:             return "OK";
   case OV_EREAD:      return "read error";
@@ -281,7 +283,7 @@ typedef struct {
 
   union {
     SV          as_text;
-    u64         as_int;
+    i64         as_int;
     const char* as_str;
   };
   TokenKind  kind;
@@ -331,11 +333,18 @@ void location_report_end(Location loc, LogLevel level)
   }
 }
 
-// TODO: support negative numbers!
-b8 parse_int(SV text, Location loc, u64* val)
+b8 parse_int(SV text, Location loc, i64* val)
 {
+  assert(text.count > 0);
   *val = 0;
 
+  i64 sign = 1;
+  if (text.data[0] == '+' || text.data[0] == '-') {
+    if (text.data[0] == '-') sign = -1;
+    text.data++;
+    text.count--;
+  }
+
   if (sv_starts_with(text, "0x") || sv_starts_with(text, "0X")) {
     text.data += 2;
     text.count  -= 2;
@@ -345,13 +354,13 @@ b8 parse_int(SV text, Location loc, u64* val)
       *val   *= 16;
 
       if (isdigit(c)) {
-        *val += (u64)(text.data[i] - '0');
+        *val += (i64)(text.data[i] - '0');
       } else if (c == '_') {
         continue;
       } else if (c >= 'A' && c <= 'F') {
-        *val += (u64)(c - 'A' + 10);
+        *val += (i64)(c - 'A' + 10);
       } else if (c >= 'a' && c <= 'f') {
-        *val += (u64)(c - 'a' + 10);
+        *val += (i64)(c - 'a' + 10);
       } else {
         loc.offset += i + 2;
         location_report(loc, LOG_ERROR,
@@ -361,6 +370,7 @@ b8 parse_int(SV text, Location loc, u64* val)
       }
     }
 
+    *val *= sign;
     return true;
   }
 
@@ -377,9 +387,10 @@ b8 parse_int(SV text, Location loc, u64* val)
     }
 
     *val *= 10;
-    *val += (u64)(c - '0');
+    *val += (i64)(c - '0');
   }
 
+  *val *= sign;
   return true;
 }
 
@@ -452,14 +463,29 @@ b8 lexer_finished(Lexer* l)
   return l->cursor >= l->count;
 }
 
+const char* escape_char(char c)
+{
+  static char str[2] = {0};
+
+  switch (c) {
+  case '\0':             return "\\0";
+  case '\t':             return "\\t";
+  case '\r':             return "\\r";
+  case '\n':             return "\\n";
+  case '\a':             return "\\a";
+  case '\b':             return "\\b";
+  default:   str[0] = c; return str;
+  }
+}
+
 b8 lexer_next(Lexer* l, Token* token)
 {
   Location loc;
-  loc.src_path        = l->src_path;
-  loc.line            = l->current_line + 1;
-  loc.offset          = l->cursor - l->current_line_start;
-  loc.text_start.data = l->data + l->current_line_start;
-  loc.text_start.count  = l->count  - l->current_line_start;
+  loc.src_path         = l->src_path;
+  loc.line             = l->current_line + 1;
+  loc.offset           = l->cursor - l->current_line_start;
+  loc.text_start.data  = l->data + l->current_line_start;
+  loc.text_start.count = l->count  - l->current_line_start;
 
   if (lexer_finished(l)) {
     location_report(loc, LOG_ERROR, "Expected token, got EOF");
@@ -601,23 +627,24 @@ b8 lexer_next(Lexer* l, Token* token)
     }
 
     token->as_text.count = count;
-  } else if (isdigit(c)) {
+  } else if (c == '+' || c == '-' || isdigit(c)) {
     SV text; u64 count = 0;
     text.data = l->data + l->cursor;
-    for (; l->cursor < l->count; l->cursor++) {
-      if (!isalnum(l->data[l->cursor])) break;
+    char c = l->data[l->cursor];
+    for (; l->cursor < l->count; c = l->data[++l->cursor]) {
+      if (!(isalnum(c) || c == '+' || c == '-')) break;
       count++;
     }
     text.count = count;
 
-    u64 val;
+    i64 val;
     if (!parse_int(text, loc, &val)) return false;
 
     token->kind   = TOKEN_INT;
     token->as_int = val;
   } else {
-    // TODO: escape `c` if needed
-    location_report(loc, LOG_ERROR, "Unexpected character '%c'", c);
+    location_report(loc, LOG_ERROR, "Unexpected character '%s'",
+                                    escape_char(c));
     return false;
   }
 
@@ -678,20 +705,20 @@ KeyInfo key_infos[KEYS_COUNT] = {
   fprintf(stderr, BOLD_WHITE "%s: " BOLD_RED "error: " RESET \
                   fmt "\n", (path), ##__VA_ARGS__)
 
-b8 check_bounds(const char* path, Key k, Config* config, u64 min, u64 max)
+b8 check_bounds(const char* path, Key k, Config* config, i64 min, i64 max)
 {
   KeyInfo info = key_infos[k];
   assert(info.kind == TOKEN_INT);
-  u64 val = *(u64*)((u8*)config + info.offset);
+  i64 val = *(i64*)((u8*)config + info.offset);
 
   if (val < min) {
-    file_report(path, "Value of field \"%s\" is too small: %zu < %zu",
+    file_report(path, "Value of field \"%s\" is too small: %ld < %ld",
                       info.name, val, min);
     return false;
   }
 
-  if (max != 0 && val > max) {
-    file_report(path, "Value of field \"%s\" is too big: %zu > %zu",
+  if (max > 0 && val > max) {
+    file_report(path, "Value of field \"%s\" is too big: %ld > %ld",
                       info.name, val, max);
     return false;
   }
@@ -749,7 +776,7 @@ b8 parse_config(const char* path, Config* config)
       key_set_flag |= 1 << k;
 
       u8* field_ptr = (u8*)config + info.offset;
-      if (info.kind == TOKEN_INT) *(u64*)     field_ptr = token.as_int;
+      if (info.kind == TOKEN_INT) *(i64*)        field_ptr = token.as_int;
       else                        *(const char**)field_ptr = token.as_str;
 
       key_found = true;
@@ -776,7 +803,7 @@ b8 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;
 
-  u64 size = config->window_width;
+  i64 size = config->window_width;
   if (config->window_height > size) size = config->window_height;
 
   if (!check_bounds(path, KEY_WINDOW_PROGRESS_HEIGHT,