ucx update
[uwplayer.git] / application / settings.c
index 22793b4..d3a670b 100644 (file)
 
 #include "main.h"
 #include "utils.h"
+#include "json.h"
 
-#include <ucx/map.h>
-#include <ucx/properties.h>
-#include <ucx/buffer.h>
-#include <ucx/utils.h>
+#include <cx/map.h>
+#include <cx/hash_map.h>
+//include <ucx/properties.h>
+#include <cx/buffer.h>
+#include <cx/utils.h>
 
 #define CONFIG_BASE_DIR ".config"
 #define UWP_CONFIG_DIR  "uwplayer"
-#define UWP_CONFIG_FILE "uwplayer.properties"
+#define UWP_CONFIG_FILE "uwplayer.conf"
+
+#define JS_READ_BUFSIZE 4096
 
 static void* player_bin_search_thread(void *data);
+static void conf_load_global_settings(void);
 
 static char *uwp_config_dir;
-static UcxMap *uwp_settings; 
+
+/*
+ * root json config object
+ */
+static JSONObject *uwp_config;
+
+/*
+ * global settings from json config converted to key/value pairs
+ */
+static CxMap *uwp_settings; 
+
+/*
+ * default settings
+ */
+static CxMap *uwp_default;
 
 static int check_config_dir(void) {
     char *home = getenv("HOME");
@@ -77,12 +96,13 @@ static int check_config_dir(void) {
     return ret;
 }
 
-int load_settings(void) {
+int load_config(void) {
     if(check_config_dir()) {
         return 1;
     }
     
-    uwp_settings = ucx_map_new(16);
+    uwp_settings = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
+    uwp_default = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 32);
     
     char *cfgfile_path = util_concat_path(uwp_config_dir, UWP_CONFIG_FILE);
     FILE *cfgfile = fopen(cfgfile_path, "r");
@@ -90,10 +110,39 @@ int load_settings(void) {
     
     int ret = 0;
     if(cfgfile) {
-        if(ucx_properties_load(uwp_settings, cfgfile)) {
-            fprintf(stderr, "Error: Cannot read uwplayer settings\n");
+        JSONParser *parser = json_parser_new();
+        
+        JSONValue *value = NULL;
+        char buf[JS_READ_BUFSIZE];
+        size_t r;
+        
+        while((ret = json_read_value(parser, &value)) >= 0) {
+            if(ret == 0) {
+                r = fread(buf, 1, JS_READ_BUFSIZE, cfgfile);
+                if(r == 0) {
+                    break;
+                }
+                json_parser_fill(parser, buf, r);
+            } else {
+                break;
+            }
+        }
+        
+        json_parser_free(parser);
+        
+        if(value) {
+            if(value->type == JSON_OBJECT) {
+                ret = 0;
+                uwp_config = &value->value.object;
+                conf_load_global_settings();
+            } else {
+                ret = 1;
+            }
+        } else {
             ret = 1;
         }
+        
+        
         fclose(cfgfile);
 
         if(ret) {
@@ -103,8 +152,8 @@ int load_settings(void) {
  
     
     // 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);
+    char *player_bin = cxMapGet(uwp_settings, cx_hash_key_str(UWP_PLAYER_BIN));
+    char *player_type = cxMapGet(uwp_settings, cx_hash_key_str(UWP_PLAYER_TYPE));
     
     if(!player_bin) {
         // try to find the mpv or mplayer binary path
@@ -117,12 +166,33 @@ int load_settings(void) {
     return 0;
 }
 
-static char* get_which_output(FILE *f, UcxBuffer *buf) {
+static void conf_load_global_settings(void) {
+    JSONValue *settings = json_obj_get(uwp_config, "settings");
+    if(!settings) {
+        return;
+    }
+    
+    if(settings->type != JSON_OBJECT) {
+        fprintf(stderr, "Warning: 'settings' not an object\n");
+        return;
+    }
+    
+    JSONObject *s = &settings->value.object;
+    
+    for(size_t i=0;i<s->size;i++) {
+        JSONObjValue *gs = &s->values[i];
+        if(gs->value->type == JSON_STRING) {
+            cxMapPut(uwp_settings, cx_hash_key_str(gs->name), strdup(gs->value->value.string.string));
+        }
+    }
+}
+
+static char* get_which_output(FILE *f, CxBuffer *buf) {
     buf->pos = 0;
     buf->size = 0;
-    ucx_stream_copy(f, buf, (read_func)fread, (write_func)ucx_buffer_write);
+    cx_stream_copy(f, buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite);
     if(!pclose(f)) {
-        ucx_buffer_putc(buf, 0);
+        cxBufferPut(buf, 0);
         size_t i;
         for(i=0;i<buf->pos;i++) {
             if(buf->space[i] == '\n') {
@@ -135,41 +205,47 @@ static char* get_which_output(FILE *f, UcxBuffer *buf) {
     return NULL;
 }
 
-static void finish_bin_search(XtPointer data, XtIntervalId *id) {
+static Boolean finish_bin_search(XtPointer data) {
     PlayerInfo *playerInfo = data;
-    ucx_map_cstr_put(uwp_settings, UWP_PLAYER_BIN, playerInfo->bin);
-    ucx_map_cstr_put(uwp_settings, UWP_PLAYER_TYPE, playerInfo->type);
+    cxMapPut(uwp_settings, cx_hash_key_str(UWP_PLAYER_BIN), playerInfo->bin);
+    cxMapPut(uwp_settings, cx_hash_key_str(UWP_PLAYER_TYPE), playerInfo->type);
     free(playerInfo);
+    return 0;
 }
 
 static void* player_bin_search_thread(void *data) {
-    UcxBuffer *buf = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
+    CxBuffer buf;
+    cxBufferInit(&buf, NULL, 256, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
     
     FILE *f = popen("which mpv", "r");
     if(f) {
-        char *bin = get_which_output(f, buf);
+        char *bin = get_which_output(f, &buf);
         if(bin) {
             PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo));
             playerInfo->bin = strdup(bin);
             playerInfo->type = strdup("mpv");
-            AppAddTimeOut(0, finish_bin_search, playerInfo);
+            AppExecProc(finish_bin_search, playerInfo);
             
-            ucx_buffer_free(buf);
+            cxBufferDestroy(&buf);
             return NULL;
         }
     }
     
     f = popen("which mplayer", "r");
     if(f) {
-        char *bin = get_which_output(f, buf);
+        char *bin = get_which_output(f, &buf);
         if(bin) {
             PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo));
             playerInfo->bin = strdup(bin);
             playerInfo->type = strdup("mplayer");
-            AppAddTimeOut(0, finish_bin_search, playerInfo);
+            AppExecProc(finish_bin_search, playerInfo);
         }
     }
     
-    ucx_buffer_free(buf);
+    cxBufferDestroy(&buf);
     return NULL;
 }
+
+char* SettingsGetPlayerBin(void) {
+    return cxMapGet(uwp_settings, cx_hash_key_str(UWP_PLAYER_BIN));
+}