annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
1 /**
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
2 * config.c:
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
3 *
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
4 * Functions to load/save/edit the config.
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
5 **/
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
6
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
7 /**
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
8 * example config:
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
9 *
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
10 * ----- wgsdk config ----
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
11 * display_title=1
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
12 * show_elapsed_time=1
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
13 **/
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
14 #include "dirtools.h"
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
15 #include "config.h"
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
16 #include "resource.h"
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
17 #include "utils.h"
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
18
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
19 #include <shlobj.h>
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
20 #include <shlwapi.h>
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
21
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 #include <assert.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 #include <stdio.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 #include <stdlib.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 #include <string.h>
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
26
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
27 #define MAX_LINE_LENGTH 256
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
29 /* from main */
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
30 extern void update_rich_presence_details(void);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
31 extern struct config config;
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
33 /* must be free'd by the caller */
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
34 LPWSTR cfg_get_path() {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
35 /* get location of appdata folder */
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
36 LPWSTR appdata_folder;
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
37 SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &appdata_folder);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
38
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
39 /* compatibility with WACUP, falls back to Winamp */
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
40 WCHAR exe_name[MAX_PATH] = {L'\0'};
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
41 if (GetModuleFileNameW(NULL, (LPWSTR)&exe_name, MAX_PATH)) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
42 PathStripPathW(exe_name);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
43 PathRemoveExtensionW(exe_name);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
44 } else { // fail
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
45 memset(exe_name, '\0', MAX_PATH * sizeof(WCHAR));
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
46 wcscpy(exe_name, L"Winamp");
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
47 }
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
48
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
49 /* concat until we get the final path */
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
50 LPWSTR path = dirtools_concat_paths(appdata_folder, exe_name);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
51 CoTaskMemFree(appdata_folder);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
52
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
53 LPWSTR final = dirtools_concat_paths(path, L"Plugins\\wgsdk");
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
54 free(path);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
55
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
56 return final;
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
57 }
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
58
9
07f0e2f43204 *: add the restrict keyword when necessary
Paper
parents: 7
diff changeset
59 int cfg_load(struct config* restrict config) {
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
60 LPWSTR fold_path = cfg_get_path();
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
61 LPWSTR path = dirtools_concat_paths(fold_path, L"config.txt");
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
62 free(fold_path);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
63
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
64 /* find some real win32 replacement for this */
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
65 FILE* config_fp = _wfopen(path, L"r");
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 if (config_fp == NULL) {
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 free(path);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 return 1;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 }
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
70
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
71 /* parse the config */
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
72 char line[MAX_LINE_LENGTH] = {0};
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 while (fgets(line, MAX_LINE_LENGTH, config_fp)) {
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
74 /* strtok is okay here. */
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 switch (crc32b((unsigned char*)strtok(line, "="))) {
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 case 0x2a666380: // display_title
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
77 config->display_title = !!atoi(strtok(NULL, "="));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 break;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 case 0xbb631f7f: // show_elapsed_time
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
80 config->show_elapsed_time = !!atoi(strtok(NULL, "="));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 break;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 default:
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 break;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 }
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
86
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 free(path);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 return 0;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90
4
59bf702b2b21 *: stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
91 int cfg_save(struct config config) {
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
92 LPWSTR fold_path = cfg_get_path();
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
93 assert(!dirtools_create_directory(fold_path));
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
94
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
95 LPWSTR path = dirtools_concat_paths(fold_path, L"config.txt");
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
96 free(fold_path);
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
97
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
98 FILE* config_fp = _wfopen(path, L"w");
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
99 if (config_fp == NULL) {
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
100 free(path);
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101 return 1;
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
102 }
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
103
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
104 fprintf(config_fp, "----- wgsdk config ----\n");
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
105 fprintf(config_fp, "display_title=%d\n", config.display_title);
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107 fclose(config_fp);
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
108 free(path);
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109 return 0;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
110 }
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
111
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
112 /* --------------------------------- */
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
113
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
114 #define conf_item_to_dlg(hwnd, cons, var) \
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
115 { \
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
116 HWND checkboxHwnd = GetDlgItem(hwnd, cons); \
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
117 SendMessage(checkboxHwnd, BM_SETCHECK, (var) ? BST_CHECKED : BST_UNCHECKED, 0); \
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
118 }
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
119 #define dlg_item_to_conf(hwnd, cons, var) \
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
120 { \
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
121 HWND checkboxHwnd = GetDlgItem(hwnd, cons); \
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
122 (var) = (SendMessage(checkboxHwnd, BM_GETCHECK, 0, 0) == BST_CHECKED); \
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
123 }
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
124 void cfg_get_controls(HWND hWnd) {
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
125 dlg_item_to_conf(hWnd, TITLE_CHECK, config.display_title);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
126 dlg_item_to_conf(hWnd, ELAPSED_TIME_CHECK, config.show_elapsed_time);
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
127 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
128
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
129 void cfg_set_controls(HWND hWnd) {
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
130 conf_item_to_dlg(hWnd, TITLE_CHECK, config.display_title);
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
131 conf_item_to_dlg(hWnd, ELAPSED_TIME_CHECK, config.show_elapsed_time);
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
132 }
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
133 #undef conf_item_to_dlg
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
134 #undef dlg_item_to_conf
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
135
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
136 void cfg_on_confirm_settings_dialog(HWND hWnd) {
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
137 cfg_get_controls(hWnd);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
138 cfg_save(config);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
139 update_rich_presence_details();
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
140 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
141
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
142 BOOL CALLBACK cfg_win_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
143 switch (msg) {
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
144 case WM_INITDIALOG:
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
145 /* do nothing if this is a child window (tab page) callback, pass to the parent */
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
146 if (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
147 return FALSE;
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
148 cfg_set_controls(hWnd);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
149 return TRUE;
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
150 case WM_COMMAND:
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
151 switch (LOWORD(wParam)) {
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
152 case IDOK:
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
153 cfg_on_confirm_settings_dialog(hWnd);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
154 EndDialog(hWnd, 0);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
155 return TRUE;
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
156 case IDCANCEL:
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
157 EndDialog(hWnd, 0);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
158 return TRUE;
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
159 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
160 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
161
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
162 return FALSE;
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
163 }