1use std::fmt::Display;
2
3use block::{ConcreteBlock, IntoConcreteBlock};
4use libc::c_ulonglong;
5use objc::{msg_send, sel, sel_impl};
6
7use crate::{
8 object,
9 objective_c_runtime::{
10 id,
11 macros::interface_impl,
12 traits::{FromId, PNSObject},
13 },
14 utils::to_bool,
15};
16
17use super::{Int, NSArray, NSDictionary, NSNotificationName, NSString, NSTimeInterval, UInt};
18
19#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
21pub struct NSOperatingSystemVersion {
22 pub major: isize,
24 pub minor: isize,
26 pub patch_version: isize,
28}
29
30impl Display for NSOperatingSystemVersion {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 write!(f, "{}.{}.{}", self.major, self.minor, self.patch_version)
33 }
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
38#[repr(u64)]
39pub enum NSActivityOptions {
40 IdleDisplaySleepDisabled = 1 << 40,
42 IdleSystemSleepDisabled = 1 << 20,
44 SuddenTerminationDisabled = 1 << 14,
46 AutomaticTerminationDisabled = 1 << 15,
48 UserInitiated = 0x00FFFFFF | Self::IdleSystemSleepDisabled as u64,
50 UserInteractive = (Self::UserInitiated as u64 | Self::LatencyCritical as u64),
52 Background = 0x000000ff,
54 LatencyCritical = 0xFF00000000,
56 InitiatedAllowingIdleSystemSleep =
58 Self::UserInitiated as u64 & !(Self::IdleSystemSleepDisabled as u64),
59 AnimationTrackingEnabled = (1 << 45),
61 TrackingEnabled = (1 << 46),
63}
64
65#[derive(Debug)]
67#[repr(i64)]
68pub enum NSProcessInfoThermalState {
69 Nominal,
71 Fair,
73 Serious,
75 Critical,
77}
78
79object! {
80 unsafe pub struct NSProcessInfo;
82}
83
84#[interface_impl(NSObject)]
85impl NSProcessInfo {
86 #[property]
91 pub fn process_info() -> NSProcessInfo {
92 unsafe { NSProcessInfo::from_id(msg_send![Self::m_class(), processInfo]) }
93 }
94
95 #[property]
100 pub fn arguments(&self) -> NSArray<NSString> {
101 unsafe { NSArray::from_id(msg_send![self.m_self(), arguments]) }
102 }
103
104 #[property]
106 pub fn environment(&self) -> NSDictionary<NSString, NSString> {
107 unsafe { NSDictionary::from_id(msg_send![self.m_self(), environment]) }
108 }
109
110 #[property]
112 pub fn globally_unique_string(&self) -> NSString {
113 unsafe { NSString::from_id(msg_send![self.m_self(), globallyUniqueString]) }
114 }
115
116 #[property]
118 pub fn mac_catalyst_app(&self) -> bool {
119 unsafe { to_bool(msg_send![self.m_self(), isMacCatalystApp]) }
120 }
121
122 #[property]
124 pub fn ios_app_on_mac(&self) -> bool {
125 unsafe { to_bool(msg_send![self.m_self(), isiOSAppOnMac]) }
126 }
127
128 #[property]
130 pub fn process_identifier(&self) -> Int {
131 unsafe { msg_send![self.m_self(), processIdentifier] }
132 }
133
134 #[property]
136 pub fn process_name(&self) -> NSString {
137 unsafe { NSString::from_id(msg_send![self.m_self(), processName]) }
138 }
139
140 #[property]
147 pub fn set_process_name(&mut self, name: NSString) {
148 unsafe { msg_send![self.m_self(), setProcessName: name] }
149 }
150
151 #[property]
156 pub fn user_name(&self) -> NSString {
157 unsafe { NSString::from_id(msg_send![self.m_self(), userName]) }
158 }
159
160 #[property]
162 pub fn full_user_name(&self) -> NSString {
163 unsafe { NSString::from_id(msg_send![self.m_self(), fullUserName]) }
164 }
165
166 #[method]
171 pub fn disable_sudden_termination(&mut self) {
172 unsafe { msg_send![self.m_self(), disableSuddenTermination] }
173 }
174
175 #[method]
177 pub fn enable_sudden_termination(&mut self) {
178 unsafe { msg_send![self.m_self(), enableSuddenTermination] }
179 }
180
181 #[method]
186 pub fn disable_automatic_termination(&mut self, reason: &NSString) {
187 unsafe { msg_send![self.m_self(), disableAutomaticTermination: reason.m_self()] }
188 }
189
190 #[method]
192 pub fn enable_automatic_termination(&mut self, reason: &NSString) {
193 unsafe { msg_send![self.m_self(), enableAutomaticTermination: reason.m_self()] }
194 }
195
196 #[property]
198 pub fn automatic_termination_support_enabled(&self) -> bool {
199 unsafe { to_bool(msg_send![self.m_self(), automaticTerminationSupportEnabled]) }
200 }
201
202 #[property]
207 pub fn host_name(&self) -> NSString {
208 unsafe { NSString::from_id(msg_send![self.m_self(), hostName]) }
209 }
210
211 #[property]
213 pub fn operating_system_version_string(&self) -> NSString {
214 unsafe { NSString::from_id(msg_send![self.m_self(), operatingSystemVersionString]) }
215 }
216
217 #[property]
219 pub fn operating_system_version(&self) -> NSOperatingSystemVersion {
220 unsafe { msg_send![self.m_self(), operatingSystemVersion] }
221 }
222
223 #[method]
225 pub fn is_operating_system_at_least_version(&self, version: NSOperatingSystemVersion) -> bool {
226 unsafe {
227 to_bool(msg_send![
228 self.m_self(),
229 isOperatingSystemAtLeastVersion: version
230 ])
231 }
232 }
233
234 #[deprecated = "Use `operating_system_version` or `is_operating_system_at_least_version` instead"]
236 #[method]
237 pub fn operating_system(&self) -> UInt {
238 unsafe { msg_send![self.m_self(), operatingSystem] }
239 }
240
241 #[method]
243 #[deprecated = "Use `operating_system_version` or `is_operating_system_at_least_version` instead"]
244 pub fn operating_system_name(&self) -> NSString {
245 unsafe { NSString::from_id(msg_send![self.m_self(), operatingSystemName]) }
246 }
247
248 #[property]
252 pub fn processor_count(&self) -> UInt {
253 unsafe { msg_send![self.m_self(), processorCount] }
254 }
255
256 #[property]
258 pub fn active_processor_count(&self) -> UInt {
259 unsafe { msg_send![self.m_self(), activeProcessorCount] }
260 }
261
262 #[property]
264 pub fn physical_memory(&self) -> c_ulonglong {
265 unsafe { msg_send![self.m_self(), physicalMemory] }
266 }
267
268 #[property]
270 pub fn system_uptime(&self) -> NSTimeInterval {
271 unsafe { msg_send![self.m_self(), systemUptime] }
272 }
273
274 #[method]
279 pub fn begin_activity_with_options_reason(
280 &self,
281 options: NSActivityOptions,
282 reason: &NSString,
283 ) -> id {
284 unsafe {
285 msg_send![self.m_self(), beginActivityWithOptions: options reason: reason.m_self()]
286 }
287 }
288
289 #[method]
291 pub fn end_activity(&self, activity: id) {
292 unsafe { msg_send![self.m_self(), endActivity: activity] }
293 }
294
295 #[method]
297 pub fn perform_activity_with_options_reason_using_block<F>(
298 &self,
299 activity: id,
300 reason: &NSString,
301 block: F,
302 ) where
303 F: IntoConcreteBlock<(), Ret = ()> + 'static,
304 {
305 let block = ConcreteBlock::new(block);
306 let block = block.copy();
307
308 unsafe {
309 msg_send![self.m_self(), performActivityWithOptions: activity reason: reason.m_self() usingBlock: block]
310 }
311 }
312
313 #[method]
315 pub fn perform_expiring_activity_with_reason_using_block<F>(&self, reason: &NSString, block: F)
316 where
317 F: IntoConcreteBlock<(bool,), Ret = ()> + 'static,
318 {
319 let block = ConcreteBlock::new(block);
320 let block = block.copy();
321
322 unsafe {
323 msg_send![self.m_self(), performExpiringActivityWithReason: reason.m_self() usingBlock: block]
324 }
325 }
326
327 #[property]
331 pub fn thermal_state(&self) -> NSProcessInfoThermalState {
332 unsafe { msg_send![self.m_self(), thermalState] }
333 }
334
335 #[property]
340 pub fn low_power_mode_enabled(&self) -> bool {
341 unsafe { msg_send![self.m_self(), isLowPowerModeEnabled] }
342 }
343}
344
345extern "C" {
346 pub static NSProcessInfoThermalStateDidChangeNotification: NSNotificationName;
348
349 pub static NSProcessInfoPowerStateDidChangeNotification: NSNotificationName;
351}