0
|
1 #include <assert.h>
|
|
2 #include <stdio.h>
|
|
3 #include <stdlib.h>
|
|
4 #include <string.h>
|
|
5 #include "dirtools.h"
|
|
6 #include "config.h"
|
1
|
7 #include "main.h"
|
|
8 #include "resource.h"
|
0
|
9 #ifndef WIN32_LEAN_AND_MEAN
|
|
10 # define WIN32_LEAN_AND_MEAN
|
|
11 #endif
|
|
12 #include <windows.h>
|
1
|
13 #include <windowsx.h>
|
0
|
14 #define MAX_LINE_LENGTH 128
|
|
15
|
1
|
16 extern struct config_t config; // from main
|
|
17
|
0
|
18 static unsigned int crc32b(unsigned char *message) {
|
|
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
|
|
36 int cfg_load(struct config_t* config) {
|
1
|
37 char line[MAX_LINE_LENGTH] = {0},
|
|
38 *path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
|
0
|
39 FILE* config_fp;
|
|
40 config_fp = fopen(path, "r");
|
|
41 if (config_fp == NULL) {
|
|
42 free(path);
|
|
43 return 1;
|
|
44 }
|
|
45 while (fgets(line, MAX_LINE_LENGTH, config_fp)) {
|
|
46 switch (crc32b((unsigned char*)strtok(line, "="))) {
|
|
47 case 0x2a666380: // display_title
|
1
|
48 config->display_title = !!atoi(strtok(NULL, "="));
|
0
|
49 break;
|
|
50 case 0xbb631f7f: // show_elapsed_time
|
1
|
51 config->show_elapsed_time = !!atoi(strtok(NULL, "="));
|
0
|
52 break;
|
|
53 default:
|
|
54 break;
|
|
55 }
|
|
56 }
|
|
57 free(path);
|
|
58 return 0;
|
|
59 }
|
|
60
|
|
61 int cfg_save(struct config_t config) {
|
1
|
62 char* path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk");
|
0
|
63 FILE* config_fp;
|
1
|
64 assert(!dirtools_create_directory(path));
|
0
|
65 free(path);
|
|
66 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
|
|
67 config_fp = fopen(path, "w");
|
|
68 if (config_fp == NULL) {
|
|
69 return 1;
|
|
70 }
|
1
|
71 fprintf(config_fp, "----- wgsdk config ----\n");
|
0
|
72 fprintf(config_fp, "display_title=%d\n", config.display_title);
|
|
73 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time);
|
|
74 fclose(config_fp);
|
|
75 return 0;
|
|
76 }
|
1
|
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 }
|