Debian Codegraph @

addons/native_dialog/dialog.c

raw
1 #define ALLEGRO_INTERNAL_UNSTABLE
2
3 #include "allegro5/allegro.h"
4 #include "allegro5/allegro_native_dialog.h"
5 #include "allegro5/internal/aintern_dtor.h"
6 #include "allegro5/internal/aintern_exitfunc.h"
7 #include "allegro5/internal/aintern_native_dialog.h"
8 #include "allegro5/internal/aintern_system.h"
9 #include "allegro5/internal/aintern_vector.h"
10
11 ALLEGRO_DEBUG_CHANNEL("native_dialog")
12
13 static bool inited_addon = false;
14
15 static _AL_VECTOR make_patterns_and_desc_vec(const ALLEGRO_USTR* patterns_ustr);
16 static void free_patterns_and_desc_vec(_AL_VECTOR *patterns_and_desc_vec);
17
18 /* Function: al_init_native_dialog_addon
19 */
20 bool al_init_native_dialog_addon(void)
21 {
22 if (!inited_addon) {
23 if (!_al_init_native_dialog_addon()) {
24 ALLEGRO_ERROR("_al_init_native_dialog_addon failed.\n");
25 return false;
26 }
27 inited_addon = true;
28 al_init_user_event_source(al_get_default_menu_event_source());
29 _al_add_exit_func(al_shutdown_native_dialog_addon,
30 "al_shutdown_native_dialog_addon");
31 }
32 return true;
33 }
34
35 /* Function: al_is_native_dialog_addon_initialized
36 */
37 bool al_is_native_dialog_addon_initialized(void)
38 {
39 return inited_addon;
40 }
41
42 /* Function: al_shutdown_native_dialog_addon
43 */
44 void al_shutdown_native_dialog_addon(void)
45 {
46 if (inited_addon) {
47 al_destroy_user_event_source(al_get_default_menu_event_source());
48 _al_shutdown_native_dialog_addon();
49 inited_addon = false;
50 }
51 }
52
53 /* Function: al_create_native_file_dialog
54 */
55 ALLEGRO_FILECHOOSER *al_create_native_file_dialog(
56 char const *initial_path,
57 char const *title,
58 char const *patterns,
59 int mode)
60 {
61 ALLEGRO_NATIVE_DIALOG *fc;
62 fc = al_calloc(1, sizeof *fc);
63
64 if (initial_path) {
65 fc->fc_initial_path = al_create_path(initial_path);
66 }
67 fc->title = al_ustr_new(title);
68 fc->fc_patterns_ustr = al_ustr_new(patterns);
69 fc->fc_patterns = make_patterns_and_desc_vec(fc->fc_patterns_ustr);
70 fc->flags = mode;
71
72 fc->dtor_item = _al_register_destructor(_al_dtor_list, "native_dialog", fc,
73 (void (*)(void *))al_destroy_native_file_dialog);
74
75 return (ALLEGRO_FILECHOOSER *)fc;
76 }
77
78 /* Function: al_show_native_file_dialog
79 */
80 bool al_show_native_file_dialog(ALLEGRO_DISPLAY *display,
81 ALLEGRO_FILECHOOSER *dialog)
82 {
83 ALLEGRO_NATIVE_DIALOG *fd = (ALLEGRO_NATIVE_DIALOG *)dialog;
84 return _al_show_native_file_dialog(display, fd);
85 }
86
87 /* Function: al_get_native_file_dialog_count
88 */
89 int al_get_native_file_dialog_count(const ALLEGRO_FILECHOOSER *dialog)
90 {
91 const ALLEGRO_NATIVE_DIALOG *fc = (const ALLEGRO_NATIVE_DIALOG *)dialog;
92 return fc->fc_path_count;
93 }
94
95 /* Function: al_get_native_file_dialog_path
96 */
97 const char *al_get_native_file_dialog_path(
98 const ALLEGRO_FILECHOOSER *dialog, size_t i)
99 {
100 const ALLEGRO_NATIVE_DIALOG *fc = (const ALLEGRO_NATIVE_DIALOG *)dialog;
101 if (i < fc->fc_path_count)
102 return al_path_cstr(fc->fc_paths[i], ALLEGRO_NATIVE_PATH_SEP);
103 return NULL;
104 }
105
106 /* Function: al_destroy_native_file_dialog
107 */
108 void al_destroy_native_file_dialog(ALLEGRO_FILECHOOSER *dialog)
109 {
110 ALLEGRO_NATIVE_DIALOG *fd = (ALLEGRO_NATIVE_DIALOG *)dialog;
111 size_t i;
112
113 if (!fd)
114 return;
115
116 _al_unregister_destructor(_al_dtor_list, fd->dtor_item);
117
118 al_ustr_free(fd->title);
119 al_destroy_path(fd->fc_initial_path);
120 for (i = 0; i < fd->fc_path_count; i++) {
121 al_destroy_path(fd->fc_paths[i]);
122 }
123 al_free(fd->fc_paths);
124 free_patterns_and_desc_vec(&fd->fc_patterns);
125 al_ustr_free(fd->fc_patterns_ustr);
126 al_free(fd);
127 }
128
129 /* Function: al_show_native_message_box
130 */
131 int al_show_native_message_box(ALLEGRO_DISPLAY *display,
132 char const *title, char const *heading, char const *text,
133 char const *buttons, int flags)
134 {
135 ALLEGRO_NATIVE_DIALOG *fc;
136 int r;
137
138 ASSERT(title);
139 ASSERT(heading);
140 ASSERT(text);
141
142 /* Note: the message box code cannot assume that Allegro is installed.
143 * al_malloc and ustr functions are okay (with the assumption that the
144 * user doesn't change the memory management functions in another thread
145 * while this message box is open).
146 */
147
148 fc = al_calloc(1, sizeof *fc);
149
150 fc->title = al_ustr_new(title);
151 fc->mb_heading = al_ustr_new(heading);
152 fc->mb_text = al_ustr_new(text);
153 fc->mb_buttons = al_ustr_new(buttons);
154 fc->flags = flags;
155
156 r = _al_show_native_message_box(display, fc);
157
158 al_ustr_free(fc->title);
159 al_ustr_free(fc->mb_heading);
160 al_ustr_free(fc->mb_text);
161 al_ustr_free(fc->mb_buttons);
162 al_free(fc);
163
164 return r;
165 }
166
167 /* Function: al_get_allegro_native_dialog_version
168 */
169 uint32_t al_get_allegro_native_dialog_version(void)
170 {
171 return ALLEGRO_VERSION_INT;
172 }
173
174
175 static _AL_VECTOR split_patterns(const ALLEGRO_USTR* ustr)
176 {
177 int pattern_start = 0;
178 int cur_pos = 0;
179 bool is_mime = false;
180 bool is_catchall = true;
181 _AL_VECTOR patterns_vec;
182 _al_vector_init(&patterns_vec, sizeof(_AL_PATTERN));
183 /* This does a straightforward split on semicolons. We check for MIME types
184 * and catchalls, as some backends care about this.
185 */
186 while (true) {
187 int32_t c = al_ustr_get(ustr, cur_pos);
188 if (c == -1 || c == ';') {
189 ALLEGRO_USTR_INFO info;
190 const ALLEGRO_USTR *pattern_ustr = al_ref_buffer(&info,
191 al_cstr(ustr) + pattern_start, cur_pos - pattern_start);
192 if (al_ustr_length(pattern_ustr) > 0) {
193 _AL_PATTERN pattern = {
194 .info = info, .is_mime = is_mime, .is_catchall = is_catchall
195 };
196 *((_AL_PATTERN*)_al_vector_alloc_back(&patterns_vec)) = pattern;
197 }
198
199 is_mime = false;
200 is_catchall = true;
201 pattern_start = cur_pos + 1;
202 }
203 else if (c == '/') {
204 is_mime = true;
205 is_catchall = false;
206 }
207 else if (c != '*' && c != '.') {
208 is_catchall = false;
209 }
210 if (c == -1) {
211 break;
212 }
213 al_ustr_next(ustr, &cur_pos);
214 }
215 return patterns_vec;
216 }
217
218
219 static _AL_VECTOR make_patterns_and_desc_vec(const ALLEGRO_USTR* patterns_ustr)
220 {
221 _AL_VECTOR patterns_and_desc_vec;
222 _al_vector_init(&patterns_and_desc_vec, sizeof(_AL_PATTERNS_AND_DESC));
223
224 if (al_ustr_length(patterns_ustr) == 0)
225 return patterns_and_desc_vec;
226
227 int line_start = 0;
228 int chunk_start = 0;
229 int cur_pos = 0;
230 /* Split by newlines + spaces simultaneously. Chunks are separated by
231 * spaces, and the final chunk is interpreted as the actual patterns, while
232 * the rest of the line is the "description".
233 */
234 while (true) {
235 int32_t c = al_ustr_get(patterns_ustr, cur_pos);
236 if (c == ' ') {
237 chunk_start = cur_pos + 1;
238 }
239 else if (c == '\n' || c == -1) {
240 ALLEGRO_USTR_INFO desc_info, real_patterns_info;
241 const ALLEGRO_USTR *ustr;
242 /* Strip trailing whitespace. */
243 int desc_end = chunk_start - 1;
244 for (; desc_end >= line_start; desc_end--) {
245 if (al_ustr_get(patterns_ustr, desc_end) != ' ')
246 break;
247 }
248 al_ref_ustr(&desc_info, patterns_ustr, line_start, desc_end + 1);
249 ustr = al_ref_ustr(&real_patterns_info, patterns_ustr, chunk_start, cur_pos);
250
251 _AL_VECTOR patterns_vec = split_patterns(ustr);
252 if (_al_vector_size(&patterns_vec) > 0) {
253 _AL_PATTERNS_AND_DESC patterns_and_desc = {
254 .desc = desc_info,
255 .patterns_vec = patterns_vec
256 };
257 *((_AL_PATTERNS_AND_DESC*)_al_vector_alloc_back(&patterns_and_desc_vec)) = patterns_and_desc;
258 }
259
260 chunk_start = cur_pos + 1;
261 line_start = cur_pos + 1;
262 }
263 if (c == -1) {
264 break;
265 }
266 al_ustr_next(patterns_ustr, &cur_pos);
267 }
268
269 return patterns_and_desc_vec;
270 }
271
272
273 static void free_patterns_and_desc_vec(_AL_VECTOR *patterns_and_desc_vec)
274 {
275 for (size_t i = 0; i < _al_vector_size(patterns_and_desc_vec); i++) {
276 _AL_PATTERNS_AND_DESC *patterns_and_desc =
277 (_AL_PATTERNS_AND_DESC*)_al_vector_ref(patterns_and_desc_vec, i);
278 _al_vector_free(&patterns_and_desc->patterns_vec);
279 }
280 _al_vector_free(patterns_and_desc_vec);
281 }
282
283 /* vim: set sts=3 sw=3 et: */