comparison 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
comparison
equal deleted inserted replaced
6:8ce85aee15c0 7:be4835547dd0
1 /**
2 * config.c:
3 *
4 * Functions to load/save/edit the config.
5 **/
6
7 /**
8 * example config:
9 *
10 * ----- wgsdk config ----
11 * display_title=1
12 * show_elapsed_time=1
13 **/
1 #include <assert.h> 14 #include <assert.h>
2 #include <stdio.h> 15 #include <stdio.h>
3 #include <stdlib.h> 16 #include <stdlib.h>
4 #include <string.h> 17 #include <string.h>
5 #include "dirtools.h" 18 #include "dirtools.h"
6 #include "config.h" 19 #include "config.h"
7 #include "main.h"
8 #include "resource.h" 20 #include "resource.h"
21 #include "utils.h"
9 #ifndef WIN32_LEAN_AND_MEAN 22 #ifndef WIN32_LEAN_AND_MEAN
10 # define WIN32_LEAN_AND_MEAN 23 # define WIN32_LEAN_AND_MEAN
11 #endif 24 #endif
12 #include <windows.h> 25 #include <windows.h>
13 #include <windowsx.h> 26 #include <windowsx.h>
14 #define MAX_LINE_LENGTH 128 27 #define MAX_LINE_LENGTH 128
15 28
16 extern struct config config; // from main 29 /* from main */
17 30 extern void update_rich_presence_details(void);
18 static unsigned int crc32b(unsigned char *message) { 31 extern struct config config;
19 int i, j;
20 unsigned int byte, crc, mask;
21
22 i = 0;
23 crc = 0xFFFFFFFF;
24 while (message[i] != 0) {
25 byte = message[i]; // Get next byte.
26 crc = crc ^ byte;
27 for (j = 7; j >= 0; j--) { // Do eight times.
28 mask = -(crc & 1);
29 crc = (crc >> 1) ^ (0xEDB88320 & mask);
30 }
31 i = i + 1;
32 }
33 return ~crc;
34 }
35 32
36 int cfg_load(struct config* config) { 33 int cfg_load(struct config* config) {
37 char line[MAX_LINE_LENGTH] = {0}, 34 char line[MAX_LINE_LENGTH] = {0},
38 *path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt"); 35 *path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
39 FILE* config_fp; 36 FILE* config_fp = fopen(path, "r");
40 config_fp = fopen(path, "r");
41 if (config_fp == NULL) { 37 if (config_fp == NULL) {
42 free(path); 38 free(path);
43 return 1; 39 return 1;
44 } 40 }
41 /* parse the config */
45 while (fgets(line, MAX_LINE_LENGTH, config_fp)) { 42 while (fgets(line, MAX_LINE_LENGTH, config_fp)) {
46 switch (crc32b((unsigned char*)strtok(line, "="))) { 43 switch (crc32b((unsigned char*)strtok(line, "="))) {
47 case 0x2a666380: // display_title 44 case 0x2a666380: // display_title
48 config->display_title = !!atoi(strtok(NULL, "=")); 45 config->display_title = !!atoi(strtok(NULL, "="));
49 break; 46 break;
58 return 0; 55 return 0;
59 } 56 }
60 57
61 int cfg_save(struct config config) { 58 int cfg_save(struct config config) {
62 char* path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk"); 59 char* path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk");
63 FILE* config_fp;
64 assert(!dirtools_create_directory(path)); 60 assert(!dirtools_create_directory(path));
65 free(path); 61 free(path);
62
66 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt"); 63 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
67 config_fp = fopen(path, "w"); 64 FILE* config_fp = fopen(path, "w");
68 if (config_fp == NULL) { 65 if (config_fp == NULL)
69 return 1; 66 return 1;
70 }
71 fprintf(config_fp, "----- wgsdk config ----\n"); 67 fprintf(config_fp, "----- wgsdk config ----\n");
72 fprintf(config_fp, "display_title=%d\n", config.display_title); 68 fprintf(config_fp, "display_title=%d\n", config.display_title);
73 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time); 69 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time);
74 fclose(config_fp); 70 fclose(config_fp);
75 return 0; 71 return 0;
76 } 72 }
77 73
78 /* --------------------------------- */ 74 /* --------------------------------- */
79 75
80 void cfg_dialog_to_struct(HWND hWnd) { 76 #define conf_item_to_dlg(cons, var) \
81 HWND checkboxHwnd = GetDlgItem(hWnd, TITLE_CHECK); 77 checkboxHwnd = GetDlgItem(hWnd, cons); \
82 config.display_title = Button_GetCheck(checkboxHwnd); 78 Button_SetCheck(checkboxHwnd, var)
79 #define dlg_item_to_conf(cons, var) \
80 checkboxHwnd = GetDlgItem(hWnd, cons); \
81 var = Button_GetCheck(checkboxHwnd)
82 void cfg_get_controls(HWND hWnd) {
83 HWND checkboxHwnd;
83 84
84 checkboxHwnd = GetDlgItem(hWnd, ELAPSED_TIME_CHECK); 85 dlg_item_to_conf(TITLE_CHECK, config.display_title);
85 config.show_elapsed_time = Button_GetCheck(checkboxHwnd); 86 dlg_item_to_conf(ELAPSED_TIME_CHECK, config.show_elapsed_time);
86 }
87
88 void cfg_on_confirm_settings_dialog(HWND hWnd)
89 {
90 cfg_dialog_to_struct(hWnd);
91 cfg_save(config);
92 update_rich_presence_details();
93 } 87 }
94 88
95 void cfg_set_controls(HWND hWnd) { 89 void cfg_set_controls(HWND hWnd) {
96 HWND checkboxHwnd = GetDlgItem(hWnd, TITLE_CHECK); 90 HWND checkboxHwnd;
97 Button_SetCheck(checkboxHwnd, config.display_title);
98 91
99 checkboxHwnd = GetDlgItem(hWnd, ELAPSED_TIME_CHECK); 92 conf_item_to_dlg(TITLE_CHECK, config.display_title);
100 Button_SetCheck(checkboxHwnd, config.show_elapsed_time); 93 conf_item_to_dlg(ELAPSED_TIME_CHECK, config.show_elapsed_time);
94 }
95 #undef conf_item_to_dlg
96 #undef dlg_item_to_conf
97
98 void cfg_on_confirm_settings_dialog(HWND hWnd) {
99 cfg_get_controls(hWnd);
100 cfg_save(config);
101 update_rich_presence_details();
101 } 102 }
102 103
103 BOOL CALLBACK cfg_win_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { 104 BOOL CALLBACK cfg_win_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
104 switch (msg) { 105 switch (msg) {
105 case WM_INITDIALOG: { 106 case WM_INITDIALOG:
107 /* do nothing if this is a child window (tab page) callback, pass to the parent */
106 if (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD) 108 if (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)
107 {
108 // do nothing if this is a child window (tab page) callback, pass to the parent
109 return FALSE; 109 return FALSE;
110 }
111
112 cfg_set_controls(hWnd); 110 cfg_set_controls(hWnd);
113 return TRUE; 111 return TRUE;
114 }
115
116 case WM_COMMAND: 112 case WM_COMMAND:
117 switch (LOWORD(wParam)) { 113 switch (LOWORD(wParam)) {
118 case IDOK: { 114 case IDOK:
119 cfg_on_confirm_settings_dialog(hWnd); 115 cfg_on_confirm_settings_dialog(hWnd);
120 EndDialog(hWnd, 0); 116 EndDialog(hWnd, 0);
121 return TRUE; 117 return TRUE;
122 break; 118 case IDCANCEL:
123 }
124 case IDCANCEL: {
125 EndDialog(hWnd, 0); 119 EndDialog(hWnd, 0);
126 return TRUE; 120 return TRUE;
127 break;
128 }
129 } 121 }
130 } 122 }
131 123
132 return FALSE; 124 return FALSE;
133 } 125 }