aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/any_ini.h12
-rw-r--r--src/comet.c23
-rw-r--r--src/config.c83
-rw-r--r--src/config.h10
-rw-r--r--src/util.c21
-rw-r--r--src/util.h4
6 files changed, 113 insertions, 40 deletions
diff --git a/src/any_ini.h b/src/any_ini.h
index f05b579..9d9afe6 100644
--- a/src/any_ini.h
+++ b/src/any_ini.h
@@ -424,7 +424,10 @@ static char *any_ini_stream_until(any_ini_stream_t *ini, size_t start, char c)
// Copy current buffer and refill
case '\0':
tmp = ANY_INI_REALLOC(value, size + ini->cursor - start);
- if (!tmp) return value;
+ if (!tmp) {
+ value[size - 1] = 0;
+ return value;
+ }
size += any_ini_copy(tmp + size, ini->buffer + start, ini->cursor - start);
value = tmp;
@@ -469,8 +472,11 @@ static char *any_ini_stream_until(any_ini_stream_t *ini, size_t start, char c)
}
}
- tmp = ANY_INI_REALLOC(value, size + ini->cursor - start);
- if (!tmp) return value;
+ tmp = ANY_INI_REALLOC(value, size + ini->cursor - start + 1);
+ if (!tmp) {
+ value[size - 1] = 0;
+ return value;
+ }
size += any_ini_copy(tmp + size, ini->buffer + start, ini->cursor - start);
size = any_ini_trim(tmp, 0, size);
diff --git a/src/comet.c b/src/comet.c
index be76621..c0343e0 100644
--- a/src/comet.c
+++ b/src/comet.c
@@ -39,10 +39,17 @@ int main(int argc, char **argv)
setlocale(LC_CTYPE, "");
any_log_init(stdout, ANY_LOG_DEBUG);
- FILE *config_file = fopen("comet.conf", "rb");
-
config_t config;
- config_init(&config, config_file);
+ config_init(&config);
+ log_debug("Copied default config");
+
+ const char *config_path = "comet.conf";
+ FILE *config_file = fopen(config_path, "rb");
+
+ if (config_file != NULL) {
+ log_debug("Reading config '%s'", config_path);
+ config_read(&config, config_file);
+ }
display_t display;
display_init(&display);
@@ -53,14 +60,11 @@ int main(int argc, char **argv)
int x_padding = 10;
int y_padding = 8;
- //int height = 30;
- //int width = display.screen_size->width - 2 * x_padding;
-
- const char *font = "Hack 12";
- log_debug("Loading font '%s'", font);
+ log_debug("Loading font '%s'", config.font);
+ PangoFontDescription *fontdesc = pango_font_description_from_string(config.font);
layout_info_t info = {
- .fontdesc = pango_font_description_from_string(font),
+ .fontdesc = fontdesc,
.context = pango_cairo_create_context(window.cr),
.x_offset = 0,
.height = config.height,
@@ -131,6 +135,7 @@ int main(int argc, char **argv)
window_close(&window);
display_close(&display);
+ config_free(&config);
return 0;
}
diff --git a/src/config.c b/src/config.c
index 55dd133..82d6231 100644
--- a/src/config.c
+++ b/src/config.c
@@ -6,14 +6,15 @@
#include "any_log.h"
#include "config.h"
+#include "util.h"
#define ANY_INI_IMPLEMENT
#include "any_ini.h"
typedef enum {
CONFIG_STRING,
- CONFIG_INT_ANY,
- CONFIG_INT_POSITIVE,
+ CONFIG_INT,
+ CONFIG_UINT,
CONFIG_DOUBLE,
} config_type_t;
@@ -24,10 +25,10 @@ typedef struct {
} config_entry_t;
static const config_entry_t config_entries[] = {
- { "width", CONFIG_INT_POSITIVE, offsetof(config_t, width) },
- { "height", CONFIG_INT_POSITIVE, offsetof(config_t, height) },
- { "font", CONFIG_STRING, offsetof(config_t, font) },
- { "monitor", CONFIG_STRING, offsetof(config_t, monitor) },
+ { "width", CONFIG_UINT, offsetof(config_t, width) },
+ { "height", CONFIG_UINT, offsetof(config_t, height) },
+ { "font", CONFIG_STRING, offsetof(config_t, font) },
+ { "monitor", CONFIG_STRING, offsetof(config_t, monitor) },
};
static bool config_read_string(const char *value, char **result)
@@ -41,9 +42,8 @@ static bool config_read_string(const char *value, char **result)
end--;
}
- *result = malloc(end - start + 1);
- memcpy(*result, value + start, end - start);
- *result[end - start] = '\0';
+ free(*result);
+ *result = strslice(value, start, end);
return true;
}
@@ -54,11 +54,23 @@ static bool config_read_int(const char *value, int *result)
*result = n;
if (n > INT_MAX || n < INT_MIN)
- log_debug("Integer is too big");
+ log_debug("Integer %ld is out of range", n);
return *end == '\0' && n != LONG_MIN && n != LONG_MAX;
}
+static bool config_read_uint(const char *value, unsigned int *result)
+{
+ char *end;
+ unsigned long n = strtoul(value, &end, 0);
+ *result = n;
+
+ if (n > UINT_MAX)
+ log_debug("Integer %lu is out of range", n);
+
+ return *end == '\0' && n != ULONG_MAX;
+}
+
static bool config_read_double(const char *value, double *result)
{
char *end;
@@ -93,23 +105,32 @@ static bool config_entry(config_t *config, int line, const char *section, const
void *result = (uint8_t *)config + config_entries[i].offset;
switch (config_entries[i].type) {
case CONFIG_STRING:
- if (config_read_string(value, (char **)result))
+ // NOTE: The previous string is freed by config_read_string
+ if (config_read_string(value, (char **)result)) {
+ log_debug("Config '%s' set to '%s'", key, *(char **)result);
return true;
+ }
break;
- case CONFIG_INT_ANY:
- if (config_read_int(value, (int *)result))
+ case CONFIG_INT:
+ if (config_read_int(value, (int *)result)) {
+ log_debug("Config '%s' set to '%d'", key, *(int *)result);
return true;
+ }
break;
- case CONFIG_INT_POSITIVE:
- if (config_read_int(value, (int *)result) && *(int *)result >= 0)
+ case CONFIG_UINT:
+ if (config_read_uint(value, (unsigned int *)result)) {
+ log_debug("Config '%s' set to '%u'", key, *(unsigned int *)result);
return true;
+ }
break;
case CONFIG_DOUBLE:
- if (config_read_double(value, (double *)result))
+ if (config_read_double(value, (double *)result)) {
+ log_debug("Config '%s' set to '%lf'", key, *(double *)result);
return true;
+ }
break;
default:
@@ -136,18 +157,24 @@ static bool config_entry(config_t *config, int line, const char *section, const
return true;
}
-static const config_t config_default = {
- .font = "monospace 10",
- .monitor = NULL,
- .height = 50,
- .width = 100,
-};
-
-void config_init(config_t *config, FILE *file)
+void config_init(config_t *config)
{
+ const config_t config_default = {
+ .font = "monospace 10",
+ .monitor = NULL,
+ .height = 50,
+ .width = 100,
+ };
+
memcpy(config, &config_default, sizeof(config_t));
- log_debug("Copied default config");
+ // NOTE: Strings must be copied
+ config->font = strcopy(config_default.font);
+ config->monitor = strcopy(config_default.monitor);
+}
+
+void config_read(config_t *config, FILE *file)
+{
any_ini_stream_t ini;
any_ini_file_init(&ini, file);
@@ -183,3 +210,9 @@ void config_init(config_t *config, FILE *file)
if (errors > 0)
log_panic("Config file contained %d errors", errors);
}
+
+void config_free(config_t *config)
+{
+ free(config->font);
+ free(config->monitor);
+}
diff --git a/src/config.h b/src/config.h
index 985ad4c..3596882 100644
--- a/src/config.h
+++ b/src/config.h
@@ -6,10 +6,14 @@
typedef struct {
char *font;
char *monitor;
- int height;
- int width;
+ unsigned int height;
+ unsigned int width;
} config_t;
-void config_init(config_t *config, FILE *file);
+void config_init(config_t *config);
+
+void config_read(config_t *config, FILE *file);
+
+void config_free(config_t *config);
#endif
diff --git a/src/util.c b/src/util.c
index 288fab6..b658769 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1,4 +1,6 @@
#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
#include <stdio.h>
#include "util.h"
@@ -51,3 +53,22 @@ bool check_capsule(int px, int py, int x, int y, int w, int h)
|| check_rect(px, py, x + radius, y, w - 2 * radius, h);
}
+char *strslice(const char *string, size_t start, size_t end)
+{
+ if (string == NULL)
+ return NULL;
+
+ char *result = malloc(end - start + 1);
+ memcpy(result, string + start, end - start);
+ result[end - start] = '\0';
+
+ return result;
+}
+
+char *strcopy(const char *string)
+{
+ if (string == NULL)
+ return NULL;
+
+ return strslice(string, 0, strlen(string));
+}
diff --git a/src/util.h b/src/util.h
index 5e661a3..06258e1 100644
--- a/src/util.h
+++ b/src/util.h
@@ -20,4 +20,8 @@ bool check_circle(int px, int py, int x, int y, int r);
// Check if point (px, py) is inside a capsule in (x, y), (x+w, y), (x, y+h) and (w+h, y+h)
bool check_capsule(int px, int py, int x, int y, int w, int h);
+char *strslice(const char *string, size_t start, size_t end);
+
+char *strcopy(const char *string);
+
#endif