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