hotplug.c

00001 #include "common.h"
00002 #include <unistd.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 static void usage(void)
00008 {
00009   fprintf(stderr, "usage: hotplug [-h -u -a\"ACTION\"]\n");
00010   fprintf(stderr, "       -u:  use udev syntax\n");
00011   fprintf(stderr, "       -H:  use hal syntax\n");
00012   fprintf(stderr, "       -a\"ACTION\": perform udev action ACTION on attachment\n");
00013   exit(1);
00014 }
00015 
00016 enum style {
00017         style_usbmap,
00018         style_udev,
00019         style_hal
00020 };
00021 
00022 int main (int argc, char **argv)
00023 {
00024   LIBMTP_device_entry_t *entries;
00025   int numentries;
00026   int i;
00027   int ret;
00028   enum style style = style_usbmap;
00029   int opt;
00030   extern int optind;
00031   extern char *optarg;
00032   char *udev_action = NULL;
00033   char default_udev_action[] = "SYMLINK+=\"libmtp-%k\", MODE=\"666\"";
00034 
00035   while ( (opt = getopt(argc, argv, "uHa:")) != -1 ) {
00036     switch (opt) {
00037     case 'a':
00038       udev_action = strdup(optarg);
00039     case 'u':
00040       style = style_udev;
00041       break;
00042     case 'H':
00043       style = style_hal;
00044       break;
00045     default:
00046       usage();
00047     }
00048   }
00049 
00050   LIBMTP_Init();
00051   ret = LIBMTP_Get_Supported_Devices_List(&entries, &numentries);
00052   if (ret == 0) {
00053     switch (style) {
00054     case style_udev:
00055       printf("# UDEV-style hotplug map for libmtp\n");
00056       printf("# Put this file in /etc/udev/rules.d\n\n");
00057       printf("SUBSYSTEM!=\"usb_device\", ACTION!=\"add\", GOTO=\"libmtp_rules_end\"\n\n");
00058       break;
00059     case style_usbmap:
00060       printf("# This usermap will call the script \"libmtp.sh\" whenever a known MTP device is attached.\n\n");
00061       break;
00062     case style_hal:
00063       printf("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <!-- -*- SGML -*- -->\n");
00064       printf("<!-- This file was generated by %s - - fdi -->\n", argv[0]);
00065       printf("<deviceinfo version=\"0.2\">\n");
00066       printf(" <device>\n");
00067       printf("  <match key=\"info.bus\" string=\"usb\">\n");
00068       break;
00069     }
00070 
00071     for (i = 0; i < numentries; i++) {
00072       LIBMTP_device_entry_t * entry = &entries[i];
00073       
00074       switch (style) {
00075       case style_udev: {
00076         char *action;
00077 
00078         printf("# %s\n", entry->name);
00079         if (udev_action != NULL) {
00080           action = udev_action;
00081         } else {
00082           action = default_udev_action;
00083         }
00084         printf("SYSFS{idVendor}==\"%04x\", SYSFS{idProduct}==\"%04x\", %s\n", entry->vendor_id, entry->product_id, action);
00085         break;
00086       }
00087       case style_usbmap:
00088         printf("# %s\n", entry->name);
00089         printf("libmtp.sh    0x0003  0x%04x  0x%04x  0x0000  0x0000  0x00    0x00    0x00    0x00    0x00    0x00    0x00000000\n", entry->vendor_id, entry->product_id);
00090         break;
00091       case style_hal:
00092         printf("   <match key=\"usb.vendor_id\" int=\"%d\">\n", entry->vendor_id);
00093         printf("    <match key=\"usb.product_id\" int=\"%d\">\n", entry->product_id);
00094         printf("     <merge key=\"info.category\" type=\"string\">portable_audio_player</merge>\n");
00095         printf("     <append key=\"info.capabilities\" type=\"strlist\">portable_audio_player</append>\n");
00096         printf("     <merge key=\"portable_audio_player.access_method\" type=\"string\">user</merge>\n");
00097         printf("     <merge key=\"portable_audio_player.type\" type=\"string\">mtp</merge>\n");
00098 
00099         /* FIXME: needs true list of formats ... But all of them can do MP3 */
00100         printf("     <append key=\"portable_audio_player.output_formats\" type=\"strlist\">audio/mpeg</append>\n");
00101         printf("    </match>\n");
00102         printf("   </match>\n");
00103         break;
00104       }
00105     }
00106   } else {
00107     printf("Error.\n");
00108     exit(1);
00109   }
00110 
00111   switch (style) {
00112   case style_usbmap:
00113     break;
00114   case style_udev:
00115     printf("\nLABEL=\"libmtp_rules_end\"\n");
00116     break;
00117   case style_hal:
00118     printf("  </match>\n");
00119     printf(" </device>\n");
00120     printf("</deviceinfo>\n");
00121     break;
00122   }
00123 
00124   exit (0);
00125 }

Generated on Mon Mar 19 14:21:14 2007 for libmtp by  doxygen 1.5.1