blob: 7f445eb4e1f4036ab29b158a4174fd36177b1fc6 [file] [log] [blame]
// Copyright 2022 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.
#include "input_sample.h"
#include <lib/async/cpp/task.h>
namespace input_sample {
zx::result<> InputSampleDriver::Start() {
// Start sending fake reports
input_report_readers_ =
std::make_shared<input_report_reader::InputReportReaderManager<SampleInputReport>>();
SendFakeReport();
// Add the fuchsia.input.report/InputDevice protocol to be served as "/svc/input-sample"
auto result = context().outgoing()->component().AddProtocol<fuchsia_input_report::InputDevice>(
[this](fidl::ServerEnd<fuchsia_input_report::InputDevice> request) {
InputSampleServer::BindDeviceClient(&logger(), dispatcher(), input_report_readers_,
std::move(request));
},
name());
if (result.is_error()) {
FDF_SLOG(ERROR, "Failed to add protocol.", KV("status", result.status_string()));
return result.take_error();
}
// Initialize the driver compat service context.
compat::Context::ConnectAndCreate(
&context(), dispatcher(),
fit::bind_member<&InputSampleDriver::InitializeCompatService>(this));
return zx::ok();
}
// Initialize the driver compat context and export `fuchsia.input.report` to devfs.
void InputSampleDriver::InitializeCompatService(
zx::result<std::shared_ptr<compat::Context>> compat_result) {
if (!compat_result.is_ok()) {
FDF_SLOG(ERROR, "Failed to create compat context.",
KV("status", compat_result.status_string()));
node().reset();
return;
}
compat_context_ = std::move(*compat_result);
child_ = compat::DeviceServer(std::string(name()), 0, compat_context_->TopologicalPath(name()));
auto status = compat_context_->devfs_exporter().ExportSync(name(), child_->topological_path(),
fuchsia_device_fs::ExportOptions(),
/* INPUT_REPORT */ 26);
if (status != ZX_OK) {
FDF_SLOG(ERROR, "Failed to export to devfs.", KV("status", zx_status_get_string(status)));
node().reset();
return;
}
}
// Periodically send a new input report with fake data.
void InputSampleDriver::SendFakeReport() {
// Update fake mouse report
report_.event_time = zx::time(zx_clock_get_monotonic());
report_.buttons++;
report_.rel_x++;
report_.rel_y++;
// Send fake mouse report
input_report_readers_->SendReportToAllReaders(report_);
// Run again in 1 second
async::PostDelayedTask(
dispatcher(), [this]() mutable { SendFakeReport(); }, zx::sec(1));
}
} // namespace input_sample
FUCHSIA_DRIVER_RECORD_CPP_V2(driver::Record<input_sample::InputSampleDriver>);