c/ui/ui_input.c · 18504 字节 · 下载原文
/* ui_input.c —— **C 语言输入/对话框接口验收**(消息队列 / 键盘 / 触摸 / 定时器 / 对话框 / 传感器注入)。
* ui_input.c - **per-interface acceptance for the C input/dialog API** (message queue / keyboard / touch / timers / dialogs / injected sensors).
*
* ## 两档模式
* ## Two modes
*
* · **脚本档(`--arg scripted`)**:桌面 `vmlcli` 用 `--input` 喂事件、`--answer` 喂对话框答案,
* · **Scripted (`--arg scripted`)**: the desktop `vmlcli` feeds events through `--input` and dialog answers through `--answer`;
* 逐条断言(消息类型 / A / B / 轮询状态 / 返回值)。判据见 `scripts/vml-ui-api-probe.sh`。
* every assertion is checked (message type / A / B / polled state / return values). Criteria live in `scripts/vml-ui-api-probe.sh`.
* · **交互档(不带参数,手机上跑)**:屏上实时显示「收到了什么事件、触摸在哪、哪个键按下」,
* · **Interactive (no argument, runs on a phone)**: the screen shows "which event arrived, where the touch is, which key is down" live,
* 手指点、按键、关窗都能当场看出接口通不通(这一档不做机器断言)。
* so a tap, a key press or closing the window shows on the spot whether the interface works (this mode makes no machine assertions).
*
* ## 判据为什么这么挑(都是实测出来的)
* ## Why the criteria look like this (all measured)
*
* · **`ui_touch_fetch` 必须单独一个语句,不能塞进 `printf`/`chk` 的实参里**:
* · **`ui_touch_fetch` must be its own statement, not an argument to `printf`/`chk`**:
* C 的实参**从右到左求值**(见 `Lib/c/vml_ui.h` 的调用约定),于是
* C evaluates arguments **right to left** (see the calling convention in `Lib/c/vml_ui.h`), so
* `printf("…", ui_touch_fetch(0), ui_touch_get_x())` 会**先**取 x(读的是上一次的缓存)、**后**刷新缓存
* `printf("...", ui_touch_fetch(0), ui_touch_get_x())` reads x **first** (the previous cache) and refreshes the cache **afterwards**
* ⇒ 读出来的坐标**晚一拍**,看着像宿主不同步。实测踩过一次(据此写过"桌面晚一拍"的错误结论):
* => the coordinates read one beat late, looking like an out-of-sync host. This repo fell into it once (and wrote a wrong "the desktop lags" conclusion):
* 把 fetch 拆成独立语句之后,`TOUCHDOWN` 之后立刻读到 120/340/down=1,**没有任何延迟**。
* after splitting fetch out, reading right after `TOUCHDOWN` gives 120/340/down=1 with **no delay at all**.
* · **`ui_msg_drop` 丢的是「某一类」**:丢完之后**别的类还在**(实测:丢 TOUCH 之后残留的 TIMER 还在)
* · **`ui_msg_drop` drops "one kind"**: other kinds remain afterwards (measured: leftover TIMER messages remain after dropping TOUCH)
* ⇒ 断言写「少了 N 条」,不写「清空了」。
* => assert "N messages gone", not "empty".
* · **`ui_timer_kill` 的返回值是 0(成功),不是 id**;杀完再等必须**等不到**(REVERSE)。
* · **`ui_timer_kill` returns 0 (success), not the id**; after killing, waiting must **time out** (REVERSE).
* · **对话框的答案由 `--answer` 顺序消费**:消息框 `ok`、单选 `1`、多选 `0,2`、输入框 `hello`。
* · **Dialog answers are consumed in order from `--answer`**: message box `ok`, single-select `1`, multi-select `0,2`, input box `hello`.
* 脚本里的顺序**必须与调用顺序一致**,见 runner。
* The order in the script **must match the call order**; see the runner.
*/
#include <vml_ui.h>
#include <stdio.h>
int g_ok;
int g_fail;
int g_unimpl;
char g_line[256];
void chk(int cond, char* name, int a, int b) {
if (cond) {
printf("PASS %s %d %d\n", name, a, b);
g_ok = g_ok + 1;
} else {
printf("FAIL %s %d %d\n", name, a, b);
g_fail = g_fail + 1;
}
}
void unimpl(char* name, int obs) {
printf("UNIMPL %s %d\n", name, obs);
g_unimpl = g_unimpl + 1;
}
int has(char* hay, char* needle) {
int i;
int j;
i = 0;
while (hay[i] != 0) {
j = 0;
while (needle[j] != 0 && hay[i + j] == needle[j]) { j = j + 1; }
if (needle[j] == 0) { return 1; }
i = i + 1;
}
return 0;
}
int eq(char* a, char* b) {
int i;
i = 0;
while (a[i] != 0 && b[i] != 0) {
if (a[i] != b[i]) { return 0; }
i = i + 1;
}
return a[i] == b[i];
}
void wait_ms(int ms) {
int t0;
t0 = ui_time_get_ms();
while (ui_time_get_ms() - t0 < ms) { }
}
/* 等到指定类型的消息(别的事件丢掉);超时回 0。A/B 存在 g_ma/g_mb。 */
/* Wait for a message of the wanted type (other events are dropped); 0 on timeout. A/B land in g_ma/g_mb. */
int g_ma;
int g_mb;
int wait_type(int want, int timeout_ms) {
int t0;
int t;
t0 = ui_time_get_ms();
while (ui_time_get_ms() - t0 < timeout_ms) {
t = ui_msg_fetch_wait(200);
if (t == 0) { continue; }
if (t == want) {
g_ma = ui_msg_get_a();
g_mb = ui_msg_get_b();
return t;
}
}
return 0;
}
/* ── 脚本档:逐条断言 ───────────────────────────────────────────── */
/* ── Scripted mode: assertion by assertion ─────────────────────── */
int run_scripted(void) {
int msg[8];
int t;
int r;
int n;
int f;
int px;
int py;
int dn;
int cnt;
int k1;
int k2;
int k3;
char buf[256];
/* ── 1. 对话框(答案由 --answer 顺序消费)── */
/* ── 1. Dialogs (answers consumed in order from --answer) ── */
r = ui_dlg_msg("ui_input", "message box (answer: ok)", VML_DLG_INFO);
chk(r == 0, "DLG_MSG", r, 0);
r = ui_dlg_select("ui_input", "pick one (answer: 1)", "a\0b\0c", 3, 0);
chk(r == 1, "DLG_SELECT", r, 1);
r = ui_dlg_multi("ui_input", "pick several (answer: 0,2)", "a\0b\0c", 3);
chk(r == 5, "DLG_MULTI", r, 5);
buf[0] = 0;
r = ui_dlg_input("ui_input", "type hello", buf, 128);
chk(r == 5 && has(buf, "hello"), "DLG_INPUT", r, 5);
/* 文件/相机选择器:注入的路径必须原样交回(返回字节数) */
/* File/camera pickers: the injected path must come back verbatim (return = byte count) */
buf[0] = 0;
r = ui_dlg_file_open(buf, 256);
chk(r > 0 && has(buf, "ui_all.c"), "DLG_FILE_OPEN", r, 0);
buf[0] = 0;
r = ui_dlg_file_save(buf, 256, "suggested.txt");
chk(r > 0, "DLG_FILE_SAVE", r, 0);
buf[0] = 0;
r = ui_camera_capture(buf, 256);
chk(r > 0 && has(buf, "shot/cam.png"), "CAMERA", r, 0);
buf[0] = 0;
r = ui_camera_pick(buf, 256);
chk(r > 0, "CAMERA_PICK", r, 0);
/* ── 2. 键盘:按下 -> 按住 -> 抬起 ── */
/* ── 2. Keyboard: down -> held -> up ── */
t = wait_type(VML_MSG_KEYDOWN, 5000);
chk(t == VML_MSG_KEYDOWN, "EV_KEYDOWN", t, VML_MSG_KEYDOWN);
chk(g_ma == VML_KEY_LEFT, "KEY_A", g_ma, VML_KEY_LEFT);
chk(ui_key_is_down(VML_KEY_LEFT) == 1, "KEY_HELD", ui_key_is_down(VML_KEY_LEFT), 1);
/* REVERSE:没按的键必须是 0(不是"所有键都算按下") */
/* REVERSE: a key that is not pressed must be 0 (not "every key counts as pressed") */
chk(ui_key_is_down(VML_KEY_RIGHT) == 0, "KEY_OTHER", ui_key_is_down(VML_KEY_RIGHT), 0);
t = wait_type(VML_MSG_KEYUP, 5000);
chk(t == VML_MSG_KEYUP, "EV_KEYUP", t, VML_MSG_KEYUP);
chk(ui_key_is_down(VML_KEY_LEFT) == 0, "KEY_RELEASED", ui_key_is_down(VML_KEY_LEFT), 0);
/* ── 3. 触摸:按下 / 移动 / 抬起 ── */
/* ── 3. Touch: down / move / up ── */
t = wait_type(VML_MSG_TOUCHDOWN, 5000);
chk(t == VML_MSG_TOUCHDOWN, "EV_TOUCHDOWN", t, VML_MSG_TOUCHDOWN);
chk(g_ma == 120 && g_mb == 340, "TOUCH_DOWN_XY", g_ma, g_mb);
/* ⚠ `ui_touch_fetch` 是**刷新缓存**,必须先单独调用;塞进实参里会因"从右到左求值"而读到旧值 */
/* ⚠ `ui_touch_fetch` **refreshes the cache**, so call it standalone; as an argument it reads stale values (right-to-left evaluation) */
f = ui_touch_fetch(0);
px = ui_touch_get_x();
py = ui_touch_get_y();
dn = ui_touch_is_down();
chk(f == 1, "TOUCH_VALID", f, 1);
chk(px == 120 && py == 340, "TOUCH_POLL_XY", px, py);
chk(dn == 1, "TOUCH_DOWN", dn, 1);
t = wait_type(VML_MSG_TOUCHMOVE, 5000);
chk(t == VML_MSG_TOUCHMOVE, "EV_TOUCHMOVE", t, VML_MSG_TOUCHMOVE);
chk(g_ma == 150 && g_mb == 360, "TOUCH_MOVE_XY", g_ma, g_mb);
f = ui_touch_fetch(0);
px = ui_touch_get_x();
py = ui_touch_get_y();
chk(px == 150 && py == 360, "TOUCH_MOVED", px, py);
t = wait_type(VML_MSG_TOUCHUP, 5000);
chk(t == VML_MSG_TOUCHUP, "EV_TOUCHUP", t, VML_MSG_TOUCHUP);
f = ui_touch_fetch(0);
dn = ui_touch_is_down();
/* REVERSE:抬起之后必须报"没按",不能一直按着 */
/* REVERSE: after lifting it must report "not pressed"; it must not stay pressed */
chk(dn == 0, "TOUCH_UP", dn, 0);
/* ── 4. 定时器:到点投消息(A=id B=tag),杀掉之后不再来 ── */
/* ── 4. Timer: posts at the interval (A=id B=tag); after killing, nothing more ── */
n = ui_timer_set(60, 9);
chk(n >= 1, "TIMER_ID", n, 0);
t = wait_type(VML_MSG_TIMER, 4000);
chk(t == VML_MSG_TIMER, "TIMER_MSG", t, VML_MSG_TIMER);
chk(g_ma == n, "TIMER_A", g_ma, n);
chk(g_mb == 9, "TIMER_B", g_mb, 9);
chk(ui_timer_kill(n) == 0, "TIMER_KILL", 0, 0);
/* REVERSE:杀掉之后**不该再有**定时器消息(窗口 400ms,够两三个周期) */
/* REVERSE: after killing there must be **no more** timer messages (400ms covers several periods) */
r = 0;
while (r < 4) {
t = ui_msg_fetch_wait(200);
if (t == VML_MSG_TIMER) { r = -1; break; }
if (t == 0) { break; }
}
chk(r == 0, "TIMER_STOPPED", r, 0);
ui_msg_clear();
/* ── 5. `ui_msg_drop` 只丢一类:自己造一串定时器消息,再按类丢掉 ── */
/* ── 5. `ui_msg_drop` drops one kind only: make a burst of timer messages, then drop by kind ── */
n = ui_timer_set(20, 4);
wait_ms(250);
cnt = ui_msg_get_count();
chk(cnt > 0, "MSG_BURST", cnt, 0);
r = ui_msg_drop(VML_MSG_KIND_TIMER);
chk(r > 0 && ui_msg_get_count() < cnt, "MSG_DROP_TIMER", r, cnt);
/* 未知类别:-1(不是 0、不是崩) */
/* Unknown kind: -1 (not 0, not a crash) */
chk(ui_msg_drop(99) == -1, "MSG_DROP_UNKNOWN", ui_msg_drop(99), -1);
n = ui_msg_clear();
chk(n >= 0 && ui_msg_get_count() == 0, "MSG_CLEAR", n, 0);
ui_timer_kill(n);
/* ── 6. 保留式读:同一条读两次都在,消费掉才走 ──
* ── 6. Keep-mode read: the same message stays for two reads and only goes away when consumed ──
* ⚠ 不能用 `ui_msg_fetch_wait` 垫底 —— 它**会把消息吃掉**,那之后就无队头可保留。
* ⚠ Do not prime with `ui_msg_fetch_wait`: it **consumes** the message, leaving no head to keep. */
ui_msg_clear();
n = ui_timer_set(40, 3);
wait_ms(200);
cnt = ui_msg_get_count();
chk(cnt > 0, "KEEP_QUEUED", cnt, 0);
k1 = ui_msg_read_ex(msg, VML_MSG_KEEP);
k2 = ui_msg_read_ex(msg, VML_MSG_KEEP);
chk(k1 == VML_MSG_TIMER && k2 == VML_MSG_TIMER, "MSG_KEEP", k1, k2);
k3 = ui_msg_read_ex(msg, VML_MSG_CONSUME);
chk(k3 == VML_MSG_TIMER, "MSG_CONSUME", k3, VML_MSG_TIMER);
ui_timer_kill(n);
ui_msg_clear();
/* ── 7. 传感器 / 电量 / 定位 / 震动:脚本注入的值要原样读回 ── */
/* ── 7. Sensors / battery / location / vibration: values injected by the script must read back verbatim ── */
if (ui_sensor_is_available(VML_SENS_ACCEL) == 1) {
f = ui_sensor_fetch(VML_SENS_ACCEL);
chk(f == 1, "SENSOR_FETCH", f, 1);
/* 脚本写 `accel 0 0 1` = 1g ⇒ z = 1000 毫克 */
/* The script says `accel 0 0 1` = 1g => z = 1000 milligee */
chk(ui_sensor_get_x() == 0 && ui_sensor_get_y() == 0 && ui_sensor_get_z() == 1000, "SENSOR_XYZ",
ui_sensor_get_x(), ui_sensor_get_z());
} else {
unimpl("SENSOR", 0);
}
if (ui_battery_fetch() == 1) {
chk(ui_battery_get_level() == 85, "BATTERY_LEVEL", ui_battery_get_level(), 85);
chk(ui_battery_is_charging() == 1, "BATTERY_CHARGING", ui_battery_is_charging(), 1);
chk(ui_power_is_saving() == 1, "POWER_SAVER", ui_power_is_saving(), 1);
} else {
unimpl("BATTERY", 0);
}
if (ui_gps_is_available() == 1) {
chk(ui_gps_fetch(500) == 1, "GPS_FETCH", 1, 0);
chk(ui_gps_get_lat_e6() == 31523000, "GPS_LAT", ui_gps_get_lat_e6(), 31523000);
chk(ui_gps_get_lon_e6() == 121470000, "GPS_LON", ui_gps_get_lon_e6(), 121470000);
} else {
unimpl("GPS", 0);
}
/* 震动在桌面只打一行日志、没有返回值;能掉进这支只说明号段通 */
/* Vibrate only logs a line on the desktop and returns nothing; reaching here just proves the number is wired up */
ui_vibrate_simple(50, 2);
/* ── 8. 窗口事件:尺寸 / 方向 / 关闭(脚本排在最后,见 runner)── */
/* ── 8. Window events: resize / orientation / close (the script puts them last; see the runner) ── */
t = wait_type(VML_MSG_WINDOWRESIZE, 12000);
chk(t == VML_MSG_WINDOWRESIZE, "EV_RESIZE", t, VML_MSG_WINDOWRESIZE);
chk(g_ma == 500 && g_mb == 700, "RESIZE_WH", g_ma, g_mb);
/* ⚠ 桌面脚手架里 `ui_scr_get_w/h` 报的是 `--screen`(见 vmlcli 的说明),**不跟着脚本 resize 变**
* ⚠ On the desktop scaffold `ui_scr_get_w/h` report `--screen` (see vmlcli's notes) and **do not follow the scripted resize**
* ⇒ 这里只要求"重问一次仍然是正数";"面积跟着变"那一维只在真机上成立,不在桌面判。
* => here we only require "asking again still gives a positive number"; the "area follows" dimension only holds on a device. */
chk(ui_scr_get_w() > 0 && ui_scr_get_h() > 0, "RESIZE_AREA_POSITIVE", ui_scr_get_w(), ui_scr_get_h());
t = wait_type(VML_MSG_WINDOWORIENT, 12000);
chk(t == VML_MSG_WINDOWORIENT, "EV_ORIENT", t, VML_MSG_WINDOWORIENT);
chk(g_ma == VML_ORIENT_LANDSCAPE, "ORIENT_A", g_ma, VML_ORIENT_LANDSCAPE);
t = wait_type(VML_MSG_WINDOWCLOSE, 12000);
chk(t == VML_MSG_WINDOWCLOSE, "EV_CLOSE", t, VML_MSG_WINDOWCLOSE);
chk(ui_win_is_closed() == 1, "CLOSED_FLAG", ui_win_is_closed(), 1);
return 0;
}
/* ── 交互档:把状态画在屏上(手机手动验收用)───────────────────── */
/* ── Interactive: draw the state on screen (manual acceptance on a phone) ── */
void draw_hud(int type, int a, int b, int keys) {
int w;
int h;
int lang;
char line[96];
w = ui_scr_get_w();
h = ui_scr_get_h();
lang = ui_scr_get_language();
ui_2d_clear(ui_color_rgb(16, 24, 32));
if (lang == 0) {
ui_2d_text(12, 20, "输入接口验收(点屏幕 / 按键 / 关窗)", ui_color_rgb(230, 240, 255), 20, VML_ANCHOR_LEFT);
} else {
ui_2d_text(12, 20, "input check (tap / press / close)", ui_color_rgb(230, 240, 255), 20, VML_ANCHOR_LEFT);
}
sprintf(line, "type=%d a=%d b=%d", type, a, b);
ui_2d_text(12, 70, line, ui_color_rgb(255, 215, 0), 18, VML_ANCHOR_LEFT);
sprintf(line, "touch v=%d x=%d y=%d down=%d", ui_touch_fetch(0), ui_touch_get_x(), ui_touch_get_y(), ui_touch_is_down());
ui_2d_text(12, 105, line, ui_color_rgb(120, 220, 255), 18, VML_ANCHOR_LEFT);
sprintf(line, "keys=%d left=%d right=%d", keys, ui_key_is_down(VML_KEY_LEFT), ui_key_is_down(VML_KEY_RIGHT));
ui_2d_text(12, 140, line, ui_color_rgb(120, 255, 160), 18, VML_ANCHOR_LEFT);
if (lang == 0) {
ui_2d_text(12, 190, "消息类型:1键下 2键上 6按下 7移动 8抬起 9定时 10关窗 11改尺寸 12转屏",
ui_color_rgb(150, 160, 180), 14, VML_ANCHOR_LEFT);
ui_2d_text(12, 215, "窗口可画区", ui_color_rgb(150, 160, 180), 14, VML_ANCHOR_LEFT);
} else {
ui_2d_text(12, 190, "msg types: 1 keydown 2 keyup 6 touchdown 7 move 8 up 9 timer 10 close 11 resize 12 orient",
ui_color_rgb(150, 160, 180), 14, VML_ANCHOR_LEFT);
ui_2d_text(12, 215, "drawable area", ui_color_rgb(150, 160, 180), 14, VML_ANCHOR_LEFT);
}
sprintf(line, "%dx%d", w, h);
ui_2d_text(12, 240, line, ui_color_rgb(150, 160, 180), 14, VML_ANCHOR_LEFT);
/* 触摸按下时在手指位置画一个圈(看得见"坐标对不对") */
/* While pressed, draw a ring at the finger (so "are the coordinates right" is visible) */
if (ui_touch_is_down()) {
ui_2d_circle(ui_touch_get_x(), ui_touch_get_y(), 30, ui_color_rgb(255, 80, 80), 1, 4);
}
ui_2d_present();
}
int run_interactive(void) {
int type;
int a;
int b;
int keys;
int idle;
type = 0; a = 0; b = 0; keys = 0; idle = 0;
draw_hud(type, a, b, keys);
while (idle < 120) {
type = ui_msg_fetch_wait(1000);
a = ui_msg_get_a();
b = ui_msg_get_b();
if (type == 0) {
idle = idle + 1;
} else {
idle = 0;
if (type == VML_MSG_KEYDOWN) { keys = keys + 1; }
if (type == VML_MSG_KEYUP) { /* 只记一次,画面上看 keys 计数 */ }
/* count once; keys are shown on screen */
}
draw_hud(type, a, b, keys);
if (type == VML_MSG_WINDOWCLOSE) { break; }
if (ui_win_is_closed()) { break; }
}
return 0;
}
int main(void) {
int scripted;
int r;
char buf[64];
g_ok = 0; g_fail = 0; g_unimpl = 0;
scripted = 0;
if (ui_arg_get_count() >= 2) {
buf[0] = 0;
ui_arg_get(1, buf, 64);
if (eq(buf, "scripted")) { scripted = 1; }
}
ui_win_open("ui_input", 0, 0);
if (scripted) {
r = run_scripted();
} else {
r = run_interactive();
}
ui_win_close();
if (scripted) {
printf("OK=%d FAIL=%d UNIMPL=%d\n", g_ok, g_fail, g_unimpl);
}
printf("DONE=1\n");
return 0;
}