Files
MeowOS-SDK/meow_ui_vol.c
T

53 lines
2.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* meow_ui_vol.c — SDK 状态栏音量换算纯函数 + settings.conf 读取(A-0 核心数源)。
*
* 单独成 TU 的目的:
* 1. 纯函数(clamp / 键读取)不依赖 meow_ui 其它子系统,宿主 gcc 直接可测
* (test_meow_ui_vol.c),无需链整库/stub。
* 2. 把「音量从 settings 读数并 clamp 到 volume_max」这一语义钉死在单点,
* 与 launcher 的 vol_clamp(meow_launcherd.c)口径对齐,杜绝两端音量漂移。
*
* 数据源:/usr/data/meow/settings.conf 扁平 key=value。launcher meow_settings.c
* 落盘为 "key = value"(带空格),本解析兼容无空格/多空格,与 launcher 读循环
* 及 sdk meow_ui_theme.c 的 get_setting_str 同款解析。
*/
#include "meow_ui_vol.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int meow_ui_vol_clamp(int vol, int vol_max)
{
if (vol < 0) vol = 0;
if (vol > MEOW_UI_VOL_MAX) vol = MEOW_UI_VOL_MAX;
if (vol_max < 0) vol_max = 0; /* 异常上限兜底:不进负值 */
if (vol > vol_max) vol = vol_max;
return vol;
}
int meow_ui_vol_read(const char *path, const char *key, int def)
{
if (!path || !key) return def;
FILE *f = fopen(path, "r");
if (!f) return def;
int kl = (int)strlen(key);
char line[160];
char *found = NULL;
while (fgets(line, sizeof line, f)) {
char *eq = strchr(line, '=');
if (!eq) continue; /* 非 key=value 行(注释/空行)跳过 */
char *k = line;
while (*k == ' ' || *k == '\t') k++; /* key 左去空白 */
int n = (int)(eq - k);
while (n > 0 && (k[n-1] == ' ' || k[n-1] == '\t')) n--; /* key 右去空白 */
if (n != kl || strncmp(k, key, (size_t)kl) != 0) continue; /* key 不精确匹配 */
found = eq + 1; /* 命中所求键 */
break;
}
fclose(f);
if (!found) return def;
while (*found == ' ' || *found == '\t') found++; /* value 左去空白;atoi 天然停于非数字 */
return atoi(found);
}