Daniel Vetter | 7f7cafe | 2012-01-24 10:50:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Intel Corporation |
| 3 | * Jesse Barnes <jesse.barnes@intel.com> |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
Daniel Vetter | 7f7cafe | 2012-01-24 10:50:05 +0100 | [diff] [blame] | 27 | |
| 28 | #include "testdisplay.h" |
Daniel Vetter | fa46120 | 2012-01-24 11:28:25 +0100 | [diff] [blame^] | 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include "config.h" |
| 31 | #endif |
Daniel Vetter | 7f7cafe | 2012-01-24 10:50:05 +0100 | [diff] [blame] | 32 | |
Daniel Vetter | fa46120 | 2012-01-24 11:28:25 +0100 | [diff] [blame^] | 33 | #if HAVE_UDEV |
| 34 | #include <libudev.h> |
Daniel Vetter | 7f7cafe | 2012-01-24 10:50:05 +0100 | [diff] [blame] | 35 | static struct udev_monitor *uevent_monitor; |
| 36 | static struct udev *udev; |
| 37 | static GIOChannel *udevchannel; |
| 38 | |
| 39 | static gboolean hotplug_event(GIOChannel *source, GIOCondition condition, |
| 40 | gpointer data) |
| 41 | { |
| 42 | struct udev_device *dev; |
| 43 | dev_t udev_devnum; |
| 44 | struct stat s; |
| 45 | const char *hotplug; |
| 46 | |
| 47 | dev = udev_monitor_receive_device(uevent_monitor); |
| 48 | if (!dev) |
| 49 | goto out; |
| 50 | |
| 51 | udev_devnum = udev_device_get_devnum(dev); |
| 52 | fstat(drm_fd, &s); |
| 53 | |
| 54 | hotplug = udev_device_get_property_value(dev, "HOTPLUG"); |
| 55 | |
| 56 | if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 && |
| 57 | hotplug && atoi(hotplug) == 1) |
| 58 | update_display(); |
| 59 | |
| 60 | udev_device_unref(dev); |
| 61 | out: |
| 62 | return TRUE; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | gboolean testdisplay_setup_hotplug(void) |
| 67 | { |
| 68 | int ret; |
| 69 | |
| 70 | udev = udev_new(); |
| 71 | if (!udev) { |
| 72 | fprintf(stderr, "failed to create udev object\n"); |
| 73 | goto out; |
| 74 | } |
| 75 | |
| 76 | uevent_monitor = udev_monitor_new_from_netlink(udev, "udev"); |
| 77 | if (!uevent_monitor) { |
| 78 | fprintf(stderr, "failed to create udev event monitor\n"); |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor, |
| 83 | "drm", |
| 84 | "drm_minor"); |
| 85 | if (ret < 0) { |
| 86 | fprintf(stderr, "failed to filter for drm events\n"); |
| 87 | goto out; |
| 88 | } |
| 89 | |
| 90 | ret = udev_monitor_enable_receiving(uevent_monitor); |
| 91 | if (ret < 0) { |
| 92 | fprintf(stderr, "failed to enable udev event reception\n"); |
| 93 | goto out; |
| 94 | } |
| 95 | |
| 96 | udevchannel = |
| 97 | g_io_channel_unix_new(udev_monitor_get_fd(uevent_monitor)); |
| 98 | if (!udevchannel) { |
| 99 | fprintf(stderr, "failed to create udev GIO channel\n"); |
| 100 | goto out; |
| 101 | } |
| 102 | |
| 103 | ret = g_io_add_watch(udevchannel, G_IO_IN | G_IO_ERR, hotplug_event, |
| 104 | udev); |
| 105 | if (ret < 0) { |
| 106 | fprintf(stderr, "failed to add watch on udev GIO channel\n"); |
| 107 | goto out; |
| 108 | } |
| 109 | |
| 110 | return TRUE; |
| 111 | |
| 112 | out: |
| 113 | testdisplay_cleanup_hotplug(); |
| 114 | return FALSE; |
| 115 | } |
| 116 | |
| 117 | void testdisplay_cleanup_hotplug(void) |
| 118 | { |
| 119 | if (udevchannel) |
| 120 | g_io_channel_shutdown(udevchannel, TRUE, NULL); |
| 121 | if (uevent_monitor) |
| 122 | udev_monitor_unref(uevent_monitor); |
| 123 | if (udev) |
| 124 | udev_unref(udev); |
| 125 | } |
Daniel Vetter | fa46120 | 2012-01-24 11:28:25 +0100 | [diff] [blame^] | 126 | #else |
| 127 | gboolean testdisplay_setup_hotplug(void) |
| 128 | { |
| 129 | fprintf(stderr, "no hotplug support on this platform\n"); |
| 130 | return TRUE; |
| 131 | } |
| 132 | |
| 133 | void testdisplay_cleanup_hotplug(void) |
| 134 | { |
| 135 | } |
| 136 | #endif |