]> git.tdb.fi Git - ext/subsurface.git/blob - macos.c
Use a more standard approach to save preferences on MacOSX
[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 #include "gtkosxapplication.h"
7
8 static GtkOSXApplication *theApp;
9
10 /* macos defines CFSTR to create a CFString object from a constant,
11  * but no similar macros if a C string variable is supposed to be
12  * the argument. We add this here (hardcoding the default allocator
13  * and MacRoman encoding */
14 #define CFSTR_VAR(_var) CFStringCreateWithCStringNoCopy(kCFAllocatorDefault,    \
15                                         (_var), kCFStringEncodingMacRoman,      \
16                                         kCFAllocatorNull)
17
18 #define SUBSURFACE_PREFERENCES CFSTR("org.hohndel.subsurface")
19 #define REL_ICON_PATH "Resources/Subsurface.icns"
20 #define UI_FONT "Arial Unicode MS 12"
21 #define DIVELIST_MAC_DEFAULT_FONT "Arial Unicode MS 9"
22
23 void subsurface_open_conf(void)
24 {
25         /* nothing at this time */
26 }
27
28 void subsurface_set_conf(char *name, pref_type_t type, const void *value)
29 {
30         switch (type) {
31         case PREF_BOOL:
32                 CFPreferencesSetAppValue(CFSTR_VAR(name),
33                         value == NULL ? kCFBooleanFalse : kCFBooleanTrue, SUBSURFACE_PREFERENCES);
34                 break;
35         case PREF_STRING:
36                 CFPreferencesSetAppValue(CFSTR_VAR(name), CFSTR_VAR(value), SUBSURFACE_PREFERENCES);
37         }
38 }
39
40 const void *subsurface_get_conf(char *name, pref_type_t type)
41 {
42         Boolean boolpref;
43         CFPropertyListRef strpref;
44
45         switch (type) {
46         case PREF_BOOL:
47                 boolpref = CFPreferencesGetAppBooleanValue(CFSTR_VAR(name), SUBSURFACE_PREFERENCES, FALSE);
48                 if (boolpref)
49                         return (void *) 1;
50                 else
51                         return NULL;
52         case PREF_STRING:
53                 strpref = CFPreferencesCopyAppValue(CFSTR_VAR(name), SUBSURFACE_PREFERENCES);
54                 if (!strpref)
55                         return NULL;
56                 return CFStringGetCStringPtr(strpref, kCFStringEncodingMacRoman);
57         }
58         /* we shouldn't get here, but having this line makes the compiler happy */
59         return NULL;
60 }
61
62 void subsurface_close_conf(void)
63 {
64         int ok = CFPreferencesAppSynchronize(SUBSURFACE_PREFERENCES);
65         if (!ok)
66                 fprintf(stderr,"Could not save preferences\n");
67 }
68
69 const char *subsurface_USB_name()
70 {
71         return "/dev/tty.SLAB_USBtoUART";
72 }
73
74 const char *subsurface_icon_name()
75 {
76         static char path[1024];
77         char *ptr1, *ptr2;
78         uint32_t size = sizeof(path); /* need extra space to copy icon path */
79         if (_NSGetExecutablePath(path, &size) == 0) {
80                 ptr1 = strcasestr(path,"MacOS/subsurface");
81                 ptr2 = strcasestr(path,"Contents");
82                 if (ptr1 && ptr2) {
83                         /* we are running as installed app from a bundle */
84                         if (ptr1 - path < size - strlen(REL_ICON_PATH)) {
85                                 strcpy(ptr1,REL_ICON_PATH);
86                                 return path;
87                         }
88                 }
89         }
90         return "packaging/macosx/Subsurface.icns";
91 }
92
93 void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
94                 GtkWidget *vbox)
95 {
96         if (!divelist_font)
97                 divelist_font = DIVELIST_MAC_DEFAULT_FONT;
98         g_object_set(G_OBJECT(settings), "gtk-font-name", UI_FONT, NULL);
99
100         theApp = g_object_new(GTK_TYPE_OSX_APPLICATION, NULL);
101         gtk_widget_hide (menubar);
102         gtk_osxapplication_set_menu_bar(theApp, GTK_MENU_SHELL(menubar));
103         gtk_osxapplication_set_use_quartz_accelerators(theApp, TRUE);
104         gtk_osxapplication_ready(theApp);
105
106 }