]> git.tdb.fi Git - ext/subsurface.git/blob - windows.c
Fix the Windows preferences support
[ext/subsurface.git] / windows.c
1 /* windows.c */
2 /* implements Windows specific functions */
3 #include "display-gtk.h"
4 #include <windows.h>
5
6 static HKEY hkey;
7
8 static int get_from_registry(HKEY hkey, const char *key)
9 {
10         DWORD value;
11         DWORD len = 4;
12         LONG success;
13
14         success = RegQueryValueEx(hkey, TEXT(key), NULL, NULL,
15                                 (LPBYTE) &value, &len );
16         if (success != ERROR_SUCCESS)
17                 return FALSE; /* that's what happens the first time we start */
18         return value;
19 }
20
21 void subsurface_open_conf(void)
22 {
23         LONG success;
24
25         success = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\subsurface"),
26                                 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
27                                 NULL, &hkey, NULL);
28         if (success != ERROR_SUCCESS)
29                 printf("CreateKey Software\\subsurface failed %ld\n", success);
30 }
31
32 void subsurface_set_conf(char *name, pref_type_t type, const void *value)
33 {
34         switch (type) {
35         case PREF_BOOL:
36                 /* we simply store the value as DWORD */
37                 RegSetValueEx(hkey, TEXT(name), 0, REG_DWORD, &value, 4);
38                 break;
39         case PREF_STRING:
40                 RegSetValueEx(hkey, TEXT(name), 0, REG_SZ, value, strlen(value));
41         }
42 }
43
44 const void *subsurface_get_conf(char *name, pref_type_t type)
45 {
46         LONG success;
47         char *string;
48         int len;
49
50         switch (type) {
51         case PREF_BOOL:
52                 return get_from_registry(hkey, name) ? (void *) 1 : NULL;
53         case PREF_STRING:
54                 string = malloc(80);
55                 len = 80;
56                 success = RegQueryValueEx(hkey, TEXT(name), NULL, NULL,
57                                         (LPBYTE) string, (LPDWORD)&len );
58                 if (success != ERROR_SUCCESS) {
59                         /* that's what happens the first time we start - just return NULL */
60                         free(string);
61                         return NULL;
62                 }
63                 return string;
64         }
65         /* we shouldn't get here */
66         return NULL;
67 }
68
69 void subsurface_close_conf(void)
70 {
71         if (RegFlushKey(hkey) != ERROR_SUCCESS)
72                 printf("RegFlushKey failed \n");
73         RegCloseKey(hkey);
74 }