[refactoring] revise interface
[platform/core/api/system-settings.git] / src / sst_utils_wrapper.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "sst_utils_wrapper.h"
17
18 #include <dlfcn.h>
19 #include "sst.h"
20 #include "sst_misc.h"
21
22 static void *sst_utils_handle;
23 static const char* const sst_utils_library = SETTING_UTILS_SO_FILE_PATH;
24
25 static void* _utils_load_lib()
26 {
27         if (sst_utils_handle)
28                 return sst_utils_handle;
29
30         sst_utils_handle = dlopen(sst_utils_library, RTLD_LAZY | RTLD_GLOBAL);
31         if (NULL == sst_utils_handle) {
32                 ERR("dlopen(%s) Fail", sst_utils_library);
33         }
34
35         return sst_utils_handle;
36 }
37
38 bool sstu_set_font_config(const char *font_name)
39 {
40         static bool (*fn_impl)(const char*);
41         if (fn_impl)
42                 return fn_impl(font_name);
43
44         void *handle = _utils_load_lib();
45         if (NULL == handle) {
46                 ERR("No Utils Library Handle");
47                 return false;
48         }
49
50         fn_impl = dlsym(handle, "sstu_set_font_config");
51         char *error = dlerror();
52         if (error) {
53                 ERR("dlsym(sstu_set_font_config) Fail(%s)", error);
54                 return false;
55         }
56
57         return fn_impl(font_name);
58 }
59
60 int sstu_is_available_font(const char *font_name)
61 {
62         static int (*fn_impl)(const char*) = NULL;
63         if (fn_impl)
64                 return fn_impl(font_name);
65
66         void *handle = _utils_load_lib();
67         if (NULL == handle) {
68                 ERR("No Utils Library Handle");
69                 return 0;
70         }
71
72         fn_impl = dlsym(handle, "sstu_is_available_font");
73         char *error = dlerror();
74         if (error) {
75                 ERR("dlsym(sstu_is_available_font) Fail(%s)", error);
76                 return 0;
77         }
78
79         return fn_impl(font_name);
80 }
81
82 char* sstu_get_default_font()
83 {
84         static char* (*fn_impl)(void) = NULL;
85         if (fn_impl)
86                 return fn_impl();
87
88         void *handle = _utils_load_lib();
89         if (NULL == handle) {
90                 ERR("No Utils Library Handle");
91                 return NULL;
92         }
93
94         fn_impl = dlsym(handle, "sstu_get_default_font");
95         char *error = dlerror();
96         if (error) {
97                 ERR("dlsym(sstu_get_default_font) Fail(%s)", error);
98                 return NULL;
99         }
100
101         return fn_impl();
102 }
103
104 bool sstu_is_valid_image(const char *path)
105 {
106         static bool (*fn_impl)(const char*) = NULL;
107         if (fn_impl)
108                 return fn_impl(path);
109
110         void *handle = _utils_load_lib();
111         if (NULL == handle) {
112                 ERR("No Utils Library Handle");
113                 return sst_misc_file_exists(path);
114         }
115
116         fn_impl = dlsym(handle, "sstu_is_valid_image");
117         char *error = dlerror();
118         if (error) {
119                 ERR("dlsym(sstu_is_valid_image) Fail(%s)", error);
120                 return false;
121         }
122
123         return fn_impl(path);
124 }
125 void sstu_set_font_size()
126 {
127         static void (*fn_impl)() = NULL;
128         if (fn_impl)
129                 return fn_impl();
130
131         void *handle = _utils_load_lib();
132         if (NULL == handle) {
133                 ERR("No Utils Library Handle");
134                 return;
135         }
136
137         fn_impl = dlsym(handle, "sstu_set_font_size");
138         char *error = dlerror();
139         if (error) {
140                 ERR("dlsym(sstu_set_font_size) Fail(%s)", error);
141                 return;
142         }
143
144         return fn_impl();
145 }
146
147 void sstu_font_config_set_notification()
148 {
149         static void (*fn_impl)() = NULL;
150         if (fn_impl)
151                 return fn_impl();
152
153         void *handle = _utils_load_lib();
154         if (NULL == handle) {
155                 ERR("No Utils Library Handle");
156                 return;
157         }
158
159         fn_impl = dlsym(handle, "sstu_font_config_set_notification");
160         char *error = dlerror();
161         if (error) {
162                 ERR("dlsym(sstu_font_config_set_notification) Fail(%s)", error);
163                 return;
164         }
165
166         return fn_impl();
167 }
168
169 //It is called after releasing the global variable(sst_utils_handle)
170 //Therefore, It is better to leave as it is.
171 /*
172 __attribute__((destructor)) static void _utils_unload_lib()
173 {
174         dlclose(sst_utils_handle);
175         sst_utils_handle = NULL;
176 }
177 */