From de44994dfcd433afbbf6d867a8dc6f4d0979b2f6 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 3 Jun 2017 15:47:33 +1200 Subject: [PATCH 1/2] Udev event device matching --- backend/drm/backend.c | 23 ++++++++++++++++++++--- backend/drm/drm.c | 1 - backend/udev.c | 15 ++------------- include/backend/udev.h | 1 - 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/backend/drm/backend.c b/backend/drm/backend.c index bceb21840..73ccf4139 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -30,7 +31,6 @@ static void wlr_drm_backend_destroy(struct wlr_backend_state *state) { } wlr_drm_renderer_free(&state->renderer); wlr_session_close_file(state->session, state->fd); - wlr_session_finish(state->session); wl_event_source_remove(state->drm_event); free(state); } @@ -73,14 +73,31 @@ static void device_resumed(struct wl_listener *listener, void *data) { } } -static void drm_invalidated(struct wl_listener *listener, void *state) { +static void drm_invalidated(struct wl_listener *listener, void *data) { struct wlr_backend_state *drm = wl_container_of(listener, drm, drm_invalidated); - wlr_drm_scan_connectors(drm); + dev_t *dev = data; + + if (drm->dev == *dev) { + char *name = drmGetDeviceNameFromFd2(drm->fd); + wlr_log(L_DEBUG, "%s invalidated", name); + free(name); + + wlr_drm_scan_connectors(drm); + } } struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, struct wlr_session *session, struct wlr_udev *udev, int gpu_fd) { assert(display && session && gpu_fd > 0); + + char *name = drmGetDeviceNameFromFd2(gpu_fd); + drmVersion *version = drmGetVersion(gpu_fd); + + wlr_log(L_INFO, "Initalizing DRM backend for %s (%s)", name, version->name); + + free(name); + drmFreeVersion(version); + struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); if (!state) { wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 483c923c0..27a5932f0 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -276,7 +276,6 @@ static void wlr_drm_output_enable(struct wlr_output_state *output, bool enable) static void wlr_drm_output_destroy(struct wlr_output_state *output) { wlr_drm_output_cleanup(output, true); - wlr_drm_renderer_free(output->renderer); free(output); } diff --git a/backend/udev.c b/backend/udev.c index 30c24e495..f9577c125 100644 --- a/backend/udev.c +++ b/backend/udev.c @@ -73,7 +73,6 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) { struct udev_list_entry *entry; int fd = -1; - char *drm_path = NULL; udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(en)) { bool is_boot_vga = false; @@ -117,9 +116,6 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) { continue; } - free(drm_path); - drm_path = strdup(path); - udev_device_unref(dev); // We've found the primary GPU @@ -130,7 +126,6 @@ int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) { udev_enumerate_unref(en); - udev->drm_path = drm_path; return fd; } @@ -143,21 +138,16 @@ static int udev_event(int fd, uint32_t mask, void *data) { } const char *action = udev_device_get_action(dev); - const char *path = udev_device_get_devnode(dev); wlr_log(L_DEBUG, "udev event for %s (%s)", udev_device_get_sysname(dev), action); - if (!path || strcmp(path, udev->drm_path) != 0) { - goto out; - } - if (!action || strcmp(action, "change") != 0) { goto out; } - // TODO: Specify the GPU that's being invalidated - wl_signal_emit(&udev->invalidate_drm, udev); + dev_t devnum = udev_device_get_devnum(dev); + wl_signal_emit(&udev->invalidate_drm, &devnum); out: udev_device_unref(dev); @@ -216,5 +206,4 @@ void wlr_udev_destroy(struct wlr_udev *udev) { udev_monitor_unref(udev->mon); udev_unref(udev->udev); - free(udev->drm_path); } diff --git a/include/backend/udev.h b/include/backend/udev.h index c50642798..0ff35bfa8 100644 --- a/include/backend/udev.h +++ b/include/backend/udev.h @@ -9,7 +9,6 @@ struct wlr_udev { struct udev *udev; struct udev_monitor *mon; - char *drm_path; struct wl_event_source *event; struct wl_signal invalidate_drm; }; From 5df56653abdef714d0a92ff88b13b9066f3167fc Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 4 Jun 2017 17:43:34 +1200 Subject: [PATCH 2/2] Changed invalidate interface. --- backend/drm/backend.c | 22 ++++++++++--------- backend/udev.c | 50 +++++++++++++++++++++++++++++++++++++++--- include/backend/udev.h | 13 ++++++++++- 3 files changed, 71 insertions(+), 14 deletions(-) diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 73ccf4139..2840f17f3 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -29,6 +29,7 @@ static void wlr_drm_backend_destroy(struct wlr_backend_state *state) { struct wlr_output_state *output = state->outputs->items[i]; wlr_output_destroy(output->wlr_output); } + wlr_udev_signal_remove(state->udev, &state->drm_invalidated); wlr_drm_renderer_free(&state->renderer); wlr_session_close_file(state->session, state->fd); wl_event_source_remove(state->drm_event); @@ -75,15 +76,15 @@ static void device_resumed(struct wl_listener *listener, void *data) { static void drm_invalidated(struct wl_listener *listener, void *data) { struct wlr_backend_state *drm = wl_container_of(listener, drm, drm_invalidated); - dev_t *dev = data; + struct wlr_udev *udev = data; - if (drm->dev == *dev) { - char *name = drmGetDeviceNameFromFd2(drm->fd); - wlr_log(L_DEBUG, "%s invalidated", name); - free(name); + (void)udev; - wlr_drm_scan_connectors(drm); - } + char *name = drmGetDeviceNameFromFd2(drm->fd); + wlr_log(L_DEBUG, "%s invalidated", name); + free(name); + + wlr_drm_scan_connectors(drm); } struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, @@ -112,6 +113,7 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, state->backend = backend; state->session = session; + state->udev = udev; state->outputs = list_create(); if (!state->outputs) { wlr_log(L_ERROR, "Failed to allocate list"); @@ -119,9 +121,6 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, } state->fd = gpu_fd; - wl_list_init(&state->drm_invalidated.link); - state->drm_invalidated.notify = drm_invalidated; - wl_signal_add(&udev->invalidate_drm, &state->drm_invalidated); struct stat st; if (fstat(state->fd, &st) < 0) { @@ -129,6 +128,9 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, } state->dev = st.st_rdev; + state->drm_invalidated.notify = drm_invalidated; + wlr_udev_signal_add(udev, state->dev, &state->drm_invalidated); + struct wl_event_loop *event_loop = wl_display_get_event_loop(display); state->drm_event = wl_event_loop_add_fd(event_loop, state->fd, diff --git a/backend/udev.c b/backend/udev.c index f9577c125..fb2133394 100644 --- a/backend/udev.c +++ b/backend/udev.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -147,7 +148,14 @@ static int udev_event(int fd, uint32_t mask, void *data) { } dev_t devnum = udev_device_get_devnum(dev); - wl_signal_emit(&udev->invalidate_drm, &devnum); + struct wlr_udev_dev *signal; + + wl_list_for_each(signal, &udev->devices, link) { + if (signal->dev == devnum) { + wl_signal_emit(&signal->invalidate, udev); + break; + } + } out: udev_device_unref(dev); @@ -164,7 +172,6 @@ struct wlr_udev *wlr_udev_create(struct wl_display *display) { wlr_log(L_ERROR, "Failed to create udev context"); goto error; } - wl_signal_init(&udev->invalidate_drm); udev->mon = udev_monitor_new_from_netlink(udev->udev, "udev"); if (!udev->mon) { @@ -185,6 +192,8 @@ struct wlr_udev *wlr_udev_create(struct wl_display *display) { goto error_mon; } + wl_list_init(&udev->devices); + wlr_log(L_DEBUG, "Successfully initialized udev"); return udev; @@ -202,8 +211,43 @@ void wlr_udev_destroy(struct wlr_udev *udev) { return; } - wl_event_source_remove(udev->event); + struct wlr_udev_dev *dev, *tmp; + wl_list_for_each_safe(dev, tmp, &udev->devices, link) { + free(dev); + } + wl_event_source_remove(udev->event); udev_monitor_unref(udev->mon); udev_unref(udev->udev); } + +bool wlr_udev_signal_add(struct wlr_udev *udev, dev_t dev, struct wl_listener *listener) { + struct wlr_udev_dev *device = malloc(sizeof(*device)); + if (!device) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return false; + } + + device->dev = dev; + wl_signal_init(&device->invalidate); + wl_signal_add(&device->invalidate, listener); + wl_list_insert(&udev->devices, &device->link); + + return true; +} + +void wlr_udev_signal_remove(struct wlr_udev *udev, struct wl_listener *listener) { + if (!udev || !listener) { + return; + } + + struct wlr_udev_dev *dev, *tmp; + wl_list_for_each_safe(dev, tmp, &udev->devices, link) { + // The signal should only have a single listener + if (wl_signal_get(&dev->invalidate, listener->notify) != NULL) { + wl_list_remove(&dev->link); + free(dev); + return; + } + } +} diff --git a/include/backend/udev.h b/include/backend/udev.h index 0ff35bfa8..a2834063a 100644 --- a/include/backend/udev.h +++ b/include/backend/udev.h @@ -1,18 +1,29 @@ #ifndef _WLR_INTERNAL_UDEV_H #define _WLR_INTERNAL_UDEV_H +#include #include #include #include #include +struct wlr_udev_dev { + dev_t dev; + struct wl_signal invalidate; + + struct wl_list link; +}; + struct wlr_udev { struct udev *udev; struct udev_monitor *mon; struct wl_event_source *event; - struct wl_signal invalidate_drm; + + struct wl_list devices; }; int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session); +bool wlr_udev_signal_add(struct wlr_udev *udev, dev_t dev, struct wl_listener *listener); +void wlr_udev_signal_remove(struct wlr_udev *udev, struct wl_listener *listener); #endif