2 * Copyright 2022 Olaf Wintermann
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
39 #include <ucx/properties.h>
40 #include <ucx/buffer.h>
41 #include <ucx/utils.h>
43 #define CONFIG_BASE_DIR ".config"
44 #define UWP_CONFIG_DIR "uwplayer"
45 #define UWP_CONFIG_FILE "uwplayer.conf"
47 #define JS_READ_BUFSIZE 4096
49 static void* player_bin_search_thread(void *data);
50 static void conf_load_global_settings(void);
52 static char *uwp_config_dir;
55 * root json config object
57 static JSONObject *uwp_config;
60 * global settings from json config converted to key/value pairs
62 static UcxMap *uwp_settings;
67 static UcxMap *uwp_default;
69 static int check_config_dir(void) {
70 char *home = getenv("HOME");
75 char *cfg_dir = util_concat_path(home, CONFIG_BASE_DIR);
77 if(mkdir(cfg_dir, S_IRWXU)) {
79 fprintf(stderr, "Error: Cannot access %s: %s\n", cfg_dir, strerror(errno));
85 uwp_config_dir = util_concat_path(cfg_dir, UWP_CONFIG_DIR);
86 if(mkdir(uwp_config_dir, S_IRWXU)) {
88 fprintf(stderr, "Error: Cannot access %s: %s\n", uwp_config_dir, strerror(errno));
98 int load_config(void) {
99 if(check_config_dir()) {
103 uwp_settings = ucx_map_new(16);
104 uwp_default = ucx_map_new(32);
106 char *cfgfile_path = util_concat_path(uwp_config_dir, UWP_CONFIG_FILE);
107 FILE *cfgfile = fopen(cfgfile_path, "r");
112 JSONParser *parser = json_parser_new();
114 JSONValue *value = NULL;
115 char buf[JS_READ_BUFSIZE];
118 while((ret = json_read_value(parser, &value)) >= 0) {
120 r = fread(buf, 1, JS_READ_BUFSIZE, cfgfile);
124 json_parser_fill(parser, buf, r);
130 json_parser_free(parser);
133 if(value->type == JSON_OBJECT) {
135 uwp_config = &value->value.object;
136 conf_load_global_settings();
153 // check if mpv or mplayer binaries are configured
154 char *player_bin = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_BIN);
155 char *player_type = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_TYPE);
158 // try to find the mpv or mplayer binary path
160 pthread_create(&st, NULL, player_bin_search_thread, NULL);
161 } else if(!player_type) {
162 fprintf(stderr, "Warning: unknown player type (mplayer, mpv)\n");
168 static void conf_load_global_settings(void) {
169 JSONValue *settings = json_obj_get(uwp_config, "settings");
174 if(settings->type != JSON_OBJECT) {
175 fprintf(stderr, "Warning: 'settings' not an object\n");
179 JSONObject *s = &settings->value.object;
181 for(size_t i=0;i<s->size;i++) {
182 JSONObjValue *gs = &s->values[i];
183 if(gs->value->type == JSON_STRING) {
184 ucx_map_cstr_put(uwp_settings, gs->name, strdup(gs->value->value.string.string));
189 static char* get_which_output(FILE *f, UcxBuffer *buf) {
192 ucx_stream_copy(f, buf, (read_func)fread, (write_func)ucx_buffer_write);
194 ucx_buffer_putc(buf, 0);
196 for(i=0;i<buf->pos;i++) {
197 if(buf->space[i] == '\n') {
207 static Boolean finish_bin_search(XtPointer data) {
208 PlayerInfo *playerInfo = data;
209 ucx_map_cstr_put(uwp_settings, UWP_PLAYER_BIN, playerInfo->bin);
210 ucx_map_cstr_put(uwp_settings, UWP_PLAYER_TYPE, playerInfo->type);
215 static void* player_bin_search_thread(void *data) {
216 UcxBuffer *buf = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
218 FILE *f = popen("which mpv", "r");
220 char *bin = get_which_output(f, buf);
222 PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo));
223 playerInfo->bin = strdup(bin);
224 playerInfo->type = strdup("mpv");
225 AppExecProc(finish_bin_search, playerInfo);
227 ucx_buffer_free(buf);
232 f = popen("which mplayer", "r");
234 char *bin = get_which_output(f, buf);
236 PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo));
237 playerInfo->bin = strdup(bin);
238 playerInfo->type = strdup("mplayer");
239 AppExecProc(finish_bin_search, playerInfo);
243 ucx_buffer_free(buf);
247 char* SettingsGetPlayerBin(void) {
248 return ucx_map_cstr_get(uwp_settings, UWP_PLAYER_BIN);