Fix error in gcc 12.2.0
[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 <string.h>
20 #include "sst.h"
21 #include "sst_misc.h"
22
23 static void *sst_utils_handle;
24 static const char* const sst_utils_library = SETTING_UTILS_SO_FILE_PATH;
25
26 static void* _utils_load_lib()
27 {
28         if (sst_utils_handle)
29                 return sst_utils_handle;
30
31         sst_utils_handle = dlopen(sst_utils_library, RTLD_LAZY | RTLD_GLOBAL);
32         if (NULL == sst_utils_handle) {
33                 ERR("dlopen(%s) Fail", sst_utils_library);
34         }
35
36         return sst_utils_handle;
37 }
38
39 bool sstu_set_font_config(const char *font_name)
40 {
41         static bool (*fn_impl)(const char*);
42         if (fn_impl)
43                 return fn_impl(font_name);
44
45         void *handle = _utils_load_lib();
46         if (NULL == handle) {
47                 ERR("No Utils Library Handle");
48                 return false;
49         }
50
51         fn_impl = dlsym(handle, "sstu_set_font_config");
52         char *error = dlerror();
53         if (error) {
54                 ERR("dlsym(sstu_set_font_config) Fail(%s)", error);
55                 return false;
56         }
57
58         return fn_impl(font_name);
59 }
60
61 int sstu_is_available_font(const char *font_name)
62 {
63         static int (*fn_impl)(const char*) = NULL;
64         if (fn_impl)
65                 return fn_impl(font_name);
66
67         void *handle = _utils_load_lib();
68         if (NULL == handle) {
69                 ERR("No Utils Library Handle");
70                 return 0;
71         }
72
73         fn_impl = dlsym(handle, "sstu_is_available_font");
74         char *error = dlerror();
75         if (error) {
76                 ERR("dlsym(sstu_is_available_font) Fail(%s)", error);
77                 return 0;
78         }
79
80         return fn_impl(font_name);
81 }
82
83 char* sstu_get_default_font()
84 {
85         static char* (*fn_impl)(void) = NULL;
86         if (fn_impl)
87                 return fn_impl();
88
89         void *handle = _utils_load_lib();
90         if (NULL == handle) {
91                 ERR("No Utils Library Handle");
92                 return NULL;
93         }
94
95         fn_impl = dlsym(handle, "sstu_get_default_font");
96         char *error = dlerror();
97         if (error) {
98                 ERR("dlsym(sstu_get_default_font) Fail(%s)", error);
99                 return NULL;
100         }
101
102         return fn_impl();
103 }
104
105 bool sstu_is_valid_image(const char *path)
106 {
107         if (!sst_misc_file_exists(path))
108                 return false;
109
110         char *ext_list[10] = {"jif", "jfif", "jpe", "jpeg", "jpg",
111                                                         "bmp", "dib", "gif", "png", "webp",
112                                                         };
113         char *filename = strrchr(path, '/');
114         if (NULL == filename || '\0' == filename[1])
115                 return false;
116         char *ext = strrchr(filename + 1, '.');
117         if (NULL == ext || '\0' == ext[1])
118                 return false;
119         for (int i =  0; i < 10; i++) {
120                 if (!strcmp(ext + 1, ext_list[i]))
121                         return true;
122         }
123         return false;
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 */