1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DISTRIBUTED_INPUT_COLLECTOR_H 17 #define DISTRIBUTED_INPUT_COLLECTOR_H 18 19 #include <atomic> 20 #include <map> 21 #include <mutex> 22 #include <set> 23 #include <string> 24 25 #include <sys/epoll.h> 26 #include <linux/input.h> 27 28 #include "event_handler.h" 29 #include "refbase.h" 30 31 #include "constants_dinput.h" 32 #include "input_hub.h" 33 #include "i_sharing_dhid_listener.h" 34 35 namespace OHOS { 36 namespace DistributedHardware { 37 namespace DistributedInput { 38 class DistributedInputCollector { 39 public: 40 static DistributedInputCollector &GetInstance(); 41 int32_t Init(std::shared_ptr<AppExecFwk::EventHandler> sinkHandler); 42 void Release(); 43 AffectDhIds SetSharingTypes(bool enabled, const uint32_t &inputType); 44 AffectDhIds SetSharingDhIds(bool enabled, std::vector<std::string> dhIds); 45 void GetMouseNodePath(std::vector<std::string> dhIds, std::string &mouseNodePath, std::string &dhid); 46 void GetShareKeyboardPathsByDhIds(std::vector<std::string> dhIds, std::vector<std::string> &shareDhidsPaths, 47 std::vector<std::string> &shareDhIds); 48 // false for sharing device exist, true for all devices stop sharing 49 bool IsAllDevicesStoped(); 50 int32_t RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener); 51 void ReportDhIdSharingState(const AffectDhIds &dhIds); 52 void GetDeviceInfoByType(const uint32_t inputTypes, std::map<int32_t, std::string> &deviceInfo); 53 54 private: 55 DistributedInputCollector(); 56 ~DistributedInputCollector(); 57 58 bool InitCollectEventsThread(); 59 static void *CollectEventsThread(void *param); 60 void StartCollectEventsThread(); 61 void StopCollectEventsThread(); 62 63 RawEvent mEventBuffer[INPUT_EVENT_BUFFER_SIZE]; 64 pthread_t collectThreadID_; 65 bool isCollectingEvents_; 66 bool isStartGetDeviceHandlerThread; 67 std::unique_ptr<InputHub> inputHub_; 68 std::shared_ptr<AppExecFwk::EventHandler> sinkHandler_; 69 uint32_t inputTypes_; 70 71 std::mutex sharingDhIdListenerMtx_; 72 std::set<sptr<ISharingDhIdListener>> sharingDhIdListeners_; 73 }; 74 } // namespace DistributedInput 75 } // namespace DistributedHardware 76 } // namespace OHOS 77 78 #endif // DISTRIBUTED_INPUT_COLLECTOR_H