• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 SERVICES_INCLUDE_CLIENT_GROUP_H
17 #define SERVICES_INCLUDE_CLIENT_GROUP_H
18 
19 #include <map>
20 #include <utility>
21 
22 #include "input_client_info.h"
23 #include "input_death_recipient.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
27 enum ClientAddEvent : int32_t {
28     PREPARE_INPUT = 0,
29     START_LISTENING,
30 };
31 
32 class ClientGroup {
33 public:
34     using ClientDiedHandler = std::function<void(const sptr<IInputClient> &)>;
ClientGroup(uint64_t displayGroupId,ClientDiedHandler diedHandler)35     ClientGroup(uint64_t displayGroupId, ClientDiedHandler diedHandler)
36         : displayGroupId_(displayGroupId), clientDiedHandler_(std::move(diedHandler))
37     {
38     }
39 
40     uint64_t GetDisplayGroupId();
41     int32_t AddClientInfo(
42         const sptr<IRemoteObject> &inputClient, const InputClientInfo &clientInfo, ClientAddEvent event);
43     void RemoveClientInfo(const sptr<IRemoteObject> &client, bool isClientDied = false);
44     void UpdateClientInfo(const sptr<IRemoteObject> &client,
45         const std::unordered_map<UpdateFlag,
46             std::variant<bool, uint32_t, ImeType, ClientState, TextTotalConfig, ClientType, pid_t>> &updateInfos);
47 
48     std::shared_ptr<InputClientInfo> GetClientInfo(sptr<IRemoteObject> inputClient);
49     std::shared_ptr<InputClientInfo> GetClientInfo(pid_t pid);
50     std::shared_ptr<InputClientInfo> GetCurrentClientInfo();
51     int64_t GetCurrentClientPid();
52     int64_t GetInactiveClientPid();
53 
54     bool IsClientExist(sptr<IRemoteObject> inputClient);
55     bool IsNotifyInputStop(const sptr<IInputClient> &client);
56 
57     sptr<IInputClient> GetCurrentClient();
58     void SetCurrentClient(sptr<IInputClient> client);
59     sptr<IInputClient> GetInactiveClient();
60     void SetInactiveClient(sptr<IInputClient> client);
61 
62     bool IsCurClientFocused(int32_t pid, int32_t uid);
63     bool IsCurClientUnFocused(int32_t pid, int32_t uid);
64 
65     // from service notify clients
66     int32_t NotifyInputStartToClients(uint32_t callingWndId, int32_t requestKeyboardReason = 0);
67     int32_t NotifyInputStopToClients();
68     int32_t NotifyPanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info);
69     int32_t NotifyImeChangeToClients(const Property &property, const SubProperty &subProperty);
70 
71 private:
72     std::map<sptr<IRemoteObject>, std::shared_ptr<InputClientInfo>> GetClientMap();
73     bool IsSameClient(sptr<IInputClient> source, sptr<IInputClient> dest);
74     void OnClientDied(sptr<IInputClient> remote);
75     uint64_t displayGroupId_{ DEFAULT_DISPLAY_ID };
76 
77     std::recursive_mutex mtx_;
78     std::map<sptr<IRemoteObject>, std::shared_ptr<InputClientInfo>> mapClients_;
79 
80     std::mutex currentClientLock_{};
81     sptr<IInputClient> currentClient_; // the current input client
82 
83     std::mutex inactiveClientLock_{};
84     sptr<IInputClient> inactiveClient_; // the inactive input client
85 
86     std::mutex clientDiedLock_{};
87     ClientDiedHandler clientDiedHandler_;
88 };
89 } // namespace MiscServices
90 } // namespace OHOS
91 #endif // SERVICES_INCLUDE_CLIENT_GROUP_H
92