diff --git a/compositor/bin/main.mbt b/compositor/bin/main.mbt index c17cb23..9030beb 100644 --- a/compositor/bin/main.mbt +++ b/compositor/bin/main.mbt @@ -92,6 +92,7 @@ fn main { for ev in n.take_input() { comp.handle_input(ev, now_ms) } + n.set_cursor_shape(comp.desired_cursor()) if n.frame_ready() { n.present(comp.present_to_gfx(gfx_ctx)) } diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index d10dc2f..21c0ee0 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -502,3 +502,16 @@ pub fn MoonPlasmaCompositor::handle_input( } } } + +///| Cursor shape (wp_cursor_shape_v1 value) for the current pointer position: shell UI +/// decides over the launcher; a client window decides inside its client area; otherwise default. +pub fn MoonPlasmaCompositor::desired_cursor(self : MoonPlasmaCompositor) -> UInt { + let shell_shape = self.shell.cursor_at(self.ptr_x.to_int(), self.ptr_y.to_int()) + if shell_shape != 0U { + return shell_shape + } + match self.ptr_win { + Some(w) => self.server.client_cursor_shape(w) + None => 1U + } +} diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index c34ce14..3c8315e 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -78,15 +78,36 @@ test "host input reaches the focused client and the launcher popup opens" { assert_true(has_msg(msgs, ptr_id, 3U)) // button assert_true(has_msg(msgs, kbd_id, 3U)) // wl_keyboard.key + // Cursor: a client that asks for a text cursor gets it inside its window... + let shape_mgr = bind_global(c, reg_id, "wp_cursor_shape_manager_v1", 1U) + let shape_dev = c.alloc_id() + let w3 = @wayland.WireWriter::new() + w3.u32(shape_dev) + w3.u32(ptr_id) + c.send(w3.finish_msg(shape_mgr, 1U)) + let w4 = @wayland.WireWriter::new() + w4.u32(1U) // serial + w4.u32(9U) // text + c.send(w4.finish_msg(shape_dev, 1U)) + for _i = 0; _i < 5; _i = _i + 1 { + comp.step() + @sys.sleep_ms(2) + } + assert_eq(comp.desired_cursor(), 9U) + // Launcher: click the button -> popup opens and swallows keys. ignore(comp.compose_frame()) // lays out the panel comp.handle_input(@nested.InputEvent::PointerMove(60.0, 700.0), 20U) + assert_eq(comp.desired_cursor(), 4U) // ...but the launcher button always shows a hand comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 21U) comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 22U) assert_true(comp.shell.popup_open()) comp.handle_input(@nested.InputEvent::Key(30U, true), 23U) let after = drain_msgs(c) assert_false(has_msg(after, kbd_id, 3U)) // swallowed by the launcher + // Off the shell and off any window: default arrow. + comp.handle_input(@nested.InputEvent::PointerMove(1270.0, 5.0), 25U) + assert_eq(comp.desired_cursor(), 1U) // Escape closes it. comp.handle_input(@nested.InputEvent::Key(1U, true), 24U) assert_false(comp.shell.popup_open()) diff --git a/compositor/nested/nested.mbt b/compositor/nested/nested.mbt index f371320..655c098 100644 --- a/compositor/nested/nested.mbt +++ b/compositor/nested/nested.mbt @@ -17,6 +17,10 @@ pub(all) struct NestedWindow { fds : Array[Int] mut keymap : Bytes mut keyboard_id : UInt + mut shape_mgr_id : UInt + mut shape_dev_id : UInt + mut enter_serial : UInt + mut shape_applied : UInt fd : Int rx : Array[Byte] globals : Map[String, (UInt, UInt)] @@ -168,6 +172,14 @@ fn NestedWindow::dispatch( w.u32(id) self.send(w.finish_msg(self.seat_id, 0U)) self.pointer_id = id + if self.shape_mgr_id != 0U { + let dev = self.alloc() + let wd = @wayland.WireWriter::new() + wd.u32(dev) + wd.u32(id) + self.send(wd.finish_msg(self.shape_mgr_id, 1U)) + self.shape_dev_id = dev + } } if (caps & 2U) != 0U && self.keyboard_id == 0U { let id = self.alloc() @@ -209,7 +221,8 @@ fn NestedWindow::dispatch( } else if self.pointer_id != 0U && obj == self.pointer_id { match op { 0U => { - let _serial = r.u32() + self.enter_serial = r.u32() + self.shape_applied = 0U // host resets the cursor on enter let _surf = r.u32() self.px = r.i32().to_double() / 256.0 self.py = r.i32().to_double() / 256.0 @@ -282,6 +295,10 @@ pub fn NestedWindow::open( fds: [], keymap: Bytes::from_array([]), keyboard_id: 0U, + shape_mgr_id: 0U, + shape_dev_id: 0U, + enter_serial: 0U, + shape_applied: 0U, rx: [], globals: {}, live_buffers: {}, @@ -313,6 +330,7 @@ pub fn NestedWindow::open( self.shm_id = self.bind("wl_shm", 1U) self.wm_id = self.bind("xdg_wm_base", 2U) self.seat_id = self.bind("wl_seat", 5U) + self.shape_mgr_id = self.bind("wp_cursor_shape_manager_v1", 1U) if self.comp_id == 0U || self.shm_id == 0U || self.wm_id == 0U { println("nested: host compositor lacks wl_compositor/wl_shm/xdg_wm_base") return None @@ -442,3 +460,16 @@ pub fn NestedWindow::close(self : NestedWindow) -> Unit { pub fn NestedWindow::frame_ready(self : NestedWindow) -> Bool { !self.frame_pending && !self.closed } + +///| Ask the host to show cursor `shape` (wp_cursor_shape_v1 enum value) over our +/// window. No-op if unchanged, unsupported by the host, or the pointer is outside. +pub fn NestedWindow::set_cursor_shape(self : NestedWindow, shape : UInt) -> Unit { + if self.shape_dev_id == 0U || self.enter_serial == 0U || shape == self.shape_applied { + return + } + let w = @wayland.WireWriter::new() + w.u32(self.enter_serial) + w.u32(shape) + self.send(w.finish_msg(self.shape_dev_id, 1U)) + self.shape_applied = shape +} diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index b9b2c14..f35955c 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -199,6 +199,24 @@ fn DesktopShell::launch(self : DesktopShell, id : String) -> Result[Unit, String } } +///| wp_cursor_shape_v1 value for the shell UI under (x, y): 4 pointer (hand) over the +/// launcher button and popup entries, 9 text over the search field, else 0 (not shell-specific). +pub fn DesktopShell::cursor_at(self : DesktopShell, x : Int, y : Int) -> UInt { + if inside(self.launcher_rect, x, y) { + 4U + } else if self.launcher_state.open && inside(self.popup_rect, x, y) { + if self.launcher_state.hover >= 0 { + 4U + } else if y - self.popup_rect.y < 52 { + 9U + } else { + 1U + } + } else { + 0U + } +} + ///| Pointer press at (x, y). Returns true when the shell consumed it. pub fn DesktopShell::pointer_down(self : DesktopShell, x : Int, y : Int) -> Bool { let launch = fn(id : String) { self.launch(id) } @@ -275,7 +293,7 @@ pub fn DesktopShell::render( let size = @ui.size(p.w, p.h) let local : Array[@types.DrawOp] = match p.id { "launcher" => @launcher.render_button(size, tokens, self.launcher_state) - "clock" => @clock.render_digital(size, tokens, now()) + "clock" => @clock.render_digital(size, tokens, now(), hour12=true) "layout" => [ @ui.fill_rect(@ui.rect(0, 0, p.w, p.h), tokens.palette.surface_hover, radius=tokens.radius.reinterpret_as_int()), @ui.text(p.w / 2, p.h / 2, layout_label, tokens.font_size.reinterpret_as_int() - 2, tokens.palette.foreground, align=CENTER), diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index 934223a..c001923 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -38,6 +38,8 @@ enum ObjKind { ObjSubsurface ObjDataManager ObjDataObj + ObjCursorShapeMgr + ObjCursorShapeDev } ///| @@ -99,6 +101,8 @@ struct ClientConn { pointer_ids : Array[UInt] keyboard_ids : Array[UInt] mut seat_ver : UInt + /// wp_cursor_shape_v1 shape the client last asked for (0 = never set). + mut cursor_shape : UInt mut alive : Bool } @@ -120,6 +124,7 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn { pointer_ids: [], keyboard_ids: [], seat_ver: 1U, + cursor_shape: 0U, alive: true, } } @@ -219,6 +224,7 @@ fn emit_registry_globals(c : ClientConn, reg_id : UInt) -> Unit { (6U, "wl_output", 4U), (7U, "wl_subcompositor", 1U), (8U, "wl_data_device_manager", 3U), + (9U, "wp_cursor_shape_manager_v1", 1U), ] for g in globals { let w = WireWriter::new() @@ -385,6 +391,8 @@ fn WaylandServer::dispatch_msg( c.objects.set(new_id, ObjSeat) c.seat_ver = ver emit_seat_info(c, new_id) + } else if iface == "wp_cursor_shape_manager_v1" { + c.objects.set(new_id, ObjCursorShapeMgr) } else if iface == "wl_data_device_manager" { c.objects.set(new_id, ObjDataManager) } else if iface == "wl_subcompositor" { @@ -497,6 +505,22 @@ fn WaylandServer::dispatch_msg( c.objects.set(id, ObjDataObj) } ObjDataObj => () + ObjCursorShapeMgr => + if opcode == 1U { + // get_pointer(cursor_shape_device: new_id, pointer) + let id = reader.u32() + c.objects.set(id, ObjCursorShapeDev) + } else if opcode == 2U { + // get_tablet_tool_v2: accepted, unused + let id = reader.u32() + c.objects.set(id, ObjCursorShapeDev) + } + ObjCursorShapeDev => + if opcode == 1U { + // set_shape(serial, shape) + let _serial = reader.u32() + c.cursor_shape = reader.u32() + } ObjSubsurface => if opcode == 0U { c.objects.remove(obj_id) @@ -1059,3 +1083,12 @@ pub fn WaylandServer::keyboard_modifiers(self : WaylandServer, win_id : UInt64, } } } + +///| Cursor shape (wp_cursor_shape_v1 enum value) the client owning `win_id` last +/// requested; 1 (default) if it never asked for one. +pub fn WaylandServer::client_cursor_shape(self : WaylandServer, win_id : UInt64) -> UInt { + match self.win_client(win_id) { + Some((c, _)) => if c.cursor_shape == 0U { 1U } else { c.cursor_shape } + None => 1U + } +} diff --git a/lib/clock/clock.mbt b/lib/clock/clock.mbt index 3d5ad75..0d35dc2 100644 --- a/lib/clock/clock.mbt +++ b/lib/clock/clock.mbt @@ -11,11 +11,16 @@ pub fn render_digital( size : @types.Size, t : @theme.Tokens, now : @types.Datetime, + hour12? : Bool = false, ) -> Array[@types.DrawOp] { let w = size.w.reinterpret_as_int() let h = size.h.reinterpret_as_int() let fs = t.font_size.reinterpret_as_int() - let time = @timefmt.hhmm(now.hour.to_int(), now.minute.to_int()) + let time = if hour12 { + @timefmt.hhmm12(now.hour.to_int(), now.minute.to_int()) + } else { + @timefmt.hhmm(now.hour.to_int(), now.minute.to_int()) + } let date = @timefmt.date_line( now.weekday.to_int(), now.day.to_int(), @@ -72,6 +77,15 @@ pub fn render_binary( ops } +///| +test "digital clock in 12-hour mode" { + let ops = render_digital(@ui.size(120, 36), test_tokens(), test_time(), hour12=true) + match ops[0] { + Text(t) => assert_eq(t.text, "2:07 PM") + _ => fail("expected text") + } +} + ///| fn test_tokens() -> @theme.Tokens { let c = @ui.color(10, 20, 30, 255) diff --git a/lib/timefmt/timefmt.mbt b/lib/timefmt/timefmt.mbt index c6d0b81..3da3a7c 100644 --- a/lib/timefmt/timefmt.mbt +++ b/lib/timefmt/timefmt.mbt @@ -30,6 +30,13 @@ pub fn hhmm(hour : Int, minute : Int) -> String { pad2(hour) + ":" + pad2(minute) } +///| 12-hour clock, e.g. 14:07 -> "2:07 PM", 00:05 -> "12:05 AM". +pub fn hhmm12(hour : Int, minute : Int) -> String { + let h = hour % 12 + let h = if h == 0 { 12 } else { h } + h.to_string() + ":" + pad2(minute) + (if hour >= 12 { " PM" } else { " AM" }) +} + ///| pub fn hhmmss(hour : Int, minute : Int, second : Int) -> String { hhmm(hour, minute) + ":" + pad2(second) @@ -55,6 +62,11 @@ pub fn bcd_bits(digit : Int, bits : Int) -> Array[Bool] { test "formatting" { assert_eq(hhmm(9, 5), "09:05") assert_eq(hhmmss(23, 59, 7), "23:59:07") + assert_eq(hhmm12(14, 7), "2:07 PM") + assert_eq(hhmm12(0, 5), "12:05 AM") + assert_eq(hhmm12(12, 0), "12:00 PM") + assert_eq(hhmm12(23, 59), "11:59 PM") + assert_eq(hhmm12(9, 30), "9:30 AM") assert_eq(date_line(2, 24, 11), "Tue 24 Nov") }