]> git.tdb.fi Git - ext/subsurface.git/blob - macos.c
Make icon file name OS helper function
[ext/subsurface.git] / macos.c
1 /* macos.c */
2 /* implements Mac OS X specific functions */
3 #include "display-gtk.h"
4 #include <CoreFoundation/CoreFoundation.h>
5 #include <mach-o/dyld.h>
6
7 static CFURLRef fileURL;
8 static CFPropertyListRef propertyList;
9 static CFMutableDictionaryRef dict = NULL;
10
11 /* macos defines CFSTR to create a CFString object from a constant,
12  * but no similar macros if a C string variable is supposed to be
13  * the argument. We add this here (hardcoding the default allocator
14  * and MacRoman encoding */
15 #define CFSTR_VAR(_var) CFStringCreateWithCStringNoCopy(kCFAllocatorDefault,    \
16                                         (_var), kCFStringEncodingMacRoman,      \
17                                         kCFAllocatorNull)
18
19 void subsurface_open_conf(void)
20 {
21         CFStringRef errorString;
22         CFDataRef resourceData;
23         Boolean status;
24         SInt32 errorCode;
25
26         fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
27                                                 CFSTR("subsurface.pref"),// file path name
28                                                 kCFURLPOSIXPathStyle,    // interpret as POSIX path
29                                                 false );                 // is it a directory?
30
31         status = CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,
32                                                         fileURL, &resourceData,
33                                                         NULL, NULL, &errorCode);
34         if (status) {
35                 propertyList = CFPropertyListCreateFromXMLData(kCFAllocatorDefault,
36                                                         resourceData, kCFPropertyListImmutable,
37                                                         &errorString);
38                 CFRelease(resourceData);
39         }
40 }
41
42 void subsurface_set_conf(char *name, pref_type_t type, const void *value)
43 {
44         if (!dict)
45                 dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
46                                                 &kCFTypeDictionaryKeyCallBacks,
47                                                 &kCFTypeDictionaryValueCallBacks);
48         switch (type) {
49         case PREF_BOOL:
50                 CFDictionarySetValue(dict, CFSTR_VAR(name), value == NULL ? CFSTR("0") : CFSTR("1"));
51                 break;
52         case PREF_STRING:
53                 CFDictionarySetValue(dict, CFSTR_VAR(name), CFSTR_VAR(value));
54         }
55 }
56 const void *subsurface_get_conf(char *name, pref_type_t type)
57 {
58         CFStringRef dict_entry;
59
60         /* if no settings exist, we return the value for FALSE */
61         if (!propertyList)
62                 return NULL;
63
64         switch (type) {
65         case PREF_BOOL:
66                 dict_entry = CFDictionaryGetValue(propertyList, CFSTR_VAR(name));
67                 if (dict_entry && ! CFStringCompare(CFSTR("1"), dict_entry, 0))
68                         return (void *) 1;
69                 else
70                         return NULL;
71         case PREF_STRING:
72                 return CFStringGetCStringPtr(CFDictionaryGetValue(propertyList,
73                                                 CFSTR_VAR(name)), kCFStringEncodingMacRoman);
74         }
75         /* we shouldn't get here, but having this line makes the compiler happy */
76         return NULL;
77 }
78
79 void subsurface_close_conf(void)
80 {
81         Boolean status;
82         SInt32 errorCode;
83         CFDataRef xmlData;
84
85         propertyList = dict;
86         dict = NULL;
87         xmlData = CFPropertyListCreateXMLData(kCFAllocatorDefault, propertyList);
88         status = CFURLWriteDataAndPropertiesToResource (fileURL, xmlData, NULL, &errorCode);
89         // some error handling - but really, what can we do?
90         CFRelease(xmlData);
91         CFRelease(propertyList);
92 }
93
94 const char *subsurface_USB_name()
95 {
96         return "/dev/tty.SLAB_USBtoUART";
97 }
98
99 #define REL_ICON_PATH "Resources/Subsurface.icns"
100 const char *subsurface_icon_name()
101 {
102         static char path[1024];
103         char *ptr1, *ptr2;
104         uint32_t size = sizeof(path); /* need extra space to copy icon path */
105         if (_NSGetExecutablePath(path, &size) == 0) {
106                 ptr1 = strcasestr(path,"MacOS/subsurface");
107                 ptr2 = strcasestr(path,"Contents");
108                 if (ptr1 && ptr2) {
109                         /* we are running as installed app from a bundle */
110                         if (ptr1 - path < size - strlen(REL_ICON_PATH)) {
111                                 strcpy(ptr1,REL_ICON_PATH);
112                                 return path;
113                         }
114                 }
115         }
116         return "packaging/macosx/Subsurface.icns";
117 }