diff --git a/AGENTS.md b/AGENTS.md index b942ead..6705514 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -135,7 +135,7 @@ Keep this file short. Full details live in `README.md`, `MANUAL.md`, `SDK.md`, ` keypad udev/Xorg rules, BusyBox applet lists, and `scripts/set-bananarchy-hostname.sh`. - The Power menu label is **Reboot to loader** and still calls `sxmo_power.sh bootloader`; `Logout` is removed from `dmenu.c`, `dmenu.so`, `sxmo_appmenu.sh`, and `sxmo_hook_contextmenu.sh`. Keep the compact Power menu layout synchronized. -- Keep `msm8905-cam.c` 180-degree rotation enabled for `nokia,beatles`, `nokia,sparkler`, and `nokia,leo` alongside cached `row_buf` RAW10 demosaic; keep `patches/kernel/0013-msm8909-early-scm-smp-cpufreq.patch` (4-core SMP + `cpufreq` up to `1.267 GHz`), disabled `vibrator@c000` + `157.0 Hz` haptic audio (`sxmo_vibrate.c`), `DEFAULT_USB_TIMEOUT_MS = 30000` (`--usb-timeout`), and `sxmo_timer.sh` / `dmenu.c` keypad cancellation (`scripts/test-timer.py`, `scripts/test-sparkler-port.py`, `scripts/test-edl-firehose-read.py`). +- Keep `msm8905-cam.c` 180-degree rotation enabled for `nokia,beatles`, `nokia,sparkler`, and `nokia,leo` alongside cached `row_buf` RAW10 demosaic; keep `patches/kernel/0013-msm8909-early-scm-smp-cpufreq.patch` (4-core SMP + `cpufreq` up to `1.267 GHz`), disabled `vibrator@c000` + volume-independent calibrated `157.0 Hz` haptic audio (`sxmo_vibrate.c`, `libpulse` + `SND_PCM_FORMAT_FLOAT_LE`, `HAPTIC_REF_RX3_VOL = 84`), `DEFAULT_USB_TIMEOUT_MS = 30000` (`--usb-timeout`), and `sxmo_timer.sh` / `dmenu.c` keypad cancellation (`scripts/test-timer.py`, `scripts/test-sparkler-port.py`, `scripts/test-edl-firehose-read.py`). - Bluetooth HOGP config and plugin load are verified, but full hardware sign-off still needs a wake-up paired keyboard and **Connect Device** to confirm real HID key events. diff --git a/CHANGELOG.md b/CHANGELOG.md index 842c061..bf353de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ Upcoming release build (`a1120` / `cocojamboo` via `scripts/version.sh`) targeti - **Layered `lk2nd` port (`bootloader/lk2nd/nokia-sparkler.patch`)**: added `nokia-sparkler.dtsi` (`msm8909-mtp.dts` `/lk2nd/nokia-sparkler`, matching `androidboot.hw.name=sparkler` without requiring `lk2nd,match-panel;`, GC9307 HLT 15-command and ST7789VX2 CTC 11-command 2.8" SPI init streams on BLSP1 QUP5 `0x078b9000`, `*` on `gpio90` as `KEY_VOLUMEUP` `0x115` alongside `#` `pm8941_resin` and Top Power) plus `"nokia,sparkler"` / `"nokia,leo"` support in `spi-display.c`; added `scripts/build-lk2nd-sparkler.sh`, `scripts/check-lk2nd-sparkler.py`, and `scripts/test-lk2nd-sparkler-port.py`, building `images/boot-lk2nd-nokia-sparkler{,-diagnostic,-display}.img` without altering `nokia-beatles` or `nokia-coconut` `lk2nd` images. - **4-core Cortex-A7 SMP, `cpufreq` DVFS up to `1.267 GHz`, and clean fast X11 startup (`0013`)**: added `patches/kernel/0013-msm8909-early-scm-smp-cpufreq.patch` allocating a cache-flushed `early_virt` page for 6-argument `qcom_scm_set_boot_addr_mc()` (`ARM_SMCCC_STD_CALL`) before `qcom_scm_probe()` creates its mempool, guarding `qcom-cpufreq-nvmem.c` with `cpu_present(cpu)`, and adding `clock-frequency = <1094400000>;` to `/cpus/cpu@0..3` in `qcom-msm8909.dtsi` and `nokia-{sparkler,leo}.dts`; ordered `kill-plymouth` (`after dbus elogind`) and raised `Xorg :0` readiness wait to `10 s` in `bananarchy-x11-session.sh`, verified live with `present=0-3`, `online=0-3` (`nproc=4`), `schedutil` scaling to `1.2672 GHz` (`a53pll`), zero SMP/`clock-frequency`/`cpuidle` boot errors, and instant first-pass `dwm` / `fcitx5` startup. -- **Kernel DTS, 157 Hz speaker-LRA haptic audio, dual-GPIO torch, and upright cached-row camera (`msm8905-cam` / `camera.so`)**: set `deviceinfo_drm="true"` and `matrix_keypad`/`matrix_keymap` in `devices/nokia-sparkler/modules-initfs`; updated `qcom-msm8909-nokia-sparkler.dts` and `qcom-msm8909-nokia-sparkler.dtb` with stock MSM8909 MTP root IDs (`0xf5`/`0x8`/`0x100`/`0x1000d`), `512 MiB` `/memory`, QDSP6/Q6Voice audio (`wcd_codec: audio-codec@f000`, `apr`, `sound@7702000` with `dai-link-multimedia3` disabled, `lpass_codec: audio-codec@771c000`), Sxmo keypad codes (`0x500d4` `KEY_CAMERA`, `0x100007f` `KEY_COMPOSE`, `0x205000e` `BackSpace`, `0x301009e` `KEY_BACK`), and disabled unpopulated PM8909 `vibrator@c000` (`status = "disabled";`); added native `157.0 Hz` speaker-LRA haptic audio (`vibrate_haptic_audio()` over PipeWire/ALSA `default` with `500 ms` buffer and `plughw:0,0` fallback) to `src/dwm-plugins/sxmo_vibrate.c` (`links libasound` + `libm`); updated `src/leds-msm8905-torch/leds-msm8905-torch.c` (`GPIO_32` `flash-en` + `GPIO_31` `flash-now` `BC847BS`); added `nokia,sparkler` and `nokia,leo` to `rotate_180` plus cached `row_buf[2][CAM_RAW_STRIDE]` burst copies in `src/msm8905-cam/msm8905-cam.c` and reduced `camera_plugin.cpp` post-frame sleep to `10 ms` (verified `/dev/video0` frame time reduced from `1.34 s` to `0.193–0.229 s/frame` with upright JPEG output); wired `nokia-sparkler` across `check-beatles-bootimg.py`, `sync-beatles-bootfs.py`, `flash-edl.py`, `prepare-release.sh`, `check-rebuild.sh`, `test-sparkler-port.py`, and docs. +- **Kernel DTS, volume-independent calibrated 157 Hz speaker-LRA haptic audio, dual-GPIO torch, and upright cached-row camera (`msm8905-cam` / `camera.so`)**: set `deviceinfo_drm="true"` and `matrix_keypad`/`matrix_keymap` in `devices/nokia-sparkler/modules-initfs`; updated `qcom-msm8909-nokia-sparkler.dts` and `qcom-msm8909-nokia-sparkler.dtb` with stock MSM8909 MTP root IDs (`0xf5`/`0x8`/`0x100`/`0x1000d`), `512 MiB` `/memory`, QDSP6/Q6Voice audio (`wcd_codec: audio-codec@f000`, `apr`, `sound@7702000` with `dai-link-multimedia3` disabled, `lpass_codec: audio-codec@771c000`), Sxmo keypad codes (`0x500d4` `KEY_CAMERA`, `0x100007f` `KEY_COMPOSE`, `0x205000e` `BackSpace`, `0x301009e` `KEY_BACK`), and disabled unpopulated PM8909 `vibrator@c000` (`status = "disabled";`); added native `157.0 Hz` speaker-LRA haptic audio (`vibrate_haptic_audio()`, `libpulse` sink/input query via `pa_sw_volume_to_linear`, `RX3 Digital Volume` + PipeWire `softVolumes` gain compensation over `SND_PCM_FORMAT_FLOAT_LE`, calibrated `1950` / `2550` / `3300` reference amplitudes at `HAPTIC_REF_RX3_VOL = 84`, `10 ms` cosine fade, `HOME=/home/user` root-drop fix, `plughw:0,0` / muted-sink bypass, and non-speaker exclusion) in `src/dwm-plugins/sxmo_vibrate.c` (`links libasound` + `libpulse` + `libm`), verified live on `nokia-sparkler` across `0%`..`100%` speaker volume (`23.95`–`26.15` RMS at L2) and concurrent playback; updated `src/leds-msm8905-torch/leds-msm8905-torch.c` (`GPIO_32` `flash-en` + `GPIO_31` `flash-now` `BC847BS`); added `nokia,sparkler` and `nokia,leo` to `rotate_180` plus cached `row_buf[2][CAM_RAW_STRIDE]` burst copies in `src/msm8905-cam/msm8905-cam.c` and reduced `camera_plugin.cpp` post-frame sleep to `10 ms` (verified `/dev/video0` frame time reduced from `1.34 s` to `0.193–0.229 s/frame` with upright JPEG output); wired `nokia-sparkler` across `check-beatles-bootimg.py`, `sync-beatles-bootfs.py`, `flash-edl.py`, `prepare-release.sh`, `check-rebuild.sh`, `test-sparkler-port.py`, and docs. - **EDL Firehose ``, USB reconnect, and `30 s` USB timeout (`vendor/edl/Library/{firehose,usb,sahara}.py`, `flash-edl.py`, `MANUAL.md`)**: handled standard Qualcomm `rawmode="true"` XML ACK packets in `qualcomm_firehose` (`_begin_raw_read` / `_read_raw_sectors`) alongside FIH's immediate raw streams and capped reads to `min(bytes_to_read, max_chunk)`, fixing GPT/partition corruption on `8k.mbn` (`nokia-sparkler`, `nokia-leo`) and `2720v.mbn`; skipped redundant `set_configuration()` in `usb_class.connect()` when an active USB configuration already exists; raised per-transfer USB bulk read/write timeout to `30000 ms` (`DEFAULT_USB_TIMEOUT_MS = 30000`, `flash-edl.py --usb-timeout`, `2000 ms` Sahara connect probe, and `5–30 s` Fastboot/ADB kick timeouts); added `nokia-leo` to `UNLOCK_DEFAULT_DEVICES`, suppressed duplicate `Bootloader (boot)` output when a per-device `lk2nd` image is active, updated the EDL/Fastboot button-combo hints to **`*` + `#`** (EDL) and **`#`** (Fastboot) for `nokia-sparkler` and `nokia-leo`, added `scripts/test-edl-firehose-read.py`, and verified full live `--only-backup --dump-diagnostics` extraction from the connected `nokia-sparkler`. ### Keypad-friendly timer countdown and alarm cancellation across all targets @@ -56,10 +56,10 @@ Upcoming release build (`a1120` / `cocojamboo` via `scripts/version.sh`) targeti ### Image persistence and upcoming `a1120` (`cocojamboo`) release archives - **Verified `a1120` / `cocojamboo` image set (`--mkinitfs`) and packaged archives**: - - `images/bananarchy-nokia-argon.img`: SHA-256 `82f617e425fa9d33b1395c4e35639a6207e3c9d0aa376c7cedf35fddaccb5cc2` (packaged in `bananarchy-nokia-argon-a1120-cocojamboo.zip`, SHA-256 `b1d47511ae36384a16db8e1cf64dc5b39ed72afcaf0128d555dbea8bffc2256b`, alongside `bootloader/lk2nd-msm8909.img`). - - `images/bananarchy-nokia-beatles.img`: SHA-256 `cc40c682fcdacb6415c112494344826362de2f965e6238685462859de66d660e`, `images/boot-bananarchy-nokia-beatles.img`: SHA-256 `786e9a1f4e4ae92efc948393a1d8f04a9405840a214b6ef37914cf5821d7ed82`, and `images/boot-lk2nd-nokia-beatles-display.img`: SHA-256 `b5549698c20f4de7603fc746d32bb5baa96ec377e41e74407823c98506fc185d` (packaged in `bananarchy-nokia-beatles-a1120-cocojamboo.zip`, SHA-256 `ab6b0bbea53298fdabb781ac8f29756f6f096872cde80fe31e4e45c851a3329f`). - - `images/bananarchy-nokia-coconut.img`: SHA-256 `e82d64bd9f14b1fcca2c078e9fd43a4c4268b29ff688f268b32a939213be0545`, `images/boot-bananarchy-nokia-coconut.img`: SHA-256 `55299e15abe5923fc525b4e54e81aa4791f0e61f8a070e84fe7497f75c62d7d7`, and `images/boot-lk2nd-nokia-coconut-display.img`: SHA-256 `e876424b38b0782f589756b4107337b24f27f7c37d624cc7d3907a5331df050a` (packaged in `bananarchy-nokia-coconut-a1120-cocojamboo.zip`, SHA-256 `3ff640fcfa4360bd23e354379ffd343dd26a782daf096c770adda917a2ea6e75`). - - `images/bananarchy-nokia-sparkler.img`: SHA-256 `5dc17319742e4af95facc42681f0fe6fb2e80ddf30f421f3c54b9dbe04fefd3f`, `images/boot-bananarchy-nokia-sparkler.img`: SHA-256 `8205e2cb486d8ecc1e261a5c0b6f9ee7f70fd4eed2534d5bafc28ebc62d953d8`, and `images/boot-lk2nd-nokia-sparkler-display.img`: SHA-256 `bc41c2ddd1079246a7e91a40222fcafb82b0b52965c8ee8081872767d006748f` (packaged in `bananarchy-nokia-sparkler-a1120-cocojamboo.zip`, SHA-256 `cd27557b8bc13ca83d04e3e41b40876d7fedb5e1149f744aee1dc8ca7a7bea73`). + - `images/bananarchy-nokia-argon.img`: SHA-256 `6ada66359a650a7e93f8937082a46353171989a54de19661821c1b91ea642ae3` (packaged in `bananarchy-nokia-argon-a1120-cocojamboo.zip`, SHA-256 `b4c8c1ede10d4dd764362f12bf4c783fd5d2558eb5fd3f0159c099a05a89c9a4`, alongside `bootloader/lk2nd-msm8909.img`). + - `images/bananarchy-nokia-beatles.img`: SHA-256 `33b8b9181d5a23dd4a4bb473d827c6a87cf3407374c2e8b409fc45a7e60881b9`, `images/boot-bananarchy-nokia-beatles.img`: SHA-256 `7e242e51cb3b16fe932d83c5807b0df93188606aa78859c484e3a8ed4414a8ff`, and `images/boot-lk2nd-nokia-beatles-display.img`: SHA-256 `b5549698c20f4de7603fc746d32bb5baa96ec377e41e74407823c98506fc185d` (packaged in `bananarchy-nokia-beatles-a1120-cocojamboo.zip`, SHA-256 `11474af3d5301856da0363b891ae2e4ec7241ecebb54043eb5bfd72297614554`). + - `images/bananarchy-nokia-coconut.img`: SHA-256 `4928a008fc1394e84c2bc2040b91b515eb2f506c39f0c822d8242e40dbf31eb6`, `images/boot-bananarchy-nokia-coconut.img`: SHA-256 `ac4ea812933bb9d990540881110ecf88f1585c31590d4b860834f1553f2f1537`, and `images/boot-lk2nd-nokia-coconut-display.img`: SHA-256 `e876424b38b0782f589756b4107337b24f27f7c37d624cc7d3907a5331df050a` (packaged in `bananarchy-nokia-coconut-a1120-cocojamboo.zip`, SHA-256 `7cb55ff1e9fedda3e2c594e1ec2e9536a09e35e3a9895a5f488c4b2eb4cf5520`). + - `images/bananarchy-nokia-sparkler.img`: SHA-256 `7b8de44bac9cded16f219d723748806a17053886dd1762d7261ac6de308918d6`, `images/boot-bananarchy-nokia-sparkler.img`: SHA-256 `1ae0dfe5f189cbf381cdc26a89d95bfbabf2e267e86091fd1e8b32392460fa79`, and `images/boot-lk2nd-nokia-sparkler-display.img`: SHA-256 `bc41c2ddd1079246a7e91a40222fcafb82b0b52965c8ee8081872767d006748f` (packaged in `bananarchy-nokia-sparkler-a1120-cocojamboo.zip`, SHA-256 `e98a3454ff9df21648cff697a11144498658479c8c971725ff8884650b656528`). --- diff --git a/overlay/rootfs/usr/bin/sxmo_vibrate b/overlay/rootfs/usr/bin/sxmo_vibrate index c6a176b..92ea47e 100755 Binary files a/overlay/rootfs/usr/bin/sxmo_vibrate and b/overlay/rootfs/usr/bin/sxmo_vibrate differ diff --git a/prebuilt/sxmo-x11/usr/bin/sxmo_vibrate b/prebuilt/sxmo-x11/usr/bin/sxmo_vibrate index c6a176b..92ea47e 100755 Binary files a/prebuilt/sxmo-x11/usr/bin/sxmo_vibrate and b/prebuilt/sxmo-x11/usr/bin/sxmo_vibrate differ diff --git a/scripts/build-dwm-plugins.sh b/scripts/build-dwm-plugins.sh index e31bbfb..fce0037 100755 --- a/scripts/build-dwm-plugins.sh +++ b/scripts/build-dwm-plugins.sh @@ -9,7 +9,7 @@ if [ -e "$ROOTFS_DIR" ]; then ROOTFS_DIR="$(realpath "$ROOTFS_DIR")" fi -exec docker run --rm --network=host \ +docker run --rm --network=host \ -e HOST_UID="$(id -u)" \ -e HOST_GID="$(id -g)" \ -v "$BASE_DIR:/bananarchy" \ @@ -41,6 +41,7 @@ EOF musl-dev \ linux-headers \ alsa-lib-dev \ + pulseaudio-dev \ libx11-dev \ libxfixes-dev \ libxft-dev \ @@ -193,9 +194,9 @@ EOF $STRIP --strip-unneeded /bananarchy/prebuilt/sxmo-x11/usr/bin/dwm-plugin $CC $OPT_CFLAGS -std=c99 -D_DEFAULT_SOURCE -Wall -Wextra \ - $(pkgconf --cflags alsa) \ + $(pkgconf --cflags alsa libpulse) \ /bananarchy/src/dwm-plugins/sxmo_vibrate.c \ - $(pkgconf --libs alsa) -lm \ + $(pkgconf --libs alsa libpulse) -lm \ -o /bananarchy/prebuilt/sxmo-x11/usr/bin/sxmo_vibrate $STRIP --strip-unneeded /bananarchy/prebuilt/sxmo-x11/usr/bin/sxmo_vibrate diff --git a/scripts/build-sxmo-x11.sh b/scripts/build-sxmo-x11.sh index 89e5b92..cddd7b1 100755 --- a/scripts/build-sxmo-x11.sh +++ b/scripts/build-sxmo-x11.sh @@ -43,6 +43,7 @@ EOF musl-dev \ linux-headers \ alsa-lib-dev \ + pulseaudio-dev \ libx11-dev \ libxfixes-dev \ libxft-dev \ @@ -296,7 +297,9 @@ EOF $STRIP --strip-unneeded /bananarchy/prebuilt/sxmo-x11/usr/bin/dwm-plugin $CC $OPT_CFLAGS -std=c99 -D_DEFAULT_SOURCE -Wall -Wextra \ + $(pkgconf --cflags alsa libpulse) \ /bananarchy/src/dwm-plugins/sxmo_vibrate.c \ + $(pkgconf --libs alsa libpulse) -lm \ -o /bananarchy/prebuilt/sxmo-x11/usr/bin/sxmo_vibrate $STRIP --strip-unneeded /bananarchy/prebuilt/sxmo-x11/usr/bin/sxmo_vibrate diff --git a/scripts/check-rebuild.sh b/scripts/check-rebuild.sh index 6239401..7a50136 100755 --- a/scripts/check-rebuild.sh +++ b/scripts/check-rebuild.sh @@ -481,15 +481,15 @@ fi echo "[check] Known-good flashable image set..." for pair in \ - "82f617e425fa9d33b1395c4e35639a6207e3c9d0aa376c7cedf35fddaccb5cc2 images/bananarchy-nokia-argon.img" \ - "cc40c682fcdacb6415c112494344826362de2f965e6238685462859de66d660e images/bananarchy-nokia-beatles.img" \ - "786e9a1f4e4ae92efc948393a1d8f04a9405840a214b6ef37914cf5821d7ed82 images/boot-bananarchy-nokia-beatles.img" \ + "6ada66359a650a7e93f8937082a46353171989a54de19661821c1b91ea642ae3 images/bananarchy-nokia-argon.img" \ + "33b8b9181d5a23dd4a4bb473d827c6a87cf3407374c2e8b409fc45a7e60881b9 images/bananarchy-nokia-beatles.img" \ + "7e242e51cb3b16fe932d83c5807b0df93188606aa78859c484e3a8ed4414a8ff images/boot-bananarchy-nokia-beatles.img" \ "b5549698c20f4de7603fc746d32bb5baa96ec377e41e74407823c98506fc185d images/boot-lk2nd-nokia-beatles-display.img" \ - "e82d64bd9f14b1fcca2c078e9fd43a4c4268b29ff688f268b32a939213be0545 images/bananarchy-nokia-coconut.img" \ - "55299e15abe5923fc525b4e54e81aa4791f0e61f8a070e84fe7497f75c62d7d7 images/boot-bananarchy-nokia-coconut.img" \ + "4928a008fc1394e84c2bc2040b91b515eb2f506c39f0c822d8242e40dbf31eb6 images/bananarchy-nokia-coconut.img" \ + "ac4ea812933bb9d990540881110ecf88f1585c31590d4b860834f1553f2f1537 images/boot-bananarchy-nokia-coconut.img" \ "e876424b38b0782f589756b4107337b24f27f7c37d624cc7d3907a5331df050a images/boot-lk2nd-nokia-coconut-display.img" \ - "5dc17319742e4af95facc42681f0fe6fb2e80ddf30f421f3c54b9dbe04fefd3f images/bananarchy-nokia-sparkler.img" \ - "8205e2cb486d8ecc1e261a5c0b6f9ee7f70fd4eed2534d5bafc28ebc62d953d8 images/boot-bananarchy-nokia-sparkler.img" \ + "7b8de44bac9cded16f219d723748806a17053886dd1762d7261ac6de308918d6 images/bananarchy-nokia-sparkler.img" \ + "1ae0dfe5f189cbf381cdc26a89d95bfbabf2e267e86091fd1e8b32392460fa79 images/boot-bananarchy-nokia-sparkler.img" \ "bc41c2ddd1079246a7e91a40222fcafb82b0b52965c8ee8081872767d006748f images/boot-lk2nd-nokia-sparkler-display.img"; do set -- $pair want=$1 @@ -2371,8 +2371,13 @@ if command -v dtc >/dev/null 2>&1 && [ -f "$SPARKLER_DTB" ]; then fi if ! grep -qF 'vibrate_haptic_audio' "$BASE_DIR/src/dwm-plugins/sxmo_vibrate.c" || \ ! grep -qF '157.0' "$BASE_DIR/src/dwm-plugins/sxmo_vibrate.c" || \ - ! grep -qF 'nokia,sparkler' "$BASE_DIR/src/dwm-plugins/sxmo_vibrate.c"; then - fail "sxmo_vibrate.c lost 157 Hz haptic audio support for nokia-sparkler" + ! grep -qF 'nokia,sparkler' "$BASE_DIR/src/dwm-plugins/sxmo_vibrate.c" || \ + ! grep -qF 'SND_PCM_FORMAT_FLOAT_LE' "$BASE_DIR/src/dwm-plugins/sxmo_vibrate.c" || \ + ! grep -qF 'pa_sw_volume_to_linear' "$BASE_DIR/src/dwm-plugins/sxmo_vibrate.c" || \ + ! grep -qF 'HAPTIC_REF_RX3_VOL' "$BASE_DIR/src/dwm-plugins/sxmo_vibrate.c" || \ + ! grep -qF 'libpulse' "$BASE_DIR/scripts/build-dwm-plugins.sh" || \ + ! grep -qF 'libpulse' "$BASE_DIR/scripts/build-sxmo-x11.sh"; then + fail "sxmo_vibrate.c or plugin build lost volume-independent 157 Hz haptic audio support for nokia-sparkler" fi if ! grep -qF 'of_machine_is_compatible("nokia,sparkler")' "$TORCH_SRC" || \ ! grep -qF 'MSM8905_TLMM_GPIO31_PHYS' "$TORCH_SRC" || \ diff --git a/scripts/test-sparkler-port.py b/scripts/test-sparkler-port.py index 96fe8ed..99d0f83 100755 --- a/scripts/test-sparkler-port.py +++ b/scripts/test-sparkler-port.py @@ -112,10 +112,20 @@ class TestSparklerPort(unittest.TestCase): self.assertIn(" qcom_scm_set_boot_addr_mc\n", sysmap) vib_c = read_text("src/dwm-plugins/sxmo_vibrate.c") - for expected in ("is_haptic_audio_device", "vibrate_haptic_audio", "157.0", "nokia,sparkler"): + for expected in ( + "is_haptic_audio_device", + "vibrate_haptic_audio", + "157.0", + "nokia,sparkler", + "SND_PCM_FORMAT_FLOAT_LE", + "pa_sw_volume_to_linear", + "HAPTIC_REF_RX3_VOL", + ): self.assertIn(expected, vib_c) - build_p = read_text("scripts/build-dwm-plugins.sh") - self.assertIn("alsa", build_p) + for script_rel in ("scripts/build-dwm-plugins.sh", "scripts/build-sxmo-x11.sh"): + build_s = read_text(script_rel) + for expected in ("alsa", "libpulse", "pulseaudio-dev"): + self.assertIn(expected, build_s, script_rel) cam_c = read_text("src/msm8905-cam/msm8905-cam.c") for expected in ( diff --git a/src/dwm-plugins/sxmo_vibrate.c b/src/dwm-plugins/sxmo_vibrate.c index 1ae5ef8..9baf85d 100644 --- a/src/dwm-plugins/sxmo_vibrate.c +++ b/src/dwm-plugins/sxmo_vibrate.c @@ -6,7 +6,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -19,6 +21,11 @@ #define M_PI 3.14159265358979323846 #endif +#define HAPTIC_REF_RX3_VOL 84L +#define HAPTIC_REF_AMP_L1 1950.0 +#define HAPTIC_REF_AMP_L2 2550.0 +#define HAPTIC_REF_AMP_L3 3300.0 + static volatile sig_atomic_t g_stop = 0; static void handle_signal(int sig) { @@ -181,6 +188,14 @@ static int is_haptic_audio_device(void) { return 0; } +static int is_non_speaker_name(const char* s) { + return contains_icase(s, "Headphone") || + contains_icase(s, "Headset") || + contains_icase(s, "Earpiece") || + contains_icase(s, "Bluetooth") || + contains_icase(s, "bluez"); +} + static int is_non_speaker_audio_route(void) { char buf[128]; const char* paths[2]; @@ -197,13 +212,7 @@ static int is_non_speaker_audio_route(void) { if (!fp) continue; if (fgets(buf, sizeof(buf), fp)) { fclose(fp); - if (contains_icase(buf, "Headphone") || - contains_icase(buf, "Headset") || - contains_icase(buf, "Bluetooth") || - contains_icase(buf, "bluez")) { - return 1; - } - return 0; + return is_non_speaker_name(buf); } fclose(fp); } @@ -218,6 +227,269 @@ static int is_pw_sink_muted(void) { return ch == '1'; } +static double read_haptic_scale_env(void) { + const char* env_sc = getenv("SXMO_VIBRATE_HAPTIC_SCALE"); + if (!env_sc || !*env_sc) return 1.0; + char* endp = NULL; + errno = 0; + double v = strtod(env_sc, &endp); + if (errno != 0 || !endp || *endp != '\0' || !isfinite(v)) return 1.0; + if (v < 0.05) v = 0.05; + if (v > 4.0) v = 4.0; + return v; +} + +static long monotonic_ms(void) { + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return 0; + return (long)ts.tv_sec * 1000L + ts.tv_nsec / 1000000L; +} + +#define HAPTIC_PW_MAX_INPUTS 32 + +struct haptic_pw_input { + uint32_t index; + int muted; +}; + +struct haptic_pw_state { + pa_mainloop* ml; + pa_context* ctx; + int connected; + int sink_found; + int non_speaker; + uint32_t sink_index; + int sink_muted; + pa_cvolume sink_vol; + double linear_gain; + int n_inputs; + struct haptic_pw_input inputs[HAPTIC_PW_MAX_INPUTS]; + int n_saved_muted_inputs; + uint32_t saved_muted_inputs[HAPTIC_PW_MAX_INPUTS]; + int modified_sink_vol; + int modified_sink_mute; + pa_cvolume orig_sink_vol; + int orig_sink_mute; +}; + +struct pw_query_wait { + struct haptic_pw_state* pw; + int ctx_ready; + int ctx_failed; + int sink_done; + int inputs_done; + int op_done; +}; + +static void pw_context_state_cb(pa_context* c, void* userdata) { + struct pw_query_wait* q = (struct pw_query_wait*)userdata; + pa_context_state_t st = pa_context_get_state(c); + if (st == PA_CONTEXT_READY) { + q->ctx_ready = 1; + } else if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED) { + q->ctx_failed = 1; + } +} + +static void pw_sink_info_cb(pa_context* c, const pa_sink_info* i, int eol, + void* userdata) { + (void)c; + struct pw_query_wait* q = (struct pw_query_wait*)userdata; + struct haptic_pw_state* pw = q->pw; + if (eol > 0) { + q->sink_done = 1; + return; + } + if (!i || pw->sink_found) return; + + pw->sink_found = 1; + pw->sink_index = i->index; + pw->sink_muted = i->mute ? 1 : 0; + pw->sink_vol = i->volume; + pw->orig_sink_vol = i->volume; + pw->orig_sink_mute = i->mute ? 1 : 0; + pw->linear_gain = pa_sw_volume_to_linear(pa_cvolume_max(&i->volume)); + + if (is_non_speaker_name(i->name) || is_non_speaker_name(i->description)) { + pw->non_speaker = 1; + } else if (i->active_port && + (is_non_speaker_name(i->active_port->name) || + is_non_speaker_name(i->active_port->description))) { + pw->non_speaker = 1; + } +} + +static void pw_sink_input_info_cb(pa_context* c, const pa_sink_input_info* i, + int eol, void* userdata) { + (void)c; + struct pw_query_wait* q = (struct pw_query_wait*)userdata; + struct haptic_pw_state* pw = q->pw; + if (eol > 0) { + q->inputs_done = 1; + return; + } + if (!i || !pw->sink_found || i->sink != pw->sink_index || i->corked) return; + if (pw->n_inputs < HAPTIC_PW_MAX_INPUTS) { + pw->inputs[pw->n_inputs].index = i->index; + pw->inputs[pw->n_inputs].muted = i->mute ? 1 : 0; + pw->n_inputs++; + } +} + +static void pw_success_cb(pa_context* c, int success, void* userdata) { + (void)c; + (void)success; + struct pw_query_wait* q = (struct pw_query_wait*)userdata; + q->op_done = 1; +} + +static void pw_wait_until(pa_mainloop* ml, const int* done_flag, + const int* fail_flag, long timeout_ms, + int honor_stop) { + long start = monotonic_ms(); + while (!*done_flag && (!fail_flag || !*fail_flag)) { + if (honor_stop && g_stop) break; + if (monotonic_ms() - start >= timeout_ms) break; + if (pa_mainloop_prepare(ml, 10000) < 0) break; + int prc = pa_mainloop_poll(ml); + if (prc < 0 && errno != EINTR) break; + if (pa_mainloop_dispatch(ml) < 0) break; + } +} + +static void haptic_pw_run_op(struct haptic_pw_state* pw, pa_operation* op, + struct pw_query_wait* q, int* flag, + long timeout_ms, int honor_stop) { + if (!op) return; + pw_wait_until(pw->ml, flag, &q->ctx_failed, timeout_ms, honor_stop); + if (pa_operation_get_state(op) == PA_OPERATION_RUNNING) { + pa_operation_cancel(op); + } + pa_operation_unref(op); +} + +static void haptic_pw_connect_and_query(struct haptic_pw_state* pw) { + memset(pw, 0, sizeof(*pw)); + pw->ml = pa_mainloop_new(); + if (!pw->ml) return; + + pa_mainloop_api* api = pa_mainloop_get_api(pw->ml); + pw->ctx = pa_context_new(api, "sxmo_vibrate"); + if (!pw->ctx) { + pa_mainloop_free(pw->ml); + pw->ml = NULL; + return; + } + + struct pw_query_wait q; + memset(&q, 0, sizeof(q)); + q.pw = pw; + pa_context_set_state_callback(pw->ctx, pw_context_state_cb, &q); + if (pa_context_connect(pw->ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) { + pa_context_unref(pw->ctx); + pw->ctx = NULL; + pa_mainloop_free(pw->ml); + pw->ml = NULL; + return; + } + + pw_wait_until(pw->ml, &q.ctx_ready, &q.ctx_failed, 150, 1); + if (!q.ctx_ready || q.ctx_failed) { + pa_context_disconnect(pw->ctx); + pa_context_unref(pw->ctx); + pw->ctx = NULL; + pa_mainloop_free(pw->ml); + pw->ml = NULL; + return; + } + + pw->connected = 1; + pa_operation* op = pa_context_get_sink_info_by_name( + pw->ctx, "@DEFAULT_SINK@", pw_sink_info_cb, &q); + haptic_pw_run_op(pw, op, &q, &q.sink_done, 150, 1); + + if (pw->sink_found && !pw->non_speaker && !g_stop) { + op = pa_context_get_sink_input_info_list(pw->ctx, pw_sink_input_info_cb, &q); + haptic_pw_run_op(pw, op, &q, &q.inputs_done, 150, 1); + } +} + +static int haptic_pw_prepare_muted_bypass(struct haptic_pw_state* pw) { + if (!pw || !pw->connected || !pw->sink_found) return -1; + if (!PA_CONTEXT_IS_GOOD(pa_context_get_state(pw->ctx))) return -1; + + struct pw_query_wait q; + memset(&q, 0, sizeof(q)); + q.pw = pw; + + /* Silence active inputs before temporarily unmuting the muted sink. */ + for (int i = 0; i < pw->n_inputs; ++i) { + if (!pw->inputs[i].muted && + pw->n_saved_muted_inputs < HAPTIC_PW_MAX_INPUTS) { + q.op_done = 0; + pa_operation* op = pa_context_set_sink_input_mute( + pw->ctx, pw->inputs[i].index, 1, pw_success_cb, &q); + haptic_pw_run_op(pw, op, &q, &q.op_done, 100, 1); + pw->saved_muted_inputs[pw->n_saved_muted_inputs++] = pw->inputs[i].index; + } + } + + pa_cvolume unity_vol; + uint8_t ch = pw->orig_sink_vol.channels; + if (ch < 1 || ch > PA_CHANNELS_MAX) ch = 2; + pa_cvolume_set(&unity_vol, ch, PA_VOLUME_NORM); + q.op_done = 0; + pa_operation* vop = pa_context_set_sink_volume_by_index( + pw->ctx, pw->sink_index, &unity_vol, pw_success_cb, &q); + haptic_pw_run_op(pw, vop, &q, &q.op_done, 100, 1); + pw->modified_sink_vol = 1; + + q.op_done = 0; + pa_operation* mop = pa_context_set_sink_mute_by_index( + pw->ctx, pw->sink_index, 0, pw_success_cb, &q); + haptic_pw_run_op(pw, mop, &q, &q.op_done, 100, 1); + pw->modified_sink_mute = 1; + return g_stop ? -1 : 0; +} + +static void haptic_pw_restore_and_close(struct haptic_pw_state* pw) { + if (!pw) return; + if (pw->ctx) { + if (pw->connected && pw->sink_found && + PA_CONTEXT_IS_GOOD(pa_context_get_state(pw->ctx))) { + struct pw_query_wait q; + memset(&q, 0, sizeof(q)); + q.pw = pw; + + if (pw->modified_sink_mute) { + q.op_done = 0; + pa_operation* mop = pa_context_set_sink_mute_by_index( + pw->ctx, pw->sink_index, pw->orig_sink_mute, pw_success_cb, &q); + haptic_pw_run_op(pw, mop, &q, &q.op_done, 100, 0); + } + if (pw->modified_sink_vol) { + q.op_done = 0; + pa_operation* vop = pa_context_set_sink_volume_by_index( + pw->ctx, pw->sink_index, &pw->orig_sink_vol, pw_success_cb, &q); + haptic_pw_run_op(pw, vop, &q, &q.op_done, 100, 0); + } + for (int i = 0; i < pw->n_saved_muted_inputs; ++i) { + q.op_done = 0; + pa_operation* iop = pa_context_set_sink_input_mute( + pw->ctx, pw->saved_muted_inputs[i], 0, pw_success_cb, &q); + haptic_pw_run_op(pw, iop, &q, &q.op_done, 100, 0); + } + } + pa_context_disconnect(pw->ctx); + pa_context_unref(pw->ctx); + pw->ctx = NULL; + } + if (pw->ml) { + pa_mainloop_free(pw->ml); + pw->ml = NULL; + } +} + struct haptic_ctl_state { snd_ctl_t* ctl; int has_spk_sw; @@ -231,6 +503,9 @@ struct haptic_ctl_state { int changed_rx3_mux; int has_rx3_vol; long saved_rx3_vol; + long active_rx3_vol; + long min_rx3_vol; + long max_rx3_vol; int changed_rx3_vol; }; @@ -249,7 +524,7 @@ static int ctl_read_elem(snd_ctl_t* ctl, const char* name, return 0; } -static void haptic_ensure_speaker_ctl(struct haptic_ctl_state* st) { +static void haptic_ctl_open(struct haptic_ctl_state* st) { memset(st, 0, sizeof(*st)); if (snd_ctl_open(&st->ctl, "hw:0", 0) < 0) { st->ctl = NULL; @@ -265,46 +540,70 @@ static void haptic_ensure_speaker_ctl(struct haptic_ctl_state* st) { info, val) == 0) { st->has_pri_mm1 = 1; st->saved_pri_mm1 = snd_ctl_elem_value_get_boolean(val, 0); - if (st->saved_pri_mm1 == 0) { - snd_ctl_elem_value_set_boolean(val, 0, 1); - if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_pri_mm1 = 1; - } } - if (ctl_read_elem(st->ctl, "RX3 MIX1 INP1", info, val) == 0) { st->has_rx3_mux = 1; st->saved_rx3_mux = snd_ctl_elem_value_get_enumerated(val, 0); - if (st->saved_rx3_mux == 0) { - unsigned int items = snd_ctl_elem_info_get_items(info); - for (unsigned int i = 0; i < items; ++i) { - snd_ctl_elem_info_set_item(info, i); - if (snd_ctl_elem_info(st->ctl, info) == 0 && - strcmp(snd_ctl_elem_info_get_item_name(info), "RX1") == 0) { - snd_ctl_elem_value_set_enumerated(val, 0, i); - if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_rx3_mux = 1; - break; - } + } + if (ctl_read_elem(st->ctl, "SPK DAC Switch", info, val) == 0) { + st->has_spk_sw = 1; + st->saved_spk_sw = snd_ctl_elem_value_get_boolean(val, 0); + } + if (ctl_read_elem(st->ctl, "RX3 Digital Volume", info, val) == 0) { + st->has_rx3_vol = 1; + st->saved_rx3_vol = snd_ctl_elem_value_get_integer(val, 0); + st->active_rx3_vol = st->saved_rx3_vol; + st->min_rx3_vol = snd_ctl_elem_info_get_min(info); + st->max_rx3_vol = snd_ctl_elem_info_get_max(info); + } +} + +static void haptic_ensure_speaker_ctl(struct haptic_ctl_state* st, + long target_rx3_vol) { + if (!st || !st->ctl) return; + + snd_ctl_elem_info_t* info = NULL; + snd_ctl_elem_value_t* val = NULL; + snd_ctl_elem_info_alloca(&info); + snd_ctl_elem_value_alloca(&val); + + if (st->has_pri_mm1 && st->saved_pri_mm1 == 0 && + ctl_read_elem(st->ctl, "PRI_MI2S_RX Audio Mixer MultiMedia1", + info, val) == 0) { + snd_ctl_elem_value_set_boolean(val, 0, 1); + if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_pri_mm1 = 1; + } + + if (st->has_rx3_mux && st->saved_rx3_mux == 0 && + ctl_read_elem(st->ctl, "RX3 MIX1 INP1", info, val) == 0) { + unsigned int items = snd_ctl_elem_info_get_items(info); + for (unsigned int i = 0; i < items; ++i) { + snd_ctl_elem_info_set_item(info, i); + if (snd_ctl_elem_info(st->ctl, info) == 0 && + strcmp(snd_ctl_elem_info_get_item_name(info), "RX1") == 0) { + snd_ctl_elem_value_set_enumerated(val, 0, i); + if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_rx3_mux = 1; + break; } } } - if (ctl_read_elem(st->ctl, "SPK DAC Switch", info, val) == 0) { - st->has_spk_sw = 1; - st->saved_spk_sw = snd_ctl_elem_value_get_boolean(val, 0); - if (st->saved_spk_sw == 0) { - snd_ctl_elem_value_set_boolean(val, 0, 1); - if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_spk_sw = 1; - } + if (st->has_spk_sw && st->saved_spk_sw == 0 && + ctl_read_elem(st->ctl, "SPK DAC Switch", info, val) == 0) { + snd_ctl_elem_value_set_boolean(val, 0, 1); + if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_spk_sw = 1; } - if (ctl_read_elem(st->ctl, "RX3 Digital Volume", info, val) == 0) { - st->has_rx3_vol = 1; - st->saved_rx3_vol = snd_ctl_elem_value_get_integer(val, 0); - if (st->saved_rx3_vol < 80) { - long maxv = snd_ctl_elem_info_get_max(info); - long target = (maxv >= 88) ? 88 : maxv; - snd_ctl_elem_value_set_integer(val, 0, target); - if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_rx3_vol = 1; + if (st->has_rx3_vol && target_rx3_vol >= 0 && + st->saved_rx3_vol != target_rx3_vol && + ctl_read_elem(st->ctl, "RX3 Digital Volume", info, val) == 0) { + long target = target_rx3_vol; + if (target < st->min_rx3_vol) target = st->min_rx3_vol; + if (target > st->max_rx3_vol) target = st->max_rx3_vol; + snd_ctl_elem_value_set_integer(val, 0, target); + if (snd_ctl_elem_write(st->ctl, val) >= 0) { + st->changed_rx3_vol = 1; + st->active_rx3_vol = target; } } } @@ -342,11 +641,14 @@ static void haptic_restore_speaker_ctl(struct haptic_ctl_state* st) { st->ctl = NULL; } -static int write_pcm_frames_all(snd_pcm_t* pcm, const short* buf, - snd_pcm_uframes_t frames) { +static int write_pcm_frames_all(snd_pcm_t* pcm, const void* buf, + snd_pcm_uframes_t frames, + size_t frame_bytes) { + const char* bytes = (const char*)buf; snd_pcm_uframes_t off = 0; while (off < frames && !g_stop) { - snd_pcm_sframes_t rc = snd_pcm_writei(pcm, buf + off * 2, frames - off); + snd_pcm_sframes_t rc = + snd_pcm_writei(pcm, bytes + off * frame_bytes, frames - off); if (rc == -EAGAIN || rc == -EINTR) { continue; } @@ -360,36 +662,52 @@ static int write_pcm_frames_all(snd_pcm_t* pcm, const short* buf, return g_stop ? -1 : 0; } -static int write_haptic_pulse(snd_pcm_t* pcm, long ms, double amp) { +static double haptic_pulse_env(long idx, long total_frames, long fade_frames) { + if (fade_frames <= 0) return 1.0; + if (idx < fade_frames) { + return 0.5 * (1.0 - cos(M_PI * (double)idx / (double)fade_frames)); + } + if (idx >= total_frames - fade_frames) { + long tail = total_frames - 1 - idx; + return 0.5 * (1.0 - cos(M_PI * (double)tail / (double)fade_frames)); + } + return 1.0; +} + +static int write_haptic_pulse(snd_pcm_t* pcm, long ms, double amp, + int is_float) { if (ms <= 0 || g_stop) return 0; const long rate = 48000L; const double step = 2.0 * M_PI * 157.0 / (double)rate; const long total_frames = (ms * rate) / 1000L; - long fade_frames = (5L * rate) / 1000L; + long fade_frames = (10L * rate) / 1000L; if (fade_frames > total_frames / 2) fade_frames = total_frames / 2; - short chunk[2400 * 2]; + short chunk_s16[2400 * 2]; + float chunk_f32[2400 * 2]; long pos = 0; while (pos < total_frames && !g_stop) { long n = total_frames - pos; if (n > 2400L) n = 2400L; for (long i = 0; i < n; ++i) { long idx = pos + i; - double env = 1.0; - if (fade_frames > 0 && idx < fade_frames) { - env = 0.5 * (1.0 - cos(M_PI * (double)idx / (double)fade_frames)); - } else if (fade_frames > 0 && idx >= total_frames - fade_frames) { - long tail = total_frames - 1 - idx; - env = 0.5 * (1.0 - cos(M_PI * (double)tail / (double)fade_frames)); + double v = haptic_pulse_env(idx, total_frames, fade_frames) * amp * + sin(step * (double)idx); + if (is_float) { + float fv = (float)v; + chunk_f32[i * 2] = fv; + chunk_f32[i * 2 + 1] = fv; + } else { + if (v > 32767.0) v = 32767.0; + if (v < -32767.0) v = -32767.0; + short s = (short)lrint(v); + chunk_s16[i * 2] = s; + chunk_s16[i * 2 + 1] = s; } - double v = env * amp * sin(step * (double)idx); - if (v > 32767.0) v = 32767.0; - if (v < -32767.0) v = -32767.0; - short s = (short)lrint(v); - chunk[i * 2] = s; - chunk[i * 2 + 1] = s; } - if (write_pcm_frames_all(pcm, chunk, (snd_pcm_uframes_t)n) < 0) { + const void* ptr = is_float ? (const void*)chunk_f32 : (const void*)chunk_s16; + size_t fbytes = is_float ? (sizeof(float) * 2U) : (sizeof(short) * 2U); + if (write_pcm_frames_all(pcm, ptr, (snd_pcm_uframes_t)n, fbytes) < 0) { return -1; } pos += n; @@ -397,17 +715,24 @@ static int write_haptic_pulse(snd_pcm_t* pcm, long ms, double amp) { return 0; } -static int write_haptic_silence(snd_pcm_t* pcm, long ms) { +static int write_haptic_silence(snd_pcm_t* pcm, long ms, int is_float) { if (ms <= 0 || g_stop) return 0; const long total_frames = (ms * 48000L) / 1000L; - short zeros[2400 * 2]; - memset(zeros, 0, sizeof(zeros)); + short zeros_s16[2400 * 2]; + float zeros_f32[2400 * 2]; + if (is_float) { + memset(zeros_f32, 0, sizeof(zeros_f32)); + } else { + memset(zeros_s16, 0, sizeof(zeros_s16)); + } + const void* ptr = is_float ? (const void*)zeros_f32 : (const void*)zeros_s16; + size_t fbytes = is_float ? (sizeof(float) * 2U) : (sizeof(short) * 2U); long pos = 0; while (pos < total_frames && !g_stop) { long n = total_frames - pos; if (n > 2400L) n = 2400L; - if (write_pcm_frames_all(pcm, zeros, (snd_pcm_uframes_t)n) < 0) { + if (write_pcm_frames_all(pcm, ptr, (snd_pcm_uframes_t)n, fbytes) < 0) { return -1; } pos += n; @@ -415,6 +740,49 @@ static int write_haptic_silence(snd_pcm_t* pcm, long ms) { return 0; } +static void play_haptic_pattern(snd_pcm_t* pcm, long duration_ms, int level, + double amp, int is_float) { + if (duration_ms <= 90) { + long actual_ms = (level <= 1 && duration_ms > 60) ? 60 : duration_ms; + (void)write_haptic_pulse(pcm, actual_ms, amp, is_float); + return; + } + + long on_ms = 140; + long off_ms = 90; + if (level <= 1) { + on_ms = 85; + off_ms = 115; + } else if (level >= 3) { + on_ms = 240; + off_ms = 60; + } + + long rem = duration_ms; + while (rem > 0 && !g_stop) { + long cur_on = (rem < on_ms) ? rem : on_ms; + if (write_haptic_pulse(pcm, cur_on, amp, is_float) < 0) break; + rem -= cur_on; + if (rem <= 0 || g_stop) break; + long cur_off = (rem < off_ms) ? rem : off_ms; + if (write_haptic_silence(pcm, cur_off, is_float) < 0) break; + rem -= cur_off; + } +} + +static snd_pcm_t* open_haptic_pcm(const char* dev, snd_pcm_format_t fmt) { + snd_pcm_t* pcm = NULL; + if (snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0) < 0) { + return NULL; + } + if (snd_pcm_set_params(pcm, fmt, SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000, + 1, 500000) < 0) { + snd_pcm_close(pcm); + return NULL; + } + return pcm; +} + static int vibrate_haptic_audio(long duration_ms, int level) { if (is_non_speaker_audio_route()) { return 0; @@ -424,6 +792,7 @@ static int vibrate_haptic_audio(long duration_ms, int level) { if (getuid() == 0 && access("/run/user/10000/pipewire-0", F_OK) == 0) { (void)initgroups("user", 10000); if (setgid(10000) == 0 && setuid(10000) == 0) { + (void)setenv("HOME", "/home/user", 1); (void)setenv("XDG_RUNTIME_DIR", "/run/user/10000", 1); } } else { @@ -437,63 +806,94 @@ static int vibrate_haptic_audio(long duration_ms, int level) { } } - struct haptic_ctl_state ctl_st; - memset(&ctl_st, 0, sizeof(ctl_st)); - - snd_pcm_t* pcm = NULL; - const char* devs[2]; - if (is_pw_sink_muted()) { - devs[0] = "plughw:0,0"; - devs[1] = "default"; - } else { - devs[0] = "default"; - devs[1] = "plughw:0,0"; + struct haptic_pw_state pw; + haptic_pw_connect_and_query(&pw); + if (pw.non_speaker || g_stop) { + haptic_pw_restore_and_close(&pw); + return 0; } - const char* opened_dev = NULL; - for (int i = 0; i < 2 && !pcm; ++i) { - if (snd_pcm_open(&pcm, devs[i], SND_PCM_STREAM_PLAYBACK, 0) == 0) { - if (snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, - SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000, - 1, 500000) < 0) { - snd_pcm_close(pcm); - pcm = NULL; - } else { - opened_dev = devs[i]; + double amp_ref = HAPTIC_REF_AMP_L2; + if (level <= 1) amp_ref = HAPTIC_REF_AMP_L1; + else if (level >= 3) amp_ref = HAPTIC_REF_AMP_L3; + amp_ref *= read_haptic_scale_env(); + + double amp_norm = amp_ref / 32768.0; + if (amp_norm > 0.92) amp_norm = 0.92; + + struct haptic_ctl_state ctl_st; + haptic_ctl_open(&ctl_st); + + snd_pcm_t* pcm = NULL; + int use_float = 0; + double play_amp = amp_ref; + + int pw_unmuted = pw.connected && pw.sink_found && !pw.sink_muted && + pw.linear_gain >= 1e-4 && !is_pw_sink_muted(); + if (pw_unmuted) { + long cur_rx3 = ctl_st.has_rx3_vol ? ctl_st.saved_rx3_vol : HAPTIC_REF_RX3_VOL; + double cur_gain_rx3 = + (cur_rx3 <= 0) ? 0.0 : pow(10.0, ((double)cur_rx3 - 84.0) / 20.0); + long target_rx3 = -1; + if (cur_rx3 <= 0 || + (pw.n_inputs == 0 && (cur_gain_rx3 <= 0.0 || + amp_norm / cur_gain_rx3 > 0.92))) { + target_rx3 = HAPTIC_REF_RX3_VOL; + } + + haptic_ensure_speaker_ctl(&ctl_st, target_rx3); + long eff_rx3 = + ctl_st.has_rx3_vol ? ctl_st.active_rx3_vol : HAPTIC_REF_RX3_VOL; + double gain_rx3 = pow(10.0, ((double)eff_rx3 - 84.0) / 20.0); + double post_pw = (gain_rx3 > 1e-6) ? (amp_norm / gain_rx3) : 0.92; + if (post_pw > 0.92) post_pw = 0.92; + + pcm = open_haptic_pcm("default", SND_PCM_FORMAT_FLOAT_LE); + if (pcm) { + use_float = 1; + play_amp = post_pw / pw.linear_gain; + } else { + haptic_restore_speaker_ctl(&ctl_st); + haptic_ctl_open(&ctl_st); + } + } + + if (!pcm) { + pcm = open_haptic_pcm("plughw:0,0", SND_PCM_FORMAT_S16_LE); + if (pcm) { + haptic_ensure_speaker_ctl(&ctl_st, HAPTIC_REF_RX3_VOL); + long eff_rx3 = + ctl_st.has_rx3_vol ? ctl_st.active_rx3_vol : HAPTIC_REF_RX3_VOL; + double gain_rx3 = pow(10.0, ((double)eff_rx3 - 84.0) / 20.0); + double post_pw = (gain_rx3 > 1e-6) ? (amp_norm / gain_rx3) : 0.92; + if (post_pw > 0.92) post_pw = 0.92; + use_float = 0; + play_amp = post_pw * 32767.0; + } else if (pw.connected && pw.sink_found) { + if (haptic_pw_prepare_muted_bypass(&pw) == 0) { + haptic_ensure_speaker_ctl(&ctl_st, HAPTIC_REF_RX3_VOL); + pcm = open_haptic_pcm("default", SND_PCM_FORMAT_FLOAT_LE); + if (pcm) { + long eff_rx3 = + ctl_st.has_rx3_vol ? ctl_st.active_rx3_vol : HAPTIC_REF_RX3_VOL; + double gain_rx3 = pow(10.0, ((double)eff_rx3 - 84.0) / 20.0); + double post_pw = (gain_rx3 > 1e-6) ? (amp_norm / gain_rx3) : 0.92; + if (post_pw > 0.92) post_pw = 0.92; + use_float = 1; + play_amp = post_pw; + } } } } if (!pcm) { + haptic_restore_speaker_ctl(&ctl_st); + haptic_pw_restore_and_close(&pw); fprintf(stderr, "sxmo_vibrate: cannot open haptic audio PCM\n"); return 1; } - if (opened_dev && strcmp(opened_dev, "plughw:0,0") == 0) { - haptic_ensure_speaker_ctl(&ctl_st); - } - - double amp = 21000.0; - if (level <= 1) amp = 13000.0; - else if (level >= 3) amp = 29000.0; - - if (level >= 3 || duration_ms <= 90) { - long actual_ms = (level == 1 && duration_ms > 55) ? 55 : duration_ms; - (void)write_haptic_pulse(pcm, actual_ms, amp); - } else { - long on_ms = (level == 1) ? 75 : 160; - long off_ms = (level == 1) ? 125 : 80; - long rem = duration_ms; - while (rem > 0 && !g_stop) { - long cur_on = (rem < on_ms) ? rem : on_ms; - if (write_haptic_pulse(pcm, cur_on, amp) < 0) break; - rem -= cur_on; - if (rem <= 0 || g_stop) break; - long cur_off = (rem < off_ms) ? rem : off_ms; - if (write_haptic_silence(pcm, cur_off) < 0) break; - rem -= cur_off; - } - } + play_haptic_pattern(pcm, duration_ms, level, play_amp, use_float); if (g_stop) { (void)snd_pcm_drop(pcm); @@ -502,6 +902,7 @@ static int vibrate_haptic_audio(long duration_ms, int level) { } snd_pcm_close(pcm); haptic_restore_speaker_ctl(&ctl_st); + haptic_pw_restore_and_close(&pw); return 0; }