summaryrefslogtreecommitdiffstats
path: root/macos.c
blob: 0b64e19a0ee3c9354d56507c2b233a90bd2f0690 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* macos.c */
/* implements Mac OS X specific functions */
#include "display-gtk.h"
#include <CoreFoundation/CoreFoundation.h>

static CFURLRef fileURL;
static CFPropertyListRef propertyList;
static CFMutableDictionaryRef dict = NULL;

/* macos defines CFSTR to create a CFString object from a constant,
 * but no similar macros if a C string variable is supposed to be
 * the argument. We add this here (hardcoding the default allocator
 * and MacRoman encoding */
#define CFSTR_VAR(_var) CFStringCreateWithCStringNoCopy(kCFAllocatorDefault,	\
					(_var), kCFStringEncodingMacRoman,	\
					kCFAllocatorNull)

void subsurface_open_conf(void)
{
	CFStringRef errorString;
	CFDataRef resourceData;
	Boolean status;
	SInt32 errorCode;

	fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
						CFSTR("subsurface.pref"),// file path name
						kCFURLPOSIXPathStyle,    // interpret as POSIX path
						false );                 // is it a directory?

	status = CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,
							fileURL, &resourceData,
							NULL, NULL, &errorCode);
	if (status) {
		propertyList = CFPropertyListCreateFromXMLData(kCFAllocatorDefault,
							resourceData, kCFPropertyListImmutable,
							&errorString);
		CFRelease(resourceData);
	}
}

void subsurface_set_conf(char *name, pref_type_t type, const void *value)
{
	if (!dict)
		dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
						&kCFTypeDictionaryKeyCallBacks,
						&kCFTypeDictionaryValueCallBacks);
	switch (type) {
	case PREF_BOOL:
		CFDictionarySetValue(dict, CFSTR_VAR(name), value == NULL ? CFSTR("0") : CFSTR("1"));
		break;
	case PREF_STRING:
		CFDictionarySetValue(dict, CFSTR_VAR(name), CFSTR_VAR(value));
	}
}
const void *subsurface_get_conf(char *name, pref_type_t type)
{
	CFStringRef dict_entry;

	/* if no settings exist, we return the value for FALSE */
	if (!propertyList)
		return NULL;

	switch (type) {
	case PREF_BOOL:
		dict_entry = CFDictionaryGetValue(propertyList, CFSTR_VAR(name));
		if (dict_entry && ! CFStringCompare(CFSTR("1"), dict_entry, 0))
			return (void *) 1;
		else
			return NULL;
	case PREF_STRING:
		return CFStringGetCStringPtr(CFDictionaryGetValue(propertyList,
						CFSTR_VAR(name)), kCFStringEncodingMacRoman);
	}
	/* we shouldn't get here, but having this line makes the compiler happy */
	return NULL;
}

void subsurface_close_conf(void)
{
	Boolean status;
	SInt32 errorCode;
	CFDataRef xmlData;

	propertyList = dict;
	dict = NULL;
	xmlData = CFPropertyListCreateXMLData(kCFAllocatorDefault, propertyList);
	status = CFURLWriteDataAndPropertiesToResource (fileURL, xmlData, NULL, &errorCode);
	// some error handling - but really, what can we do?
	CFRelease(xmlData);
	CFRelease(propertyList);
}

const char *subsurface_USB_name()
{
	return("/dev/tty.SLAB_USBtoUART");
}