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 std::cout << "program_location=" << boost::dll::program_location() << "\n";
94 return boost::dll::program_location().parent_path().parent_path();
97 boost::filesystem::path
100 return app_contents() / "Resources";
104 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
106 boost::filesystem::path path = app_contents();
110 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
111 LOG_GENERAL (N_("Probing with %1"), ffprobe);
112 system (ffprobe.c_str ());
115 list<pair<string, string> >
118 list<pair<string, string> > m;
122 boost::filesystem::path
125 boost::filesystem::path path = app_contents();
131 boost::filesystem::path
134 boost::filesystem::path path = app_contents();
136 path /= "dcpomatic2_disk_writer";
140 /* Apparently there is no way to create an ofstream using a UTF-8
141 filename under Windows. We are hence reduced to using fopen
145 fopen_boost (boost::filesystem::path p, string t)
147 return fopen (p.c_str(), t.c_str ());
151 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
153 return fseek (stream, offset, whence);
164 boost::mutex::scoped_lock lm (_mutex);
165 /* We should use this */
166 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
167 /* but it's not available on 10.5, so we use this */
168 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
173 boost::mutex::scoped_lock lm (_mutex);
174 IOPMAssertionRelease (_assertion_id);
178 start_tool (boost::filesystem::path dcpomatic, string executable, string app)
180 boost::filesystem::path batch = dcpomatic.parent_path ();
181 batch = batch.parent_path (); // MacOS
182 batch = batch.parent_path (); // Contents
183 batch = batch.parent_path (); // DCP-o-matic.app
184 batch = batch.parent_path (); // Applications
192 int const r = system (batch.string().c_str());
193 exit (WEXITSTATUS (r));
198 start_batch_converter (boost::filesystem::path dcpomatic)
200 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
204 start_player (boost::filesystem::path dcpomatic)
206 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
212 return (uint64_t) pthread_self ();
216 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
218 return avio_open (s, file.c_str(), flags);
221 boost::filesystem::path
224 return getenv("HOME");
228 command_and_read (string cmd)
233 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
237 /* I'm assuming nobody does this on OS X */
242 disk_appeared(DADiskRef disk, void* context)
244 const char* name = DADiskGetBSDName (disk);
246 list<Drive>* drives = reinterpret_cast<list<Drive>*> (context);
247 drives->push_back (Drive(name, 0, false, optional<string>(), optional<string>()));
254 vector<Drive> drives;
256 DASessionRef session = DASessionCreate(kCFAllocatorDefault);
261 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &drives);
262 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
263 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
264 CFRunLoopStop (run_loop);
265 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
266 DAUnregisterCallback(session, (void *) disk_appeared, &drives);
273 Drive::description () const
276 snprintf(gb, 64, "%.1f", _size / 1000000000.0);
283 if (name.size() > 0) {
284 name += " " + *_model;
287 if (name.size() == 0) {
291 return String::compose("%1 (%2 GB) [%3]", name, gb, _internal_name);
294 boost::filesystem::path
297 boost::filesystem::path p;
298 p /= g_get_home_dir ();
301 p /= "com.dcpomatic";