<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>17309</bug_id>
          
          <creation_ts>2012-06-04 14:10:27 +0000</creation_ts>
          <short_desc>It is not defined what the value of Gamepad.index should be after disconnecting the gamepad</short_desc>
          <delta_ts>2015-04-24 10:47:56 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebAppsWG</product>
          <component>HISTORICAL - Gamepad</component>
          <version>unspecified</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>MOVED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          <dependson>21434</dependson>
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Olli Pettay">bugs</reporter>
          <assigned_to name="Ted Mielczarek [:ted]">ted</assigned_to>
          <cc>b.kelemen</cc>
    
    <cc>mike</cc>
    
    <cc>public-webapps</cc>
    
    <cc>scott.w3bugzilla</cc>
          
          <qa_contact>public-webapps-bugzilla</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>68573</commentid>
    <comment_count>0</comment_count>
    <who name="Olli Pettay">bugs</who>
    <bug_when>2012-06-04 14:10:27 +0000</bug_when>
    <thetext>It is not defined what the value of Gamepad.index should be after disconnecting the gamepad. If the index is not changed, then it lies, since the gamepad
isn&apos;t anymore in navigator.gamepads.
Perhaps index should become -1 ?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>68581</commentid>
    <comment_count>1</comment_count>
    <who name="Ted Mielczarek [:ted]">ted</who>
    <bug_when>2012-06-04 17:00:01 +0000</bug_when>
    <thetext>Hm. This is a good point. I guess leaving it unchanged would be bad, because if you plugged in a different gamepad, it could reuse the same index, so you could get bad data.

Since this is JS, and we&apos;re writing WebIDL, I guess we could just make this a nullable type, and say that it must be set to null when the gamepad is disconnected. Does that sound reasonable?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>68582</commentid>
    <comment_count>2</comment_count>
    <who name="Olli Pettay">bugs</who>
    <bug_when>2012-06-04 17:02:56 +0000</bug_when>
    <thetext>Uh, nullable Number values sound pretty horrible.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>68583</commentid>
    <comment_count>3</comment_count>
    <who name="Ted Mielczarek [:ted]">ted</who>
    <bug_when>2012-06-04 17:04:02 +0000</bug_when>
    <thetext>Why? How is that any worse than using -1 as a sigil for &quot;not connected&quot;? null seems like a saner choice in that regard. Either way, the point is just that writing navigator.gamepads[gamepad.index] will throw.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>68584</commentid>
    <comment_count>4</comment_count>
    <who name="Olli Pettay">bugs</who>
    <bug_when>2012-06-04 17:10:52 +0000</bug_when>
    <thetext>well, javascript arrays work with number values.
(new Array()).indexOf(&quot;anything&quot;) == -1</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>68593</commentid>
    <comment_count>5</comment_count>
    <who name="Ted Mielczarek [:ted]">ted</who>
    <bug_when>2012-06-04 20:47:08 +0000</bug_when>
    <thetext>I still don&apos;t see how that example makes any difference. You can pass anything as the array index, you&apos;ll just get back undefined:
a = [&apos;a&apos;,&apos;b&apos;,&apos;c&apos;]

a[0]
a

a[null] == undefined
true</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>68594</commentid>
    <comment_count>6</comment_count>
    <who name="Olli Pettay">bugs</who>
    <bug_when>2012-06-04 20:53:19 +0000</bug_when>
    <thetext>I mean, the index of something not in the array is -1, not null.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>84843</commentid>
    <comment_count>7</comment_count>
    <who name="Ted Mielczarek [:ted]">ted</who>
    <bug_when>2013-03-22 15:17:28 +0000</bug_when>
    <thetext>So, the spec currently says &quot;When multiple gamepads are connected to a user agent, indices MUST be assigned on a first-come, first-serve basis, starting at zero. If a gamepad is disconnected, previously assigned indices MUST NOT be reassigned to gamepads that continue to be connected. However, if a gamepad is disconnected, and subsequently the same or a different gamepad is then connected, index entries MUST be reused.&quot;

Given that, perhaps we should just leave .index alone, since if you reconnect the same gamepad the spec says the index should be reused?

I suppose the followup question there is &quot;should navigator.getGamepads()[&lt;index of a disconnected gamepad&gt;] return the gamepad object with .connected == false, or should it return null?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>84846</commentid>
    <comment_count>8</comment_count>
    <who name="Scott Graham">scott.w3bugzilla</who>
    <bug_when>2013-03-22 15:52:45 +0000</bug_when>
    <thetext>The important part here is that unplugging #0 doesn&apos;t change the index on #1 when 1 continues to be connected.

When 0 is reinserted, it should just find the first empty slot, meaning that if you had 0,1,2 plugged in, unplugged 0 and 1 and then replugged 1 first, it&apos;d be OK for that to become 0 (rather than resuming being 1). If there&apos;s some reason for it to stay 1, that&apos;s OK too (e.g. the Xbox 360 indicator thingy has the user still assigned as player 1)

Given that, I&apos;m fine with whatever value as a sigil, but it probably makes sense to lose its .index.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>84847</commentid>
    <comment_count>9</comment_count>
    <who name="Scott Graham">scott.w3bugzilla</who>
    <bug_when>2013-03-22 15:55:06 +0000</bug_when>
    <thetext>(In reply to comment #7)
&gt; I suppose the followup question there is &quot;should
&gt; navigator.getGamepads()[&lt;index of a disconnected gamepad&gt;] return the
&gt; gamepad object with .connected == false, or should it return null?

Hmm, good question. When there&apos;s no devices at all, I&apos;m just returning undefined which made the most sense at the time. Creating disconnected objects for indices to infinity seems a bit strange.

Stepping back a bit... In the polling model, I&apos;d honestly been thinking of them as &quot;values&quot;, not references to be held on to. So, the model was more that you&apos;d get data via navigator.getGamepads()[N], consume it, and then do the same next time you wanted refreshed data. In that model, there wasn&apos;t even a connected attribute required because you either got data or undefined in the array. I&apos;m not sure if that model helps though, given the other approach of onconnected events and hanging on to the gamepad objects indefinitely.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>84851</commentid>
    <comment_count>10</comment_count>
    <who name="Ted Mielczarek [:ted]">ted</who>
    <bug_when>2013-03-22 17:32:39 +0000</bug_when>
    <thetext>(In reply to comment #8)
&gt; The important part here is that unplugging #0 doesn&apos;t change the index on #1
&gt; when 1 continues to be connected.
&gt; 
&gt; When 0 is reinserted, it should just find the first empty slot, meaning that
&gt; if you had 0,1,2 plugged in, unplugged 0 and 1 and then replugged 1 first,
&gt; it&apos;d be OK for that to become 0 (rather than resuming being 1). If there&apos;s
&gt; some reason for it to stay 1, that&apos;s OK too (e.g. the Xbox 360 indicator
&gt; thingy has the user still assigned as player 1)

Should we loosen up the spec language then? Something like &quot;If a gamepad is disconnected, previously assigned indices MUST NOT be reassigned to gamepads that continue to be connected. However, if a gamepad is disconnected, and subsequently the same or a different gamepad is then connected, index entries MAY be reused. It is RECOMMENDED that when the same gamepad is disconnected and subsequently reconnected that the index value be reused.&quot;

(In reply to comment #9)
&gt; Hmm, good question. When there&apos;s no devices at all, I&apos;m just returning
&gt; undefined which made the most sense at the time. Creating disconnected
&gt; objects for indices to infinity seems a bit strange.

Sure, that seems sensible, I wasn&apos;t proposing having mock objects for all indices. I simply meant &quot;for an index that used to contain a gamepad object that is now disconnected&quot;. Should we return undefined after it&apos;s disconnected, or should we return the (now-useless) gamepad object?

&gt; Stepping back a bit... In the polling model, I&apos;d honestly been thinking of
&gt; them as &quot;values&quot;, not references to be held on to. So, the model was more
&gt; that you&apos;d get data via navigator.getGamepads()[N], consume it, and then do
&gt; the same next time you wanted refreshed data. In that model, there wasn&apos;t
&gt; even a connected attribute required because you either got data or undefined
&gt; in the array. I&apos;m not sure if that model helps though, given the other
&gt; approach of onconnected events and hanging on to the gamepad objects
&gt; indefinitely.

I don&apos;t think this is going to fly, because content can stash these objects in arbitrary slots, and will probably expect things to work. I can imagine someone doing something like:
function Player() {} var p = Player(); p.input = navigator.getGamepads()[0];
// ... poll p.input ...

I can also imagine scenarios where someone has implemented a mock Gamepad object (say to abstract away keyboard vs. gamepad input).

(Also note that the &quot;grab the object from the onconnected event&quot; is mostly just a hackaround for my implementation lacking navigator.getGamepads.)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>85151</commentid>
    <comment_count>11</comment_count>
    <who name="Ted Mielczarek [:ted]">ted</who>
    <bug_when>2013-03-29 13:38:58 +0000</bug_when>
    <thetext>Filed bug 21434 to handle spec&apos;ing the &quot;liveness&quot; of Gamepad objects, since that seems to be the root of our confusion here.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>119876</commentid>
    <comment_count>12</comment_count>
    <who name="Ted Mielczarek [:ted]">ted</who>
    <bug_when>2015-04-24 10:47:56 +0000</bug_when>
    <thetext>Moved to https://github.com/w3c/gamepad/issues/5</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>