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();
|
|
|
|
Power::PowerGetActiveScheme(None, &mut active);
|
|
|
|
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 || {
|
|
|
|
Power::PowerSetActiveScheme(None, Some(active));
|
|
|
|
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>,
|
|
|
|
) {
|
|
|
|
for event in focus.iter() {
|
|
|
|
if event.focused {
|
|
|
|
#[cfg(windows)]
|
|
|
|
unsafe {
|
2022-09-21 23:25:52 +00:00
|
|
|
Power::PowerSetActiveScheme(None, Some(&GUID_MIN_POWER_SAVINGS));
|
2022-05-17 19:25:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#[cfg(windows)]
|
|
|
|
if config.restore_original_scheme_on_unfocus {
|
|
|
|
unsafe {
|
2022-09-21 23:25:52 +00:00
|
|
|
Power::PowerSetActiveScheme(None, Some(&**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>) {
|
|
|
|
for event in exit.iter() {
|
|
|
|
#[cfg(windows)]
|
|
|
|
unsafe {
|
|
|
|
Power::PowerSetActiveScheme(None, Some(&**scheme));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|