aboutsummaryrefslogtreecommitdiff
path: root/options.c
blob: 249af719e738f3f090ab126b7d76d27c9bc3531e (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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "options.h"

int show_fps;
int limit_frames;
int enable_audio;
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 52
#define MAX_LINES 3

static void truncate(char *string, size_t max_len) {
	size_t len = strlen(string) + 1;
	if (len <= max_len) return;

	strncpy(&string[max_len - 4], "...\0", 4);
}

static void wrap(char *string, size_t max_len, size_t max_lines) {
	char *line = string;

	for (size_t i = 1; i < max_lines; i++) {
		char *p = line;
		char *prev;
		do {
			prev = p;
			p = strchr(prev+1, ' ');
		} while (p && p - line < (int)max_len);

		if (!p && strlen(line) < max_len) break;

		if (prev && prev != line) {
			line = prev + 1;
			*prev = '\n';
		}
	}
	truncate(line, max_len);

	return;
}

static const char *blocked_options[] = { "gpsp_save_method", NULL};

static bool option_blocked(const char *key) {
	for (int i = 0; blocked_options[i]; i++) {
		if (!strcmp(blocked_options[i], key))
			return true;
	}
	return false;
}

static int options_default_override(const char *key) {
	if (!strcmp(key, "snes9x2002_frameskip")) {
		return 1;
	} else if (!strcmp(key, "snes9x2002_frameskip_interval")) {
		return 3;
	} else if (!strcmp(key, "mame2000-frameskip")) {
		return 1;
	} else if (!strcmp(key, "mame2000-frameskip_interval")) {
		return 1;
	} else if (!strcmp(key, "mame2000-skip_disclaimer")) {
		return 1;
	} else if (!strcmp(key, "mame2000-sample_rate")) {
		return 1;
	}

	return -1;
}


static int options_default_index(const char *key, const struct retro_core_option_definition *def) {
	const char *value;
	int default_override = options_default_override(key);
	if (default_override >= 0)
		return default_override;

	if (!def)
		return 0;

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

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];

		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->value = options_default_index(def->key, def);
		entry->prev_value = entry->value;
		entry->default_value = entry->value;
		entry->blocked = option_blocked(def->key);
		if (entry->blocked)
			core_options.visible_len--;

		len = strlen(def->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, def->desc, len);
		truncate(entry->desc, MAX_DESC_LEN);

		if (def->info) {
			len = strlen(def->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, def->info, len);
			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 = def->values[j].value;
			size_t value_len = strlen(value);
			const char *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];
			}
		}
	}
}

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];
		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);

		entry->value = options_default_index(var->key, NULL);
		entry->prev_value = entry->value;
		entry->default_value = entry->value;
		entry->blocked = option_blocked(var->key);
		if (entry->blocked)
			core_options.visible_len--;

		len = strlen(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, var->value, len);

		p = strchr(value, ';');
		if (p && *(p + 1) == ' ') {
			*p = '\0';
			entry->desc = value;
			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;
	}
}

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

void options_update_changed(void) {
	if (core_options.changed)
		return;

	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;
}


bool options_is_blocked(const char *key) {
	struct core_option_entry* entry = options_get_entry(key);
	if (entry) {
		return entry->blocked;
	}
	return true;
}

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();
	}
}

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);
			}

			if (entry->labels)
				free(entry->labels);

			if (entry->values)
				free(entry->values);

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