summaryrefslogtreecommitdiffstats
path: root/windows.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2011-11-23 22:56:57 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2011-11-24 10:12:40 -0800
commit671f6544ac8b4a6eb68576b37344e84808511eb8 (patch)
tree12a0b650a6d926dbaa7392ef099c04b6fe5a67c7 /windows.c
parentcf6e0e7ca1ef74f4090f660484bdc4726609b5d3 (diff)
downloadsubsurface-671f6544ac8b4a6eb68576b37344e84808511eb8.tar.gz
Split reading/writing preferences into OS specific files
This adds tested code for Linux and Mac OS, implementing the api that Linus suggested. The Windows code was moved into its own file, but hasn't even been compile tested, yet. In order to have just one interface to set or get a preference value we encode TRUE as (void *) 1 and FALSE as NULL. This works consistently on all platforms and regardless of whether we have 32 or 64 bit. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'windows.c')
-rw-r--r--windows.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/windows.c b/windows.c
new file mode 100644
index 000000000..df04aa0f2
--- /dev/null
+++ b/windows.c
@@ -0,0 +1,77 @@
+/* windows.c */
+/* implements Windows specific functions */
+#include "display-gtk.h"
+#include <windows.h>
+
+static HKEY hkey;
+
+static int get_from_registry(HKEY hkey, const char *key)
+{
+ DWORD value;
+ DWORD len = 4;
+ LONG success;
+
+ success = RegQueryValueEx(hkey, TEXT(key), NULL, NULL,
+ (LPBYTE) &value, &len );
+ if (success != ERROR_SUCCESS)
+ return FALSE; /* that's what happens the first time we start */
+ return value;
+}
+
+void subsurface_open_conf(void)
+{
+ LONG success;
+
+ success = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\subsurface"), 0,
+ KEY_QUERY_VALUE, &hkey);
+ if (success != ERROR_SUCCESS) {
+ success = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\subsurface"),
+ 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
+ NULL, &hkey, NULL);
+ if (success != ERROR_SUCCESS)
+ printf("CreateKey Software\\subsurface failed %ld\n", success);
+ }
+}
+
+void subsurface_set_conf(char *name, pref_type_t type, const void *value)
+{
+ switch (type) {
+ case PREF_BOOL:
+ /* we simply store the value as DWORD */
+ RegSetValueEx(hkey, TEXT(name), 0, REG_DWORD, (DWORD)value, 4);
+ break;
+ case PREF_STRING:
+ RegSetValueEx(hkey, TEXT(name), 0, REG_SZ, value, strlen(value));
+ }
+}
+
+const void *subsurface_get_conf(char *name, pref_type_t type)
+{
+ char *string;
+ int len;
+
+ switch (type) {
+ case PREF_BOOL:
+ return get_from_registry(hkey, name) ? (void *) 1 : NULL;
+ case PREF_STRING:
+ string = malloc(80);
+ len = 80;
+ success = RegQueryValueEx(hkey, TEXT(name), NULL, NULL,
+ (LPBYTE) string, &len );
+ if (success != ERROR_SUCCESS) {
+ /* that's what happens the first time we start - just return NULL */
+ free(string);
+ return NULL;
+ }
+ return string;
+ }
+ /* we shouldn't get here */
+ return NULL;
+}
+
+void subsurface_close_conf(void)
+{
+ if (RegFlushKey(hkey) != ERROR_SUCCESS)
+ printf("RegFlushKey failed %ld\n");
+ RegCloseKey(hkey);
+}