From 4406efcdf7536738f89fbc28b41bbaf0035070d6 Mon Sep 17 00:00:00 2001 From: Hans Ulrich Niedermann Date: Tue, 30 Jul 2002 12:16:28 +0000 Subject: [PATCH] added print-usb-usermap from CLI git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@4957 67ed7778-7388-44ab-90cf-0a291f65f57c --- packaging/ChangeLog | 5 + packaging/linux-hotplug/.cvsignore | 3 + packaging/linux-hotplug/print-usb-usermap.c | 206 ++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 packaging/linux-hotplug/print-usb-usermap.c diff --git a/packaging/ChangeLog b/packaging/ChangeLog index e6b15462e..99309a459 100644 --- a/packaging/ChangeLog +++ b/packaging/ChangeLog @@ -1,3 +1,8 @@ +2002-07-30 Hans Ulrich Niedermann + + * linux-hotplug/print-usb-usermap.c: moved code here from CLI + * linux-hotplug/Makefile.am: added print-usb-usermap + 2002-06-11 Hans Ulrich Niedermann * rpm/package.spec.in: package dependency on libusb only if required diff --git a/packaging/linux-hotplug/.cvsignore b/packaging/linux-hotplug/.cvsignore index de19a1258..026b84d8a 100644 --- a/packaging/linux-hotplug/.cvsignore +++ b/packaging/linux-hotplug/.cvsignore @@ -1,2 +1,5 @@ *~ .*~ Makefile Makefile.in +.deps +.libs +print-usb-usermap diff --git a/packaging/linux-hotplug/print-usb-usermap.c b/packaging/linux-hotplug/print-usb-usermap.c new file mode 100644 index 000000000..228a97aea --- /dev/null +++ b/packaging/linux-hotplug/print-usb-usermap.c @@ -0,0 +1,206 @@ +/* $Id$ + * + * Copyright (C) 2002 Hans Ulrich Niedermann + * Portions Copyright (C) 2002 Lutz Müller + * Portions Copyright (C) 2002 FIXME + * + * 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 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#define HELP_TEXT \ +"print-usb-usermap - print usb.usermap file for linux-hotplug\n" \ +"\n" \ +"Syntax:\n" \ +" print-usb-usermap [ --verbose ] [ --debug ] [ ]\n" \ +"\n" \ +" --verbose also print comments with camera model names\n" \ +" --debug print all debug output\n" \ +"\n" \ +"print-usb-usermap prints the lines for the usb.usermap file on stdout.\n" \ +"All other messages are printed on stderr. In case of any error, the \n" \ +"program aborts regardless of data printed on stdout and returns a non-zero\n" \ +"status code.\n" \ +"If no is given, print-usb-usermap uses the script name\n" \ +"\"usbcam\"." + +#include +#include +#include + +#include +#include + +#ifndef TRUE +#define TRUE (0==0) +#endif +#ifndef FALSE +#define FALSE (0!=0) +#endif + +#define GP_USB_HOTPLUG_SCRIPT "usbcam" + +#define GP_USB_HOTPLUG_MATCH_VENDOR_ID 0x0001 +#define GP_USB_HOTPLUG_MATCH_PRODUCT_ID 0x0002 + +#define GP_USB_HOTPLUG_MATCH_DEV_CLASS 0x0080 +#define GP_USB_HOTPLUG_MATCH_DEV_SUBCLASS 0x0100 +#define GP_USB_HOTPLUG_MATCH_DEV_PROTOCOL 0x0200 + +#define CR(result) {int r = (result); if (r < 0) return (r);} + +/* print-usb-usermap + * + * Print out lines that can be included into usb.usermap + * - for all cams supported by our instance of libgphoto2. + * + * written after list_cameras + */ + +static int print_usb_usermap(const char *usermap_script, const int add_comments) +{ + int x, n; + CameraAbilitiesList *al; + CameraAbilities a; + + CR (gp_abilities_list_new (&al)); + CR (gp_abilities_list_load (al, NULL)); /* use NULL context */ + CR (n = gp_abilities_list_count (al)); + + for (x = 0; x < n; x++) { + int flags = 0; + int class = 0, subclass = 0, proto = 0; + int usb_vendor = 0, usb_product = 0; + + CR (gp_abilities_list_get_abilities (al, x, &a)); + + if (!(a.port & GP_PORT_USB)) + continue; + + if (a.usb_vendor && a.usb_product) { + class = 0; + subclass = 0; + proto = 0; + flags = GP_USB_HOTPLUG_MATCH_VENDOR_ID | GP_USB_HOTPLUG_MATCH_PRODUCT_ID; + usb_vendor = a.usb_vendor; + usb_product = a.usb_product; + } else if (a.usb_class) { + class = a.usb_class; + subclass = a.usb_subclass; + proto = a.usb_protocol; + flags = GP_USB_HOTPLUG_MATCH_DEV_CLASS; + if (subclass != -1) + flags |= GP_USB_HOTPLUG_MATCH_DEV_SUBCLASS; + else + subclass = 0; + if (proto != -1) + flags |= GP_USB_HOTPLUG_MATCH_DEV_PROTOCOL; + else + proto = 0; + usb_vendor = 0; + usb_product = 0; + } else { + flags = 0; + } + + if (flags != 0) { + printf ("# %s\n", + a.model); + printf ("%-20s " + "0x%04x 0x%04x 0x%04x 0x0000 " + "0x0000 0x%02x 0x%02x " + "0x%02x 0x00 0x00 " + "0x00 0x00000000\n", + usermap_script, flags, + a.usb_vendor, a.usb_product, + class, subclass, proto); + } else { + fputs ("Error: Neither vendor/product nor class/subclass matched\n", stderr); + return 2; + } + } + CR (gp_abilities_list_free (al)); + + return (0); +} + +/* time zero for debug log time stamps */ +struct timeval glob_tv_zero = { 0, 0 }; + +static void +debug_func (GPLogLevel level, const char *domain, const char *format, + va_list args, void *data) +{ + struct timeval tv; + gettimeofday (&tv,NULL); + fprintf (stderr, "%li.%06li %s(%i): ", + tv.tv_sec - glob_tv_zero.tv_sec, + (1000000 + tv.tv_usec - glob_tv_zero.tv_usec) % 1000000, + domain, level); + vfprintf (stderr, format, args); + fprintf (stderr, "\n"); +} + +int main(int argc, char *argv[]) +{ + char *usermap_script = NULL; /* script name to use */ + int add_comments = FALSE; /* whether to add cam model as a comment */ + int debug_mode = FALSE; /* whether we should output debug messages */ + + int i; + + /* check command line arguments */ + for (i=1; i