xref: /aosp_15_r20/external/drm_hwcomposer/drm/UEventListener.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
2  * Copyright (C) 2016 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 #define LOG_TAG "drmhwc"
18 
19 #include "UEventListener.h"
20 
21 #include <thread>
22 
23 #include "utils/log.h"
24 
25 namespace android {
26 
StopThread()27 void UEventListener::StopThread() {
28   uevent_->Stop();
29 }
30 
CreateInstance()31 auto UEventListener::CreateInstance() -> std::shared_ptr<UEventListener> {
32   auto uel = std::shared_ptr<UEventListener>(new UEventListener());
33 
34   uel->uevent_ = UEvent::CreateInstance();
35   if (!uel->uevent_)
36     return {};
37 
38   std::thread(&UEventListener::ThreadFn, uel.get(), uel).detach();
39 
40   return uel;
41 }
42 
ThreadFn(const std::shared_ptr<UEventListener> & uel)43 void UEventListener::ThreadFn(const std::shared_ptr<UEventListener> &uel) {
44   // TODO(nobody): Rework code to allow stopping the thread (low priority)
45   while (true) {
46     if (uel.use_count() == 1)
47       break;
48 
49     auto uevent_str = uel->uevent_->ReadNext();
50 
51     if (!hotplug_handler_ || !uevent_str)
52       continue;
53 
54     auto drm_event = uevent_str->find("DEVTYPE=drm_minor") != std::string::npos;
55     auto hotplug_event = uevent_str->find("HOTPLUG=1") != std::string::npos;
56 
57     if (drm_event && hotplug_event) {
58       constexpr useconds_t kDelayAfterUeventUs = 200000;
59       /* We need some delay to ensure DrmConnector::UpdateModes() will query
60        * correct modes list, otherwise at least RPI4 board may report 0 modes */
61       usleep(kDelayAfterUeventUs);
62       hotplug_handler_();
63     }
64   }
65 
66   ALOGI("UEvent thread exit");
67 }
68 
69 }  // namespace android
70