]> git.tdb.fi Git - ext/subsurface.git/blob - windows.c
Improve cross compile support and fix windows.c
[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, 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         LONG success;
51         char *string;
52         int len;
53
54         switch (type) {
55         case PREF_BOOL:
56                 return get_from_registry(hkey, name) ? (void *) 1 : NULL;
57         case PREF_STRING:
58                 string = malloc(80);
59                 len = 80;
60                 success = RegQueryValueEx(hkey, TEXT(name), NULL, NULL,
61                                         (LPBYTE) string, (LPDWORD)&len );
62                 if (success != ERROR_SUCCESS) {
63                         /* that's what happens the first time we start - just return NULL */
64                         free(string);
65                         return NULL;
66                 }
67                 return string;
68         }
69         /* we shouldn't get here */
70         return NULL;
71 }
72
73 void subsurface_close_conf(void)
74 {
75         if (RegFlushKey(hkey) != ERROR_SUCCESS)
76                 printf("RegFlushKey failed \n");
77         RegCloseKey(hkey);
78 }