Fixed some issues found by winapi_check.
[wine] / programs / wineconsole / registry.c
1 /*
2  * an application for displaying Win32 console
3  *      registry and init functions
4  *
5  * Copyright 2001 Eric Pouech
6  *
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.
11  *
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.
16  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "winbase.h"
23 #include "winreg.h"
24 #include "winecon_private.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
29
30 static const WCHAR wszConsole[]           = {'C','o','n','s','o','l','e',0};
31 static const WCHAR wszCursorSize[]        = {'C','u','r','s','o','r','S','i','z','e',0};
32 static const WCHAR wszCursorVisible[]     = {'C','u','r','s','o','r','V','i','s','i','b','l','e',0};
33 static const WCHAR wszExitOnDie[]         = {'E','x','i','t','O','n','D','i','e',0};
34 static const WCHAR wszFaceName[]          = {'F','a','c','e','N','a','m','e',0};
35 static const WCHAR wszFontSize[]          = {'F','o','n','t','S','i','z','e',0};
36 static const WCHAR wszFontWeight[]        = {'F','o','n','t','W','e','i','g','h','t',0};
37 static const WCHAR wszHistoryBufferSize[] = {'H','i','s','t','o','r','y','B','u','f','f','e','r','S','i','z','e',0};
38 static const WCHAR wszHistoryNoDup[]      = {'H','i','s','t','o','r','y','N','o','D','u','p',0};
39 static const WCHAR wszMenuMask[]          = {'M','e','n','u','M','a','s','k',0};
40 static const WCHAR wszQuickEdit[]         = {'Q','u','i','c','k','E','d','i','t',0};
41 static const WCHAR wszScreenBufferSize[]  = {'S','c','r','e','e','n','B','u','f','f','e','r','S','i','z','e',0};
42 static const WCHAR wszScreenColors[]      = {'S','c','r','e','e','n','C','o','l','o','r','s',0};
43 static const WCHAR wszWindowSize[]        = {'W','i','n','d','o','w','S','i','z','e',0};
44
45 void WINECON_DumpConfig(const char* pfx, const struct config_data* cfg)
46 {
47     WINE_TRACE("%s cell=(%u,%u) cursor=(%d,%d) attr=%02lx font=%s/%lu hist=%lu/%d flags=%c%c msk=%08lx sb=(%u,%u) win=(%u,%u)x(%u,%u) registry=%s\n",
48                pfx, cfg->cell_width, cfg->cell_height, cfg->cursor_size, cfg->cursor_visible, cfg->def_attr,
49                wine_dbgstr_w(cfg->face_name), cfg->font_weight, cfg->history_size, cfg->history_nodup ? 1 : 2,
50                cfg->quick_edit ? 'Q' : 'q', cfg->exit_on_die ? 'X' : 'x',
51                cfg->menu_mask, cfg->sb_width, cfg->sb_height, cfg->win_pos.X, cfg->win_pos.Y, cfg->win_width, cfg->win_height,
52                wine_dbgstr_w(cfg->registry));
53 }
54
55 /******************************************************************
56  *              WINECON_CreateKeyName
57  *
58  * Get a proper key name from an appname (mainly convert '\\' to '_')
59  */
60 static LPWSTR   WINECON_CreateKeyName(LPCWSTR kn)
61 {
62     LPWSTR      ret = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(kn) + 1) * sizeof(WCHAR));
63     LPWSTR      ptr = ret;
64
65     if (!ptr) WINECON_Fatal("OOM");
66
67     do
68     {
69         *ptr++ = *kn == '\\' ? '_' : *kn;
70     } while (*kn++ != 0);
71     return ret;
72 }
73
74 /******************************************************************
75  *              WINECON_RegLoadHelper
76  *
77  * Read the basic configuration from any console key or subkey
78  */
79 static void WINECON_RegLoadHelper(HKEY hConKey, struct config_data* cfg)
80 {
81     DWORD       type;
82     DWORD       count;
83     DWORD       val;
84
85     count = sizeof(val);
86     if (!RegQueryValueEx(hConKey, wszCursorSize, 0, &type, (char*)&val, &count))
87         cfg->cursor_size = val;
88
89     count = sizeof(val);
90     if (!RegQueryValueEx(hConKey, wszCursorVisible, 0, &type, (char*)&val, &count))
91         cfg->cursor_visible = val;
92
93     count = sizeof(val);
94     if (!RegQueryValueEx(hConKey, wszExitOnDie, 0, &type, (char*)&val, &count))
95         cfg->exit_on_die = val;
96
97     count = sizeof(cfg->face_name);
98     RegQueryValueEx(hConKey, wszFaceName, 0, &type, (char*)&cfg->face_name, &count);
99
100     count = sizeof(val);
101     if (!RegQueryValueEx(hConKey, wszFontSize, 0, &type, (char*)&val, &count))
102     {
103         cfg->cell_height = HIWORD(val);
104         cfg->cell_width  = LOWORD(val);
105     }
106
107     count = sizeof(val);
108     if (!RegQueryValueEx(hConKey, wszFontWeight, 0, &type, (char*)&val, &count))
109         cfg->font_weight = val;
110
111     count = sizeof(val);
112     if (!RegQueryValueEx(hConKey, wszHistoryBufferSize, 0, &type, (char*)&val, &count))
113         cfg->history_size = val;
114
115     count = sizeof(val);
116     if (!RegQueryValueEx(hConKey, wszHistoryNoDup, 0, &type, (char*)&val, &count))
117         cfg->history_nodup = val;
118
119     count = sizeof(val);
120     if (!RegQueryValueEx(hConKey, wszMenuMask, 0, &type, (char*)&val, &count))
121         cfg->menu_mask = val;
122
123     count = sizeof(val);
124     if (!RegQueryValueEx(hConKey, wszQuickEdit, 0, &type, (char*)&val, &count))
125         cfg->quick_edit = val;
126
127     count = sizeof(val);
128     if (!RegQueryValueEx(hConKey, wszScreenBufferSize, 0, &type, (char*)&val, &count))
129     {
130         cfg->sb_height = HIWORD(val);
131         cfg->sb_width  = LOWORD(val);
132     }
133
134     count = sizeof(val);
135     if (!RegQueryValueEx(hConKey, wszScreenColors, 0, &type, (char*)&val, &count))
136         cfg->def_attr = val;
137
138     count = sizeof(val);
139     if (!RegQueryValueEx(hConKey, wszWindowSize, 0, &type, (char*)&val, &count))
140     {
141         cfg->win_height = HIWORD(val);
142         cfg->win_width  = LOWORD(val);
143     }
144
145     /* win_pos isn't read from registry */
146 }
147
148 /******************************************************************
149  *              WINECON_RegLoad
150  *
151  *
152  */
153 void WINECON_RegLoad(const WCHAR* appname, struct config_data* cfg)
154 {
155     HKEY        hConKey;
156
157     WINE_TRACE("loading %s registry settings.\n", appname ? wine_dbgstr_w(appname) : "default");
158
159     /* first set default values */
160     cfg->cursor_size = 25;
161     cfg->cursor_visible = 1;
162     cfg->exit_on_die = 1;
163     cfg->face_name[0] = 0;
164     cfg->cell_height = 12;
165     cfg->cell_width  = 8;
166     cfg->font_weight = 0;
167     cfg->history_size = 0;
168     cfg->history_nodup = 0;
169     cfg->menu_mask = 0;
170     cfg->quick_edit = 0;
171     cfg->sb_height = 25;
172     cfg->sb_width  = 80;
173     cfg->def_attr = 0x000F;
174     cfg->win_height = 25;
175     cfg->win_width  = 80;
176     cfg->registry = NULL;
177
178     /* then read global settings */
179     if (!RegOpenKey(HKEY_CURRENT_USER, wszConsole, &hConKey))
180     {
181         WINECON_RegLoadHelper(hConKey, cfg);
182         /* if requested, load part related to console title */
183         if (appname)
184         {
185             HKEY        hAppKey;
186
187             cfg->registry = WINECON_CreateKeyName(appname);
188             if (!RegOpenKey(hConKey, cfg->registry, &hAppKey))
189             {
190                 WINECON_RegLoadHelper(hAppKey, cfg);
191                 RegCloseKey(hAppKey);
192             }
193         }
194         RegCloseKey(hConKey);
195     }
196     WINECON_DumpConfig("load", cfg);
197 }
198
199 /******************************************************************
200  *              WINECON_RegSaveHelper
201  *
202  *
203  */
204 static void WINECON_RegSaveHelper(HKEY hConKey, const struct config_data* cfg)
205 {
206     DWORD       val;
207
208     WINECON_DumpConfig("save", cfg);
209
210     val = cfg->cursor_size;
211     RegSetValueEx(hConKey, wszCursorSize, 0, REG_DWORD, (char*)&val, sizeof(val));
212
213     val = cfg->cursor_visible;
214     RegSetValueEx(hConKey, wszCursorVisible, 0, REG_DWORD, (char*)&val, sizeof(val));
215
216     val = cfg->exit_on_die;
217     RegSetValueEx(hConKey, wszExitOnDie, 0, REG_DWORD, (char*)&val, sizeof(val));
218
219     RegSetValueEx(hConKey, wszFaceName, 0, REG_SZ, (char*)&cfg->face_name, sizeof(cfg->face_name));
220
221     val = MAKELONG(cfg->cell_width, cfg->cell_height);
222     RegSetValueEx(hConKey, wszFontSize, 0, REG_DWORD, (char*)&val, sizeof(val));
223
224     val = cfg->font_weight;
225     RegSetValueEx(hConKey, wszFontWeight, 0, REG_DWORD, (char*)&val, sizeof(val));
226
227     val = cfg->history_size;
228     RegSetValueEx(hConKey, wszHistoryBufferSize, 0, REG_DWORD, (char*)&val, sizeof(val));
229
230     val = cfg->history_nodup;
231     RegSetValueEx(hConKey, wszHistoryNoDup, 0, REG_DWORD, (char*)&val, sizeof(val));
232
233     val = cfg->menu_mask;
234     RegSetValueEx(hConKey, wszMenuMask, 0, REG_DWORD, (char*)&val, sizeof(val));
235
236     val = cfg->quick_edit;
237     RegSetValueEx(hConKey, wszQuickEdit, 0, REG_DWORD, (char*)&val, sizeof(val));
238
239     val = MAKELONG(cfg->sb_width, cfg->sb_height);
240     RegSetValueEx(hConKey, wszScreenBufferSize, 0, REG_DWORD, (char*)&val, sizeof(val));
241
242     val = cfg->def_attr;
243     RegSetValueEx(hConKey, wszScreenColors, 0, REG_DWORD, (char*)&val, sizeof(val));
244
245     val = MAKELONG(cfg->win_width, cfg->win_height);
246     RegSetValueEx(hConKey, wszWindowSize, 0, REG_DWORD, (char*)&val, sizeof(val));
247 }
248
249 /******************************************************************
250  *              WINECON_RegSave
251  *
252  *
253  */
254 void WINECON_RegSave(const struct config_data* cfg)
255 {
256     HKEY        hConKey;
257
258     WINE_TRACE("saving registry settings.\n");
259     if (RegCreateKey(HKEY_CURRENT_USER, wszConsole, &hConKey))
260     {
261         WINE_ERR("Can't open registry for saving\n");
262     }
263     else
264     {
265         if (cfg->registry)
266         {
267             HKEY    hAppKey;
268
269             if (RegCreateKey(hConKey, cfg->registry, &hAppKey))
270             {
271                 WINE_ERR("Can't open registry for saving\n");
272             }
273             else
274             {
275                 /* FIXME: maybe only save the values different from the default value ? */
276                 WINECON_RegSaveHelper(hAppKey, cfg);
277                 RegCloseKey(hAppKey);
278             }
279         }
280         else WINECON_RegSaveHelper(hConKey, cfg);
281         RegCloseKey(hConKey);
282     }
283 }