Re: hal, udev and video4linux probers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 2009-05-09 at 18:19 +0200, Kay Sievers wrote:
> On Sat, May 9, 2009 at 16:54, Filippo Argiolas <fargiolas@xxxxxxxxx> wrote:
> > On Sat, May 9, 2009 at 4:24 PM, Filippo Argiolas <fargiolas@xxxxxxxxx> wrote:
> >> On Sat, May 9, 2009 at 1:49 PM, Kay Sievers <kay.sievers@xxxxxxxx> wrote:
> >>> I've committed a merge of both versions.
> >>
> >> Great :)
> >> I still don't see it though in the udev (nor udev-extras) git repositories.
> 
> Yeah, seems the master -> public rsync is really slow the last days.
> 
> > Now it's ok. I forgot the email in the copyright line, you can put
> > <filippo.argiolas@xxxxxxxxx> if you want.
> 
> Done.

Here is small hack using libudev, that enumerates all capture-capable
video devices, and listens for new devices connected, or devices going
away.

It builds with:
  gcc -Wall -o v4l-monitor v4l-monitor.c -ludev

And prints when started the built-in camera:
  $ ./v4l-monitor
  UVC Camera (17ef:4807) (/dev/video0)
and while new devices are plugged/unplugged:
  UVC Camera (046d:09a4) (/dev/video1) (add)
  OV511+ USB Camera (/dev/video2) (add)
  UVC Camera (046d:09a4) (/dev/video1) (remove)
  OV511+ USB Camera (/dev/video2) (remove)
  OV511+ USB Camera (/dev/video1) (add)
  UVC Camera (046d:09a4) (/dev/video2) (add)
  ...

Cheers,
Kay



/*
 * enumerate and monitor v4l devices
 *
 * Copyright (C) 2009 Kay Sievers <kay.sievers@xxxxxxxx>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <syslog.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/select.h>

#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE 1
#include "libudev.h"

int main(int argc, char *argv[])
{
	struct udev *udev;
	struct udev_monitor *monitor;
	struct udev_enumerate *enumerate;
	struct udev_list_entry *list_entry;

	/* libudev context */
	udev = udev_new();

	/* connect to event source */
	monitor = udev_monitor_new_from_netlink(udev, "udev");
	/* install subsytem filter, we will not wake-up for other events */
	udev_monitor_filter_add_match_subsystem_devtype(monitor, "video4linux", NULL);
	/* listen to events, and buffer them */
	udev_monitor_enable_receiving(monitor);

	/* prepare a device scan */
	enumerate = udev_enumerate_new(udev);
	/* filter for video devices */
	udev_enumerate_add_match_subsystem(enumerate,"video4linux");
	/* filter for capture capable devices */
	udev_enumerate_add_match_property(enumerate, "ID_V4L_CAPABILITIES", "*:capture:*");
	/* retrieve the list */
	udev_enumerate_scan_devices(enumerate);
	/* print devices */
	udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
		struct udev_device *device;

		device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
						      udev_list_entry_get_name(list_entry));
		if (device == NULL)
			continue;
		printf("%s (%s)\n",
		       udev_device_get_property_value(device, "ID_V4L_PRODUCT"),
		       udev_device_get_devnode(device));
		udev_device_unref(device);
	}
	udev_enumerate_unref(enumerate);

	/* process events */
	while (1) {
		struct pollfd pfd[2];
		struct udev_device *device;

		pfd[0].fd = udev_monitor_get_fd(monitor);
		pfd[0].events = POLLIN;
		pfd[1].fd = STDIN_FILENO;
		pfd[1].events = POLLIN;
		if (poll(pfd, 2, -1) < 1)
			continue;

		if (pfd[0].revents & POLLIN) {
			const char *cap;

			/* get device from event */
			device = udev_monitor_receive_device(monitor);
			if (device == NULL)
				continue;
			/* filter capture-capable devices */
			cap = udev_device_get_property_value(device, "ID_V4L_CAPABILITIES");
			if (cap == NULL || strstr(":capture:", cap) == NULL)
				continue;
			/* print device */
			printf("%s (%s) (%s)\n",
			       udev_device_get_property_value(device, "ID_V4L_PRODUCT"),
			       udev_device_get_devnode(device),
			       udev_device_get_action(device));
			udev_device_unref(device);
		}

		/* exit the loop on console input */
		if (pfd[1].revents & POLLIN)
			break;
	}
	udev_monitor_unref(monitor);

	udev_unref(udev);
	return 0;
}


--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel]     [Linux DVB]     [Asterisk Internet PBX]     [DCCP]     [Netdev]     [X.org]     [Util Linux NG]     [Fedora Women]     [ALSA Devel]     [Linux USB]

  Powered by Linux