hotplug.c

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

Generated on Fri Sep 14 22:09:19 2007 for libmtp by  doxygen 1.5.1