1#include <cstdlib>
2#include <SimpleIni.h>
3#include "config.hpp"
4#include "gui.hpp"
5
6namespace GUI {
7
8/* Settings */
9CSimpleIniA ini(true, false, false);
10
11/* Window settings */
12int last_window_size = 1;
13
14/* Controls settings */
15SDL_Scancode KEY_A [] = { SDL_SCANCODE_A, SDL_SCANCODE_ESCAPE };
16SDL_Scancode KEY_B [] = { SDL_SCANCODE_S, SDL_SCANCODE_ESCAPE };
17SDL_Scancode KEY_SELECT[] = { SDL_SCANCODE_SPACE, SDL_SCANCODE_ESCAPE };
18SDL_Scancode KEY_START [] = { SDL_SCANCODE_RETURN, SDL_SCANCODE_ESCAPE };
19SDL_Scancode KEY_UP [] = { SDL_SCANCODE_UP, SDL_SCANCODE_ESCAPE };
20SDL_Scancode KEY_DOWN [] = { SDL_SCANCODE_DOWN, SDL_SCANCODE_ESCAPE };
21SDL_Scancode KEY_LEFT [] = { SDL_SCANCODE_LEFT, SDL_SCANCODE_ESCAPE };
22SDL_Scancode KEY_RIGHT [] = { SDL_SCANCODE_RIGHT, SDL_SCANCODE_ESCAPE };
23int BTN_UP [] = { -1, -1 };
24int BTN_DOWN [] = { -1, -1 };
25int BTN_LEFT [] = { -1, -1 };
26int BTN_RIGHT [] = { -1, -1 };
27int BTN_A [] = { -1, -1 };
28int BTN_B [] = { -1, -1 };
29int BTN_SELECT[] = { -1, -1 };
30int BTN_START [] = { -1, -1 };
31bool useJoystick[] = { false, false };
32
33
34
35/* Ensure config directory exists */
36const char* get_config_path(char* buf, int buflen)
37{
38 /* Bail on the complex stuff if we don't need it */
39 if (!USE_CONFIG_DIR)
40 return CONFIG_FALLBACK;
41
42 /* First, get the home directory */
43 char homepath[CONFIG_PATH_MAX];
44 char path[CONFIG_PATH_MAX];
45 char * home = getenv("HOME");
46 if (home == NULL)
47 return CONFIG_FALLBACK;
48
49 snprintf(homepath, sizeof(homepath), "%s/.config", home);
50
51 /* Then, .config as a folder */
52 int res = mkdir(homepath, CONFIG_DIR_DEFAULT_MODE);
53 int err = errno;
54
55 if (res == -1 && err != EEXIST)
56 return CONFIG_FALLBACK;
57
58 snprintf(path, sizeof(path), "%s/%s", homepath, CONFIG_DIR_NAME);
59
60 /* Finally, CONFIG_DIR_NAME as a sub-folder */
61 res = mkdir(path, CONFIG_DIR_DEFAULT_MODE);
62 err = errno;
63
64 if (res == -1 && err != EEXIST)
65 return CONFIG_FALLBACK;
66
67 snprintf(buf, buflen, "%s/settings", path);
68
69 return buf;
70}
71
72
73/* Load settings */
74void load_settings()
75{
76 /* Files */
77 char path[CONFIG_PATH_MAX];
78 ini.LoadFile(get_config_path(path, sizeof(path)));
79
80 /* Screen settings */
81 int screen_size = atoi(ini.GetValue("screen", "size", "1"));
82 if (screen_size < 1 || screen_size > 4)
83 screen_size = 1;
84
85 set_size(screen_size);
86
87 /* Control settings */
88 for (int p = 0; p <= 1; p++)
89 {
90 const char* section = (p == 0) ? "controls p1" : "controls p2";
91
92 useJoystick[p] = (ini.GetValue(section, "usejoy", "no"))[0] == 'y';
93 if (useJoystick[p])
94 {
95 BTN_UP[p] = atoi(ini.GetValue(section, "UP", "-1"));
96 BTN_DOWN[p] = atoi(ini.GetValue(section, "DOWN", "-1"));
97 BTN_LEFT[p] = atoi(ini.GetValue(section, "LEFT", "-1"));
98 BTN_RIGHT[p] = atoi(ini.GetValue(section, "RIGHT", "-1"));
99 BTN_A[p] = atoi(ini.GetValue(section, "A", "-1"));
100 BTN_B[p] = atoi(ini.GetValue(section, "B", "-1"));
101 BTN_SELECT[p] = atoi(ini.GetValue(section, "SELECT", "-1"));
102 BTN_START[p] = atoi(ini.GetValue(section, "START", "-1"));
103 }
104 else
105 {
106 KEY_UP[p] = (SDL_Scancode)atoi(ini.GetValue(section, "UP", "82"));
107 KEY_DOWN[p] = (SDL_Scancode)atoi(ini.GetValue(section, "DOWN", "81"));
108 KEY_LEFT[p] = (SDL_Scancode)atoi(ini.GetValue(section, "LEFT", "80"));
109 KEY_RIGHT[p] = (SDL_Scancode)atoi(ini.GetValue(section, "RIGHT", "79"));
110 KEY_A[p] = (SDL_Scancode)atoi(ini.GetValue(section, "A", "4"));
111 KEY_B[p] = (SDL_Scancode)atoi(ini.GetValue(section, "B", "22"));
112 KEY_SELECT[p] = (SDL_Scancode)atoi(ini.GetValue(section, "SELECT", "44"));
113 KEY_START[p] = (SDL_Scancode)atoi(ini.GetValue(section, "START", "40"));
114 }
115 }
116}
117
118
119/* Save settings */
120void save_settings()
121{
122 /* Screen settings */
123 char buf[10];
124 sprintf(buf, "%d", last_window_size);
125 ini.SetValue("screen", "size", buf);
126
127 /* Control settings */
128 for (int p = 0; p < 2; p++)
129 {
130 const char* section = (p == 0) ? "controls p1" : "controls p2";
131
132 sprintf(buf, "%d", useJoystick[p] ? BTN_UP[p] : KEY_UP[p]);
133 ini.SetValue(section, "UP", buf);
134 sprintf(buf, "%d", useJoystick[p] ? BTN_DOWN[p] : KEY_DOWN[p]);
135 ini.SetValue(section, "DOWN", buf);
136 sprintf(buf, "%d", useJoystick[p] ? BTN_LEFT[p] : KEY_LEFT[p]);
137 ini.SetValue(section, "LEFT", buf);
138 sprintf(buf, "%d", useJoystick[p] ? BTN_RIGHT[p] : KEY_RIGHT[p]);
139 ini.SetValue(section, "RIGHT", buf);
140 sprintf(buf, "%d", useJoystick[p] ? BTN_A[p] : KEY_A[p]);
141 ini.SetValue(section, "A", buf);
142 sprintf(buf, "%d", useJoystick[p] ? BTN_B[p] : KEY_B[p]);
143 ini.SetValue(section, "B", buf);
144 sprintf(buf, "%d", useJoystick[p] ? BTN_SELECT[p] : KEY_SELECT[p]);
145 ini.SetValue(section, "SELECT", buf);
146 sprintf(buf, "%d", useJoystick[p] ? BTN_START[p] : KEY_START[p]);
147 ini.SetValue(section, "START", buf);
148 ini.SetValue(section, "usejoy", useJoystick[p] ? "yes" : "no");
149 }
150
151 char path[CONFIG_PATH_MAX];
152 ini.SaveFile(get_config_path(path, sizeof(path)));
153}
154
155}
156