1 /*
2  * Copyright (c) 2020-2021 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 GRAPHIC_LITE_INPUT_MANAGER_SERVICE_H
17 #define GRAPHIC_LITE_INPUT_MANAGER_SERVICE_H
18 
19 #include <pthread.h>
20 #include <queue>
21 #include "input_event_distributer.h"
22 #include "input_event_hub.h"
23 
24 namespace OHOS {
25 /** @brief Manager service of input event. */
26 class InputManagerService {
27 public:
28     /**
29      * @brief Get instance of input manager service.
30      *
31      * @returns instance of input manager service
32      */
33     static InputManagerService* GetInstance();
34 
35     /**
36      * Init input manager service.
37      */
38     static void Run();
39 
40     /**
41      * Delete input manager service.
42      */
43     static void Stop();
44 
45     /**
46      * @brief Distribute task function.
47      *
48      * @param args no use
49      */
50     static void* Distribute(void* args);
51 
52     /**
53      * @brief Get input event distributer in service.
54      *
55      * @returns input event distributer
56      */
GetDistributer()57     InputEventDistributer* GetDistributer()
58     {
59         return &distributer_;
60     }
61 
62     /**
63      * @brief Get input event hub in service.
64      *
65      * @returns input event hub
66      */
GetHub()67     InputEventHub* GetHub()
68     {
69         return hub_;
70     }
71 
72 private:
73     static void ReadCallback(const RawEvent* event);
InputManagerService()74     InputManagerService()
75     {
76         pthread_mutex_init(&lock_, nullptr);
77         pthread_cond_init(&nonEmpty_, nullptr);
78         pthread_cond_init(&nonFull_, nullptr);
79     }
80     ~InputManagerService();
81 
82     InputManagerService(const InputManagerService&) = delete;
83     InputManagerService& operator=(const InputManagerService&) = delete;
84     InputManagerService(InputManagerService&&) = delete;
85     InputManagerService& operator=(InputManagerService&&) = delete;
86 
87     static InputEventHub* hub_;
88     static InputEventDistributer distributer_;
89     static pthread_t distributerThread_;
90     static int32_t distributerThreadCreated_;
91     static std::queue<RawEvent> eventQueue_;
92     static pthread_mutex_t lock_;
93     static pthread_cond_t nonEmpty_;
94     static pthread_cond_t nonFull_;
95 };
96 } // namespace OHOS
97 #endif // GRAPHIC_LITE_INPUT_MANAGER_SERVICE_H
98