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>
31 #include <libavformat/avio.h>
33 #include <boost/algorithm/string.hpp>
34 #include <boost/regex.hpp>
35 #if BOOST_VERSION >= 106100
36 #include <boost/dll/runtime_symbol_info.hpp>
38 #include <ApplicationServices/ApplicationServices.h>
39 #include <sys/sysctl.h>
40 #include <mach-o/dyld.h>
41 #include <IOKit/pwr_mgt/IOPMLib.h>
42 #include <IOKit/storage/IOMedia.h>
43 #include <DiskArbitration/DADisk.h>
44 #include <DiskArbitration/DiskArbitration.h>
45 #include <CoreFoundation/CFURL.h>
46 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
64 using std::runtime_error;
66 using std::shared_ptr;
67 using boost::optional;
71 /** @param s Number of seconds to sleep for */
73 dcpomatic_sleep_seconds (int s)
80 dcpomatic_sleep_milliseconds (int ms)
86 /** @return A string of CPU information (model name etc.) */
93 size_t N = sizeof (buffer);
94 if (sysctlbyname("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
102 boost::filesystem::path
103 directory_containing_executable ()
105 return boost::filesystem::canonical(boost::dll::program_location()).parent_path();
109 boost::filesystem::path
112 return directory_containing_executable().parent_path() / "Resources";
116 boost::filesystem::path
117 libdcp_resources_path ()
119 return resources_path();
124 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
126 auto path = directory_containing_executable () / "ffprobe";
128 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
129 LOG_GENERAL (N_("Probing with %1"), ffprobe);
130 system (ffprobe.c_str ());
135 list<pair<string, string>>
142 boost::filesystem::path
145 return directory_containing_executable() / "openssl";
149 #ifdef DCPOMATIC_DISK
150 /* Note: this isn't actually used at the moment as the disk writer is started as a service */
151 boost::filesystem::path
154 return directory_containing_executable() / "dcpomatic2_disk_writer";
159 /* Apparently there is no way to create an ofstream using a UTF-8
160 filename under Windows. We are hence reduced to using fopen
164 fopen_boost (boost::filesystem::path p, string t)
166 return fopen (p.c_str(), t.c_str());
171 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
173 return fseek (stream, offset, whence);
186 boost::mutex::scoped_lock lm (_mutex);
187 IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
193 boost::mutex::scoped_lock lm (_mutex);
194 IOPMAssertionRelease (_assertion_id);
199 start_tool (string executable, string app)
201 auto exe_path = directory_containing_executable();
202 exe_path = exe_path.parent_path(); // Contents
203 exe_path = exe_path.parent_path(); // DCP-o-matic 2.app
204 exe_path = exe_path.parent_path(); // Applications
206 exe_path /= "Contents";
208 exe_path /= executable;
212 LOG_GENERAL ("start_tool %1 %2 with path %3", executable, app, exe_path.string());
213 int const r = system (exe_path.string().c_str());
214 exit (WEXITSTATUS (r));
215 } else if (pid == -1) {
216 LOG_ERROR_NC("Fork failed in start_tool");
222 start_batch_converter ()
224 start_tool ("dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
231 start_tool ("dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
238 return (uint64_t) pthread_self ();
243 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
245 return avio_open (s, file.c_str(), flags);
249 boost::filesystem::path
252 return getenv("HOME");
256 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
260 /* I'm assuming nobody does this on OS X */
265 static optional<string>
266 get_vendor (CFDictionaryRef& description)
268 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceVendorKey);
273 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
279 boost::algorithm::trim (s);
284 static optional<string>
285 get_model (CFDictionaryRef& description)
287 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceModelKey);
292 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
298 boost::algorithm::trim (s);
303 static optional<OSXMediaPath>
304 analyse_media_path (CFDictionaryRef& description)
306 using namespace boost::algorithm;
308 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionMediaPathKey);
310 LOG_DISK_NC("There is no MediaPathKey (no dictionary value)");
314 auto path_key_cstr = CFStringGetCStringPtr((CFStringRef) str, kCFStringEncodingUTF8);
315 if (!path_key_cstr) {
316 LOG_DISK_NC("There is no MediaPathKey (no cstring)");
320 string path(path_key_cstr);
321 LOG_DISK("MediaPathKey is %1", path);
322 return analyse_osx_media_path (path);
327 is_whole_drive (DADiskRef& disk)
329 io_service_t service = DADiskCopyIOMedia (disk);
330 CFTypeRef whole_media_ref = IORegistryEntryCreateCFProperty (service, CFSTR(kIOMediaWholeKey), kCFAllocatorDefault, 0);
331 bool whole_media = false;
332 if (whole_media_ref) {
333 whole_media = CFBooleanGetValue((CFBooleanRef) whole_media_ref);
334 CFRelease (whole_media_ref);
336 IOObjectRelease (service);
341 static optional<boost::filesystem::path>
342 mount_point (CFDictionaryRef& description)
344 auto volume_path_key = (CFURLRef) CFDictionaryGetValue (description, kDADiskDescriptionVolumePathKey);
345 if (!volume_path_key) {
349 char mount_path_buffer[1024];
350 if (!CFURLGetFileSystemRepresentation(volume_path_key, false, (UInt8 *) mount_path_buffer, sizeof(mount_path_buffer))) {
353 return boost::filesystem::path(mount_path_buffer);
357 /* Here follows some rather intricate and (probably) fragile code to find the list of available
358 * "real" drives on macOS that we might want to write a DCP to.
360 * We use the Disk Arbitration framework to give us a series of mount_points (/dev/disk0, /dev/disk1,
361 * /dev/disk1s1 and so on) and we use the API to gather useful information about these mount_points into
362 * a vector of Disk structs.
364 * Then we read the Disks that we found and try to derive a list of drives that we should offer to the
365 * user, with details of whether those drives are currently mounted or not.
367 * At the basic level we find the "disk"-level mount_points, looking at whether any of their partitions are mounted.
369 * This is complicated enormously by recent-ish macOS versions' habit of making `synthesized' volumes which
370 * reflect data in `real' partitions. So, for example, we might have a real (physical) drive /dev/disk2 with
371 * a partition /dev/disk2s2 whose content is made into a synthesized /dev/disk3, itself containing some partitions
372 * which are mounted. /dev/disk2s2 is not considered to be mounted, in this case. So we need to know that
373 * disk2s2 is related to disk3 so we can consider disk2s2 as mounted if any parts of disk3 are. In order to do
374 * this I am taking the first two parts of the IODeviceTree and seeing if they exist anywhere in a
375 * IOService identifier. If they do, I am assuming the IOService device is on the matching IODeviceTree device.
377 * Lots of this is guesswork and may be broken. In my defence the documentation that I have been able to
378 * unearth is, to put it impolitely, crap.
382 disk_appeared (DADiskRef disk, void* context)
384 auto bsd_name = DADiskGetBSDName (disk);
386 LOG_DISK_NC("Disk with no BSDName appeared");
389 LOG_DISK("%1 appeared", bsd_name);
393 this_disk.device = string("/dev/") + bsd_name;
394 LOG_DISK("Device is %1", this_disk.device);
396 CFDictionaryRef description = DADiskCopyDescription (disk);
398 this_disk.vendor = get_vendor (description);
399 this_disk.model = get_model (description);
400 LOG_DISK("Vendor/model: %1 %2", this_disk.vendor.get_value_or("[none]"), this_disk.model.get_value_or("[none]"));
402 auto media_path = analyse_media_path (description);
404 LOG_DISK("Finding media path for %1 failed", bsd_name);
408 this_disk.media_path = *media_path;
409 this_disk.whole = is_whole_drive (disk);
410 auto mp = mount_point (description);
412 this_disk.mount_points.push_back (*mp);
416 "%1 %2 mounted at %3",
417 this_disk.media_path.real ? "Real" : "Synth",
418 this_disk.whole ? "whole" : "part",
419 mp ? mp->string() : "[nowhere]"
422 auto media_size_cstr = CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey);
423 if (!media_size_cstr) {
424 LOG_DISK_NC("Could not read media size");
428 CFNumberGetValue ((CFNumberRef) media_size_cstr, kCFNumberLongType, &this_disk.size);
429 CFRelease (description);
431 reinterpret_cast<vector<OSXDisk>*>(context)->push_back(this_disk);
438 using namespace boost::algorithm;
439 vector<OSXDisk> disks;
441 auto session = DASessionCreate(kCFAllocatorDefault);
446 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &disks);
447 auto run_loop = CFRunLoopGetCurrent ();
448 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
449 CFRunLoopStop (run_loop);
450 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
451 DAUnregisterCallback(session, (void *) disk_appeared, &disks);
454 return osx_disks_to_drives (disks);
458 boost::filesystem::path
459 config_path (optional<string> version)
461 boost::filesystem::path p;
462 p /= g_get_home_dir ();
465 p /= "com.dcpomatic";
474 void done_callback(DADiskRef, DADissenterRef dissenter, void* context)
476 LOG_DISK_NC("Unmount finished");
477 bool* success = reinterpret_cast<bool*> (context);
479 LOG_DISK("Error: %1", DADissenterGetStatus(dissenter));
482 LOG_DISK_NC("Successful");
491 LOG_DISK_NC("Unmount operation started");
493 auto session = DASessionCreate(kCFAllocatorDefault);
498 auto disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, _device.c_str());
502 LOG_DISK("Requesting unmount of %1 from %2", _device, thread_id());
503 bool success = false;
504 DADiskUnmount(disk, kDADiskUnmountOptionWhole, &done_callback, &success);
507 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
508 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
509 CFRunLoopStop (run_loop);
510 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.5, 0);
513 LOG_DISK_NC("End of unmount");
519 disk_write_finished ()
526 make_foreground_application ()
528 ProcessSerialNumber serial;
529 LIBDCP_DISABLE_WARNINGS
530 GetCurrentProcess (&serial);
531 LIBDCP_ENABLE_WARNINGS
532 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
537 dcpomatic::get_process_id ()
539 return dcp::raw_convert<string>(getpid());
543 boost::filesystem::path
544 fix_long_path (boost::filesystem::path path)
551 show_in_file_manager (boost::filesystem::path, boost::filesystem::path select)
553 int r = system (String::compose("open -R \"%1\"", select.string()).c_str());
554 return static_cast<bool>(WEXITSTATUS(r));