aboutsummaryrefslogtreecommitdiff
path: root/options.c
blob: decc29c7dc70261bbf3d41dc3cedc92c193377d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "options.h"
#include "overrides.h"
#include "util.h"
#include "scale.h"

int show_fps;
int show_cpu;
int show_hud;
int limit_frames;
int enable_audio;
int enable_drc;
unsigned audio_buffer_size;
enum scale_size scale_size;
enum scale_filter scale_filter;

struct core_options core_options;

#define MAX_DESC_LEN 20
#define MAX_LINE_LEN SCREEN_WIDTH / 6
#define MAX_LINES 4

static int options_default_index(const struct core_option_entry* entry, const char *default_value) {
	const char *value;
	if (!default_value)
		return 0;

	for(int i = 0; (value = entry->values[i]); i++) {
		if (!strcmp(value, default_value)) {
			return i;
		}
	}
	return 0;
}

static const struct core_override_option *get_option_override(const char *key) {
	const struct core_override *override = NULL;
	const struct core_override_option *option = NULL;
	const char *option_key = NULL;

	if (!(override = get_overrides()))
		return NULL;

	for (int i = 0; (option_key = override->options[i].key); i++) {
		if (!strcmp(key, option_key))
			return &override->options[i];
	}

	return option;
}

void options_init(const struct retro_core_option_definition *defs) {
	size_t i;

	for (i = 0; defs[i].key; i++)
 		;

	core_options.visible_len = core_options.len = i;

	core_options.entries = (struct core_option_entry *)calloc(core_options.len, sizeof(struct core_option_entry));
	if (!core_options.entries) {
		PA_ERROR("Error allocating option entries\n");
		options_free();
		return;
	}

	for (i = 0; i < core_options.len; i++) {
		int j, len;
		const struct retro_core_option_definition *def = &defs[i];
		struct core_option_entry *entry = &core_options.entries[i];
		const struct core_override_option *override = get_option_override(def->key);
		const char *desc = CORE_OVERRIDE(override, desc, def->desc);
		const char *info = CORE_OVERRIDE(override, info, def->info);

		len = strlen(def->key) + 1;
		entry->key = (char *)calloc(len, sizeof(char));
		if (!entry->key) {
			PA_ERROR("Error allocating option entries\n");
			options_free();
			return;
		}
		strncpy(entry->key, def->key, len);

		entry->blocked = CORE_OVERRIDE(override, blocked, false);
		if (entry->blocked)
			core_options.visible_len--;
		entry->visible = true;

		len = strlen(desc) + 1;
		entry->desc = (char *)calloc(len, sizeof(char));
		if (!entry->desc) {
			PA_ERROR("Error allocating option entries\n");
			options_free();
			return;
		}

		strncpy(entry->desc, desc, len);
		string_truncate(entry->desc, MAX_DESC_LEN);

		if (info) {
			len = strlen(info) + 1;
			entry->info = (char *)calloc(len, sizeof(char));
			if (!entry->info) {
				PA_ERROR("Error allocating description string\n");
				options_free();
				return;
			}
			strncpy(entry->info, info, len);
			string_wrap(entry->info, MAX_LINE_LEN, MAX_LINES);
		}

		for (j = 0; def->values[j].value; j++)
			;
		j++; /* Make room for NULL entry */

		entry->values = (char **)calloc(j, sizeof(char *));
		if (!entry->values) {
			PA_ERROR("Error allocating option entries\n");
			options_free();
			return;
		}

		entry->labels = (char **)calloc(j, sizeof(char *));
		if (!entry->labels) {
			PA_ERROR("Error allocating option entries\n");
			options_free();
			return;
		}

		for (j = 0; def->values[j].value; j++) {
			const char *value = CORE_OVERRIDE(override, options[j].value, def->values[j].value);
			size_t value_len = strlen(value);
			const char *label = CORE_OVERRIDE(override, options[j].label, def->values[j].label);
			size_t label_len = 0;

			entry->values[j] = (char *)calloc(value_len + 1, sizeof(char));
			if (!entry->values[j]) {
				PA_ERROR("Error allocating memory for option values");
				options_free();
				return;
			}

			strncpy(entry->values[j], value, value_len);

			if (label) {
				label_len = strlen(label);

				entry->labels[j] = (char *)calloc(label_len + 1, sizeof(char));
				if (!entry->labels[j]) {
					PA_ERROR("Error allocating memory for option labels");
					options_free();
					return;
				}

				strncpy(entry->labels[j], label, label_len);
			} else {
				entry->labels[j] = entry->values[j];
			}
		}

		entry->value = options_default_index(
			entry,
			CORE_OVERRIDE(override, default_value, def->default_value));

		entry->prev_value = entry->value;
		entry->default_value = entry->value;
	}
}

void options_init_variables(const struct retro_variable *vars) {
	size_t i;

	for (i = 0; vars[i].key; i++)
		;

	core_options.visible_len = core_options.len = i;

	core_options.entries = (struct core_option_entry *)calloc(core_options.len, sizeof(struct core_option_entry));
	if (!core_options.entries) {
		PA_ERROR("Error allocating option entries\n");
		options_free();
		return;
	}

	for (i = 0; i < core_options.len; i++) {
		int j = 0;
		int len;
		struct core_option_entry *entry = &core_options.entries[i];
		const struct retro_variable *var = &vars[i];
		const struct core_override_option *override = get_option_override(var->key);
		const char *info = CORE_OVERRIDE(override, info, NULL);
		const char *retro_var_value = CORE_OVERRIDE(override, retro_var_value, var->value);
		char *p;
		char *opt_ptr;
		char *value;

		len = strlen(var->key) + 1;
		entry->key = (char *)calloc(len, sizeof(char));
		if (!entry->key) {
			PA_ERROR("Error allocating option entries\n");
			options_free();
			return;
		}
		strncpy(entry->key, var->key, len);

		if (info) {
			len = strlen(info) + 1;
			entry->info = (char *)calloc(len, sizeof(char));
			if (!entry->info) {
				PA_ERROR("Error allocating description string\n");
				options_free();
				return;
			}
			strncpy(entry->info, info, len);
			string_wrap(entry->info, MAX_LINE_LEN, MAX_LINES);
		}

		entry->blocked = CORE_OVERRIDE(override, blocked, false);
		if (entry->blocked)
			core_options.visible_len--;

		entry->visible = true;
		len = strlen(retro_var_value) + 1;
		value = (char *)calloc(len, sizeof(char));
		if (!value) {
			PA_ERROR("Error allocating option entries\n");
			options_free();
			return;
		}

		entry->retro_var_value = value;

		strncpy(value, retro_var_value, len);

		p = strchr(value, ';');
		if (p && *(p + 1) == ' ') {
			*p = '\0';
			entry->desc = value;
			string_truncate(entry->desc, MAX_DESC_LEN);
			p++;
			p++;
		}

		opt_ptr = p;

		for(j = 0; (p = strchr(p, '|')); p++, j++)
			;
		j++; /* Make room for last entry (not ending in |) */
		j++; /* Make room for NULL entry */

		entry->values = (char **)calloc(j, sizeof(char *));
		if (!entry->values) {
			PA_ERROR("Error allocating option entries\n");
			options_free();
			return;
		}

		entry->labels = (char **)calloc(j, sizeof(char *));
		if (!entry->labels) {
			PA_ERROR("Error allocating option entries\n");
			options_free();
			return;
		}

		p = opt_ptr;
		for (j = 0; (p = strchr(p, '|')); j++) {
			entry->values[j] = opt_ptr;
			entry->labels[j] = opt_ptr;
			*p = '\0';
			p++;
			opt_ptr = p;
		}
		entry->values[j] = opt_ptr;
		entry->labels[j] = opt_ptr;

		entry->value = options_default_index(entry, CORE_OVERRIDE(override, default_value, NULL));
		entry->prev_value = entry->value;
		entry->default_value = entry->value;
	}
}

bool options_changed(void) {
	bool was_changed = core_options.changed;
	core_options.changed = false;
	return was_changed;
}

void options_update_changed(void) {
	for(size_t i = 0; i < core_options.len; i++) {
		struct core_option_entry* entry = &core_options.entries[i];
		if (entry->value != entry->prev_value) {
			core_options.changed = true;
			entry->prev_value = entry->value;
		}
	}
}

const char* options_get_key(int index) {
	return core_options.entries[index].key;
}

struct core_option_entry* options_get_entry(const char* key) {
	for(size_t i = 0; i < core_options.len; i++) {
		const char *opt_key = options_get_key(i);

		if (!strcmp(opt_key, key)) {
			return &core_options.entries[i];
		}
	}
	return NULL;
}

const char* options_get_value(const char* key) {
	struct core_option_entry* entry = options_get_entry(key);
	if (entry)
		return entry->values[entry->value];

	return NULL;
}

int* options_get_value_ptr(const char* key) {
	struct core_option_entry* entry = options_get_entry(key);
	if (entry) {
		return &(entry->value);
	}
	return NULL;
}

int options_get_value_index(const char* key) {
	struct core_option_entry* entry = options_get_entry(key);
	if (entry) {
		return entry->value;
	}
	return 0;
}

void options_set_value(const char* key, const char *value) {
	struct core_option_entry* entry = options_get_entry(key);
	if (entry) {
		char *option;
		entry->value = entry->default_value;

		for (int i = 0; (option = entry->values[i]); i++) {
			if (!strcmp(option, value)) {
				entry->value = i;
				options_update_changed();
				return;
			}
		}
	}
}

void options_set_value_index(const char* key, int value) {
	struct core_option_entry* entry = options_get_entry(key);
	if (entry) {
		entry->value = value;
		options_update_changed();
	}
}

void options_set_visible(const char* key, bool visible) {
	struct core_option_entry* entry = options_get_entry(key);
	if (entry) {
		entry->visible = visible;
	}
}

const char** options_get_options(const char* key) {
	struct core_option_entry* entry = options_get_entry(key);
	if (entry) {
		return (const char **)entry->labels;
	}
	return NULL;
}

void options_free(void) {
	if (core_options.entries) {
		for (size_t i = 0; i < core_options.len; i++) {
			struct core_option_entry* entry = &core_options.entries[i];
			if (entry->retro_var_value) {
				/* option values / labels are all pointers into retro_var_value,
				 * no need to free them one by one */
				free(entry->retro_var_value);
			} else {
				if (entry->labels) {
					char *label;
					for (int j = 0; (label = entry->labels[j]); j++) {
						if (label != entry->values[j])
							free(label);
					}
				}

				if (entry->values) {
					char *value;
					for (int j = 0; (value = entry->values[j]); j++) {
					        free(value);
					}
				}

				if (entry->desc)
					free(entry->desc);
			}

			free(entry->info);
			free(entry->labels);
			free(entry->values);
			free(entry->key);
		}
		free(core_options.entries);
	}
	memset(&core_options, 0, sizeof(core_options));
}