The [`input_report_reader`](https://cs.opensource.google/fuchsia/fuchsia/+/main:/src/ui/input/lib/input-report-reader) library facilitates the implementation
of input drivers by providing one of the commonly required functionalities: managing and
keeping track of upstream drivers that want to receive reports.

`InputReportReaderManager` in the library creates and manages `InputReportReaders`, which
implement [`fuchsia.input.report.InputReportsReader`](https://cs.opensource.google/fuchsia/fuchsia/+/main:/sdk/fidl/fuchsia.input.report/device.fidl). The manager can send
reports to all existing `InputReportReaders`.

An upstream driver that wants to read input reports from an input device may register with
`InputReportReaderManager` by calling the `CreateReader` method. When an input report arrives
(whether in the form of HID reports or device readings by polling and so on), the report is
pushed to all the registered readers using the `SendReportToAllReaders` method. The report is
then translated to `fuchsia_input_report::InputReport`. When the `InputReportReaderManager`
instance is destructed, all of the existing `InputReportReaders` are freed.

## How to use

The steps below walk through how to use the input report reader library in an input device:

1. Define a class that holds all the data that needs to be reported (this struct must declare
   the required `ToFidlInputReport` method), for example:

       struct MyExampleMouseReport {
         zx::time event_time;
         int64_t x;
         int64_t y;

         void ToFidlInputReport(fidl::WireTableBuilder<::fuchsia_input_report::wire::InputReport>&
                                input_report, fidl::AnyArena& allocator) const;
       }

2. Implement the `ToFidlInputReport` method, for example:

       void MyExampleMouseReport::ToFidlInputReport(
           fidl::WireTableBuilder<::fuchsia_input_report::wire::InputReport>& input_report,
           fidl::AnyArena& allocator) const {
         auto mouse_input_rpt = fuchsia_input_report::wire::MouseInputReport::Builder(allocator);
         mouse_input_rpt.movement_x(x);
         mouse_input_rpt.movement_y(y);

         input_report.mouse(mouse_input_rpt.Build());
         input_report.event_time(event_time.get());
       }

3. In your input device, declare an instance of `InputReportReaderManager` templated on
   your struct, for example:

       input_report_reader::InputReportReaderManager<MyExampleMouseReport> readers_;

4. When a report arrives, parse the report, fill out `MyExampleMouseReport`
   with the report data, and call the `SendReportToAllReaders` method, for example:

       readers_.SendReportToAllReaders(report);

5. In the implementation of [`fuchsia.input.report.InputDevice.GetInputReportsReader`](https://cs.opensource.google/fuchsia/fuchsia/+/main:/sdk/fidl/fuchsia.input.report/device.fidl),
   call the `CreateReader` method, for example:

       void MyExampleInputDevice::GetInputReportsReader(GetInputReportsReaderRequestView request,
                                                     GetInputReportsReaderCompleter::Sync& completer) {
         auto status = readers_.CreateReader(dispatcher_, std::move(request->reader));
         if (status != ZX_OK) {
            // ...
         }
       }

[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/development/drivers/concepts/driver_architectures/input_drivers/input_report_reader.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/development/drivers/concepts/driver_architectures/input_drivers/input_report_reader.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/development/drivers/concepts/driver_architectures/input_drivers/input_report_reader.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/development/drivers/concepts/driver_architectures/input_drivers/input_report_reader.md "Edit the source code of this page")