]> git.tdb.fi Git - ext/subsurface.git/blob - windows.c
Add more typecasts for Windows`
[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, (LPCTSTR)TEXT(key), NULL, NULL,
15                                 (LPBYTE) &value, (LPDWORD)&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, (LPCTSTR)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         /* since we are using the pointer 'value' as both an actual
35          * pointer to the string setting and as a way to pass the
36          * numbers 0 and 1 to this function for booleans, one of the
37          * calls to RegSetValueEx needs to pass &value (when we want
38          * to pass the boolean value), the other one passes value (the
39          * address of the string. */
40         switch (type) {
41         case PREF_BOOL:
42                 /* we simply store the value as DWORD */
43                 RegSetValueEx(hkey, (LPCTSTR)TEXT(name), 0, REG_DWORD, (const BYTE *)&value, 4);
44                 break;
45         case PREF_STRING:
46                 RegSetValueEx(hkey, (LPCTSTR)TEXT(name), 0, REG_SZ, (const BYTE *)value, strlen(value));
47         }
48 }
49
50 const void *subsurface_get_conf(char *name, pref_type_t type)
51 {
52         LONG success;
53         char *string;
54         int len;
55
56         switch (type) {
57         case PREF_BOOL:
58                 return get_from_registry(hkey, name) ? (void *) 1 : NULL;
59         case PREF_STRING:
60                 string = malloc(80);
61                 len = 80;
62                 success = RegQueryValueEx(hkey, (LPCTSTR)TEXT(name), NULL, NULL,
63                                         (LPBYTE) string, (LPDWORD)&len );
64                 if (success != ERROR_SUCCESS) {
65                         /* that's what happens the first time we start - just return NULL */
66                         free(string);
67                         return NULL;
68                 }
69                 return string;
70         }
71         /* we shouldn't get here */
72         return NULL;
73 }
74
75 void subsurface_close_conf(void)
76 {
77         if (RegFlushKey(hkey) != ERROR_SUCCESS)
78                 printf("RegFlushKey failed \n");
79         RegCloseKey(hkey);
80 }