blob: 05be5b11339bda08111fb1e53f4800e1b2a431a3 [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.
#ifndef FUCHSIA_SDK_EXAMPLES_CC_INPUT_SAMPLE_H_
#define FUCHSIA_SDK_EXAMPLES_CC_INPUT_SAMPLE_H_
#include <fidl/fuchsia.device.fs/cpp/wire.h>
#include <fidl/fuchsia.driver.framework/cpp/wire.h>
#include <fidl/fuchsia.input.report/cpp/wire.h>
#include <lib/async/dispatcher.h>
#include <lib/driver2/namespace.h>
#include <lib/driver2/record_cpp.h>
#include <lib/driver2/structured_logger.h>
#include <lib/fdf/cpp/dispatcher.h>
#include <lib/input_report_reader/reader.h>
#include <lib/sys/component/cpp/outgoing_directory.h>
#include <lib/zx/status.h>
#include <unistd.h>
#include <memory>
#include <mutex>
namespace input_sample {
// Used by the input report reader library to transform reports over FIDL
struct SampleInputReport {
zx::time event_time;
uint8_t buttons = 0;
int8_t rel_x = 0;
int8_t rel_y = 0;
void ToFidlInputReport(
fidl::WireTableBuilder<::fuchsia_input_report::wire::InputReport>& input_report,
fidl::AnyArena& arena);
};
// Sample driver for a virtual input device that generates input reports.
class InputSampleDriver {
public:
InputSampleDriver(async_dispatcher_t* dispatcher,
fidl::WireSharedClient<fuchsia_driver_framework::Node> node,
driver::Namespace ns, driver::Logger logger)
: dispatcher_(dispatcher),
outgoing_(component::OutgoingDirectory::Create(dispatcher)),
node_(std::move(node)),
ns_(std::move(ns)),
logger_(std::move(logger)) {}
virtual ~InputSampleDriver() = default;
static constexpr const char* Name() { return "input-sample"; }
static zx::status<std::unique_ptr<InputSampleDriver>> Start(
fuchsia_driver_framework::wire::DriverStartArgs& start_args,
fdf::UnownedDispatcher dispatcher,
fidl::WireSharedClient<fuchsia_driver_framework::Node> node, driver::Namespace ns,
driver::Logger logger);
private:
zx::status<> Run(fidl::ServerEnd<fuchsia_io::Directory> outgoing_dir);
void SendFakeReport();
async_dispatcher_t* const dispatcher_;
component::OutgoingDirectory outgoing_;
fidl::WireSharedClient<fuchsia_driver_framework::Node> node_;
driver::Namespace ns_;
driver::Logger logger_;
std::shared_ptr<input_report_reader::InputReportReaderManager<SampleInputReport>>
input_report_readers_;
SampleInputReport report_;
};
} // namespace input_sample
#endif // FUCHSIA_SDK_EXAMPLES_CC_INPUT_SAMPLE_H_