7
|
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 **/
|
0
|
14 #include <assert.h>
|
|
15 #include <stdio.h>
|
|
16 #include <stdlib.h>
|
|
17 #include <string.h>
|
|
18 #include "dirtools.h"
|
|
19 #include "config.h"
|
1
|
20 #include "resource.h"
|
7
|
21 #include "utils.h"
|
0
|
22 #ifndef WIN32_LEAN_AND_MEAN
|
|
23 # define WIN32_LEAN_AND_MEAN
|
|
24 #endif
|
|
25 #include <windows.h>
|
1
|
26 #include <windowsx.h>
|
0
|
27 #define MAX_LINE_LENGTH 128
|
|
28
|
7
|
29 /* from main */
|
|
30 extern void update_rich_presence_details(void);
|
|
31 extern struct config config;
|
0
|
32
|
9
|
33 int cfg_load(struct config* restrict config) {
|
7
|
34 char line[MAX_LINE_LENGTH] = {0},
|
1
|
35 *path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
|
7
|
36 FILE* config_fp = fopen(path, "r");
|
0
|
37 if (config_fp == NULL) {
|
|
38 free(path);
|
|
39 return 1;
|
|
40 }
|
7
|
41 /* parse the config */
|
0
|
42 while (fgets(line, MAX_LINE_LENGTH, config_fp)) {
|
|
43 switch (crc32b((unsigned char*)strtok(line, "="))) {
|
|
44 case 0x2a666380: // display_title
|
1
|
45 config->display_title = !!atoi(strtok(NULL, "="));
|
0
|
46 break;
|
|
47 case 0xbb631f7f: // show_elapsed_time
|
1
|
48 config->show_elapsed_time = !!atoi(strtok(NULL, "="));
|
0
|
49 break;
|
|
50 default:
|
|
51 break;
|
|
52 }
|
|
53 }
|
|
54 free(path);
|
|
55 return 0;
|
|
56 }
|
|
57
|
4
|
58 int cfg_save(struct config config) {
|
1
|
59 char* path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk");
|
|
60 assert(!dirtools_create_directory(path));
|
0
|
61 free(path);
|
7
|
62
|
0
|
63 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
|
7
|
64 FILE* config_fp = fopen(path, "w");
|
|
65 if (config_fp == NULL)
|
0
|
66 return 1;
|
1
|
67 fprintf(config_fp, "----- wgsdk config ----\n");
|
7
|
68 fprintf(config_fp, "display_title=%d\n", config.display_title);
|
0
|
69 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time);
|
|
70 fclose(config_fp);
|
|
71 return 0;
|
|
72 }
|
1
|
73
|
|
74 /* --------------------------------- */
|
|
75
|
7
|
76 #define conf_item_to_dlg(cons, var) \
|
|
77 checkboxHwnd = GetDlgItem(hWnd, cons); \
|
|
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;
|
1
|
84
|
7
|
85 dlg_item_to_conf(TITLE_CHECK, config.display_title);
|
|
86 dlg_item_to_conf(ELAPSED_TIME_CHECK, config.show_elapsed_time);
|
1
|
87 }
|
|
88
|
|
89 void cfg_set_controls(HWND hWnd) {
|
7
|
90 HWND checkboxHwnd;
|
1
|
91
|
7
|
92 conf_item_to_dlg(TITLE_CHECK, config.display_title);
|
|
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();
|
1
|
102 }
|
|
103
|
|
104 BOOL CALLBACK cfg_win_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
105 switch (msg) {
|
7
|
106 case WM_INITDIALOG:
|
|
107 /* do nothing if this is a child window (tab page) callback, pass to the parent */
|
1
|
108 if (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)
|
|
109 return FALSE;
|
|
110 cfg_set_controls(hWnd);
|
|
111 return TRUE;
|
|
112 case WM_COMMAND:
|
|
113 switch (LOWORD(wParam)) {
|
7
|
114 case IDOK:
|
1
|
115 cfg_on_confirm_settings_dialog(hWnd);
|
|
116 EndDialog(hWnd, 0);
|
|
117 return TRUE;
|
7
|
118 case IDCANCEL:
|
1
|
119 EndDialog(hWnd, 0);
|
|
120 return TRUE;
|
|
121 }
|
|
122 }
|
|
123
|
7
|
124 return FALSE;
|
1
|
125 }
|