Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Keep streams connected with the phone's screen off; screen off for app windows

Bee1fcf21985ece
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dabd4b8..9d84fb9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -51,6 +51,13 @@ change things that need a step from you, and its notes say so.
the PC dropped or was replaced just as you opened an app window or the
phone's screen, the phone gave up at once with "Offline". It now waits up
to 10 seconds for the connection to come back, and opens the stream on it.
+- **Mirroring and app windows stay connected with the phone's screen off.**
+ With its screen off, the phone dozed off the network, the PC dropped the
+ connection every few seconds and the picture kept restarting. While it
+ streams, the phone now keeps its Wi-Fi and processor awake.
+- **Turn the phone's screen off from an app window**, as screen mirroring
+ could: the phone stays awake with its screen dark, and the apps keep
+ running on the PC. Keep awake works there too.
- **App windows keep running when the phone would sleep.** With an app open
in a window on the PC, the phone went to sleep after its screen timeout,
which stopped the app and froze the window. While a window is open, the
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt
index 70fc9ce..257ef50 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt
@@ -146,6 +146,7 @@ internal class PhonePlatform(
override fun mirrorPowerRequested(pcId: String, stayAwake: Boolean, screenOff: Boolean) {
MirrorService.setPower(pcId, stayAwake, screenOff)
+ appWindows.setPower(pcId, stayAwake, screenOff)
}
override fun mirrorStopRequested(pcId: String, session: UInt) {
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/mirror/AppWindows.kt b/android/app/src/main/kotlin/app/nectarlink/android/mirror/AppWindows.kt
index af0bdd9..2ba91c4 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/mirror/AppWindows.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/mirror/AppWindows.kt
@@ -64,6 +64,9 @@ class AppWindows(
private val windows = ConcurrentHashMap<Pair<String, UInt>, Window>()
+ /** What each PC asks of the phone while its windows are open: (stay awake, screen off). */
+ private val power = ConcurrentHashMap<String, Pair<Boolean, Boolean>>()
+
init {
instance = this
}
@@ -76,6 +79,7 @@ class AppWindows(
val (width, height, dpi) = size(maxSize)
val socket = Elevated.openApp(pkg, width, height, dpi, options.bitrate.toInt(), options.fps.toInt()) ?: return false
val window = Window(pc, options.session, socket, width, height, dpi, maxSize)
+ power[pc] = options.stayAwake to options.screenOff
windows.put(pc to options.session, window)?.let { runCatching { it.socket.close() } }
changed()
scope.launch(Dispatchers.IO) {
@@ -202,7 +206,22 @@ class AppWindows(
return Triple(fit(shortSide), fit(longSide), dpi)
}
+ /** The PC turned keeping the phone awake, or its screen off, on or off. */
+ fun setPower(pc: String, stayAwake: Boolean, screenOff: Boolean) {
+ if (windows.values.none { it.pc == pc }) return
+ power[pc] = stayAwake to screenOff
+ applyPower()
+ }
+
+ private fun applyPower() {
+ val open = windows.values.map { it.pc }.toSet()
+ power.keys.retainAll(open)
+ val wants = power.values
+ StreamPower.set(context, StreamPower.APPS, open.isNotEmpty(), wants.any { it.first }, wants.any { it.second })
+ }
+
private fun changed() {
+ applyPower()
val manager = NotificationManagerCompat.from(context)
if (windows.isEmpty()) {
manager.cancel(NOTIFICATION_ID)
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/mirror/MirrorService.kt b/android/app/src/main/kotlin/app/nectarlink/android/mirror/MirrorService.kt
index c5db0e9..7987a4a 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/mirror/MirrorService.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/mirror/MirrorService.kt
@@ -54,7 +54,6 @@ class MirrorService : Service() {
private var elevatedDisplayListener: android.hardware.display.DisplayManager.DisplayListener? = null
private var sound: SoundCapture? = null
private var pcId: String? = null
- private var wakeLock: android.os.PowerManager.WakeLock? = null
private var stayAwake: Boolean = false
private var screenOff: Boolean = false
@@ -268,26 +267,9 @@ class MirrorService : Service() {
}
}
- @Suppress("DEPRECATION")
private fun applyPower() {
val active = (encoder != null && projection != null) || elevatedSocket != null
- val wantWake = active && (stayAwake || screenOff)
- if (wantWake) {
- if (wakeLock == null) {
- wakeLock = getSystemService(android.os.PowerManager::class.java)?.newWakeLock(
- android.os.PowerManager.SCREEN_BRIGHT_WAKE_LOCK or android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP,
- "nectarlink:mirror",
- )?.apply { setReferenceCounted(false) }
- }
- if (wakeLock?.isHeld == false) {
- runCatching { wakeLock?.acquire(4 * 60 * 60 * 1000L) }
- }
- } else {
- if (wakeLock?.isHeld == true) {
- runCatching { wakeLock?.release() }
- }
- }
- app.nectarlink.android.elevated.Elevated.setScreenOff(active && screenOff)
+ StreamPower.set(this, StreamPower.SCREEN, active, stayAwake, screenOff)
}
/** The sound, on a stream of its own; the screen goes on without it if it can't. */
@@ -326,9 +308,7 @@ class MirrorService : Service() {
stayAwake = false
screenOff = false
_session.value = MirrorSession()
- runCatching { if (wakeLock?.isHeld == true) wakeLock?.release() }
- wakeLock = null
- app.nectarlink.android.elevated.Elevated.setScreenOff(false)
+ StreamPower.set(this, StreamPower.SCREEN, streaming = false)
elevatedDisplayListener?.let {
getSystemService(android.hardware.display.DisplayManager::class.java)?.unregisterDisplayListener(it)
}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/mirror/StreamPower.kt b/android/app/src/main/kotlin/app/nectarlink/android/mirror/StreamPower.kt
new file mode 100644
index 0000000..ae1af97
--- /dev/null
+++ b/android/app/src/main/kotlin/app/nectarlink/android/mirror/StreamPower.kt
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.mirror
+
+import android.content.Context
+import android.net.wifi.WifiManager
+import android.os.PowerManager
+import app.nectarlink.android.elevated.Elevated
+
+/**
+ * The phone's power while it streams to a PC: its screen ([SCREEN]) or apps
+ * in windows of their own ([APPS]). Each says what it wants with [set], and
+ * the phone does the most any of them asks for, so neither undoes the other.
+ *
+ * - While anything streams, a CPU wake lock and a Wi-Fi lock keep the phone
+ * on the network with its screen off. Without them it dozes, misses the
+ * PC's keep-alives, and the PC drops the connection after 8 s; the stream
+ * then restarts every few seconds.
+ * - Stay awake keeps the screen on; screen off keeps it on too, with the
+ * panel turned off through Elevated, so apps on the PC keep running.
+ */
+object StreamPower {
+ const val SCREEN = "screen"
+ const val APPS = "apps"
+
+ private data class Want(val stayAwake: Boolean, val screenOff: Boolean)
+
+ private val wants = HashMap<String, Want>()
+ private var cpu: PowerManager.WakeLock? = null
+ private var wifi: WifiManager.WifiLock? = null
+ private var screen: PowerManager.WakeLock? = null
+ private var panelOff: Boolean? = null
+
+ /** [source] streams ([streaming]), and wants the phone kept awake or its screen off. */
+ @Synchronized
+ fun set(context: Context, source: String, streaming: Boolean, stayAwake: Boolean = false, screenOff: Boolean = false) {
+ if (streaming) wants[source] = Want(stayAwake, screenOff) else wants.remove(source)
+ apply(context.applicationContext)
+ }
+
+ @Suppress("DEPRECATION")
+ private fun apply(context: Context) {
+ val streaming = wants.isNotEmpty()
+ val off = wants.values.any { it.screenOff }
+ val awake = off || wants.values.any { it.stayAwake }
+ val power = context.getSystemService(PowerManager::class.java)
+ val wifiManager = context.getSystemService(WifiManager::class.java)
+
+ cpu = hold(cpu, streaming) { power?.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "nectarlink:stream") }
+ // HIGH_PERF: Wi-Fi out of power save with the screen off (newer
+ // Android treats it as low latency, which only helps with it on).
+ wifi = if (streaming) {
+ (wifi ?: wifiManager?.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "nectarlink:stream"))
+ ?.apply {
+ setReferenceCounted(false)
+ if (!isHeld) runCatching { acquire() }
+ }
+ } else {
+ wifi?.let { if (it.isHeld) runCatching { it.release() } }
+ null
+ }
+ screen = hold(screen, awake) {
+ power?.newWakeLock(
+ PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
+ "nectarlink:mirror",
+ )
+ }
+ if (panelOff != off) {
+ panelOff = off
+ Elevated.setScreenOff(off)
+ }
+ }
+
+ /** Holds [lock] (made with [make] the first time) while [on], releases it otherwise. */
+ private fun hold(lock: PowerManager.WakeLock?, on: Boolean, make: () -> PowerManager.WakeLock?): PowerManager.WakeLock? {
+ if (!on) {
+ lock?.let { if (it.isHeld) runCatching { it.release() } }
+ return null
+ }
+ val held = (lock ?: make()?.apply { setReferenceCounted(false) }) ?: return null
+ // Renewed on every change; a stream left open longer lets go by itself.
+ runCatching { held.acquire(HOLD_MAX_MS) }
+ return held
+ }
+
+ private const val HOLD_MAX_MS = 4 * 60 * 60 * 1000L
+}
diff --git a/desktop/app/qml/MirrorWindow.qml b/desktop/app/qml/MirrorWindow.qml
index 1348e56..b5c2de6 100644
--- a/desktop/app/qml/MirrorWindow.qml
+++ b/desktop/app/qml/MirrorWindow.qml
@@ -351,7 +351,6 @@ Window {
}
IconButton {
id: stayAwakeBtn
- visible: !win.info.app
iconPath: Icons.coffee
iconColor: win.info.stayAwake === true ? Theme.primary : "white"
label: win.info.stayAwake === true
@@ -361,7 +360,7 @@ Window {
}
IconButton {
id: screenOffBtn
- visible: !win.info.app && win.info.canScreenOff === true
+ visible: win.info.canScreenOff === true
iconPath: Icons.screenOff
iconColor: win.info.screenOff === true ? Theme.primary : "white"
label: win.info.screenOff === true
diff --git a/desktop/app/src/bridge/mirror.rs b/desktop/app/src/bridge/mirror.rs
index 0544508..59bd973 100644
--- a/desktop/app/src/bridge/mirror.rs
+++ b/desktop/app/src/bridge/mirror.rs
@@ -235,9 +235,9 @@ impl qobject::Mirror {
"sound": window.session == MIRROR_SCREEN && mirror::has_sound(&window.device),
"recording": mirror::is_recording(&window),
"recordingStartedMs": mirror::recording_started_ms(&window),
- "stayAwake": window.session == MIRROR_SCREEN && mirror::stay_awake(&window.device),
- "screenOff": window.session == MIRROR_SCREEN && mirror::screen_off(&window.device),
- "canScreenOff": window.session == MIRROR_SCREEN && can_screen_off,
+ "stayAwake": mirror::stay_awake(&window.device),
+ "screenOff": mirror::screen_off(&window.device),
+ "canScreenOff": can_screen_off,
"notice": mirror::last_notice(&window),
"savedX": geom.map(|g| g.x),
"savedY": geom.map(|g| g.y),
diff --git a/desktop/app/src/mirror.rs b/desktop/app/src/mirror.rs
index e36e941..5dc8cbe 100644
--- a/desktop/app/src/mirror.rs
+++ b/desktop/app/src/mirror.rs
@@ -167,9 +167,10 @@ struct State {
last_focus_clip: Option<String>,
/// Phones whose sound is coming in.
sound: HashSet<DeviceId>,
- /// Phones kept awake while mirroring their screen.
+ /// Phones kept awake while mirroring (their screen or app windows).
stay_awake: HashSet<DeviceId>,
- /// Phones whose physical screen is turned off while mirroring.
+ /// Phones whose physical screen is turned off while mirroring (their
+ /// screen or app windows); cleared when the last window closes.
screen_off: HashSet<DeviceId>,
/// Phone currently receiving PC keyboard input without screen mirroring.
keyboard_device: Option<DeviceId>,
@@ -179,6 +180,15 @@ struct State {
saved: SavedWindows,
}
+impl State {
+ /// Whether `device` has a window open or opening (the screen or an app).
+ fn mirroring(&self, device: &DeviceId) -> bool {
+ self.windows
+ .iter()
+ .any(|(w, shown)| w.device == *device && matches!(shown.phase, Phase::Asking | Phase::Showing))
+ }
+}
+
impl Default for State {
fn default() -> Self {
Self {
@@ -237,7 +247,7 @@ fn set_sound(device: DeviceId, on: bool) {
core_host::host().hub.changed(Changes::MIRROR);
}
-/// Whether `device` is set to stay awake while mirroring its screen.
+/// Whether `device` is set to stay awake while mirroring.
pub fn stay_awake(device: &DeviceId) -> bool {
state(|s| s.stay_awake.contains(device))
}
@@ -247,11 +257,8 @@ pub fn screen_off(device: &DeviceId) -> bool {
state(|s| s.screen_off.contains(device))
}
-/// Toggles keeping the phone awake while mirroring (`session == 0`).
+/// Toggles keeping the phone awake while mirroring (its screen or app windows).
pub fn set_stay_awake(window: Window, on: bool) {
- if window.session != MIRROR_SCREEN {
- return;
- }
let screen_off = state(|s| {
if on {
s.stay_awake.insert(window.device);
@@ -273,11 +280,9 @@ pub fn set_stay_awake(window: Window, on: bool) {
});
}
-/// Toggles turning the phone's physical screen off while mirroring (`session == 0`).
+/// Toggles turning the phone's physical screen off while mirroring (its
+/// screen or app windows).
pub fn set_screen_off(window: Window, on: bool) {
- if window.session != MIRROR_SCREEN {
- return;
- }
let stay_awake = state(|s| {
if on {
s.screen_off.insert(window.device);
@@ -736,6 +741,11 @@ fn record_recent(s: &mut State, device: &DeviceId, pkg: &str) {
s.saved.save();
}
+/// What `device` is set to while mirroring: `(stay_awake, screen_off)`.
+fn power(s: &State, device: &DeviceId) -> (bool, bool) {
+ (s.stay_awake.contains(device), s.screen_off.contains(device))
+}
+
/// Asks the phone for its screen.
pub fn start(device: DeviceId) {
if keyboard_device() == Some(device) {
@@ -744,9 +754,12 @@ pub fn start(device: DeviceId) {
let window = Window::screen(device);
ffi::video_clear(&window.key());
let (stay_awake, screen_off) = state(|s| {
- s.screen_off.remove(&device);
+ // The phone's screen starts on, unless app windows already turned it off.
+ if !s.mirroring(&device) {
+ s.screen_off.remove(&device);
+ }
s.windows.insert(window, Shown { phase: Phase::Asking, app: None, pkg: None });
- (s.stay_awake.contains(&device), false)
+ power(s, &device)
});
request(window, MirrorStart { stay_awake, screen_off, ..OPTIONS });
}
@@ -839,12 +852,25 @@ pub fn start_app(device: DeviceId, pkg: String, label: String) {
.map(|(w, _)| *w);
let window =
ended.unwrap_or_else(|| Window { device, session: NEXT_SESSION.fetch_add(1, Ordering::Relaxed) });
+ if !s.mirroring(&device) {
+ s.screen_off.remove(&device);
+ }
s.windows.insert(window, Shown { phase: Phase::Asking, app: Some(label), pkg: Some(pkg.clone()) });
- Some(window)
+ Some((window, power(s, &device)))
});
- let Some(window) = window else { return };
+ let Some((window, (stay_awake, screen_off))) = window else { return };
ffi::video_clear(&window.key());
- request(window, MirrorStart { audio: false, session: window.session, app: Some(pkg), ..OPTIONS });
+ request(
+ window,
+ MirrorStart {
+ audio: false,
+ session: window.session,
+ app: Some(pkg),
+ stay_awake,
+ screen_off,
+ ..OPTIONS
+ },
+ );
}
/// Reopens a mirroring window that ended (the screen or an app window).
@@ -854,15 +880,28 @@ pub fn reopen(window: Window) {
return;
}
let pkg = state(|s| {
+ if !s.mirroring(&window.device) {
+ s.screen_off.remove(&window.device);
+ }
let shown = s.windows.get_mut(&window)?;
let pkg = shown.pkg.clone()?;
shown.phase = Phase::Asking;
record_recent(s, &window.device, &pkg);
- Some(pkg)
+ Some((pkg, power(s, &window.device)))
});
- let Some(pkg) = pkg else { return };
+ let Some((pkg, (stay_awake, screen_off))) = pkg else { return };
ffi::video_clear(&window.key());
- request(window, MirrorStart { audio: false, session: window.session, app: Some(pkg), ..OPTIONS });
+ request(
+ window,
+ MirrorStart {
+ audio: false,
+ session: window.session,
+ app: Some(pkg),
+ stay_awake,
+ screen_off,
+ ..OPTIONS
+ },
+ );
}
/// Resizes an app window's display on the phone (`width` and `height` are the
@@ -937,7 +976,7 @@ pub fn stop(window: Window) {
state(|s| {
s.windows.remove(&window);
s.notices.remove(&window);
- if window.session == MIRROR_SCREEN {
+ if !s.mirroring(&window.device) {
s.screen_off.remove(&window.device);
}
});
@@ -969,7 +1008,6 @@ pub fn on_event(event: &NodeEvent) {
clear_window_buffers(&window);
if *session == MIRROR_SCREEN {
let cleared_kb = state(|s| {
- s.screen_off.remove(device);
if s.keyboard_device == Some(*device) {
s.keyboard_device = None;
true
@@ -984,6 +1022,11 @@ pub fn on_event(event: &NodeEvent) {
if phase(&window).is_some() {
set_phase(window, Phase::Ended(None));
}
+ state(|s| {
+ if !s.mirroring(device) {
+ s.screen_off.remove(device);
+ }
+ });
}
// A phone that went away takes its app list with it.
NodeEvent::LinkChanged { device, link: nectarlink_core::LinkState::Offline { .. } } => {