1100ae2f9Sopenharmony_ci/* 2100ae2f9Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3100ae2f9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4100ae2f9Sopenharmony_ci * you may not use this file except in compliance with the License. 5100ae2f9Sopenharmony_ci * You may obtain a copy of the License at 6100ae2f9Sopenharmony_ci * 7100ae2f9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8100ae2f9Sopenharmony_ci * 9100ae2f9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10100ae2f9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11100ae2f9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12100ae2f9Sopenharmony_ci * See the License for the specific language governing permissions and 13100ae2f9Sopenharmony_ci * limitations under the License. 14100ae2f9Sopenharmony_ci */ 15100ae2f9Sopenharmony_ci 16100ae2f9Sopenharmony_ci#include "emitter_ffi.h" 17100ae2f9Sopenharmony_ci#include "emitter_log.h" 18100ae2f9Sopenharmony_ci#include "emitter.h" 19100ae2f9Sopenharmony_ci#include "cj_lambda.h" 20100ae2f9Sopenharmony_ci 21100ae2f9Sopenharmony_ciusing namespace OHOS::FFI; 22100ae2f9Sopenharmony_ciusing namespace OHOS::EventsEmitter; 23100ae2f9Sopenharmony_ci 24100ae2f9Sopenharmony_ciextern "C" { 25100ae2f9Sopenharmony_ci CallbackImpl *CreateCallback(CEventCallback &callbackInfo) 26100ae2f9Sopenharmony_ci { 27100ae2f9Sopenharmony_ci auto onChange = [lambda = CJLambda::Create(callbackInfo.callbackRef)](const CEventData data) -> void { 28100ae2f9Sopenharmony_ci lambda(data); 29100ae2f9Sopenharmony_ci }; 30100ae2f9Sopenharmony_ci CallbackImpl *callbackImpl = new CallbackImpl(std::string(callbackInfo.name), onChange); 31100ae2f9Sopenharmony_ci if (callbackImpl == nullptr) { 32100ae2f9Sopenharmony_ci LOGE("Fail to create CallbackImpl.") 33100ae2f9Sopenharmony_ci return nullptr; 34100ae2f9Sopenharmony_ci } 35100ae2f9Sopenharmony_ci return callbackImpl; 36100ae2f9Sopenharmony_ci } 37100ae2f9Sopenharmony_ci 38100ae2f9Sopenharmony_ci int32_t CJ_OnWithId(uint32_t eventId, CEventCallback callbackInfo) 39100ae2f9Sopenharmony_ci { 40100ae2f9Sopenharmony_ci CallbackImpl *callback = CreateCallback(callbackInfo); 41100ae2f9Sopenharmony_ci if (callback == nullptr) { 42100ae2f9Sopenharmony_ci return MEMORY_ERROR; 43100ae2f9Sopenharmony_ci } 44100ae2f9Sopenharmony_ci return Emitter::On(eventId, callback); 45100ae2f9Sopenharmony_ci } 46100ae2f9Sopenharmony_ci 47100ae2f9Sopenharmony_ci int32_t CJ_OnWithStringId(char* eventId, CEventCallback callbackInfo) 48100ae2f9Sopenharmony_ci { 49100ae2f9Sopenharmony_ci CallbackImpl *callback = CreateCallback(callbackInfo); 50100ae2f9Sopenharmony_ci if (callback == nullptr) { 51100ae2f9Sopenharmony_ci return MEMORY_ERROR; 52100ae2f9Sopenharmony_ci } 53100ae2f9Sopenharmony_ci return Emitter::On(eventId, callback); 54100ae2f9Sopenharmony_ci } 55100ae2f9Sopenharmony_ci 56100ae2f9Sopenharmony_ci int32_t CJ_OnceWithId(uint32_t eventId, CEventCallback callbackInfo) 57100ae2f9Sopenharmony_ci { 58100ae2f9Sopenharmony_ci CallbackImpl *callback = CreateCallback(callbackInfo); 59100ae2f9Sopenharmony_ci if (callback == nullptr) { 60100ae2f9Sopenharmony_ci return MEMORY_ERROR; 61100ae2f9Sopenharmony_ci } 62100ae2f9Sopenharmony_ci return Emitter::Once(eventId, callback); 63100ae2f9Sopenharmony_ci } 64100ae2f9Sopenharmony_ci 65100ae2f9Sopenharmony_ci int32_t CJ_OnceWithStringId(char* eventId, CEventCallback callbackInfo) 66100ae2f9Sopenharmony_ci { 67100ae2f9Sopenharmony_ci CallbackImpl *callback = CreateCallback(callbackInfo); 68100ae2f9Sopenharmony_ci if (callback == nullptr) { 69100ae2f9Sopenharmony_ci return MEMORY_ERROR; 70100ae2f9Sopenharmony_ci } 71100ae2f9Sopenharmony_ci return Emitter::Once(eventId, callback); 72100ae2f9Sopenharmony_ci } 73100ae2f9Sopenharmony_ci 74100ae2f9Sopenharmony_ci void CJ_OffWithId(uint32_t eventId) 75100ae2f9Sopenharmony_ci { 76100ae2f9Sopenharmony_ci Emitter::Off(eventId); 77100ae2f9Sopenharmony_ci } 78100ae2f9Sopenharmony_ci 79100ae2f9Sopenharmony_ci void CJ_OffWithString(char* eventId) 80100ae2f9Sopenharmony_ci { 81100ae2f9Sopenharmony_ci Emitter::Off(eventId); 82100ae2f9Sopenharmony_ci } 83100ae2f9Sopenharmony_ci 84100ae2f9Sopenharmony_ci int32_t CJ_OffWithIdCallback(uint32_t eventId, CEventCallback callbackInfo) 85100ae2f9Sopenharmony_ci { 86100ae2f9Sopenharmony_ci CallbackImpl *callback = CreateCallback(callbackInfo); 87100ae2f9Sopenharmony_ci if (callback == nullptr) { 88100ae2f9Sopenharmony_ci return MEMORY_ERROR; 89100ae2f9Sopenharmony_ci } 90100ae2f9Sopenharmony_ci Emitter::Off(eventId, callback); 91100ae2f9Sopenharmony_ci delete callback; 92100ae2f9Sopenharmony_ci return SUCCESS_CODE; 93100ae2f9Sopenharmony_ci } 94100ae2f9Sopenharmony_ci 95100ae2f9Sopenharmony_ci int32_t CJ_OffWithStringCallback(char* eventId, CEventCallback callbackInfo) 96100ae2f9Sopenharmony_ci { 97100ae2f9Sopenharmony_ci CallbackImpl *callback = CreateCallback(callbackInfo); 98100ae2f9Sopenharmony_ci if (callback == nullptr) { 99100ae2f9Sopenharmony_ci return MEMORY_ERROR; 100100ae2f9Sopenharmony_ci } 101100ae2f9Sopenharmony_ci Emitter::Off(eventId, callback); 102100ae2f9Sopenharmony_ci delete callback; 103100ae2f9Sopenharmony_ci return SUCCESS_CODE; 104100ae2f9Sopenharmony_ci } 105100ae2f9Sopenharmony_ci 106100ae2f9Sopenharmony_ci void CJ_EmitWithId(uint32_t eventId, uint32_t priority, CEventData data) 107100ae2f9Sopenharmony_ci { 108100ae2f9Sopenharmony_ci Emitter::Emit(eventId, priority, data); 109100ae2f9Sopenharmony_ci } 110100ae2f9Sopenharmony_ci 111100ae2f9Sopenharmony_ci void CJ_EmitWithString(char* eventId, uint32_t priority, CEventData data) 112100ae2f9Sopenharmony_ci { 113100ae2f9Sopenharmony_ci Emitter::Emit(eventId, priority, data); 114100ae2f9Sopenharmony_ci } 115100ae2f9Sopenharmony_ci 116100ae2f9Sopenharmony_ci uint32_t CJ_GetListenerCountById(uint32_t eventId) 117100ae2f9Sopenharmony_ci { 118100ae2f9Sopenharmony_ci auto ret = Emitter::GetListenerCount(eventId); 119100ae2f9Sopenharmony_ci return ret; 120100ae2f9Sopenharmony_ci } 121100ae2f9Sopenharmony_ci 122100ae2f9Sopenharmony_ci uint32_t CJ_GetListenerCountByString(char* eventId) 123100ae2f9Sopenharmony_ci { 124100ae2f9Sopenharmony_ci auto ret = Emitter::GetListenerCount(std::string(eventId)); 125100ae2f9Sopenharmony_ci return ret; 126100ae2f9Sopenharmony_ci } 127100ae2f9Sopenharmony_ci} 128