2017-04-27 18:18:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
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"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "subsurface-string.h"
|
2022-03-12 09:44:37 +00:00
|
|
|
#include "device.h"
|
|
|
|
#include "libdivecomputer.h"
|
2011-11-24 06:56:57 +00:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2016-03-06 14:55:24 +00:00
|
|
|
#if !defined(__IPHONE_5_0)
|
2013-02-28 12:19:27 +00:00
|
|
|
#include <CoreServices/CoreServices.h>
|
2016-03-06 14:55:24 +00:00
|
|
|
#endif
|
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>
|
2013-12-19 13:00:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2014-11-13 17:36:08 +00:00
|
|
|
#include <unistd.h>
|
2019-08-11 03:56:30 +00:00
|
|
|
#include <zip.h>
|
|
|
|
#include <sys/stat.h>
|
2023-04-29 09:43:17 +00:00
|
|
|
#include <string>
|
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 */
|
2014-02-28 04:09:57 +00:00
|
|
|
#define CFSTR_VAR(_var) CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, \
|
|
|
|
(_var), kCFStringEncodingMacRoman, \
|
|
|
|
kCFAllocatorNull)
|
2011-11-24 06:56:57 +00:00
|
|
|
|
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
|
|
|
|
2023-04-29 09:43:17 +00:00
|
|
|
static std::string system_default_path()
|
|
|
|
{
|
|
|
|
std::string home(getenv("HOME"));
|
|
|
|
return home + "/Library/Application Support/Subsurface";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string make_default_filename()
|
|
|
|
{
|
|
|
|
std::string user = getenv("LOGNAME");
|
|
|
|
if (user.empty())
|
|
|
|
user = "username";
|
|
|
|
return system_default_path() + "/" + user + ".xml";
|
|
|
|
}
|
|
|
|
|
2014-08-22 22:22:02 +00:00
|
|
|
const char mac_system_divelist_default_font[] = "Arial";
|
|
|
|
const char *system_divelist_default_font = mac_system_divelist_default_font;
|
2014-08-27 13:00:10 +00:00
|
|
|
double system_divelist_default_font_size = -1.0;
|
2012-01-01 21:41:47 +00:00
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_OS_pref_setup()
|
2014-08-22 22:22:02 +00:00
|
|
|
{
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
|
2023-04-29 09:43:17 +00:00
|
|
|
bool subsurface_ignore_font(const char *)
|
2014-08-27 13:00:10 +00:00
|
|
|
{
|
|
|
|
// there are no old default fonts to ignore
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
const char *system_default_directory()
|
2015-10-06 10:10:17 +00:00
|
|
|
{
|
2023-04-29 09:43:17 +00:00
|
|
|
static const std::string path = system_default_path();
|
|
|
|
return path.c_str();
|
2015-10-06 10:10:17 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
const char *system_default_filename()
|
2015-10-06 10:10:17 +00:00
|
|
|
{
|
2023-04-29 09:43:17 +00:00
|
|
|
static const std::string fn = make_default_filename();
|
|
|
|
return fn.c_str();
|
2015-10-06 10:10:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 17:32:14 +00:00
|
|
|
int enumerate_devices(device_callback_t callback, void *userdata, unsigned int transport)
|
2013-09-16 21:04:42 +00:00
|
|
|
{
|
2014-05-18 05:13:23 +00:00
|
|
|
int index = -1, entries = 0;
|
2013-09-16 21:04:42 +00:00
|
|
|
DIR *dp = NULL;
|
|
|
|
struct dirent *ep = NULL;
|
2018-08-27 17:32:14 +00:00
|
|
|
if (transport & DC_TRANSPORT_SERIAL) {
|
2018-09-24 13:23:59 +00:00
|
|
|
// on Mac we always support FTDI now
|
|
|
|
callback("FTDI", userdata);
|
|
|
|
|
2014-05-18 04:29:40 +00:00
|
|
|
const char *dirname = "/dev";
|
|
|
|
const char *patterns[] = {
|
|
|
|
"tty.*",
|
2023-04-29 16:02:13 +00:00
|
|
|
"usbserial"
|
2014-05-18 04:29:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
dp = opendir(dirname);
|
|
|
|
if (dp == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ep = readdir(dp)) != NULL) {
|
2023-04-29 16:02:13 +00:00
|
|
|
for (const char *pattern: patterns) {
|
|
|
|
if (fnmatch(pattern, ep->d_name, 0) == 0) {
|
|
|
|
std::string filename = std::string(dirname) + "/" + ep->d_name;
|
|
|
|
callback(filename.c_str(), userdata);
|
|
|
|
if (is_default_dive_computer_device(filename.c_str()))
|
2014-05-18 05:13:23 +00:00
|
|
|
index = entries;
|
|
|
|
entries++;
|
2014-05-18 04:29:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
2013-09-16 21:04:42 +00:00
|
|
|
}
|
2018-08-27 17:32:14 +00:00
|
|
|
if (transport & DC_TRANSPORT_USBSTORAGE) {
|
2014-05-18 04:29:40 +00:00
|
|
|
const char *dirname = "/Volumes";
|
2014-05-18 05:13:23 +00:00
|
|
|
int num_uemis = 0;
|
2014-05-18 04:29:40 +00:00
|
|
|
dp = opendir(dirname);
|
|
|
|
if (dp == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-16 21:04:42 +00:00
|
|
|
|
2014-05-18 04:29:40 +00:00
|
|
|
while ((ep = readdir(dp)) != NULL) {
|
2018-08-27 15:27:07 +00:00
|
|
|
if (fnmatch("UEMISSDA", ep->d_name, 0) == 0 ||
|
|
|
|
fnmatch("GARMIN", ep->d_name, 0) == 0) {
|
2013-09-16 21:04:42 +00:00
|
|
|
char filename[1024];
|
2014-02-28 04:09:57 +00:00
|
|
|
int n = snprintf(filename, sizeof(filename), "%s/%s", dirname, ep->d_name);
|
2016-03-24 00:25:11 +00:00
|
|
|
if (n >= (int)sizeof(filename)) {
|
2014-02-28 04:09:57 +00:00
|
|
|
closedir(dp);
|
2013-09-16 21:04:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-02-28 04:09:57 +00:00
|
|
|
callback(filename, userdata);
|
2013-09-16 21:04:42 +00:00
|
|
|
if (is_default_dive_computer_device(filename))
|
2014-05-18 05:13:23 +00:00
|
|
|
index = entries;
|
|
|
|
entries++;
|
|
|
|
num_uemis++;
|
2013-09-16 21:04:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-18 04:29:40 +00:00
|
|
|
closedir(dp);
|
2014-05-18 05:13:23 +00:00
|
|
|
if (num_uemis == 1 && entries == 1) /* if we find exactly one entry and that's a Uemis, select it */
|
|
|
|
index = 0;
|
2013-09-16 21:04:42 +00:00
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
2013-12-19 13:00:50 +00:00
|
|
|
|
|
|
|
/* NOP wrappers to comform with windows.c */
|
2014-02-16 21:25:02 +00:00
|
|
|
int subsurface_rename(const char *path, const char *newpath)
|
|
|
|
{
|
|
|
|
return rename(path, newpath);
|
|
|
|
}
|
|
|
|
|
2013-12-19 13:00:50 +00:00
|
|
|
int subsurface_open(const char *path, int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
return open(path, oflags, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *subsurface_fopen(const char *path, const char *mode)
|
|
|
|
{
|
|
|
|
return fopen(path, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *subsurface_opendir(const char *path)
|
|
|
|
{
|
|
|
|
return (void *)opendir(path);
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:36:08 +00:00
|
|
|
int subsurface_access(const char *path, int mode)
|
|
|
|
{
|
|
|
|
return access(path, mode);
|
|
|
|
}
|
|
|
|
|
2017-02-24 09:14:06 +00:00
|
|
|
int subsurface_stat(const char* path, struct stat* buf)
|
|
|
|
{
|
|
|
|
return stat(path, buf);
|
|
|
|
}
|
|
|
|
|
2013-12-19 13:00:50 +00:00
|
|
|
struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
|
|
|
|
{
|
|
|
|
return zip_open(path, flags, errorp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int subsurface_zip_close(struct zip *zip)
|
|
|
|
{
|
|
|
|
return zip_close(zip);
|
|
|
|
}
|
2014-03-25 14:55:56 +00:00
|
|
|
|
|
|
|
/* win32 console */
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_console_init()
|
2014-03-25 14:55:56 +00:00
|
|
|
{
|
|
|
|
/* NOP */
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_console_exit()
|
2014-03-25 14:55:56 +00:00
|
|
|
{
|
|
|
|
/* NOP */
|
|
|
|
}
|
2016-03-25 08:21:45 +00:00
|
|
|
|
|
|
|
bool subsurface_user_is_root()
|
|
|
|
{
|
2018-02-17 20:21:16 +00:00
|
|
|
return geteuid() == 0;
|
2016-03-25 08:21:45 +00:00
|
|
|
}
|