2011-11-24 06:56:57 +00:00
|
|
|
/* macos.c */
|
|
|
|
/* implements Mac OS X specific functions */
|
2013-02-09 09:36:02 +00:00
|
|
|
#include <stdlib.h>
|
2013-09-17 09:18:52 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fnmatch.h>
|
2012-09-09 16:06:44 +00:00
|
|
|
#include "dive.h"
|
2013-05-03 20:32:23 +00:00
|
|
|
#include "display.h"
|
|
|
|
#if USE_GTK_UI
|
2011-11-24 06:56:57 +00:00
|
|
|
#include "display-gtk.h"
|
2013-05-03 20:32:23 +00:00
|
|
|
#endif /* USE_GTK_UI */
|
2011-11-24 06:56:57 +00:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2013-02-28 12:19:27 +00:00
|
|
|
#include <CoreServices/CoreServices.h>
|
2011-12-28 23:57:36 +00:00
|
|
|
#include <mach-o/dyld.h>
|
2013-03-04 01:53:43 +00:00
|
|
|
#include <sys/syslimits.h>
|
2012-01-01 21:41:47 +00:00
|
|
|
|
2011-11-24 06:56:57 +00:00
|
|
|
/* 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)
|
|
|
|
|
2012-01-02 14:14:42 +00:00
|
|
|
#define SUBSURFACE_PREFERENCES CFSTR("org.hohndel.subsurface")
|
2012-01-02 16:26:24 +00:00
|
|
|
#define ICON_NAME "Subsurface.icns"
|
2013-02-04 08:50:43 +00:00
|
|
|
#define UI_FONT "Arial 12"
|
2013-01-12 01:07:22 +00:00
|
|
|
|
2013-02-04 08:50:43 +00:00
|
|
|
const char system_divelist_default_font[] = "Arial 10";
|
2012-01-01 21:41:47 +00:00
|
|
|
|
2011-11-24 06:56:57 +00:00
|
|
|
void subsurface_open_conf(void)
|
|
|
|
{
|
2012-01-02 14:14:42 +00:00
|
|
|
/* nothing at this time */
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
void subsurface_unset_conf(const char *name)
|
2013-01-11 01:26:10 +00:00
|
|
|
{
|
|
|
|
CFPreferencesSetAppValue(CFSTR_VAR(name), NULL, SUBSURFACE_PREFERENCES);
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
void subsurface_set_conf(const char *name, const char *value)
|
2011-11-24 06:56:57 +00:00
|
|
|
{
|
2013-01-11 03:33:53 +00:00
|
|
|
CFPreferencesSetAppValue(CFSTR_VAR(name), CFSTR_VAR(value), SUBSURFACE_PREFERENCES);
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
2012-01-02 14:14:42 +00:00
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
void subsurface_set_conf_bool(const char *name, int value)
|
2011-11-24 06:56:57 +00:00
|
|
|
{
|
2013-01-11 03:33:53 +00:00
|
|
|
CFPreferencesSetAppValue(CFSTR_VAR(name),
|
|
|
|
value ? kCFBooleanTrue : kCFBooleanFalse, SUBSURFACE_PREFERENCES);
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
void subsurface_set_conf_int(const char *name, int value)
|
2013-02-12 12:03:23 +00:00
|
|
|
{
|
2013-02-12 12:03:24 +00:00
|
|
|
CFNumberRef numRef = CFNumberCreate(NULL, kCFNumberIntType, &value);
|
|
|
|
CFPreferencesSetAppValue(CFSTR_VAR(name), numRef, SUBSURFACE_PREFERENCES);
|
2013-02-12 12:03:23 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 15:55:50 +00:00
|
|
|
const char *subsurface_get_conf(const char *name)
|
2013-01-11 03:33:53 +00:00
|
|
|
{
|
|
|
|
CFPropertyListRef strpref;
|
|
|
|
|
|
|
|
strpref = CFPreferencesCopyAppValue(CFSTR_VAR(name), SUBSURFACE_PREFERENCES);
|
|
|
|
if (!strpref)
|
|
|
|
return NULL;
|
|
|
|
return strdup(CFStringGetCStringPtr(strpref, kCFStringEncodingMacRoman));
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
int subsurface_get_conf_bool(const char *name)
|
2013-01-11 03:33:53 +00:00
|
|
|
{
|
|
|
|
Boolean boolpref, exists;
|
2011-11-24 06:56:57 +00:00
|
|
|
|
2013-01-11 03:33:53 +00:00
|
|
|
boolpref = CFPreferencesGetAppBooleanValue(CFSTR_VAR(name), SUBSURFACE_PREFERENCES, &exists);
|
|
|
|
if (!exists)
|
|
|
|
return -1;
|
|
|
|
return boolpref;
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
int subsurface_get_conf_int(const char *name)
|
2013-02-12 12:03:23 +00:00
|
|
|
{
|
2013-02-12 12:03:24 +00:00
|
|
|
Boolean exists;
|
|
|
|
CFIndex value;
|
2013-02-12 12:03:23 +00:00
|
|
|
|
2013-02-12 12:03:24 +00:00
|
|
|
value = CFPreferencesGetAppIntegerValue(CFSTR_VAR(name), SUBSURFACE_PREFERENCES, &exists);
|
|
|
|
if (!exists)
|
|
|
|
return -1;
|
|
|
|
return value;
|
2013-02-12 12:03:23 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 17:03:48 +00:00
|
|
|
void subsurface_flush_conf(void)
|
2011-11-24 06:56:57 +00:00
|
|
|
{
|
2012-01-02 14:14:42 +00:00
|
|
|
int ok = CFPreferencesAppSynchronize(SUBSURFACE_PREFERENCES);
|
|
|
|
if (!ok)
|
|
|
|
fprintf(stderr,"Could not save preferences\n");
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
2011-12-14 04:34:56 +00:00
|
|
|
|
2012-05-02 17:03:48 +00:00
|
|
|
void subsurface_close_conf(void)
|
|
|
|
{
|
|
|
|
/* Nothing */
|
|
|
|
}
|
|
|
|
|
2013-05-22 12:35:42 +00:00
|
|
|
#if USE_GTK_UI
|
2012-11-04 18:51:03 +00:00
|
|
|
int subsurface_fill_device_list(GtkListStore *store)
|
2011-12-14 04:34:56 +00:00
|
|
|
{
|
2012-10-26 22:52:39 +00:00
|
|
|
int i = 0;
|
|
|
|
int index = -1;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GDir *dev;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
dev = g_dir_open("/dev", 0, NULL);
|
|
|
|
while (dev && (name = g_dir_read_name(dev)) != NULL) {
|
2013-09-16 21:04:42 +00:00
|
|
|
if (strstr(name, "usbserial") ||
|
|
|
|
(strstr(name, "SerialPort") && strstr(name, "cu"))) {
|
2012-10-26 22:52:39 +00:00
|
|
|
int len = strlen(name) + 6;
|
|
|
|
char *devicename = malloc(len);
|
|
|
|
snprintf(devicename, len, "/dev/%s", name);
|
|
|
|
gtk_list_store_append(store, &iter);
|
|
|
|
gtk_list_store_set(store, &iter,
|
|
|
|
0, devicename, -1);
|
|
|
|
if (is_default_dive_computer_device(devicename))
|
|
|
|
index = i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dev)
|
|
|
|
g_dir_close(dev);
|
|
|
|
dev = g_dir_open("/Volumes", 0, NULL);
|
|
|
|
while (dev && (name = g_dir_read_name(dev)) != NULL) {
|
|
|
|
if (strstr(name, "UEMISSDA")) {
|
|
|
|
int len = strlen(name) + 10;
|
|
|
|
char *devicename = malloc(len);
|
|
|
|
snprintf(devicename, len, "/Volumes/%s", name);
|
|
|
|
gtk_list_store_append(store, &iter);
|
|
|
|
gtk_list_store_set(store, &iter,
|
|
|
|
0, devicename, -1);
|
|
|
|
if (is_default_dive_computer_device(devicename))
|
|
|
|
index = i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dev)
|
|
|
|
g_dir_close(dev);
|
|
|
|
if (i == 0) {
|
|
|
|
/* if we can't find anything, use the default */
|
|
|
|
gtk_list_store_append(store, &iter);
|
|
|
|
gtk_list_store_set(store, &iter,
|
|
|
|
0, "/dev/tty.SLAB_USBtoUART", -1);
|
|
|
|
if (is_default_dive_computer_device("/dev/tty.SLAB_USBtoUART"))
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
return index;
|
2011-12-14 04:34:56 +00:00
|
|
|
}
|
2013-05-22 12:35:42 +00:00
|
|
|
#endif
|
2011-12-28 23:57:36 +00:00
|
|
|
|
|
|
|
const char *subsurface_icon_name()
|
|
|
|
{
|
2013-03-04 01:53:43 +00:00
|
|
|
static char path[PATH_MAX];
|
2012-01-02 16:26:24 +00:00
|
|
|
|
2013-05-03 20:13:14 +00:00
|
|
|
#if USE_GTK_UI
|
2013-03-04 01:53:43 +00:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", gtkosx_application_get_resource_path(), ICON_NAME);
|
2013-05-03 20:13:14 +00:00
|
|
|
#else
|
|
|
|
/* need Qt path */
|
|
|
|
#endif
|
2012-01-02 16:26:24 +00:00
|
|
|
return path;
|
2011-12-28 23:57:36 +00:00
|
|
|
}
|
2012-01-01 21:41:47 +00:00
|
|
|
|
2013-01-12 01:07:22 +00:00
|
|
|
const char *system_default_filename(void)
|
2012-09-09 16:06:44 +00:00
|
|
|
{
|
2013-01-12 01:07:22 +00:00
|
|
|
const char *home, *user;
|
|
|
|
char *buffer;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
home = g_get_home_dir();
|
|
|
|
user = g_get_user_name();
|
|
|
|
len = strlen(home) + strlen(user) + 45;
|
|
|
|
buffer = malloc(len);
|
|
|
|
snprintf(buffer, len, "%s/Library/Application Support/Subsurface/%s.xml", home, user);
|
|
|
|
return buffer;
|
2012-09-09 16:06:44 +00:00
|
|
|
}
|
|
|
|
|
2012-10-18 21:30:45 +00:00
|
|
|
const char *subsurface_gettext_domainpath(char *argv0)
|
2012-10-15 12:44:01 +00:00
|
|
|
{
|
2012-10-18 21:30:45 +00:00
|
|
|
/* on a Mac we ignore the argv0 argument and instead use the resource_path
|
|
|
|
* to figure out where to find the translation files */
|
2013-05-03 20:13:14 +00:00
|
|
|
#if USE_GTK_UI
|
2013-03-04 01:53:43 +00:00
|
|
|
static char buffer[PATH_MAX];
|
2013-01-17 18:52:30 +00:00
|
|
|
const char *resource_path = gtkosx_application_get_resource_path();
|
2012-10-17 04:24:02 +00:00
|
|
|
if (resource_path) {
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/share/locale", resource_path);
|
|
|
|
return buffer;
|
2012-10-15 12:44:01 +00:00
|
|
|
}
|
2013-05-03 20:13:14 +00:00
|
|
|
#endif /* USE_GTK_UI */
|
2012-10-17 04:24:02 +00:00
|
|
|
return "./share/locale";
|
2012-10-15 12:44:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 20:13:14 +00:00
|
|
|
#if USE_GTK_UI
|
2012-09-12 16:18:56 +00:00
|
|
|
static void show_main_window(GtkWidget *w, gpointer data)
|
|
|
|
{
|
|
|
|
gtk_widget_show(main_window);
|
|
|
|
gtk_window_present(GTK_WINDOW(main_window));
|
|
|
|
}
|
|
|
|
|
2012-01-01 21:41:47 +00:00
|
|
|
void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
|
2012-01-03 04:49:10 +00:00
|
|
|
GtkWidget *vbox, GtkUIManager *ui_manager)
|
2012-01-01 21:41:47 +00:00
|
|
|
{
|
2012-01-03 04:49:10 +00:00
|
|
|
GtkWidget *menu_item, *sep;
|
2013-03-04 01:53:43 +00:00
|
|
|
static char path[PATH_MAX];
|
2013-02-09 09:36:02 +00:00
|
|
|
|
2013-03-04 01:53:43 +00:00
|
|
|
snprintf(path, sizeof(path), "%s/xslt", gtkosx_application_get_resource_path());
|
2013-02-09 09:36:02 +00:00
|
|
|
setenv("SUBSURFACE_XSLT_PATH", path, TRUE);
|
2012-01-03 04:49:10 +00:00
|
|
|
|
2012-01-01 21:41:47 +00:00
|
|
|
g_object_set(G_OBJECT(settings), "gtk-font-name", UI_FONT, NULL);
|
|
|
|
|
2013-01-17 18:52:30 +00:00
|
|
|
osx_app = g_object_new(GTKOSX_TYPE_APPLICATION, NULL);
|
2012-01-01 21:41:47 +00:00
|
|
|
gtk_widget_hide (menubar);
|
2013-01-17 18:52:30 +00:00
|
|
|
gtkosx_application_set_menu_bar(osx_app, GTK_MENU_SHELL(menubar));
|
2012-01-03 19:18:04 +00:00
|
|
|
|
2012-09-18 12:08:23 +00:00
|
|
|
sep = gtk_ui_manager_get_widget(ui_manager, "/MainMenu/FileMenu/Separator3");
|
2012-08-18 02:52:49 +00:00
|
|
|
if (sep)
|
|
|
|
gtk_widget_destroy(sep);
|
2012-01-03 19:18:04 +00:00
|
|
|
|
2012-01-03 04:49:10 +00:00
|
|
|
menu_item = gtk_ui_manager_get_widget(ui_manager, "/MainMenu/FileMenu/Quit");
|
|
|
|
gtk_widget_hide (menu_item);
|
|
|
|
menu_item = gtk_ui_manager_get_widget(ui_manager, "/MainMenu/Help/About");
|
2013-01-17 18:52:30 +00:00
|
|
|
gtkosx_application_insert_app_menu_item(osx_app, menu_item, 0);
|
2012-01-03 19:18:04 +00:00
|
|
|
|
2012-01-03 04:49:10 +00:00
|
|
|
sep = gtk_separator_menu_item_new();
|
|
|
|
g_object_ref(sep);
|
2013-01-17 18:52:30 +00:00
|
|
|
gtkosx_application_insert_app_menu_item (osx_app, sep, 1);
|
2012-01-03 19:18:04 +00:00
|
|
|
|
2012-01-03 04:49:10 +00:00
|
|
|
menu_item = gtk_ui_manager_get_widget(ui_manager, "/MainMenu/FileMenu/Preferences");
|
2013-01-17 18:52:30 +00:00
|
|
|
gtkosx_application_insert_app_menu_item(osx_app, menu_item, 2);
|
2012-01-03 19:18:04 +00:00
|
|
|
|
2012-01-03 04:49:10 +00:00
|
|
|
sep = gtk_separator_menu_item_new();
|
|
|
|
g_object_ref(sep);
|
2013-01-17 18:52:30 +00:00
|
|
|
gtkosx_application_insert_app_menu_item (osx_app, sep, 3);
|
2012-01-03 19:18:04 +00:00
|
|
|
|
2013-01-17 18:52:30 +00:00
|
|
|
gtkosx_application_set_use_quartz_accelerators(osx_app, TRUE);
|
2012-09-12 16:18:56 +00:00
|
|
|
g_signal_connect(osx_app,"NSApplicationDidBecomeActive",G_CALLBACK(show_main_window),NULL);
|
2012-12-09 23:19:17 +00:00
|
|
|
g_signal_connect(osx_app, "NSApplicationBlockTermination", G_CALLBACK(on_delete), NULL);
|
2012-09-12 16:18:56 +00:00
|
|
|
|
2013-01-17 18:52:30 +00:00
|
|
|
gtkosx_application_ready(osx_app);
|
2012-01-01 21:41:47 +00:00
|
|
|
}
|
2013-05-03 20:13:14 +00:00
|
|
|
#endif /* UES_GTK_UI */
|
2012-10-04 00:44:47 +00:00
|
|
|
|
|
|
|
void subsurface_command_line_init(gint *argc, gchar ***argv)
|
|
|
|
{
|
|
|
|
/* this is a no-op */
|
|
|
|
}
|
|
|
|
|
|
|
|
void subsurface_command_line_exit(gint *argc, gchar ***argv)
|
|
|
|
{
|
|
|
|
/* this is a no-op */
|
|
|
|
}
|
2012-10-19 23:31:06 +00:00
|
|
|
|
|
|
|
gboolean subsurface_os_feature_available(os_feature_t f)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-01-14 23:20:29 +00:00
|
|
|
|
|
|
|
gboolean subsurface_launch_for_uri(const char* uri)
|
|
|
|
{
|
2013-02-25 21:22:33 +00:00
|
|
|
CFURLRef urlref = CFURLCreateWithBytes(NULL, uri, strlen(uri), kCFStringEncodingMacRoman, NULL);
|
|
|
|
OSStatus status = LSOpenCFURLRef(urlref, NULL);
|
|
|
|
if (status)
|
2013-01-14 23:20:29 +00:00
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-09-16 21:04:42 +00:00
|
|
|
|
|
|
|
int enumerate_devices (device_callback_t callback, void *userdata)
|
|
|
|
{
|
|
|
|
int index = -1;
|
|
|
|
DIR *dp = NULL;
|
|
|
|
struct dirent *ep = NULL;
|
|
|
|
size_t i;
|
|
|
|
const char *dirname = "/dev";
|
|
|
|
const char *patterns[] = {
|
|
|
|
"tty.*",
|
|
|
|
"usbserial",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
dp = opendir (dirname);
|
|
|
|
if (dp == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ep = readdir (dp)) != NULL) {
|
|
|
|
for (i = 0; patterns[i] != NULL; ++i) {
|
|
|
|
if (fnmatch (patterns[i], ep->d_name, 0) == 0) {
|
|
|
|
char filename[1024];
|
2013-09-17 09:18:52 +00:00
|
|
|
int n = snprintf (filename, sizeof (filename), "%s/%s", dirname, ep->d_name);
|
2013-09-16 21:04:42 +00:00
|
|
|
if (n >= sizeof (filename)) {
|
|
|
|
closedir (dp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
callback (filename, userdata);
|
|
|
|
if (is_default_dive_computer_device(filename))
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: list UEMIS mount point from /proc/mounts
|
|
|
|
|
|
|
|
closedir (dp);
|
|
|
|
return index;
|
|
|
|
}
|