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.
38 #include <ucx/properties.h>
39 #include <ucx/buffer.h>
40 #include <ucx/utils.h>
42 #define CONFIG_BASE_DIR ".config"
43 #define UWP_CONFIG_DIR "uwplayer"
44 #define UWP_CONFIG_FILE "uwplayer.properties"
46 static void* player_bin_search_thread(void *data);
48 static char *uwp_config_dir;
49 static UcxMap *uwp_settings;
51 static int check_config_dir(void) {
52 char *home = getenv("HOME");
57 char *cfg_dir = util_concat_path(home, CONFIG_BASE_DIR);
59 if(mkdir(cfg_dir, S_IRWXU)) {
61 fprintf(stderr, "Error: Cannot access %s: %s\n", cfg_dir, strerror(errno));
67 uwp_config_dir = util_concat_path(cfg_dir, UWP_CONFIG_DIR);
68 if(mkdir(uwp_config_dir, S_IRWXU)) {
70 fprintf(stderr, "Error: Cannot access %s: %s\n", uwp_config_dir, strerror(errno));
80 int load_settings(void) {
81 if(check_config_dir()) {
85 uwp_settings = ucx_map_new(16);
87 char *cfgfile_path = util_concat_path(uwp_config_dir, UWP_CONFIG_FILE);
88 FILE *cfgfile = fopen(cfgfile_path, "r");
93 if(ucx_properties_load(uwp_settings, cfgfile)) {
94 fprintf(stderr, "Error: Cannot read uwplayer settings\n");
105 // check if mpv or mplayer binaries are configured
106 char *player_bin = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_BIN);
107 char *player_type = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_TYPE);
110 // try to find the mpv or mplayer binary path
112 pthread_create(&st, NULL, player_bin_search_thread, NULL);
113 } else if(!player_type) {
114 fprintf(stderr, "Warning: unknown player type (mplayer, mpv)\n");
120 static char* get_which_output(FILE *f, UcxBuffer *buf) {
123 ucx_stream_copy(f, buf, (read_func)fread, (write_func)ucx_buffer_write);
125 ucx_buffer_putc(buf, 0);
127 for(i=0;i<buf->pos;i++) {
128 if(buf->space[i] == '\n') {
138 static Boolean finish_bin_search(XtPointer data) {
139 PlayerInfo *playerInfo = data;
140 ucx_map_cstr_put(uwp_settings, UWP_PLAYER_BIN, playerInfo->bin);
141 ucx_map_cstr_put(uwp_settings, UWP_PLAYER_TYPE, playerInfo->type);
146 static void* player_bin_search_thread(void *data) {
147 UcxBuffer *buf = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
149 FILE *f = popen("which mpv", "r");
151 char *bin = get_which_output(f, buf);
153 PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo));
154 playerInfo->bin = strdup(bin);
155 playerInfo->type = strdup("mpv");
156 AppExecProc(finish_bin_search, playerInfo);
158 ucx_buffer_free(buf);
163 f = popen("which mplayer", "r");
165 char *bin = get_which_output(f, buf);
167 PlayerInfo *playerInfo = malloc(sizeof(PlayerInfo));
168 playerInfo->bin = strdup(bin);
169 playerInfo->type = strdup("mplayer");
170 AppExecProc(finish_bin_search, playerInfo);
174 ucx_buffer_free(buf);
178 char* SettingsGetPlayerBin(void) {
179 return ucx_map_cstr_get(uwp_settings, UWP_PLAYER_BIN);