-
Notifications
You must be signed in to change notification settings - Fork 28.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: add --no-sleep flag, implementation for macos (#171885)
First part of microsoft/vscode-remote-release#7127
- Loading branch information
1 parent
00e8fde
commit 4cf496e
Showing
7 changed files
with
123 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
#[cfg(target_os = "windows")] | ||
pub type SleepInhibitor = NoOpSleepInhibitor; | ||
|
||
#[cfg(target_os = "linux")] | ||
pub type SleepInhibitor = NoOpSleepInhibitor; | ||
|
||
#[cfg(target_os = "macos")] | ||
pub type SleepInhibitor = super::nosleep_macos::SleepInhibitor; | ||
|
||
/// Stub no-sleep implementation for unsupported platforms. | ||
#[allow(dead_code)] | ||
pub struct NoOpSleepInhibitor(); | ||
|
||
#[allow(dead_code)] | ||
impl NoOpSleepInhibitor { | ||
pub fn new() -> std::io::Result<Self> { | ||
Ok(NoOpSleepInhibitor()) | ||
} | ||
} | ||
|
||
impl Drop for NoOpSleepInhibitor { | ||
fn drop(&mut self) { | ||
// no-op | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
use std::io; | ||
|
||
use const_format::concatcp; | ||
use core_foundation::base::TCFType; | ||
use core_foundation::string::{CFString, CFStringRef}; | ||
use libc::c_int; | ||
|
||
use crate::constants::APPLICATION_NAME; | ||
|
||
extern "C" { | ||
pub fn IOPMAssertionCreateWithName( | ||
assertion_type: CFStringRef, | ||
assertion_level: u32, | ||
assertion_name: CFStringRef, | ||
assertion_id: &mut u32, | ||
) -> c_int; | ||
|
||
pub fn IOPMAssertionRelease(assertion_id: u32) -> c_int; | ||
} | ||
|
||
pub struct SleepInhibitor { | ||
assertion_id: u32, | ||
} | ||
|
||
impl SleepInhibitor { | ||
pub fn new() -> io::Result<Self> { | ||
let mut assertion_id = 0; | ||
let assertion_type = CFString::from_static_string("PreventSystemSleep"); | ||
let assertion_name = | ||
CFString::from_static_string(concatcp!(APPLICATION_NAME, " running tunnel")); | ||
let result = unsafe { | ||
IOPMAssertionCreateWithName( | ||
assertion_type.as_concrete_TypeRef(), | ||
255, | ||
assertion_name.as_concrete_TypeRef(), | ||
&mut assertion_id, | ||
) | ||
}; | ||
|
||
if result != 0 { | ||
return Err(io::Error::last_os_error()); | ||
} | ||
|
||
Ok(Self { assertion_id }) | ||
} | ||
} | ||
|
||
impl Drop for SleepInhibitor { | ||
fn drop(&mut self) { | ||
unsafe { | ||
IOPMAssertionRelease(self.assertion_id); | ||
} | ||
} | ||
} |