2 Copyright (C) 2012-2020 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/>.
22 #include "compose.hpp"
24 #include "dcpomatic_log.h"
26 #include "exceptions.h"
27 #include <dcp/raw_convert.h>
30 #include <libavformat/avio.h>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/dll/runtime_symbol_info.hpp>
35 #include <boost/regex.hpp>
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 <sys/types.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
60 using std::runtime_error;
61 using boost::shared_ptr;
62 using boost::optional;
64 /** @param s Number of seconds to sleep for */
66 dcpomatic_sleep_seconds (int s)
72 dcpomatic_sleep_milliseconds (int ms)
77 /** @return A string of CPU information (model name etc.) */
84 size_t N = sizeof (buffer);
85 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
92 /** @return Path of the Contents directory in the .app */
93 boost::filesystem::path
96 return boost::dll::program_location().parent_path().parent_path();
99 boost::filesystem::path
102 return app_contents() / "Resources";
106 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
108 boost::filesystem::path path = app_contents();
112 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
113 LOG_GENERAL (N_("Probing with %1"), ffprobe);
114 system (ffprobe.c_str ());
117 list<pair<string, string> >
120 list<pair<string, string> > m;
124 boost::filesystem::path
127 boost::filesystem::path path = app_contents();
133 boost::filesystem::path
136 return "/Users/carl/dcpomatic/src/dcpomatic/build/src/tools/dcpomatic2_disk_writer";
137 boost::filesystem::path path = app_contents();
139 path /= "dcpomatic2_disk_writer";
143 /* Apparently there is no way to create an ofstream using a UTF-8
144 filename under Windows. We are hence reduced to using fopen
148 fopen_boost (boost::filesystem::path p, string t)
150 return fopen (p.c_str(), t.c_str ());
154 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
156 return fseek (stream, offset, whence);
167 boost::mutex::scoped_lock lm (_mutex);
168 /* We should use this */
169 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
170 /* but it's not available on 10.5, so we use this */
171 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
176 boost::mutex::scoped_lock lm (_mutex);
177 IOPMAssertionRelease (_assertion_id);
181 start_tool (boost::filesystem::path dcpomatic, string executable, string app)
183 boost::filesystem::path batch = dcpomatic.parent_path ();
184 batch = batch.parent_path (); // MacOS
185 batch = batch.parent_path (); // Contents
186 batch = batch.parent_path (); // DCP-o-matic.app
187 batch = batch.parent_path (); // Applications
195 int const r = system (batch.string().c_str());
196 exit (WEXITSTATUS (r));
201 start_batch_converter (boost::filesystem::path dcpomatic)
203 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
207 start_player (boost::filesystem::path dcpomatic)
209 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
215 return (uint64_t) pthread_self ();
219 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
221 return avio_open (s, file.c_str(), flags);
224 boost::filesystem::path
227 return getenv("HOME");
231 command_and_read (string cmd)
236 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
240 /* I'm assuming nobody does this on OS X */
245 disk_appeared (DADiskRef disk, void* context)
247 const char* name = DADiskGetBSDName (disk);
252 vector<Drive>* drives = reinterpret_cast<vector<Drive>*> (context);
254 CFDictionaryRef description = DADiskCopyDescription (disk);
256 optional<string> vendor;
257 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceVendorKey);
259 vendor = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
260 vendor = boost::algorithm::trim (*vendor);
263 optional<string> model;
264 str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceModelKey);
266 model = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
267 model = boost::algorithm::trim (*model);
270 str = CFDictionaryGetValue (description, kDADiskDescriptionMediaPathKey);
272 char const* path = CFStringGetCStringPtr((CFStringRef) str, kCFStringEncodingUTF8);
273 if (strncmp(path, "IODeviceTree:", 13) != 0) {
278 io_service_t service = DADiskCopyIOMedia (disk);
279 CFTypeRef whole_media_ref = IORegistryEntryCreateCFProperty (service, CFSTR(kIOMediaWholeKey), kCFAllocatorDefault, 0);
280 bool whole_media = false;
281 if (whole_media_ref) {
282 whole_media = CFBooleanGetValue((CFBooleanRef) whole_media_ref);
283 CFRelease (whole_media_ref);
285 IOObjectRelease (service);
291 CFNumberGetValue ((CFNumberRef) CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey), kCFNumberLongType, &size);
293 CFRelease (description);
295 drives->push_back (Drive(name, size, false, vendor, model));
301 vector<Drive> drives;
303 DASessionRef session = DASessionCreate(kCFAllocatorDefault);
308 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &drives);
309 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
310 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
311 CFRunLoopStop (run_loop);
312 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
313 DAUnregisterCallback(session, (void *) disk_appeared, &drives);
319 boost::filesystem::path
322 boost::filesystem::path p;
323 p /= g_get_home_dir ();
326 p /= "com.dcpomatic";