virtual_console_lib/
app.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
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
// 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::args::VirtualConsoleArgs;
use crate::colors::ColorScheme;
use crate::log::{Log, LogClient};
use crate::session_manager::{SessionManager, SessionManagerClient};
use crate::terminal::Terminal;
use crate::view::{ViewMessages, VirtualConsoleViewAssistant};
use anyhow::Error;
use carnelian::app::Config;
use carnelian::{make_message, AppAssistant, AppSender, MessageTarget, ViewAssistantPtr, ViewKey};
use fidl::prelude::*;
use fidl_fuchsia_hardware_display::VirtconMode;
use fidl_fuchsia_virtualconsole::SessionManagerMarker;
use fuchsia_async as fasync;
use pty::ServerPty;
use std::collections::BTreeMap;
use term_model::event::{Event, EventListener};

const DEBUGLOG_ID: u32 = 0;
const FIRST_SESSION_ID: u32 = 1;

pub struct EventProxy {
    app_sender: AppSender,
    id: u32,
}

impl EventProxy {
    pub fn new(app_sender: &AppSender, id: u32) -> Self {
        Self { app_sender: app_sender.clone(), id }
    }
}

impl EventListener for EventProxy {
    fn send_event(&self, event: Event) {
        match event {
            Event::MouseCursorDirty => {
                self.app_sender.queue_message(
                    MessageTarget::Application,
                    make_message(AppMessages::RequestTerminalUpdateMessage(self.id)),
                );
            }
            _ => (),
        }
    }
}

enum AppMessages {
    AddTerminalMessage(u32, Terminal<EventProxy>, bool),
    RequestTerminalUpdateMessage(u32),
}

#[derive(Clone)]
struct VirtualConsoleClient {
    app_sender: AppSender,
    color_scheme: ColorScheme,
    scrollback_rows: u32,
}

impl VirtualConsoleClient {
    fn create_terminal(
        &self,
        id: u32,
        title: String,
        make_active: bool,
        pty: Option<ServerPty>,
    ) -> Result<Terminal<EventProxy>, Error> {
        let event_proxy = EventProxy::new(&self.app_sender, id);
        let terminal =
            Terminal::new(event_proxy, title, self.color_scheme, self.scrollback_rows, pty);
        let terminal_clone = terminal.try_clone()?;
        self.app_sender.queue_message(
            MessageTarget::Application,
            make_message(AppMessages::AddTerminalMessage(id, terminal_clone, make_active)),
        );
        Ok(terminal)
    }

    fn request_update(&self, id: u32) {
        self.app_sender.queue_message(
            MessageTarget::Application,
            make_message(AppMessages::RequestTerminalUpdateMessage(id)),
        );
    }
}

impl LogClient for VirtualConsoleClient {
    type Listener = EventProxy;

    fn create_terminal(&self, id: u32, title: String) -> Result<Terminal<Self::Listener>, Error> {
        VirtualConsoleClient::create_terminal(self, id, title, false, None)
    }

    fn request_update(&self, id: u32) {
        VirtualConsoleClient::request_update(self, id)
    }
}

impl SessionManagerClient for VirtualConsoleClient {
    type Listener = EventProxy;

    fn create_terminal(
        &self,
        id: u32,
        title: String,
        make_active: bool,
        pty: ServerPty,
    ) -> Result<Terminal<Self::Listener>, Error> {
        VirtualConsoleClient::create_terminal(self, id, title, make_active, Some(pty))
    }

    fn request_update(&self, id: u32) {
        VirtualConsoleClient::request_update(self, id)
    }
}

pub struct VirtualConsoleAppAssistant {
    app_sender: AppSender,
    args: VirtualConsoleArgs,
    read_only_debuglog: Option<zx::DebugLog>,
    session_manager: SessionManager,
    terminals: BTreeMap<u32, (Terminal<EventProxy>, bool)>,
    first_view: Option<ViewKey>,
}

impl VirtualConsoleAppAssistant {
    pub fn new(
        app_sender: &AppSender,
        args: VirtualConsoleArgs,
        read_only_debuglog: Option<zx::DebugLog>,
    ) -> Result<VirtualConsoleAppAssistant, Error> {
        let app_sender = app_sender.clone();
        let session_manager = SessionManager::new(args.keep_log_visible, FIRST_SESSION_ID);
        let terminals = BTreeMap::new();

        Ok(VirtualConsoleAppAssistant {
            app_sender,
            args,
            read_only_debuglog,
            session_manager,
            terminals,
            first_view: None,
        })
    }

    fn start_log(&self, read_only_debuglog: zx::DebugLog) -> Result<(), Error> {
        let app_sender = self.app_sender.clone();
        let color_scheme = self.args.color_scheme;
        let scrollback_rows = self.args.scrollback_rows;
        let client = VirtualConsoleClient { app_sender, color_scheme, scrollback_rows };
        Log::start(read_only_debuglog, &client, DEBUGLOG_ID)
    }

    #[cfg(test)]
    fn new_for_test() -> Result<VirtualConsoleAppAssistant, Error> {
        let app_sender = AppSender::new_for_testing_purposes_only();
        Self::new(&app_sender, VirtualConsoleArgs::default(), None)
    }
}

impl AppAssistant for VirtualConsoleAppAssistant {
    fn setup(&mut self) -> Result<(), Error> {
        if let Some(read_only_debuglog) = self.read_only_debuglog.take() {
            self.start_log(read_only_debuglog).expect("failed to start debuglog");
        }
        Ok(())
    }

    fn create_view_assistant(&mut self, view_key: ViewKey) -> Result<ViewAssistantPtr, Error> {
        // The first view created will take the role as primary output.
        let is_primary = if self.first_view.is_none() {
            // Terminal messages will be routed to this view from this point forward.
            self.first_view = Some(view_key);
            true
        } else {
            false
        };

        let view_assistant = VirtualConsoleViewAssistant::new(
            &self.app_sender,
            view_key,
            self.args.color_scheme,
            self.args.rounded_corners,
            self.args.font_size,
            self.args.dpi.iter().cloned().collect(),
            self.args.boot_animation,
            is_primary,
        )?;

        // Early out if terminals are already associated with a view.
        // TODO(reveman): Improve this when we have multi-display support.
        if !is_primary {
            return Ok(view_assistant);
        }

        // Primary display has connected when this is called.
        self.session_manager.set_has_primary_connected(true);

        // Add all terminals to this view.
        for (id, (terminal, make_active)) in &self.terminals {
            let terminal_clone = terminal.try_clone().expect("failed to clone terminal");
            self.app_sender.queue_message(
                MessageTarget::View(view_key),
                make_message(ViewMessages::AddTerminalMessage(*id, terminal_clone, *make_active)),
            );
        }

        Ok(view_assistant)
    }

    fn outgoing_services_names(&self) -> Vec<&'static str> {
        [SessionManagerMarker::PROTOCOL_NAME].to_vec()
    }

    fn handle_service_connection_request(
        &mut self,
        _service_name: &str,
        channel: fasync::Channel,
    ) -> Result<(), Error> {
        let app_sender = self.app_sender.clone();
        let color_scheme = self.args.color_scheme;
        let scrollback_rows = self.args.scrollback_rows;
        let client = VirtualConsoleClient { app_sender, color_scheme, scrollback_rows };
        self.session_manager.bind(&client, channel);
        Ok(())
    }

    fn filter_config(&mut self, config: &mut Config) {
        config.view_mode = carnelian::app::ViewMode::Direct;
        config.virtcon_mode = Some(VirtconMode::Forced);
        config.keyboard_autorepeat = self.args.keyrepeat;
        config.display_rotation = self.args.display_rotation;
        config.keymap_name = Some(self.args.keymap.clone());
        config.buffer_count = Some(self.args.buffer_count);
    }

    fn handle_message(&mut self, message: carnelian::Message) {
        if let Some(message) = message.downcast_ref::<AppMessages>() {
            match message {
                AppMessages::AddTerminalMessage(id, terminal, make_active) => {
                    let terminal_clone = terminal.try_clone().expect("failed to clone terminal");
                    self.terminals.insert(*id, (terminal_clone, *make_active));
                    if let Some(view_key) = self.first_view {
                        let terminal_clone =
                            terminal.try_clone().expect("failed to clone terminal");
                        self.app_sender.queue_message(
                            MessageTarget::View(view_key),
                            make_message(ViewMessages::AddTerminalMessage(
                                *id,
                                terminal_clone,
                                *make_active,
                            )),
                        );
                    }
                }
                AppMessages::RequestTerminalUpdateMessage(id) => {
                    if let Some(view_key) = self.first_view {
                        self.app_sender.queue_message(
                            MessageTarget::View(view_key),
                            make_message(ViewMessages::RequestTerminalUpdateMessage(*id)),
                        );
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fuchsia_async as fasync;

    #[fasync::run_singlethreaded(test)]
    async fn can_create_app() -> Result<(), Error> {
        let _ = VirtualConsoleAppAssistant::new_for_test()?;
        Ok(())
    }

    #[fasync::run_singlethreaded(test)]
    async fn can_create_virtual_console_view() -> Result<(), Error> {
        let mut app = VirtualConsoleAppAssistant::new_for_test()?;
        app.create_view_assistant(Default::default())?;
        Ok(())
    }

    #[fasync::run_singlethreaded(test)]
    async fn can_handle_service_connection_request_without_view() -> Result<(), Error> {
        let mut app = VirtualConsoleAppAssistant::new_for_test()?;
        let (_, server_end) = zx::Channel::create();
        let channel = fasync::Channel::from_channel(server_end);
        app.handle_service_connection_request(SessionManagerMarker::PROTOCOL_NAME, channel)?;
        Ok(())
    }
}