• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <fstream>
17 #include <iostream>
18 #include <sstream>
19 #include <thread>
20 #include <map>
21 
22 #include "adapter/preview/entrance/ace_ability.h"
23 #include "adapter/preview/entrance/ace_run_args.h"
24 #include "run_args_parser.h"
25 #include "frameworks/base/log/log.h"
26 #include "core/event/touch_event.h"
27 #include "core/event/key_event.h"
28 #include "adapter/preview/entrance/event_dispatcher.h"
29 #include "base/utils/macros.h"
30 
31 namespace {
32 const std::map<int, OHOS::Ace::TouchType>TOUCH_TYPE_MAP = {
33     {0, OHOS::Ace::TouchType::DOWN},
34     {1, OHOS::Ace::TouchType::UP},
35     {2, OHOS::Ace::TouchType::MOVE},
36     {3, OHOS::Ace::TouchType::CANCEL},
37     {4, OHOS::Ace::TouchType::UNKNOWN},
38 };
39 const std::map<int, OHOS::Ace::SourceType>SOURCE_TYPE_MAP = {
40     {0, OHOS::Ace::SourceType::NONE},
41     {1, OHOS::Ace::SourceType::MOUSE},
42     {2, OHOS::Ace::SourceType::TOUCH},
43     {3, OHOS::Ace::SourceType::TOUCH_PAD},
44     {4, OHOS::Ace::SourceType::KEYBOARD},
45 };
46 
47 using Callback = void (*)(const void* pp, const size_t bufferSize, const int32_t width, const int32_t height);
48 Callback cb {nullptr};
49 
__anon40b7fbd40202(const void* p, const size_t bufferSize, const int32_t width, const int32_t height) 50 auto renderCallback = [](const void* p, const size_t bufferSize, const int32_t width, const int32_t height) -> bool {
51     cb(p, bufferSize, width, height);
52     LOGI("bufferSize = %{public}d, width = %{public}d, height = %{public}d.",
53         static_cast<uint32_t>(bufferSize), width, height);
54     return true;
55 };
56 
__anon40b7fbd40302(const std::string currentPagePath) 57 auto pageCallback = [](const std::string currentPagePath) -> bool {
58     LOGI("callback currentPage:%s", currentPagePath.c_str());
59     return true;
60 };
61 } // namespace
62 
63 extern "C" ACE_FORCE_EXPORT_WITH_PREVIEW bool RegisterCallback(Callback callback);
64 extern "C" ACE_FORCE_EXPORT_WITH_PREVIEW int StartPreviewer(const char* configContents);
65 extern "C" ACE_FORCE_EXPORT_WITH_PREVIEW void Stop();
66 extern "C" ACE_FORCE_EXPORT_WITH_PREVIEW void DispatchTouchEvent(int id, float x, float y,
67     int touchtype, int sourcetype);
68 extern "C" ACE_FORCE_EXPORT_WITH_PREVIEW void DispatchBackPressedEvent();
69 extern "C" ACE_FORCE_EXPORT_WITH_PREVIEW void DispatchInputMethodEvent(unsigned int codePoint);
70 
71 // registering callback function
RegisterCallback(Callback callback)72 bool RegisterCallback(Callback callback)
73 {
74     cb = callback;
75     if (!cb) {
76         LOGE("registering callback function failed!");
77         return false;
78     }
79     return true;
80 }
81 
StartPreviewer(const char * configContents)82 int StartPreviewer(const char* configContents)
83 {
84     OHOS::Ace::Samples::RunArgsParser runArgsParser;
85     bool flagParser = runArgsParser.Parse(configContents);
86     if (!flagParser) {
87         LOGE("Parse run args failed!");
88         return -1;
89     }
90     OHOS::Ace::Platform::AceRunArgs args = runArgsParser.GetAceRunArgs();
91     args.onRender = std::move(renderCallback);
92     args.onRouterChange = std::move(pageCallback);
93     auto ability = OHOS::Ace::Platform::AceAbility::CreateInstance(args);
94     if (!ability) {
95         LOGE("Could not create AceAbility!");
96         return -1;
97     }
98     ability->InitEnv();
99     LOGI("Ace initialize done. run loop now");
100     ability->Start();
101     return 0;
102 }
103 
Stop()104 void Stop()
105 {
106     OHOS::Ace::Platform::AceAbility::Stop();
107 }
108 
DispatchTouchEvent(int id,float x,float y,int touchtype,int sourcetype)109 void DispatchTouchEvent(int id, float x, float y, int touchtype, int sourcetype)
110 {
111     OHOS::Ace::TouchEvent events;
112     events.id=id;
113     events.x=x;
114     events.y=y;
115     events.type=TOUCH_TYPE_MAP.at(touchtype);
116     events.sourceType=SOURCE_TYPE_MAP.at(sourcetype);
117     LOGI("id =%{public}d,x=%{public}f,y=%{public}f", events.id, events.x, events.y);
118     OHOS::Ace::Platform::EventDispatcher::GetInstance().DispatchTouchEvent(events);
119 }
120 
DispatchBackPressedEvent()121 void DispatchBackPressedEvent()
122 {
123     OHOS::Ace::Platform::EventDispatcher::GetInstance().DispatchBackPressedEvent();
124 }
125 
DispatchInputMethodEvent(unsigned int codePoint)126 void DispatchInputMethodEvent(unsigned int codePoint)
127 {
128     OHOS::Ace::Platform::EventDispatcher::GetInstance().DispatchInputMethodEvent(codePoint);
129 }
130