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";
168 boost::mutex::scoped_lock lm (_mutex);
169 IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
175 boost::mutex::scoped_lock lm (_mutex);
176 IOPMAssertionRelease (_assertion_id);
181 start_tool (string executable, string app)
183 auto exe_path = directory_containing_executable();
184 exe_path = exe_path.parent_path(); // Contents
185 exe_path = exe_path.parent_path(); // DCP-o-matic 2.app
186 exe_path = exe_path.parent_path(); // Applications
188 exe_path /= "Contents";
190 exe_path /= executable;
194 LOG_GENERAL ("start_tool %1 %2 with path %3", executable, app, exe_path.string());
195 int const r = system (exe_path.string().c_str());
196 exit (WEXITSTATUS (r));
197 } else if (pid == -1) {
198 LOG_ERROR_NC("Fork failed in start_tool");
204 start_batch_converter ()
206 start_tool ("dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
213 start_tool ("dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
220 return (uint64_t) pthread_self ();
225 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
227 return avio_open (s, file.c_str(), flags);
231 boost::filesystem::path
234 return getenv("HOME");
238 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
242 /* I'm assuming nobody does this on OS X */
247 static optional<string>
248 get_vendor (CFDictionaryRef& description)
250 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceVendorKey);
255 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
261 boost::algorithm::trim (s);
266 static optional<string>
267 get_model (CFDictionaryRef& description)
269 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceModelKey);
274 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
280 boost::algorithm::trim (s);
285 static optional<OSXMediaPath>
286 analyse_media_path (CFDictionaryRef& description)
288 using namespace boost::algorithm;
290 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionMediaPathKey);
292 LOG_DISK_NC("There is no MediaPathKey (no dictionary value)");
296 auto path_key_cstr = CFStringGetCStringPtr((CFStringRef) str, kCFStringEncodingUTF8);
297 if (!path_key_cstr) {
298 LOG_DISK_NC("There is no MediaPathKey (no cstring)");
302 string path(path_key_cstr);
303 LOG_DISK("MediaPathKey is %1", path);
304 return analyse_osx_media_path (path);
309 is_whole_drive (DADiskRef& disk)
311 io_service_t service = DADiskCopyIOMedia (disk);
312 CFTypeRef whole_media_ref = IORegistryEntryCreateCFProperty (service, CFSTR(kIOMediaWholeKey), kCFAllocatorDefault, 0);
313 bool whole_media = false;
314 if (whole_media_ref) {
315 whole_media = CFBooleanGetValue((CFBooleanRef) whole_media_ref);
316 CFRelease (whole_media_ref);
318 IOObjectRelease (service);
323 static optional<boost::filesystem::path>
324 mount_point (CFDictionaryRef& description)
326 auto volume_path_key = (CFURLRef) CFDictionaryGetValue (description, kDADiskDescriptionVolumePathKey);
327 if (!volume_path_key) {
331 char mount_path_buffer[1024];
332 if (!CFURLGetFileSystemRepresentation(volume_path_key, false, (UInt8 *) mount_path_buffer, sizeof(mount_path_buffer))) {
335 return boost::filesystem::path(mount_path_buffer);
339 /* Here follows some rather intricate and (probably) fragile code to find the list of available
340 * "real" drives on macOS that we might want to write a DCP to.
342 * We use the Disk Arbitration framework to give us a series of mount_points (/dev/disk0, /dev/disk1,
343 * /dev/disk1s1 and so on) and we use the API to gather useful information about these mount_points into
344 * a vector of Disk structs.
346 * Then we read the Disks that we found and try to derive a list of drives that we should offer to the
347 * user, with details of whether those drives are currently mounted or not.
349 * At the basic level we find the "disk"-level mount_points, looking at whether any of their partitions are mounted.
351 * This is complicated enormously by recent-ish macOS versions' habit of making `synthesized' volumes which
352 * reflect data in `real' partitions. So, for example, we might have a real (physical) drive /dev/disk2 with
353 * a partition /dev/disk2s2 whose content is made into a synthesized /dev/disk3, itself containing some partitions
354 * which are mounted. /dev/disk2s2 is not considered to be mounted, in this case. So we need to know that
355 * disk2s2 is related to disk3 so we can consider disk2s2 as mounted if any parts of disk3 are. In order to do
356 * this I am taking the first two parts of the IODeviceTree and seeing if they exist anywhere in a
357 * IOService identifier. If they do, I am assuming the IOService device is on the matching IODeviceTree device.
359 * Lots of this is guesswork and may be broken. In my defence the documentation that I have been able to
360 * unearth is, to put it impolitely, crap.
364 disk_appeared (DADiskRef disk, void* context)
366 auto bsd_name = DADiskGetBSDName (disk);
368 LOG_DISK_NC("Disk with no BSDName appeared");
371 LOG_DISK("%1 appeared", bsd_name);
375 this_disk.device = string("/dev/") + bsd_name;
376 LOG_DISK("Device is %1", this_disk.device);
378 CFDictionaryRef description = DADiskCopyDescription (disk);
380 this_disk.vendor = get_vendor (description);
381 this_disk.model = get_model (description);
382 LOG_DISK("Vendor/model: %1 %2", this_disk.vendor.get_value_or("[none]"), this_disk.model.get_value_or("[none]"));
384 auto media_path = analyse_media_path (description);
386 LOG_DISK("Finding media path for %1 failed", bsd_name);
390 this_disk.media_path = *media_path;
391 this_disk.whole = is_whole_drive (disk);
392 auto mp = mount_point (description);
394 this_disk.mount_points.push_back (*mp);
398 "%1 %2 mounted at %3",
399 this_disk.media_path.real ? "Real" : "Synth",
400 this_disk.whole ? "whole" : "part",
401 mp ? mp->string() : "[nowhere]"
404 auto media_size_cstr = CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey);
405 if (!media_size_cstr) {
406 LOG_DISK_NC("Could not read media size");
410 CFNumberGetValue ((CFNumberRef) media_size_cstr, kCFNumberLongType, &this_disk.size);
411 CFRelease (description);
413 reinterpret_cast<vector<OSXDisk>*>(context)->push_back(this_disk);
420 using namespace boost::algorithm;
421 vector<OSXDisk> disks;
423 auto session = DASessionCreate(kCFAllocatorDefault);
428 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &disks);
429 auto run_loop = CFRunLoopGetCurrent ();
430 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
431 CFRunLoopStop (run_loop);
432 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
433 DAUnregisterCallback(session, (void *) disk_appeared, &disks);
436 return osx_disks_to_drives (disks);
440 boost::filesystem::path
441 config_path (optional<string> version)
443 boost::filesystem::path p;
444 p /= g_get_home_dir ();
447 p /= "com.dcpomatic";
456 void done_callback(DADiskRef, DADissenterRef dissenter, void* context)
458 LOG_DISK_NC("Unmount finished");
459 bool* success = reinterpret_cast<bool*> (context);
461 LOG_DISK("Error: %1", DADissenterGetStatus(dissenter));
464 LOG_DISK_NC("Successful");
473 LOG_DISK_NC("Unmount operation started");
475 auto session = DASessionCreate(kCFAllocatorDefault);
480 auto disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, _device.c_str());
484 LOG_DISK("Requesting unmount of %1 from %2", _device, thread_id());
485 bool success = false;
486 DADiskUnmount(disk, kDADiskUnmountOptionWhole, &done_callback, &success);
489 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
490 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
491 CFRunLoopStop (run_loop);
492 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.5, 0);
495 LOG_DISK_NC("End of unmount");
501 disk_write_finished ()
508 make_foreground_application ()
510 ProcessSerialNumber serial;
511 LIBDCP_DISABLE_WARNINGS
512 GetCurrentProcess (&serial);
513 LIBDCP_ENABLE_WARNINGS
514 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
519 dcpomatic::get_process_id ()
521 return dcp::raw_convert<string>(getpid());
526 show_in_file_manager (boost::filesystem::path, boost::filesystem::path select)
528 int r = system (String::compose("open -R \"%1\"", select.string()).c_str());
529 return static_cast<bool>(WEXITSTATUS(r));