oleaut32: Remove unnecessary consts.
[wine.git] / programs / wineconsole / registry.c
blob92c53a7ca49201a9ea51f7b11bc9cee13b667927
1 /*
2 * an application for displaying Win32 console
3 * registry and init functions
5 * Copyright 2001 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "winecon_private.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
34 static const WCHAR wszColorTable[] = {'C','o','l','o','r','T','a','b','l','e',0};
35 static const WCHAR wszConsole[] = {'C','o','n','s','o','l','e',0};
36 static const WCHAR wszCursorSize[] = {'C','u','r','s','o','r','S','i','z','e',0};
37 static const WCHAR wszCursorVisible[] = {'C','u','r','s','o','r','V','i','s','i','b','l','e',0};
38 static const WCHAR wszEditionMode[] = {'E','d','i','t','i','o','n','M','o','d','e',0};
39 static const WCHAR wszExitOnDie[] = {'E','x','i','t','O','n','D','i','e',0};
40 static const WCHAR wszFaceName[] = {'F','a','c','e','N','a','m','e',0};
41 static const WCHAR wszFontSize[] = {'F','o','n','t','S','i','z','e',0};
42 static const WCHAR wszFontWeight[] = {'F','o','n','t','W','e','i','g','h','t',0};
43 static const WCHAR wszHistoryBufferSize[] = {'H','i','s','t','o','r','y','B','u','f','f','e','r','S','i','z','e',0};
44 static const WCHAR wszHistoryNoDup[] = {'H','i','s','t','o','r','y','N','o','D','u','p',0};
45 static const WCHAR wszInsertMode[] = {'I','n','s','e','r','t','M','o','d','e',0};
46 static const WCHAR wszMenuMask[] = {'M','e','n','u','M','a','s','k',0};
47 static const WCHAR wszPopupColors[] = {'P','o','p','u','p','C','o','l','o','r','s',0};
48 static const WCHAR wszQuickEdit[] = {'Q','u','i','c','k','E','d','i','t',0};
49 static const WCHAR wszScreenBufferSize[] = {'S','c','r','e','e','n','B','u','f','f','e','r','S','i','z','e',0};
50 static const WCHAR wszScreenColors[] = {'S','c','r','e','e','n','C','o','l','o','r','s',0};
51 static const WCHAR wszWindowSize[] = {'W','i','n','d','o','w','S','i','z','e',0};
53 static const WCHAR color_name_fmt[] = {'%','s','%','0','2','d',0};
55 #define NUM_COLORS 16
57 void WINECON_DumpConfig(const char* pfx, const struct config_data* cfg)
59 WINE_TRACE("%s cell=(%u,%u) cursor=(%d,%d) attr=%02x pop-up=%02x font=%s/%u hist=%u/%d flags=%c%c%c "
60 "msk=%08x sb=(%u,%u) win=(%u,%u)x(%u,%u) edit=%u registry=%s\n",
61 pfx, cfg->cell_width, cfg->cell_height, cfg->cursor_size, cfg->cursor_visible, cfg->def_attr,
62 cfg->popup_attr, wine_dbgstr_w(cfg->face_name), cfg->font_weight, cfg->history_size,
63 cfg->history_nodup ? 1 : 2, cfg->insert_mode ? 'I' : 'i', cfg->quick_edit ? 'Q' : 'q',
64 cfg->exit_on_die ? 'X' : 'x', cfg->menu_mask, cfg->sb_width, cfg->sb_height,
65 cfg->win_pos.X, cfg->win_pos.Y, cfg->win_width, cfg->win_height, cfg->edition_mode,
66 wine_dbgstr_w(cfg->registry));
69 /******************************************************************
70 * WINECON_CreateKeyName
72 * Get a proper key name from an appname (mainly convert '\\' to '_')
74 static LPWSTR WINECON_CreateKeyName(LPCWSTR kn)
76 LPWSTR ret = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(kn) + 1) * sizeof(WCHAR));
77 LPWSTR ptr = ret;
79 if (!ret) return NULL;
83 *ptr++ = *kn == '\\' ? '_' : *kn;
84 } while (*kn++ != 0);
85 return ret;
88 /******************************************************************
89 * WINECON_RegLoadHelper
91 * Read the basic configuration from any console key or subkey
93 static void WINECON_RegLoadHelper(HKEY hConKey, struct config_data* cfg)
95 int i;
96 DWORD type, count, val;
97 WCHAR color_name[13];
99 for (i = 0; i < NUM_COLORS; i++)
101 sprintfW(color_name, color_name_fmt, wszColorTable, i);
102 count = sizeof(val);
103 if (!RegQueryValueExW(hConKey, color_name, 0, &type, (LPBYTE)&val, &count))
104 cfg->color_map[i] = val;
107 count = sizeof(val);
108 if (!RegQueryValueExW(hConKey, wszCursorSize, 0, &type, (LPBYTE)&val, &count))
109 cfg->cursor_size = val;
111 count = sizeof(val);
112 if (!RegQueryValueExW(hConKey, wszCursorVisible, 0, &type, (LPBYTE)&val, &count))
113 cfg->cursor_visible = val;
115 count = sizeof(val);
116 if (!RegQueryValueExW(hConKey, wszEditionMode, 0, &type, (LPBYTE)&val, &count))
117 cfg->edition_mode = val;
119 count = sizeof(val);
120 if (!RegQueryValueExW(hConKey, wszExitOnDie, 0, &type, (LPBYTE)&val, &count))
121 cfg->exit_on_die = val;
123 count = sizeof(cfg->face_name);
124 RegQueryValueExW(hConKey, wszFaceName, 0, &type, (LPBYTE)&cfg->face_name, &count);
126 count = sizeof(val);
127 if (!RegQueryValueExW(hConKey, wszFontSize, 0, &type, (LPBYTE)&val, &count))
129 int height = HIWORD(val);
130 int width = LOWORD(val);
131 /* A value of zero reflects the default settings */
132 if (height != 0)
134 cfg->cell_height = height;
136 if (width != 0)
138 cfg->cell_width = width;
142 count = sizeof(val);
143 if (!RegQueryValueExW(hConKey, wszFontWeight, 0, &type, (LPBYTE)&val, &count))
144 cfg->font_weight = val;
146 count = sizeof(val);
147 if (!RegQueryValueExW(hConKey, wszHistoryBufferSize, 0, &type, (LPBYTE)&val, &count))
148 cfg->history_size = val;
150 count = sizeof(val);
151 if (!RegQueryValueExW(hConKey, wszHistoryNoDup, 0, &type, (LPBYTE)&val, &count))
152 cfg->history_nodup = val;
154 count = sizeof(val);
155 if (!RegQueryValueExW(hConKey, wszInsertMode, 0, &type, (LPBYTE)&val, &count))
156 cfg->insert_mode = val;
158 count = sizeof(val);
159 if (!RegQueryValueExW(hConKey, wszMenuMask, 0, &type, (LPBYTE)&val, &count))
160 cfg->menu_mask = val;
162 count = sizeof(val);
163 if (!RegQueryValueExW(hConKey, wszPopupColors, 0, &type, (LPBYTE)&val, &count))
164 cfg->popup_attr = val;
166 count = sizeof(val);
167 if (!RegQueryValueExW(hConKey, wszQuickEdit, 0, &type, (LPBYTE)&val, &count))
168 cfg->quick_edit = val;
170 count = sizeof(val);
171 if (!RegQueryValueExW(hConKey, wszScreenBufferSize, 0, &type, (LPBYTE)&val, &count))
173 cfg->sb_height = HIWORD(val);
174 cfg->sb_width = LOWORD(val);
177 count = sizeof(val);
178 if (!RegQueryValueExW(hConKey, wszScreenColors, 0, &type, (LPBYTE)&val, &count))
179 cfg->def_attr = val;
181 count = sizeof(val);
182 if (!RegQueryValueExW(hConKey, wszWindowSize, 0, &type, (LPBYTE)&val, &count))
184 cfg->win_height = HIWORD(val);
185 cfg->win_width = LOWORD(val);
188 /* win_pos isn't read from registry */
191 /******************************************************************
192 * WINECON_RegLoad
196 void WINECON_RegLoad(const WCHAR* appname, struct config_data* cfg)
198 static const COLORREF color_map[NUM_COLORS] =
200 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x80), RGB(0x00, 0x80, 0x00), RGB(0x00, 0x80, 0x80),
201 RGB(0x80, 0x00, 0x00), RGB(0x80, 0x00, 0x80), RGB(0x80, 0x80, 0x00), RGB(0xC0, 0xC0, 0xC0),
202 RGB(0x80, 0x80, 0x80), RGB(0x00, 0x00, 0xFF), RGB(0x00, 0xFF, 0x00), RGB(0x00, 0xFF, 0xFF),
203 RGB(0xFF, 0x00, 0x00), RGB(0xFF, 0x00, 0xFF), RGB(0xFF, 0xFF, 0x00), RGB(0xFF, 0xFF, 0xFF),
206 int i;
207 HKEY hConKey;
209 WINE_TRACE("loading %s registry settings.\n", appname ? wine_dbgstr_w(appname) : "default");
211 /* first set default values */
212 for (i = 0; i < NUM_COLORS; i++)
214 cfg->color_map[i] = color_map[i];
216 cfg->cursor_size = 25;
217 cfg->cursor_visible = 1;
218 cfg->exit_on_die = 1;
219 memset(cfg->face_name, 0, sizeof(cfg->face_name));
220 cfg->cell_height = 12;
221 cfg->cell_width = 8;
222 cfg->font_weight = 0;
223 cfg->history_size = 50;
224 cfg->history_nodup = 0;
225 cfg->insert_mode = 1;
226 cfg->menu_mask = 0;
227 cfg->popup_attr = 0xF5;
228 cfg->quick_edit = 0;
229 cfg->sb_height = 25;
230 cfg->sb_width = 80;
231 cfg->def_attr = 0x000F;
232 cfg->win_height = 25;
233 cfg->win_width = 80;
234 cfg->win_pos.X = 0;
235 cfg->win_pos.Y = 0;
236 cfg->edition_mode = 0;
237 cfg->registry = NULL;
239 /* then read global settings */
240 if (!RegOpenKeyW(HKEY_CURRENT_USER, wszConsole, &hConKey))
242 WINECON_RegLoadHelper(hConKey, cfg);
243 /* if requested, load part related to console title */
244 if (appname)
246 HKEY hAppKey;
248 cfg->registry = WINECON_CreateKeyName(appname);
249 if (!RegOpenKeyW(hConKey, cfg->registry, &hAppKey))
251 WINECON_RegLoadHelper(hAppKey, cfg);
252 RegCloseKey(hAppKey);
255 RegCloseKey(hConKey);
257 WINECON_DumpConfig("load", cfg);
260 /******************************************************************
261 * WINECON_RegSaveHelper
265 static void WINECON_RegSaveHelper(HKEY hConKey, const struct config_data* cfg)
267 int i;
268 DWORD val;
269 WCHAR color_name[13];
271 WINECON_DumpConfig("save", cfg);
273 for (i = 0; i < NUM_COLORS; i++)
275 sprintfW(color_name, color_name_fmt, wszColorTable, i);
276 val = cfg->color_map[i];
277 RegSetValueExW(hConKey, color_name, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
280 val = cfg->cursor_size;
281 RegSetValueExW(hConKey, wszCursorSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
283 val = cfg->cursor_visible;
284 RegSetValueExW(hConKey, wszCursorVisible, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
286 val = cfg->edition_mode;
287 RegSetValueExW(hConKey, wszEditionMode, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
289 val = cfg->exit_on_die;
290 RegSetValueExW(hConKey, wszExitOnDie, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
292 RegSetValueExW(hConKey, wszFaceName, 0, REG_SZ, (LPBYTE)&cfg->face_name, sizeof(cfg->face_name));
294 val = MAKELONG(cfg->cell_width, cfg->cell_height);
295 RegSetValueExW(hConKey, wszFontSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
297 val = cfg->font_weight;
298 RegSetValueExW(hConKey, wszFontWeight, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
300 val = cfg->history_size;
301 RegSetValueExW(hConKey, wszHistoryBufferSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
303 val = cfg->history_nodup;
304 RegSetValueExW(hConKey, wszHistoryNoDup, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
306 val = cfg->insert_mode;
307 RegSetValueExW(hConKey, wszInsertMode, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
309 val = cfg->menu_mask;
310 RegSetValueExW(hConKey, wszMenuMask, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
312 val = cfg->popup_attr;
313 RegSetValueExW(hConKey, wszPopupColors, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
315 val = cfg->quick_edit;
316 RegSetValueExW(hConKey, wszQuickEdit, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
318 val = MAKELONG(cfg->sb_width, cfg->sb_height);
319 RegSetValueExW(hConKey, wszScreenBufferSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
321 val = cfg->def_attr;
322 RegSetValueExW(hConKey, wszScreenColors, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
324 val = MAKELONG(cfg->win_width, cfg->win_height);
325 RegSetValueExW(hConKey, wszWindowSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
328 /******************************************************************
329 * WINECON_RegSave
333 void WINECON_RegSave(const struct config_data* cfg)
335 HKEY hConKey;
337 WINE_TRACE("saving registry settings.\n");
338 if (RegCreateKeyW(HKEY_CURRENT_USER, wszConsole, &hConKey))
340 WINE_ERR("Can't open registry for saving\n");
342 else
344 if (cfg->registry)
346 HKEY hAppKey;
348 if (RegCreateKeyW(hConKey, cfg->registry, &hAppKey))
350 WINE_ERR("Can't open registry for saving\n");
352 else
354 /* FIXME: maybe only save the values different from the default value ? */
355 WINECON_RegSaveHelper(hAppKey, cfg);
356 RegCloseKey(hAppKey);
359 else WINECON_RegSaveHelper(hConKey, cfg);
360 RegCloseKey(hConKey);