293 lines
12 KiB
C++
293 lines
12 KiB
C++
/* Preference storage only: simulation cells and runtime history are session data. */
|
|
typedef enum { SETTING_INT, SETTING_BOOL, SETTING_ULL } SettingType;
|
|
typedef struct {
|
|
const char *name;
|
|
SettingType type;
|
|
void *value;
|
|
long long defaultValue, minimum, maximum, step;
|
|
} Setting;
|
|
|
|
static int settingsVersion = 1;
|
|
static const Setting settings[] = {
|
|
{"version", SETTING_INT, &settingsVersion, 1, 1, 1, 1},
|
|
{"resolution", SETTING_INT, &resolution, 200, 25, 400, 25},
|
|
{"start_density", SETTING_INT, &startDensity, 50, 0, 100, 5},
|
|
{"update_interval_ms", SETTING_INT, &updateInterval, 100, 10, 1000, 10},
|
|
{"hue", SETTING_INT, &hue, 275, 0, 360, 1},
|
|
{"rainbow_hue", SETTING_BOOL, &rainbowHue, 0, 0, 1, 1},
|
|
{"rainbow_speed", SETTING_INT, &rainbowSpeed, 30, 1, 180, 1},
|
|
{"brush_size", SETTING_INT, &brushSize, 1, 1, 25, 1},
|
|
{"grid_mode", SETTING_INT, &gridMode, GRID_OFF, GRID_OFF, GRID_GUIDELINES, 1},
|
|
{"mirror_mode", SETTING_INT, &mirrorMode, 0, 0, 3, 1},
|
|
{"line_snap", SETTING_BOOL, &snapLine, 1, 0, 1, 1},
|
|
{"circle_from_corner", SETTING_BOOL, &circleFromCorner, 0, 0, 1, 1},
|
|
{"pause_on_draw", SETTING_BOOL, &pauseOnDraw, 1, 0, 1, 1},
|
|
{"loop_enabled", SETTING_BOOL, &loopEnabled, 0, 0, 1, 1},
|
|
{"loop_end", SETTING_ULL, &loopEnd, 100, 1, MAX_LOOP_END, 1},
|
|
{"show_top_bar", SETTING_BOOL, &showTopBar, 1, 0, 1, 1},
|
|
{"show_controls", SETTING_BOOL, &showControls, 1, 0, 1, 1},
|
|
{"tool", SETTING_INT, &tool, TOOL_BRUSH, TOOL_BRUSH, TOOL_SELECT, 1},
|
|
{"selected_stamp", SETTING_INT, &selectedStamp, 0, 0, STAMP_COUNT - 1, 1},
|
|
{"rotation", SETTING_INT, &rotation, 0, 0, 3, 1},
|
|
{"category", SETTING_INT, &category, CATEGORY_DRAW, CATEGORY_DRAW, CATEGORY_HELP, 1},
|
|
{"window_width", SETTING_INT, &windowWidth, 1240, 1, 16384, 1},
|
|
{"window_height", SETTING_INT, &windowHeight, 900, 1, 16384, 1}
|
|
};
|
|
#define SETTING_COUNT (sizeof(settings) / sizeof(settings[0]))
|
|
static char settingsPath[PATH_MAX] = "settings.cfg";
|
|
static char settingsError[160];
|
|
static bool settingsEnabled, hasSavedSettings;
|
|
static long long savedSettings[SETTING_COUNT];
|
|
|
|
static long long settingValue(size_t index) {
|
|
const Setting *s = &settings[index];
|
|
if (s->value == &tool && tool == TOOL_PASTE) return TOOL_SELECT;
|
|
if (s->type == SETTING_BOOL) return *(bool *)s->value;
|
|
if (s->type == SETTING_ULL) return (long long)*(unsigned long long *)s->value;
|
|
return *(int *)s->value;
|
|
}
|
|
|
|
static void applySettings(const long long values[SETTING_COUNT]) {
|
|
for (size_t i = 0; i < SETTING_COUNT; ++i) {
|
|
if (settings[i].type == SETTING_BOOL) *(bool *)settings[i].value = values[i] != 0;
|
|
else if (settings[i].type == SETTING_ULL)
|
|
*(unsigned long long *)settings[i].value = (unsigned long long)values[i];
|
|
else *(int *)settings[i].value = (int)values[i];
|
|
}
|
|
}
|
|
|
|
/* Small, editable name=value format; booleans are 0 or 1. */
|
|
static bool parseSettings(const char *text, long long values[SETTING_COUNT]) {
|
|
bool seen[SETTING_COUNT] = {false};
|
|
for (size_t i = 0; i < SETTING_COUNT; ++i) values[i] = settings[i].defaultValue;
|
|
bool any = false;
|
|
while (*text) {
|
|
const char *end = strchr(text, '\n');
|
|
size_t length = end ? (size_t)(end - text) : strlen(text);
|
|
if (length >= 128) return false;
|
|
char line[128], key[64], extra;
|
|
memcpy(line, text, length);
|
|
line[length] = '\0';
|
|
char *p = line;
|
|
while (isspace((unsigned char)*p)) ++p;
|
|
if (*p && *p != '#') {
|
|
char number[64];
|
|
if (sscanf(p, "%63[a-z_] = %63s %c", key, number, &extra) != 2) return false;
|
|
size_t i;
|
|
for (i = 0; i < SETTING_COUNT; ++i) if (strcmp(key, settings[i].name) == 0) break;
|
|
if (i == SETTING_COUNT || seen[i]) return false;
|
|
char *tail;
|
|
errno = 0;
|
|
long long value = strtoll(number, &tail, 10);
|
|
if (errno || *tail || tail == number || value < settings[i].minimum
|
|
|| value > settings[i].maximum || (value - settings[i].minimum) % settings[i].step)
|
|
return false;
|
|
values[i] = value;
|
|
seen[i] = any = true;
|
|
}
|
|
text += length;
|
|
if (*text == '\n') ++text;
|
|
}
|
|
return any;
|
|
}
|
|
|
|
#ifndef SETTINGS_BEFORE_WRITE
|
|
#define SETTINGS_BEFORE_WRITE() ((void)0)
|
|
#endif
|
|
|
|
/* This function uses only its immutable arguments and is safe on the writer thread. */
|
|
static bool writeSettingsSnapshot(const char *path, const long long values[SETTING_COUNT],
|
|
char error[160]) {
|
|
SETTINGS_BEFORE_WRITE();
|
|
char temporary[PATH_MAX + 16];
|
|
snprintf(temporary, sizeof(temporary), "%s.tmp.XXXXXX", path);
|
|
int fd = mkstemp(temporary);
|
|
if (fd < 0) goto failed;
|
|
FILE *file = fdopen(fd, "w");
|
|
if (!file) {
|
|
close(fd);
|
|
unlink(temporary);
|
|
goto failed;
|
|
}
|
|
bool ok = fprintf(file, "# Game of Life settings; booleans: 0=off, 1=on\n") >= 0;
|
|
for (size_t i = 0; i < SETTING_COUNT; ++i)
|
|
if (fprintf(file, "%s=%lld\n", settings[i].name, values[i]) < 0) ok = false;
|
|
if (fflush(file) != 0) ok = false;
|
|
if (ok && fsync(fd) != 0) ok = false;
|
|
if (fclose(file) != 0) ok = false;
|
|
if (!ok || rename(temporary, path) != 0) {
|
|
unlink(temporary);
|
|
goto failed;
|
|
}
|
|
error[0] = '\0';
|
|
return true;
|
|
failed:
|
|
snprintf(error, 160, "Cannot save settings.cfg. Check that the game folder is writable.");
|
|
return false;
|
|
}
|
|
|
|
/* Synchronous startup repair; interactive saves use the worker below. */
|
|
static bool writeSettings(void) {
|
|
long long values[SETTING_COUNT];
|
|
for (size_t i = 0; i < SETTING_COUNT; ++i) values[i] = settingValue(i);
|
|
if (!writeSettingsSnapshot(settingsPath, values, settingsError)) return false;
|
|
memcpy(savedSettings, values, sizeof(savedSettings));
|
|
hasSavedSettings = true;
|
|
return true;
|
|
}
|
|
|
|
static pthread_t settingsThread;
|
|
static pthread_mutex_t settingsMutex = PTHREAD_MUTEX_INITIALIZER;
|
|
static pthread_cond_t settingsCondition = PTHREAD_COND_INITIALIZER;
|
|
static bool settingsWorkerRunning, settingsWorkerStopping, settingsWritePending;
|
|
static bool settingsResultReady, settingsResultSuccess;
|
|
static long long pendingSettings[SETTING_COUNT], resultSettings[SETTING_COUNT];
|
|
static long long queuedSettings[SETTING_COUNT]; /* UI-thread-only comparison cache. */
|
|
static bool hasQueuedSettings;
|
|
static char pendingSettingsPath[PATH_MAX], settingsResultError[160];
|
|
static struct timespec settingsDeadline;
|
|
|
|
static void *settingsWriter(void *unused) {
|
|
(void)unused;
|
|
pthread_mutex_lock(&settingsMutex);
|
|
for (;;) {
|
|
while (!settingsWritePending && !settingsWorkerStopping)
|
|
pthread_cond_wait(&settingsCondition, &settingsMutex);
|
|
if (!settingsWritePending && settingsWorkerStopping) break;
|
|
if (!settingsWorkerStopping) {
|
|
int result = pthread_cond_timedwait(&settingsCondition, &settingsMutex, &settingsDeadline);
|
|
if (result != ETIMEDOUT) continue; /* A new change extends the debounce. */
|
|
}
|
|
long long values[SETTING_COUNT];
|
|
char path[PATH_MAX], error[160] = {0};
|
|
memcpy(values, pendingSettings, sizeof(values));
|
|
memcpy(path, pendingSettingsPath, sizeof(path));
|
|
settingsWritePending = false;
|
|
pthread_mutex_unlock(&settingsMutex);
|
|
|
|
bool success = writeSettingsSnapshot(path, values, error);
|
|
|
|
pthread_mutex_lock(&settingsMutex);
|
|
settingsResultReady = true;
|
|
settingsResultSuccess = success;
|
|
memcpy(resultSettings, values, sizeof(values));
|
|
memcpy(settingsResultError, error, sizeof(error));
|
|
}
|
|
pthread_mutex_unlock(&settingsMutex);
|
|
return NULL;
|
|
}
|
|
|
|
static void pollSettingsWriter(void) {
|
|
if (!settingsWorkerRunning) return;
|
|
pthread_mutex_lock(&settingsMutex);
|
|
if (settingsResultReady) {
|
|
if (settingsResultSuccess) {
|
|
memcpy(savedSettings, resultSettings, sizeof(savedSettings));
|
|
hasSavedSettings = true;
|
|
}
|
|
memcpy(settingsError, settingsResultError, sizeof(settingsError));
|
|
settingsResultReady = false;
|
|
}
|
|
pthread_mutex_unlock(&settingsMutex);
|
|
}
|
|
|
|
static void saveSettingsIfChanged(void) {
|
|
if (!settingsEnabled) return;
|
|
long long values[SETTING_COUNT];
|
|
for (size_t i = 0; i < SETTING_COUNT; ++i) values[i] = settingValue(i);
|
|
const long long *previous = settingsWorkerRunning ? queuedSettings : savedSettings;
|
|
bool hasPrevious = settingsWorkerRunning ? hasQueuedSettings : hasSavedSettings;
|
|
if (hasPrevious && memcmp(values, previous, sizeof(values)) == 0) return;
|
|
if (!settingsWorkerRunning) { writeSettings(); return; }
|
|
|
|
/* No file operations or disk synchronization occur in input callbacks. */
|
|
pthread_mutex_lock(&settingsMutex);
|
|
memcpy(pendingSettings, values, sizeof(values));
|
|
memcpy(pendingSettingsPath, settingsPath, sizeof(settingsPath));
|
|
clock_gettime(CLOCK_REALTIME, &settingsDeadline);
|
|
settingsDeadline.tv_nsec += 250000000L;
|
|
if (settingsDeadline.tv_nsec >= 1000000000L) {
|
|
++settingsDeadline.tv_sec;
|
|
settingsDeadline.tv_nsec -= 1000000000L;
|
|
}
|
|
settingsWritePending = true;
|
|
pthread_cond_signal(&settingsCondition);
|
|
pthread_mutex_unlock(&settingsMutex);
|
|
memcpy(queuedSettings, values, sizeof(values));
|
|
hasQueuedSettings = true;
|
|
}
|
|
|
|
static void startSettingsWriter(void) {
|
|
if (settingsWorkerRunning || !settingsEnabled) return;
|
|
settingsWorkerStopping = settingsWritePending = settingsResultReady = false;
|
|
hasQueuedSettings = hasSavedSettings;
|
|
memcpy(queuedSettings, savedSettings, sizeof(queuedSettings));
|
|
if (pthread_create(&settingsThread, NULL, settingsWriter, NULL) != 0) {
|
|
snprintf(settingsError, sizeof(settingsError), "Cannot start settings autosave worker.");
|
|
settingsEnabled = false;
|
|
return;
|
|
}
|
|
settingsWorkerRunning = true;
|
|
}
|
|
|
|
static void stopSettingsWriter(void) {
|
|
if (!settingsWorkerRunning) return;
|
|
saveSettingsIfChanged();
|
|
pthread_mutex_lock(&settingsMutex);
|
|
settingsWorkerStopping = true;
|
|
pthread_cond_signal(&settingsCondition);
|
|
pthread_mutex_unlock(&settingsMutex);
|
|
/* Only shutdown waits for disk I/O, including the latest queued preference. */
|
|
pthread_join(settingsThread, NULL);
|
|
pollSettingsWriter();
|
|
settingsWorkerRunning = false;
|
|
}
|
|
|
|
static bool loadSettings(void) {
|
|
char buffer[8192];
|
|
long long values[SETTING_COUNT];
|
|
bool valid = false;
|
|
FILE *file = fopen(settingsPath, "rb");
|
|
if (file) {
|
|
size_t count = fread(buffer, 1, sizeof(buffer) - 1, file);
|
|
bool readable = !ferror(file) && feof(file);
|
|
buffer[count] = '\0';
|
|
if (readable && !memchr(buffer, '\0', count)) valid = parseSettings(buffer, values);
|
|
fclose(file);
|
|
}
|
|
if (!valid)
|
|
for (size_t i = 0; i < SETTING_COUNT; ++i) values[i] = settings[i].defaultValue;
|
|
applySettings(values);
|
|
settingsEnabled = true;
|
|
hasSavedSettings = false;
|
|
/* Canonical output also fills any omitted preferences with their defaults. */
|
|
writeSettings();
|
|
return valid;
|
|
}
|
|
|
|
static void initializeSettings(void) {
|
|
#if !APP_PORTABLE
|
|
if (!initializeUserPaths(settingsPath)) {
|
|
fprintf(stderr, "%s: cannot create user configuration/data directories.\n", APP_NAME);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
#else
|
|
/* Keep preferences beside the executable, even when launched from elsewhere. */
|
|
char executable[PATH_MAX];
|
|
ssize_t length = readlink("/proc/self/exe", executable, sizeof(executable) - 1);
|
|
if (length > 0 && (size_t)length < sizeof(executable) - 1) {
|
|
executable[length] = '\0';
|
|
char *slash = strrchr(executable, '/');
|
|
if (slash) {
|
|
slash[1] = '\0';
|
|
size_t prefixLength = strlen(executable);
|
|
if (prefixLength + sizeof("settings.cfg") <= sizeof(settingsPath)) {
|
|
memcpy(settingsPath, executable, prefixLength);
|
|
memcpy(settingsPath + prefixLength, "settings.cfg", sizeof("settings.cfg"));
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
loadSettings();
|
|
}
|