Add a built-in on-screen keyboard for handset sessions
CI / test (push) Failing after 52s

- Opens for text-input-v3 fields, terminals, launcher search, Network password, emoji picker and the password dialog; a KB pill summons it manually.
- Enabled on CHASSIS=handset; CHANDRA_OSK=1|0 overrides, and it stays off under CHANDRA_TEST_CONFIG.
- Work area and panel popups shrink above the keyboard so focused content stays visible.
- Keys send evdev codes with Shift, Ctrl and Alt modifiers.
- chandra-ctl: `screenshot [PATH]` saves a PNG without opening the editor.
- Key names cover the full punctuation set and shifted symbols.
- Launcher plasmoid min width drops from 60 to 36.
This commit is contained in:
nandi committed 2026-11-28 00:18:32 -08:00
1 parent 1205329abd
commit 905dab8725
21 files changed
+1335 -68

No files matched your search

+1 -1
View File
@@ -150,7 +150,7 @@ pub fn Kms::present_frame(_self : Kms) -> Unit {
///| The seat, DRM and input fds `poll` services, for an idle loop to block on.
pub fn Kms::wait_fds(_self : Kms) -> Array[Int] {
let out : Array[Int] = []
for i = 0; i < 3; i = i + 1 {
for i = 0; i < 4; i = i + 1 {
let fd = c_fd(i)
if fd >= 0 {
out.push(fd)
+185 -28
View File
@@ -17,6 +17,7 @@
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <libseat.h>
@@ -32,6 +33,9 @@
#define KEY_F11_CODE 87
#define KEY_F12_CODE 88
#define BTN_LEFT_CODE 0x110
#define BTN_RIGHT_CODE 0x111
#define TOUCH_LONG_MS 500
#define TOUCH_DRAG_PX 12
/* ---- damage ---- */
@@ -142,6 +146,9 @@ static int g_front; /* index currently scanned out */
static int g_flip_pending;
static int g_need_modeset;
static int g_width, g_height;
static int g_phys_w, g_phys_h;
static uint32_t g_mm_w, g_mm_h;
static int g_scale = 1;
static int g_blank; /* screen turned off (DPMS) by mp_kms_set_power */
static int fb_create(struct fbuf *b, int w, int h) {
@@ -189,6 +196,7 @@ static int pick_output(int fd) {
}
if (crtc) {
g_conn = c->connector_id; g_crtc = crtc; g_mode = c->modes[mi];
g_mm_w = c->mmWidth; g_mm_h = c->mmHeight;
ok = 0;
}
}
@@ -206,8 +214,11 @@ static int g_cur_ready;
static int g_cur_shape = -1;
static int g_px, g_py; /* pointer position (hotspot) */
static int g_hotx, g_hoty;
static int g_touch_active;
enum { TOUCH_IDLE = 0, TOUCH_PENDING = 1, TOUCH_DRAG = 2, TOUCH_LONG = 3 };
static int g_touch_state;
static int32_t g_touch_slot;
static int g_touch_x0, g_touch_y0;
static int g_touch_timer = -1;
static const char *ART_ARROW[] = {
"X...........", "XX..........", "XoX.........", "XooX........", "XoooX.......",
@@ -228,13 +239,26 @@ static void cursor_draw(int shape) {
g_hotx = 0; g_hoty = 0;
if (shape == 4) { art = ART_HAND; g_hotx = 5; g_hoty = 0; }
else if (shape == 9) { art = ART_IBEAM; g_hotx = 3; g_hoty = 7; }
int sc = g_scale > 1 ? g_scale : 1;
if (sc > 3) sc = 3;
g_hotx *= sc;
g_hoty *= sc;
uint32_t *px = (uint32_t *)g_cur_buf.map;
memset(px, 0, (size_t)CUR * CUR * 4);
for (int y = 0; art[y] && y < CUR; y++) {
for (int x = 0; art[y][x] && x < CUR; x++) {
for (int y = 0; art[y]; y++) {
for (int x = 0; art[y][x]; x++) {
char ch = art[y][x];
if (ch == 'X') px[y * (g_cur_buf.pitch / 4) + x] = 0xFF000000u;
else if (ch == 'o') px[y * (g_cur_buf.pitch / 4) + x] = 0xFFFFFFFFu;
if (ch != 'X' && ch != 'o') continue;
uint32_t col = ch == 'X' ? 0xFF000000u : 0xFFFFFFFFu;
for (int sy = 0; sy < sc; sy++) {
int py = y * sc + sy;
if (py >= CUR) break;
for (int sx = 0; sx < sc; sx++) {
int px_i = x * sc + sx;
if (px_i >= CUR) break;
px[py * (g_cur_buf.pitch / 4) + px_i] = col;
}
}
}
}
}
@@ -242,7 +266,31 @@ static void cursor_draw(int shape) {
static void cursor_apply(void) {
if (!g_cur_ready || !g_seat_enabled) return;
drmModeSetCursor(g_drm, g_crtc, g_cur_buf.handle, CUR, CUR);
drmModeMoveCursor(g_drm, g_crtc, g_px - g_hotx, g_py - g_hoty);
drmModeMoveCursor(g_drm, g_crtc, g_px * g_scale - g_hotx, g_py * g_scale - g_hoty);
}
/* ---- touch long-press timer ---------------------------------------------- */
static void touch_timer_arm(int ms) {
if (g_touch_timer < 0) return;
struct itimerspec its = {0};
its.it_value.tv_sec = ms / 1000;
its.it_value.tv_nsec = (long)(ms % 1000) * 1000000L;
timerfd_settime(g_touch_timer, 0, &its, NULL);
}
static void touch_timer_disarm(void) {
if (g_touch_timer < 0) return;
struct itimerspec its = {0};
timerfd_settime(g_touch_timer, 0, &its, NULL);
}
static void touch_cancel(void) {
touch_timer_disarm();
if (g_touch_state == TOUCH_DRAG) {
q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0);
}
g_touch_state = TOUCH_IDLE;
}
/* ---- xkb + libinput ------------------------------------------------------ */
@@ -338,35 +386,64 @@ static void handle_li_event(struct libinput_event *ev) {
}
case LIBINPUT_EVENT_TOUCH_DOWN: {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
if (!g_touch_active) {
g_touch_active = 1;
if (g_touch_state == TOUCH_IDLE) {
g_touch_slot = libinput_event_touch_get_seat_slot(t);
g_px = (int)libinput_event_touch_get_x_transformed(t, (uint32_t)g_width);
g_py = (int)libinput_event_touch_get_y_transformed(t, (uint32_t)g_height);
clamp_pointer();
g_touch_x0 = g_px;
g_touch_y0 = g_py;
g_touch_state = TOUCH_PENDING;
q_push(EV_MOVE, g_px, g_py, 0, 0);
q_push(EV_BUTTON, BTN_LEFT_CODE, 1, 0, 0);
cursor_apply();
touch_timer_arm(TOUCH_LONG_MS);
}
break;
}
case LIBINPUT_EVENT_TOUCH_MOTION: {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
if (g_touch_active && libinput_event_touch_get_seat_slot(t) == g_touch_slot) {
if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) {
g_px = (int)libinput_event_touch_get_x_transformed(t, (uint32_t)g_width);
g_py = (int)libinput_event_touch_get_y_transformed(t, (uint32_t)g_height);
clamp_pointer();
q_push(EV_MOVE, g_px, g_py, 0, 0);
cursor_apply();
if (g_touch_state == TOUCH_PENDING) {
int dx = g_px - g_touch_x0;
int dy = g_py - g_touch_y0;
if (dx < -TOUCH_DRAG_PX || dx > TOUCH_DRAG_PX || dy < -TOUCH_DRAG_PX || dy > TOUCH_DRAG_PX) {
touch_timer_disarm();
g_touch_state = TOUCH_DRAG;
q_push(EV_MOVE, g_touch_x0, g_touch_y0, 0, 0);
q_push(EV_BUTTON, BTN_LEFT_CODE, 1, 0, 0);
q_push(EV_MOVE, g_px, g_py, 0, 0);
cursor_apply();
}
} else if (g_touch_state == TOUCH_DRAG) {
q_push(EV_MOVE, g_px, g_py, 0, 0);
cursor_apply();
}
}
break;
}
case LIBINPUT_EVENT_TOUCH_UP: {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) {
int st = g_touch_state;
touch_timer_disarm();
g_touch_state = TOUCH_IDLE;
if (st == TOUCH_PENDING) {
q_push(EV_MOVE, g_px, g_py, 0, 0);
q_push(EV_BUTTON, BTN_LEFT_CODE, 1, 0, 0);
q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0);
} else if (st == TOUCH_DRAG) {
q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0);
}
}
break;
}
case LIBINPUT_EVENT_TOUCH_UP:
case LIBINPUT_EVENT_TOUCH_CANCEL: {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
if (g_touch_active && libinput_event_touch_get_seat_slot(t) == g_touch_slot) {
g_touch_active = 0;
q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0);
if (g_touch_state != TOUCH_IDLE && libinput_event_touch_get_seat_slot(t) == g_touch_slot) {
touch_cancel();
}
break;
}
@@ -429,6 +506,36 @@ void mp_kms_set_power(int32_t on) {
}
static int is_handset_chassis(void) {
FILE *f = fopen("/etc/machine-info", "r");
if (!f) return 0;
char buf[1024];
size_t n = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[n] = '\0';
return strstr(buf, "CHASSIS=handset") != NULL;
}
static int pick_scale(int pw, int ph, uint32_t mm_w, uint32_t mm_h) {
const char *env = getenv("CHANDRA_SCALE");
if (env && *env) {
int s = atoi(env);
if (s < 1) s = 1;
if (s > 4) s = 4;
return s;
}
int dpi = 0;
if (mm_w > 0 && mm_h > 0) {
int dx = (int)((double)pw * 25.4 / (double)mm_w);
int dy = (int)((double)ph * 25.4 / (double)mm_h);
dpi = dx > dy ? dx : dy;
}
if ((dpi >= 200 || is_handset_chassis()) && pw >= 800 && ph >= 800) {
return 2;
}
return 1;
}
/* Open seat, DRM output and input. Returns 0 on success, negative step code on failure. */
MOONBIT_FFI_EXPORT
int32_t mp_kms_open(void) {
@@ -448,10 +555,18 @@ int32_t mp_kms_open(void) {
else { libseat_close_device(g_seat, id); close(fd); }
}
if (g_drm < 0) { fprintf(stderr, "kms: no DRM device with a connected display\n"); return -3; }
g_width = g_mode.hdisplay; g_height = g_mode.vdisplay;
g_phys_w = g_mode.hdisplay;
g_phys_h = g_mode.vdisplay;
g_scale = pick_scale(g_phys_w, g_phys_h, g_mm_w, g_mm_h);
g_width = g_phys_w / g_scale;
g_height = g_phys_h / g_scale;
if (g_scale > 1) {
fprintf(stderr, "kms: output %dx%d scale %dx -> logical %dx%d\n",
g_phys_w, g_phys_h, g_scale, g_width, g_height);
}
g_saved_crtc = drmModeGetCrtc(g_drm, g_crtc);
if (fb_create(&g_fb[0], g_width, g_height) < 0 || fb_create(&g_fb[1], g_width, g_height) < 0) {
if (fb_create(&g_fb[0], g_phys_w, g_phys_h) < 0 || fb_create(&g_fb[1], g_phys_w, g_phys_h) < 0) {
fprintf(stderr, "kms: cannot create framebuffers\n");
return -4;
}
@@ -481,6 +596,7 @@ int32_t mp_kms_open(void) {
fprintf(stderr, "kms: cannot start libinput on seat %s\n", g_seat ? libseat_seat_name(g_seat) : "?");
return -6;
}
g_touch_timer = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
dispatch_input();
return 0;
}
@@ -493,12 +609,13 @@ MOONBIT_FFI_EXPORT int32_t mp_kms_quit_requested(void) { return g_quit; }
MOONBIT_FFI_EXPORT
int32_t mp_kms_poll(void) {
if (!g_seat) return 0;
struct pollfd pfd[3];
struct pollfd pfd[4];
int n = 0;
pfd[n].fd = libseat_get_fd(g_seat); pfd[n].events = POLLIN; n++;
pfd[n].fd = g_drm; pfd[n].events = POLLIN; n++;
int lifd = g_li ? libinput_get_fd(g_li) : -1;
pfd[n].fd = lifd; pfd[n].events = POLLIN; n++;
pfd[n].fd = g_touch_timer; pfd[n].events = POLLIN; n++;
poll(pfd, (nfds_t)n, 0);
libseat_dispatch(g_seat, 0);
if (pfd[1].revents & POLLIN) {
@@ -509,10 +626,7 @@ int32_t mp_kms_poll(void) {
/* VT switched away: stop input, give up DRM master, acknowledge. */
g_seat_lost = 0;
g_suspended = 1;
if (g_touch_active) {
g_touch_active = 0;
q_push(EV_BUTTON, BTN_LEFT_CODE, 0, 0, 0);
}
touch_cancel();
if (g_li) libinput_suspend(g_li);
drmDropMaster(g_drm);
libseat_disable_seat(g_seat);
@@ -525,11 +639,24 @@ int32_t mp_kms_poll(void) {
if (g_blank) set_dpms(0);
}
if (g_li) dispatch_input();
if (g_touch_timer >= 0 && (pfd[3].revents & POLLIN)) {
uint64_t exp = 0;
while (read(g_touch_timer, &exp, sizeof exp) > 0) {}
if (g_touch_state == TOUCH_PENDING) {
g_touch_state = TOUCH_LONG;
g_px = g_touch_x0;
g_py = g_touch_y0;
q_push(EV_MOVE, g_px, g_py, 0, 0);
q_push(EV_BUTTON, BTN_RIGHT_CODE, 1, 0, 0);
q_push(EV_BUTTON, BTN_RIGHT_CODE, 0, 0, 0);
cursor_apply();
}
}
return g_seat_enabled;
}
/* The fds mp_kms_poll services (0 seat, 1 DRM, 2 libinput), for an idle loop to block on;
-1 when not open. */
/* The fds mp_kms_poll services (0 seat, 1 DRM, 2 libinput, 3 touch timer), for an idle loop to
block on; -1 when not open. */
MOONBIT_FFI_EXPORT
int32_t mp_kms_fd(int32_t i) {
if (!g_seat) return -1;
@@ -537,6 +664,7 @@ int32_t mp_kms_fd(int32_t i) {
case 0: return libseat_get_fd(g_seat);
case 1: return g_drm;
case 2: return g_li ? libinput_get_fd(g_li) : -1;
case 3: return g_touch_timer;
default: return -1;
}
}
@@ -549,12 +677,39 @@ int32_t mp_kms_frame_ready(void) {
/* RGBA8888 rows -> the scanout buffer's XRGB8888 (B, G, R, X), one pixel as one 32-bit word. */
static void copy_rows(struct fbuf *b, const uint8_t *src, int w, int x0, int y0, int x1, int y1) {
if (g_scale <= 1) {
for (int y = y0; y < y1; y++) {
uint32_t *d = (uint32_t *)(b->map + (size_t)y * b->pitch);
const uint32_t *s = (const uint32_t *)(src + (size_t)y * (size_t)w * 4);
for (int x = x0; x < x1; x++) {
uint32_t v = s[x];
d[x] = 0xff000000u | (v & 0x0000ff00u) | ((v >> 16) & 0xffu) | ((v & 0xffu) << 16);
}
}
return;
}
int sc = g_scale;
int px0 = x0 * sc;
int px1 = x1 * sc;
if (px1 > g_phys_w) px1 = g_phys_w;
if (px0 >= px1) return;
size_t row_bytes = (size_t)(px1 - px0) * 4;
for (int y = y0; y < y1; y++) {
uint32_t *d = (uint32_t *)(b->map + (size_t)y * b->pitch);
int py0 = y * sc;
if (py0 >= g_phys_h) break;
uint32_t *d0 = (uint32_t *)(b->map + (size_t)py0 * b->pitch);
const uint32_t *s = (const uint32_t *)(src + (size_t)y * (size_t)w * 4);
for (int x = x0; x < x1; x++) {
uint32_t v = s[x];
d[x] = 0xff000000u | (v & 0x0000ff00u) | ((v >> 16) & 0xffu) | ((v & 0xffu) << 16);
uint32_t c = 0xff000000u | (v & 0x0000ff00u) | ((v >> 16) & 0xffu) | ((v & 0xffu) << 16);
int dx = x * sc;
for (int k = 0; k < sc && dx + k < g_phys_w; k++) {
d0[dx + k] = c;
}
}
for (int sy = 1; sy < sc && py0 + sy < g_phys_h; sy++) {
uint8_t *dy = b->map + (size_t)(py0 + sy) * b->pitch + (size_t)px0 * 4;
memcpy(dy, (const uint8_t *)(d0 + px0), row_bytes);
}
}
}
@@ -649,7 +804,9 @@ int32_t mp_kms_keymap_copy(moonbit_bytes_t out) {
MOONBIT_FFI_EXPORT
void mp_kms_close(void) {
g_touch_active = 0;
touch_cancel();
if (g_touch_timer >= 0) { close(g_touch_timer); g_touch_timer = -1; }
g_phys_w = 0; g_phys_h = 0; g_mm_w = 0; g_mm_h = 0; g_scale = 1;
if (g_li) { libinput_unref(g_li); g_li = NULL; }
if (g_udev) { udev_unref(g_udev); g_udev = NULL; }
if (g_xkb_state) { xkb_state_unref(g_xkb_state); g_xkb_state = NULL; }