blob: 2910ba44c8e98e579f4d2cba0e9e65cd5ddc19c1 [file] [log] [blame]
Vishnu Nairc519ff72021-01-21 08:23:08 -08001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stdint.h>
20#include <optional>
21#include <unordered_map>
22
chaviw98318de2021-05-19 16:45:23 -050023#include <android/gui/FocusRequest.h>
Vishnu Nairc519ff72021-01-21 08:23:08 -080024#include <binder/Binder.h>
chaviw98318de2021-05-19 16:45:23 -050025#include <gui/WindowInfo.h>
Vishnu Nairc519ff72021-01-21 08:23:08 -080026
27namespace android::inputdispatcher {
28
29// Keeps track of the focused window per display. The class listens to updates from input dispatcher
30// and provides focus changes.
31//
32// Focus Policy
33// Window focusabilty - A window token can be focused if there is at least one window handle that
34// is visible with the same token and all window handles with the same token are focusable.
35// See FocusResolver::isTokenFocusable
36//
Vishnu Nair1dcad982021-02-24 14:38:25 -080037// Focus request - Request will be granted if the window is focusable. If it's not
38// focusable, then the request is persisted and granted when it becomes focusable. The currently
39// focused window will lose focus and any pending keys will be added to a queue so it can be sent
40// to the window when it gets focus.
41//
42// Condition focus request - Request with a focus token specified. Request will be granted if the
43// window is focusable and the focus token is the currently focused. Otherwise, the request is
44// dropped. Conditional focus requests are not persisted. The window will lose focus and go back
45// to the focus token if it becomes not focusable.
Vishnu Nairc519ff72021-01-21 08:23:08 -080046//
47// Window handle updates - Focus is lost when the currently focused window becomes not focusable.
Vishnu Nair1dcad982021-02-24 14:38:25 -080048// If the previous focus request is focusable, then we will try to grant that window focus.
Vishnu Nairc519ff72021-01-21 08:23:08 -080049class FocusResolver {
50public:
51 // Returns the focused window token on the specified display.
Linnan Li13bf76a2024-05-05 19:18:02 +080052 sp<IBinder> getFocusedWindowToken(ui::LogicalDisplayId displayId) const;
Vishnu Nairc519ff72021-01-21 08:23:08 -080053
54 struct FocusChanges {
55 sp<IBinder> oldFocus;
56 sp<IBinder> newFocus;
Linnan Li13bf76a2024-05-05 19:18:02 +080057 ui::LogicalDisplayId displayId;
Vishnu Nairc519ff72021-01-21 08:23:08 -080058 std::string reason;
59 };
60 std::optional<FocusResolver::FocusChanges> setInputWindows(
Linnan Li13bf76a2024-05-05 19:18:02 +080061 ui::LogicalDisplayId displayId,
62 const std::vector<sp<android::gui::WindowInfoHandle>>& windows);
Vishnu Nairc519ff72021-01-21 08:23:08 -080063 std::optional<FocusResolver::FocusChanges> setFocusedWindow(
chaviw98318de2021-05-19 16:45:23 -050064 const android::gui::FocusRequest& request,
65 const std::vector<sp<android::gui::WindowInfoHandle>>& windows);
Vishnu Nairc519ff72021-01-21 08:23:08 -080066
Vishnu Nair599f1412021-06-21 10:39:58 -070067 // Display has been removed from the system, clean up old references.
Linnan Li13bf76a2024-05-05 19:18:02 +080068 void displayRemoved(ui::LogicalDisplayId displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -070069
Vishnu Nairc519ff72021-01-21 08:23:08 -080070 // exposed for debugging
71 bool hasFocusedWindowTokens() const { return !mFocusedWindowTokenByDisplay.empty(); }
72 std::string dumpFocusedWindows() const;
73 std::string dump() const;
74
75private:
Vishnu Nair1dcad982021-02-24 14:38:25 -080076 enum class Focusability {
Vishnu Nairc519ff72021-01-21 08:23:08 -080077 OK,
78 NO_WINDOW,
79 NOT_FOCUSABLE,
80 NOT_VISIBLE,
Dominik Laskowski75788452021-02-09 18:51:25 -080081
82 ftl_last = NOT_VISIBLE
Vishnu Nairc519ff72021-01-21 08:23:08 -080083 };
84
85 // Checks if the window token can be focused on a display. The token can be focused if there is
86 // at least one window handle that is visible with the same token and all window handles with
87 // the same token are focusable.
88 //
89 // In the case of mirroring, two windows may share the same window token and their visibility
90 // might be different. Example, the mirrored window can cover the window its mirroring. However,
91 // we expect the focusability of the windows to match since its hard to reason why one window
92 // can receive focus events and the other cannot when both are backed by the same input channel.
93 //
chaviw98318de2021-05-19 16:45:23 -050094 static Focusability isTokenFocusable(
95 const sp<IBinder>& token,
Chavi Weingarten847e8512023-03-29 00:26:09 +000096 const std::vector<sp<android::gui::WindowInfoHandle>>& windows,
97 sp<android::gui::WindowInfoHandle>& outFocusableWindow);
98
99 static FocusResolver::Focusability getResolvedFocusWindow(
100 const sp<IBinder>& token,
101 const std::vector<sp<android::gui::WindowInfoHandle>>& windows,
102 sp<android::gui::WindowInfoHandle>& outFocusableWindow);
Vishnu Nairc519ff72021-01-21 08:23:08 -0800103
104 // Focus tracking for keys, trackball, etc. A window token can be associated with one or
105 // more InputWindowHandles. If a window is mirrored, the window and its mirror will share
106 // the same token. Focus is tracked by the token per display and the events are dispatched
107 // to the channel associated by this token.
108 typedef std::pair<std::string /* name */, sp<IBinder>> NamedToken;
Linnan Li13bf76a2024-05-05 19:18:02 +0800109 std::unordered_map<ui::LogicalDisplayId /* displayId */, NamedToken>
110 mFocusedWindowTokenByDisplay;
Vishnu Nairc519ff72021-01-21 08:23:08 -0800111
Vishnu Nair1dcad982021-02-24 14:38:25 -0800112 // This map will store the focus request per display. When the input window handles are updated,
113 // the current request will be checked to see if it can be processed at that time.
Linnan Li13bf76a2024-05-05 19:18:02 +0800114 std::unordered_map<ui::LogicalDisplayId /* displayId */, android::gui::FocusRequest>
115 mFocusRequestByDisplay;
Vishnu Nair1dcad982021-02-24 14:38:25 -0800116
117 // Last reason for not granting a focus request. This is used to add more debug information
118 // in the event logs.
Linnan Li13bf76a2024-05-05 19:18:02 +0800119 std::unordered_map<ui::LogicalDisplayId /* displayId */, Focusability>
120 mLastFocusResultByDisplay;
Vishnu Nairc519ff72021-01-21 08:23:08 -0800121
122 std::optional<FocusResolver::FocusChanges> updateFocusedWindow(
Linnan Li13bf76a2024-05-05 19:18:02 +0800123 ui::LogicalDisplayId displayId, const std::string& reason, const sp<IBinder>& token,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800124 const std::string& tokenName = "");
Linnan Li13bf76a2024-05-05 19:18:02 +0800125 std::optional<android::gui::FocusRequest> getFocusRequest(ui::LogicalDisplayId displayId);
Vishnu Nairc519ff72021-01-21 08:23:08 -0800126};
127
Dominik Laskowski75788452021-02-09 18:51:25 -0800128} // namespace android::inputdispatcher