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 <sys/sysctl.h>
36 #include <mach-o/dyld.h>
37 #include <IOKit/pwr_mgt/IOPMLib.h>
38 #include <DiskArbitration/DADisk.h>
39 #include <DiskArbitration/DiskArbitration.h>
40 #include <sys/types.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
57 using std::runtime_error;
58 using boost::shared_ptr;
59 using boost::optional;
61 /** @param s Number of seconds to sleep for */
63 dcpomatic_sleep_seconds (int s)
69 dcpomatic_sleep_milliseconds (int ms)
74 /** @return A string of CPU information (model name etc.) */
81 size_t N = sizeof (buffer);
82 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
89 /** @return Path of the Contents directory in the .app */
90 boost::filesystem::path
93 return boost::dll::program_location().parent_path().parent_path();
96 boost::filesystem::path
99 return app_contents() / "Resources";
103 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
105 boost::filesystem::path path = app_contents();
109 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
110 LOG_GENERAL (N_("Probing with %1"), ffprobe);
111 system (ffprobe.c_str ());
114 list<pair<string, string> >
117 list<pair<string, string> > m;
121 boost::filesystem::path
124 boost::filesystem::path path = app_contents();
130 boost::filesystem::path
133 boost::filesystem::path path = app_contents();
135 path /= "dcpomatic2_disk_writer";
139 /* Apparently there is no way to create an ofstream using a UTF-8
140 filename under Windows. We are hence reduced to using fopen
144 fopen_boost (boost::filesystem::path p, string t)
146 return fopen (p.c_str(), t.c_str ());
150 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
152 return fseek (stream, offset, whence);
163 boost::mutex::scoped_lock lm (_mutex);
164 /* We should use this */
165 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
166 /* but it's not available on 10.5, so we use this */
167 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
172 boost::mutex::scoped_lock lm (_mutex);
173 IOPMAssertionRelease (_assertion_id);
177 start_tool (boost::filesystem::path dcpomatic, string executable, string app)
179 boost::filesystem::path batch = dcpomatic.parent_path ();
180 batch = batch.parent_path (); // MacOS
181 batch = batch.parent_path (); // Contents
182 batch = batch.parent_path (); // DCP-o-matic.app
183 batch = batch.parent_path (); // Applications
191 int const r = system (batch.string().c_str());
192 exit (WEXITSTATUS (r));
197 start_batch_converter (boost::filesystem::path dcpomatic)
199 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
203 start_player (boost::filesystem::path dcpomatic)
205 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
211 return (uint64_t) pthread_self ();
215 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
217 return avio_open (s, file.c_str(), flags);
220 boost::filesystem::path
223 return getenv("HOME");
227 command_and_read (string cmd)
232 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
236 /* I'm assuming nobody does this on OS X */
241 disk_appeared(DADiskRef disk, void* context)
243 const char* name = DADiskGetBSDName (disk);
245 list<Drive>* drives = reinterpret_cast<list<Drive>*> (context);
246 drives->push_back (Drive(name, 0, false, optional<string>(), optional<string>()));
253 vector<Drive> drives;
255 DASessionRef session = DASessionCreate(kCFAllocatorDefault);
260 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &drives);
261 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
262 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
263 CFRunLoopStop (run_loop);
264 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
265 DAUnregisterCallback(session, (void *) disk_appeared, &drives);
272 Drive::description () const
275 snprintf(gb, 64, "%.1f", _size / 1000000000.0);
282 if (name.size() > 0) {
283 name += " " + *_model;
286 if (name.size() == 0) {
290 return String::compose("%1 (%2 GB) [%3]", name, gb, _internal_name);
293 boost::filesystem::path
296 boost::filesystem::path p;
297 p /= g_get_home_dir ();
300 p /= "com.dcpomatic";