• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
17 
18 
19 using namespace OHOS::Ace;
20 using namespace OHOS::Ace::Framework;
21 
MallocCString(const std::string & origin)22 ExternalString Utils::MallocCString(const std::string& origin)
23 {
24     if (origin.empty()) {
25         return {nullptr, nullptr};
26     }
27     auto len = origin.length() + 1;
28     char* res = (char*)malloc(sizeof(char) * len);
29     if (res == nullptr) {
30         return {nullptr, nullptr};
31     }
32     std::char_traits<char>::copy(res, origin.c_str(), len);
33     return {res, reinterpret_cast<void(*)(const void*)>(free)};
34 }
35 
GetCurrentFrontend()36 RefPtr<Frontend> Utils::GetCurrentFrontend()
37 {
38     auto container = Container::Current();
39     if (!container) {
40         container = Container::GetActive();
41         if (!container) {
42             LOGE("Can not found valid container");
43             return nullptr;
44         }
45     }
46     return container->GetFrontend();
47 }
48 
GetFunctionKey(int32_t functionKey)49 std::string Utils::GetFunctionKey(int32_t functionKey)
50 {
51     std::map<FunctionKey, std::string> keyNameMap {
52         {FunctionKey::ESC, "ESC"},
53         {FunctionKey::F1, "F1"},
54         {FunctionKey::F2, "F2"},
55         {FunctionKey::F3, "F3"},
56         {FunctionKey::F4, "F4"},
57         {FunctionKey::F5, "F5"},
58         {FunctionKey::F6, "F6"},
59         {FunctionKey::F7, "F7"},
60         {FunctionKey::F8, "F8"},
61         {FunctionKey::F9, "F9"},
62         {FunctionKey::F10, "F10"},
63         {FunctionKey::F11, "F11"},
64         {FunctionKey::F12, "F12"},
65         {FunctionKey::TAB, "TAB"},
66         {FunctionKey::DPAD_UP, "DPAD_UP"},
67         {FunctionKey::DPAD_DOWN, "DPAD_DOWN"},
68         {FunctionKey::DPAD_LEFT, "DPAD_LEFT"},
69         {FunctionKey::DPAD_RIGHT, "DPAD_RIGHT"}
70     };
71     auto result = keyNameMap.find(static_cast<FunctionKey>(functionKey));
72     return (result != keyNameMap.end()) ? result->second : std::string();
73 }
74