add config file loader
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 6 Jan 2022 10:29:47 +0000 (11:29 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 6 Jan 2022 10:29:47 +0000 (11:29 +0100)
application/Makefile
application/main.c
application/settings.c [new file with mode: 0644]
application/settings.h [new file with mode: 0644]
application/utils.c [new file with mode: 0644]
application/utils.h [new file with mode: 0644]

index ea6cb48..8424552 100644 (file)
@@ -32,9 +32,11 @@ include $(BUILD_ROOT)/config.mk
 CFLAGS += -I../ucx -I..
 
 SRC = main.c
+SRC += Fsb.c
 SRC += window.c
 SRC += player.c
-SRC += Fsb.c
+SRC += settings.c
+SRC += utils.c
 
 OBJ = $(SRC:%.c=$(BUILD_ROOT)/build/application/%.$(OBJ_EXT))
 
index 440a3f4..29f6ba9 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "window.h"
 #include "main.h"
+#include "settings.h"
 
 #include <ucx/buffer.h>
 #include <ucx/utils.h>
@@ -60,6 +61,7 @@ int main(int argc, char** argv) {
     // has a bug on freebsd and doesn't flush the output after a newline
     setvbuf(stdout, NULL, _IONBF, 0);
     
+    // initialize toolkit
     XtToolkitInitialize();
     XtSetLanguageProc(NULL, NULL, NULL);
     app = XtCreateApplicationContext();
@@ -67,6 +69,11 @@ int main(int argc, char** argv) {
     
     display =  XtOpenDisplay(app, NULL, APP_NAME, APP_CLASS, NULL, 0, &argc, argv);
     
+    // load settings
+    if(load_settings()) {
+        return 1;
+    }
+    
     MainWindow *window = WindowCreate(display);
     
     WindowShow(window);
diff --git a/application/settings.c b/application/settings.c
new file mode 100644 (file)
index 0000000..d651a81
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2022 Olaf Wintermann
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"), 
+ * to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+ * and/or sell copies of the Software, and to permit persons to whom the 
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in 
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "settings.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <pthread.h>
+
+#include "utils.h"
+
+#include <ucx/map.h>
+#include <ucx/properties.h>
+
+#define CONFIG_BASE_DIR ".config"
+#define UWP_CONFIG_DIR  "uwplayer"
+#define UWP_CONFIG_FILE "uwplayer.properties"
+
+static void* player_bin_search_thread(void *data);
+
+static char *uwp_config_dir;
+static UcxMap *uwp_settings; 
+
+static int check_config_dir(void) {
+    char *home = getenv("HOME");
+    if(!home) {
+        return 1;
+    }
+    
+    char *cfg_dir = util_concat_path(home, CONFIG_BASE_DIR);
+    int ret = 0;
+    if(mkdir(cfg_dir, S_IRWXU)) {
+        if(errno != EEXIST) {
+            fprintf(stderr, "Error: Cannot access %s: %s\n", cfg_dir, strerror(errno));
+            ret = 1;
+        }
+    }
+    
+    if(!ret) {
+        uwp_config_dir = util_concat_path(cfg_dir, UWP_CONFIG_DIR);
+        if(mkdir(uwp_config_dir, S_IRWXU)) {
+            if(errno != EEXIST) {
+                fprintf(stderr, "Error: Cannot access %s: %s\n", uwp_config_dir, strerror(errno));
+                ret = 1;
+            }
+        }
+    }
+    
+    free(cfg_dir);
+    return ret;
+}
+
+int load_settings(void) {
+    if(check_config_dir()) {
+        return 1;
+    }
+    
+    uwp_settings = ucx_map_new(16);
+    
+    char *cfgfile_path = util_concat_path(uwp_config_dir, UWP_CONFIG_FILE);
+    FILE *cfgfile = fopen(cfgfile_path, "r");
+    free(cfgfile_path);
+    if(!cfgfile) return 0;
+    
+    int ret = 0;
+    if(ucx_properties_load(uwp_settings, cfgfile)) {
+        fprintf(stderr, "Error: Cannot read uwplayer settings\n");
+        ret = 1;
+    }
+    fclose(cfgfile);
+    
+    if(ret) {
+        return ret;
+    }
+    
+    // check if mpv or mplayer binaries are configured
+    char *player_bin = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_BIN);
+    char *player_type = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_TYPE);
+    
+    if(!player_bin) {
+        // try to find the mpv or mplayer binary path
+        pthread_t st;
+        pthread_create(&st, NULL, player_bin_search_thread, NULL);
+    } else if(!player_type) {
+        fprintf(stderr, "Warning: unknown player type (mplayer, mpv)\n");
+    }
+    
+    return 0;
+}
+
+static void* player_bin_search_thread(void *data) {
+    // TODO:
+    //printf("search\n");
+    
+    return NULL;
+}
diff --git a/application/settings.h b/application/settings.h
new file mode 100644 (file)
index 0000000..b7ada4e
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2022 Olaf Wintermann
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"), 
+ * to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+ * and/or sell copies of the Software, and to permit persons to whom the 
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in 
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef UWP_SETTINGS_H
+#define UWP_SETTINGS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+    
+#define UWP_PLAYER_BIN    "player"
+#define UWP_PLAYER_TYPE   "player_type"
+
+int load_settings(void);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* UWP_SETTINGS_H */
+
diff --git a/application/utils.c b/application/utils.c
new file mode 100644 (file)
index 0000000..35b0ab5
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2022 Olaf Wintermann
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"), 
+ * to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+ * and/or sell copies of the Software, and to permit persons to whom the 
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in 
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "utils.h"
+
+#include <ucx/string.h>
+
+
+char* util_concat_path(const char *url_base, const char *p) {
+    sstr_t base = sstr((char*)url_base);
+    sstr_t path;
+    if(p) {
+        path = sstr((char*)p);
+    } else {
+        path = sstrn("", 0);
+    }
+    
+    int add_separator = 0;
+    if(base.length != 0 && base.ptr[base.length-1] == '/') {
+        if(path.ptr[0] == '/') {
+            base.length--;
+        }
+    } else {
+        if(path.length == 0 || path.ptr[0] != '/') {
+            add_separator = 1;
+        }
+    }
+    
+    sstr_t url;
+    if(add_separator) {
+        url = sstrcat(3, base, sstr("/"), path);
+    } else {
+        url = sstrcat(2, base, path);
+    }
+    
+    return url.ptr;
+}
diff --git a/application/utils.h b/application/utils.h
new file mode 100644 (file)
index 0000000..813fe65
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2022 Olaf Wintermann
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"), 
+ * to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+ * and/or sell copies of the Software, and to permit persons to whom the 
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in 
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef UWP_UTILS_H
+#define UWP_UTILS_H
+
+#include <stdlib.h>
+#include <inttypes.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+char* util_concat_path(const char *url_base, const char *p);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* UWP_UTILS_H */
+