Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > by-pkgid > 23fdeec2afeeb035357795bcffb419c6 > files > 52

libmtp-doc-1.0.2-1mdv2010.1.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>libmtp: hotplug.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>hotplug.c</h1><div class="fragment"><pre class="fragment">
<span class="preprocessor">#include &quot;common.h&quot;</span>
<span class="preprocessor">#include &lt;unistd.h&gt;</span>
<span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<span class="preprocessor">#include &lt;stdio.h&gt;</span>
<span class="preprocessor">#include &lt;string.h&gt;</span>

<span class="keyword">static</span> <span class="keywordtype">void</span> usage(<span class="keywordtype">void</span>)
{
  fprintf(stderr, <span class="stringliteral">&quot;usage: hotplug [-u -H -i -a\&quot;ACTION\&quot;]\n&quot;</span>);
  fprintf(stderr, <span class="stringliteral">&quot;       -u:  use udev syntax\n&quot;</span>);
  fprintf(stderr, <span class="stringliteral">&quot;       -H:  use hal syntax\n&quot;</span>);
  fprintf(stderr, <span class="stringliteral">&quot;       -i:  use usb.ids simple list syntax\n&quot;</span>);
  fprintf(stderr, <span class="stringliteral">&quot;       -a\&quot;ACTION\&quot;: perform udev action ACTION on attachment\n&quot;</span>);
  exit(1);
}

<span class="keyword">enum</span> style {
  style_usbmap,
  style_udev,
  style_hal,
  style_usbids
};

<span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
{
  <a name="_a0"></a><a class="code" href="structLIBMTP__device__entry__struct.html">LIBMTP_device_entry_t</a> *entries;
  <span class="keywordtype">int</span> numentries;
  <span class="keywordtype">int</span> i;
  <span class="keywordtype">int</span> ret;
  <span class="keyword">enum</span> style style = style_usbmap;
  <span class="keywordtype">int</span> opt;
  <span class="keyword">extern</span> <span class="keywordtype">int</span> optind;
  <span class="keyword">extern</span> <span class="keywordtype">char</span> *optarg;
  <span class="keywordtype">char</span> *udev_action = NULL;
  <span class="keywordtype">char</span> default_udev_action[] = <span class="stringliteral">&quot;SYMLINK+=\&quot;libmtp-%k\&quot;, MODE=\&quot;666\&quot;&quot;</span>;
  <span class="keywordtype">char</span> *action; <span class="comment">// To hold the action actually used.</span>
  uint16_t last_vendor = 0x0000U;  

  <span class="keywordflow">while</span> ( (opt = getopt(argc, argv, <span class="stringliteral">&quot;uUiHa:&quot;</span>)) != -1 ) {
    <span class="keywordflow">switch</span> (opt) {
    <span class="keywordflow">case</span> <span class="charliteral">&#39;a&#39;</span>:
      udev_action = strdup(optarg);
    <span class="keywordflow">case</span> <span class="charliteral">&#39;u&#39;</span>:
      style = style_udev;
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> <span class="charliteral">&#39;H&#39;</span>:
      style = style_hal;
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> <span class="charliteral">&#39;i&#39;</span>:
      style = style_usbids;
      <span class="keywordflow">break</span>;
    <span class="keywordflow">default</span>:
      usage();
    }
  }

  <span class="keywordflow">if</span> (udev_action != NULL) {
    action = udev_action;
  } <span class="keywordflow">else</span> {
    action = default_udev_action;
  }

  <a name="a1"></a><a class="code" href="group__internals.html#gaaca880ea9ab434e8cf1a1051128318ef">LIBMTP_Init</a>();
  ret = <a name="a2"></a><a class="code" href="group__internals.html#ga0d452be0f83d37296cfa3307fb9a8fd7">LIBMTP_Get_Supported_Devices_List</a>(&amp;entries, &amp;numentries);
  <span class="keywordflow">if</span> (ret == 0) {
    <span class="keywordflow">switch</span> (style) {
    <span class="keywordflow">case</span> style_udev:
      printf(<span class="stringliteral">&quot;# UDEV-style hotplug map for libmtp\n&quot;</span>);
      printf(<span class="stringliteral">&quot;# Put this file in /etc/udev/rules.d\n\n&quot;</span>);
      printf(<span class="stringliteral">&quot;ACTION!=\&quot;add\&quot;, GOTO=\&quot;libmtp_rules_end\&quot;\n&quot;</span>);
      printf(<span class="stringliteral">&quot;ENV{MAJOR}!=\&quot;?*\&quot;, GOTO=\&quot;libmtp_rules_end\&quot;\n&quot;</span>);
      printf(<span class="stringliteral">&quot;SUBSYSTEM==\&quot;usb\&quot;, GOTO=\&quot;libmtp_usb_rules\&quot;\n&quot;</span>
             <span class="stringliteral">&quot;# The following thing will be deprecated when older kernels are phased out.\n&quot;</span>
             <span class="stringliteral">&quot;SUBSYSTEM==\&quot;usb_device\&quot;, GOTO=\&quot;libmtp_usb_device_rules\&quot;\n&quot;</span>
             <span class="stringliteral">&quot;GOTO=\&quot;libmtp_rules_end\&quot;\n\n&quot;</span>
             <span class="stringliteral">&quot;LABEL=\&quot;libmtp_usb_rules\&quot;\n\n&quot;</span>);
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> style_usbmap:
      printf(<span class="stringliteral">&quot;# This usermap will call the script \&quot;libmtp.sh\&quot; whenever a known MTP device is attached.\n\n&quot;</span>);
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> style_hal:
      printf(<span class="stringliteral">&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;ISO-8859-1\&quot;?&gt; &lt;!-- -*- SGML -*- --&gt;\n&quot;</span>);
      printf(<span class="stringliteral">&quot;&lt;!-- This file was generated by %s - - fdi --&gt;\n&quot;</span>, argv[0]);
      printf(<span class="stringliteral">&quot;&lt;deviceinfo version=\&quot;0.2\&quot;&gt;\n&quot;</span>);
      printf(<span class="stringliteral">&quot;  &lt;device&gt;\n&quot;</span>);
      printf(<span class="stringliteral">&quot;    &lt;match key=\&quot;info.subsystem\&quot; string=\&quot;usb\&quot;&gt;\n&quot;</span>);
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> style_usbids:
      printf(<span class="stringliteral">&quot;# usb.ids style device list from libmtp\n&quot;</span>);
      printf(<span class="stringliteral">&quot;# Compare: http://www.linux-usb.org/usb.ids\n&quot;</span>);
      <span class="keywordflow">break</span>;
    }

    <span class="keywordflow">for</span> (i = 0; i &lt; numentries; i++) {
      <a class="code" href="structLIBMTP__device__entry__struct.html">LIBMTP_device_entry_t</a> * entry = &amp;entries[i];

      <span class="keywordflow">switch</span> (style) {
      <span class="keywordflow">case</span> style_udev: 
        {
          printf(<span class="stringliteral">&quot;# %s %s\n&quot;</span>, entry-&gt;<a name="a3"></a><a class="code" href="structLIBMTP__device__entry__struct.html#aa32c5d07072e58790a4935eca4159e9c">vendor</a>, entry-&gt;<a name="a4"></a><a class="code" href="structLIBMTP__device__entry__struct.html#ad392e158dfb9bb0aef58d180b98357e5">product</a>);
          <span class="comment">// Old style directly SYSFS named.</span>
          <span class="comment">// printf(&quot;SYSFS{idVendor}==\&quot;%04x\&quot;, SYSFS{idProduct}==\&quot;%04x\&quot;, %s\n&quot;, entry-&gt;vendor_id, entry-&gt;product_id, action);</span>
          <span class="comment">// Newer style</span>
          printf(<span class="stringliteral">&quot;ATTR{idVendor}==\&quot;%04x\&quot;, ATTR{idProduct}==\&quot;%04x\&quot;, %s\n&quot;</span>, entry-&gt;<a name="a5"></a><a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a>, entry-&gt;<a name="a6"></a><a class="code" href="structLIBMTP__device__entry__struct.html#a450dfc3a7c9d46f998ad1599df776193">product_id</a>, action);
          <span class="keywordflow">break</span>;
        }
      <span class="keywordflow">case</span> style_usbmap:
          printf(<span class="stringliteral">&quot;# %s %s\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#aa32c5d07072e58790a4935eca4159e9c">vendor</a>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#ad392e158dfb9bb0aef58d180b98357e5">product</a>);
          printf(<span class="stringliteral">&quot;libmtp.sh    0x0003  0x%04x  0x%04x  0x0000  0x0000  0x00    0x00    0x00    0x00    0x00    0x00    0x00000000\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a450dfc3a7c9d46f998ad1599df776193">product_id</a>);
          <span class="keywordflow">break</span>;
        <span class="keywordflow">case</span> style_hal:
          printf(<span class="stringliteral">&quot;      &lt;!-- %s %s --&gt;\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#aa32c5d07072e58790a4935eca4159e9c">vendor</a>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#ad392e158dfb9bb0aef58d180b98357e5">product</a>);
          printf(<span class="stringliteral">&quot;      &lt;match key=\&quot;usb.vendor_id\&quot; int=\&quot;0x%04x\&quot;&gt;\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a>);
          printf(<span class="stringliteral">&quot;        &lt;match key=\&quot;usb.product_id\&quot; int=\&quot;0x%04x\&quot;&gt;\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a450dfc3a7c9d46f998ad1599df776193">product_id</a>);
          <span class="comment">/* FIXME: If hal &gt;=0.5.10 can be depended upon, the matches below with contains_not can instead use addset */</span>
          printf(<span class="stringliteral">&quot;          &lt;match key=\&quot;info.capabilities\&quot; contains_not=\&quot;portable_audio_player\&quot;&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;            &lt;append key=\&quot;info.capabilities\&quot; type=\&quot;strlist\&quot;&gt;portable_audio_player&lt;/append&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;/match&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;merge key=\&quot;info.vendor\&quot; type=\&quot;string\&quot;&gt;%s&lt;/merge&gt;\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#aa32c5d07072e58790a4935eca4159e9c">vendor</a>);
          printf(<span class="stringliteral">&quot;          &lt;merge key=\&quot;info.product\&quot; type=\&quot;string\&quot;&gt;%s&lt;/merge&gt;\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#ad392e158dfb9bb0aef58d180b98357e5">product</a>);
          printf(<span class="stringliteral">&quot;          &lt;merge key=\&quot;info.category\&quot; type=\&quot;string\&quot;&gt;portable_audio_player&lt;/merge&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;merge key=\&quot;portable_audio_player.access_method\&quot; type=\&quot;string\&quot;&gt;user&lt;/merge&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;match key=\&quot;portable_audio_player.access_method.protocols\&quot; contains_not=\&quot;mtp\&quot;&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;            &lt;append key=\&quot;portable_audio_player.access_method.protocols\&quot; type=\&quot;strlist\&quot;&gt;mtp&lt;/append&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;/match&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;append key=\&quot;portable_audio_player.access_method.drivers\&quot; type=\&quot;strlist\&quot;&gt;libmtp&lt;/append&gt;\n&quot;</span>);
          <span class="comment">/* FIXME: needs true list of formats ... But all of them can do MP3 and WMA */</span>
          printf(<span class="stringliteral">&quot;          &lt;match key=\&quot;portable_audio_player.output_formats\&quot; contains_not=\&quot;audio/mpeg\&quot;&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;            &lt;append key=\&quot;portable_audio_player.output_formats\&quot; type=\&quot;strlist\&quot;&gt;audio/mpeg&lt;/append&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;/match&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;match key=\&quot;portable_audio_player.output_formats\&quot; contains_not=\&quot;audio/x-ms-wma\&quot;&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;            &lt;append key=\&quot;portable_audio_player.output_formats\&quot; type=\&quot;strlist\&quot;&gt;audio/x-ms-wma&lt;/append&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;          &lt;/match&gt;\n&quot;</span>);
          <span class="comment">/* Special hack to support the OGG format - irivers, TrekStor and NormSoft (Palm) can always play these files! */</span>
          <span class="keywordflow">if</span> (entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a> == 0x4102 || <span class="comment">// iriver</span>
              entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a> == 0x066f || <span class="comment">// TrekStor</span>
              entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a> == 0x1703) { <span class="comment">// NormSoft, Inc.</span>
            printf(<span class="stringliteral">&quot;          &lt;match key=\&quot;portable_audio_player.output_formats\&quot; contains_not=\&quot;application/ogg\&quot;&gt;\n&quot;</span>);
            printf(<span class="stringliteral">&quot;            &lt;append key=\&quot;portable_audio_player.output_formats\&quot; type=\&quot;strlist\&quot;&gt;application/ogg&lt;/append&gt;\n&quot;</span>);
            printf(<span class="stringliteral">&quot;          &lt;/match&gt;\n&quot;</span>);
          }
          printf(<span class="stringliteral">&quot;          &lt;merge key=\&quot;portable_audio_player.libmtp.protocol\&quot; type=\&quot;string\&quot;&gt;mtp&lt;/merge&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;        &lt;/match&gt;\n&quot;</span>);
          printf(<span class="stringliteral">&quot;      &lt;/match&gt;\n&quot;</span>);
        <span class="keywordflow">break</span>;
        <span class="keywordflow">case</span> style_usbids:
          <span class="keywordflow">if</span> (last_vendor != entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a>) {
            printf(<span class="stringliteral">&quot;%04x\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a>);
          }
          printf(<span class="stringliteral">&quot;\t%04x  %s %s\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a450dfc3a7c9d46f998ad1599df776193">product_id</a>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#aa32c5d07072e58790a4935eca4159e9c">vendor</a>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#ad392e158dfb9bb0aef58d180b98357e5">product</a>);
        <span class="keywordflow">break</span>;
      }
      last_vendor = entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a>;
    }
  } <span class="keywordflow">else</span> {
    printf(<span class="stringliteral">&quot;Error.\n&quot;</span>);
    exit(1);
  }

  <span class="comment">// For backward comparibility with the #$!+@! ever changing</span>
  <span class="comment">// udev rule style...</span>
  <span class="keywordflow">if</span> (style == style_udev) {
    printf(<span class="stringliteral">&quot;GOTO=\&quot;libmtp_rules_end\&quot;\n\n&quot;</span>);
    printf(<span class="stringliteral">&quot;LABEL=\&quot;libmtp_usb_device_rules\&quot;\n&quot;</span>);
    <span class="keywordflow">for</span> (i = 0; i &lt; numentries; i++) {
      <a class="code" href="structLIBMTP__device__entry__struct.html">LIBMTP_device_entry_t</a> * entry = &amp;entries[i];

      printf(<span class="stringliteral">&quot;# %s %s\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#aa32c5d07072e58790a4935eca4159e9c">vendor</a>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#ad392e158dfb9bb0aef58d180b98357e5">product</a>);
      printf(<span class="stringliteral">&quot;ATTRS{idVendor}==\&quot;%04x\&quot;, ATTRS{idProduct}==\&quot;%04x\&quot;, %s\n&quot;</span>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a132af448075c74acbbc389d074ce1f97">vendor_id</a>, entry-&gt;<a class="code" href="structLIBMTP__device__entry__struct.html#a450dfc3a7c9d46f998ad1599df776193">product_id</a>, action); 
    }
    printf(<span class="stringliteral">&quot;GOTO=\&quot;libmtp_rules_end\&quot;\n\n&quot;</span>);
  }

  <span class="comment">// Then the footer.</span>
  <span class="keywordflow">switch</span> (style) {
  <span class="keywordflow">case</span> style_usbmap:
    <span class="keywordflow">break</span>;
  <span class="keywordflow">case</span> style_udev:
    printf(<span class="stringliteral">&quot;LABEL=\&quot;libmtp_rules_end\&quot;\n&quot;</span>);
    <span class="keywordflow">break</span>;
  <span class="keywordflow">case</span> style_hal:
    printf(<span class="stringliteral">&quot;    &lt;/match&gt;\n&quot;</span>);
    printf(<span class="stringliteral">&quot;  &lt;/device&gt;\n&quot;</span>);
    printf(<span class="stringliteral">&quot;&lt;/deviceinfo&gt;\n&quot;</span>);
    <span class="keywordflow">break</span>;
  <span class="keywordflow">case</span> style_usbids:
    printf(<span class="stringliteral">&quot;\n&quot;</span>);
  }

  exit (0);
}
</pre></div> </div>
<hr size="1"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>