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 ANI_INPUT_CONSUMER_H 17 #define ANI_INPUT_CONSUMER_H 18 19 #include <ani.h> 20 #include <array> 21 #include <list> 22 #include <map> 23 #include <set> 24 #include <string> 25 26 #include "key_option.h" 27 28 #define SUCCESS_CODE 0 29 #define ERROR_CODE (-1) 30 #define UNREGISTERED_CODE (-2) 31 #define PRE_KEY_MAX_COUNT 4 32 33 enum JS_CALLBACK_EVENT { 34 JS_CALLBACK_EVENT_FAILED = -1, 35 JS_CALLBACK_EVENT_SUCCESS = 1, 36 JS_CALLBACK_EVENT_EXIST = 2, 37 JS_CALLBACK_EVENT_NOT_EXIST = 3, 38 }; 39 40 namespace OHOS { 41 namespace MMI { 42 43 struct KeyEventMonitorInfo { 44 ani_env* env = nullptr; 45 std::string eventType; 46 std::string name; 47 ani_ref callback = nullptr; 48 int32_t subscribeId = 0; 49 ani_ref keyOptionsObj = nullptr; 50 std::shared_ptr<KeyOption> keyOption = nullptr; 51 ~KeyEventMonitorInfo(); 52 }; 53 54 class AniLocalScopeGuard { 55 public: AniLocalScopeGuard(ani_env * env,size_t nrRefs)56 AniLocalScopeGuard(ani_env *env, size_t nrRefs) : env_(env) 57 { 58 status_ = env_->CreateLocalScope(nrRefs); 59 } 60 ~AniLocalScopeGuard()61 ~AniLocalScopeGuard() 62 { 63 if (ANI_OK != status_) { 64 return; 65 } 66 env_->DestroyLocalScope(); 67 } 68 IsStatusOK()69 bool IsStatusOK() 70 { 71 return ANI_OK == status_; 72 } 73 GetStatus()74 ani_status GetStatus() 75 { 76 return status_; 77 } 78 79 private: 80 ani_env *env_ = nullptr; 81 ani_status status_ = ANI_ERROR; 82 }; 83 typedef std::map<std::string, std::list<std::shared_ptr<KeyEventMonitorInfo>>> Callbacks; 84 } // namespace MMI 85 } // namespace OHOS 86 87 #endif // ANI_INPUT_CONSUMER_H 88