1// AT-SPI for the Wayland window (PLAN.md, F6c): when the2// session's accessibility is on (org.a11y.Status), the window joins the3// accessibility bus, embeds its application object in the registry's4// desktop and answers screen readers such as Orca from the tree the5// program sends: org.a11y.atspi.Accessible, Component, Action, Value,6// Text and EditableText, Table and TableCell, and the properties, on an object a node under7// /org/a11y/atspi/accessible/. Changes to the tree go out as the8// Event.Object signals Orca follows; what a screen reader asks comes back9// to the program as Event::Action.1011import std::string;12import std::vector;13import std::option;14import std::dispose;15import std::map;16import std::math;17import pkg::alloy::dbus;1819import window;20import unicode;21import text_ranges;2223const ROOT_PATH: &[u8] = &"/org/a11y/atspi/accessible/root";24const NODE_PATH: &[u8] = &"/org/a11y/atspi/accessible/";2526pub type Atspi : Disposable = struct {27 pub bus: Connection,28 pub title: String,29 pub tree: AccessTree,30 // the desktop the application is embedded in, as an object reference31 pub desktop: [String : 2],32 pub actions: Vector<ActionRequest>,33 pub id: i32, // what the registry calls the application34 pub locale: String = String::from(&"en.UTF-8"), // the window's language, as POSIX writes it35 // Text's answers, set by the first tree taken36 texts: Option<(&var Atspi, Object, &Message) -> Option<Message>> = ::None,37};3839// Joins the accessibility bus when the session's accessibility is on;40// none otherwise.41pub fn Atspi::connect(title: &[u8]) -> Option<Atspi> {42 var session = if (Connection::session() is ::Some |move c|) move c else {43 return ::None;44 };45 var enabled = false;46 const properties: [&[u8] : 2] = [&"IsEnabled", &"ScreenReaderEnabled"];47 for (properties) |&property| {48 if (49 session.get_property(50 &"org.a11y.Bus",51 &"/org/a11y/bus",52 &"org.a11y.Status",53 &property54 ) is ::Some |&v|55 ) {56 if (v.as_integer() != 0) {57 enabled = true;58 }59 }60 }61 var address = String::empty();62 if (enabled) {63 if (64 session.call(65 &"org.a11y.Bus",66 &"/org/a11y/bus",67 &"org.a11y.Bus",68 &"GetAddress",69 &"",70 Vector<Value>::empty()71 ) is ::Some |&r|72 ) {73 if (!r.is_error() && r.body.length() > 0) {74 address = String::from(&r.body.get(0).as_text());75 }76 }77 }78 session.dispose();79 if (address.length() == 0) {80 return ::None;81 }82 var bus = if (Connection::open(&address.view()) is ::Some |move c|) move c else {83 return ::None;84 };85 var a = Atspi {86 .bus = move bus,87 .title = String::from(&title),88 .tree = AccessTree::empty(),89 .desktop = [String::from(&"org.a11y.atspi.Registry"), String::from(&ROOT_PATH)],90 .actions = Vector<ActionRequest>::empty(),91 .id = 092 };93 // Socket.Embed: the application's reference for the desktop's94 var body = Vector<Value>::empty();95 body.push(a.self_reference());96 if (97 a.bus.call(98 &"org.a11y.atspi.Registry",99 &ROOT_PATH,100 &"org.a11y.atspi.Socket",101 &"Embed",102 &"(so)",103 move body104 ) is ::Some |&r|105 ) {106 if (!r.is_error() && r.body.length() > 0) {107 const parent = &r.body.get(0).items();108 if (parent.length() == 2) {109 a.desktop = [110 String::from(&parent[0].as_text()),111 String::from(&parent[1].as_text())112 ];113 }114 }115 }116 return ::Some(move a);117}118119// Takes the window's language, a BCP 47 tag, as POSIX writes a locale:120// `ar_EG.UTF-8`.121pub fn set_language(self a: &var Atspi, tag: &[u8]) {122 var locale = String::empty();123 for (tag) |c| {124 locale.push(if (c == '-') '_' else c);125 }126 locale.append(&".UTF-8");127 a.locale = move locale;128}129130pub fn dispose(self a: &var Atspi) {131 a.bus.dispose();132}133134pub fn fd(self a: &Atspi) -> i32 => a.bus.fd();135136fn reference(self a: &Atspi, path: &[u8]) -> Value {137 var fields = Vector<Value>::empty();138 fields.push(Value::Text(copy a.bus.unique_name));139 fields.push(Value::path(&path));140 return Value::Struct(move fields);141}142143fn self_reference(self a: &Atspi) -> Value => a.reference(&ROOT_PATH);144145fn node_reference(self a: &Atspi, id: u64) -> Value => a.reference(&path_of(id).view());146147fn path_of(id: u64) -> String {148 var out = String::from(&NODE_PATH);149 out.append(&f"{id}".view());150 return move out;151}152153// What an object path names: the application, or a node of the tree.154type Object = enum {155 Application,156 Node: u64,157};158159// The object the path names, if any.160fn object_of(self a: &Atspi, path: &[u8]) -> Option<Object> {161 if (path == ROOT_PATH) {162 return ::Some(Object::Application);163 }164 if (path.length() <= NODE_PATH.length() || path[..NODE_PATH.length()] != NODE_PATH) {165 return ::None;166 }167 var id: u64 = 0;168 for (path[NODE_PATH.length()..]) |c| {169 if (c < '0' || c > '9') {170 return ::None;171 }172 id = id * 10 + (c - '0') to u64;173 }174 if (!a.tree.nodes.contains(&id)) {175 return ::None;176 }177 return ::Some(Object::Node(id));178}179180/* --- The tree and its signals --- */181182// Takes the program's update, announcing what changed. The window183// reaches it through `Requests.atspi`, which only sending a tree sets, so184// a program sending none links none of this nor Text's answers.185pub fn take_tree(a: &var Atspi, update: TreeUpdate) {186 a.texts = ::Some(text);187 const first = a.tree.root == 0;188 const focus = a.tree.focus;189 var announced = Vector<Message>::empty();190 for (update.nodes.view()) |&node| {191 if (a.tree.nodes.try_get(&node.id) is ::Some |&old|) {192 a.changes(&old, &node, &var announced);193 }194 }195 a.tree.apply(move update);196 if (first) {197 // the window under the application198 announced.push(a.event(199 &ROOT_PATH,200 &"ChildrenChanged",201 &"add",202 0,203 0,204 a.node_reference(a.tree.root)205 ));206 }207 if (a.tree.focus != focus) {208 if (focus is ::Some |before| && a.tree.nodes.contains(&before)) {209 announced.push(a.event(210 &path_of(before).view(),211 &"StateChanged",212 &"focused",213 0,214 0,215 Value::Int32(0)216 ));217 }218 if (a.tree.focus is ::Some |now| && now != a.tree.root) {219 announced.push(a.event(220 &path_of(now).view(),221 &"StateChanged",222 &"focused",223 1,224 0,225 Value::Int32(0)226 ));227 }228 }229 for (announced.items()) |&var m| {230 a.bus.send(move m);231 }232}233234fn changes(self a: &Atspi, old: &AccessNode, node: &AccessNode, out: &var Vector<Message>) {235 const path = path_of(node.id);236 const s = &node.semantics;237 if (old.semantics.label != s.label) {238 out.push(a.event(239 &path.view(),240 &"PropertyChange",241 &"accessible-name",242 0,243 0,244 Value::Text(copy s.label)245 ));246 }247 if (old.semantics.checked != s.checked) {248 out.push(a.event(249 &path.view(),250 &"StateChanged",251 &"checked",252 if (s.checked == ::Some(true)) 1 else 0,253 0,254 Value::Int32(0)255 ));256 }257 if (old.semantics.description != s.description) {258 out.push(a.event(259 &path.view(),260 &"PropertyChange",261 &"accessible-description",262 0,263 0,264 Value::Text(copy s.description)265 ));266 }267 if (old.semantics.expanded != s.expanded && s.expanded is ::Some |open|) {268 out.push(a.event(269 &path.view(),270 &"StateChanged",271 &"expanded",272 if (open) 1 else 0,273 0,274 Value::Int32(0)275 ));276 }277 if (old.semantics.busy != s.busy) {278 out.push(a.event(279 &path.view(),280 &"StateChanged",281 &"busy",282 if (s.busy) 1 else 0,283 0,284 Value::Int32(0)285 ));286 }287 if (old.semantics.invalid != s.invalid) {288 out.push(a.event(289 &path.view(),290 &"StateChanged",291 &"invalid-entry",292 if (s.invalid) 1 else 0,293 0,294 Value::Int32(0)295 ));296 }297 if (old.semantics.required != s.required) {298 out.push(a.event(299 &path.view(),300 &"StateChanged",301 &"required",302 if (s.required) 1 else 0,303 0,304 Value::Int32(0)305 ));306 }307 if (old.semantics.selected != s.selected) {308 out.push(a.event(309 &path.view(),310 &"StateChanged",311 &"selected",312 if (s.selected == ::Some(true)) 1 else 0,313 0,314 Value::Int32(0)315 ));316 }317 if (old.semantics.value != s.value) {318 out.push(a.event(319 &path.view(),320 &"PropertyChange",321 &"accessible-value",322 0,323 0,324 Value::Text(copy s.value)325 ));326 }327 if (offers_text(&node)) {328 a.text_changes(&path.view(), &old, &node, &var out);329 }330 // children added and removed, by their places331 if (old.children == node.children) {332 return;333 }334 const before = places(&old.children);335 const after = places(&node.children);336 for ([..node.children.length()]) |k| {337 const child = node.children.get(k);338 if (!before.contains(&child)) {339 out.push(a.event(340 &path.view(),341 &"ChildrenChanged",342 &"add",343 k to i32,344 0,345 a.node_reference(child)346 ));347 }348 }349 for ([..old.children.length()]) |k| {350 const child = old.children.get(k);351 if (!after.contains(&child)) {352 out.push(a.event(353 &path.view(),354 &"ChildrenChanged",355 &"remove",356 k to i32,357 0,358 a.node_reference(child)359 ));360 }361 }362}363364// What changed of a node's text, as Orca follows it: the text deleted365// and inserted between what the two have the same at their ends, the366// caret moved, the selection changed.367fn text_changes(368 self a: &Atspi,369 path: &[u8],370 old: &AccessNode,371 node: &AccessNode,372 out: &var Vector<Message>373) {374 const before = &text_of(&old);375 const after = &text_of(&node);376 if (before != after) {377 var same: u64 = 0;378 while (same < before.length() && same < after.length() && before[same] == after[same]) {379 same += 1;380 }381 while (same > 0 && ((same < before.length() && (before[same] & 0xC0) == 0x80)382 || (same < after.length() && (after[same] & 0xC0) == 0x80))) {383 same -= 1;384 }385 var tail: u64 = 0;386 while (tail < before.length() - same && tail < after.length() - same387 && before[before.length() - 1 - tail] == after[after.length() - 1 - tail]) {388 tail += 1;389 }390 while (tail > 0 && (before[before.length() - tail] & 0xC0) == 0x80) {391 tail -= 1;392 }393 const at = characters(&before[..same]) to i32;394 const gone = &before[same..before.length() - tail];395 const added = &after[same..after.length() - tail];396 if (gone.length() > 0) {397 out.push(a.event(398 &path,399 &"TextChanged",400 &"delete",401 at,402 characters(&gone) to i32,403 Value::text(&gone)404 ));405 }406 if (added.length() > 0) {407 out.push(a.event(408 &path,409 &"TextChanged",410 &"insert",411 at,412 characters(&added) to i32,413 Value::text(&added)414 ));415 }416 }417 if (418 old.semantics.selection != node.semantics.selection419 && node.semantics.selection is ::Some |s|420 ) {421 const caret = characters(&after[..min(s[1], after.length())]) to i32;422 if (423 old.semantics.selection is ::None424 || old.semantics.selection.unwrap_or([0, 0])[1] != s[1]425 ) {426 out.push(a.event(&path, &"TextCaretMoved", &"", caret, 0, Value::Int32(0)));427 }428 const was = selected(&old);429 const now = selected(&node);430 if (was != now && (was[0] < was[1] || now[0] < now[1])) {431 out.push(a.event(&path, &"TextSelectionChanged", &"", 0, 0, Value::Int32(0)));432 }433 }434}435436fn places(ids: &Vector<u64>) -> Map<u64, u64> {437 var out = Map<u64, u64>::empty();438 for ([..ids.length()]) |k| {439 out.insert(ids.get(k), k);440 }441 return move out;442}443444// An org.a11y.atspi.Event.Object signal from the object.445fn event(446 self a: &Atspi,447 path: &[u8],448 member: &[u8],449 detail: &[u8],450 one: i32,451 two: i32,452 data: Value453) -> Message {454 var body = Vector<Value>::empty();455 body.push(Value::text(&detail));456 body.push(Value::Int32(one));457 body.push(Value::Int32(two));458 body.push(Value::variant(move data));459 body.push(Value::Array(Vector<Value>::empty()));460 return Message::signal(&path, &"org.a11y.atspi.Event.Object", &member, &"siiva{sv}", move body);461}462463// Has screen readers say the text: an Announcement from the window,464// polite (1) or assertive (2), as at-spi2-core 2.46 gives them.465pub fn announce(self a: &var Atspi, text: &String, assertive: bool) {466 if (a.tree.root == 0) {467 return;468 }469 var m = a.event(470 &path_of(a.tree.root).view(),471 &"Announcement",472 &"",473 if (assertive) 2 else 1,474 0,475 Value::Text(copy text)476 );477 a.bus.send(move m);478}479480/* --- Answering --- */481482// Answers what came on the bus, giving what a screen reader asked of the483// program.484pub fn answer(self a: &var Atspi) -> Vector<ActionRequest> {485 while (a.bus.receive() is ::Some |&m|) {486 if (!(m.kind is ::Call)) {487 continue;488 }489 const object = if (a.object_of(&m.path.view()) is ::Some |o|) o else {490 a.bus.send(m.error(&"org.freedesktop.DBus.Error.UnknownObject", &"no such object"));491 continue;492 };493 if (a.answer_call(object, &m) is ::Some |move reply|) {494 a.bus.send(move reply);495 } else {496 a.bus.send(m.error(&"org.freedesktop.DBus.Error.UnknownMethod", &m.member.view()));497 }498 }499 var asked = move a.actions;500 a.actions = Vector<ActionRequest>::empty();501 return move asked;502}503504fn answer_call(self a: &var Atspi, object: Object, m: &Message) -> Option<Message> {505 const face = &m.interface_name.view();506 match (face) {507 "org.freedesktop.DBus.Properties" {508 return a.properties(object, &m);509 }510 "org.freedesktop.DBus.Introspectable" {511 return reply_with(&m, &"s", Value::text(&"<node/>"));512 }513 "org.a11y.atspi.Accessible" {514 return a.accessible(object, &m);515 }516 "org.a11y.atspi.Component" {517 return a.component(object, &m);518 }519 "org.a11y.atspi.Action" {520 return a.action(object, &m);521 }522 "org.a11y.atspi.Text", "org.a11y.atspi.EditableText" {523 if (a.texts is ::Some |text|) {524 return text(&var a, object, &m);525 }526 }527 "org.a11y.atspi.Table" {528 return a.table(object, &m);529 }530 "org.a11y.atspi.TableCell" {531 if (m.member.view() == "GetRowColumnSpan" && a.semantics(object) is ::Some |&s|) {532 const at: [u32 : 2] = if (s.cell is ::Some |c|) c else [0, 0];533 var body = Vector<Value>::empty();534 for ([at[0], at[1], 1, 1]) |n| {535 body.push(Value::Int32(n to i32));536 }537 return ::Some(m.reply(&"iiii", move body));538 }539 }540 "org.a11y.atspi.Application" {541 if (m.member.view() == "GetLocale") {542 return reply_with(&m, &"s", Value::text(&a.locale.view()));543 }544 }545 else {}546 }547 return ::None;548}549550// The reply to the call, carrying the value.551fn reply_with(call: &Message, signature: &[u8], value: Value) -> Option<Message> {552 var body = Vector<Value>::empty();553 body.push(move value);554 return ::Some(call.reply(&signature, move body));555}556557// org.freedesktop.DBus.Properties: Get, GetAll and Set.558fn properties(self a: &var Atspi, object: Object, m: &Message) -> Option<Message> {559 const member = &m.member.view();560 match (member) {561 "Get" {562 if (m.body.length() == 2) {563 const name = &m.body.get(1).as_text();564 if (a.property(object, &m.body.get(0).as_text(), &name) is ::Some |move v|) {565 return reply_with(&m, &"v", Value::variant(move v));566 }567 return ::Some(m.error(&"org.freedesktop.DBus.Error.UnknownProperty", &name));568 }569 }570 "GetAll" {571 if (m.body.length() == 1) {572 const face = &m.body.get(0).as_text();573 var all = Vector<Value>::empty();574 for (property_names(&face)) |&name| {575 if (a.property(object, &face, &name) is ::Some |move v|) {576 all.push(Value::entry(Value::text(&name), Value::variant(move v)));577 }578 }579 return reply_with(&m, &"a{sv}", Value::Array(move all));580 }581 }582 "Set" {583 if (m.body.length() == 3) {584 a.set_property(object, &m.body.get(1).as_text(), &m.body.get(2));585 return ::Some(m.reply(&"", Vector<Value>::empty()));586 }587 }588 else {}589 }590 return ::None;591}592593fn property_names(face: &[u8]) -> &[&[u8]] {594 return match (face) {595 "org.a11y.atspi.Accessible" &[596 &"Name",597 &"Description",598 &"Parent",599 &"ChildCount",600 &"Locale",601 &"AccessibleId"602 ];603 "org.a11y.atspi.Application" &[&"ToolkitName", &"Version", &"AtspiVersion", &"Id"];604 "org.a11y.atspi.Value" &[605 &"MinimumValue",606 &"MaximumValue",607 &"MinimumIncrement",608 &"CurrentValue",609 &"Text"610 ];611 "org.a11y.atspi.Action" &[&"NActions"];612 "org.a11y.atspi.Text" &[&"CharacterCount", &"CaretOffset"];613 "org.a11y.atspi.Table" &[&"NRows", &"NColumns", &"NSelectedRows", &"NSelectedColumns"];614 "org.a11y.atspi.TableCell" &[&"Position", &"RowSpan", &"ColumnSpan", &"Table"];615 else &[];616 };617}618619// The node's semantics; none for the application.620fn semantics(self a: &Atspi, object: Object) -> Option<&Semantics> {621 if (object is ::Node |id| && a.tree.nodes.try_get(&id) is ::Some |&node|) {622 return ::Some(&node.semantics);623 }624 return ::None;625}626627fn property(self a: &var Atspi, object: Object, face: &[u8], name: &[u8]) -> Option<Value> {628 match (face) {629 "org.a11y.atspi.Accessible" {630 return a.accessible_property(object, &name);631 }632 "org.a11y.atspi.Application" {633 if (object is ::Application) {634 return match (name) {635 "ToolkitName" ::Some(Value::text(&"Alloy"));636 "Version" ::Some(Value::text(&"0.1.0"));637 "AtspiVersion" ::Some(Value::text(&"2.1"));638 "Id" ::Some(Value::Int32(a.id));639 else ::None;640 };641 }642 }643 "org.a11y.atspi.Value" {644 if (a.semantics(object) is ::Some |&s| && s.range is ::Some |r|) {645 return match (name) {646 "MinimumValue" ::Some(Value::Double(r.minimum));647 "MaximumValue" ::Some(Value::Double(r.maximum));648 "MinimumIncrement" ::Some(Value::Double(r.step));649 "CurrentValue" ::Some(Value::Double(r.value));650 "Text" ::Some(Value::Text(copy s.value));651 else ::None;652 };653 }654 }655 "org.a11y.atspi.Table" {656 const size = a.grid_size(object);657 return match (name) {658 "NRows" ::Some(Value::Int32(size[0] to i32));659 "NColumns" ::Some(Value::Int32(size[1] to i32));660 "NSelectedRows", "NSelectedColumns" ::Some(Value::Int32(0));661 else ::None;662 };663 }664 "org.a11y.atspi.TableCell" {665 if (a.semantics(object) is ::Some |&s| && s.cell is ::Some |c|) {666 return match (name) {667 "Position" ::Some(Value::Struct(Vector<Value>::from(&[668 Value::Int32(c[0] to i32),669 Value::Int32(c[1] to i32)670 ])));671 "RowSpan", "ColumnSpan" ::Some(Value::Int32(1));672 "Table" ::Some(a.parent_reference(object));673 else ::None;674 };675 }676 }677 "org.a11y.atspi.Action" {678 if (name == "NActions" && a.semantics(object) is ::Some |&s|) {679 return ::Some(Value::Int32(if (has_action(&s)) 1 else 0));680 }681 }682 "org.a11y.atspi.Text" {683 if (object is ::Node |id| && a.tree.nodes.try_get(&id) is ::Some |&node|) {684 const shown = &text_of(&node);685 if (name == "CharacterCount") {686 return ::Some(Value::Int32(characters(&shown) to i32));687 }688 if (name == "CaretOffset") {689 var caret: u64 = 0;690 if (node.semantics.selection is ::Some |range|) {691 caret = range[1];692 }693 const end = if (caret > shown.length()) shown.length() else caret;694 return ::Some(Value::Int32(characters(&shown[..end]) to i32));695 }696 }697 }698 else {}699 }700 return ::None;701}702703fn accessible_property(self a: &Atspi, object: Object, name: &[u8]) -> Option<Value> {704 match (name) {705 "Name" {706 if (object is ::Application || object == Object::Node(a.tree.root)) {707 return ::Some(Value::Text(copy a.title));708 }709 if (a.semantics(object) is ::Some |&s|) {710 return ::Some(Value::Text(copy s.label));711 }712 }713 "Description" {714 if (a.semantics(object) is ::Some |&s|) {715 return ::Some(Value::Text(copy s.description));716 }717 return ::Some(Value::text(&""));718 }719 "AccessibleId" {720 return ::Some(Value::text(&""));721 }722 "Locale" {723 return ::Some(Value::text(&a.locale.view()));724 }725 "Parent" {726 return ::Some(a.parent_reference(object));727 }728 "ChildCount" {729 return ::Some(Value::Int32(a.children_of(object).length() to i32));730 }731 else {}732 }733 return ::None;734}735736fn set_property(self a: &var Atspi, object: Object, name: &[u8], value: &Value) {737 if (name == "Id") {738 a.id = value.as_integer() to i32;739 } else if (name == "CurrentValue") {740 var number: f64 = 0.0;741 if (value is ::Variant |&inner|) {742 if (inner is ::Double |d|) {743 number = d;744 }745 } else if (value is ::Double |d|) {746 number = d;747 }748 a.ask(object, Action::SetValue(f"{number}"));749 }750}751752fn ask(self a: &var Atspi, object: Object, action: Action) {753 if (object is ::Node |id|) {754 a.actions.push(ActionRequest { .node = id, .action = move action });755 }756}757758fn has_action(s: &Semantics) -> bool {759 return match (s.role) {760 ::Button,761 ::CheckBox,762 ::MenuItem,763 ::MenuItemCheckbox,764 ::ComboBox,765 ::ListOption,766 ::RadioButton,767 ::Tab,768 ::GridCell true;769 else false;770 };771}772773fn children_of(self a: &Atspi, object: Object) -> Vector<u64> {774 var out = Vector<u64>::empty();775 match (object) {776 ::Application {777 if (a.tree.root != 0) {778 out.push(a.tree.root);779 }780 }781 ::Node |id| {782 if (a.tree.nodes.try_get(&id) is ::Some |&node|) {783 out = copy node.children;784 }785 }786 }787 return move out;788}789790fn parent_reference(self a: &Atspi, object: Object) -> Value {791 const id = if (object is ::Node |id|) id else {792 var fields = Vector<Value>::empty();793 fields.push(Value::Text(copy a.desktop[0]));794 fields.push(Value::ObjectPath(copy a.desktop[1]));795 return Value::Struct(move fields);796 };797 if (id != a.tree.root && a.tree.try_parent_of(id) is ::Some |parent|) {798 return a.node_reference(parent);799 }800 return a.self_reference();801}802803// The roles AT-SPI reads the objects as, by their numbers in804// atspi-constants.h.805type AtspiRole = enum : u32 {806 Application = 75,807 Frame = 23,808 Panel = 39,809 Label = 29,810 PushButton = 43,811 CheckBox = 7,812 Slider = 51,813 Text = 61,814 Entry = 79,815 PasswordText = 40,816 Image = 27,817 List = 31,818 ListItem = 32,819 ScrollPane = 49,820 Dialog = 16,821 Alert = 2,822 Menu = 33,823 MenuItem = 35,824 CheckMenuItem = 8,825 ToolTip = 64,826 ComboBox = 11,827 ListBox = 98,828 RadioButton = 44,829 PageTab = 37,830 PageTabList = 38,831 Calendar = 5,832 TableCell = 56,833 ProgressBar = 42,834 Unknown = 67,835};836837fn name(self role: &AtspiRole) -> &[u8] {838 return match (role) {839 ::Application &"application";840 ::Frame &"frame";841 ::Label &"label";842 ::PushButton &"push button";843 ::CheckBox &"check box";844 ::Slider &"slider";845 ::Text &"text";846 ::Entry &"entry";847 ::PasswordText &"password text";848 ::Image &"image";849 ::List &"list";850 ::ListItem &"list item";851 ::ScrollPane &"scroll pane";852 ::Dialog &"dialog";853 ::Alert &"alert";854 ::Menu &"menu";855 ::MenuItem &"menu item";856 ::CheckMenuItem &"check menu item";857 ::ToolTip &"tool tip";858 ::ComboBox &"combo box";859 ::ListBox &"list box";860 ::RadioButton &"radio button";861 ::PageTab &"page tab";862 ::PageTabList &"page tab list";863 ::Calendar &"calendar";864 ::TableCell &"table cell";865 ::ProgressBar &"progress bar";866 ::Panel, ::Unknown &"panel";867 };868}869870fn role_of(self a: &Atspi, object: Object) -> AtspiRole {871 if (object is ::Application) {872 return AtspiRole::Application;873 }874 const s = if (a.semantics(object) is ::Some |&s|) &s else {875 return AtspiRole::Unknown;876 };877 return match (s.role) {878 ::Window AtspiRole::Frame;879 ::Group, ::RadioGroup, ::Unknown AtspiRole::Panel;880 ::Label AtspiRole::Label;881 ::Button AtspiRole::PushButton;882 ::CheckBox AtspiRole::CheckBox;883 ::Slider AtspiRole::Slider;884 ::TextInput if (s.secret) AtspiRole::PasswordText else if (885 s.multiline886 ) AtspiRole::Text else AtspiRole::Entry;887 ::Image AtspiRole::Image;888 ::List AtspiRole::List;889 ::ListItem, ::ListOption AtspiRole::ListItem;890 ::ScrollView AtspiRole::ScrollPane;891 ::Dialog AtspiRole::Dialog;892 ::Alert AtspiRole::Alert;893 ::Menu AtspiRole::Menu;894 ::MenuItem AtspiRole::MenuItem;895 ::MenuItemCheckbox AtspiRole::CheckMenuItem;896 ::Tooltip AtspiRole::ToolTip;897 ::ComboBox AtspiRole::ComboBox;898 ::ListBox AtspiRole::ListBox;899 ::RadioButton AtspiRole::RadioButton;900 ::TabList AtspiRole::PageTabList;901 ::Tab AtspiRole::PageTab;902 ::Grid AtspiRole::Calendar;903 ::GridCell AtspiRole::TableCell;904 ::ProgressBar AtspiRole::ProgressBar;905 };906}907908// The AtspiStateType bits the objects set.909type State = enum : u64 {910 Active = 1,911 Busy = 3,912 Checked = 4,913 Collapsed = 5,914 Editable = 7,915 Enabled = 8,916 Expandable = 9,917 Expanded = 10,918 Focusable = 11,919 Focused = 12,920 Horizontal = 14,921 Modal = 16,922 MultiLine = 17,923 Selectable = 22,924 Selected = 23,925 Sensitive = 24,926 Showing = 25,927 SingleLine = 26,928 Visible = 30,929 Required = 33,930 InvalidEntry = 36,931 Checkable = 41,932 HasPopup = 42,933};934935// The two words of AtspiStateType bits.936fn states(self a: &Atspi, object: Object) -> [u32 : 2] {937 const id = if (object is ::Node |id|) id else {938 return [0, 0];939 };940 const s = if (a.semantics(object) is ::Some |&s|) &s else {941 return [0, 0];942 };943 var bits: u64 = 0;944 const set = |&var bits| (state: State) => bits |= 1 << (state to u64);945 set(::Showing);946 set(::Visible);947 if (!s.disabled) {948 set(::Enabled);949 set(::Sensitive);950 }951 if (s.focusable) {952 set(::Focusable);953 }954 if (a.tree.focus == ::Some(id)) {955 set(::Focused);956 }957 if (id == a.tree.root) {958 set(::Active);959 }960 if (s.checked is ::Some |c|) {961 set(::Checkable);962 if (c) {963 set(::Checked);964 }965 }966 if (s.role is ::TextInput) {967 set(::Editable);968 set(if (s.multiline) State::MultiLine else State::SingleLine);969 }970 if (s.role is ::Slider || s.role is ::ProgressBar) {971 set(::Horizontal);972 }973 if (s.busy) {974 set(::Busy);975 }976 if (s.invalid) {977 set(::InvalidEntry);978 }979 if (s.required) {980 set(::Required);981 }982 if (s.modal) {983 set(::Modal);984 }985 if (s.expanded is ::Some |open|) {986 set(::Expandable);987 set(::HasPopup);988 set(if (open) State::Expanded else State::Collapsed);989 }990 if (s.selected is ::Some |chosen|) {991 set(::Selectable);992 if (chosen) {993 set(::Selected);994 }995 }996 return [(bits & 0xFFFFFFFF) to u32, (bits >> 32) to u32];997}998999fn interfaces(self a: &Atspi, object: Object) -> Vector<Value> {1000 var out = Vector<Value>::empty();1001 out.push(Value::text(&"org.a11y.atspi.Accessible"));1002 if (object is ::Application) {1003 out.push(Value::text(&"org.a11y.atspi.Application"));1004 return move out;1005 }1006 out.push(Value::text(&"org.a11y.atspi.Component"));1007 if (object is ::Node |id| && a.tree.nodes.try_get(&id) is ::Some |&node|) {1008 const s = &node.semantics;1009 if (has_action(&s)) {1010 out.push(Value::text(&"org.a11y.atspi.Action"));1011 }1012 if (s.range is ::Some) {1013 out.push(Value::text(&"org.a11y.atspi.Value"));1014 }1015 // a field's text, a text's of several lines, the chosen item's1016 // label, as a combo box shows it, and a button's value: a date1017 // picker's date1018 if (offers_text(&node)) {1019 out.push(Value::text(&"org.a11y.atspi.Text"));1020 }1021 if (s.role is ::TextInput) {1022 out.push(Value::text(&"org.a11y.atspi.EditableText"));1023 }1024 if (s.role is ::Grid) {1025 out.push(Value::text(&"org.a11y.atspi.Table"));1026 }1027 if (s.cell is ::Some) {1028 out.push(Value::text(&"org.a11y.atspi.TableCell"));1029 }1030 }1031 return move out;1032}10331034fn accessible(self a: &var Atspi, object: Object, m: &Message) -> Option<Message> {1035 const member = &m.member.view();1036 match (member) {1037 "GetChildAtIndex" {1038 const index = if (m.body.length() > 0) m.body.get(0).as_integer() else 0;1039 const children = a.children_of(object);1040 if (index < 0 || index as u64 >= children.length()) {1041 return ::Some(m.error(&"org.freedesktop.DBus.Error.InvalidArgs", &"no such child"));1042 }1043 return reply_with(&m, &"(so)", a.node_reference(children.get(index as u64)));1044 }1045 "GetChildren" {1046 var list = Vector<Value>::empty();1047 for (a.children_of(object).view()) |child| {1048 list.push(a.node_reference(child));1049 }1050 return reply_with(&m, &"a(so)", Value::Array(move list));1051 }1052 "GetIndexInParent" {1053 return reply_with(&m, &"i", Value::Int32(a.index_in_parent(object)));1054 }1055 "GetRelationSet" {1056 return reply_with(&m, &"a(ua(so))", Value::Array(Vector<Value>::empty()));1057 }1058 "GetRole" {1059 return reply_with(&m, &"u", Value::Uint32(a.role_of(object) to u32));1060 }1061 "GetRoleName", "GetLocalizedRoleName" {1062 return reply_with(&m, &"s", Value::text(&a.role_of(object).name()));1063 }1064 "GetState" {1065 const bits = a.states(object);1066 var words = Vector<Value>::empty();1067 words.push(Value::Uint32(bits[0]));1068 words.push(Value::Uint32(bits[1]));1069 return reply_with(&m, &"au", Value::Array(move words));1070 }1071 "GetAttributes" {1072 var attributes = Vector<Value>::empty();1073 attributes.push(Value::entry(Value::text(&"toolkit"), Value::text(&"Alloy")));1074 // where an option or a radio is among its group's, as browsers1075 // give them1076 if (a.semantics(object) is ::Some |&s| && s.position is ::Some |p|) {1077 attributes.push(Value::entry(Value::text(&"posinset"), Value::Text(f"{p[0]}")));1078 attributes.push(Value::entry(Value::text(&"setsize"), Value::Text(f"{p[1]}")));1079 }1080 // today among a calendar's days, as browsers give aria-current1081 if (a.semantics(object) is ::Some |&s| && s.current) {1082 const kind: &[u8] = if (s.role is ::GridCell) &"date" else &"true";1083 attributes.push(Value::entry(Value::text(&"current"), Value::text(&kind)));1084 }1085 return reply_with(&m, &"a{ss}", Value::Array(move attributes));1086 }1087 "GetApplication" {1088 return reply_with(&m, &"(so)", a.self_reference());1089 }1090 "GetInterfaces" {1091 return reply_with(&m, &"as", Value::Array(a.interfaces(object)));1092 }1093 else {}1094 }1095 return ::None;1096}10971098// Where a node is among its parent's children; -1 for the application.1099fn index_in_parent(self a: &Atspi, object: Object) -> i32 {1100 const id = if (object is ::Node |id|) id else {1101 return -1;1102 };1103 if (id == a.tree.root) {1104 return 0;1105 }1106 if (a.tree.try_parent_of(id) is ::Some |parent|) {1107 const siblings = a.children_of(Object::Node(parent));1108 for ([..siblings.length()]) |k| {1109 if (siblings.get(k) == id) {1110 return k to i32;1111 }1112 }1113 }1114 return -1;1115}11161117// A node's bounds in the window, in pixels: x, y, width, height; the1118// application's are the window's.1119fn extents(self a: &Atspi, object: Object) -> [i32 : 4] {1120 const id = if (object is ::Node |id|) id else a.tree.root;1121 if (a.tree.nodes.try_get(&id) is ::Some |&node|) {1122 const b = node.bounds;1123 return [b.x to i32, b.y to i32, b.width to i32, b.height to i32];1124 }1125 return [0, 0, 0, 0];1126}11271128fn component(self a: &var Atspi, object: Object, m: &Message) -> Option<Message> {1129 const member = &m.member.view();1130 const e = a.extents(object);1131 match (member) {1132 "GetExtents" {1133 var fields = Vector<Value>::empty();1134 for (e) |v| {1135 fields.push(Value::Int32(v));1136 }1137 return reply_with(&m, &"(iiii)", Value::Struct(move fields));1138 }1139 "GetPosition", "GetSize" {1140 const at: u64 = if (member == "GetPosition") 0 else 2;1141 var body = Vector<Value>::empty();1142 body.push(Value::Int32(e[at]));1143 body.push(Value::Int32(e[at + 1]));1144 return ::Some(m.reply(&"ii", move body));1145 }1146 "Contains" {1147 if (m.body.length() >= 2) {1148 const x = m.body.get(0).as_integer() to i32;1149 const y = m.body.get(1).as_integer() to i32;1150 const inside = x >= e[0] && y >= e[1] && x < e[0] + e[2] && y < e[1] + e[3];1151 return reply_with(&m, &"b", Value::Boolean(inside));1152 }1153 }1154 "GetAccessibleAtPoint" {1155 if (m.body.length() >= 2) {1156 const at = Point {1157 .x = m.body.get(0).as_integer() to f32,1158 .y = m.body.get(1).as_integer() to f321159 };1160 const from = if (object is ::Node |id|) id else a.tree.root;1161 if (a.tree.node_at(from, at) is ::Some |found|) {1162 return reply_with(&m, &"(so)", a.node_reference(found));1163 }1164 return reply_with(&m, &"(so)", a.reference(&"/org/a11y/atspi/null"));1165 }1166 }1167 "GetLayer" {1168 // WINDOW, WIDGET1169 const layer: u32 = if (object == Object::Node(a.tree.root)) 7 else 3;1170 return reply_with(&m, &"u", Value::Uint32(layer));1171 }1172 "GetMDIZOrder" {1173 return reply_with(&m, &"n", Value::Int16(0));1174 }1175 "GetAlpha" {1176 return reply_with(&m, &"d", Value::Double(1.0));1177 }1178 "GrabFocus" {1179 a.ask(object, Action::Focus);1180 return reply_with(&m, &"b", Value::Boolean(true));1181 }1182 "ScrollTo", "ScrollToPoint" {1183 a.ask(object, Action::ScrollIntoView);1184 return reply_with(&m, &"b", Value::Boolean(true));1185 }1186 else {}1187 }1188 return ::None;1189}11901191fn action(self a: &var Atspi, object: Object, m: &Message) -> Option<Message> {1192 const member = &m.member.view();1193 const name: &[u8] = if (1194 a.semantics(object) is ::Some |&s|1195 && s.role is ::CheckBox1196 ) &"toggle" else &"click";1197 match (member) {1198 "GetActions" {1199 var fields = Vector<Value>::empty();1200 fields.push(Value::text(&name));1201 fields.push(Value::text(&""));1202 fields.push(Value::text(&""));1203 var list = Vector<Value>::empty();1204 list.push(Value::Struct(move fields));1205 return reply_with(&m, &"a(sss)", Value::Array(move list));1206 }1207 "GetName", "GetLocalizedName" {1208 return reply_with(&m, &"s", Value::text(&name));1209 }1210 "GetDescription", "GetKeyBinding" {1211 return reply_with(&m, &"s", Value::text(&""));1212 }1213 "DoAction" {1214 a.ask(object, Action::Click);1215 return reply_with(&m, &"b", Value::Boolean(true));1216 }1217 else {}1218 }1219 return ::None;1220}12211222// org.a11y.atspi.Text and EditableText: a field's text, a text's of1223// several lines, a combo box's or a button's value, read by characters1224// (graphemes, counted as code points), words, lines and paragraphs1225// (standing for sentences too) from its map (text_ranges.alloy), with1226// their extents in the window, the selection and one run of attributes.1227fn text(a: &var Atspi, object: Object, m: &Message) -> Option<Message> {1228 const id = if (object is ::Node |id|) id else {1229 return ::None;1230 };1231 const node = if (a.tree.nodes.try_get(&id) is ::Some |&n|) &n else {1232 return ::None;1233 };1234 const text = &text_of(&node);1235 var made = Option<TextMap>::None;1236 if (node.text is ::None) {1237 made = ::Some(TextMap::plain(&text, &node.bounds));1238 }1239 const map = if (1240 node.text is ::Some |&kept|1241 ) &kept else if (made is ::Some |&plain|) &plain else {1242 return ::None;1243 };1244 return a.text_reply(object, &m, &node, &text, &map);1245}12461247fn text_reply(1248 self a: &var Atspi,1249 object: Object,1250 m: &Message,1251 node: &AccessNode,1252 text: &[u8],1253 map: &TextMap1254) -> Option<Message> {1255 const member = &m.member.view();1256 const first = if (m.body.length() > 0) m.body.get(0).as_integer() else 0;1257 const second = if (m.body.length() > 1) m.body.get(1).as_integer() else 0;1258 const at = byte_at(&text, first);1259 match (member) {1260 "GetText" {1261 const end = if (second < 0) text.length() else byte_at(&text, second);1262 return reply_with(&m, &"s", Value::text(&text[at..max(at, end)]));1263 }1264 "GetStringAtOffset", "GetTextAtOffset", "GetTextBeforeOffset", "GetTextAfterOffset" {1265 const unit = if (1266 member == "GetStringAtOffset"1267 ) granularity(second) else boundary(second);1268 var range = map.unit_at(&text, at to u32, unit);1269 if (member == "GetTextBeforeOffset") {1270 range = if (range[0] == 0) [0, 0] else map.unit_at(&text, range[0] - 1, unit);1271 } else if (member == "GetTextAfterOffset") {1272 range = if (range[1] >= map.end()) [1273 map.end(),1274 map.end()1275 ] else map.unit_at(&text, range[1], unit);1276 }1277 return ::Some(m.reply(&"sii", a_range(&text, range)));1278 }1279 "GetCharacterAtOffset" {1280 const c = if (at < text.length()) code_at(&text, at) to i32 else 0;1281 return reply_with(&m, &"i", Value::Int32(c));1282 }1283 "GetCharacterExtents" {1284 const g = map.unit_at(&text, at to u32, TextUnit::Character);1285 return ::Some(m.reply(&"iiii", extents_of(&node, &map.boxes(g[0], g[1]))));1286 }1287 "GetRangeExtents" {1288 const end = byte_at(&text, second) to u32;1289 return ::Some(1290 m.reply(1291 &"iiii",1292 extents_of(&node, &map.boxes(min(at to u32, end), max(at to u32, end)))1293 )1294 );1295 }1296 "GetOffsetAtPoint" {1297 const x = first to f32 - node.bounds.x;1298 const y = second to f32 - node.bounds.y;1299 var offset: i32 = -1;1300 if (map.grapheme_at(x, y) is ::Some |g|) {1301 offset = characters(&text[..g to u64]) to i32;1302 }1303 return reply_with(&m, &"i", Value::Int32(offset));1304 }1305 "GetNSelections" {1306 const s = selected(&node);1307 return reply_with(&m, &"i", Value::Int32(if (s[0] < s[1]) 1 else 0));1308 }1309 "GetSelection" {1310 const s = selected(&node);1311 var body = Vector<Value>::empty();1312 body.push(Value::Int32(characters(&text[..min(s[0], text.length())]) to i32));1313 body.push(Value::Int32(characters(&text[..min(s[1], text.length())]) to i32));1314 return ::Some(m.reply(&"ii", move body));1315 }1316 "SetSelection", "AddSelection" {1317 // the one selection: SetSelection's number, then its offsets1318 const from = if (member == "SetSelection") second else first;1319 const last = if (1320 m.body.length() > 2 && member == "SetSelection"1321 ) m.body.get(2).as_integer() else second;1322 a.ask(object, Action::SetSelection([byte_at(&text, from), byte_at(&text, last)]));1323 return reply_with(&m, &"b", Value::Boolean(true));1324 }1325 "RemoveSelection" {1326 const caret = if (node.semantics.selection is ::Some |s|) s[1] else 0;1327 a.ask(object, Action::SetSelection([caret, caret]));1328 return reply_with(&m, &"b", Value::Boolean(true));1329 }1330 "GetAttributes", "GetAttributeRun" {1331 const line = &map.lines.get(map.line_of(at to u32));1332 var body = Vector<Value>::empty();1333 body.push(Value::Array(attributes(&node, &map, line.right_to_left)));1334 body.push(Value::Int32(characters(&text[..line.start to u64]) to i32));1335 body.push(Value::Int32(characters(&text[..line.end to u64]) to i32));1336 return ::Some(m.reply(&"a{ss}ii", move body));1337 }1338 "GetDefaultAttributes", "GetDefaultAttributeSet" {1339 const rtl = map.lines.length() > 0 && map.lines.get(0).right_to_left;1340 return reply_with(&m, &"a{ss}", Value::Array(attributes(&node, &map, rtl)));1341 }1342 "GetAttributeValue" {1343 const name = if (m.body.length() > 1) &m.body.get(1).as_text() else &"";1344 const line = &map.lines.get(map.line_of(at to u32));1345 var found = String::empty();1346 for (attributes(&node, &map, line.right_to_left).view()) |&entry| {1347 if (entry.items()[0].as_text() == name) {1348 found = String::from(&entry.items()[1].as_text());1349 }1350 }1351 return reply_with(&m, &"s", Value::Text(move found));1352 }1353 "GetBoundedRanges" {1354 return reply_with(&m, &"a(iisv)", Value::Array(Vector<Value>::empty()));1355 }1356 "ScrollSubstringTo", "ScrollSubstringToPoint" {1357 a.ask(object, Action::ScrollIntoView);1358 return reply_with(&m, &"b", Value::Boolean(true));1359 }1360 "SetTextContents" {1361 if (m.body.length() == 1) {1362 a.ask(object, Action::SetValue(String::from(&m.body.get(0).as_text())));1363 return reply_with(&m, &"b", Value::Boolean(true));1364 }1365 }1366 "SetCaretOffset" {1367 a.ask(object, Action::SetSelection([at, at]));1368 return reply_with(&m, &"b", Value::Boolean(true));1369 }1370 else {}1371 }1372 return ::None;1373}13741375// The text a node offers through Text: a field's, a combo box's or a1376// button's value, else its label (a text of several lines).1377fn text_of(node: &AccessNode) -> &[u8] {1378 const s = &node.semantics;1379 const valued = s.role is ::TextInput || s.role is ::ComboBox || s.role is ::Button;1380 return if (valued) &s.value.view() else &s.label.view();1381}13821383// Whether the node offers Text: a field, a combo box, a button with a1384// value, or a text of several lines (with a map).1385fn offers_text(node: &AccessNode) -> bool {1386 const s = &node.semantics;1387 return s.role is ::TextInput || s.role is ::ComboBox1388 || (s.role is ::Button && s.value.length() > 0) || node.text is ::Some;1389}13901391// The byte where the character (code point) `index` starts, within the1392// text.1393fn byte_at(text: &[u8], index: i64) -> u64 => if (index <= 0) 0 else byte_of(&text, index);13941395// A field's selection as start and end in bytes; nothing for others.1396fn selected(node: &AccessNode) -> [u64 : 2] {1397 if (node.semantics.selection is ::Some |s|) {1398 return [min(s[0], s[1]), max(s[0], s[1])];1399 }1400 return [0, 0];1401}14021403// GetStringAtOffset's granularities: char, word, sentence (as a1404// paragraph), line, paragraph.1405fn granularity(n: i64) -> TextUnit => match (n) {1406 1 TextUnit::Word;1407 2, 4 TextUnit::Paragraph;1408 3 TextUnit::Line;1409 else TextUnit::Character;1410};14111412// GetTextAtOffset's boundaries: char, word start and end, sentence start1413// and end (as paragraphs), line start and end.1414fn boundary(n: i64) -> TextUnit => match (n) {1415 1, 2 TextUnit::Word;1416 3, 4 TextUnit::Paragraph;1417 5, 6 TextUnit::Line;1418 else TextUnit::Character;1419};14201421// A range's text and its offsets in characters.1422fn a_range(text: &[u8], range: [u32 : 2]) -> Vector<Value> {1423 const start = range[0] to u64;1424 const end = range[1] to u64;1425 var body = Vector<Value>::empty();1426 body.push(Value::text(&text[start..end]));1427 body.push(Value::Int32(characters(&text[..start]) to i32));1428 body.push(Value::Int32(characters(&text[..end]) to i32));1429 return move body;1430}14311432// The boxes' union in the window: x, y, width, height.1433fn extents_of(node: &AccessNode, boxes: &Vector<Area>) -> Vector<Value> {1434 var out = Vector<Value>::empty();1435 if (boxes.length() == 0) {1436 for ([..4 to u64]) {1437 out.push(Value::Int32(0));1438 }1439 return move out;1440 }1441 var left: f32 = 1000000000.0;1442 var top: f32 = 1000000000.0;1443 var right: f32 = -1000000000.0;1444 var bottom: f32 = -1000000000.0;1445 for (boxes.view()) |&b| {1446 left = min(left, b.x);1447 top = min(top, b.y);1448 right = max(right, b.x + b.width);1449 bottom = max(bottom, b.y + b.height);1450 }1451 const x = (node.bounds.x + left) to i32;1452 const y = (node.bounds.y + top) to i32;1453 out.push(Value::Int32(x));1454 out.push(Value::Int32(y));1455 out.push(Value::Int32((node.bounds.x + right) to i32 - x));1456 out.push(Value::Int32((node.bounds.y + bottom) to i32 - y));1457 return move out;1458}14591460// The text's one run of attributes, as ATK names them, the line's1461// direction among them.1462fn attributes(node: &AccessNode, map: &TextMap, rtl: bool) -> Vector<Value> {1463 const f = &map.format;1464 var out = Vector<Value>::empty();1465 const put = |&var out| (name: &[u8], value: String) => out.push(Value::entry(1466 Value::text(&name),1467 Value::Text(move value)1468 ));1469 if (f.family.length() > 0) {1470 put(&"family-name", copy f.family);1471 // points, at 96 pixels an inch1472 put(&"size", f"{(f.size * 0.75 + 0.5) to u32}");1473 put(&"weight", f"{f.weight}");1474 put(1475 &"fg-color",1476 f"{f.color[0] to u32 * 257},{f.color[1] to u32 * 257},{f.color[2] to u32 * 257}"1477 );1478 }1479 if (f.language.length() > 0) {1480 put(&"language", copy f.language);1481 }1482 put(&"direction", String::from(if (rtl) &"rtl" else &"ltr"));1483 const editable = node.semantics.role is ::TextInput && !f.read_only && !node.semantics.disabled;1484 put(&"editable", String::from(if (editable) &"true" else &"false"));1485 return move out;1486}14871488/* --- Tables --- */14891490// A grid's rows and columns, from its cells'.1491fn grid_size(self a: &Atspi, object: Object) -> [u32 : 2] {1492 var size: [u32 : 2] = [0, 0];1493 for (a.children_of(object).view()) |child| {1494 if (a.tree.nodes.try_get(&child) is ::Some |&c| && c.semantics.cell is ::Some |at|) {1495 size = [max(size[0], at[0] + 1), max(size[1], at[1] + 1)];1496 }1497 }1498 return size;1499}15001501// The grid's cell at the row and column, and where it is among the1502// grid's children.1503fn cell_at(self a: &Atspi, object: Object, row: i64, column: i64) -> Option<[u64 : 2]> {1504 const children = a.children_of(object);1505 for (children.view(), [..children.length()]) |child, k| {1506 if (a.tree.nodes.try_get(&child) is ::Some |&c| && c.semantics.cell is ::Some |at|) {1507 if (at[0] to i64 == row && at[1] to i64 == column) {1508 return ::Some([child, k]);1509 }1510 }1511 }1512 return ::None;1513}15141515// org.a11y.atspi.Table: a calendar's days by row and column.1516fn table(self a: &var Atspi, object: Object, m: &Message) -> Option<Message> {1517 const member = &m.member.view();1518 const first = if (m.body.length() > 0) m.body.get(0).as_integer() else -1;1519 const second = if (m.body.length() > 1) m.body.get(1).as_integer() else -1;1520 match (member) {1521 "GetAccessibleAt" {1522 if (a.cell_at(object, first, second) is ::Some |found|) {1523 return reply_with(&m, &"(so)", a.node_reference(found[0]));1524 }1525 return reply_with(&m, &"(so)", a.reference(&"/org/a11y/atspi/null"));1526 }1527 "GetIndexAt" {1528 const index = if (1529 a.cell_at(object, first, second) is ::Some |found|1530 ) found[1] to i32 else -1;1531 return reply_with(&m, &"i", Value::Int32(index));1532 }1533 "GetRowAtIndex", "GetColumnAtIndex" {1534 const children = a.children_of(object);1535 var at: i32 = -1;1536 if (first >= 0 && (first as u64) < children.length()) {1537 if (1538 a.tree.nodes.try_get(&children.get(first as u64)) is ::Some |&c|1539 && c.semantics.cell is ::Some |cell|1540 ) {1541 at = cell[if (member == "GetRowAtIndex") 0 else 1] to i32;1542 }1543 }1544 return reply_with(&m, &"i", Value::Int32(at));1545 }1546 "IsSelected" {1547 var selected = false;1548 if (1549 a.cell_at(object, first, second) is ::Some |found|1550 && a.tree.nodes.try_get(&found[0]) is ::Some |&c|1551 ) {1552 selected = c.semantics.selected == ::Some(true);1553 }1554 return reply_with(&m, &"b", Value::Boolean(selected));1555 }1556 "GetRowExtentAt", "GetColumnExtentAt" {1557 return reply_with(&m, &"i", Value::Int32(1));1558 }1559 "GetRowDescription", "GetColumnDescription" {1560 return reply_with(&m, &"s", Value::text(&""));1561 }1562 else {}1563 }1564 return ::None;1565}