Mercurial > wgsdk
diff src/config.c @ 7:be4835547dd0
clean up code, convert git files to hg, etc.
author | Paper |
---|---|
date | Fri, 16 Dec 2022 20:35:06 -0500 |
parents | 59bf702b2b21 |
children | 07f0e2f43204 |
line wrap: on
line diff
--- a/src/config.c Sat Dec 17 01:48:13 2022 +0000 +++ b/src/config.c Fri Dec 16 20:35:06 2022 -0500 @@ -1,11 +1,24 @@ +/** + * config.c: + * + * Functions to load/save/edit the config. +**/ + +/** + * example config: + * + * ----- wgsdk config ---- + * display_title=1 + * show_elapsed_time=1 +**/ #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "dirtools.h" #include "config.h" -#include "main.h" #include "resource.h" +#include "utils.h" #ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN #endif @@ -13,35 +26,19 @@ #include <windowsx.h> #define MAX_LINE_LENGTH 128 -extern struct config config; // from main - -static unsigned int crc32b(unsigned char *message) { - int i, j; - unsigned int byte, crc, mask; - - i = 0; - crc = 0xFFFFFFFF; - while (message[i] != 0) { - byte = message[i]; // Get next byte. - crc = crc ^ byte; - for (j = 7; j >= 0; j--) { // Do eight times. - mask = -(crc & 1); - crc = (crc >> 1) ^ (0xEDB88320 & mask); - } - i = i + 1; - } - return ~crc; -} +/* from main */ +extern void update_rich_presence_details(void); +extern struct config config; int cfg_load(struct config* config) { - char line[MAX_LINE_LENGTH] = {0}, + char line[MAX_LINE_LENGTH] = {0}, *path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt"); - FILE* config_fp; - config_fp = fopen(path, "r"); + FILE* config_fp = fopen(path, "r"); if (config_fp == NULL) { free(path); return 1; } + /* parse the config */ while (fgets(line, MAX_LINE_LENGTH, config_fp)) { switch (crc32b((unsigned char*)strtok(line, "="))) { case 0x2a666380: // display_title @@ -60,16 +57,15 @@ int cfg_save(struct config config) { char* path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk"); - FILE* config_fp; assert(!dirtools_create_directory(path)); free(path); + path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt"); - config_fp = fopen(path, "w"); - if (config_fp == NULL) { + FILE* config_fp = fopen(path, "w"); + if (config_fp == NULL) return 1; - } fprintf(config_fp, "----- wgsdk config ----\n"); - fprintf(config_fp, "display_title=%d\n", config.display_title); + fprintf(config_fp, "display_title=%d\n", config.display_title); fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time); fclose(config_fp); return 0; @@ -77,57 +73,53 @@ /* --------------------------------- */ -void cfg_dialog_to_struct(HWND hWnd) { - HWND checkboxHwnd = GetDlgItem(hWnd, TITLE_CHECK); - config.display_title = Button_GetCheck(checkboxHwnd); +#define conf_item_to_dlg(cons, var) \ + checkboxHwnd = GetDlgItem(hWnd, cons); \ + Button_SetCheck(checkboxHwnd, var) +#define dlg_item_to_conf(cons, var) \ + checkboxHwnd = GetDlgItem(hWnd, cons); \ + var = Button_GetCheck(checkboxHwnd) +void cfg_get_controls(HWND hWnd) { + HWND checkboxHwnd; - checkboxHwnd = GetDlgItem(hWnd, ELAPSED_TIME_CHECK); - config.show_elapsed_time = Button_GetCheck(checkboxHwnd); -} - -void cfg_on_confirm_settings_dialog(HWND hWnd) -{ - cfg_dialog_to_struct(hWnd); - cfg_save(config); - update_rich_presence_details(); + dlg_item_to_conf(TITLE_CHECK, config.display_title); + dlg_item_to_conf(ELAPSED_TIME_CHECK, config.show_elapsed_time); } void cfg_set_controls(HWND hWnd) { - HWND checkboxHwnd = GetDlgItem(hWnd, TITLE_CHECK); - Button_SetCheck(checkboxHwnd, config.display_title); + HWND checkboxHwnd; - checkboxHwnd = GetDlgItem(hWnd, ELAPSED_TIME_CHECK); - Button_SetCheck(checkboxHwnd, config.show_elapsed_time); + conf_item_to_dlg(TITLE_CHECK, config.display_title); + conf_item_to_dlg(ELAPSED_TIME_CHECK, config.show_elapsed_time); +} +#undef conf_item_to_dlg +#undef dlg_item_to_conf + +void cfg_on_confirm_settings_dialog(HWND hWnd) { + cfg_get_controls(hWnd); + cfg_save(config); + update_rich_presence_details(); } BOOL CALLBACK cfg_win_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { - case WM_INITDIALOG: { + case WM_INITDIALOG: + /* do nothing if this is a child window (tab page) callback, pass to the parent */ if (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD) - { - // do nothing if this is a child window (tab page) callback, pass to the parent return FALSE; - } - cfg_set_controls(hWnd); return TRUE; - } - case WM_COMMAND: switch (LOWORD(wParam)) { - case IDOK: { + case IDOK: cfg_on_confirm_settings_dialog(hWnd); EndDialog(hWnd, 0); return TRUE; - break; - } - case IDCANCEL: { + case IDCANCEL: EndDialog(hWnd, 0); return TRUE; - break; - } } } - return FALSE; + return FALSE; }