diff --git a/POWER-OPTIMIZATION.md b/POWER-OPTIMIZATION.md index e0a0315..46a7b6b 100644 --- a/POWER-OPTIMIZATION.md +++ b/POWER-OPTIMIZATION.md @@ -8,7 +8,7 @@ Configured in [kernel/configs/geminux-mt6797x.config](file:///home/lux/proj/gemi - **`CONFIG_HZ_100=y`**: 100 Hz tick rate cuts periodic timer interrupt overhead compared to 250 Hz / 1000 Hz kernels while remaining responsive for interactive X11 workloads. - **`CONFIG_RCU_LAZY=y`**: Batches RCU callbacks during idle periods to extend CPU C-state residency. -- **`CONFIG_SUSPEND=y`**: Enables deep Suspend-to-RAM (`mem` / `deep`) triggered on Gemini PDA lid close (`SW_LID`) or power button press. +- **`CONFIG_SUSPEND=y`**: Enables deep Suspend-to-RAM (`mem` / `deep`) via `mt6797-spm-sleep` (`sodi_idle=0 pmic_modes=0 timer_sec=600` in [etc/modprobe.d/geminux.conf](file:///home/lux/proj/geminux/overlay/rootfs/etc/modprobe.d/geminux.conf)) triggered on Gemini PDA lid close (`SW_LID`), battery idle timeout, or power button press. Keeping `pmic_modes=0` avoids asserting `MT6351` `VCORE` sleep voltage (`0.600 V`) while CPU0 remains powered in `WFI`, `spm_enter()` bounds silent PCM timer rollovers cleanly without `.suspend_again`, `mtk-eint` clears hardware debounce when switching wakeup pins such as `SW_LID` (`EINT5`) to edge-sensitive mode, and `mtk-pmic-keys` reports `pm_wakeup_event()` on wakeup keypresses. - **`CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL=y`**: Uses scheduler utilization feedback (`schedutil`) across the MT6797X tri-cluster topology instead of static `performance` governors. [etc/init.d/geminux-hwinit](file:///home/lux/proj/geminux/overlay/rootfs/etc/init.d/geminux-hwinit) loads `clk-mt6797-mcusys` and `cpufreq-dt` by default (`GEMINUX_ENABLE_CPUFREQ="yes"` in [etc/conf.d/geminux-hwinit](file:///home/lux/proj/geminux/overlay/rootfs/etc/conf.d/geminux-hwinit)) independently of experimental Cortex-A72 bring-up (`GEMINUX_ENABLE_A72="no"`), enabling `policy0` (`221..897 MHz`) and `policy4` (`325..1209 MHz`) frequency scaling and `soc-thermal` cooling throttling. - **`mt6797-gate-unused`**: Loaded at the end of `geminux-hwinit` (`GEMINUX_GATE_UNUSED_CLOCKS="yes"`) to gate ~25 unused bootloader peripheral leaf clocks (unused `SPI`, `UART`, `PWM`, `IRTX`, `ICUSB`, `ANC_MD32`, etc.) left enabled due to `clk_ignore_unused` while leaving active display/I2C clocks untouched. - **Sensor IR LED gating**: `stk3310` is blacklisted in [etc/modprobe.d/geminux.conf](file:///home/lux/proj/geminux/overlay/rootfs/etc/modprobe.d/geminux.conf) (`GEMINUX_ENABLE_SENSORS="no"`) because its probe unconditionally enables continuous ALS and proximity IR LED polling with no userspace consumer. diff --git a/overlay/rootfs/etc/X11/xorg.conf.d/10-geminux-gpu.conf b/overlay/rootfs/etc/X11/xorg.conf.d/10-geminux-gpu.conf index d432384..06c9b5f 100644 --- a/overlay/rootfs/etc/X11/xorg.conf.d/10-geminux-gpu.conf +++ b/overlay/rootfs/etc/X11/xorg.conf.d/10-geminux-gpu.conf @@ -5,7 +5,7 @@ Section "OutputClass" Option "PrimaryGPU" "true" Option "SWcursor" "true" Option "AccelMethod" "none" - Option "TearFree" "false" + Option "TearFree" "true" Option "ShadowFB" "true" Option "PageFlip" "false" EndSection @@ -17,7 +17,7 @@ Section "OutputClass" Option "PrimaryGPU" "true" Option "SWcursor" "true" Option "AccelMethod" "none" - Option "TearFree" "false" + Option "TearFree" "true" Option "ShadowFB" "true" Option "PageFlip" "false" EndSection diff --git a/overlay/rootfs/etc/modprobe.d/geminux.conf b/overlay/rootfs/etc/modprobe.d/geminux.conf index a10008c..e60faeb 100644 --- a/overlay/rootfs/etc/modprobe.d/geminux.conf +++ b/overlay/rootfs/etc/modprobe.d/geminux.conf @@ -1,5 +1,5 @@ # Enforce MT6797X safe hardware initialization order -options mt6797_spm_sleep sodi_idle=0 pmic_modes=1 timer_sec=600 +options mt6797_spm_sleep sodi_idle=0 pmic_modes=0 timer_sec=600 options mt6797_wlan power_save=0 softdep cpufreq_dt pre: clk_mt6797_mcusys softdep mtk_cci_devfreq pre: clk_mt6797_mcusys diff --git a/patches/kernel/0008-gemini-resume-wake-and-spm-standby.patch b/patches/kernel/0008-gemini-resume-wake-and-spm-standby.patch index 18df2c0..acdfc82 100644 --- a/patches/kernel/0008-gemini-resume-wake-and-spm-standby.patch +++ b/patches/kernel/0008-gemini-resume-wake-and-spm-standby.patch @@ -762,7 +762,7 @@ diff --git a/drivers/soc/mediatek/mt6797-ccci-cldma.c b/drivers/soc/mediatek/mt6 diff --git a/drivers/soc/mediatek/mt6797-spm-sleep.c b/drivers/soc/mediatek/mt6797-spm-sleep.c --- a/drivers/soc/mediatek/mt6797-spm-sleep.c +++ b/drivers/soc/mediatek/mt6797-spm-sleep.c -@@ -482,11 +482,11 @@ +@@ -482,7 +482,7 @@ u32 im_resident; /* the program IM holds after a return, or 0 */ } s; @@ -771,12 +771,37 @@ diff --git a/drivers/soc/mediatek/mt6797-spm-sleep.c b/drivers/soc/mediatek/mt67 module_param(timer_sec, uint, 0644); MODULE_PARM_DESC(timer_sec, "PCM wake timer in seconds (1-600)"); --static bool pmic_modes; -+static bool pmic_modes = true; - module_param(pmic_modes, bool, 0644); - MODULE_PARM_DESC(pmic_modes, "Set the static PMIC sleep modes for each suspend"); +@@ -571,7 +571,7 @@ + { 0x0a34, 0xe4, BIT(2) }, /* LDO_VIO28_CON0 */ + { 0x0a4c, 0xe4, BIT(2) }, /* LDO_VIO18_CON0 */ + { 0x0a6e, 0xe4, BIT(2) }, /* LDO_VA10_CON0 */ +- { 0x0a9c, 0xe4, BIT(2) }, /* LDO_VLDO28_CON0 */ ++ { 0x0aa2, 0xe4, BIT(2) }, /* LDO_VLDO28_CON0 */ + { 0x0204, BIT(6), BIT(6) }, /* TOP_CON: oscillator in hardware mode */ + }; -@@ -1388,7 +1388,11 @@ +@@ -1349,10 +1349,19 @@ + static int spm_enter(suspend_state_t state) + { ++ int ret, pass; ++ + if (state != PM_SUSPEND_MEM) + return -EINVAL; + if (!spm_off(CONN_PWR_CON)) + return -EBUSY; + s.sleep.ticks = timer_sec * 32768; +- return spm_run(&s.sleep); ++ for (pass = 0; pass < 6; pass++) { ++ ret = spm_run(&s.sleep); ++ if (ret || !s.entered || !s.wake.returned || s.wake.backstop) ++ break; ++ if (s.wake.r12 != BIT(0) || spm_gic_pending() || pm_wakeup_pending()) ++ break; ++ } ++ return ret; + } + +@@ -1388,7 +1397,11 @@ disable_irq_wake(s.irq); disable_irq(s.irq); spm_pmic_finish(); @@ -789,7 +814,7 @@ diff --git a/drivers/soc/mediatek/mt6797-spm-sleep.c b/drivers/soc/mediatek/mt67 } static void spm_log_wake(const char *what) -@@ -1609,7 +1613,7 @@ +@@ -1609,7 +1622,7 @@ MODULE_PARM_DESC(sodi_idle_max_ms, "SODI idle: longest period in ms (1-2000)"); /* Whether to register the cpuidle driver at all (stage D5d bisects it) */ @@ -798,26 +823,74 @@ diff --git a/drivers/soc/mediatek/mt6797-spm-sleep.c b/drivers/soc/mediatek/mt67 module_param(sodi_idle, bool, 0444); MODULE_PARM_DESC(sodi_idle, "SODI idle: register the CPU0 cpuidle driver"); -@@ -1870,11 +1874,22 @@ - s.idle_registered = false; - } - -+static bool spm_suspend_again(void) -+{ -+ if (!s.entered || !s.wake.returned || s.wake.backstop) -+ return false; -+ if (pm_wakeup_pending()) -+ return false; -+ /* Stay asleep across silent PCM timer rollovers; wake on EINT/other. */ -+ return s.wake.r12 == BIT(0); -+} +diff --git a/drivers/pinctrl/mediatek/mtk-eint.c b/drivers/pinctrl/mediatek/mtk-eint.c +--- a/drivers/pinctrl/mediatek/mtk-eint.c ++++ b/drivers/pinctrl/mediatek/mtk-eint.c +@@ -218,6 +218,17 @@ + if (type & IRQ_TYPE_EDGE_BOTH) { + reg = mtk_eint_get_offset(eint, d->hwirq, eint->regs->sens_clr); + writel(mask, reg); ++ if (eint->hw->db_time && eint->pins[d->hwirq].debounce) { ++ unsigned int idx = eint->pins[d->hwirq].index; ++ unsigned int inst = eint->pins[d->hwirq].instance; ++ unsigned int eint_offset = (idx % 4) * 8; ++ unsigned int clr_offset = (idx / 4) * 4 + ++ eint->regs->dbnc_clr; + - static const struct platform_suspend_ops spm_suspend_ops = { - .valid = spm_valid, - .prepare = spm_prepare, - .enter = spm_enter, - .finish = spm_finish, -+ .suspend_again = spm_suspend_again, - }; ++ writel(0xff << eint_offset, ++ eint->base[inst] + clr_offset); ++ udelay(1); ++ } + } else { + reg = mtk_eint_get_offset(eint, d->hwirq, eint->regs->sens_set); + writel(mask, reg); +diff --git a/drivers/input/keyboard/mtk-pmic-keys.c b/drivers/input/keyboard/mtk-pmic-keys.c +--- a/drivers/input/keyboard/mtk-pmic-keys.c ++++ b/drivers/input/keyboard/mtk-pmic-keys.c +@@ -20,6 +20,7 @@ + #include + #include + #include ++#include + #include - /* ---- load ---- */ + #define MTK_PMIC_RST_DU_MASK GENMASK(9, 8) +@@ -247,6 +248,9 @@ + key_deb &= info->regs->deb_mask; + + pressed = !key_deb; ++ ++ if (info->wakeup) ++ pm_wakeup_event(info->keys->dev, 500); + + input_report_key(info->keys->input_dev, info->keycode, pressed); + input_sync(info->keys->input_dev); +@@ -363,6 +367,7 @@ + { + int error, index = 0; + unsigned int keycount; ++ bool any_wakeup = false; + struct mt6397_chip *pmic_chip = dev_get_drvdata(pdev->dev.parent); + struct device_node *node = pdev->dev.of_node; + static const char *const irqnames[] = { "powerkey", "homekey" }; +@@ -426,8 +431,10 @@ + return error; + } + +- if (of_property_read_bool(child, "wakeup-source")) ++ if (of_property_read_bool(child, "wakeup-source")) { + keys->keys[index].wakeup = true; ++ any_wakeup = true; ++ } + + error = mtk_pmic_key_setup(keys, &keys->keys[index]); + if (error) +@@ -449,6 +456,8 @@ + } + + platform_set_drvdata(pdev, keys); ++ if (any_wakeup) ++ device_init_wakeup(&pdev->dev, true); + + return 0; + } diff --git a/patches/xlibre/0003-fast-90-270-shadow-rotation.patch b/patches/xlibre/0003-fast-90-270-shadow-rotation.patch index e4773fe..fe222ea 100644 --- a/patches/xlibre/0003-fast-90-270-shadow-rotation.patch +++ b/patches/xlibre/0003-fast-90-270-shadow-rotation.patch @@ -1,13 +1,25 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Geminux Project Date: Tue, 24 Nov 2026 10:25:00 +0200 -Subject: [PATCH] geminux: skip redundant shadow->front_bo copy and add 16x16 tiled 32bpp rotation +Subject: [PATCH] geminux: CPU TearFree with direct 16x16 tiled rotation and shadow copy skip --- diff --git a/hw/xfree86/drivers/video/modesetting/driver.c b/hw/xfree86/drivers/video/modesetting/driver.c --- a/hw/xfree86/drivers/video/modesetting/driver.c +++ b/hw/xfree86/drivers/video/modesetting/driver.c -@@ -1666,6 +1666,28 @@ +@@ -1543,8 +1543,9 @@ + if (pScrn->is_gpu) { + xf86DrvMsg(pScrn->scrnIndex, X_WARNING, + "TearFree cannot synchronize PRIME; use 'PRIME Synchronization' instead\n"); +- } else if (ms->drmmode.glamor_gbm) { ++ } else if (ms->drmmode.glamor_gbm || ms->drmmode.shadow_enable) { + if (!ms->drmmode.pageflip || cap_universal_planes) { + ms->drmmode.tearfree_enable = TRUE; +- xf86DrvMsg(pScrn->scrnIndex, X_INFO, "TearFree: enabled\n"); ++ xf86DrvMsg(pScrn->scrnIndex, X_INFO, "TearFree: enabled%s\n", ++ ms->drmmode.glamor_gbm ? "" : " (CPU/ShadowFB)"); + } else { +@@ -1666,6 +1667,31 @@ { ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); modesettingPtr ms = modesettingPTR(pScrn); @@ -15,19 +27,22 @@ diff --git a/hw/xfree86/drivers/video/modesetting/driver.c b/hw/xfree86/drivers/ + + if (xf86_config && xf86_config->num_crtc > 0) { + int enabled_crtcs = 0; -+ Bool all_rotated = TRUE; ++ Bool all_direct_scanout = TRUE; + + for (int c = 0; c < xf86_config->num_crtc; c++) { + xf86CrtcPtr crtc = xf86_config->crtc[c]; ++ drmmode_crtc_private_ptr drmmode_crtc; + if (!crtc || !crtc->enabled) + continue; + enabled_crtcs++; -+ if (!crtc->transform_in_use || !crtc->rotatedPixmap) { -+ all_rotated = FALSE; ++ drmmode_crtc = crtc->driver_private; ++ if (!(drmmode_crtc && drmmode_crtc->tearfree.buf[0].px) && ++ (!crtc->transform_in_use || !crtc->rotatedPixmap)) { ++ all_direct_scanout = FALSE; + break; + } + } -+ if (enabled_crtcs > 0 && all_rotated) { ++ if (enabled_crtcs > 0 && all_direct_scanout) { + DamageEmpty(pBuf->pDamage); + return; + } @@ -36,6 +51,77 @@ diff --git a/hw/xfree86/drivers/video/modesetting/driver.c b/hw/xfree86/drivers/ bool use_3224 = ms->drmmode.force_24_32 && pScrn->bitsPerPixel == 32; if (ms->drmmode.shadow_enable2 && ms->drmmode.shadow_fb2) do { +diff --git a/hw/xfree86/drivers/video/modesetting/drmmode_display.c b/hw/xfree86/drivers/video/modesetting/drmmode_display.c +--- a/hw/xfree86/drivers/video/modesetting/drmmode_display.c ++++ b/hw/xfree86/drivers/video/modesetting/drmmode_display.c +@@ -1429,19 +1429,24 @@ + /* Copy the screen's pixmap into the destination pixmap */ + if (crtc->rotatedPixmap) { + src = &crtc->rotatedPixmap->drawable; + xf86RotateCrtcRedisplay(crtc, dst, src, dmg, FALSE); ++ } else if ((crtc->driverIsPerformingTransform & XF86DriverTransformOutput) && ++ crtc->transform_in_use) { ++ src = &pScreen->GetScreenPixmap(pScreen)->drawable; ++ xf86RotateCrtcRedisplay(crtc, dst, src, dmg, TRUE); + } else { + src = &pScreen->GetScreenPixmap(pScreen)->drawable; + PixmapDirtyCopyArea(dst, src, 0, 0, -crtc->x, -crtc->y, dmg); + } + + /* Reset the damages if requested */ + if (empty) + RegionEmpty(dmg); + + #ifdef GLAMOR + /* Wait until the GC operations finish */ +- modesettingPTR(crtc->scrn)->glamor.finish(pScreen); ++ if (modesettingPTR(crtc->scrn)->drmmode.glamor_gbm) ++ modesettingPTR(crtc->scrn)->glamor.finish(pScreen); + #endif + } + +@@ -1582,7 +1587,9 @@ + crtc->x = x; + crtc->y = y; + crtc->rotation = rotation; ++ if (drmmode->tearfree_enable) ++ crtc->driverIsPerformingTransform |= XF86DriverTransformOutput; + + if (!xf86CrtcRotate(crtc)) { + goto done; + } +@@ -2863,6 +2870,8 @@ + drmmode_crtc->vblank_pipe = drmmode_crtc_vblank_pipe(num); + xorg_list_init(&drmmode_crtc->mode_list); + xorg_list_init(&drmmode_crtc->tearfree.dri_flip_list); + drmmode_crtc->next_msc = UINT64_MAX; ++ if (drmmode->tearfree_enable) ++ crtc->driverIsPerformingTransform |= XF86DriverTransformOutput; + + /* Setup the fallback cursor immediately. */ +@@ -4324,7 +4333,9 @@ + crtc->mode = crtc->desiredMode; + crtc->rotation = crtc->desiredRotation; + crtc->x = crtc->desiredX; + crtc->y = crtc->desiredY; ++ if (drmmode->tearfree_enable) ++ crtc->driverIsPerformingTransform |= XF86DriverTransformOutput; + if (!xf86CrtcRotate(crtc)) + return FALSE; + } +diff --git a/hw/xfree86/drivers/video/modesetting/pageflip.c b/hw/xfree86/drivers/video/modesetting/pageflip.c +--- a/hw/xfree86/drivers/video/modesetting/pageflip.c ++++ b/hw/xfree86/drivers/video/modesetting/pageflip.c +@@ -414,6 +414,9 @@ + return TRUE; + } + ++ if (!ms->drmmode.glamor_gbm) ++ goto error_free_event; ++ + ms->glamor.block_handler(screen); + + new_front_bo = ms->glamor.gbm_bo_from_pixmap(screen, new_front); diff --git a/hw/xfree86/modes/xf86Rotate.c b/hw/xfree86/modes/xf86Rotate.c --- a/hw/xfree86/modes/xf86Rotate.c +++ b/hw/xfree86/modes/xf86Rotate.c @@ -222,8 +308,10 @@ diff --git a/hw/xfree86/modes/xf86Rotate.c b/hw/xfree86/modes/xf86Rotate.c void xf86RotateCrtcRedisplay(xf86CrtcPtr crtc, PixmapPtr dst_pixmap, DrawableRec *src_drawable, RegionPtr region, -@@ -57,6 +226,10 @@ - if (crtc->driverIsPerformingTransform & XF86DriverTransformOutput) +@@ -57,6 +226,11 @@ +- if (crtc->driverIsPerformingTransform & XF86DriverTransformOutput) ++ if ((crtc->driverIsPerformingTransform & XF86DriverTransformOutput) && ++ (!dst_pixmap || dst_pixmap == crtc->rotatedPixmap)) return; + if (xf86RotateTryFast32(crtc, dst_pixmap, src_drawable, region, diff --git a/prebuilt/geminux/usr/sbin/geminux-powerd b/prebuilt/geminux/usr/sbin/geminux-powerd index 8174b8b..825eac8 100755 Binary files a/prebuilt/geminux/usr/sbin/geminux-powerd and b/prebuilt/geminux/usr/sbin/geminux-powerd differ diff --git a/prebuilt/kernel/boot/Image.gz b/prebuilt/kernel/boot/Image.gz index ca98233..03baa04 100644 Binary files a/prebuilt/kernel/boot/Image.gz and b/prebuilt/kernel/boot/Image.gz differ diff --git a/prebuilt/kernel/lib/modules/7.3.5-geminux/kernel/drivers/soc/mediatek/mt6797-spm-sleep.ko.zst b/prebuilt/kernel/lib/modules/7.3.5-geminux/kernel/drivers/soc/mediatek/mt6797-spm-sleep.ko.zst index 8096c55..3b4bbab 100644 Binary files a/prebuilt/kernel/lib/modules/7.3.5-geminux/kernel/drivers/soc/mediatek/mt6797-spm-sleep.ko.zst and b/prebuilt/kernel/lib/modules/7.3.5-geminux/kernel/drivers/soc/mediatek/mt6797-spm-sleep.ko.zst differ diff --git a/prebuilt/suckless/usr/bin/st b/prebuilt/suckless/usr/bin/st index abfd3e7..3c13f27 100755 Binary files a/prebuilt/suckless/usr/bin/st and b/prebuilt/suckless/usr/bin/st differ diff --git a/prebuilt/xlibre/usr/bin/Xorg b/prebuilt/xlibre/usr/bin/Xorg index f99d998..7b07aac 100755 Binary files a/prebuilt/xlibre/usr/bin/Xorg and b/prebuilt/xlibre/usr/bin/Xorg differ diff --git a/prebuilt/xlibre/usr/lib/xorg/modules/xlibre-25/drivers/modesetting_drv.so b/prebuilt/xlibre/usr/lib/xorg/modules/xlibre-25/drivers/modesetting_drv.so index 1e31229..e919901 100755 Binary files a/prebuilt/xlibre/usr/lib/xorg/modules/xlibre-25/drivers/modesetting_drv.so and b/prebuilt/xlibre/usr/lib/xorg/modules/xlibre-25/drivers/modesetting_drv.so differ diff --git a/scripts/test-power-policy.py b/scripts/test-power-policy.py index ebe41b7..418245f 100755 --- a/scripts/test-power-policy.py +++ b/scripts/test-power-policy.py @@ -668,9 +668,13 @@ def main(): os.path.join(overlay_dir, "etc", "modprobe.d", "geminux.conf"): ( "blacklist stk3310", "softdep cpufreq_dt pre: clk_mt6797_mcusys", - "options mt6797_spm_sleep sodi_idle=0 pmic_modes=1 timer_sec=600", + "options mt6797_spm_sleep sodi_idle=0 pmic_modes=0 timer_sec=600", "options mt6797_wlan power_save=0", ), + os.path.join(overlay_dir, "etc", "X11", "xorg.conf.d", "10-geminux-gpu.conf"): ( + 'Option "TearFree" "true"', + 'Option "ShadowFB" "true"', + ), os.path.join(BASE_DIR, "patches", "kernel", "series"): ( "0009-mt6797-wlan-drop-free-stability.patch", ), diff --git a/src/geminux-daemons/geminux-powerd.c b/src/geminux-daemons/geminux-powerd.c index 82fede4..ec2b3a8 100644 --- a/src/geminux-daemons/geminux-powerd.c +++ b/src/geminux-daemons/geminux-powerd.c @@ -305,7 +305,12 @@ static void arm_timer_ms(int tmr_fd, uint64_t wait_ms) { timerfd_settime(tmr_fd, 0, &its, NULL); } +static void restore_consys_after_resume(powerd_ctx_t *ctx); + static void evaluate_idle(powerd_ctx_t *ctx) { + if (ctx->consys_suspended && (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy))) { + restore_consys_after_resume(ctx); + } uint64_t now = monotonic_ms(); uint64_t elapsed = (now >= ctx->last_input_ms) ? (now - ctx->last_input_ms) : 0; uint64_t next_wait_ms = 0; @@ -330,6 +335,9 @@ static void evaluate_idle(powerd_ctx_t *ctx) { } static void note_user_activity(powerd_ctx_t *ctx) { + if (ctx->consys_suspended && !ctx->policy.lid_closed) { + restore_consys_after_resume(ctx); + } ctx->last_input_ms = monotonic_ms(); if (ctx->idle_stage != IDLE_STAGE_ACTIVE) { ctx->idle_stage = IDLE_STAGE_ACTIVE; @@ -666,18 +674,23 @@ static void refresh_lid_state(powerd_ctx_t *ctx) { /* gpio-keys SW_LID applies a 64 ms debounce timer on wakeup; wait up to * timeout_ms using poll() so a lid-open wake sees SW_LID == 0 before we - * decide whether to light the backlight. */ -static void wait_post_resume_lid_events(powerd_ctx_t *ctx, int timeout_ms) { + * decide whether to light the backlight. Returns true if an input/lid wake + * event was observed or the lid is open. */ +static bool wait_post_resume_lid_events(powerd_ctx_t *ctx, int timeout_ms) { + bool saw_wake_input = false; refresh_lid_state(ctx); - if (!ctx->policy.lid_closed || ctx->num_ev <= 0) return; + if (ctx->num_ev <= 0) return !ctx->policy.lid_closed; - uint64_t deadline = monotonic_ms() + (uint64_t)timeout_ms; + uint64_t deadline = ctx->policy.lid_closed ? (monotonic_ms() + (uint64_t)timeout_ms) : 0; struct pollfd pfds[MAX_EVDEVS]; - while (ctx->policy.lid_closed) { - uint64_t now = monotonic_ms(); - if (now >= deadline) break; - int slice = (int)(deadline - now); - if (slice > 20) slice = 20; + for (;;) { + int slice = 0; + if (ctx->policy.lid_closed) { + uint64_t now = monotonic_ms(); + if (now >= deadline) break; + slice = (int)(deadline - now); + if (slice > 20) slice = 20; + } for (int i = 0; i < ctx->num_ev; i++) { pfds[i].fd = ctx->ev_fds[i]; @@ -695,13 +708,21 @@ static void wait_post_resume_lid_events(powerd_ctx_t *ctx, int timeout_ms) { while (read(ctx->ev_fds[i], &iev, sizeof(iev)) == (ssize_t)sizeof(iev)) { if (iev.type == EV_SW && iev.code == SW_LID) { ctx->policy.lid_closed = (iev.value != 0); - } else if (iev.type == EV_KEY || iev.type == EV_REL || iev.type == EV_ABS) { + if (!ctx->policy.lid_closed) saw_wake_input = true; + } else if ((iev.type == EV_KEY && iev.value != 0) || + iev.type == EV_REL || iev.type == EV_ABS) { ctx->last_input_ms = monotonic_ms(); + saw_wake_input = true; } } } refresh_lid_state(ctx); + if (!ctx->policy.lid_closed) { + saw_wake_input = true; + break; + } } + return saw_wake_input; } static void prepare_cpus_for_suspend(void) { @@ -749,18 +770,26 @@ static void trigger_suspend(powerd_ctx_t *ctx) { bool suspended = (write_sysfs_str("/sys/power/state", "mem") == 0 || write_sysfs_str("/sys/power/state", "freeze") == 0); restore_cpus_after_resume(ctx); - restore_consys_after_resume(ctx); - wait_post_resume_lid_events(ctx, 90); + bool saw_wake_input = wait_post_resume_lid_events(ctx, 90); refresh_ac_and_hdmi(ctx); - if (suspended && !ctx->policy.lid_closed) { + if (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy)) { + restore_consys_after_resume(ctx); + } + if ((suspended || saw_wake_input) && !ctx->policy.lid_closed) { ctx->idle_stage = IDLE_STAGE_ACTIVE; ctx->last_input_ms = monotonic_ms(); toggle_backlight(ctx, true); apply_cpu_policy(ctx, true); + evaluate_idle(ctx); } else { toggle_backlight(ctx, false); ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK; - arm_timer_ms(ctx->tmr_fd, 0); + if (ctx->policy.lid_closed ? geminux_should_suspend_on_lid(&ctx->policy) + : (!ctx->policy.on_ac_power && !saw_wake_input)) { + arm_timer_ms(ctx->tmr_fd, 2000); + } else { + arm_timer_ms(ctx->tmr_fd, 0); + } apply_cpu_sysfs(ctx, true); } } @@ -817,6 +846,9 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) { trigger_suspend(ctx); } else if (strncmp(buf, "inhibit add ", 12) == 0) { geminux_policy_add_inhibit(&ctx->policy, buf + 12); + if (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy)) { + restore_consys_after_resume(ctx); + } apply_cpu_policy(ctx, false); dprintf(cfd, "OK inhibits=%d\n", ctx->policy.inhibit_count); } else if (strncmp(buf, "inhibit del ", 12) == 0) { @@ -828,6 +860,7 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) { dprintf(cfd, "OK lid_suspend=on\n"); } else if (strcmp(buf, "lid-suspend off") == 0) { ctx->policy.lid_suspend_enabled = false; + restore_consys_after_resume(ctx); dprintf(cfd, "OK lid_suspend=off\n"); } else if (strcmp(buf, "status") == 0) { dprintf(cfd, "profile=%s ac=%d hdmi=%d lid_closed=%d lid_suspend=%d inhibits=%d\n", @@ -943,7 +976,12 @@ int main(void) { uint64_t exp = 0; while (read(ctx.tmr_fd, &exp, sizeof(exp)) > 0) {} refresh_ac_and_hdmi(&ctx); - evaluate_idle(&ctx); + if (ctx.policy.lid_closed && geminux_should_suspend_on_lid(&ctx.policy)) { + trigger_suspend(&ctx); + } else { + restore_consys_after_resume(&ctx); + evaluate_idle(&ctx); + } } else if (fd == ctx.uev_fd) { char ubuf[2048]; ssize_t ur; @@ -973,6 +1011,9 @@ int main(void) { bool prev_ac = ctx.policy.on_ac_power; bool prev_hdmi = ctx.policy.hdmi_connected; refresh_ac_and_hdmi(&ctx); + if (!ctx.policy.lid_closed || !geminux_should_suspend_on_lid(&ctx.policy)) { + restore_consys_after_resume(&ctx); + } if (prev_ac != ctx.policy.on_ac_power || prev_hdmi != ctx.policy.hdmi_connected) { if (!ctx.policy.lid_closed) { note_user_activity(&ctx); @@ -993,14 +1034,17 @@ int main(void) { if (geminux_should_suspend_on_lid(&ctx.policy)) { trigger_suspend(&ctx); } else { + restore_consys_after_resume(&ctx); toggle_backlight(&ctx, false); } } else { + restore_consys_after_resume(&ctx); ctx.idle_stage = IDLE_STAGE_ACTIVE; ctx.last_input_ms = monotonic_ms(); toggle_backlight(&ctx, true); refresh_ac_and_hdmi(&ctx); apply_cpu_policy(&ctx, true); + evaluate_idle(&ctx); } } else if (iev.type == EV_KEY && iev.code == KEY_POWER && iev.value == 1) { if (ctx.idle_stage == IDLE_STAGE_LOCKED_BLANK) { diff --git a/src/st/x.c b/src/st/x.c index e2381dc..2a0e99c 100644 --- a/src/st/x.c +++ b/src/st/x.c @@ -286,10 +286,122 @@ static int invertcolors = 0; static int oldbutton = 3; /* button event on startup: 3 = release */ static int selectionmodeenabled = 1; +#define MAX_XDIRTY_RECTS 32 + +typedef struct { + int x1, y1, x2, y2; +} XDirtyRect; + +typedef struct { + XftFont *font; + FT_UInt glyph; + uint32_t fg; + uint32_t bg; + ushort mode; + uchar nonspace; + uchar valid; +} XRenderedCell; + +static XDirtyRect xdirty[MAX_XDIRTY_RECTS]; +static int xdirty_cnt = 0; +static int xw_buf_ready = 0; +static XRenderedCell *xw_cells = NULL; +static int xw_cells_cols = 0; +static int xw_cells_rows = 0; +static int xlast_cur_drawn = 0; + +static void +xinvalidatecells(void) +{ + if (xw_cells && xw_cells_cols > 0 && xw_cells_rows > 0) + memset(xw_cells, 0, (size_t)xw_cells_cols * (size_t)xw_cells_rows * sizeof(*xw_cells)); + xlast_cur_drawn = 0; +} + +static void +xinvalidatecellrange(int y, int x1, int x2) +{ + int x; + + if (!xw_cells || y < 0 || y >= xw_cells_rows) + return; + if (x1 < 0) + x1 = 0; + if (x2 > xw_cells_cols) + x2 = xw_cells_cols; + for (x = x1; x < x2; x++) + xw_cells[y * xw_cells_cols + x].valid = 0; +} + +static void +xmarkdirty(int x1, int y1, int x2, int y2) +{ + int i; + + if (x1 < 0) + x1 = 0; + if (y1 < 0) + y1 = 0; + if (x2 > win.w) + x2 = win.w; + if (y2 > win.h) + y2 = win.h; + if (x1 >= x2 || y1 >= y2) + return; + + for (i = 0; i < xdirty_cnt; i++) { + if (x1 >= xdirty[i].x1 && y1 >= xdirty[i].y1 && + x2 <= xdirty[i].x2 && y2 <= xdirty[i].y2) + return; + if ((y1 == xdirty[i].y1 && y2 == xdirty[i].y2 && + x1 <= xdirty[i].x2 + win.cw && x2 >= xdirty[i].x1 - win.cw) || + (x1 == xdirty[i].x1 && x2 == xdirty[i].x2 && + y1 <= xdirty[i].y2 && y2 >= xdirty[i].y1) || + (x1 < xdirty[i].x2 && x2 > xdirty[i].x1 && + y1 < xdirty[i].y2 && y2 > xdirty[i].y1)) { + if (x1 < xdirty[i].x1) + xdirty[i].x1 = x1; + if (y1 < xdirty[i].y1) + xdirty[i].y1 = y1; + if (x2 > xdirty[i].x2) + xdirty[i].x2 = x2; + if (y2 > xdirty[i].y2) + xdirty[i].y2 = y2; + return; + } + } + + if (xdirty_cnt >= MAX_XDIRTY_RECTS) { + for (i = 1; i < xdirty_cnt; i++) { + if (xdirty[i].x1 < xdirty[0].x1) + xdirty[0].x1 = xdirty[i].x1; + if (xdirty[i].y1 < xdirty[0].y1) + xdirty[0].y1 = xdirty[i].y1; + if (xdirty[i].x2 > xdirty[0].x2) + xdirty[0].x2 = xdirty[i].x2; + if (xdirty[i].y2 > xdirty[0].y2) + xdirty[0].y2 = xdirty[i].y2; + } + if (x1 < xdirty[0].x1) + xdirty[0].x1 = x1; + if (y1 < xdirty[0].y1) + xdirty[0].y1 = y1; + if (x2 > xdirty[0].x2) + xdirty[0].x2 = x2; + if (y2 > xdirty[0].y2) + xdirty[0].y2 = y2; + xdirty_cnt = 1; + return; + } + + xdirty[xdirty_cnt++] = (XDirtyRect){ x1, y1, x2, y2 }; +} + void invert(const Arg *dummy) { invertcolors = !invertcolors; + xinvalidatecells(); redraw(); } @@ -814,6 +926,13 @@ xresize(int col, int row) win.tw = col * win.cw; win.th = row * win.ch; + xw_cells = xrealloc(xw_cells, (size_t)col * (size_t)row * sizeof(*xw_cells)); + xw_cells_cols = col; + xw_cells_rows = row; + xinvalidatecells(); + xw_buf_ready = 0; + xdirty_cnt = 0; + XFreePixmap(xw.dpy, xw.buf); xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, DefaultDepth(xw.dpy, xw.scr)); @@ -877,6 +996,7 @@ xloadcols(void) die("could not allocate color %d\n", i); } loaded = 1; + xinvalidatecells(); } int @@ -892,6 +1012,7 @@ xsetcolorname(int x, const char *name) XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]); dc.col[x] = ncolor; + xinvalidatecells(); return 0; } @@ -907,7 +1028,11 @@ xclear(int x1, int y1, int x2, int y2) if (invertcolors) { c = invertedcolor(&c); } + if (x2 > borderpx && x1 < borderpx + win.tw && + y2 > borderpx && y1 < borderpx + win.th) + xinvalidatecells(); XftDrawRect(xw.draw, &c, x1, y1, x2-x1, y2-y1); + xmarkdirty(x1, y1, x2, y2); } void @@ -1239,6 +1364,12 @@ xinit(int cols, int rows) DefaultDepth(xw.dpy, xw.scr)); XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel); XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h); + xw_cells = xmalloc((size_t)cols * (size_t)rows * sizeof(*xw_cells)); + xw_cells_cols = cols; + xw_cells_rows = rows; + xinvalidatecells(); + xw_buf_ready = 0; + xmarkdirty(0, 0, win.w, win.h); /* font spec buffer */ xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec)); @@ -1586,6 +1717,7 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i /* Reset clip to none. */ XftDrawSetClip(xw.draw, 0); + xmarkdirty(winx, winy, winx + width, winy + win.ch); } void @@ -1605,9 +1737,13 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) XRenderColor colbg; /* remove the old cursor */ - if (selected(ox, oy)) - og.mode ^= ATTR_REVERSE; - xdrawglyph(og, ox, oy); + if (xlast_cur_drawn || !IS_SET(MODE_HIDE)) { + if (selected(ox, oy)) + og.mode ^= ATTR_REVERSE; + xinvalidatecellrange(oy, ox, ox + ((og.mode & ATTR_WIDE) ? 2 : 1)); + xdrawglyph(og, ox, oy); + xlast_cur_drawn = 0; + } if (IS_SET(MODE_HIDE)) return; @@ -1666,6 +1802,8 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) borderpx + (cy + 1) * win.ch - \ cursorthickness, win.cw, cursorthickness); + xmarkdirty(borderpx + cx * win.cw, borderpx + cy * win.ch, + borderpx + (cx + 1) * win.cw, borderpx + (cy + 1) * win.ch); break; case 5: /* Blinking bar */ case 6: /* Steady bar */ @@ -1673,6 +1811,8 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) borderpx + cx * win.cw, borderpx + cy * win.ch, cursorthickness, win.ch); + xmarkdirty(borderpx + cx * win.cw, borderpx + cy * win.ch, + borderpx + (cx + 1) * win.cw, borderpx + (cy + 1) * win.ch); break; } } else { @@ -1692,7 +1832,11 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) borderpx + cx * win.cw, borderpx + (cy + 1) * win.ch - 1, win.cw, 1); + xmarkdirty(borderpx + cx * win.cw, borderpx + cy * win.ch, + borderpx + (cx + 1) * win.cw, borderpx + (cy + 1) * win.ch); } + xinvalidatecellrange(cy, cx, cx + ((g.mode & ATTR_WIDE) ? 2 : 1)); + xlast_cur_drawn = 1; } void @@ -1731,6 +1875,116 @@ xdrawline(Line line, int x1, int y1, int x2) XftGlyphFontSpec *specs = xw.specbuf; numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1); + if (xw_cells && y1 >= 0 && y1 < xw_cells_rows && x1 >= 0 && x2 <= xw_cells_cols && + x2 > x1 && (x2 - x1) <= 512) { + int n = x2 - x1; + uchar draw_col[512]; + uchar tok_col[512]; + int sidx = 0; + int any_dirty = 0; + + for (int k = 0; k < n; k++) { + Glyph g = line[x1 + k]; + if (g.mode != ATTR_WDUMMY && selected(x1 + k, y1)) + g.mode ^= ATTR_REVERSE; + ushort eff_mode = g.mode; + if ((eff_mode & ATTR_BLINK) && (win.mode & MODE_BLINK)) + eff_mode |= ATTR_INVISIBLE; + uchar cur_ns = (g.mode == ATTR_WDUMMY) || (g.u != ' ' && g.u != 0); + XRenderedCell *rc = &xw_cells[y1 * xw_cells_cols + x1 + k]; + tok_col[k] = cur_ns || (rc->valid && rc->nonspace); + + int matches = 0; + if (g.mode == ATTR_WDUMMY) { + matches = rc->valid && rc->mode == ATTR_WDUMMY; + } else if (sidx < numspecs) { + matches = rc->valid && + rc->font == specs[sidx].font && + rc->glyph == specs[sidx].glyph && + rc->fg == g.fg && + rc->bg == g.bg && + rc->mode == eff_mode; + sidx++; + } + draw_col[k] = !matches; + if (!matches) + any_dirty = 1; + } + if (!any_dirty) + return; + + for (int k = 0; k < n - 1; k++) { + if (draw_col[k] && + (line[x1 + k + 1].mode == ATTR_WDUMMY || + (tok_col[k] && tok_col[k + 1]))) + draw_col[k + 1] = 1; + } + for (int k = n - 1; k > 0; k--) { + if (draw_col[k] && + (line[x1 + k].mode == ATTR_WDUMMY || + (tok_col[k] && tok_col[k - 1]))) + draw_col[k - 1] = 1; + } + + i = ox = 0; + for (x = x1; x < x2; x++) { + new = line[x]; + if (new.mode == ATTR_WDUMMY) { + if (draw_col[x - x1]) { + xw_cells[y1 * xw_cells_cols + x] = (XRenderedCell){ + .mode = ATTR_WDUMMY, + .nonspace = 1, + .valid = 1, + }; + } + continue; + } + if (i >= numspecs) + break; + if (!draw_col[x - x1]) { + if (i > 0) { + xdrawglyphfontspecs(specs, base, i, ox, y1); + specs += i; + numspecs -= i; + i = 0; + } + specs++; + numspecs--; + continue; + } + if (selected(x, y1)) + new.mode ^= ATTR_REVERSE; + if (i > 0 && ATTRCMP(base, new)) { + xdrawglyphfontspecs(specs, base, i, ox, y1); + specs += i; + numspecs -= i; + i = 0; + } + if (i == 0) { + ox = x; + base = new; + } + { + ushort eff_mode = new.mode; + if ((eff_mode & ATTR_BLINK) && (win.mode & MODE_BLINK)) + eff_mode |= ATTR_INVISIBLE; + xw_cells[y1 * xw_cells_cols + x] = (XRenderedCell){ + .font = specs[i].font, + .glyph = specs[i].glyph, + .fg = new.fg, + .bg = new.bg, + .mode = eff_mode, + .nonspace = (new.u != ' ' && new.u != 0), + .valid = 1, + }; + } + i++; + } + if (i > 0) + xdrawglyphfontspecs(specs, base, i, ox, y1); + return; + } + i = ox = 0; for (x = x1; x < x2 && i < numspecs; x++) { new = line[x]; @@ -1757,8 +2011,17 @@ xdrawline(Line line, int x1, int y1, int x2) void xfinishdraw(void) { - XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w, - win.h, 0, 0); + int i; + + for (i = 0; i < xdirty_cnt; i++) { + XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, + xdirty[i].x1, xdirty[i].y1, + xdirty[i].x2 - xdirty[i].x1, + xdirty[i].y2 - xdirty[i].y1, + xdirty[i].x1, xdirty[i].y1); + } + xdirty_cnt = 0; + xw_buf_ready = 1; XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg].pixel); @@ -1774,6 +2037,15 @@ xximspot(int x, int y) void expose(XEvent *ev) { + XExposeEvent *e = &ev->xexpose; + + if (xw_buf_ready && xw.buf != None) { + if (e->width > 0 && e->height > 0) + XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, + e->x, e->y, e->width, e->height, e->x, e->y); + return; + } + xinvalidatecells(); redraw(); } @@ -1855,8 +2127,10 @@ xsetmode(int set, unsigned int flags) { int mode = win.mode; MODBIT(win.mode, set, flags); - if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE)) + if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE)) { + xinvalidatecells(); redraw(); + } } int @@ -1899,6 +2173,8 @@ focus(XEvent *ev) return; win.mode |= MODE_FOCUSED; xseturgency(0); + if (!IS_SET(MODE_HIDE)) + xlast_cur_drawn = 1; if (IS_SET(MODE_FOCUS)) ttywrite("\033[I", 3, 0); redraw(); @@ -1909,6 +2185,8 @@ focus(XEvent *ev) if (e->mode == NotifyGrab) return; win.mode &= ~MODE_FOCUSED; + if (!IS_SET(MODE_HIDE)) + xlast_cur_drawn = 1; if (IS_SET(MODE_FOCUS)) ttywrite("\033[O", 3, 0); redraw();