1 /* 2 * Copyright (C) 2019 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 #pragma once 18 19 #include <array> 20 #include <cinttypes> 21 #include <functional> 22 #include <memory> 23 #include <vector> 24 25 #include <https/RunLoop.h> 26 27 namespace android { 28 29 struct InputEventBuffer { 30 virtual ~InputEventBuffer() = default; 31 virtual void addEvent(uint16_t type, uint16_t code, int32_t value) = 0; 32 virtual size_t size() const = 0; 33 virtual const void* data() const = 0; 34 }; 35 36 struct InputSink : public std::enable_shared_from_this<InputSink> { 37 explicit InputSink(std::shared_ptr<RunLoop> runLoop, int serverFd, 38 bool write_virtio_input); 39 ~InputSink(); 40 void start(); 41 42 std::unique_ptr<InputEventBuffer> getEventBuffer() const; 43 void SendEvents(std::unique_ptr<InputEventBuffer> evt_buffer); 44 45 private: 46 std::shared_ptr<RunLoop> mRunLoop; 47 int mServerFd; 48 49 int mClientFd; 50 51 std::mutex mLock; 52 std::vector<uint8_t> mOutBuffer; 53 bool mSendPending; 54 bool mWriteVirtioInput; 55 56 void onServerConnection(); 57 void onSocketRecv(); 58 void onSocketSend(); 59 60 void sendRawEvents(const void* evt_buffer, size_t length); 61 }; 62 63 } // namespace android 64