comparison src/config.c @ 1:7abb5d8b20ea v1.0

Initial commit: part 2 I added a config GUI, and actual stuff to the README. The GUI was 'made' in Visual Studio, but I trimmed down the resource file because it had weird VS2010 skid marks all over it.
author Paper <mrpapersonic@gmail.com>
date Sun, 07 Aug 2022 10:23:10 -0400
parents d91dfd53b8b4
children 59bf702b2b21
comparison
equal deleted inserted replaced
0:d91dfd53b8b4 1:7abb5d8b20ea
2 #include <stdio.h> 2 #include <stdio.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <string.h> 4 #include <string.h>
5 #include "dirtools.h" 5 #include "dirtools.h"
6 #include "config.h" 6 #include "config.h"
7 #include "main.h"
8 #include "resource.h"
7 #ifndef WIN32_LEAN_AND_MEAN 9 #ifndef WIN32_LEAN_AND_MEAN
8 # define WIN32_LEAN_AND_MEAN 10 # define WIN32_LEAN_AND_MEAN
9 #endif 11 #endif
10 #include <windows.h> 12 #include <windows.h>
13 #include <windowsx.h>
11 #define MAX_LINE_LENGTH 128 14 #define MAX_LINE_LENGTH 128
15
16 extern struct config_t config; // from main
12 17
13 static unsigned int crc32b(unsigned char *message) { 18 static unsigned int crc32b(unsigned char *message) {
14 int i, j; 19 int i, j;
15 unsigned int byte, crc, mask; 20 unsigned int byte, crc, mask;
16 21
27 } 32 }
28 return ~crc; 33 return ~crc;
29 } 34 }
30 35
31 int cfg_load(struct config_t* config) { 36 int cfg_load(struct config_t* config) {
32 char* path = NULL; 37 char line[MAX_LINE_LENGTH] = {0},
33 char line[MAX_LINE_LENGTH] = {0}; 38 *path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
34 FILE* config_fp; 39 FILE* config_fp;
35 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
36 config_fp = fopen(path, "r"); 40 config_fp = fopen(path, "r");
37 if (config_fp == NULL) { 41 if (config_fp == NULL) {
38 free(path); 42 free(path);
39 return 1; 43 return 1;
40 } 44 }
41 while (fgets(line, MAX_LINE_LENGTH, config_fp)) { 45 while (fgets(line, MAX_LINE_LENGTH, config_fp)) {
42 switch (crc32b((unsigned char*)strtok(line, "="))) { 46 switch (crc32b((unsigned char*)strtok(line, "="))) {
43 case 0x2a666380: // display_title 47 case 0x2a666380: // display_title
44 config->display_title = atoi(strtok(NULL, "=")); 48 config->display_title = !!atoi(strtok(NULL, "="));
45 break; 49 break;
46 case 0xbb631f7f: // show_elapsed_time 50 case 0xbb631f7f: // show_elapsed_time
47 config->show_elapsed_time = atoi(strtok(NULL, "=")); 51 config->show_elapsed_time = !!atoi(strtok(NULL, "="));
48 break; 52 break;
49 default: 53 default:
50 break; 54 break;
51 } 55 }
52 } 56 }
53 free(path); 57 free(path);
54 return 0; 58 return 0;
55 } 59 }
56 60
57 int cfg_save(struct config_t config) { 61 int cfg_save(struct config_t config) {
58 char* path = NULL; 62 char* path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk");
59 FILE* config_fp; 63 FILE* config_fp;
60 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk"); 64 assert(!dirtools_create_directory(path));
61 assert(dirtools_create_directory(path));
62 free(path); 65 free(path);
63 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt"); 66 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
64 config_fp = fopen(path, "w"); 67 config_fp = fopen(path, "w");
65 if (config_fp == NULL) { 68 if (config_fp == NULL) {
66 return 1; 69 return 1;
67 } 70 }
71 fprintf(config_fp, "----- wgsdk config ----\n");
68 fprintf(config_fp, "display_title=%d\n", config.display_title); 72 fprintf(config_fp, "display_title=%d\n", config.display_title);
69 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time); 73 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time);
70 fclose(config_fp); 74 fclose(config_fp);
71 return 0; 75 return 0;
72 } 76 }
77
78 /* --------------------------------- */
79
80 void cfg_dialog_to_struct(HWND hWnd) {
81 HWND checkboxHwnd = GetDlgItem(hWnd, TITLE_CHECK);
82 config.display_title = Button_GetCheck(checkboxHwnd);
83
84 checkboxHwnd = GetDlgItem(hWnd, ELAPSED_TIME_CHECK);
85 config.show_elapsed_time = Button_GetCheck(checkboxHwnd);
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 }
94
95 void cfg_set_controls(HWND hWnd) {
96 HWND checkboxHwnd = GetDlgItem(hWnd, TITLE_CHECK);
97 Button_SetCheck(checkboxHwnd, config.display_title);
98
99 checkboxHwnd = GetDlgItem(hWnd, ELAPSED_TIME_CHECK);
100 Button_SetCheck(checkboxHwnd, config.show_elapsed_time);
101 }
102
103 BOOL CALLBACK cfg_win_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
104 switch (msg) {
105 case WM_INITDIALOG: {
106 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;
110 }
111
112 cfg_set_controls(hWnd);
113 return TRUE;
114 }
115
116 case WM_COMMAND:
117 switch (LOWORD(wParam)) {
118 case IDOK: {
119 cfg_on_confirm_settings_dialog(hWnd);
120 EndDialog(hWnd, 0);
121 return TRUE;
122 break;
123 }
124 case IDCANCEL: {
125 EndDialog(hWnd, 0);
126 return TRUE;
127 break;
128 }
129 }
130 }
131
132 return FALSE;
133 }