micetools/src/micetools/lib/mice/config.c

193 lines
9.2 KiB
C

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../../../subprojects/inih_dep/ini.h"
#include "../../dll/devices/smb_pca9535.h"
#include "../../dll/hooks/files.h"
#include "../../dll/key_config.h"
config_t MiceConfig = {
#define SECTION(s, comment) .s = {
#define CFG_str(s, n, default, comment) .n = default,
#define CFG_bool(s, n, default, comment) .n = default,
#define CFG_int(s, n, default, comment) .n = default,
#define CFG_hex(s, n, precision, default, comment) .n = 0x##default,
#define CFG_ipv4(s, n, a, b, c, d, comment) \
.n = (unsigned int)(((unsigned char)a << 24) | ((unsigned char)b << 16) | \
((unsigned char)c << 8) | (unsigned char)d),
#define ENDSECTION(s) \
} \
,
#include "config.def"
._keep_linter_happy = true
};
static void fprintf_prefix(FILE *file, const char *prefix, const char *text) {
char *copy = (char *)malloc(strlen(text) + 1);
memcpy_s(copy, strlen(text) + 1, text, strlen(text) + 1);
char *next_token;
char *token = strtok_s(copy, "\n", &next_token);
while (token != NULL) {
fprintf(file, "%s%s\n", prefix, token);
token = strtok_s(NULL, "\n", &next_token);
}
free(copy);
}
char keybindBuffer[2 * JVS_IO_MAX * (sizeof _MiceJvsBoards[0].m_Bindings[0]) * 32];
void save_current_config(bool writeDefault) {
CreateDirectoryA("mice", NULL); // TODO: A touch nicer?
FILE *config_file;
fopen_s(&config_file, CONFIG_PATH, "w");
if (config_file == NULL) {
puts("Failed to create config file!");
return;
}
int first_section = true;
#define CFG_str(s, n, default, comment) \
if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
fprintf(config_file, "; (string) default = %s\n", writeDefault ? MiceConfig.s.n : default); \
fprintf(config_file, "%s = %s\n", #n, MiceConfig.s.n);
#define CFG_bool(s, n, default, comment) \
if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
fprintf(config_file, "; (bool) default = %s\n", \
(writeDefault ? MiceConfig.s.n : default) ? "true" : "false"); \
fprintf(config_file, "%s = %s\n", #n, MiceConfig.s.n ? "true" : "false");
#define CFG_int(s, n, default, comment) \
if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
fprintf(config_file, "; (int) default = %d\n", writeDefault ? MiceConfig.s.n : default); \
fprintf(config_file, "%s = %d\n", #n, MiceConfig.s.n);
#define CFG_hex(s, n, precision, default, comment) \
if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
fprintf(config_file, "; (hex, %d byte%s) default = %.*X \n", precision, \
precision == 1 ? "" : "s", precision, writeDefault ? MiceConfig.s.n : 0x##default); \
fprintf(config_file, "%s = %.*X\n", #n, precision, MiceConfig.s.n);
#define CFG_ipv4(s, n, a, b, c, d, comment) \
if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
fprintf(config_file, "; (ipv4) default = %hhu.%hhu.%hhu.%hhu ;(ipv4)\n", \
writeDefault ? MiceConfig.s.n >> 24 : a, \
writeDefault ? (MiceConfig.s.n >> 16) & 0xff : b, \
writeDefault ? (MiceConfig.s.n >> 8) & 0xff : c, \
writeDefault ? (MiceConfig.s.n & 0xff) : d); \
fprintf(config_file, "%s = %hhu.%hhu.%hhu.%hhu\n", #n, (MiceConfig.s.n >> 24), \
((MiceConfig.s.n >> 16) & 0xff), ((MiceConfig.s.n >> 8) & 0xff), \
(MiceConfig.s.n & 0xff));
#define SECTION(s, comment) \
if (!first_section) fprintf(config_file, "\n"); \
first_section = false; \
if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
fprintf(config_file, "[%s]\n", #s);
#define COMMENT(comment) \
if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment);
#include "config.def"
fclose(config_file);
}
static int handler(void *user, const char *section, const char *name, const char *value) {
config_t *cfg = (config_t *)user;
char *end;
if (false)
;
#define CFG_str(s, n, default, comment) \
else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) cfg->s.n = _strdup(value);
#define CFG_bool(s, n, default, comment) \
else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) cfg->s.n = \
strcmp(value, "true") == 0;
#define CFG_int(s, n, default, comment) \
else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) { \
cfg->s.n = strtol(value, &end, 10); \
if (end == value || *end != '\0' || errno == ERANGE) cfg->s.n = default; \
}
#define CFG_hex(s, n, precision, default, comment) \
else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) { \
cfg->s.n = strtol(value, &end, 16); \
if (end == value || *end != '\0' || errno == ERANGE) cfg->s.n = 0x##default; \
}
#define CFG_ipv4(s, n, a, b, c, d, comment) \
else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) { \
cfg->s.n = strtol(value, &end, 16); \
unsigned char ip_a, ip_b, ip_c, ip_d; \
if (sscanf_s(value, "%hhu.%hhu.%hhu.%hhu", &ip_a, &ip_b, &ip_c, &ip_d) == 4) \
cfg->s.n = (unsigned int)((ip_a << 24) | (ip_b << 16) | (ip_c << 8) | ip_d); \
else \
cfg->s.n = (unsigned int)(((unsigned char)a << 24) | ((unsigned char)b << 16) | \
((unsigned char)c << 8) | (unsigned char)d); \
}
#include "config.def"
return 1;
}
const unsigned int RES_W[8] = { 640, 640, 1024, 1024, 1280, 1280, 1360, 1920 };
const unsigned int RES_H[8] = { 480, 480, 600, 768, 720, 1024, 768, 1080 };
void load_mice_config() {
if (ini_parse(CONFIG_PATH, handler, &MiceConfig) < 0) {
printf("Loading config defaults\n");
if (MiceConfig.sysconf.pcb_serial) free(MiceConfig.sysconf.pcb_serial);
MiceConfig.sysconf.pcb_serial = malloc(17);
memset(MiceConfig.sysconf.pcb_serial, 0, 17);
if (MiceConfig.sysconf.keychip_serial) free(MiceConfig.sysconf.keychip_serial);
MiceConfig.sysconf.keychip_serial = malloc(17);
memset(MiceConfig.sysconf.keychip_serial, 0, 17);
GetSerialNumbers(MiceConfig.sysconf.pcb_serial, MiceConfig.sysconf.keychip_serial);
save_current_config(true);
}
if (!MiceConfig.sysconf.pcb_serial || strlen(MiceConfig.sysconf.pcb_serial) == 0) {
if (MiceConfig.sysconf.pcb_serial) free(MiceConfig.sysconf.pcb_serial);
MiceConfig.sysconf.pcb_serial = malloc(17);
memset(MiceConfig.sysconf.pcb_serial, 0, 17);
GetSerialNumbers(MiceConfig.sysconf.pcb_serial, NULL);
save_current_config(false);
}
if (!MiceConfig.sysconf.keychip_serial || strlen(MiceConfig.sysconf.keychip_serial) == 0) {
if (MiceConfig.sysconf.keychip_serial) free(MiceConfig.sysconf.keychip_serial);
MiceConfig.sysconf.keychip_serial = malloc(17);
memset(MiceConfig.sysconf.keychip_serial, 0, 17);
GetSerialNumbers(NULL, MiceConfig.sysconf.keychip_serial);
save_current_config(false);
}
if (MiceConfig.window.dipsw) {
MiceConfig.window.w = RES_W[(MiceConfig.sysconf.dipsw >> 4) & 0b111];
MiceConfig.window.h = RES_H[(MiceConfig.sysconf.dipsw >> 4) & 0b111];
} else {
MiceConfig.sysconf.dipsw &= 0b1'000'1111;
if (MiceConfig.window.w == 1920 && MiceConfig.window.h == 1080)
MiceConfig.sysconf.dipsw |= DIPSW_RES_1920x1080;
else if (MiceConfig.window.w == 1360 && MiceConfig.window.h == 768)
MiceConfig.sysconf.dipsw |= DIPSW_RES_1360x768;
else if (MiceConfig.window.w == 1280 && MiceConfig.window.h == 1024)
MiceConfig.sysconf.dipsw |= DIPSW_RES_1280x1024;
else if (MiceConfig.window.w == 1280 && MiceConfig.window.h == 720)
MiceConfig.sysconf.dipsw |= DIPSW_RES_1280x720;
else if (MiceConfig.window.w == 1024 && MiceConfig.window.h == 768)
MiceConfig.sysconf.dipsw |= DIPSW_RES_1024x768;
else if (MiceConfig.window.w == 1024 && MiceConfig.window.h == 600)
MiceConfig.sysconf.dipsw |= DIPSW_RES_1024x600;
else if (MiceConfig.window.w == 640 && MiceConfig.window.h == 480)
MiceConfig.sysconf.dipsw |= DIPSW_RES_640x480;
}
}