libmtp: hotplug.c

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Examples

# 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, "       -a\"ACTION\": perform udev action ACTION on attachment\n");
    00012   exit(1);
    00013 }
    00014
    00015 int main (int argc, char **argv)
    00016 {
    00017   LIBMTP_device_entry_t *entries;
    00018   int numentries;
    00019   int i;
    00020   int ret;
    00021   int udev_style = 0;
    00022   int opt;
    00023   extern int optind;
    00024   extern char *optarg;
    00025   char *udev_action = NULL;
    00026   char default_udev_action[] = "SYMLINK+=\"libmtp-%k\", MODE=\"666\"";
    00027
    00028   while ( (opt = getopt(argc, argv, "ua:")) != -1 ) {
    00029     switch (opt) {
    00030     case 'a':
    00031       udev_action = strdup(optarg);
    00032     case 'u':
    00033       udev_style = 1;
    00034       break;
    00035     default:
    00036       usage();
    00037     }
    00038   }
    00039
    00040   LIBMTP_Init();
    00041   ret = LIBMTP_Get_Supported_Devices_List(&entries, &numentries);
    00042   if (ret == 0) {
    00043     if (udev_style) {
    00044       printf("# UDEV-style hotplug map for libmtp\n");
    00045       printf("# Put this file in /etc/udev/rules.d\n\n");
    00046       printf("SUBSYSTEM!=\"usb_device\", ACTION!=\"add\", GOTO=\"libmtp_rules_end\"\n\n");
    00047     } else {
    00048       printf("# This usermap will call the script \"libmtp.sh\" whenever a known MTP device is attached.\n\n");
    00049     }
    00050
    00051     for (i = 0; i < numentries; i++) {
    00052       LIBMTP_device_entry_t * entry = &entries[i];
    00053
    00054       printf("# %s\n", entry->name);
    00055       if (udev_style) {
    00056         char *action;
    00057
    00058         if (udev_action != NULL) {
    00059           action = udev_action;
    00060         } else {
    00061           action = default_udev_action;
    00062         }
    00063         printf("SYSFS{idVendor}==\"%04x\", SYSFS{idProduct}==\"%04x\", %s\n", entry->vendor_id, entry->product_id, action);
    00064       } else {
    00065         printf("libmtp.sh    0x0003  0x%04x  0x%04x  0x0000  0x0000  0x00    0x00    0x00    0x00    0x00    0x00    0x00000000\n", entry->vendor_id, entry->product_id);
    00066       }
    00067     }
    00068   } else {
    00069     printf("Error.\n");
    00070     exit(1);
    00071   }
    00072
    00073   if (udev_style) {
    00074     printf("\nLABEL=\"libmtp_rules_end\"\n");
    00075   }
    00076
    00077   exit (0);
    00078 }