testdisplay: extract hotplug code
authorDaniel Vetter <daniel.vetter@ffwll.ch>
Tue, 24 Jan 2012 09:50:05 +0000 (10:50 +0100)
committerDaniel Vetter <daniel.vetter@ffwll.ch>
Tue, 24 Jan 2012 09:50:10 +0000 (10:50 +0100)
Signed-Off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
tests/Makefile.am
tests/testdisplay.c
tests/testdisplay.h [new file with mode: 0644]
tests/testdisplay_hotplug.c [new file with mode: 0644]

index 6e169de..b250d37 100644 (file)
@@ -92,6 +92,12 @@ AM_CFLAGS = $(DRM_CFLAGS) $(CWARNFLAGS) \
        -I$(srcdir)/../lib
 LDADD = ../lib/libintel_tools.la $(PCIACCESS_LIBS) $(DRM_LIBS) 
 
+testdisplay_SOURCES = \
+       testdisplay.c \
+       testdisplay.h \
+       testdisplay_hotplug.c \
+       $(NULL)
+
 TESTS_progs += testdisplay
 LDADD += $(CAIRO_LIBS) $(LIBUDEV_LIBS) $(GLIB_LIBS)
 AM_CFLAGS += $(CAIRO_CFLAGS) $(LIBUDEV_CFLAGS) $(GLIB_CFLAGS)
index 6a35307..14d8843 100644 (file)
 #include <assert.h>
 #include <cairo.h>
 #include <errno.h>
-#include <glib.h>
-#include <libudev.h>
 #include <math.h>
 #include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include <unistd.h>
 #include <sys/poll.h>
 #include <sys/time.h>
 #include "xf86drmMode.h"
 #include "i915_drm.h"
 #include "drmtest.h"
+#include "testdisplay.h"
 
 #if defined(DRM_IOCTL_MODE_ADDFB2) && defined(DRM_I915_SET_SPRITE_COLORKEY)
 #define TEST_PLANES 1
 #include "drm_fourcc.h"
 #endif
 
-struct udev_monitor *uevent_monitor;
 drmModeRes *resources;
 int drm_fd, modes;
 int dump_info = 0, test_all_modes =0, test_preferred_mode = 0, force_mode = 0,
@@ -977,7 +972,7 @@ set_mode(struct connector *c)
  * Each connector has a corresponding encoder, except in the SDVO case
  * where an encoder may have multiple connectors.
  */
-static int update_display(void)
+int update_display(void)
 {
        struct connector *connectors;
        int c;
@@ -1032,32 +1027,6 @@ static void usage(char *name)
 
 #define dump_resource(res) if (res) dump_##res()
 
-static gboolean hotplug_event(GIOChannel *source, GIOCondition condition,
-                             gpointer data)
-{
-       struct udev_device *dev;
-       dev_t udev_devnum;
-       struct stat s;
-       const char *hotplug;
-
-       dev = udev_monitor_receive_device(uevent_monitor);
-       if (!dev)
-               goto out;
-
-       udev_devnum = udev_device_get_devnum(dev);
-       fstat(drm_fd, &s);
-
-       hotplug = udev_device_get_property_value(dev, "HOTPLUG");
-
-       if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
-           hotplug && atoi(hotplug) == 1)
-               update_display();
-
-       udev_device_unref(dev);
-out:
-       return TRUE;
-}
-
 static gboolean input_event(GIOChannel *source, GIOCondition condition,
                            gpointer data)
 {
@@ -1093,9 +1062,8 @@ int main(int argc, char **argv)
        int c;
        const char *modules[] = { "i915" };
        unsigned int i;
-       struct udev *u;
        int ret = 0;
-       GIOChannel *udevchannel, *stdinchannel;
+       GIOChannel *stdinchannel;
        GMainLoop *mainloop;
 
        opterr = 0;
@@ -1160,90 +1128,49 @@ int main(int argc, char **argv)
                goto out;
        }
 
-       u = udev_new();
-       if (!u) {
-               fprintf(stderr, "failed to create udev object\n");
-               ret = -1;
-               goto out_close;
-       }
-
-       uevent_monitor = udev_monitor_new_from_netlink(u, "udev");
-       if (!uevent_monitor) {
-               fprintf(stderr, "failed to create udev event monitor\n");
-               ret = -1;
-               goto out_udev_unref;
-       }
-
-       ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor,
-                                                             "drm",
-                                                             "drm_minor");
-       if (ret < 0) {
-               fprintf(stderr, "failed to filter for drm events\n");
-               goto out_udev_mon_unref;
-       }
-
-       ret = udev_monitor_enable_receiving(uevent_monitor);
-       if (ret < 0) {
-               fprintf(stderr, "failed to enable udev event reception\n");
-               goto out_udev_mon_unref;
-       }
-
        mainloop = g_main_loop_new(NULL, FALSE);
        if (!mainloop) {
                fprintf(stderr, "failed to create glib mainloop\n");
                ret = -1;
-               goto out_mainloop_unref;
-       }
-
-       udevchannel =
-               g_io_channel_unix_new(udev_monitor_get_fd(uevent_monitor));
-       if (!udevchannel) {
-               fprintf(stderr, "failed to create udev GIO channel\n");
-               goto out_mainloop_unref;
+               goto out_close;
        }
 
-       ret = g_io_add_watch(udevchannel, G_IO_IN | G_IO_ERR, hotplug_event,
-                            u);
-       if (ret < 0) {
-               fprintf(stderr, "failed to add watch on udev GIO channel\n");
-               goto out_udev_off;
+       if (!testdisplay_setup_hotplug()) {
+               fprintf(stderr, "failed to initialize hotplug support\n");
+               goto out_mainloop;
        }
 
        stdinchannel = g_io_channel_unix_new(0);
        if (!stdinchannel) {
                fprintf(stderr, "failed to create stdin GIO channel\n");
-               goto out_udev_off;
+               goto out_hotplug;
        }
 
        ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
                             NULL);
        if (ret < 0) {
                fprintf(stderr, "failed to add watch on stdin GIO channel\n");
-               goto out_stdio_off;
+               goto out_stdio;
        }
 
        ret = 0;
 
        if (!update_display()) {
                ret = 1;
-               goto out_stdio_off;
+               goto out_stdio;
        }
 
        if (dump_info || test_all_modes)
-               goto out_stdio_off;
+               goto out_stdio;
 
        g_main_loop_run(mainloop);
 
-out_stdio_off:
+out_stdio:
        g_io_channel_shutdown(stdinchannel, TRUE, NULL);
-out_udev_off:
-       g_io_channel_shutdown(udevchannel, TRUE, NULL);
-out_mainloop_unref:
+out_hotplug:
+       testdisplay_cleanup_hotplug();
+out_mainloop:
        g_main_loop_unref(mainloop);
-out_udev_mon_unref:
-       udev_monitor_unref(uevent_monitor);
-out_udev_unref:
-       udev_unref(u);
 out_close:
        if (test_plane)
                disable_planes(drm_fd);
diff --git a/tests/testdisplay.h b/tests/testdisplay.h
new file mode 100644 (file)
index 0000000..962e621
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2010 Intel Corporation
+ *   Jesse Barnes <jesse.barnes@intel.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <glib.h>
+
+extern int drm_fd;
+
+gboolean testdisplay_setup_hotplug(void);
+void testdisplay_cleanup_hotplug(void);
+
+/* called by the hotplug code */
+int update_display(void);
diff --git a/tests/testdisplay_hotplug.c b/tests/testdisplay_hotplug.c
new file mode 100644 (file)
index 0000000..504e7b0
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2010 Intel Corporation
+ *   Jesse Barnes <jesse.barnes@intel.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <libudev.h>
+
+#include "testdisplay.h"
+
+static struct udev_monitor *uevent_monitor;
+static struct udev *udev;
+static GIOChannel *udevchannel;
+
+static gboolean hotplug_event(GIOChannel *source, GIOCondition condition,
+                             gpointer data)
+{
+       struct udev_device *dev;
+       dev_t udev_devnum;
+       struct stat s;
+       const char *hotplug;
+
+       dev = udev_monitor_receive_device(uevent_monitor);
+       if (!dev)
+               goto out;
+
+       udev_devnum = udev_device_get_devnum(dev);
+       fstat(drm_fd, &s);
+
+       hotplug = udev_device_get_property_value(dev, "HOTPLUG");
+
+       if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
+           hotplug && atoi(hotplug) == 1)
+               update_display();
+
+       udev_device_unref(dev);
+out:
+       return TRUE;
+}
+
+
+gboolean testdisplay_setup_hotplug(void)
+{
+       int ret;
+
+       udev = udev_new();
+       if (!udev) {
+               fprintf(stderr, "failed to create udev object\n");
+               goto out;
+       }
+
+       uevent_monitor = udev_monitor_new_from_netlink(udev, "udev");
+       if (!uevent_monitor) {
+               fprintf(stderr, "failed to create udev event monitor\n");
+               goto out;
+       }
+
+       ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor,
+                                                             "drm",
+                                                             "drm_minor");
+       if (ret < 0) {
+               fprintf(stderr, "failed to filter for drm events\n");
+               goto out;
+       }
+
+       ret = udev_monitor_enable_receiving(uevent_monitor);
+       if (ret < 0) {
+               fprintf(stderr, "failed to enable udev event reception\n");
+               goto out;
+       }
+
+       udevchannel =
+               g_io_channel_unix_new(udev_monitor_get_fd(uevent_monitor));
+       if (!udevchannel) {
+               fprintf(stderr, "failed to create udev GIO channel\n");
+               goto out;
+       }
+
+       ret = g_io_add_watch(udevchannel, G_IO_IN | G_IO_ERR, hotplug_event,
+                            udev);
+       if (ret < 0) {
+               fprintf(stderr, "failed to add watch on udev GIO channel\n");
+               goto out;
+       }
+
+       return TRUE;
+
+out:
+       testdisplay_cleanup_hotplug();
+       return FALSE;
+}
+
+void testdisplay_cleanup_hotplug(void)
+{
+       if (udevchannel)
+               g_io_channel_shutdown(udevchannel, TRUE, NULL);
+       if (uevent_monitor)
+               udev_monitor_unref(uevent_monitor);
+       if (udev)
+               udev_unref(udev);
+}