add config file loader
[uwplayer.git] / application / settings.c
1 /*
2  * Copyright 2022 Olaf Wintermann
3  *
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:
10  * 
11  * The above copyright notice and this permission notice shall be included in 
12  * all copies or substantial portions of the Software.
13  *
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.
21  */
22
23 #include "settings.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 #include <pthread.h>
33
34 #include "utils.h"
35
36 #include <ucx/map.h>
37 #include <ucx/properties.h>
38
39 #define CONFIG_BASE_DIR ".config"
40 #define UWP_CONFIG_DIR  "uwplayer"
41 #define UWP_CONFIG_FILE "uwplayer.properties"
42
43 static void* player_bin_search_thread(void *data);
44
45 static char *uwp_config_dir;
46 static UcxMap *uwp_settings; 
47
48 static int check_config_dir(void) {
49     char *home = getenv("HOME");
50     if(!home) {
51         return 1;
52     }
53     
54     char *cfg_dir = util_concat_path(home, CONFIG_BASE_DIR);
55     int ret = 0;
56     if(mkdir(cfg_dir, S_IRWXU)) {
57         if(errno != EEXIST) {
58             fprintf(stderr, "Error: Cannot access %s: %s\n", cfg_dir, strerror(errno));
59             ret = 1;
60         }
61     }
62     
63     if(!ret) {
64         uwp_config_dir = util_concat_path(cfg_dir, UWP_CONFIG_DIR);
65         if(mkdir(uwp_config_dir, S_IRWXU)) {
66             if(errno != EEXIST) {
67                 fprintf(stderr, "Error: Cannot access %s: %s\n", uwp_config_dir, strerror(errno));
68                 ret = 1;
69             }
70         }
71     }
72     
73     free(cfg_dir);
74     return ret;
75 }
76
77 int load_settings(void) {
78     if(check_config_dir()) {
79         return 1;
80     }
81     
82     uwp_settings = ucx_map_new(16);
83     
84     char *cfgfile_path = util_concat_path(uwp_config_dir, UWP_CONFIG_FILE);
85     FILE *cfgfile = fopen(cfgfile_path, "r");
86     free(cfgfile_path);
87     if(!cfgfile) return 0;
88     
89     int ret = 0;
90     if(ucx_properties_load(uwp_settings, cfgfile)) {
91         fprintf(stderr, "Error: Cannot read uwplayer settings\n");
92         ret = 1;
93     }
94     fclose(cfgfile);
95     
96     if(ret) {
97         return ret;
98     }
99     
100     // check if mpv or mplayer binaries are configured
101     char *player_bin = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_BIN);
102     char *player_type = ucx_map_cstr_get(uwp_settings, UWP_PLAYER_TYPE);
103     
104     if(!player_bin) {
105         // try to find the mpv or mplayer binary path
106         pthread_t st;
107         pthread_create(&st, NULL, player_bin_search_thread, NULL);
108     } else if(!player_type) {
109         fprintf(stderr, "Warning: unknown player type (mplayer, mpv)\n");
110     }
111     
112     return 0;
113 }
114
115 static void* player_bin_search_thread(void *data) {
116     // TODO:
117     //printf("search\n");
118     
119     return NULL;
120 }