Mercurial > wgsdk
comparison src/config.c @ 10:42ac054c0231
*: huge refactoring
dirtools now uses wchar (wayyy overdue)
the timer doesn't have a stupid design anymore
we don't use windows.h at all now
...
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Sun, 11 Feb 2024 19:43:31 -0500 |
| parents | 07f0e2f43204 |
| children | e6a594f16403 |
comparison
equal
deleted
inserted
replaced
| 9:07f0e2f43204 | 10:42ac054c0231 |
|---|---|
| 9 * | 9 * |
| 10 * ----- wgsdk config ---- | 10 * ----- wgsdk config ---- |
| 11 * display_title=1 | 11 * display_title=1 |
| 12 * show_elapsed_time=1 | 12 * show_elapsed_time=1 |
| 13 **/ | 13 **/ |
| 14 #include "dirtools.h" | |
| 15 #include "config.h" | |
| 16 #include "resource.h" | |
| 17 #include "utils.h" | |
| 18 | |
| 19 #include <shlobj.h> | |
| 20 #include <shlwapi.h> | |
| 21 | |
| 14 #include <assert.h> | 22 #include <assert.h> |
| 15 #include <stdio.h> | 23 #include <stdio.h> |
| 16 #include <stdlib.h> | 24 #include <stdlib.h> |
| 17 #include <string.h> | 25 #include <string.h> |
| 18 #include "dirtools.h" | 26 |
| 19 #include "config.h" | 27 #define MAX_LINE_LENGTH 256 |
| 20 #include "resource.h" | |
| 21 #include "utils.h" | |
| 22 #ifndef WIN32_LEAN_AND_MEAN | |
| 23 # define WIN32_LEAN_AND_MEAN | |
| 24 #endif | |
| 25 #include <windows.h> | |
| 26 #include <windowsx.h> | |
| 27 #define MAX_LINE_LENGTH 128 | |
| 28 | 28 |
| 29 /* from main */ | 29 /* from main */ |
| 30 extern void update_rich_presence_details(void); | 30 extern void update_rich_presence_details(void); |
| 31 extern struct config config; | 31 extern struct config config; |
| 32 | 32 |
| 33 /* must be free'd by the caller */ | |
| 34 LPWSTR cfg_get_path() { | |
| 35 /* get location of appdata folder */ | |
| 36 LPWSTR appdata_folder; | |
| 37 SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &appdata_folder); | |
| 38 | |
| 39 /* compatibility with WACUP, falls back to Winamp */ | |
| 40 WCHAR exe_name[MAX_PATH] = {L'\0'}; | |
| 41 if (GetModuleFileNameW(NULL, (LPWSTR)&exe_name, MAX_PATH)) { | |
| 42 PathStripPathW(exe_name); | |
| 43 PathRemoveExtensionW(exe_name); | |
| 44 } else { // fail | |
| 45 memset(exe_name, '\0', MAX_PATH * sizeof(WCHAR)); | |
| 46 wcscpy(exe_name, L"Winamp"); | |
| 47 } | |
| 48 | |
| 49 /* concat until we get the final path */ | |
| 50 LPWSTR path = dirtools_concat_paths(appdata_folder, exe_name); | |
| 51 CoTaskMemFree(appdata_folder); | |
| 52 | |
| 53 LPWSTR final = dirtools_concat_paths(path, L"Plugins\\wgsdk"); | |
| 54 free(path); | |
| 55 | |
| 56 return final; | |
| 57 } | |
| 58 | |
| 33 int cfg_load(struct config* restrict config) { | 59 int cfg_load(struct config* restrict config) { |
| 34 char line[MAX_LINE_LENGTH] = {0}, | 60 LPWSTR fold_path = cfg_get_path(); |
| 35 *path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt"); | 61 LPWSTR path = dirtools_concat_paths(fold_path, L"config.txt"); |
| 36 FILE* config_fp = fopen(path, "r"); | 62 free(fold_path); |
| 63 | |
| 64 /* find some real win32 replacement for this */ | |
| 65 FILE* config_fp = _wfopen(path, L"r"); | |
| 37 if (config_fp == NULL) { | 66 if (config_fp == NULL) { |
| 38 free(path); | 67 free(path); |
| 39 return 1; | 68 return 1; |
| 40 } | 69 } |
| 70 | |
| 41 /* parse the config */ | 71 /* parse the config */ |
| 72 char line[MAX_LINE_LENGTH] = {0}; | |
| 42 while (fgets(line, MAX_LINE_LENGTH, config_fp)) { | 73 while (fgets(line, MAX_LINE_LENGTH, config_fp)) { |
| 74 /* strtok is okay here. */ | |
| 43 switch (crc32b((unsigned char*)strtok(line, "="))) { | 75 switch (crc32b((unsigned char*)strtok(line, "="))) { |
| 44 case 0x2a666380: // display_title | 76 case 0x2a666380: // display_title |
| 45 config->display_title = !!atoi(strtok(NULL, "=")); | 77 config->display_title = !!atoi(strtok(NULL, "=")); |
| 46 break; | 78 break; |
| 47 case 0xbb631f7f: // show_elapsed_time | 79 case 0xbb631f7f: // show_elapsed_time |
| 49 break; | 81 break; |
| 50 default: | 82 default: |
| 51 break; | 83 break; |
| 52 } | 84 } |
| 53 } | 85 } |
| 86 | |
| 54 free(path); | 87 free(path); |
| 55 return 0; | 88 return 0; |
| 56 } | 89 } |
| 57 | 90 |
| 58 int cfg_save(struct config config) { | 91 int cfg_save(struct config config) { |
| 59 char* path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk"); | 92 LPWSTR fold_path = cfg_get_path(); |
| 60 assert(!dirtools_create_directory(path)); | 93 assert(!dirtools_create_directory(fold_path)); |
| 61 free(path); | |
| 62 | 94 |
| 63 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt"); | 95 LPWSTR path = dirtools_concat_paths(fold_path, L"config.txt"); |
| 64 FILE* config_fp = fopen(path, "w"); | 96 free(fold_path); |
| 65 if (config_fp == NULL) | 97 |
| 98 FILE* config_fp = _wfopen(path, L"w"); | |
| 99 if (config_fp == NULL) { | |
| 100 free(path); | |
| 66 return 1; | 101 return 1; |
| 102 } | |
| 103 | |
| 67 fprintf(config_fp, "----- wgsdk config ----\n"); | 104 fprintf(config_fp, "----- wgsdk config ----\n"); |
| 68 fprintf(config_fp, "display_title=%d\n", config.display_title); | 105 fprintf(config_fp, "display_title=%d\n", config.display_title); |
| 69 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time); | 106 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time); |
| 70 fclose(config_fp); | 107 fclose(config_fp); |
| 108 free(path); | |
| 71 return 0; | 109 return 0; |
| 72 } | 110 } |
| 73 | 111 |
| 74 /* --------------------------------- */ | 112 /* --------------------------------- */ |
| 75 | 113 |
| 76 #define conf_item_to_dlg(cons, var) \ | 114 #define conf_item_to_dlg(hwnd, cons, var) \ |
| 77 checkboxHwnd = GetDlgItem(hWnd, cons); \ | 115 { \ |
| 78 Button_SetCheck(checkboxHwnd, var) | 116 HWND checkboxHwnd = GetDlgItem(hwnd, cons); \ |
| 79 #define dlg_item_to_conf(cons, var) \ | 117 SendMessage(checkboxHwnd, BM_SETCHECK, (var) ? BST_CHECKED : BST_UNCHECKED, 0); \ |
| 80 checkboxHwnd = GetDlgItem(hWnd, cons); \ | 118 } |
| 81 var = Button_GetCheck(checkboxHwnd) | 119 #define dlg_item_to_conf(hwnd, cons, var) \ |
| 120 { \ | |
| 121 HWND checkboxHwnd = GetDlgItem(hwnd, cons); \ | |
| 122 (var) = (SendMessage(checkboxHwnd, BM_GETCHECK, 0, 0) == BST_CHECKED); \ | |
| 123 } | |
| 82 void cfg_get_controls(HWND hWnd) { | 124 void cfg_get_controls(HWND hWnd) { |
| 83 HWND checkboxHwnd; | 125 dlg_item_to_conf(hWnd, TITLE_CHECK, config.display_title); |
| 84 | 126 dlg_item_to_conf(hWnd, ELAPSED_TIME_CHECK, config.show_elapsed_time); |
| 85 dlg_item_to_conf(TITLE_CHECK, config.display_title); | |
| 86 dlg_item_to_conf(ELAPSED_TIME_CHECK, config.show_elapsed_time); | |
| 87 } | 127 } |
| 88 | 128 |
| 89 void cfg_set_controls(HWND hWnd) { | 129 void cfg_set_controls(HWND hWnd) { |
| 90 HWND checkboxHwnd; | 130 conf_item_to_dlg(hWnd, TITLE_CHECK, config.display_title); |
| 91 | 131 conf_item_to_dlg(hWnd, ELAPSED_TIME_CHECK, config.show_elapsed_time); |
| 92 conf_item_to_dlg(TITLE_CHECK, config.display_title); | |
| 93 conf_item_to_dlg(ELAPSED_TIME_CHECK, config.show_elapsed_time); | |
| 94 } | 132 } |
| 95 #undef conf_item_to_dlg | 133 #undef conf_item_to_dlg |
| 96 #undef dlg_item_to_conf | 134 #undef dlg_item_to_conf |
| 97 | 135 |
| 98 void cfg_on_confirm_settings_dialog(HWND hWnd) { | 136 void cfg_on_confirm_settings_dialog(HWND hWnd) { |
