1 /* 2 * Copyright (c) 2025 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 MULTIMODAL_INPUT_PLUGIN_MANAGER_H 17 #define MULTIMODAL_INPUT_PLUGIN_MANAGER_H 18 19 #include <plugin_stage.h> 20 #include <timer_manager.h> 21 22 #include <dirent.h> 23 #include <dlfcn.h> 24 #include <map> 25 26 namespace OHOS { 27 namespace MMI { 28 29 /* 框架获取plugin对象 30 * ctx: 框架注册给plugin调用框架的对象实例 31 * plugin:plugin实例 32 * return:= 0: success 33 * !=0: error 34 */ 35 typedef int32_t (*InitPlugin)(std::shared_ptr<IPluginContext> ctx, std::shared_ptr<IInputPlugin>& plugin); 36 /* 框架通知plugin删除plugin对象 37 * ctx: 框架注册给plugin调用框架的对象实例 38 * plugin:plugin实例 39 * return:= 0: success 40 * !=0: error 41 */ 42 typedef int32_t (*UnintPlugin)(std::shared_ptr<IInputPlugin> plugin); 43 44 const int32_t RET_NOTDO = 0; 45 const int32_t RET_DO = 1; 46 47 struct InputPlugin : public IPluginContext { 48 public: InputPluginInputPlugin49 InputPlugin() {}; 50 virtual ~InputPlugin(); 51 int32_t Init(std::shared_ptr<IInputPlugin> pin); 52 void UnInit(); 53 PluginResult HandleEvent(libinput_event *event, int64_t frameTime); 54 PluginResult HandleEvent(std::shared_ptr<KeyEvent> keyEvent, InputPluginStage stage); 55 56 int32_t AddTimer(std::function<void()> func, int32_t intervalMs, int32_t repeatCount) override; 57 int32_t RemoveTimer(int32_t id) override; 58 void DispatchEvent(libinput_event *event, int64_t frameTime) override; 59 void DispatchEvent(std::shared_ptr<KeyEvent> keyEvent, InputDispatchStage stage) override; 60 61 int32_t timerCnt_ = 0; 62 int32_t prio_ = 200; 63 std::function<void(libinput_event*, int64_t)> callback_; 64 std::function<void(std::shared_ptr<KeyEvent>)> keyEventCallback_; 65 UnintPlugin unintPlugin_ = nullptr; 66 std::shared_ptr<IInputPlugin> plugin_; 67 std::string name_; 68 void* handle_; 69 70 private: 71 InputPluginStage stage_; 72 }; 73 74 struct InputPluginManager { 75 public: 76 ~InputPluginManager(); InputPluginManagerInputPluginManager77 explicit InputPluginManager(const std::string& directory) : directory_(directory) {}; 78 static std::shared_ptr<InputPluginManager> GetInstance(const std::string &directory = ""); 79 int32_t Init(); 80 void Dump(int fd); 81 int32_t HandleEvent(libinput_event* event, int64_t frameTime, InputPluginStage stage); 82 int32_t HandleEvent(std::shared_ptr<KeyEvent> keyEvent, InputPluginStage stage); 83 void PluginAssignmentCallBack(std::function<void(libinput_event*, int64_t)> callback, InputPluginStage stage); 84 void PluginAssignmentCallBack(std::function<void(std::shared_ptr<KeyEvent>)> callback, InputPluginStage stage); 85 void PrintPlugins(); 86 int32_t DoHandleEvent(libinput_event *event, int64_t frameTime, InputPlugin *iplugin, InputPluginStage stage); 87 int32_t DoHandleEvent(std::shared_ptr<KeyEvent> keyEvent, InputPlugin *iplugin, InputPluginStage stage); 88 89 private: 90 bool IntermediateEndEvent(libinput_event *event); 91 bool LoadPlugin(const std::string &path); 92 93 std::string directory_; 94 std::map<InputPluginStage, std::list<std::shared_ptr<InputPlugin>>> plugins_; 95 static std::shared_ptr<InputPluginManager> instance_; 96 static std::once_flag init_flag_; 97 }; 98 } // namespace MMI 99 } // namespace OHOS 100 #endif // MULTIMODAL_INPUT_PLUGIN_MANAGER_H 101