Skip to content

Commit 97d6ef2

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "EventHub: Pull out device change handling from getEvents" into main
2 parents e20c771 + b469313 commit 97d6ef2

File tree

2 files changed

+82
-78
lines changed

2 files changed

+82
-78
lines changed

services/inputflinger/reader/EventHub.cpp

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,84 +1899,7 @@ std::vector<RawEvent> EventHub::getEvents(int timeoutMillis) {
18991899

19001900
handleSysfsNodeChangeNotificationsLocked();
19011901

1902-
// Use a do-while loop to ensure that we drain the closing and opening devices loop
1903-
// at least once, even if there are no devices to re-open.
1904-
do {
1905-
if (!mDeviceIdsToReopen.empty()) {
1906-
// If there are devices that need to be re-opened, ensure that we re-open them
1907-
// one at a time to send the DEVICE_REMOVED and DEVICE_ADDED notifications for
1908-
// each before moving on to the next. This is to avoid notifying all device
1909-
// removals and additions in one batch, which could cause additional unnecessary
1910-
// device added/removed notifications for merged InputDevices from InputReader.
1911-
const int32_t deviceId = mDeviceIdsToReopen.back();
1912-
mDeviceIdsToReopen.erase(mDeviceIdsToReopen.end() - 1);
1913-
if (auto it = mDevices.find(deviceId); it != mDevices.end()) {
1914-
ALOGI("Reopening input device: id=%d, name=%s", it->second->id,
1915-
it->second->identifier.name.c_str());
1916-
const auto path = it->second->path;
1917-
closeDeviceLocked(*it->second);
1918-
openDeviceLocked(path);
1919-
}
1920-
}
1921-
1922-
// Report any devices that had last been added/removed.
1923-
for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();) {
1924-
std::unique_ptr<Device> device = std::move(*it);
1925-
ALOGV("Reporting device closed: id=%d, name=%s\n", device->id,
1926-
device->path.c_str());
1927-
const int32_t deviceId = (device->id == mBuiltInKeyboardId)
1928-
? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID
1929-
: device->id;
1930-
events.push_back({
1931-
.when = now,
1932-
.deviceId = deviceId,
1933-
.type = DEVICE_REMOVED,
1934-
});
1935-
it = mClosingDevices.erase(it);
1936-
if (events.size() == EVENT_BUFFER_SIZE) {
1937-
break;
1938-
}
1939-
}
1940-
1941-
if (mNeedToScanDevices) {
1942-
mNeedToScanDevices = false;
1943-
scanDevicesLocked();
1944-
}
1945-
1946-
while (!mOpeningDevices.empty()) {
1947-
std::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin());
1948-
mOpeningDevices.pop_back();
1949-
ALOGV("Reporting device opened: id=%d, name=%s\n", device->id,
1950-
device->path.c_str());
1951-
const int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
1952-
events.push_back({
1953-
.when = now,
1954-
.deviceId = deviceId,
1955-
.type = DEVICE_ADDED,
1956-
});
1957-
1958-
// Try to find a matching video device by comparing device names
1959-
for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();
1960-
it++) {
1961-
std::unique_ptr<TouchVideoDevice>& videoDevice = *it;
1962-
if (tryAddVideoDeviceLocked(*device, videoDevice)) {
1963-
// videoDevice was transferred to 'device'
1964-
it = mUnattachedVideoDevices.erase(it);
1965-
break;
1966-
}
1967-
}
1968-
1969-
auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device));
1970-
if (!inserted) {
1971-
ALOGW("Device id %d exists, replaced.", device->id);
1972-
}
1973-
if (events.size() == EVENT_BUFFER_SIZE) {
1974-
break;
1975-
}
1976-
}
1977-
1978-
// Perform this loop of re-opening devices so that we re-open one device at a time.
1979-
} while (!mDeviceIdsToReopen.empty());
1902+
handleDeviceChangesLocked(events, now);
19801903

19811904
if (events.size() == EVENT_BUFFER_SIZE) {
19821905
break;
@@ -2157,6 +2080,85 @@ std::vector<RawEvent> EventHub::getEvents(int timeoutMillis) {
21572080
return events;
21582081
}
21592082

2083+
void EventHub::handleDeviceChangesLocked(std::vector<RawEvent>& events, nsecs_t now) {
2084+
// Use a do-while loop to ensure that we drain the closing and opening devices
2085+
// at least once, even if there are no devices to re-open.
2086+
do {
2087+
if (!mDeviceIdsToReopen.empty()) {
2088+
// If there are devices that need to be re-opened, ensure that we re-open them
2089+
// one at a time to send the DEVICE_REMOVED and DEVICE_ADDED notifications for
2090+
// each before moving on to the next. This is to avoid notifying all device
2091+
// removals and additions in one batch, which could cause additional unnecessary
2092+
// device added/removed notifications for merged InputDevices from InputReader.
2093+
const int32_t deviceId = mDeviceIdsToReopen.back();
2094+
mDeviceIdsToReopen.erase(mDeviceIdsToReopen.end() - 1);
2095+
if (auto it = mDevices.find(deviceId); it != mDevices.end()) {
2096+
ALOGI("Reopening input device: id=%d, name=%s", it->second->id,
2097+
it->second->identifier.name.c_str());
2098+
const auto path = it->second->path;
2099+
closeDeviceLocked(*it->second);
2100+
openDeviceLocked(path);
2101+
}
2102+
}
2103+
2104+
// Report any devices that had last been added/removed.
2105+
for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();) {
2106+
std::unique_ptr<Device> device = std::move(*it);
2107+
ALOGV("Reporting device closed: id=%d, name=%s\n", device->id, device->path.c_str());
2108+
const int32_t deviceId = (device->id == mBuiltInKeyboardId)
2109+
? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID
2110+
: device->id;
2111+
events.push_back({
2112+
.when = now,
2113+
.deviceId = deviceId,
2114+
.type = DEVICE_REMOVED,
2115+
});
2116+
it = mClosingDevices.erase(it);
2117+
if (events.size() == EVENT_BUFFER_SIZE) {
2118+
break;
2119+
}
2120+
}
2121+
2122+
if (mNeedToScanDevices) {
2123+
mNeedToScanDevices = false;
2124+
scanDevicesLocked();
2125+
}
2126+
2127+
while (!mOpeningDevices.empty()) {
2128+
std::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin());
2129+
mOpeningDevices.pop_back();
2130+
ALOGV("Reporting device opened: id=%d, name=%s\n", device->id, device->path.c_str());
2131+
const int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
2132+
events.push_back({
2133+
.when = now,
2134+
.deviceId = deviceId,
2135+
.type = DEVICE_ADDED,
2136+
});
2137+
2138+
// Try to find a matching video device by comparing device names
2139+
for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();
2140+
it++) {
2141+
std::unique_ptr<TouchVideoDevice>& videoDevice = *it;
2142+
if (tryAddVideoDeviceLocked(*device, videoDevice)) {
2143+
// videoDevice was transferred to 'device'
2144+
it = mUnattachedVideoDevices.erase(it);
2145+
break;
2146+
}
2147+
}
2148+
2149+
auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device));
2150+
if (!inserted) {
2151+
ALOGW("Device id %d exists, replaced.", device->id);
2152+
}
2153+
if (events.size() == EVENT_BUFFER_SIZE) {
2154+
break;
2155+
}
2156+
}
2157+
2158+
// Perform this loop of re-opening devices so that we re-open one device at a time.
2159+
} while (!mDeviceIdsToReopen.empty());
2160+
}
2161+
21602162
std::vector<TouchVideoFrame> EventHub::getVideoFrames(int32_t deviceId) {
21612163
std::scoped_lock _l(mLock);
21622164

services/inputflinger/reader/include/EventHub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ class EventHub : public EventHubInterface {
783783

784784
void handleSysfsNodeChangeNotificationsLocked() REQUIRES(mLock);
785785

786+
void handleDeviceChangesLocked(std::vector<RawEvent>& events, nsecs_t now) REQUIRES(mLock);
787+
786788
// Protect all internal state.
787789
mutable std::mutex mLock;
788790

0 commit comments

Comments
 (0)