settings/input/
input_fidl_handler.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::base::SettingType;
6use crate::handler::base::Request;
7use crate::ingress::{request, watch, Scoped};
8use crate::input::types::{DeviceStateSource, InputDevice, InputDeviceType};
9use crate::job::source::{Error as JobError, ErrorResponder};
10use crate::job::Job;
11use fidl::endpoints::{ControlHandle, Responder};
12use fidl_fuchsia_settings::{
13    InputRequest, InputSetResponder, InputSetResult, InputSettings, InputState as FidlInputState,
14    InputWatchResponder,
15};
16
17impl ErrorResponder for InputSetResponder {
18    fn id(&self) -> &'static str {
19        "Input_Set"
20    }
21
22    fn respond(self: Box<Self>, error: fidl_fuchsia_settings::Error) -> Result<(), fidl::Error> {
23        self.send(Err(error))
24    }
25}
26
27impl request::Responder<Scoped<InputSetResult>> for InputSetResponder {
28    fn respond(self, Scoped(response): Scoped<InputSetResult>) {
29        let _ = self.send(response);
30    }
31}
32
33impl watch::Responder<InputSettings, zx::Status> for InputWatchResponder {
34    fn respond(self, response: Result<InputSettings, zx::Status>) {
35        match response {
36            Ok(settings) => {
37                let _ = self.send(&settings);
38            }
39            Err(error) => {
40                self.control_handle().shutdown_with_epitaph(error);
41            }
42        }
43    }
44}
45
46fn to_request(fidl_input_states: Vec<FidlInputState>) -> Option<Request> {
47    // Every device requires at least a device type and state flags.
48    let mut input_states_invalid_args = fidl_input_states
49        .iter()
50        .filter(|input_state| input_state.device_type.is_none() || input_state.state.is_none());
51
52    // If any devices were filtered out, the args were invalid, so exit.
53    if input_states_invalid_args.next().is_some() {
54        log::error!("Failed to parse input request: missing args");
55        return None;
56    }
57
58    let input_states = fidl_input_states
59        .iter()
60        .map(|input_state| {
61            let device_type: InputDeviceType = input_state.device_type.unwrap().into();
62            let device_state = input_state.state.clone().unwrap().into();
63            let device_name = input_state.name.clone().unwrap_or_else(|| device_type.to_string());
64            let source_states = [(DeviceStateSource::SOFTWARE, device_state)].into();
65            InputDevice { name: device_name, device_type, state: device_state, source_states }
66        })
67        .collect();
68
69    Some(Request::SetInputStates(input_states))
70}
71
72impl TryFrom<InputRequest> for Job {
73    type Error = JobError;
74    fn try_from(req: InputRequest) -> Result<Self, Self::Error> {
75        // Support future expansion of FIDL.
76        #[allow(unreachable_patterns)]
77        match req {
78            InputRequest::Set { input_states, responder } => match to_request(input_states) {
79                Some(request) => {
80                    Ok(request::Work::new(SettingType::Input, request, responder).into())
81                }
82                None => Err(JobError::InvalidInput(Box::new(responder))),
83            },
84            InputRequest::Watch { responder } => {
85                Ok(watch::Work::new_job(SettingType::Input, responder))
86            }
87            _ => {
88                log::warn!("Received a call to an unsupported API: {:?}", req);
89                Err(JobError::Unsupported)
90            }
91        }
92    }
93}