Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/cli/src/tunnels/nosleep_macos.rs
3314 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
use std::io;
7
8
use core_foundation::base::TCFType;
9
use core_foundation::string::{CFString, CFStringRef};
10
use libc::c_int;
11
12
use crate::constants::TUNNEL_ACTIVITY_NAME;
13
14
extern "C" {
15
pub fn IOPMAssertionCreateWithName(
16
assertion_type: CFStringRef,
17
assertion_level: u32,
18
assertion_name: CFStringRef,
19
assertion_id: &mut u32,
20
) -> c_int;
21
22
pub fn IOPMAssertionRelease(assertion_id: u32) -> c_int;
23
}
24
25
const NUM_ASSERTIONS: usize = 2;
26
27
const ASSERTIONS: [&str; NUM_ASSERTIONS] = ["PreventUserIdleSystemSleep", "PreventSystemSleep"];
28
29
struct Assertion(u32);
30
31
impl Assertion {
32
pub fn make(typ: &CFString, name: &CFString) -> io::Result<Self> {
33
let mut assertion_id = 0;
34
let result = unsafe {
35
IOPMAssertionCreateWithName(
36
typ.as_concrete_TypeRef(),
37
255,
38
name.as_concrete_TypeRef(),
39
&mut assertion_id,
40
)
41
};
42
43
if result != 0 {
44
Err(io::Error::last_os_error())
45
} else {
46
Ok(Self(assertion_id))
47
}
48
}
49
}
50
51
impl Drop for Assertion {
52
fn drop(&mut self) {
53
unsafe {
54
IOPMAssertionRelease(self.0);
55
}
56
}
57
}
58
59
pub struct SleepInhibitor {
60
_assertions: Vec<Assertion>,
61
}
62
63
impl SleepInhibitor {
64
pub async fn new() -> io::Result<Self> {
65
let mut assertions = Vec::with_capacity(NUM_ASSERTIONS);
66
let assertion_name = CFString::from_static_string(TUNNEL_ACTIVITY_NAME);
67
for typ in ASSERTIONS {
68
assertions.push(Assertion::make(
69
&CFString::from_static_string(typ),
70
&assertion_name,
71
)?);
72
}
73
74
Ok(Self {
75
_assertions: assertions,
76
})
77
}
78
}
79
80