annotate src/config.c @ 9:07f0e2f43204

*: add the restrict keyword when necessary also fixed some typos in the README
author Paper
date Fri, 16 Dec 2022 21:55:37 -0500
parents be4835547dd0
children 42ac054c0231
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 **/
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 #include <assert.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 #include <stdio.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 #include <stdlib.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 #include <string.h>
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 #include "dirtools.h"
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 #include "config.h"
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
20 #include "resource.h"
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
21 #include "utils.h"
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 #ifndef WIN32_LEAN_AND_MEAN
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 # define WIN32_LEAN_AND_MEAN
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 #endif
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 #include <windows.h>
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
26 #include <windowsx.h>
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 #define MAX_LINE_LENGTH 128
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
9
07f0e2f43204 *: add the restrict keyword when necessary
Paper
parents: 7
diff changeset
33 int cfg_load(struct config* restrict config) {
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
34 char line[MAX_LINE_LENGTH] = {0},
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
35 *path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
36 FILE* config_fp = fopen(path, "r");
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 if (config_fp == NULL) {
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 free(path);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 return 1;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 }
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
41 /* parse the config */
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 while (fgets(line, MAX_LINE_LENGTH, config_fp)) {
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 switch (crc32b((unsigned char*)strtok(line, "="))) {
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 case 0x2a666380: // display_title
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
45 config->display_title = !!atoi(strtok(NULL, "="));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 break;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 case 0xbb631f7f: // show_elapsed_time
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
48 config->show_elapsed_time = !!atoi(strtok(NULL, "="));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 break;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 default:
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 break;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 free(path);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 return 0;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 }
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57
4
59bf702b2b21 *: stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
58 int cfg_save(struct config config) {
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
59 char* path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk");
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
60 assert(!dirtools_create_directory(path));
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 free(path);
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
62
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
64 FILE* config_fp = fopen(path, "w");
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
65 if (config_fp == NULL)
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 return 1;
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
67 fprintf(config_fp, "----- wgsdk config ----\n");
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
68 fprintf(config_fp, "display_title=%d\n", config.display_title);
0
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 fclose(config_fp);
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 return 0;
d91dfd53b8b4 Initial commit
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 }
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
73
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
74 /* --------------------------------- */
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
75
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
76 #define conf_item_to_dlg(cons, var) \
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
77 checkboxHwnd = GetDlgItem(hWnd, cons); \
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
78 Button_SetCheck(checkboxHwnd, var)
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
79 #define dlg_item_to_conf(cons, var) \
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
80 checkboxHwnd = GetDlgItem(hWnd, cons); \
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
81 var = Button_GetCheck(checkboxHwnd)
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
82 void cfg_get_controls(HWND hWnd) {
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
83 HWND checkboxHwnd;
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
84
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
85 dlg_item_to_conf(TITLE_CHECK, config.display_title);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
86 dlg_item_to_conf(ELAPSED_TIME_CHECK, config.show_elapsed_time);
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
87 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
88
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
89 void cfg_set_controls(HWND hWnd) {
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
90 HWND checkboxHwnd;
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
91
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
92 conf_item_to_dlg(TITLE_CHECK, config.display_title);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
93 conf_item_to_dlg(ELAPSED_TIME_CHECK, config.show_elapsed_time);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
94 }
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
95 #undef conf_item_to_dlg
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
96 #undef dlg_item_to_conf
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
97
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
98 void cfg_on_confirm_settings_dialog(HWND hWnd) {
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
99 cfg_get_controls(hWnd);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
100 cfg_save(config);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
101 update_rich_presence_details();
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
102 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
103
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
104 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
105 switch (msg) {
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
106 case WM_INITDIALOG:
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
107 /* 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
108 if (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
109 return FALSE;
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
110 cfg_set_controls(hWnd);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
111 return TRUE;
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
112 case WM_COMMAND:
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
113 switch (LOWORD(wParam)) {
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
114 case IDOK:
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
115 cfg_on_confirm_settings_dialog(hWnd);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
116 EndDialog(hWnd, 0);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
117 return TRUE;
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
118 case IDCANCEL:
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
119 EndDialog(hWnd, 0);
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
120 return TRUE;
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
121 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
122 }
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
123
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents: 4
diff changeset
124 return FALSE;
1
7abb5d8b20ea Initial commit: part 2
Paper <mrpapersonic@gmail.com>
parents: 0
diff changeset
125 }