server: Fix crash when calling GetNamedPipeHandleState on partially closed pipe.
[wine.git] / programs / wineconsole / registry.c
blobc1d04c1c5e5aa3d3d0a6853bbf1e024f7804504d
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/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
33 static const WCHAR wszConsole[] = {'C','o','n','s','o','l','e',0};
34 static const WCHAR wszCursorSize[] = {'C','u','r','s','o','r','S','i','z','e',0};
35 static const WCHAR wszCursorVisible[] = {'C','u','r','s','o','r','V','i','s','i','b','l','e',0};
36 static const WCHAR wszEditionMode[] = {'E','d','i','t','i','o','n','M','o','d','e',0};
37 static const WCHAR wszExitOnDie[] = {'E','x','i','t','O','n','D','i','e',0};
38 static const WCHAR wszFaceName[] = {'F','a','c','e','N','a','m','e',0};
39 static const WCHAR wszFontSize[] = {'F','o','n','t','S','i','z','e',0};
40 static const WCHAR wszFontWeight[] = {'F','o','n','t','W','e','i','g','h','t',0};
41 static const WCHAR wszHistoryBufferSize[] = {'H','i','s','t','o','r','y','B','u','f','f','e','r','S','i','z','e',0};
42 static const WCHAR wszHistoryNoDup[] = {'H','i','s','t','o','r','y','N','o','D','u','p',0};
43 static const WCHAR wszInsertMode[] = {'I','n','s','e','r','t','M','o','d','e',0};
44 static const WCHAR wszMenuMask[] = {'M','e','n','u','M','a','s','k',0};
45 static const WCHAR wszQuickEdit[] = {'Q','u','i','c','k','E','d','i','t',0};
46 static const WCHAR wszScreenBufferSize[] = {'S','c','r','e','e','n','B','u','f','f','e','r','S','i','z','e',0};
47 static const WCHAR wszScreenColors[] = {'S','c','r','e','e','n','C','o','l','o','r','s',0};
48 static const WCHAR wszWindowSize[] = {'W','i','n','d','o','w','S','i','z','e',0};
50 void WINECON_DumpConfig(const char* pfx, const struct config_data* cfg)
52 WINE_TRACE("%s cell=(%u,%u) cursor=(%d,%d) attr=%02x font=%s/%u hist=%u/%d flags=%c%c%c msk=%08x sb=(%u,%u) win=(%u,%u)x(%u,%u) edit=%u registry=%s\n",
53 pfx, cfg->cell_width, cfg->cell_height, cfg->cursor_size, cfg->cursor_visible, cfg->def_attr,
54 wine_dbgstr_w(cfg->face_name), cfg->font_weight, cfg->history_size, cfg->history_nodup ? 1 : 2,
55 cfg->insert_mode ? 'I' : 'i', cfg->quick_edit ? 'Q' : 'q', cfg->exit_on_die ? 'X' : 'x',
56 cfg->menu_mask, cfg->sb_width, cfg->sb_height, cfg->win_pos.X, cfg->win_pos.Y, cfg->win_width, cfg->win_height,
57 cfg->edition_mode,
58 wine_dbgstr_w(cfg->registry));
61 /******************************************************************
62 * WINECON_CreateKeyName
64 * Get a proper key name from an appname (mainly convert '\\' to '_')
66 static LPWSTR WINECON_CreateKeyName(LPCWSTR kn)
68 LPWSTR ret = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(kn) + 1) * sizeof(WCHAR));
69 LPWSTR ptr = ret;
71 if (!ptr) WINECON_Fatal("OOM");
75 *ptr++ = *kn == '\\' ? '_' : *kn;
76 } while (*kn++ != 0);
77 return ret;
80 /******************************************************************
81 * WINECON_RegLoadHelper
83 * Read the basic configuration from any console key or subkey
85 static void WINECON_RegLoadHelper(HKEY hConKey, struct config_data* cfg)
87 DWORD type;
88 DWORD count;
89 DWORD val;
91 count = sizeof(val);
92 if (!RegQueryValueExW(hConKey, wszCursorSize, 0, &type, (LPBYTE)&val, &count))
93 cfg->cursor_size = val;
95 count = sizeof(val);
96 if (!RegQueryValueExW(hConKey, wszCursorVisible, 0, &type, (LPBYTE)&val, &count))
97 cfg->cursor_visible = val;
99 count = sizeof(val);
100 if (!RegQueryValueExW(hConKey, wszEditionMode, 0, &type, (LPBYTE)&val, &count))
101 cfg->edition_mode = val;
103 count = sizeof(val);
104 if (!RegQueryValueExW(hConKey, wszExitOnDie, 0, &type, (LPBYTE)&val, &count))
105 cfg->exit_on_die = val;
107 count = sizeof(cfg->face_name);
108 RegQueryValueExW(hConKey, wszFaceName, 0, &type, (LPBYTE)&cfg->face_name, &count);
110 count = sizeof(val);
111 if (!RegQueryValueExW(hConKey, wszFontSize, 0, &type, (LPBYTE)&val, &count))
113 cfg->cell_height = HIWORD(val);
114 cfg->cell_width = LOWORD(val);
117 count = sizeof(val);
118 if (!RegQueryValueExW(hConKey, wszFontWeight, 0, &type, (LPBYTE)&val, &count))
119 cfg->font_weight = val;
121 count = sizeof(val);
122 if (!RegQueryValueExW(hConKey, wszHistoryBufferSize, 0, &type, (LPBYTE)&val, &count))
123 cfg->history_size = val;
125 count = sizeof(val);
126 if (!RegQueryValueExW(hConKey, wszHistoryNoDup, 0, &type, (LPBYTE)&val, &count))
127 cfg->history_nodup = val;
129 count = sizeof(val);
130 if (!RegQueryValueExW(hConKey, wszInsertMode, 0, &type, (LPBYTE)&val, &count))
131 cfg->insert_mode = val;
133 count = sizeof(val);
134 if (!RegQueryValueExW(hConKey, wszMenuMask, 0, &type, (LPBYTE)&val, &count))
135 cfg->menu_mask = val;
137 count = sizeof(val);
138 if (!RegQueryValueExW(hConKey, wszQuickEdit, 0, &type, (LPBYTE)&val, &count))
139 cfg->quick_edit = val;
141 count = sizeof(val);
142 if (!RegQueryValueExW(hConKey, wszScreenBufferSize, 0, &type, (LPBYTE)&val, &count))
144 cfg->sb_height = HIWORD(val);
145 cfg->sb_width = LOWORD(val);
148 count = sizeof(val);
149 if (!RegQueryValueExW(hConKey, wszScreenColors, 0, &type, (LPBYTE)&val, &count))
150 cfg->def_attr = val;
152 count = sizeof(val);
153 if (!RegQueryValueExW(hConKey, wszWindowSize, 0, &type, (LPBYTE)&val, &count))
155 cfg->win_height = HIWORD(val);
156 cfg->win_width = LOWORD(val);
159 /* win_pos isn't read from registry */
162 /******************************************************************
163 * WINECON_RegLoad
167 void WINECON_RegLoad(const WCHAR* appname, struct config_data* cfg)
169 HKEY hConKey;
171 WINE_TRACE("loading %s registry settings.\n", appname ? wine_dbgstr_w(appname) : "default");
173 /* first set default values */
174 cfg->cursor_size = 25;
175 cfg->cursor_visible = 1;
176 cfg->exit_on_die = 1;
177 memset(cfg->face_name, 0, sizeof(cfg->face_name));
178 cfg->cell_height = 12;
179 cfg->cell_width = 8;
180 cfg->font_weight = 0;
181 cfg->history_size = 50;
182 cfg->history_nodup = 0;
183 cfg->insert_mode = 1;
184 cfg->menu_mask = 0;
185 cfg->quick_edit = 0;
186 cfg->sb_height = 25;
187 cfg->sb_width = 80;
188 cfg->def_attr = 0x000F;
189 cfg->win_height = 25;
190 cfg->win_width = 80;
191 cfg->win_pos.X = 0;
192 cfg->win_pos.Y = 0;
193 cfg->edition_mode = 0;
194 cfg->registry = NULL;
196 /* then read global settings */
197 if (!RegOpenKeyW(HKEY_CURRENT_USER, wszConsole, &hConKey))
199 WINECON_RegLoadHelper(hConKey, cfg);
200 /* if requested, load part related to console title */
201 if (appname)
203 HKEY hAppKey;
205 cfg->registry = WINECON_CreateKeyName(appname);
206 if (!RegOpenKeyW(hConKey, cfg->registry, &hAppKey))
208 WINECON_RegLoadHelper(hAppKey, cfg);
209 RegCloseKey(hAppKey);
212 RegCloseKey(hConKey);
214 WINECON_DumpConfig("load", cfg);
217 /******************************************************************
218 * WINECON_RegSaveHelper
222 static void WINECON_RegSaveHelper(HKEY hConKey, const struct config_data* cfg)
224 DWORD val;
226 WINECON_DumpConfig("save", cfg);
228 val = cfg->cursor_size;
229 RegSetValueExW(hConKey, wszCursorSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
231 val = cfg->cursor_visible;
232 RegSetValueExW(hConKey, wszCursorVisible, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
234 val = cfg->edition_mode;
235 RegSetValueExW(hConKey, wszEditionMode, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
237 val = cfg->exit_on_die;
238 RegSetValueExW(hConKey, wszExitOnDie, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
240 RegSetValueExW(hConKey, wszFaceName, 0, REG_SZ, (LPBYTE)&cfg->face_name, sizeof(cfg->face_name));
242 val = MAKELONG(cfg->cell_width, cfg->cell_height);
243 RegSetValueExW(hConKey, wszFontSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
245 val = cfg->font_weight;
246 RegSetValueExW(hConKey, wszFontWeight, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
248 val = cfg->history_size;
249 RegSetValueExW(hConKey, wszHistoryBufferSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
251 val = cfg->history_nodup;
252 RegSetValueExW(hConKey, wszHistoryNoDup, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
254 val = cfg->insert_mode;
255 RegSetValueExW(hConKey, wszInsertMode, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
257 val = cfg->menu_mask;
258 RegSetValueExW(hConKey, wszMenuMask, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
260 val = cfg->quick_edit;
261 RegSetValueExW(hConKey, wszQuickEdit, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
263 val = MAKELONG(cfg->sb_width, cfg->sb_height);
264 RegSetValueExW(hConKey, wszScreenBufferSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
266 val = cfg->def_attr;
267 RegSetValueExW(hConKey, wszScreenColors, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
269 val = MAKELONG(cfg->win_width, cfg->win_height);
270 RegSetValueExW(hConKey, wszWindowSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
273 /******************************************************************
274 * WINECON_RegSave
278 void WINECON_RegSave(const struct config_data* cfg)
280 HKEY hConKey;
282 WINE_TRACE("saving registry settings.\n");
283 if (RegCreateKeyW(HKEY_CURRENT_USER, wszConsole, &hConKey))
285 WINE_ERR("Can't open registry for saving\n");
287 else
289 if (cfg->registry)
291 HKEY hAppKey;
293 if (RegCreateKeyW(hConKey, cfg->registry, &hAppKey))
295 WINE_ERR("Can't open registry for saving\n");
297 else
299 /* FIXME: maybe only save the values different from the default value ? */
300 WINECON_RegSaveHelper(hAppKey, cfg);
301 RegCloseKey(hAppKey);
304 else WINECON_RegSaveHelper(hConKey, cfg);
305 RegCloseKey(hConKey);