/* * 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 #include #include #include #include #include #include #include #include "main.h" #include "utils.h" #include "json.h" #include #include //include #include #include #define CONFIG_BASE_DIR ".config" #define UWP_CONFIG_DIR "uwplayer" #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; /* * 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"); 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_config(void) { if(check_config_dir()) { return 1; } 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"); free(cfgfile_path); int ret = 0; if(cfgfile) { 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) { return ret; } } // check if mpv or mplayer binaries are configured 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 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 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;isize;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; cx_stream_copy(f, buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite); if(!pclose(f)) { cxBufferPut(buf, 0); size_t i; for(i=0;ipos;i++) { if(buf->space[i] == '\n') { buf->space[i] = 0; break; } } return buf->space; } return NULL; } static Boolean finish_bin_search(XtPointer data) { PlayerInfo *playerInfo = data; 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) { 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); if(bin) { PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo)); playerInfo->bin = strdup(bin); playerInfo->type = strdup("mpv"); AppExecProc(finish_bin_search, playerInfo); cxBufferDestroy(&buf); return NULL; } } f = popen("which mplayer", "r"); if(f) { char *bin = get_which_output(f, &buf); if(bin) { PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo)); playerInfo->bin = strdup(bin); playerInfo->type = strdup("mplayer"); AppExecProc(finish_bin_search, playerInfo); } } cxBufferDestroy(&buf); return NULL; } char* SettingsGetPlayerBin(void) { return cxMapGet(uwp_settings, cx_hash_key_str(UWP_PLAYER_BIN)); }