crabshell-services 0.8.1

Crabshell's services: workspaces, tray, audio, clock, power, Bluetooth, network, media players, notifications, apps, programs, files, PAM, greetd and polkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! The answers to AT-SPI calls on the application and its windows'
//! widgets, from at-spi2-core's interfaces: `Accessible`, `Application`,
//! `Component`, `Action`, `Text`, `Value` and `Properties`.

use std::collections::HashMap;

use crabshell_ui::{AccessId, AccessNode, AccessRole, AccessText, Accessibility};
use zbus::message::{Flags, Header};
use zbus::{Connection, Message, ObjectPath, Variant};

use super::{App, PREFIX, ROOT};
use crate::dbus::PROPERTIES;

const ACCESSIBLE: &str = "org.a11y.atspi.Accessible";
const APPLICATION: &str = "org.a11y.atspi.Application";
const COMPONENT: &str = "org.a11y.atspi.Component";
const ACTION: &str = "org.a11y.atspi.Action";
const TEXT: &str = "org.a11y.atspi.Text";
const VALUE: &str = "org.a11y.atspi.Value";
const CACHE: &str = "org.a11y.atspi.Cache";
const CACHE_PATH: &str = "/org/a11y/atspi/cache";
const NULL: &str = "/org/a11y/atspi/null";
const INVALID_ARGS: &str = "org.freedesktop.DBus.Error.InvalidArgs";
const ACCESS_DENIED: &str = "org.freedesktop.DBus.Error.AccessDenied";
const UNKNOWN_METHOD: &str = "org.freedesktop.DBus.Error.UnknownMethod";
const UNKNOWN_OBJECT: &str = "org.freedesktop.DBus.Error.UnknownObject";

// A reply's tuple is its list of arguments, so one struct argument, as
// `(so)`, is sent as a tuple of one.

/// AT-SPI's states, by bit.
const ACTIVE: u32 = 1;
const EDITABLE: u32 = 7;
const ENABLED: u32 = 8;
const FOCUSABLE: u32 = 11;
const FOCUSED: u32 = 12;
const MULTI_LINE: u32 = 17;
const SENSITIVE: u32 = 24;
const SHOWING: u32 = 25;
const SINGLE_LINE: u32 = 26;
const VISIBLE: u32 = 30;
/// `Component`'s coordinates relative to the parent; the screen's and
/// the window's are both the window's, which is all Wayland tells.
const COORD_PARENT: u32 = 2;

/// The path a screen reader names widget `id` by.
pub(crate) fn path(id: AccessId) -> String {
  format!("{PREFIX}{}_{}", id.window, id.node)
}

/// An AT-SPI reference: the shell's bus name and an object's path.
pub(super) type Reference = (String, ObjectPath<'static>);

pub(super) fn reference(name: &str, path: String) -> Reference {
  let path = ObjectPath::try_from(path).expect("invariant: the shell's object paths are valid");
  (name.to_owned(), path)
}

/// What a path names: the application, or a widget as it is now.
enum Object {
  App,
  // Boxed: a node is far larger than the application.
  Node(AccessId, Box<AccessNode>),
}

fn object(access: &Accessibility, path: &str) -> Option<Object> {
  if path == ROOT {
    return Some(Object::App);
  }
  let (window, node) = path.strip_prefix(PREFIX)?.split_once('_')?;
  let id = AccessId {
    window: window.parse().ok()?,
    node: node.parse().ok()?,
  };
  Some(Object::Node(id, Box::new(access.node(id)?)))
}

/// AT-SPI's number and name for a role.
fn role(role: AccessRole) -> (u32, &'static str) {
  match role {
    AccessRole::Frame => (23, "frame"),
    AccessRole::Window => (69, "window"),
    AccessRole::Filler => (20, "filler"),
    AccessRole::Button => (43, "push button"),
    AccessRole::Label => (29, "label"),
    AccessRole::Entry => (79, "entry"),
    AccessRole::PasswordText => (40, "password text"),
    AccessRole::Slider => (51, "slider"),
    AccessRole::Image => (27, "image"),
  }
}

/// The state set as AT-SPI sends it: 64 bits in two words, low first.
fn states(node: &AccessNode) -> [u32; 2] {
  let bits = [
    (node.active, ACTIVE),
    (node.editable, EDITABLE),
    (node.enabled, ENABLED),
    (node.enabled, SENSITIVE),
    (node.focusable, FOCUSABLE),
    (node.focused, FOCUSED),
    (node.editable && node.multi_line, MULTI_LINE),
    (node.editable && !node.multi_line, SINGLE_LINE),
    (node.shown, SHOWING),
    (node.shown, VISIBLE),
  ];
  let set = bits
    .iter()
    .filter(|(on, _)| *on)
    .fold(0u64, |set, (_, bit)| set | 1 << bit);
  [set as u32, (set >> 32) as u32]
}

/// What a call on an object is answered with: its parent, children and
/// so on, the same for the application as for a widget.
struct Facts {
  name: String,
  role: (u32, &'static str),
  parent: Reference,
  children: Vec<Reference>,
  index: i32,
  states: [u32; 2],
  interfaces: Vec<&'static str>,
}

fn facts(access: &Accessibility, app: &App, name: &str, object: &Object) -> Facts {
  let node_ref = |id: AccessId| reference(name, path(id));
  match object {
    Object::App => {
      let desktop = app.desktop.borrow().clone();
      let parent = desktop.map_or_else(
        || reference(name, NULL.to_owned()),
        |(bus, path)| (bus, path.into_inner()),
      );
      Facts {
        name: app_name(),
        role: (75, "application"),
        parent,
        children: access.windows().into_iter().map(node_ref).collect(),
        index: -1,
        states: [0, 0],
        interfaces: vec![ACCESSIBLE, APPLICATION],
      }
    }
    Object::Node(_, node) => {
      let mut interfaces = vec![ACCESSIBLE, COMPONENT];
      if node.clickable {
        interfaces.push(ACTION);
      }
      if node.text.is_some() {
        interfaces.push(TEXT);
      }
      if node.value.is_some() {
        interfaces.push(VALUE);
      }
      Facts {
        name: node.name.clone(),
        role: role(node.role),
        parent: node
          .parent
          .map_or_else(|| reference(name, ROOT.to_owned()), node_ref),
        children: node.children.iter().copied().map(node_ref).collect(),
        index: i32::try_from(node.index).unwrap_or(i32::MAX),
        states: states(node),
        interfaces,
      }
    }
  }
}

/// The shell's program name, as the application's.
fn app_name() -> String {
  let program = std::env::args_os().next().unwrap_or_default();
  let name = std::path::Path::new(&program)
    .file_name()
    .unwrap_or_default();
  name.to_string_lossy().into_owned()
}

/// Answers one method call, as a toolkit's AT-SPI bridge does. Anything
/// it doesn't know gets the bus's usual error, so no caller waits for a
/// timeout.
pub(super) async fn answer(
  access: &Accessibility,
  app: &App,
  connection: &Connection,
  name: &str,
  call: &Message,
) {
  let header = call.header();
  let answer = reply(access, app, name, call, &header);
  if header.primary().flags().contains(Flags::NO_REPLY_EXPECTED) {
    return;
  }
  let sent = match answer {
    Ok(answer) => connection.send(&answer).await,
    Err(error) => Err(error),
  };
  if let Err(error) = sent {
    tracing::debug!(%error, "an accessibility answer wasn't sent");
  }
}

fn reply(
  access: &Accessibility,
  app: &App,
  name: &str,
  call: &Message,
  header: &Header<'_>,
) -> zbus::Result<Message> {
  let refuse = |error: &str, why: &str| Message::error(header, error).build(&why);
  let path = header.path().map_or("", |path| path.as_str());
  let interface = header
    .interface()
    .map_or("", |interface| interface.as_str());
  let member = header.member().map_or("", |member| member.as_str());
  // Nothing cached up front, so a screen reader asks for what it reads
  // and never holds a stale copy.
  if (path, interface, member) == (CACHE_PATH, CACHE, "GetItems") {
    return reply_empty_cache(header);
  }
  let Some(object) = object(access, path) else {
    return refuse(UNKNOWN_OBJECT, "no such object");
  };
  let reply = Message::method_return(header);
  let body = call.body();
  let node = match &object {
    Object::Node(id, node) => Some((*id, &**node)),
    Object::App => None,
  };
  match (interface, member) {
    (PROPERTIES, "Get") => {
      let Ok((interface, property)) = body.deserialize::<(&str, &str)>() else {
        return refuse(INVALID_ARGS, "not an interface and a name");
      };
      let facts = facts(access, app, name, &object);
      match properties(&facts, node.map(|(_, node)| node), app, interface).remove(property) {
        Some(value) => reply.build(&value),
        None => refuse(INVALID_ARGS, "no such property"),
      }
    }
    (PROPERTIES, "GetAll") => {
      let Ok(interface) = body.deserialize::<&str>() else {
        return refuse(INVALID_ARGS, "not an interface");
      };
      let facts = facts(access, app, name, &object);
      reply.build(&properties(
        &facts,
        node.map(|(_, node)| node),
        app,
        interface,
      ))
    }
    // The registry numbers the application, and a screen reader moves a
    // slider; nothing else is writable.
    (PROPERTIES, "Set") => match body.deserialize::<(&str, &str, Variant<'_>)>() {
      Ok((APPLICATION, "Id", value)) if node.is_none() => match i32::try_from(&value) {
        Ok(id) => {
          app.id.set(id);
          reply.build(&())
        }
        Err(_) => refuse(INVALID_ARGS, "the id isn't an int32"),
      },
      Ok((VALUE, "CurrentValue", value)) => match (node, f64::try_from(&value)) {
        (Some((id, node)), Ok(value)) if node.value.is_some() => {
          match access.set_value(id, value) {
            true => reply.build(&()),
            false => refuse(ACCESS_DENIED, "the slider is off"),
          }
        }
        _ => refuse(INVALID_ARGS, "not a slider and a double"),
      },
      _ => refuse(INVALID_ARGS, "read-only"),
    },
    (ACCESSIBLE, member) => {
      let facts = facts(access, app, name, &object);
      match member {
        "GetChildAtIndex" => match body.deserialize::<i32>() {
          Ok(at) => {
            let child = usize::try_from(at)
              .ok()
              .and_then(|at| facts.children.get(at).cloned());
            reply.build(&(child.unwrap_or_else(|| reference(name, NULL.to_owned())),))
          }
          Err(_) => refuse(INVALID_ARGS, "not an index"),
        },
        "GetChildren" => reply.build(&facts.children),
        "GetIndexInParent" => reply.build(&facts.index),
        "GetRelationSet" => reply.build(&Vec::<(u32, Vec<Reference>)>::new()),
        "GetRole" => reply.build(&facts.role.0),
        "GetRoleName" | "GetLocalizedRoleName" => reply.build(&facts.role.1),
        "GetState" => reply.build(&facts.states.as_slice()),
        "GetAttributes" => reply.build(&HashMap::<&str, &str>::new()),
        "GetApplication" => reply.build(&(reference(name, ROOT.to_owned()),)),
        "GetInterfaces" => reply.build(&facts.interfaces),
        _ => refuse(UNKNOWN_METHOD, "no such method"),
      }
    }
    (APPLICATION, "GetLocale") if node.is_none() => reply.build(&""),
    (COMPONENT, member) => match node {
      Some((_, node)) => component(access, node, name, member, call, reply, refuse),
      None => refuse(UNKNOWN_METHOD, "no such method"),
    },
    (ACTION, member) => match node {
      Some((id, node)) if node.clickable => match member {
        "GetActions" => reply.build(&[("click", "Click", "")].as_slice()),
        "GetName" => reply.build(&"click"),
        "GetLocalizedName" => reply.build(&"Click"),
        "GetDescription" | "GetKeyBinding" => reply.build(&""),
        "DoAction" => match body.deserialize::<i32>() {
          Ok(0) => reply.build(&access.click(id)),
          _ => refuse(INVALID_ARGS, "no such action"),
        },
        _ => refuse(UNKNOWN_METHOD, "no such method"),
      },
      _ => refuse(UNKNOWN_METHOD, "no such method"),
    },
    (TEXT, member) => match node.and_then(|(_, node)| node.text.as_ref()) {
      Some(text) => text_call(text, member, call, reply, refuse),
      None => refuse(UNKNOWN_METHOD, "no such method"),
    },
    _ => refuse(UNKNOWN_METHOD, "no such method"),
  }
}

/// `Text`'s answers on a field, in characters of what a screen reader
/// reads: a password's dots, never its text.
fn text_call(
  text: &AccessText,
  member: &str,
  call: &Message,
  reply: zbus::message::Builder<'_>,
  refuse: impl Fn(&str, &str) -> zbus::Result<Message>,
) -> zbus::Result<Message> {
  let body = call.body();
  let count = text.text.chars().count();
  let offset = |n: usize| i32::try_from(n).unwrap_or(i32::MAX);
  // A negative end, as AT-SPI's -1, is the end of the text.
  let at = |n: i32| usize::try_from(n).map_or(count, |n| n.min(count));
  let slice = |start: usize, end: usize| -> String {
    text
      .text
      .chars()
      .skip(start)
      .take(end.saturating_sub(start))
      .collect()
  };
  match member {
    "GetText" => match body.deserialize::<(i32, i32)>() {
      Ok((start, end)) => reply.build(&slice(at(start.max(0)), at(end))),
      Err(_) => refuse(INVALID_ARGS, "not two offsets"),
    },
    "GetCharacterAtOffset" => match body.deserialize::<i32>() {
      Ok(at) => {
        let char = usize::try_from(at)
          .ok()
          .and_then(|at| text.text.chars().nth(at));
        reply.build(&char.map_or(0, |char| char as i32))
      }
      Err(_) => refuse(INVALID_ARGS, "not an offset"),
    },
    "GetStringAtOffset" => match body.deserialize::<(i32, u32)>() {
      Ok((at, granularity)) => match text.segment(usize::try_from(at).unwrap_or(0), granularity) {
        Some((start, end)) => reply.build(&(slice(start, end), offset(start), offset(end))),
        None => refuse(INVALID_ARGS, "no such granularity"),
      },
      Err(_) => refuse(INVALID_ARGS, "not an offset and a granularity"),
    },
    "GetNSelections" => reply.build(&i32::from(text.selection.is_some())),
    "GetSelection" => match (body.deserialize::<i32>(), text.selection) {
      (Ok(0), Some((start, end))) => reply.build(&(offset(start), offset(end))),
      (Ok(_), _) => reply.build(&(0i32, 0i32)),
      (Err(_), _) => refuse(INVALID_ARGS, "not a selection number"),
    },
    // Plain text: no attributes, the same over the whole field.
    "GetAttributes" | "GetAttributeRun" => {
      reply.build(&(HashMap::<&str, &str>::new(), 0i32, offset(count)))
    }
    "GetDefaultAttributes" | "GetDefaultAttributeSet" => reply.build(&HashMap::<&str, &str>::new()),
    _ => refuse(UNKNOWN_METHOD, "no such method"),
  }
}

/// An empty `GetItems`, in the signature libatspi reads.
fn reply_empty_cache(header: &Header<'_>) -> zbus::Result<Message> {
  type Item = (
    Reference,
    Reference,
    Reference,
    i32,
    i32,
    Vec<String>,
    String,
    u32,
    Vec<u32>,
  );
  Message::method_return(header).build(&Vec::<Item>::new())
}

/// An interface's properties, by name.
fn properties(
  facts: &Facts,
  node: Option<&AccessNode>,
  app: &App,
  interface: &str,
) -> HashMap<&'static str, Variant<'static>> {
  let text = |text: &str| Variant::from(text.to_owned());
  match interface {
    ACCESSIBLE => HashMap::from([
      ("Name", text(&facts.name)),
      ("Description", text("")),
      ("Parent", Variant::from(facts.parent.clone())),
      (
        "ChildCount",
        Variant::from(i32::try_from(facts.children.len()).unwrap_or(i32::MAX)),
      ),
      ("Locale", text("")),
      ("AccessibleId", text("")),
      ("HelpText", text("")),
    ]),
    APPLICATION if node.is_none() => HashMap::from([
      ("ToolkitName", text("crabshell")),
      ("Version", text(env!("CARGO_PKG_VERSION"))),
      ("AtspiVersion", text("2.1")),
      ("Id", Variant::from(app.id.get())),
    ]),
    ACTION if node.is_some_and(|node| node.clickable) => {
      HashMap::from([("NActions", Variant::from(1i32))])
    }
    TEXT => match node.and_then(|node| node.text.as_ref()) {
      Some(text) => {
        let offset = |n: usize| Variant::from(i32::try_from(n).unwrap_or(i32::MAX));
        HashMap::from([
          ("CharacterCount", offset(text.text.chars().count())),
          ("CaretOffset", offset(text.caret)),
        ])
      }
      None => HashMap::new(),
    },
    VALUE => match node.and_then(|node| node.value) {
      Some(value) => HashMap::from([
        ("MinimumValue", Variant::from(value.min)),
        ("MaximumValue", Variant::from(value.max)),
        ("CurrentValue", Variant::from(value.current)),
        ("MinimumIncrement", Variant::from(value.increment)),
        ("Text", text("")),
      ]),
      None => HashMap::new(),
    },
    _ => HashMap::new(),
  }
}

/// `Component`'s answers: where a widget is, in the window's logical
/// pixels.
fn component(
  access: &Accessibility,
  node: &AccessNode,
  name: &str,
  member: &str,
  call: &Message,
  reply: zbus::message::Builder<'_>,
  refuse: impl Fn(&str, &str) -> zbus::Result<Message>,
) -> zbus::Result<Message> {
  let body = call.body();
  // Rounded to whole pixels, as AT-SPI's integers are.
  let extents = |coords: u32| {
    let parent = node
      .parent
      .filter(|_| coords == COORD_PARENT)
      .and_then(|parent| access.node(parent));
    let (x, y) = parent.map_or((0.0, 0.0), |parent| (parent.rect.x, parent.rect.y));
    let rect = node.rect;
    let round = |n: f32| n.round() as i32;
    (
      round(rect.x - x),
      round(rect.y - y),
      round(rect.width),
      round(rect.height),
    )
  };
  match member {
    "GetExtents" => match body.deserialize::<u32>() {
      Ok(coords) => reply.build(&(extents(coords),)),
      Err(_) => refuse(INVALID_ARGS, "not a coordinate type"),
    },
    "GetPosition" => match body.deserialize::<u32>() {
      Ok(coords) => {
        let (x, y, _, _) = extents(coords);
        reply.build(&(x, y))
      }
      Err(_) => refuse(INVALID_ARGS, "not a coordinate type"),
    },
    "GetSize" => {
      let (_, _, width, height) = extents(0);
      reply.build(&(width, height))
    }
    "Contains" => match body.deserialize::<(i32, i32, u32)>() {
      Ok((px, py, coords)) => {
        let (x, y, width, height) = extents(coords);
        reply.build(&(px >= x && py >= y && px < x + width && py < y + height))
      }
      Err(_) => refuse(INVALID_ARGS, "not a point"),
    },
    // Mouse review isn't served: which window a point is in is unknown.
    "GetAccessibleAtPoint" => reply.build(&(reference(name, NULL.to_owned()),)),
    "GetLayer" => match node.role {
      AccessRole::Frame | AccessRole::Window => reply.build(&7u32),
      _ => reply.build(&3u32),
    },
    "GetMDIZOrder" => reply.build(&0i16),
    "GetAlpha" => reply.build(&1.0f64),
    "GrabFocus" => reply.build(&false),
    _ => refuse(UNKNOWN_METHOD, "no such method"),
  }
}