settings/keyboard/
keyboard_controller.rs

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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::base::{SettingInfo, SettingType};
use crate::handler::base::Request;
use crate::handler::setting_handler::persist::{controller as data_controller, ClientProxy};
use crate::handler::setting_handler::{
    controller, ControllerError, IntoHandlerResult, SettingHandlerResult,
};
use crate::keyboard::types::{KeyboardInfo, KeymapId};
use crate::trace;
use settings_storage::device_storage::{DeviceStorage, DeviceStorageCompatible};
use settings_storage::storage_factory::{NoneT, StorageAccess};

use async_trait::async_trait;

impl DeviceStorageCompatible for KeyboardInfo {
    type Loader = NoneT;
    const KEY: &'static str = "keyboard_info";
}

impl Default for KeyboardInfo {
    fn default() -> Self {
        // The US_QWERTY keymap is the default if no settings are ever applied.
        KeyboardInfo { keymap: Some(KeymapId::UsQwerty), autorepeat: None }
    }
}

impl From<KeyboardInfo> for SettingInfo {
    fn from(info: KeyboardInfo) -> SettingInfo {
        SettingInfo::Keyboard(info)
    }
}

pub struct KeyboardController {
    client: ClientProxy,
}

impl StorageAccess for KeyboardController {
    type Storage = DeviceStorage;
    type Data = KeyboardInfo;
    const STORAGE_KEY: &'static str = KeyboardInfo::KEY;
}

#[async_trait(?Send)]
impl data_controller::Create for KeyboardController {
    async fn create(client: ClientProxy) -> Result<Self, ControllerError> {
        Ok(KeyboardController { client })
    }
}

#[async_trait(?Send)]
impl controller::Handle for KeyboardController {
    async fn handle(&self, request: Request) -> Option<SettingHandlerResult> {
        match request {
            Request::SetKeyboardInfo(keyboard_info) => {
                let id = fuchsia_trace::Id::new();
                trace!(id, c"set keyboard");
                let mut current = self.client.read_setting::<KeyboardInfo>(id).await;
                if !keyboard_info.is_valid() {
                    return Some(Err(ControllerError::InvalidArgument(
                        SettingType::Keyboard,
                        "keyboard".into(),
                        format!("{keyboard_info:?}").into(),
                    )));
                }
                // Save the value locally.
                current.keymap = keyboard_info.keymap.or(current.keymap);
                current.autorepeat =
                    keyboard_info.autorepeat.or(current.autorepeat).and_then(|value| {
                        if value.delay == 0 && value.period == 0 {
                            // Clean up Autorepeat when delay and period are set to zero.
                            None
                        } else {
                            Some(value)
                        }
                    });
                Some(self.client.write_setting(current.into(), id).await.into_handler_result())
            }
            Request::Get => {
                let id = fuchsia_trace::Id::new();
                trace!(id, c"get keyboard");
                Some(self.client.read_setting_info::<KeyboardInfo>(id).await.into_handler_result())
            }
            _ => None,
        }
    }
}