use std::marker::PhantomData;
use std::sync::{Mutex, MutexGuard};
static HELD: Mutex<u32> = Mutex::new(0);
fn held() -> MutexGuard<'static, u32> {
HELD.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
#[must_use]
pub fn is_supported() -> bool {
imp::supported()
}
#[must_use]
pub fn is_held() -> bool {
*held() > 0
}
#[must_use = "the screen may sleep again as soon as the lock is dropped"]
pub fn keep_screen_on() -> ScreenLock {
let mut n = held();
*n += 1;
if *n == 1 {
imp::hold(true);
}
ScreenLock {
_ui_thread: PhantomData,
}
}
pub struct ScreenLock {
_ui_thread: PhantomData<*const ()>,
}
impl Drop for ScreenLock {
fn drop(&mut self) {
let mut n = held();
*n = n.saturating_sub(1);
if *n == 0 {
imp::hold(false);
}
}
}
#[cfg(target_os = "ios")]
mod imp {
use objc2::MainThreadMarker;
use objc2_ui_kit::UIApplication;
pub fn supported() -> bool {
true
}
pub fn hold(on: bool) {
if let Some(mtm) = MainThreadMarker::new() {
UIApplication::sharedApplication(mtm).setIdleTimerDisabled(on);
}
}
}
#[cfg(target_os = "macos")]
mod imp {
use std::ffi::{c_char, c_void};
use std::sync::Mutex;
type CFStringRef = *const c_void;
#[link(name = "CoreFoundation", kind = "framework")]
unsafe extern "C" {
fn CFStringCreateWithCString(
alloc: *const c_void,
text: *const c_char,
encoding: u32,
) -> CFStringRef;
fn CFRelease(value: *const c_void);
}
#[link(name = "IOKit", kind = "framework")]
unsafe extern "C" {
fn IOPMAssertionCreateWithName(
kind: CFStringRef,
level: u32,
name: CFStringRef,
id: *mut u32,
) -> i32;
fn IOPMAssertionRelease(id: u32) -> i32;
}
const UTF8: u32 = 0x0800_0100;
const LEVEL_ON: u32 = 255;
static ASSERTION: Mutex<Option<u32>> = Mutex::new(None);
pub fn supported() -> bool {
true
}
pub fn hold(on: bool) {
let mut held = ASSERTION
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if on && held.is_none() {
unsafe {
let kind = CFStringCreateWithCString(
std::ptr::null(),
c"PreventUserIdleDisplaySleep".as_ptr(),
UTF8,
);
let name = CFStringCreateWithCString(
std::ptr::null(),
c"Keeping the screen on while the app shows something".as_ptr(),
UTF8,
);
let mut id = 0u32;
if !kind.is_null()
&& !name.is_null()
&& IOPMAssertionCreateWithName(kind, LEVEL_ON, name, &raw mut id) == 0
{
*held = Some(id);
}
for s in [kind, name] {
if !s.is_null() {
CFRelease(s);
}
}
}
} else if !on && let Some(id) = held.take() {
unsafe {
IOPMAssertionRelease(id);
}
}
}
}
#[cfg(windows)]
mod imp {
use windows::Win32::System::Power::{
ES_CONTINUOUS, ES_DISPLAY_REQUIRED, SetThreadExecutionState,
};
pub fn supported() -> bool {
true
}
pub fn hold(on: bool) {
let state = if on {
ES_CONTINUOUS | ES_DISPLAY_REQUIRED
} else {
ES_CONTINUOUS
};
unsafe {
SetThreadExecutionState(state);
}
}
}
#[cfg(any(
target_os = "android",
all(target_os = "linux", target_env = "ohos"),
target_arch = "wasm32"
))]
mod imp {
use day_bridge::Support;
pub fn supported() -> bool {
super::ready_native_support() != Support::Unsupported
&& super::ready_native().unwrap_or(false)
}
pub fn hold(on: bool) {
super::hold_native(on);
}
}
#[cfg(not(any(
target_os = "ios",
target_os = "macos",
windows,
target_os = "android",
all(target_os = "linux", target_env = "ohos"),
target_arch = "wasm32"
)))]
mod imp {
pub fn supported() -> bool {
false
}
pub fn hold(_on: bool) {}
}
day_bridge::bridge! {
#[day_bridge::declare]
extern "day" {
fn hold_native(on: bool);
fn ready_native() -> Result<bool, day_bridge::Error>;
}
#[day_bridge::impl(java, platforms = [android])]
java!(
prelude = r#"
import android.app.Activity;
import android.content.Context;
import android.view.WindowManager;
import dev.daybrite.day.bridge.DayRuntime;
"#,
body = r#"
public static void hold_native(boolean on) {
Context ctx = DayRuntime.ctx;
if (!(ctx instanceof Activity)) {
return;
}
Activity activity = (Activity) ctx;
// Window flags belong to the UI thread.
DayRuntime.main.post(() -> {
if (on) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
public static boolean ready_native() {
return DayRuntime.ctx instanceof Activity;
}
"#,
);
#[day_bridge::impl(arkts, platforms = [ohos])]
arkts!(
prelude = r#"
import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
"#,
body = r#"
export function hold_native(on: boolean): void {
const ctx = getContext() as common.UIAbilityContext;
window.getLastWindow(ctx).then((win: window.Window) => {
return win.setWindowKeepScreenOn(on);
}).catch((e: Error) => {
console.warn(`day-part-wakelock: ${e.message}`);
});
}
export function ready_native(): boolean {
return true;
}
"#,
);
#[day_bridge::impl(js, platforms = [web])]
js!(r#"
let dayLock = null;
let dayWanted = false;
let dayListening = false;
async function dayTake() {
if (!dayWanted || dayLock !== null || document.visibilityState !== "visible") {
return;
}
try {
const lock = await navigator.wakeLock.request("screen");
if (!dayWanted) {
lock.release();
return;
}
dayLock = lock;
lock.addEventListener("release", () => {
if (dayLock === lock) {
dayLock = null;
}
});
} catch (e) {
console.warn(`day-part-wakelock: ${e}`);
}
}
export function hold_native(on) {
if (!("wakeLock" in navigator)) {
return;
}
dayWanted = Boolean(on);
if (!dayListening) {
dayListening = true;
document.addEventListener("visibilitychange", () => {
dayTake();
});
}
if (dayWanted) {
dayTake();
} else if (dayLock !== null) {
dayLock.release();
dayLock = null;
}
}
export function ready_native() {
return "wakeLock" in navigator;
}
"#);
#[day_bridge::impl(rust, platforms = [other])]
fn hold_native(_on: bool) {}
#[day_bridge::impl(rust, platforms = [other])]
fn ready_native() -> Result<bool, day_bridge::Error> {
Ok(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn locks_nest() {
assert!(!is_held());
let first = keep_screen_on();
let second = keep_screen_on();
assert!(is_held());
drop(first);
assert!(is_held(), "the second lock still holds the screen");
drop(second);
assert!(!is_held());
}
}