2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
23 #include "compose.hpp"
25 #include "dcpomatic_log.h"
27 #include "exceptions.h"
28 #include <dcp/raw_convert.h>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/regex.hpp>
32 #if BOOST_VERSION >= 106100
33 #include <boost/dll/runtime_symbol_info.hpp>
35 #include <ApplicationServices/ApplicationServices.h>
36 #include <sys/sysctl.h>
37 #include <mach-o/dyld.h>
38 #include <IOKit/pwr_mgt/IOPMLib.h>
39 #include <IOKit/storage/IOMedia.h>
40 #include <DiskArbitration/DADisk.h>
41 #include <DiskArbitration/DiskArbitration.h>
42 #include <CoreFoundation/CFURL.h>
43 #include <sys/types.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
61 using std::runtime_error;
63 using std::shared_ptr;
64 using boost::optional;
68 /** @return A string of CPU information (model name etc.) */
75 size_t N = sizeof (buffer);
76 if (sysctlbyname("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
84 boost::filesystem::path
85 directory_containing_executable ()
87 return boost::filesystem::canonical(boost::dll::program_location()).parent_path();
91 boost::filesystem::path
94 return directory_containing_executable().parent_path() / "Resources";
98 boost::filesystem::path
99 libdcp_resources_path ()
101 return resources_path();
106 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
108 auto path = directory_containing_executable () / "ffprobe";
110 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
111 LOG_GENERAL (N_("Probing with %1"), ffprobe);
112 system (ffprobe.c_str ());
117 list<pair<string, string>>
124 boost::filesystem::path
127 return directory_containing_executable() / "openssl";
131 #ifdef DCPOMATIC_DISK
132 /* Note: this isn't actually used at the moment as the disk writer is started as a service */
133 boost::filesystem::path
136 return directory_containing_executable() / "dcpomatic2_disk_writer";
150 boost::mutex::scoped_lock lm (_mutex);
151 IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
157 boost::mutex::scoped_lock lm (_mutex);
158 IOPMAssertionRelease (_assertion_id);
163 start_tool (string executable, string app)
165 auto exe_path = directory_containing_executable();
166 exe_path = exe_path.parent_path(); // Contents
167 exe_path = exe_path.parent_path(); // DCP-o-matic 2.app
168 exe_path = exe_path.parent_path(); // Applications
170 exe_path /= "Contents";
172 exe_path /= executable;
176 LOG_GENERAL ("start_tool %1 %2 with path %3", executable, app, exe_path.string());
177 int const r = system (exe_path.string().c_str());
178 exit (WEXITSTATUS (r));
179 } else if (pid == -1) {
180 LOG_ERROR_NC("Fork failed in start_tool");
186 start_batch_converter ()
188 start_tool ("dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
195 start_tool ("dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
199 static optional<string>
200 get_vendor (CFDictionaryRef& description)
202 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceVendorKey);
207 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
213 boost::algorithm::trim (s);
218 static optional<string>
219 get_model (CFDictionaryRef& description)
221 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceModelKey);
226 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
232 boost::algorithm::trim (s);
237 static optional<OSXMediaPath>
238 analyse_media_path (CFDictionaryRef& description)
240 using namespace boost::algorithm;
242 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionMediaPathKey);
244 LOG_DISK_NC("There is no MediaPathKey (no dictionary value)");
248 auto path_key_cstr = CFStringGetCStringPtr((CFStringRef) str, kCFStringEncodingUTF8);
249 if (!path_key_cstr) {
250 LOG_DISK_NC("There is no MediaPathKey (no cstring)");
254 string path(path_key_cstr);
255 LOG_DISK("MediaPathKey is %1", path);
256 return analyse_osx_media_path (path);
261 is_whole_drive (DADiskRef& disk)
263 io_service_t service = DADiskCopyIOMedia (disk);
264 CFTypeRef whole_media_ref = IORegistryEntryCreateCFProperty (service, CFSTR(kIOMediaWholeKey), kCFAllocatorDefault, 0);
265 bool whole_media = false;
266 if (whole_media_ref) {
267 whole_media = CFBooleanGetValue((CFBooleanRef) whole_media_ref);
268 CFRelease (whole_media_ref);
270 IOObjectRelease (service);
275 static optional<boost::filesystem::path>
276 mount_point (CFDictionaryRef& description)
278 auto volume_path_key = (CFURLRef) CFDictionaryGetValue (description, kDADiskDescriptionVolumePathKey);
279 if (!volume_path_key) {
283 char mount_path_buffer[1024];
284 if (!CFURLGetFileSystemRepresentation(volume_path_key, false, (UInt8 *) mount_path_buffer, sizeof(mount_path_buffer))) {
287 return boost::filesystem::path(mount_path_buffer);
291 /* Here follows some rather intricate and (probably) fragile code to find the list of available
292 * "real" drives on macOS that we might want to write a DCP to.
294 * We use the Disk Arbitration framework to give us a series of mount_points (/dev/disk0, /dev/disk1,
295 * /dev/disk1s1 and so on) and we use the API to gather useful information about these mount_points into
296 * a vector of Disk structs.
298 * Then we read the Disks that we found and try to derive a list of drives that we should offer to the
299 * user, with details of whether those drives are currently mounted or not.
301 * At the basic level we find the "disk"-level mount_points, looking at whether any of their partitions are mounted.
303 * This is complicated enormously by recent-ish macOS versions' habit of making `synthesized' volumes which
304 * reflect data in `real' partitions. So, for example, we might have a real (physical) drive /dev/disk2 with
305 * a partition /dev/disk2s2 whose content is made into a synthesized /dev/disk3, itself containing some partitions
306 * which are mounted. /dev/disk2s2 is not considered to be mounted, in this case. So we need to know that
307 * disk2s2 is related to disk3 so we can consider disk2s2 as mounted if any parts of disk3 are. In order to do
308 * this I am taking the first two parts of the IODeviceTree and seeing if they exist anywhere in a
309 * IOService identifier. If they do, I am assuming the IOService device is on the matching IODeviceTree device.
311 * Lots of this is guesswork and may be broken. In my defence the documentation that I have been able to
312 * unearth is, to put it impolitely, crap.
316 disk_appeared (DADiskRef disk, void* context)
318 auto bsd_name = DADiskGetBSDName (disk);
320 LOG_DISK_NC("Disk with no BSDName appeared");
323 LOG_DISK("%1 appeared", bsd_name);
327 this_disk.device = string("/dev/") + bsd_name;
328 LOG_DISK("Device is %1", this_disk.device);
330 CFDictionaryRef description = DADiskCopyDescription (disk);
332 this_disk.vendor = get_vendor (description);
333 this_disk.model = get_model (description);
334 LOG_DISK("Vendor/model: %1 %2", this_disk.vendor.get_value_or("[none]"), this_disk.model.get_value_or("[none]"));
336 auto media_path = analyse_media_path (description);
338 LOG_DISK("Finding media path for %1 failed", bsd_name);
342 this_disk.media_path = *media_path;
343 this_disk.whole = is_whole_drive (disk);
344 auto mp = mount_point (description);
346 this_disk.mount_points.push_back (*mp);
350 "%1 %2 mounted at %3",
351 this_disk.media_path.real ? "Real" : "Synth",
352 this_disk.whole ? "whole" : "part",
353 mp ? mp->string() : "[nowhere]"
356 auto media_size_cstr = CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey);
357 if (!media_size_cstr) {
358 LOG_DISK_NC("Could not read media size");
362 CFNumberGetValue ((CFNumberRef) media_size_cstr, kCFNumberLongType, &this_disk.size);
363 CFRelease (description);
365 reinterpret_cast<vector<OSXDisk>*>(context)->push_back(this_disk);
372 using namespace boost::algorithm;
373 vector<OSXDisk> disks;
375 auto session = DASessionCreate(kCFAllocatorDefault);
380 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &disks);
381 auto run_loop = CFRunLoopGetCurrent ();
382 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
383 CFRunLoopStop (run_loop);
384 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
385 DAUnregisterCallback(session, (void *) disk_appeared, &disks);
388 return osx_disks_to_drives (disks);
392 boost::filesystem::path
393 config_path (optional<string> version)
395 boost::filesystem::path p;
396 p /= g_get_home_dir ();
399 p /= "com.dcpomatic";
408 void done_callback(DADiskRef, DADissenterRef dissenter, void* context)
410 LOG_DISK_NC("Unmount finished");
411 bool* success = reinterpret_cast<bool*> (context);
413 LOG_DISK("Error: %1", DADissenterGetStatus(dissenter));
416 LOG_DISK_NC("Successful");
425 LOG_DISK_NC("Unmount operation started");
427 auto session = DASessionCreate(kCFAllocatorDefault);
432 auto disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, _device.c_str());
436 LOG_DISK("Requesting unmount of %1 from %2", _device, thread_id());
437 bool success = false;
438 DADiskUnmount(disk, kDADiskUnmountOptionWhole, &done_callback, &success);
441 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
442 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
443 CFRunLoopStop (run_loop);
444 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.5, 0);
447 LOG_DISK_NC("End of unmount");
453 disk_write_finished ()
460 make_foreground_application ()
462 ProcessSerialNumber serial;
463 LIBDCP_DISABLE_WARNINGS
464 GetCurrentProcess (&serial);
465 LIBDCP_ENABLE_WARNINGS
466 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
471 show_in_file_manager (boost::filesystem::path, boost::filesystem::path select)
473 int r = system (String::compose("open -R \"%1\"", select.string()).c_str());
474 return static_cast<bool>(WEXITSTATUS(r));