64cb2e6b83f5bb8952fb1d2c4c10d629e7515385
[dcpomatic.git] / src / lib / cross.h
1 /*
2     Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  src/lib/cross.h
22  *  @brief Cross-platform compatibility code.
23  */
24
25 #ifndef DCPOMATIC_CROSS_H
26 #define DCPOMATIC_CROSS_H
27
28 #ifdef DCPOMATIC_OSX
29 #include <IOKit/pwr_mgt/IOPMLib.h>
30 #endif
31 #include <boost/filesystem.hpp>
32 #include <boost/thread/mutex.hpp>
33 #include <boost/optional.hpp>
34 #include <boost/function.hpp>
35
36 #ifdef DCPOMATIC_WINDOWS
37 #define WEXITSTATUS(w) (w)
38 #endif
39
40 class Log;
41 struct AVIOContext;
42
43 extern void dcpomatic_sleep_seconds (int);
44 extern void dcpomatic_sleep_milliseconds (int);
45 extern std::string cpu_info ();
46 extern void run_ffprobe (boost::filesystem::path, boost::filesystem::path);
47 extern std::list<std::pair<std::string, std::string> > mount_info ();
48 extern boost::filesystem::path openssl_path ();
49 extern void make_foreground_application ();
50 #ifdef DCPOMATIC_DISK
51 extern boost::filesystem::path disk_writer_path ();
52 #endif
53 #ifdef DCPOMATIC_WINDOWS
54 extern void maybe_open_console ();
55 #endif
56 extern boost::filesystem::path shared_path ();
57 extern FILE * fopen_boost (boost::filesystem::path, std::string);
58 extern int dcpomatic_fseek (FILE *, int64_t, int);
59 extern void start_batch_converter ();
60 extern void start_player ();
61 extern uint64_t thread_id ();
62 extern int avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags);
63 extern boost::filesystem::path home_directory ();
64 extern std::string command_and_read (std::string cmd);
65 extern bool running_32_on_64 ();
66 extern void unprivileged ();
67 extern boost::filesystem::path config_path ();
68 extern boost::filesystem::path directory_containing_executable ();
69
70 class PrivilegeEscalator
71 {
72 public:
73         PrivilegeEscalator ();
74         ~PrivilegeEscalator ();
75 };
76
77 /** @class Waker
78  *  @brief A class which tries to keep the computer awake on various operating systems.
79  *
80  *  Create a Waker to prevent sleep, and call nudge() every so often (every minute or so).
81  *  Destroy the Waker to allow sleep again.
82  */
83 class Waker
84 {
85 public:
86         Waker ();
87         ~Waker ();
88
89         void nudge ();
90
91 private:
92         boost::mutex _mutex;
93 #ifdef DCPOMATIC_OSX
94         IOPMAssertionID _assertion_id;
95 #endif
96 };
97
98 class Drive
99 {
100 public:
101         Drive (std::string device, std::vector<boost::filesystem::path> mount_points, uint64_t size, boost::optional<std::string> vendor, boost::optional<std::string> model)
102                 : _device(device)
103                 , _mount_points(mount_points)
104                 , _size(size)
105                 , _vendor(vendor)
106                 , _model(model)
107         {}
108
109         explicit Drive (std::string);
110
111         std::string as_xml () const;
112
113         std::string description () const;
114
115         std::string device () const {
116                 return _device;
117         }
118
119         bool mounted () const {
120                 return !_mount_points.empty();
121         }
122
123         std::string log_summary () const;
124
125         /** Unmount any mounted partitions on a drive.
126          *  @return true on success, false on failure.
127          */
128         bool unmount ();
129
130         static std::vector<Drive> get ();
131
132 private:
133         std::string _device;
134         std::vector<boost::filesystem::path> _mount_points;
135         /** size in bytes */
136         uint64_t _size;
137         boost::optional<std::string> _vendor;
138         boost::optional<std::string> _model;
139 };
140
141 void disk_write_finished ();
142
143 #endif