2023-02-12 16:44:26 +00:00
|
|
|
use bevy::{app::AppExit, prelude::*, window::WindowFocused};
|
2022-01-19 20:52:52 +00:00
|
|
|
#[cfg(windows)]
|
2022-05-17 19:25:24 +00:00
|
|
|
use windows::{
|
|
|
|
core::GUID,
|
|
|
|
Win32::System::{Power, SystemServices::GUID_MIN_POWER_SAVINGS},
|
|
|
|
};
|
2022-01-19 20:52:52 +00:00
|
|
|
|
2022-05-17 19:25:24 +00:00
|
|
|
#[cfg(windows)]
|
2022-12-20 15:35:06 +00:00
|
|
|
#[derive(Resource, Deref, DerefMut)]
|
2022-05-17 19:25:24 +00:00
|
|
|
struct DefaultScheme(GUID);
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
2022-12-20 15:35:06 +00:00
|
|
|
#[derive(Resource)]
|
2022-05-17 19:25:24 +00:00
|
|
|
struct DefaultScheme();
|
|
|
|
|
|
|
|
#[allow(unused_mut, unused_variables)]
|
|
|
|
fn setup(mut commands: Commands) {
|
2022-01-19 20:52:52 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
unsafe {
|
2022-05-17 19:25:24 +00:00
|
|
|
let mut active: *mut GUID = std::ptr::null_mut();
|
2023-09-09 17:52:02 +00:00
|
|
|
Power::PowerGetActiveScheme(None, &mut active).expect("Failed to get active power scheme");
|
2022-05-17 19:25:24 +00:00
|
|
|
if let Some(active) = active.as_ref() {
|
|
|
|
let scheme = DefaultScheme(*active);
|
|
|
|
commands.insert_resource(scheme);
|
2023-02-12 16:44:26 +00:00
|
|
|
ctrlc::set_handler(move || {
|
2023-09-09 17:52:02 +00:00
|
|
|
Power::PowerSetActiveScheme(None, Some(active))
|
|
|
|
.expect("Failed to set power scheme");
|
2023-02-12 16:44:26 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
})
|
|
|
|
.expect("Failed to set exit handler");
|
2022-05-17 19:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 01:31:00 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
commands.insert_resource(DefaultScheme());
|
2022-05-17 19:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn focus_change(
|
2022-12-20 15:35:06 +00:00
|
|
|
config: Res<FullThrottlePlugin>,
|
2022-05-17 19:25:24 +00:00
|
|
|
mut focus: EventReader<WindowFocused>,
|
|
|
|
scheme: Res<DefaultScheme>,
|
|
|
|
) {
|
2024-01-02 17:36:30 +00:00
|
|
|
for event in focus.read() {
|
2022-05-17 19:25:24 +00:00
|
|
|
if event.focused {
|
|
|
|
#[cfg(windows)]
|
|
|
|
unsafe {
|
2023-09-09 17:52:02 +00:00
|
|
|
Power::PowerSetActiveScheme(None, Some(&GUID_MIN_POWER_SAVINGS))
|
|
|
|
.expect("Failed to set power scheme");
|
2022-05-17 19:25:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#[cfg(windows)]
|
|
|
|
if config.restore_original_scheme_on_unfocus {
|
|
|
|
unsafe {
|
2023-09-09 17:52:02 +00:00
|
|
|
Power::PowerSetActiveScheme(None, Some(&**scheme))
|
|
|
|
.expect("Failed to set power scheme");
|
2022-05-17 19:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 20:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:44:26 +00:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn exit(mut exit: EventReader<AppExit>, scheme: Res<DefaultScheme>) {
|
2024-01-02 17:36:30 +00:00
|
|
|
for event in exit.read() {
|
2023-02-12 16:44:26 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
unsafe {
|
2023-09-09 17:52:02 +00:00
|
|
|
Power::PowerSetActiveScheme(None, Some(&**scheme))
|
|
|
|
.expect("Failed to restore original power scheme");
|
2023-02-12 16:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 15:35:06 +00:00
|
|
|
#[derive(Resource, Clone, Copy, Default)]
|
|
|
|
pub struct FullThrottlePlugin {
|
|
|
|
pub restore_original_scheme_on_unfocus: bool,
|
|
|
|
}
|
2022-01-19 20:52:52 +00:00
|
|
|
|
|
|
|
impl Plugin for FullThrottlePlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2022-12-20 15:35:06 +00:00
|
|
|
app.insert_resource(*self)
|
2023-07-16 17:53:23 +00:00
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(PostUpdate, (focus_change, exit));
|
2022-01-19 20:52:52 +00:00
|
|
|
}
|
|
|
|
}
|