1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "host/libs/input_connector/event_buffer.h"
18
19 #include <cstdint>
20 #include <cstdlib>
21 #include <memory>
22 #include <vector>
23
24 #include <linux/input.h>
25
26 namespace cuttlefish {
27 namespace {
28
29 struct virtio_input_event {
30 uint16_t type;
31 uint16_t code;
32 int32_t value;
33 };
34
35 template <typename T>
36 struct EventBufferImpl : public EventBuffer {
EventBufferImplcuttlefish::__anona492b3a50111::EventBufferImpl37 EventBufferImpl(size_t num_events) { buffer_.reserve(num_events); }
AddEventcuttlefish::__anona492b3a50111::EventBufferImpl38 void AddEvent(uint16_t type, uint16_t code, int32_t value) override {
39 buffer_.push_back({.type = type, .code = code, .value = value});
40 }
datacuttlefish::__anona492b3a50111::EventBufferImpl41 const void* data() const override { return buffer_.data(); }
sizecuttlefish::__anona492b3a50111::EventBufferImpl42 std::size_t size() const override { return buffer_.size() * sizeof(T); }
43
44 private:
45 std::vector<T> buffer_;
46 };
47
48 } // namespace
49
CreateBuffer(InputEventType event_type,size_t num_events)50 std::unique_ptr<EventBuffer> CreateBuffer(InputEventType event_type,
51 size_t num_events) {
52 switch (event_type) {
53 case InputEventType::Virtio:
54 return std::unique_ptr<EventBuffer>(
55 new EventBufferImpl<virtio_input_event>(num_events));
56 case InputEventType::Evdev:
57 return std::unique_ptr<EventBuffer>(
58 new EventBufferImpl<input_event>(num_events));
59 }
60 }
61
62 } // namespace cuttlefish
63