2 /* implements Mac OS X specific functions */
3 #include "display-gtk.h"
4 #include <CoreFoundation/CoreFoundation.h>
5 #include <mach-o/dyld.h>
6 #include "gtkosxapplication.h"
9 static CFURLRef fileURL;
10 static CFPropertyListRef propertyList;
11 static CFMutableDictionaryRef dict = NULL;
12 static GtkOSXApplication *theApp;
14 /* macos defines CFSTR to create a CFString object from a constant,
15 * but no similar macros if a C string variable is supposed to be
16 * the argument. We add this here (hardcoding the default allocator
17 * and MacRoman encoding */
18 #define CFSTR_VAR(_var) CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, \
19 (_var), kCFStringEncodingMacRoman, \
22 #define SUBSURFACE_PREFERENCES "~/Library/Preferences/org.hohndel.subsurface.plist"
23 #define REL_ICON_PATH "Resources/Subsurface.icns"
24 #define UI_FONT "Arial Unicode MS 12"
25 #define DIVELIST_MAC_DEFAULT_FONT "Arial Unicode MS 9"
27 static void show_error(SInt32 errorCode, char *action)
32 case -11: errortext = "kCFURLUnknownSchemeError";
34 case -12: errortext = "kCFURLResourceNotFoundError";
36 case -13: errortext = "kCFURLResourceAccessViolationError";
38 case -14: errortext = "kCFURLRemoteHostUnavailableError";
40 case -15: errortext = "kCFURLImproperArgumentsError";
42 case -16: errortext = "kCFURLUnknownPropertyKeyError";
44 case -17: errortext = "kCFURLPropertyKeyUnavailableError";
46 case -18: errortext = "kCFURLTimeoutError";
48 default: errortext = "kCFURLUnknownError";
50 fprintf(stderr, "Error %s preferences: %s\n", action, errortext);
53 void subsurface_open_conf(void)
55 CFStringRef errorString;
56 CFDataRef resourceData;
60 fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
61 CFSTR(SUBSURFACE_PREFERENCES),// file path name
62 kCFURLPOSIXPathStyle, // interpret as POSIX path
63 false ); // is it a directory?
65 status = CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,
66 fileURL, &resourceData,
67 NULL, NULL, &errorCode);
69 propertyList = CFPropertyListCreateFromXMLData(kCFAllocatorDefault,
70 resourceData, kCFPropertyListImmutable,
72 CFRelease(resourceData);
74 show_error(errorCode, "reading");
78 void subsurface_set_conf(char *name, pref_type_t type, const void *value)
81 dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
82 &kCFTypeDictionaryKeyCallBacks,
83 &kCFTypeDictionaryValueCallBacks);
86 CFDictionarySetValue(dict, CFSTR_VAR(name), value == NULL ? CFSTR("0") : CFSTR("1"));
89 CFDictionarySetValue(dict, CFSTR_VAR(name), CFSTR_VAR(value));
92 const void *subsurface_get_conf(char *name, pref_type_t type)
94 CFStringRef dict_entry;
96 /* if no settings exist, we return the value for FALSE */
102 dict_entry = CFDictionaryGetValue(propertyList, CFSTR_VAR(name));
103 if (dict_entry && ! CFStringCompare(CFSTR("1"), dict_entry, 0))
108 return CFStringGetCStringPtr(CFDictionaryGetValue(propertyList,
109 CFSTR_VAR(name)), kCFStringEncodingMacRoman);
111 /* we shouldn't get here, but having this line makes the compiler happy */
115 void subsurface_close_conf(void)
123 xmlData = CFPropertyListCreateXMLData(kCFAllocatorDefault, propertyList);
124 status = CFURLWriteDataAndPropertiesToResource (fileURL, xmlData, NULL, &errorCode);
126 show_error(errorCode, "writing");
129 CFRelease(propertyList);
132 const char *subsurface_USB_name()
134 return "/dev/tty.SLAB_USBtoUART";
137 const char *subsurface_icon_name()
139 static char path[1024];
141 uint32_t size = sizeof(path); /* need extra space to copy icon path */
142 if (_NSGetExecutablePath(path, &size) == 0) {
143 ptr1 = strcasestr(path,"MacOS/subsurface");
144 ptr2 = strcasestr(path,"Contents");
146 /* we are running as installed app from a bundle */
147 if (ptr1 - path < size - strlen(REL_ICON_PATH)) {
148 strcpy(ptr1,REL_ICON_PATH);
153 return "packaging/macosx/Subsurface.icns";
156 void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
160 divelist_font = DIVELIST_MAC_DEFAULT_FONT;
161 g_object_set(G_OBJECT(settings), "gtk-font-name", UI_FONT, NULL);
163 theApp = g_object_new(GTK_TYPE_OSX_APPLICATION, NULL);
164 gtk_widget_hide (menubar);
165 gtk_osxapplication_set_menu_bar(theApp, GTK_MENU_SHELL(menubar));
166 gtk_osxapplication_set_use_quartz_accelerators(theApp, TRUE);
167 gtk_osxapplication_ready(theApp);