1 /*
2  * Copyright (c) 2023 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 TOUCH_SCREEN_H
17 #define TOUCH_SCREEN_H
18 
19 #include <dlfcn.h>
20 
21 #include "nocopyable.h"
22 #include "platform/common/rs_log.h"
23 #include "singleton.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 class TouchScreen final {
28 public:
29     using TsSetFeatureConfig = int32_t (*)(int32_t, const char *);
30     using TsSetAftConfig = int32_t (*)(const char *);
31 
32     DISALLOW_COPY_AND_MOVE(TouchScreen);
33 
34     void InitTouchScreen();
35 
IsSetFeatureConfigHandleValid()36     bool IsSetFeatureConfigHandleValid() const
37     {
38         return setFeatureConfigHandle_ != nullptr;
39     }
40 
SetFeatureConfig(int32_t feature,const char * config)41     int32_t SetFeatureConfig(int32_t feature, const char *config)
42     {
43         return setFeatureConfigHandle_(feature, config);
44     }
45 
IsSetAftConfigHandleValid()46     bool IsSetAftConfigHandleValid() const
47     {
48         return setAftConfigHandle_ != nullptr;
49     }
50 
SetAftConfig(const char * config)51     int32_t SetAftConfig(const char *config)
52     {
53         return setAftConfigHandle_(config);
54     }
55 protected:
56     template<typename Handle>
GetHandleBySymbol(Handle & handle,const char * symbol)57     void GetHandleBySymbol(Handle& handle, const char* symbol)
58     {
59         handle = reinterpret_cast<Handle>(dlsym(touchScreenHandle_, symbol));
60         if (handle == nullptr) {
61             RS_LOGE("touch screen get handle by %{public}s failed, error: %{public}s",
62                 symbol, dlerror());
63         } else {
64             RS_LOGI("touch screen get handle by %{public}s success", symbol);
65         }
66     }
67 
68 private:
69     DECLARE_DELAYED_SINGLETON(TouchScreen);
70 
71     void* touchScreenHandle_ = nullptr;
72     TsSetFeatureConfig setFeatureConfigHandle_ = nullptr;
73     TsSetAftConfig setAftConfigHandle_ = nullptr;
74 };
75 
76 #define TOUCH_SCREEN ::OHOS::DelayedSingleton<TouchScreen>::GetInstance()
77 } // namespace Rosen
78 } // namespace OHOS
79 #endif // TOUCH_SCREEN_H