2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwc-drm-event-listener"
19 #include "drmeventlistener.h"
20 #include "drmresources.h"
22 #include <linux/netlink.h>
23 #include <sys/socket.h>
25 #include <cutils/log.h>
30 DrmEventListener::DrmEventListener(DrmResources *drm)
31 : Worker("drm-event-listener", HAL_PRIORITY_URGENT_DISPLAY),
35 int DrmEventListener::Init() {
36 uevent_fd_.Set(socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT));
37 if (uevent_fd_.get() < 0) {
38 ALOGE("Failed to open uevent socket %d", uevent_fd_.get());
39 return uevent_fd_.get();
42 struct sockaddr_nl addr;
43 memset(&addr, 0, sizeof(addr));
44 addr.nl_family = AF_NETLINK;
45 addr.nl_pid = getpid();
46 addr.nl_groups = 0xFFFFFFFF;
48 int ret = bind(uevent_fd_.get(), (struct sockaddr *)&addr, sizeof(addr));
50 ALOGE("Failed to bind uevent socket %d", -errno);
55 FD_SET(drm_->fd(), &fds_);
56 FD_SET(uevent_fd_.get(), &fds_);
57 max_fd_ = std::max(drm_->fd(), uevent_fd_.get());
62 void DrmEventListener::RegisterHotplugHandler(DrmEventHandler *handler) {
63 assert(!hotplug_handler_);
64 hotplug_handler_ = handler;
67 void DrmEventListener::FlipHandler(int /* fd */, unsigned int /* sequence */,
68 unsigned int tv_sec, unsigned int tv_usec,
70 DrmEventHandler *handler = (DrmEventHandler *)user_data;
74 handler->HandleEvent((uint64_t)tv_sec * 1000 * 1000 + tv_usec);
78 void DrmEventListener::UEventHandler() {
83 uint64_t timestamp = 0;
84 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
86 timestamp = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
88 ALOGE("Failed to get monotonic clock on hotplug %d", ret);
91 ret = read(uevent_fd_.get(), &buffer, sizeof(buffer));
95 ALOGE("Got error reading uevent %d", ret);
99 if (!hotplug_handler_)
102 bool drm_event = false, hotplug_event = false;
103 for (int i = 0; i < ret;) {
104 char *event = buffer + i;
105 if (strcmp(event, "DEVTYPE=drm_minor"))
107 else if (strcmp(event, "HOTPLUG=1"))
108 hotplug_event = true;
110 i += strlen(event) + 1;
113 if (drm_event && hotplug_event)
114 hotplug_handler_->HandleEvent(timestamp);
118 void DrmEventListener::Routine() {
121 ret = select(max_fd_ + 1, &fds_, NULL, NULL, NULL);
122 } while (ret == -1 && errno == EINTR);
124 if (FD_ISSET(drm_->fd(), &fds_)) {
125 drmEventContext event_context = {
126 .version = DRM_EVENT_CONTEXT_VERSION,
127 .vblank_handler = NULL,
128 .page_flip_handler = DrmEventListener::FlipHandler};
129 drmHandleEvent(drm_->fd(), &event_context);
132 if (FD_ISSET(uevent_fd_.get(), &fds_))