]> git.tdb.fi Git - ext/subsurface.git/blob - windows.c
Split reading/writing preferences into OS specific files
[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 = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\subsurface"), 0,
26                         KEY_QUERY_VALUE, &hkey);
27         if (success != ERROR_SUCCESS) {
28                 success = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\subsurface"),
29                                         0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
30                                         NULL, &hkey, NULL);
31                 if (success != ERROR_SUCCESS)
32                         printf("CreateKey Software\\subsurface failed %ld\n", success);
33         }
34 }
35
36 void subsurface_set_conf(char *name, pref_type_t type, const void *value)
37 {
38         switch (type) {
39         case PREF_BOOL:
40                 /* we simply store the value as DWORD */
41                 RegSetValueEx(hkey, TEXT(name), 0, REG_DWORD, (DWORD)value, 4);
42                 break;
43         case PREF_STRING:
44                 RegSetValueEx(hkey, TEXT(name), 0, REG_SZ, value, strlen(value));
45         }
46 }
47
48 const void *subsurface_get_conf(char *name, pref_type_t type)
49 {
50         char *string;
51         int len;
52
53         switch (type) {
54         case PREF_BOOL:
55                 return get_from_registry(hkey, name) ? (void *) 1 : NULL;
56         case PREF_STRING:
57                 string = malloc(80);
58                 len = 80;
59                 success = RegQueryValueEx(hkey, TEXT(name), NULL, NULL,
60                                         (LPBYTE) string, &len );
61                 if (success != ERROR_SUCCESS) {
62                         /* that's what happens the first time we start - just return NULL */
63                         free(string);
64                         return NULL;
65                 }
66                 return string;
67         }
68         /* we shouldn't get here */
69         return NULL;
70 }
71
72 void subsurface_close_conf(void)
73 {
74         if (RegFlushKey(hkey) != ERROR_SUCCESS)
75                 printf("RegFlushKey failed %ld\n");
76         RegCloseKey(hkey);
77 }