diff --git a/app/src/main/java/com/droiddeck/launcher/session/BatteryComponent.kt b/app/src/main/java/com/droiddeck/launcher/session/BatteryComponent.kt index f15a8517b..9b0834e17 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/BatteryComponent.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/BatteryComponent.kt @@ -18,11 +18,15 @@ import kotlin.math.abs * from Android's own battery API - the same on every device - into a directory the session binds * over `/sys/class/power_supply`, refreshed every few seconds while the session runs. * + * Steam's Quick Access Menu takes its time estimates from neither: it reads the files in `/run/vpower` (as on + * SteamOS), and with those absent showed "?h ?m" for projected battery life and for time to full. + * The same estimates are written there too, into [vpowerDir] in the guest's own `/run`. + * * Values: charge in µAh from the charge counter; energy in µWh from charge × voltage; power in µW * from |current| × voltage; time to empty from charge ÷ |average current| while discharging. Where * a device reports no current, the client still gets status and percentage. */ -class BatteryComponent(val dir: File) : SessionPart() { +class BatteryComponent(val dir: File, private val vpowerDir: File) : SessionPart() { @Volatile private var running = false private var thread: Thread? = null @@ -42,6 +46,8 @@ class BatteryComponent(val dir: File) : SessionPart() { running = false thread?.interrupt() thread = null + // Stale estimates would outlive the session in the rootfs; Steam falls back to "?h ?m". + for (name in VPOWER_FILES) File(vpowerDir, name).delete() } private fun write() { @@ -115,6 +121,17 @@ class BatteryComponent(val dir: File) : SessionPart() { for ((k, v) in attrs) uevent.append("POWER_SUPPLY_").append(k.uppercase()).append('=').append(v).append('\n') put(File(bat, "uevent"), uevent.toString()) } + val plugged = (sticky?.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) ?: 0) != 0 + vpowerDir.mkdirs() + val vpower = linkedMapOf( + "ac_status" to if (plugged) "Connected" else "Disconnected", + "battery_percent" to "%.2f".format(java.util.Locale.ROOT, pct.toDouble()), + "battery_status" to statusText, + // Steam reads a separate file for each direction; -1 = no estimate. + "secs_until_battery_full" to (if (timeToFullS > 0) timeToFullS else -1L).toString(), + "secs_until_shutdown_request" to (if (timeToEmptyS > 0) timeToEmptyS else -1L).toString(), + ) + for ((k, v) in vpower) put(File(vpowerDir, k), v + "\n") } catch (t: Throwable) { Log.w(TAG, "battery: could not write", t) } @@ -130,5 +147,9 @@ class BatteryComponent(val dir: File) : SessionPart() { companion object { private const val TAG = "SessionService" private const val PERIOD_MS = 5_000L + private val VPOWER_FILES = listOf( + "ac_status", "battery_percent", "battery_status", + "secs_until_battery_full", "secs_until_shutdown_request", + ) } } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 4114c627e..2bca7f669 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -674,7 +674,11 @@ class SessionService : Service() { // /sys/class/power_supply/BAT/..., a laptop's or a Deck's naming; Android's supply is // called "battery" and its files differ, so the client sees no battery at all. A directory // of our own, written from Android's battery API every few seconds, is bound over it. - val battery = BatteryComponent(File(filesDir, "session/sys/power_supply")) + // Steam's time estimates come from /run/vpower instead, written straight into the rootfs. + val battery = BatteryComponent( + File(filesDir, "session/sys/power_supply"), + File(LinuxRuntime.rootDir(this), "run/vpower"), + ) battery.attach(this) components.add(battery) binds.add(battery.dir.path + ":/sys/class/power_supply") diff --git a/tools/linuxfs/preload/kgslid.c b/tools/linuxfs/preload/kgslid.c new file mode 100644 index 000000000..3dee629d1 --- /dev/null +++ b/tools/linuxfs/preload/kgslid.c @@ -0,0 +1,58 @@ +/* + * Adreno revisions the runtime's Turnip does not know, reported as the revision it does. + * + * Retail SM8850 phones report their A840 as chip_id 0x44050A21 (KGSL gpu_model "Adreno840v2"). + * Mesa only lists 0x44050A31, so the runtime's glibc Turnip refuses the device outright + * ("device (chip_id = 44050A21, gpu_id = 6860) is unsupported") and gamescope exits before Steam + * starts. Banners-Turnip fixes its own builds by registering the v2 id on the same A840 GPU info + * (patches/a840v2.py); answering KGSL's device-info query with the known id has the same effect + * on any Turnip in the session, including the one the runtime was built with. A driver that + * already knows the v2 id resolves the rewritten one to the same entry. + * + * Only the KGSL device-info property is touched. Its layout is the kernel's uapi (msm_kgsl.h). + */ +#define _GNU_SOURCE 1 +#include +#include + +struct kgsl_devinfo { + unsigned int device_id; + unsigned int chip_id; + unsigned int mmu_enabled; + unsigned long gmem_gpubaseaddr; + unsigned int gpu_id; + size_t gmem_sizebytes; +}; + +struct kgsl_device_getproperty { + unsigned int type; + void *value; + size_t sizebytes; +}; + +#define KGSL_IOC_TYPE 0x09 +#define IOCTL_KGSL_DEVICE_GETPROPERTY _IOWR(KGSL_IOC_TYPE, 0x2, struct kgsl_device_getproperty) +#define KGSL_PROP_DEVICE_INFO 0x1 + +static const struct { + unsigned int reported; + unsigned int known; +} CHIP_IDS[] = { + {0x44050A21, 0x44050A31}, /* Adreno 840v2 -> Adreno 840 */ +}; + +void bl_kgsl_chip_id_fixup(unsigned long request, void *arg, int rc) { + if (rc != 0 || request != IOCTL_KGSL_DEVICE_GETPROPERTY || arg == NULL) return; + struct kgsl_device_getproperty *prop = arg; + if (prop->type != KGSL_PROP_DEVICE_INFO || prop->value == NULL + || prop->sizebytes < sizeof(struct kgsl_devinfo)) { + return; + } + struct kgsl_devinfo *info = prop->value; + for (size_t i = 0; i < sizeof(CHIP_IDS) / sizeof(CHIP_IDS[0]); i++) { + if (info->chip_id == CHIP_IDS[i].reported) { + info->chip_id = CHIP_IDS[i].known; + return; + } + } +} diff --git a/tools/linuxfs/preload/netif.c b/tools/linuxfs/preload/netif.c index a835c6cdd..078f6a301 100644 --- a/tools/linuxfs/preload/netif.c +++ b/tools/linuxfs/preload/netif.c @@ -349,6 +349,7 @@ static int is_link_query(unsigned long request) { || request == SIOCGIFBRDADDR; } +void bl_kgsl_chip_id_fixup(unsigned long request, void *arg, int rc); /* kgslid.c */ int bl_kgsl_poll(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); @@ -364,8 +365,8 @@ int ioctl(int fd, unsigned long request, ...) { if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl"); int rc; - if (bl_kgsl_poll(real, fd, request, arg, &rc)) return rc; - rc = real(fd, request, arg); + if (!bl_kgsl_poll(real, fd, request, arg, &rc)) rc = real(fd, request, arg); + bl_kgsl_chip_id_fixup(request, arg, rc); if (arg == NULL || !is_link_query(request)) return rc; int saved = errno; if (answer_ifreq(request, (struct ifreq *) arg, rc)) return 0;