ntoskrnl.exe/tests: Fix path buffer allocation size.
[wine.git] / programs / wineconsole / registry.c
blob359034f838f7279c5df8acf374ffed2e5ee377ff
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) cfg->cell_height = MulDiv( height, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI );
133 if (width != 0) cfg->cell_width = MulDiv( width, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI );
136 count = sizeof(val);
137 if (!RegQueryValueExW(hConKey, wszFontWeight, 0, &type, (LPBYTE)&val, &count))
138 cfg->font_weight = val;
140 count = sizeof(val);
141 if (!RegQueryValueExW(hConKey, wszHistoryBufferSize, 0, &type, (LPBYTE)&val, &count))
142 cfg->history_size = val;
144 count = sizeof(val);
145 if (!RegQueryValueExW(hConKey, wszHistoryNoDup, 0, &type, (LPBYTE)&val, &count))
146 cfg->history_nodup = val;
148 count = sizeof(val);
149 if (!RegQueryValueExW(hConKey, wszInsertMode, 0, &type, (LPBYTE)&val, &count))
150 cfg->insert_mode = val;
152 count = sizeof(val);
153 if (!RegQueryValueExW(hConKey, wszMenuMask, 0, &type, (LPBYTE)&val, &count))
154 cfg->menu_mask = val;
156 count = sizeof(val);
157 if (!RegQueryValueExW(hConKey, wszPopupColors, 0, &type, (LPBYTE)&val, &count))
158 cfg->popup_attr = val;
160 count = sizeof(val);
161 if (!RegQueryValueExW(hConKey, wszQuickEdit, 0, &type, (LPBYTE)&val, &count))
162 cfg->quick_edit = val;
164 count = sizeof(val);
165 if (!RegQueryValueExW(hConKey, wszScreenBufferSize, 0, &type, (LPBYTE)&val, &count))
167 cfg->sb_height = HIWORD(val);
168 cfg->sb_width = LOWORD(val);
171 count = sizeof(val);
172 if (!RegQueryValueExW(hConKey, wszScreenColors, 0, &type, (LPBYTE)&val, &count))
173 cfg->def_attr = val;
175 count = sizeof(val);
176 if (!RegQueryValueExW(hConKey, wszWindowSize, 0, &type, (LPBYTE)&val, &count))
178 cfg->win_height = HIWORD(val);
179 cfg->win_width = LOWORD(val);
182 /* win_pos isn't read from registry */
185 /******************************************************************
186 * WINECON_RegLoad
190 void WINECON_RegLoad(const WCHAR* appname, struct config_data* cfg)
192 static const COLORREF color_map[NUM_COLORS] =
194 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x80), RGB(0x00, 0x80, 0x00), RGB(0x00, 0x80, 0x80),
195 RGB(0x80, 0x00, 0x00), RGB(0x80, 0x00, 0x80), RGB(0x80, 0x80, 0x00), RGB(0xC0, 0xC0, 0xC0),
196 RGB(0x80, 0x80, 0x80), RGB(0x00, 0x00, 0xFF), RGB(0x00, 0xFF, 0x00), RGB(0x00, 0xFF, 0xFF),
197 RGB(0xFF, 0x00, 0x00), RGB(0xFF, 0x00, 0xFF), RGB(0xFF, 0xFF, 0x00), RGB(0xFF, 0xFF, 0xFF),
200 int i;
201 HKEY hConKey;
203 WINE_TRACE("loading %s registry settings.\n", appname ? wine_dbgstr_w(appname) : "default");
205 /* first set default values */
206 for (i = 0; i < NUM_COLORS; i++)
208 cfg->color_map[i] = color_map[i];
210 cfg->cursor_size = 25;
211 cfg->cursor_visible = 1;
212 cfg->exit_on_die = 1;
213 memset(cfg->face_name, 0, sizeof(cfg->face_name));
214 cfg->cell_height = MulDiv( 16, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI );
215 cfg->cell_width = MulDiv( 8, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI );
216 cfg->font_weight = FW_NORMAL;
217 cfg->history_size = 50;
218 cfg->history_nodup = 0;
219 cfg->insert_mode = 1;
220 cfg->menu_mask = 0;
221 cfg->popup_attr = 0xF5;
222 cfg->quick_edit = 0;
223 cfg->sb_height = 25;
224 cfg->sb_width = 80;
225 cfg->def_attr = 0x000F;
226 cfg->win_height = 25;
227 cfg->win_width = 80;
228 cfg->win_pos.X = 0;
229 cfg->win_pos.Y = 0;
230 cfg->edition_mode = 0;
231 cfg->registry = NULL;
233 /* then read global settings */
234 if (!RegOpenKeyW(HKEY_CURRENT_USER, wszConsole, &hConKey))
236 WINECON_RegLoadHelper(hConKey, cfg);
237 /* if requested, load part related to console title */
238 if (appname)
240 HKEY hAppKey;
242 cfg->registry = WINECON_CreateKeyName(appname);
243 if (!RegOpenKeyW(hConKey, cfg->registry, &hAppKey))
245 WINECON_RegLoadHelper(hAppKey, cfg);
246 RegCloseKey(hAppKey);
249 RegCloseKey(hConKey);
251 WINECON_DumpConfig("load", cfg);
254 /******************************************************************
255 * WINECON_RegSaveHelper
259 static void WINECON_RegSaveHelper(HKEY hConKey, const struct config_data* cfg)
261 int i;
262 DWORD val, width, height;
263 WCHAR color_name[13];
265 WINECON_DumpConfig("save", cfg);
267 for (i = 0; i < NUM_COLORS; i++)
269 sprintfW(color_name, color_name_fmt, wszColorTable, i);
270 val = cfg->color_map[i];
271 RegSetValueExW(hConKey, color_name, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
274 val = cfg->cursor_size;
275 RegSetValueExW(hConKey, wszCursorSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
277 val = cfg->cursor_visible;
278 RegSetValueExW(hConKey, wszCursorVisible, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
280 val = cfg->edition_mode;
281 RegSetValueExW(hConKey, wszEditionMode, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
283 val = cfg->exit_on_die;
284 RegSetValueExW(hConKey, wszExitOnDie, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
286 RegSetValueExW(hConKey, wszFaceName, 0, REG_SZ, (LPBYTE)&cfg->face_name, sizeof(cfg->face_name));
288 width = MulDiv( cfg->cell_width, USER_DEFAULT_SCREEN_DPI, GetDpiForSystem() );
289 height = MulDiv( cfg->cell_height, USER_DEFAULT_SCREEN_DPI, GetDpiForSystem() );
290 val = MAKELONG(width, height);
291 RegSetValueExW(hConKey, wszFontSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
293 val = cfg->font_weight;
294 RegSetValueExW(hConKey, wszFontWeight, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
296 val = cfg->history_size;
297 RegSetValueExW(hConKey, wszHistoryBufferSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
299 val = cfg->history_nodup;
300 RegSetValueExW(hConKey, wszHistoryNoDup, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
302 val = cfg->insert_mode;
303 RegSetValueExW(hConKey, wszInsertMode, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
305 val = cfg->menu_mask;
306 RegSetValueExW(hConKey, wszMenuMask, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
308 val = cfg->popup_attr;
309 RegSetValueExW(hConKey, wszPopupColors, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
311 val = cfg->quick_edit;
312 RegSetValueExW(hConKey, wszQuickEdit, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
314 val = MAKELONG(cfg->sb_width, cfg->sb_height);
315 RegSetValueExW(hConKey, wszScreenBufferSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
317 val = cfg->def_attr;
318 RegSetValueExW(hConKey, wszScreenColors, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
320 val = MAKELONG(cfg->win_width, cfg->win_height);
321 RegSetValueExW(hConKey, wszWindowSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
324 /******************************************************************
325 * WINECON_RegSave
329 void WINECON_RegSave(const struct config_data* cfg)
331 HKEY hConKey;
333 WINE_TRACE("saving registry settings.\n");
334 if (RegCreateKeyW(HKEY_CURRENT_USER, wszConsole, &hConKey))
336 WINE_ERR("Can't open registry for saving\n");
338 else
340 if (cfg->registry)
342 HKEY hAppKey;
344 if (RegCreateKeyW(hConKey, cfg->registry, &hAppKey))
346 WINE_ERR("Can't open registry for saving\n");
348 else
350 /* FIXME: maybe only save the values different from the default value ? */
351 WINECON_RegSaveHelper(hAppKey, cfg);
352 RegCloseKey(hAppKey);
355 else WINECON_RegSaveHelper(hConKey, cfg);
356 RegCloseKey(hConKey);