Add display rotation: Rot pill, chandra-ctl rotate, sensor auto-rotate
CI / test (push) Failing after 51s

- Rotate the display 0/90/180/270 degrees in the KMS host, with scanout and libinput touch/pointer coordinates transformed to match
- Add a touch "Rot" pill beside "KB": tap turns 90 CW, long-press or right-click flips 180
- Add `chandra-ctl rotate [cw|ccw|flip|0|90|180|270|status]`; CHANDRA_ROTATE sets the initial orientation
- Add session/chandra-auto-rotate, which follows iio-sensor-proxy orientation (disable with CHANDRA_AUTO_ROTATE=0)
- Advertise window_menu, maximize, fullscreen and minimize via xdg_toplevel.wm_capabilities (xdg_wm_base v5+)
- Add docs/handset.md covering handset touch, scaling, OSK and rotation
This commit is contained in:
nandi committed 2026-11-28 00:39:24 -08:00
1 parent 905dab8725
commit 2e00009c79
21 files changed
+669 -63

No files matched your search

+151 -34
View File
@@ -147,10 +147,45 @@ 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 int g_base_w, g_base_h; /* unrotated logical size */
static int g_rot = 0; /* 0=0 deg, 1=90 deg CW, 2=180 deg flip, 3=270 deg CW */
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 void logical_to_unrot(int x, int y, int *ux, int *uy) {
switch (g_rot) {
case 1: *ux = (g_base_w - 1) - y; *uy = x; break;
case 2: *ux = (g_base_w - 1) - x; *uy = (g_base_h - 1) - y; break;
case 3: *ux = y; *uy = (g_base_h - 1) - x; break;
default: *ux = x; *uy = y; break;
}
}
static void unrot_to_logical(int rx, int ry, int *x, int *y) {
if (rx < 0) rx = 0;
if (ry < 0) ry = 0;
if (g_base_w > 0 && rx >= g_base_w) rx = g_base_w - 1;
if (g_base_h > 0 && ry >= g_base_h) ry = g_base_h - 1;
switch (g_rot) {
case 1: *x = ry; *y = (g_base_w - 1) - rx; break;
case 2: *x = (g_base_w - 1) - rx; *y = (g_base_h - 1) - ry; break;
case 3: *x = (g_base_h - 1) - ry; *y = rx; break;
default: *x = rx; *y = ry; break;
}
}
static void apply_rotation_dims(int rot) {
g_rot = ((rot % 4) + 4) % 4;
if (g_rot == 1 || g_rot == 3) {
g_width = g_base_h;
g_height = g_base_w;
} else {
g_width = g_base_w;
g_height = g_base_h;
}
}
static int fb_create(struct fbuf *b, int w, int h) {
struct drm_mode_create_dumb c = { .width = (uint32_t)w, .height = (uint32_t)h, .bpp = 32 };
if (drmIoctl(g_drm, DRM_IOCTL_MODE_CREATE_DUMB, &c) < 0) return -1;
@@ -265,8 +300,12 @@ static void cursor_draw(int shape) {
static void cursor_apply(void) {
if (!g_cur_ready || !g_seat_enabled) return;
int ux = g_px, uy = g_py;
if (g_rot != 0 && g_base_w > 0 && g_base_h > 0) {
logical_to_unrot(g_px, g_py, &ux, &uy);
}
drmModeSetCursor(g_drm, g_crtc, g_cur_buf.handle, CUR, CUR);
drmModeMoveCursor(g_drm, g_crtc, g_px * g_scale - g_hotx, g_py * g_scale - g_hoty);
drmModeMoveCursor(g_drm, g_crtc, ux * g_scale - g_hotx, uy * g_scale - g_hoty);
}
/* ---- touch long-press timer ---------------------------------------------- */
@@ -354,8 +393,9 @@ static void handle_li_event(struct libinput_event *ev) {
}
case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: {
struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
g_px = (int)libinput_event_pointer_get_absolute_x_transformed(p, (uint32_t)g_width);
g_py = (int)libinput_event_pointer_get_absolute_y_transformed(p, (uint32_t)g_height);
int rx = (int)libinput_event_pointer_get_absolute_x_transformed(p, (uint32_t)g_base_w);
int ry = (int)libinput_event_pointer_get_absolute_y_transformed(p, (uint32_t)g_base_h);
unrot_to_logical(rx, ry, &g_px, &g_py);
clamp_pointer();
q_push(EV_MOVE, g_px, g_py, 0, 0);
cursor_apply();
@@ -388,8 +428,9 @@ static void handle_li_event(struct libinput_event *ev) {
struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
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);
int rx = (int)libinput_event_touch_get_x_transformed(t, (uint32_t)g_base_w);
int ry = (int)libinput_event_touch_get_y_transformed(t, (uint32_t)g_base_h);
unrot_to_logical(rx, ry, &g_px, &g_py);
clamp_pointer();
g_touch_x0 = g_px;
g_touch_y0 = g_py;
@@ -403,8 +444,9 @@ static void handle_li_event(struct libinput_event *ev) {
case LIBINPUT_EVENT_TOUCH_MOTION: {
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) {
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);
int rx = (int)libinput_event_touch_get_x_transformed(t, (uint32_t)g_base_w);
int ry = (int)libinput_event_touch_get_y_transformed(t, (uint32_t)g_base_h);
unrot_to_logical(rx, ry, &g_px, &g_py);
clamp_pointer();
if (g_touch_state == TOUCH_PENDING) {
int dx = g_px - g_touch_x0;
@@ -558,11 +600,19 @@ int32_t mp_kms_open(void) {
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_base_w = g_phys_w / g_scale;
g_base_h = g_phys_h / g_scale;
int init_rot = 0;
const char *renv = getenv("CHANDRA_ROTATE");
if (renv && *renv) {
if (!strcmp(renv, "90") || !strcmp(renv, "1")) init_rot = 1;
else if (!strcmp(renv, "180") || !strcmp(renv, "2")) init_rot = 2;
else if (!strcmp(renv, "270") || !strcmp(renv, "3")) init_rot = 3;
}
apply_rotation_dims(init_rot);
if (g_scale > 1 || g_rot != 0) {
fprintf(stderr, "kms: output %dx%d scale %dx rot %d -> logical %dx%d\n",
g_phys_w, g_phys_h, g_scale, g_rot * 90, g_width, g_height);
}
g_saved_crtc = drmModeGetCrtc(g_drm, g_crtc);
@@ -603,8 +653,23 @@ int32_t mp_kms_open(void) {
MOONBIT_FFI_EXPORT int32_t mp_kms_width(void) { return g_width; }
MOONBIT_FFI_EXPORT int32_t mp_kms_height(void) { return g_height; }
MOONBIT_FFI_EXPORT int32_t mp_kms_rotation(void) { return g_rot; }
MOONBIT_FFI_EXPORT int32_t mp_kms_quit_requested(void) { return g_quit; }
/* Switch display rotation (0=0, 1=90 CW, 2=180, 3=270 CW). Returns (width << 16) | height. */
MOONBIT_FFI_EXPORT
int32_t mp_kms_set_rotation(int32_t rot) {
if (g_base_w <= 0 || g_base_h <= 0) return 0;
touch_cancel();
apply_rotation_dims(rot);
g_px = g_width / 2;
g_py = g_height / 2;
stale_add(0, 0, 1 << 16, 1 << 16);
cursor_apply();
fprintf(stderr, "kms: rotation %d deg -> logical %dx%d\n", g_rot * 90, g_width, g_height);
return ((g_width & 0xFFFF) << 16) | (g_height & 0xFFFF);
}
/* Service seat, DRM and input fds without blocking. Returns 1 while the seat is active. */
MOONBIT_FFI_EXPORT
int32_t mp_kms_poll(void) {
@@ -677,7 +742,8 @@ 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) {
int sc = g_scale > 1 ? g_scale : 1;
if (g_rot == 0 && sc == 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);
@@ -688,28 +754,78 @@ static void copy_rows(struct fbuf *b, const uint8_t *src, int w, int x0, int y0,
}
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++) {
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];
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;
if (g_rot == 0) {
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++) {
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];
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);
}
}
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);
return;
}
if (g_rot == 2) {
int px0 = (g_base_w - x1) * sc;
int px1 = (g_base_w - x0) * sc;
if (px0 < 0) px0 = 0;
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++) {
int uy = (g_base_h - 1) - y;
int py0 = uy * sc;
if (py0 < 0 || py0 >= g_phys_h) continue;
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];
uint32_t c = 0xff000000u | (v & 0x0000ff00u) | ((v >> 16) & 0xffu) | ((v & 0xffu) << 16);
int dx = ((g_base_w - 1) - x) * sc;
for (int k = 0; k < sc && dx + k < g_phys_w; k++) {
if (dx + k >= 0) 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);
}
}
return;
}
/* 90 deg CW (g_rot == 1) and 270 deg CW (g_rot == 3). */
for (int y = y0; y < y1; y++) {
int ux = (g_rot == 1) ? ((g_base_w - 1) - y) : y;
int px = ux * sc;
if (px < 0 || px >= g_phys_w) continue;
const uint32_t *s = (const uint32_t *)(src + (size_t)y * (size_t)w * 4);
for (int x = x0; x < x1; x++) {
int uy = (g_rot == 1) ? x : ((g_base_h - 1) - x);
int py = uy * sc;
if (py < 0 || py >= g_phys_h) continue;
uint32_t v = s[x];
uint32_t c = 0xff000000u | (v & 0x0000ff00u) | ((v >> 16) & 0xffu) | ((v & 0xffu) << 16);
for (int sy = 0; sy < sc && py + sy < g_phys_h; sy++) {
uint32_t *d = (uint32_t *)(b->map + (size_t)(py + sy) * b->pitch);
for (int sx = 0; sx < sc && px + sx < g_phys_w; sx++) {
d[px + sx] = c;
}
}
}
}
}
@@ -723,6 +839,7 @@ int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h);
MOONBIT_FFI_EXPORT
int32_t mp_kms_present_damage(moonbit_bytes_t pixels, int32_t w, int32_t h, int32_t dx, int32_t dy, int32_t dw,
int32_t dh) {
if (w != g_width || h != g_height) return 0;
int cw = w < g_width ? w : g_width;
int ch = h < g_height ? h : g_height;
int x0 = dx < 0 ? 0 : dx, y0 = dy < 0 ? 0 : dy;
@@ -806,7 +923,7 @@ MOONBIT_FFI_EXPORT
void mp_kms_close(void) {
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;
g_phys_w = 0; g_phys_h = 0; g_base_w = 0; g_base_h = 0; g_rot = 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; }