<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[robothaus]]></title>
  <link href="http://blog.robothaus.org//atom.xml" rel="self"/>
  <link href="http://blog.robothaus.org//"/>
  <updated>2012-03-06T12:23:17-05:00</updated>
  <id>http://blog.robothaus.org//</id>
  <author>
    <name><![CDATA[Bobby Richter]]></name>
    <email><![CDATA[secretrobotron@gmail.com]]></email>
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Butter 0.2 - 'Ghostbusters']]></title>
    <link href="http://blog.robothaus.org//2012/03/06/butter-0-dot-2-ghostbusters/"/>
    <updated>2012-03-06T00:40:00-05:00</updated>
    <id>http://blog.robothaus.org//2012/03/06/butter-0-dot-2-ghostbusters</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://blog.robothaus.org//images/butter0-2.jpg" width="400"> Last week at the Mozilla Toronto office, we celebrated as I clicked the &#8220;close milestone&#8221; button for Butter, marking the <strong>0.2</strong> release, codenamed &#8220;Ghostbusters&#8221;. It was also <a href="https://twitter.com/#!/dcseifried">Dave</a>&#8217;s birth-week, so we celebrated with a aptly themed cake. [<em>Photo credit: Ben Moskowitz</em>]</p>

<p>The repo has been moved to Mozilla&#8217;s github account, so feel free to clone the repo and follow progress: <a href="https://github.com/mozilla/butter">https://github.com/mozilla/butter</a></p>

<p>Considering the amount of work we did to roadmap Butter for the year, and the amount of time the team had to produce this release, I&#8217;m quite proud of our accomplishment. Butter already looks better, functions faster, and feels more solid as a foundation than its predecessors. This is a great start to <a href="https://webmademovies.lighthouseapp.com/projects/65733-butter/milestones">a year of Butter</a>, and <strong>0.3</strong> is just on the horizon.</p>

<!--more-->


<p>Rather than delving into details about time and estimation analysis (re: <a href="http://robothaus.org/mozilla/butter-roadmap/">robothaus.org/mozilla/butter-roadmap/</a>) about <strong>0.2</strong>, I&#8217;ll just boast about its exciting features and talk about what&#8217;s to come in <strong>0.3</strong>. Besides, I&#8217;m told that my posts are notoriously long, so splitting out that content might not be such a bad strategy.</p>

<h2>The UI</h2>

<p>Ben and I spent a lot of time pulling apart Popcorn Maker&#8217;s UI to attempt to deliver a more pleasing experience with this refresh of Butter. We still have some work to do to accomplish the design we envisioned in our <a href="https://webmademovies.lighthouseapp.com/projects/80723/tickets/286-popcorn-maker-user-stories">vision document</a>, but we&#8217;ve made significant steps toward a flexible UI environment which we can fill with better functionality in the near future.</p>

<p>Here are a few things to look for so far:</p>

<ul>
<li><img class="right" src="http://blog.robothaus.org//images/butter0-2-timeline.jpg"> The timeline lost a lot of unnecessary height as we decided to make more efficient use of some key areas. Now, after minimizing the iconic Butter tray, you&#8217;re still able to use the timeline scrubber and play, pause or mute the media.</li>
<li><img class="right" src="http://blog.robothaus.org//images/butter0-2-scrollbars.jpg"> Scrollbars look far less ridiculous since we wrote our own implementation. Not only do we get to decide visual aspects of the scrollbars, but also the user experience for them.</li>
<li><img class="right" src="http://blog.robothaus.org//images/butter0-2-juicy.jpg"> Look at how juicy these track events and track handles look now. They&#8217;re more defined, and have a colouring scheme based on a hashing of the track event type &#8211; so <code>image</code> is always red, <code>attribution</code> is always turquoise, regardless of which OS, browser, or version of Butter you&#8217;re using.</li>
</ul>


<p>Butter isn&#8217;t just more visually pleasing though, it&#8217;s more helpful. We&#8217;ve added a highlighting system to show you what&#8217;s targetable, and what&#8217;s been targeted.</p>

<p><img class="left" src="http://blog.robothaus.org//images/butter0-2-drop.jpg"> When dragging a plugin from the plugin-tray, an icon will appear under your mouse to show you what kind of object you&#8217;re going to produce when you drop. However, you&#8217;re not limited to dropping plugins on tracks anymore; you can drop them right on the target you want to affect. As you move the icon over certain objects, they will light up to tell you that you can drop content on them.</p>

<p><img class="left" src="http://blog.robothaus.org//images/butter0-2-drop-add.jpg"> After dropping, a track event will appear on a track at a time corresponding to the position of the timeline scrubber. Clicking on the new track event will produce a similar effect: an object on the page will flash, letting you know it&#8217;s the targeted object.</p>

<p>This whole procedure feels a lot more natural than our previous attempts. Your intentions as a designer or film-creator are more directly realizeable.</p>

<p>As a result of these UI improvements, just through some preliminary testing, the software feels like it&#8217;s more reactive and less confusing. We dug out some old code that profiled <strong>very</strong> poorly, and made sure our approach stayed clean and simple. Obviously, it&#8217;s pleasing us so far, so hopefully it will test well in future releases.</p>

<h2>The Code</h2>

<p>Firstly, the HTML/Template situation got a lot simpler. The HTML code listing below and a bit of JavaScript is <strong>all</strong> you need to put Butter to work in your page now:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;html&gt;
</span><span class='line'>  &lt;head&gt;
</span><span class='line'>    &lt;script src="../src/butter.js" data-butter-exclude /&gt;&lt;/script&gt;
</span><span class='line'>    &lt;script type="text/javascript" src="template.js" data-butter-exclude /&gt;&lt;/script&gt;
</span><span class='line'>    &lt;link rel="stylesheet" href="../src/ui/butter.ui.css" data-butter-exclude /&gt;
</span><span class='line'>  &lt;/head&gt;
</span><span class='line'>  &lt;body&gt;
</span><span class='line'>    &lt;video id="main" data-butter="media" controls &gt;
</span><span class='line'>      &lt;source src="http://videos-cdn.mozilla.net/serv/webmademovies/laylapop.ogv" /&gt;
</span><span class='line'>    &lt;/video&gt;
</span><span class='line'>    &lt;div class="container-div"&gt;
</span><span class='line'>      &lt;div class="content-div" data-butter="target" id="Area1"&gt;&lt;/div&gt;
</span><span class='line'>    &lt;/div&gt;
</span><span class='line'>    &lt;div class="container-div"&gt;
</span><span class='line'>      &lt;div class="content-div" data-butter="target" id="Area2" data-butter-default &gt;&lt;/div&gt;
</span><span class='line'>    &lt;/div&gt;
</span><span class='line'>    &lt;div id="target-div"&gt;&lt;/div&gt;
</span><span class='line'>  &lt;/body&gt;
</span><span class='line'>&lt;/html&gt;</span></code></pre></td></tr></table></div></figure>


<p>Here are some noteworthy points about this code:</p>

<ol>
<li>There are really two important inclusions: <code>butter.js</code> and <code>butter.ui.css</code>. The components of Butter will build themselves from these two libraries.</li>
<li>Two separate <code>data-</code> attributes help Butter identify elements of your page at different stages during the editing process: <code>data-butter</code> and <code>data-butter-exclude</code>. The former is a statement of important elements on your page &#8211; like targets or media elements for Butter to consume and use &#8211; while the latter marks a tag to be excluded from an HTML export of the page.</li>
<li>Starting Butter is still a manual process, keeping it an opt-in feature of your page. We want developers to be able to control their individual experiences. Above, another script is included, <code>template.js</code>, which looks similar to this:</li>
</ol>


<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>  Butter({
</span><span class='line'>    config: "../config/default.conf",
</span><span class='line'>    ready: function( butter ){
</span><span class='line'>      var media = butter.media[ 0 ];
</span><span class='line'>
</span><span class='line'>      media.listen( "mediaready", function( e ){
</span><span class='line'>
</span><span class='line'>        var track = media.addTrack( "Track1" );
</span><span class='line'>
</span><span class='line'>        butter.plugin.add([
</span><span class='line'>          {
</span><span class='line'>            name: "footnote",
</span><span class='line'>            type: "footnote",
</span><span class='line'>            path: "../external/popcorn-js/plugins/footnote/popcorn.footnote.js"
</span><span class='line'>          },
</span><span class='line'>        ], function( e ) {
</span><span class='line'>
</span><span class='line'>          var event = track.addTrackEvent({
</span><span class='line'>            type: "footnote",
</span><span class='line'>            popcornOptions: {
</span><span class='line'>              start: 0,
</span><span class='line'>              end: 3,
</span><span class='line'>              text: "test",
</span><span class='line'>              target: "Area1"
</span><span class='line'>            }
</span><span class='line'>          });
</span><span class='line'>
</span><span class='line'>        });
</span><span class='line'>
</span><span class='line'>      });
</span><span class='line'>    } 
</span><span class='line'>  });
</span></code></pre></td></tr></table></div></figure>


<p>Here&#8217;s a brief walkthrough:</p>

<ol>
<li>Start Butter with the <code>Butter</code> function, providing a url to a configuration file, and a ready callback function.</li>
<li>Once the ready callback has been called, a reference to the <code>butter</code> object is provided, and butter is almost ready to use.</li>
<li>Wait for the media to be available (it still has to load) by attaching a listener for <code>mediaready</code> to a <code>Media</code> object.</li>
<li>Let Butter know which plugins should be available in the UI by calling <code>butter.plugin.add</code></li>
<li>Butter!</li>
</ol>


<p>I believe there are still too many steps to get going initially, so we&#8217;ll be widdling it down over the next couple of releases. However, this code runs consistently well now, and is becoming increasingly data-driven. For instance, we use a config file instead of an inline <code>JSON</code> blob. Soon, we&#8217;ll be working on ways to encompass the needs of developers as utility functions, so even less code will be necessary unless you wish to abnormally customize your experience.</p>

<h2>The Server</h2>

<p><a href="https://twitter.com/#!/jbuckca">Jon Buckley</a> started laying the foundation for our server infrastructure this month. The project is hilariously titled <a href="https://webmademovies.lighthouseapp.com/projects/90181-cornfield-server/overview"><strong>Cornfield</strong></a> (also available on the <a href="https://github.com/mozilla/cornfield">Mozilla github account</a>).</p>

<p>For Butter, this means having the ability to connect to a server which is specifically prepared to save, load, import, publish, and share content within the framework.</p>

<p>Currently, Butter has the ability to communicate with this rudamentary server, but we&#8217;re still working on the API layer and UI design to make it shine. Look for more about Cornfield in future releases.</p>

<h2>The Tests</h2>

<p>Briefly: at crunch time last year we forgot that our test suite existed, but now we&#8217;ve paid particular attention to making sure our tests are up-to-date, and that new features don&#8217;t hinder a passing grade. We have to make sure this stays in tact, especially as Butter continues as a formal foundation for the year&#8217;s overall project.</p>

<p>So, if you clone our software and run the test suite in your environment (however convoluted it may be), and some tests don&#8217;t pass, <strong>file a ticket. We want to know about it</strong>.</p>

<h1>Onward</h1>

<p><strong>0.2</strong> is publicly available, and we already have <a href="https://webmademovies.lighthouseapp.com/projects/65733-butter/milestones/current">more than 100 issues to tackle in <strong>0.3</strong></a>, which is slated for release the first Tuesday of next month.</p>

<p>Again, I&#8217;m going to be gathering metrics about ticket completion and bug generation so that we can refine our development process throughout the year. Another blog post is deserved to talk about the results of that campaign.</p>

<p>I&#8217;m proud of our progress so far though, and I can&#8217;t wait to see what we will produce in the months to come. We&#8217;re already beginning to wonder what kind of cake best fits the <em>Breakfast Club</em> theme.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Popcorn: It's Roadmapping Season]]></title>
    <link href="http://blog.robothaus.org//2012/02/14/popcorn-its-roadmapping-season/"/>
    <updated>2012-02-14T06:21:00-05:00</updated>
    <id>http://blog.robothaus.org//2012/02/14/popcorn-its-roadmapping-season</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://blog.robothaus.org//images/popcorn-2012.png" title="&#34;Popcorn in 2012&#34;" alt="&#34;Popcorn in 2012&#34;">
A significant portion of the Mozilla Foundation&#8217;s effort this year will contribute to Popcorn Maker&#8217;s eventual success, culminating in a 1.0 release in or around November. We&#8217;ve correctly identified that achieving this is a tall order, so we planned and roadmapped harder than I&#8217;ve ever planned and roadmapped before.</p>

<!--more-->


<p><strong>Update:</strong> The popcorn roadmapping PDF explains the entirety of the project from a high-level design perspective. <a href="https://webmademovies.lighthouseapp.com/projects/80723/tickets/286-popcorn-maker-user-stories">Here is the lighthouse link</a> to add your comments about what will be happening, and <a href="https://webmademovies.lighthouseapp.com/projects/80723/tickets/286/a/1961749/popcorn-maker-2012-vision-fina.pdf">here is a direct link</a> to the PDF.</p>

<h2>Popcorn Maker - Here and Now</h2>

<p>In the past, we treated <em>Butter</em>, and to some extent <em>Popcorn.js</em>, as means to an end &#8211; namely, <strong>Popcorn Maker</strong>. This model used to make sense: we were creating an app (Popcorn Maker), which needed an API (Butter) and underlying library (Popcorn.js) to make it all tick. With some vision from the foundation, and specifically from <a href="http://www.benmoskowitz.com/?p=458">Ben Moskowitz</a> and <a href="http://mozillapopcorn.org/roadmapping-popcorn-maker-1-0/">Brett Gaylor</a> who have both blogged tirelessly on the subject, that outlook has changed significantly. And, I believe it&#8217;s for the better.</p>

<h3>Butter</h3>

<p>Previously, we wrote Butter, and quickly <a href="https://github.com/secretrobotron/popcorn-maker/tree/master/butter">stuffed it underneath</a> Popcorn Maker once it was &quot;<a href="https://github.com/secretrobotron/butter/tree/598904beaa29b76991e61e7efb871225c30ef609">ready</a>&quot;. Finishing the app-version of Popcorn Maker was a greater and more demanding task than maintaining Butter separately, so we hacked at it only to give us results we could immediately use. It even lost touch with its own repo as we voted to make it a git <em>sub-tree</em> instead of a <em>sub-module</em> (deserving a rather passionate rant).</p>

<p>As spiteful as I might sound, this approach wasn&#8217;t all wrong. There was simply no need to maintain an API that was going to be used directly by a handful of people for a specific project at an accelerated pace for a short period of time. Butter was classified as a library &#8211; the underpinnings &#8211; of its shinier cousin, Popcorn Maker. Luckily, though, as careful engineers do when they acknowledge reasonable fear and forethought, we kept Butter isolated so as to still be separated from Popcorn Maker by an API &#8211; just an ugly one.</p>

<p>Now, as I&#8217;ve already stated, things are a bit different. Some of the things Butter did well were identified as useful to other parties, and we don&#8217;t necessarily want to package the entire Popcorn experience inside of one app, Popcorn Maker. So, we&#8217;ve pulled out the Butter which lay unappreciated inside of Popcorn Maker, and given it <a href="https://github.com/secretrobotron/butter">its own repo</a> again!</p>

<p>After a few iterations, Butter will be an SDK (the BDK, perhaps) to support the development of Popcorn-centric apps, exclusive of Popcorn Maker or not. It will retain control of editors and templates, but with an important architectural change.</p>

<p>With <strong>Popcorn Hacker</strong>, Scott Downe <a href="http://scottdowne.wordpress.com/2011/11/13/popcorn-js-has-a-new-brother-popcorn-hacker/">showed us</a> that editing Popcorn directly in a page is a great feature &#8211; a feature which was downplayed by Popcorn Maker for sake of app consistency and safety. So, we&#8217;ve folded the idea back into Butter.</p>

<p>Butter is slimmer, and meaner. It lives directly inside the page it&#8217;s editing, so no <code>postMessage</code> loops are necessary. Bloat down, functionality up. With some external guidance (bookmarklet or manual insertion), it opens itself in a page (UI and all) and, with increased versatility, attempts to recongnize existing Butter or Popcorn data. When the user is done editing, Butter attempts to stay resilient and small, insisting only on HTML and JSON for storage &#8211; <strong>no external dependencies</strong>.</p>

<p>The result is an SDK which allows developers to create templates and apps based on the Butter library to take advantage of the UI elements and Popcorn abstraction which Butter achieves. Developing an app specifically for <strong>&quot;making videos with contextual tidbits which popup at specific times&quot;</strong> is as easy as building a design for the app with CSS and HTML (the template), and enabling the Butter library within it.</p>

<p>Also, we heard a loud cry from template and editor developers to have editors which interact directly with the template. Now, editors can live <em>in</em> the template, simply using the Butter API to interact with timed events.</p>

<p>Sounds great, right? Well, we&#8217;re only getting started: the 0.2 release is scheduled for the end of this month.</p>

<h3>The Popcorn Software Suite (PSS)</h3>

<p>With Butter sketched out as an broader SDK rather than a library with a struggling identity, the Popcorn software suite unfolds like this for 2012:</p>

<ul>
<li><a href="https://webmademovies.lighthouseapp.com/projects/65733-butter">Butter</a> (SDK) - The SDK which will allow developers to build apps, including Popcorn Maker. Its library will reside inside of templates which enable Butter&#8217;s tools in a customizable fashion. It includes the usual UI pieces &#8211; a timeline, a plugin list, editors, and popups &#8211; all of which are programmable with API, and have management systems to govern them.</li>
<li><a href="https://webmademovies.lighthouseapp.com/projects/90181-cornfield-server">Cornfield</a> (Server) - The server component in the Popcorn software suite, allowing users to store their work, share templates, fork projects, and manage video content.</li>
<li><a href="https://webmademovies.lighthouseapp.com/projects/80723-popcorn-maker">Popcorn Maker</a> (Site) - A site which encompasses Butter and Cornfield with a well-designed entry-point and fancy UI. This is where content galleries, ratings, profiles, and tutorials live, and will be as accessible as possible directly from our public-facing websites.</li>
</ul>


<p>I&#8217;m particularly excited about being able to offer <em>Cornfield</em>, and specifically its <strong>Video Transcoding</strong> feature. This will let users upload their videos, and have them automatically cross-browser compatible (mp4, ogv, webm), which makes creating and distributing a project very simple.</p>

<p>Also, there is a focus on making each component easy to use and install. In general we want to use <strong><a href="http://npmjs.org/">npm</a></strong> as much as possible to make <em>Cornfield</em> and <em>Popcorn Maker</em> installable packages with setup scripts (or web pages, like Wordpress) to initialize them.</p>

<h2>Technical Roadmap</h2>

<p>Aside from Butter, there is a lot of software to imagine, design, write and maintain this year in the Popcorn project alone. In an attempt to organize the team, I <a href="http://robothaus.org/mozilla/butter-roadmap">created a document</a> which illustrates the features we need to complete, categorized by milestone and project.</p>

<p>Here&#8217;s an excerpt:</p>

<p><img class="center" src="http://blog.robothaus.org//images/roadmap-butter.jpg" title="&#34;Butter Roadmap&#34;" alt="&#34;Butter Roadmap&#34;"></p>

<p>I really tried to stay away from assigning time to features, since I could only estimate from general experience how long each would take. Instead of spending time filing down a time estimate to precision, I attempted to give each feature a reasonable rating of <strong>effort</strong>. For example, the first top-left green block represents the <em>Droppable Regions</em> feature. It received a rating of 3 because I considered the amount of CSS detailing, the jquery-ui interfacing, and Butter-specific scripting we would have to do to complete it as a non-trivial amount of effort.</p>

<p>As you might have already noticed, features don&#8217;t necessarily exist in only one milestone; they can span milestones with varying degrees of effort, displaying three important areas in the lifetime of a feature:</p>

<ol>
<li>Research and Design</li>
<li>Implementation</li>
<li>Bugs</li>
</ol>


<p>The first two are sometimes lumped into the same milestone, but often there is special consideration for the bugs that are a result of the implementation of a feature. These bugs are separate from <em>Bugs</em> in general which have their own colour and path through the graph.</p>

<p>My favourite part of the document is the <em>Summary</em> graph:</p>

<p><img class="center" src="http://blog.robothaus.org//images/roadmap-summary.jpg" title="&#34;Roadmap Summary&#34;" alt="&#34;Roadmap Summary&#34;"></p>

<p>I think it&#8217;s particularly interesting to see the shape of work as the year progresses. I wasn&#8217;t surprised to see a rough bell-curve, but its height in the beginning of the year is concerning. Either estimates are wrong and will be adjusted to reshape the curve (likely), bugs will be much more significant with respect to effort than anticipated (pretty likely), or we&#8217;ll just be working very, very hard the entire year (very likely).</p>

<p>Note that no graph takes into consideration the number of people working on the project, so, as more people contribute, the larger the spread of the effort in each milestone, which yields higher relaxation times and lower stress levels for engineers :).</p>

<p>Of course, by no means is this meant to be a perfect description of how the year will occur. There are bound to be features which slide into subsequent releases or get dropped altogether. There are also certainly some estimates I have made which will prove to be completely incorrect, either negatively or positively. The document is intended to be a living one, so throughout the year I will update it to keep track of progress and to hone feature estimates &#8211; for this project and for future ones.</p>

<p><strong>You can find the document, in all of its estimated glory, <a href="http://robothaus.org/mozilla/butter-roadmap">here</a></strong>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Popcorn.js - All Grown Up]]></title>
    <link href="http://blog.robothaus.org//2011/12/22/popcorn-dot-js-all-grown-up/"/>
    <updated>2011-12-22T13:37:00-05:00</updated>
    <id>http://blog.robothaus.org//2011/12/22/popcorn-dot-js-all-grown-up</id>
    <content type="html"><![CDATA[<p>This morning, CDOT student and Mozilla hacker, <a href="http://twitter.com/#!/jbuckca">Jon Buckley</a> discovered that the <a href="https://services.addons.mozilla.org/en-US/firefox/discovery/pane/pane/pane/strict">Firefox 9 Addons About Page</a> uses <a href="http://www.popcornjs.org">Popcorn.js</a> to augment a tutorial video about Firefox addons by adding apropos content to the page while the video plays.</p>

<p><img class="center" src="http://blog.robothaus.org//images/addons-about.gif"></p>

<p>Of course it&#8217;s great to see another example of Popcorn in the wild, but this is a use case we had never before considered. Using Popcorn.js to show synchronized interactive content, the page explains addons, what kinds of addons there are, and how a user can use them in Firefox.</p>

<!-- more -->


<h2>Being Sherlock</h2>

<p>Such an elegant use of the library deserves a little investigatory work. Since the authors of the many pieces of user-facing Firefox content do such a good job, it may not be immediately obvious that the addons about page is made of the web. Nevertheless, because it is indeed a webpage, I can <strong>right click to learn more</strong>, and open up the page in a new tab to isolate it.</p>

<p>Without flirting too much with a tangent about the merits of the <em>Web Console</em>, finding out that the page contains an instance of <em>Popcorn</em> is pretty easy:</p>

<ol>
<li>Press <code>Ctrl</code>, <code>Shift</code> and <code>K</code> altogether to bring up the <em>Web Console</em>.</li>
<li>Type in <code>Popcorn</code> &#8211; which should auto-complete even before you have typed <code>Pop</code> &#8211; and press <code>Enter</code>.</li>
</ol>


<p>Right away, without digging through HTML for a <code>script</code> tag, we can see that <em>Popcorn.js</em> is included on the page somewhere. But, we can do more than that:</p>

<ol>
<li>After starting the video, try typing <code>Popcorn.instances</code> into the <em>Web Console</em>. It should report back with a large blob of text, which is actually an array containing Popcorn objects.</li>
<li>You can see the events that are going to occur during the video by typing <code>Popcorn.instances[0].getTrackEvents()</code>, which should return another blob &#8211; an array of Popcorn events.</li>
<li>Similarly, by trying commands like <code>Popcorn.instances[0].getTrackEvents()[0].start</code>, or <code>Popcorn.instances[0].getTrackEvents()[0]._natives.type</code> you can see when the first event will start, and what <em>plugin type</em> it is respectively. Feel free to copy &amp; paste.</li>
</ol>


<p>By poking around in a similar fashion, I was able to discover that the addons page makes use of the <em>code</em> plugin to add and remove <em>CSS classes</em> from elements on the page. This approach lets the video change the visibility and interactivity properties of the information bar&#8217;s constituent elements.</p>

<p>If this bit of detective work caught your attention, we&#8217;re only where the rabbit hole begins. In fact, <a href="http://popcornjs.org/popcorn-docs/">there&#8217;s a whole API to discover and play with</a> (recent documentation update courtesy of another CDOT student and Mozilla hacker, <a href="http://twitter.com/#!/dcseifried">David Seifried</a>).</p>

<h2>Feeling the Effects</h2>

<p>Needless to say, it was surprising seeing comparatively excessive usage stats on the <em>popcornjs.org</em> server, as throngs of people visited the addons about page:</p>

<p><img class="center" src="http://blog.robothaus.org//images/popcorn-server-graph.png"></p>

<p>Luckily, Jon Buckley, and <a href="http://twitter.com/#!/rwaldron">Rick Waldron</a> (another passionate Popcorn contributor from <a href="http://bocoup.com/">Bocoup</a>) were able to advise the people managing the addons website to pull the server out of accidental <a href="http://en.wikipedia.org/wiki/Denial-of-service_attack">DOS</a> territory.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[It's a Good Day to Render]]></title>
    <link href="http://blog.robothaus.org//2011/12/14/its-a-good-day-to-render/"/>
    <updated>2011-12-14T01:52:00-05:00</updated>
    <id>http://blog.robothaus.org//2011/12/14/its-a-good-day-to-render</id>
    <content type="html"><![CDATA[<p>After what seemed like a totally unproductive and inconsequential morning, and an equally slow and frustrating afternoon, I managed to at least prototype two rather important things that have been lingering around Paladin and CubicVR for a long time.</p>

<p>First, Gladius had a rendering engine, but it was almost entirely useless unless you really like clearing a canvas. Second, CubicVR.js could only draw to one canvas on a page. Not &#8220;one canvas at a time&#8221;, just one canvas.</p>

<p>Now, the hard part of both of those problems are dealt with, and it&#8217;s no coincidence that they were both attacked in the same day.</p>

<!--more-->


<h2>State of Gladius</h2>

<p><img class="right" src="http://blog.robothaus.org//images/gladius-render-cube.png" width="400" title="Gladius Renders a Cube!" alt="Gladius Renders a Cube!">
Progress on Gladius has been steady as <a href="http://twitter.com/#!/alankligman">Alan</a> irons out the last bits of the framework for the looming 0.1 deadline (roughly, the end of December). Doing my part, <a href="http://robothaus.org/blog/blog/2011/11/23/gladius-gets-a-renderer/">last month</a> I boasted about Gladius getting a renderer, but it was really only partially complete.</p>

<p><strong>What it did</strong>: cleared the screen</p>

<p><strong>What it does now</strong>: builds models from meshes and materials, clears the screen, renders those models, and has a scheduled engine task to repeat it all</p>

<p>Clearly, today&#8217;s work yields far better results than the prior engine. Obviously, we still lack a lot of things that one would assume a modern graphics engine would provide to game developers, but this is a big leap.</p>

<p>In short here&#8217;s how preparing and rendering something looks like right now:</p>

<ol>
<li>create a Mesh resource from a script</li>
<li>create a Material resource from a script</li>
<li>create a Model component</li>
<li>create an Entity to hold the Model</li>
<li>feed the Mesh and Material to the Model</li>
<li>add the Model to the Entity</li>
<li><strong>render!</strong></li>
</ol>


<p>This approach is interesting because it relies heavily on prepared scripts to build resources like Meshes and Materials. For instance, there is a procedural cube Mesh resource which will (unsurprisingly) create a Mesh that is a cube. There is also a procedural sample Material resource which will create a simple red Material (no textures yet, just colour). Then, as a game developer, it&#8217;s easy to swap out Mesh or Material resources when you want different ones for a particular Model, and it&#8217;s easy to reuse the same procedural resource to give you lots of slightly different Meshes or Materials.</p>

<p>Also, it may not be entirely obvious that, by using procedural resources, we have the ability to build an entire game without loading a single bitmap or otherwise baked asset. Imagine building a game that involves a simulation of a universe. Instead of forcing the user to wait precious seconds to download a large file describing the shape and state of the universe, they can download a tiny piece of code which, when used with a particular seed, creates the same universe. This same technique can be used to create not only Meshes, Textures and Materials, but even Sounds, Forces, or Terrain. A lot of demoscene groups <a href="http://scene.org/awards.php">solve size problems similarly</a> to create the notoriously mind-boggling 64k intro category.</p>

<p>Just imaging stuffing this into 64k:</p>

<iframe width="420" height="315" src="http://www.youtube.com/embed/LkEsP9H2HGM" frameborder="0" allowfullscreen></iframe>


<p>Some have even attempted procedurally-generated games:</p>

<iframe width="420" height="315" src="http://www.youtube.com/embed/oeyerXaLKt0" frameborder="0" allowfullscreen></iframe>


<p>Coasting on back from that tangent, Gladius is a long way off from being able to produce these kind of effects, but that&#8217;s generally what we&#8217;ve got in mind. We want devs to really be able to use their imaginations with Gladius, especially in a climate where fat downloads are often not appreciated.</p>

<h2>Massaging CubicVR.js</h2>

<p>The way we&#8217;ve used CubicVR.js in the past it may be surprising that its in need of some caretaking. I don&#8217;t at all mean to imply that it lacks functionality. In fact, it continues to surprise me even as I flip through test html pages fixing syntax problems I&#8217;ve introduced. However, there are a couple pieces of CubicVR.js that require some love:</p>

<ul>
<li>The Octree implementation has drifted slightly from the progress and structure of the rest of the engine, so it needs repair. The current implementation looks a lot like Java (my fault), and there is a new, mostly complete JavaScript-inspired version called <a href="https://github.com/secretrobotron/octree.js">octree.js</a> which is available to start integrating.</li>
<li>CubicVR can only render to one canvas on a page. Rendering to multiple canvas elements should be possible, and indeed easy.</li>
</ul>


<p>Luckily, one of <a href="http://vocamus.net/dave/">Dave Humphrey&#8217;s</a> students has offered to help bring CubicVR.js&#8217; Octree back to life, which he has <a href="https://github.com/cjcliffe/CubicVR.js/pull/55">already completed</a> to some degree. Soon, while <strong>octree.js</strong> gets wedged in, we&#8217;ll need a few tests to compare it with CubicVR.js&#8217; current implementation &#8211; for sanity, and for speed comparison.</p>

<h3>Multi-&lt;canvas&gt; Support</h3>

<p>While getting Gladius&#8217; renderer in order today, I encountered a few pieces of CubicVR.js on which I&#8217;ve had a grudge for some while. The initialization sequence and global namespace CubicVR.js uses are cumbersome when being used inside a project like Gladius, which we&#8217;re trying to keep as flexible as possible. Much of my perturbation stems from the question, &#8220;What happens if the developers wants to render to two independent canvas elements on a page?&#8221;</p>

<p>Gladius is an especially good example because of its <strong>qunit testbed</strong>, which uses async tests likely more often than it should. Guess what that means. You probably thought, &#8220;multiple simultaenous renders!&#8221; Since we need to test multiple engine initialization scenarios, Gladius&#8217; testbed will try to render to one canvas multiple times, or several canvases once each. I chose to implement the latter because of the async demands, but I really sidestepped the problem by only rendering <strong>once in the entire test module</strong>, &#8230;which is bad. Our testbed will only get more complicated, so, CubicVR.js needs a multi-canvas solution fast.</p>

<h4>Digging Into the Problem</h4>

<p>It&#8217;s not immediately obvious what will happen if I try to do this with a current build of CubicVR.js (which I&#8217;ve actually <a href="https://github.com/cjcliffe/CubicVR.js/issues/56">documented</a>):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>var gl1 = CubicVR.init( canvas1 );
</span><span class='line'>var gl2 = CubicVR.init( canvas2 );</span></code></pre></td></tr></table></div></figure>


<p>In fact, we end up in something of an undefined space here when we want to continue to use both canvases. When you create a texture, to which WebGL context does it belong? If you try to find out, Firefox will likely crash like it did for me today a few dozen times while I debugged the situation.</p>

<p>Initially, <a href="https://github.com/cjcliffe/CubicVR.js/commit/716cfd88f02dad4e69b8159eb4e237e96d458914#commitcomment-638203">I attempted to solve this problem</a> in a stateful manner, inspired by the way WebGL itself works:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>CubicVR.setContext(gl1,true);
</span><span class='line'>var m1 = new CubicVR.Texture();
</span><span class='line'>CubicVR.setContext(gl2,true);
</span><span class='line'>var m2 = new CubicVR.Texture();</span></code></pre></td></tr></table></div></figure>


<p>In this simple example, we attempt to create a Texture for each canvas. It&#8217;s clear which canvas owns which Texture, since explicitly switch &#8220;contexts&#8221;. <a href="https://github.com/cjcliffe/CubicVR.js/commit/716cfd88f02dad4e69b8159eb4e237e96d458914#commitcomment-638203">CJ was quick to point out the pitfalls of this approach</a>, and the problem sat unanswered for a while. Just think of the nightmare inflicted upon forgetful developers when they leave out a <code>setContext</code> somewhere and don&#8217;t understand why their renders are incorrect.</p>

<h4>Proposal for a Solution</h4>

<p>Fortunately, there was some work done to create <a href="https://github.com/cjcliffe/CubicVR.js/issues/21">Automated Testing</a> which skirted on the multiple-canvas problem, but didn&#8217;t fully address it. It did, however influence the codebase in a way which was already occuring in several other spots.</p>

<p>CubicVR.js uses a require.js-like module structure, where modules are registered by the core, and executed before initialization to ensure the CubicVR namepsace is full of all the goodies that one would expect, like <code>CubicVR.Mesh</code> and <code>CubicVR.Scene</code>. What I hadn&#8217;t thought of before was how much this design allows the isolated initialization of each module.</p>

<p>In other words, why not initialize each registered module whenever a call to <code>CubicVR.init</code> or <code>CubicVR.start</code> occurs, and attach the result to a properly scoped object? So, I did just that: I forced much of the CubicVR namepsace into a closure which is instantiated by a call to <code>CubicVR.init</code> or <code>CubicVR.start</code>. My proposed solution makes the return value from a call to either function a CubicVR &#8220;Core&#8221;, which is basically the entire namespace as you would expect it. For example:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>var core = CubicVR.init( canvas ),
</span><span class='line'>    texture = core.Texture( "lolcat.gif" ),
</span><span class='line'>    mesh = core.Mesh( catMeshDescription ),
</span><span class='line'>    scene = core.Scene( 300, 300, 60 );
</span><span class='line'>
</span><span class='line'>mesh.prepare();
</span><span class='line'>var sceneObj = core.SceneObject( mesh );
</span><span class='line'>
</span><span class='line'>scene.bind( sceneObj );
</span></code></pre></td></tr></table></div></figure>


<p>This code looks remarkably similar to current CubicVR.js code. In case you missed it, the only change is that I&#8217;ve replaced every <code>CubicVR</code> with <code>core</code>, except where completely necessary, like initialization. With this method, only a few fundamental functions are attached to the global <code>CubicVR</code> namespace, so many versions of CubicVR.js&#8217; modules can exist in harmony.</p>

<p><img class="right" src="http://blog.robothaus.org//images/cubicvr-multi-canvas.png" width="600" title="CubicVR Multi-Canvas Demo" alt="CubicVR Multi-Canvas Demo"> The <a href="https://github.com/secretrobotron/CubicVR.js/tree/instancing">branch in which I&#8217;m doing my multi-canvas support work</a> contains a test of the functionality in the <em>instancing</em> test folder, which demonstrates the ability to have CubicVR.js render to multiple canvas elements, and control them independently with respective  <code>MouseViewController</code> objects. I thought interactivity would be particularly convincing.</p>

<p>A bit more work needs to be done to update the rest of the tests and samples with the syntax change I&#8217;ve proposed, and I&#8217;d like to get some more feedback from CJ, but I&#8217;d say this work puts us in the position to have multiple canvas support in the very near future of CubicVR.js.</p>

<h2>Rendering a Conclusion&#8230;</h2>

<p>I&#8217;m glad Paladin and Gladius are affecting CubicVR.js so positively. The focus is forcing me to address problems that are otherwise easy to ignore. More importantly, however, there is much more activity from contributors recently, many of which are involved in Dave Humphrey&#8217;s courses at Seneca. The sudden increase in bug fixes and feature additions in Paladin as a whole &#8211; constituents included &#8211; is really starting to show in the way of more stable tools.</p>

<p>More progress soon. Expect big things.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Gamepad Vibration - Zero to Hero]]></title>
    <link href="http://blog.robothaus.org//2011/12/09/gamepad-vibration-zero-to-hero/"/>
    <updated>2011-12-09T23:31:00-05:00</updated>
    <id>http://blog.robothaus.org//2011/12/09/gamepad-vibration-zero-to-hero</id>
    <content type="html"><![CDATA[<p>If you&#8217;ve been paying attention to the recent surge of activity surrounding implementation of game-related browser API&#8217;s, you&#8217;ve probably already caught wind of Mozilla&#8217;s stream of cross-platform <a href="http://www.youtube.com/watch?v=50BRBE_-H2E&amp;feature=related">gamepad-ready try-builds</a>, <a href="https://github.com/jbuck/input.js">supporting libraries</a> and working <a href="http://arewefunyet.com/rescuefox/src/">RescueFox demos</a>, or perhaps <a href="http://www.conceivablytech.com/9909/products/chrome-first-to-get-much-anticipated-gamepad-api">Google&#8217;s announcement</a> of Scott Graham&#8217;s Gamepad API and his <a href="http://boxes-wot-shoot.appspot.com/">game to show it off</a>.</p>

<p>Today, I&#8217;m happy to announce that, as far as I know, the first working prototype of force-feedback (rumble) support is <a href="https://github.com/secretrobotron/mozilla-central/tree/gamepad-api-vibration">available to play with</a>. I also forked Jon Buckley&#8217;s <a href="https://github.com/secretrobotron/input.js/tree/gamepad-api-vibration">input.js</a> to <a href="https://github.com/secretrobotron/input.js/blob/9a137549e7cede2ba2ab305c7f0a5c73f6b7a606/raw-gamepad-api-test-vibration.html">augment its test suite</a> with vibrational goodness.</p>

<!-- more -->


<h2>Zero &rarr; Discovery</h2>

<p>Watching Dave Humphrey <a href="http://vocamus.net/dave/?cat=28">lead a class of students</a> at Seneca College to implement mouse locking in Firefox really inspired me to do some digging for myself. It was an enlightening experience to partake in one of his classes and subsequently hack around with <a href="http://twitter.com/#!/jbuckca">Jon Buckley</a> on adding XInput support to <a href="http://twitter.com/#!/TedMielczarek">Ted Mielczarek</a>&#8217;s recent Firefox Gamepad API implementation.</p>

<h3>Optimism with FF_RUMBLE and Friends</h3>

<p>Obviously, having rumble support is strictly cooler than input-only gamepad support (which is already very cool). So, I thought about how to get it done which was ostensibly straightforward, as things usually are at first. My development system of choice being Linux, I dove into some google searches for generic rumble support and found some <a href="http://freegamedev.net/wiki/Force_Feedback">helpful, but scant and unsupported information</a>. Getting operational info from my gamepad was the easy part.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>unsigned long features[4];
</span><span class='line'>int num_effects;
</span><span class='line'>
</span><span class='line'>ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features); 
</span><span class='line'>ioctl(fd, EVIOCGEFFECTS, &num_effects);</span></code></pre></td></tr></table></div></figure>


<p>Actually causing some force feedback to occur requires two steps:</p>

<ol>
<li>Prepare an &#8220;effect&#8221;, which is a reusable construct that tells something which supports force feedback how to react. They have different types like <em>FF_RUMBLE</em>, or <em>FF_PERIODIC</em>, which you can find out by bit-testing the <em>features</em> array against these constants. Once it&#8217;s created, upload it to the controller. You can upload several effects in a row and activate them at will.</li>
<li>When force feedback actually needs to occur (for instance, when your car hits a wall, or some reasonable physics-related facsimile thereof), create an &#8220;input event&#8221; to activate an effect, and upload it to the controller.</li>
</ol>


<p>The <em>C++</em> looks a bit like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>//setup effect
</span><span class='line'>ff_effect effect;
</span><span class='line'>effect.type = FF_RUMBLE;
</span><span class='line'>effect.id = -1;
</span><span class='line'>effect.u.rumble.strong_magnitude = 0;
</span><span class='line'>effect.u.rumble.weak_magnitude   = 0xc000;
</span><span class='line'>effect.replay.length = 5000;
</span><span class='line'>effect.replay.delay  = 0;
</span><span class='line'>
</span><span class='line'>//upload effect
</span><span class='line'>ioctl(fd, EVIOCSFF, &effect);</span></code></pre></td></tr></table></div></figure>


<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>//setup input event
</span><span class='line'>struct input_event play;
</span><span class='line'>play.type = EV_FF;
</span><span class='line'>play.code = effect.id;
</span><span class='line'>play.value = 1;
</span><span class='line'>
</span><span class='line'>//upload input event
</span><span class='line'>write(fd, (const void*) &play, sizeof(play));</span></code></pre></td></tr></table></div></figure>


<p>One thing I need to point out is that you <strong>can</strong> run out of space rather quickly on your device. My Logitech F510 can hold 15 independent effects, and it&#8217;s not entirely obvious how to reuse an effect without taking up another effect slot on your gamepad.</p>

<p>The key is this line:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>effect.id = -1;</span></code></pre></td></tr></table></div></figure>


<p>If the effect id is set to <code>-1</code>, uploading the effect will reset it to something <code>&gt; -1</code> to identify the effect. If your code sets it to <code>-1</code> again, you will upload a <strong>new</strong> effect with the same properties, and the struct will get a new id. However, if you just want to update the effect&#8217;s parameters and re-upload it, <strong>don&#8217;t touch the id</strong>. An id of greater than <code>-1</code> will be assumed to identify an effect that already exists.</p>

<p>So, we create effects, upload them, activate them, and that should be it, right? Problem solved! Mission accomplished! Gamepad is vibrating&#8230;! Well, no, not quite&#8230;</p>

<h3>Trouble</h3>

<p>Alas, initially, uploading effects always failed (returned <code>-1</code> from <em>ioctl</em>). I searched around for some answers, and discovered three imporant things:</p>

<ol>
<li><em>ff_test</em>, a tiny program that tests force feedback for devices in linux, exists and comes in most Linux package managers with <em>jscal</em>, <em>joystick-utils</em>, or just by itself.</li>
<li>In order for <em>O_RDWR</em> access to be granted when opening an fd (so you can actually write to the device and not just read from it), you need to have full access to the device. Fortunately, this is easily temporarily attainable with <em>sudo</em>.</li>
<li>As far as I can tell, support for the PS3 controller with default Linux gamepad drivers won&#8217;t give you force-feedback. I&#8217;m still not entirely convinced I&#8217;m not doing something wrong, but purchasing a Logitech F510 for testing instead made me care a lot less (because it just works).</li>
</ol>


<p>Of course, <em>ff_test</em> didn&#8217;t want to install cleanly on my x86_64 Arch Linux setup, but luckily, finding and compiling the <em>ff_test</em> <a href="http://www.koders.com/c/fidF60D5962FCA8B937A6D0947AA81AE95A8C58FB36.aspx">source code</a> was a tremendous help to my progress anyway. <strong>Here, I had a working piece of software that let me <em>read</em> how it made force-feedback work.</strong> Learning by example is incredible in these situations.</p>

<p>I made so much progress in finding out what I needed and what I had been doing wrong, and was understandably excited to arrive back at my desk, rip open my new F510, and plug it in, almost all in one continuous motion. Then, disappointment: still no rumble, even though the words &#8220;Rumble Support&#8221; decorated the gamepad&#8217;s box. After a bit more digging and help from Ted, I found out that you can&#8217;t actually write to <em>/dev/input/jsXX</em> fd&#8217;s; you have to use their <em>/dev/input/eventXX</em> counterpart! Using the <em>event</em> instead, my F510 started to come alive.</p>

<p>The really fun part is finding out which <em>event</em> device corresponds to a given <em>js</em> device. If you&#8217;re trying <em>ff_test</em> for yourself and failing, try</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>sudo ff_test /dev/input/eventXX</span></code></pre></td></tr></table></div></figure>


<p>where <em>XX</em> is the correct device number corresponding to your gamepad. If you don&#8217;t know which one is the one you want, read <em>dmesg</em> or just&#8230; try them all (start with 0). But, if you want this to happen programatically, say in Firefox, you need a different, more dependable solution.</p>

<h3>udev</h3>

<p>Thankfully, udev will get you most of the way there. If you&#8217;re running a modern Linux distro, you should have access to <em>udevadm</em> which will give you a lot of useful information about HID&#8217;s. Try this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>for i in /dev/input/js*;do udevadm info --query=all --name=$i;done</span></code></pre></td></tr></table></div></figure>


<p>Gamepads and their corresponding <em>event</em> devices will have an entry reading <em>ID_INPUT_JOYSTICK=1</em> which is very helpful for identification. However , if you have more than one gamepad plugged in, they will all have that property. So, we need to do some matching based on another property. I decided on <em>ID_PATH</em> because looks like some sort of usb identifier. This might change in favour of a quicker, or less sloppy method, but here&#8217;s how I&#8217;m doing it right now:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>bool
</span><span class='line'>LinuxGamepadService::is_gamepad_event(struct udev_device* dev) {
</span><span class='line'>  if (!mUdev.udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
</span><span class='line'>    return false;
</span><span class='line'>  
</span><span class='line'>  const char* devpath = mUdev.udev_device_get_devnode(dev);
</span><span class='line'>  const char kJS[] = "/dev/input/event";
</span><span class='line'>  if ( !devpath || strncmp(kJS, devpath, sizeof(kJS) - 1) != 0)
</span><span class='line'>    return false;
</span><span class='line'>
</span><span class='line'>  return true;
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>struct udev_device* 
</span><span class='line'>LinuxGamepadService::get_corresponding_event_device(const char* cmp_id_path) {
</span><span class='line'>
</span><span class='line'>  struct udev_enumerate* en = mUdev.udev_enumerate_new(mUdev.udev);
</span><span class='line'>  mUdev.udev_enumerate_add_match_subsystem(en, "input");
</span><span class='line'>  mUdev.udev_enumerate_scan_devices(en);
</span><span class='line'>  struct udev_list_entry *dev_list_entry;
</span><span class='line'>  for (dev_list_entry = mUdev.udev_enumerate_get_list_entry(en);
</span><span class='line'>       dev_list_entry != NULL;
</span><span class='line'>       dev_list_entry = mUdev.udev_list_entry_get_next(dev_list_entry)) {
</span><span class='line'>    const char* path = mUdev.udev_list_entry_get_name(dev_list_entry);
</span><span class='line'>    struct udev_device* dev = mUdev.udev_device_new_from_syspath(mUdev.udev, path);
</span><span class='line'>    const char* id_path = mUdev.udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
</span><span class='line'>
</span><span class='line'>    if (is_gamepad_event(dev) && 
</span><span class='line'>        strncmp( cmp_id_path, id_path, sizeof(cmp_id_path) - 1) == 0 ) {
</span><span class='line'>      return dev;
</span><span class='line'>    }
</span><span class='line'>  }
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>  return NULL;
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>const char* id_path = mUdev.udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
</span><span class='line'>struct udev_device* event = get_corresponding_event_device(id_path);
</span></code></pre></td></tr></table></div></figure>


<p><strong>NOTE</strong>: There is <strong>definitely</strong> a bug in this code, which I will fix as soon as I feel like programming again. Try to spot it: it&#8217;s something to do with the property I&#8217;m trying to use for comparison (<em>ID_PATH</em>). I get the matching <em>event</em> device, but until I fix this bug, it will only work in some cases.</p>

<p>Using this technique, I was able to consistently open two associated file descriptors: <em>/dev/input/js0</em> for reading, and <em>/dev/input/event11</em> for writing. And guess what. My gamepad started rumbling.</p>

<h3>DOM</h3>

<p>Somehow, the easiest part of all of this work was getting the JavaScript Gamepad object to expose a new set of functions for vibration. Simply altering <em>nsIDOMGamepad.idl</em> gave me most of what I wanted, and I was able to find and copy a couple of other existing IDL files which gave me the rest.</p>

<p>Just to be able to set the vibration state, I needed a function in the gamepad interface like this,</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>void setVibration(in unsigned long duration, in unsigned long strongMagnitude, in unsigned long weakMagnitude);</span></code></pre></td></tr></table></div></figure>


<p>which, on the implementation side, turned into C++ code like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>NS_IMETHODIMP nsDOMGamepad::SetVibration(PRUint32 aDuration, PRUint32 aStrongMagnitude, PRUint32 aWeakMagnitude, PRUint8 aArgsc) 
</span><span class='line'>{
</span><span class='line'>  mGamepadService-&gt;SetGamepadVibration(mIndex, aDuration, aStrongMagnitude, aWeakMagnitude);
</span><span class='line'>  return NS_OK;
</span><span class='line'>}
</span></code></pre></td></tr></table></div></figure>


<p>Ted&#8217;s Gamepad API code is split up so that gamepad services are implemented on a per-OS basis, but they all inherit from a <em>GamepadService</em> class. So, in the <em>GamepadService</em> and  <em>LinuxGamepadService</em> classes, I created <em>SetGamepadVibration</em> to deliver vibration state changes from <em>Gamepad</em> DOM objects to the service.</p>

<p>The only piece left was actually talking to the device.</p>

<h3>Using <code>select</code> and <code>pipe</code></h3>

<p>Having done a considerable amount of detective work, learning about udev and the kernel in the process, the end felt near. I just needed to wire up the browser to push values from JavaScript to the gamepad service. The trouble was that an OS-specific service ran in its own thread to talk to devices, using <em>select</em> to wait for events, processing them, and sending consequent messages to the main thread. So, getting data to move in the other direction (from DOM objects to the Gamepad service; from the main thread to the service thread) would have to involve <em>pipes</em>.</p>

<p><em>pipes</em> are simple communication channels which two running bodies (threads, processes, etc.) can use to send each other information. In simple terms, a <code>pipe</code> can be written to with <code>write</code>, and then read from with <code>read</code>, and all the data should arrive in the order it was sent. So, sending a vibration event over a pipe is as simple as writing a few pieces about the event in sequence. So, the main thread writes to the pipe like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>write(mVibePipefds[1], &index, sizeof(index));
</span><span class='line'>write(mVibePipefds[1], &duration, sizeof(duration));
</span><span class='line'>write(mVibePipefds[1], &strongMag, sizeof(strongMag));
</span><span class='line'>write(mVibePipefds[1], &weakMag, sizeof(weakMag));</span></code></pre></td></tr></table></div></figure>


<p>where <code>mVibePipefds</code> is the <code>pipe</code>, <code>mVibePipefds[1]</code> is the end of the pipe to which we write, and <code>&amp;index</code>, <code>&amp;duration</code>, <code>&amp;strongMag</code>, and <code>&amp;weakMag</code> are the pieces of data we want to deliver to the service.</p>

<p>On the receiving end the code looks incredibly similar:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>read(self-&gt;mVibePipefds[0], &index, sizeof(index));
</span><span class='line'>read(self-&gt;mVibePipefds[0], &duration, sizeof(duration));
</span><span class='line'>read(self-&gt;mVibePipefds[0], &strongMag, sizeof(strongMag));
</span><span class='line'>read(self-&gt;mVibePipefds[0], &weakMag, sizeof(weakMag));</span></code></pre></td></tr></table></div></figure>


<p>the only different being <code>mVibePipefds[0]</code> which is the end of the pipe from which we <strong>read</strong>.</p>

<p>I put together a handy function to finish the job:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>void
</span><span class='line'>LinuxGamepadService::SetVibration(Gamepad& gamepad, PRUint32 duration, PRUint32 weakMag, PRUint32 strongMag)
</span><span class='line'>{
</span><span class='line'>  if (gamepad.eventFd &gt; -1) {
</span><span class='line'>
</span><span class='line'>    //set up the effect
</span><span class='line'>    //TODO: safety-check effect values
</span><span class='line'>    gamepad.ffEffect.u.rumble.strong_magnitude = strongMag;
</span><span class='line'>    gamepad.ffEffect.u.rumble.weak_magnitude = weakMag;
</span><span class='line'>    gamepad.ffEffect.replay.length = duration;
</span><span class='line'>    gamepad.ffEffect.replay.delay = 0;
</span><span class='line'>
</span><span class='line'>    //upload the effect
</span><span class='line'>    if (ioctl(gamepad.eventFd, EVIOCSFF, &(gamepad.ffEffect)) == -1) {
</span><span class='line'>      printf("failed to set vibration :(\n");
</span><span class='line'>    }
</span><span class='line'>
</span><span class='line'>    //set up the command
</span><span class='line'>    gamepad.ffInputEvent.type = EV_FF;
</span><span class='line'>    gamepad.ffInputEvent.code = gamepad.ffEffect.id;
</span><span class='line'>    gamepad.ffInputEvent.value = 1;
</span><span class='line'>
</span><span class='line'>    //upload the command
</span><span class='line'>    if (write(gamepad.eventFd, (const void*) &(gamepad.ffInputEvent), sizeof(gamepad.ffInputEvent)) == -1) {
</span><span class='line'>      printf( "failed to activate vibration :(\n" );
</span><span class='line'>    }
</span><span class='line'>  } //if
</span><span class='line'>}
</span></code></pre></td></tr></table></div></figure>


<p>Most of the code here is code I&#8217;ve already explained, so none of it should be surprising. I just wanted to consolidate it properly. Note, though, that it&#8217;s incomplete (as you can plainly see), and note that I&#8217;m not ever changing the <code>id</code> of <code>gamepad.ffEffect</code>, so I reuse only one effect per device.</p>

<p>Finally, some on-command rumbling from JavaScript.</p>

<h3>Proof</h3>

<p>As soon as I had this working from front to back, I had to prove it to the web. So, I filmed it:</p>

<iframe width="420" height="315" src="http://www.youtube.com/embed/TPcWW1p2Ss8" frameborder="0" allowfullscreen></iframe>


<h2>On to the Hero Phase</h2>

<p>Getting this work past the prototype phase is going to take some work:</p>

<ul>
<li>Implementing cross-platform support, which means augmenting the Windows and OSX services at least.</li>
<li>Getting the community to help decide on a good API. There is a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=679966">WebVibrator bug</a> with which it would be useful to coordinate so that using any vibration capabilities on the web seems similar and natural. It&#8217;s also very useful to replicate something like <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ee417001(v=vs.85).aspx#setting_vibration">XInput&#8217;s API</a> for vibration, since it&#8217;s already used (a lot) in practice, and a lot of devices are being produced to support XInput specifically.</li>
</ul>


<p>If you&#8217;d like to follow along, add yourself to the <em>cc</em> list on the <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=680289">bug which I&#8217;ve very recently inherited</a>. Let me know your thoughts. More progress soon.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Popcorn Maker 0.1 - For The Developers]]></title>
    <link href="http://blog.robothaus.org//2011/11/24/popcornmaker-0-dot-1-for-the-developers/"/>
    <updated>2011-11-24T10:30:00-05:00</updated>
    <id>http://blog.robothaus.org//2011/11/24/popcornmaker-0-dot-1-for-the-developers</id>
    <content type="html"><![CDATA[<p>After my recent stint in Europe for the Mozilla Festival and MozCamp I had the opportunity to reflect on the development environment of Popcorn Maker, courtesy of the 8 hour flight from Berlin to New York. The result is a version of Popcorn Maker which can be proudly labeled as <strong>0.1</strong>, and very friendly to developers.</p>

<!-- more -->


<h2>History Lesson: Butter</h2>

<p><img class="left" src="http://blog.robothaus.org//images/butter.jpg" width="400" title="Butter" alt="Butter"> Butter was an initial exploration of making a GUI for producers hoping to use popcorn.js for their projects. The app enjoyed some success, but it was eclipsed by the need to expand the app in different directions, which I have <a href="http://mozillapopcorn.org/unsalted-thinking-roadmapping-butter/">written about previously</a>. Like most promising experiments, it needed to be rewritten to be the powerful app we hoped it would become.</p>

<p>Consequently, there was a lot of discussion about how to engineer the experience which was to become Popcorn Maker, which Brett and Ben detailed in their post, <a href="http://mozillapopcorn.org/designing-the-popcorn-maker/">Designing the Popcorn Maker</a>. In short, the current goal is to create a web app which encompasses the same ideas as Butter, but makes traditional content producers (usually of the film persuasion) comfortable by drawing memes from other popular pieces of software. However, a functioning web app is only a piece of the puzzle, since harnessing community involvement is a very powerful concept. Eventually, users will be able to use Popcorn Maker like they use WordPress &#8211; by downloading and installing the app on a machine of their choice &#8211; but with unique sharing capabilities and editing opportunities, like publishing your <em>popcornized</em><sup><sup>tm</sup></sup> template which might be a remix of something like <a href="http://www.youtube.com/watch?v=BeGjTiUqw8U">a youtube video page</a>.</p>

<h2>Require Saves The Day</h2>

<p><img class="right" src="http://blog.robothaus.org//images/butter-to-pm.png" width="400" title="Butter To Popcorn Maker" alt="Butter -&gt; Popcorn Maker"> The subsequent struggle to design an acceptable user experience and its contention with pace of development and fluctuating feature requests hasn&#8217;t offered the opportunity for Popcorn Maker&#8217;s codebase to settle. As a result, Popcorn Maker&#8217;s developers have had to suffer through a poor debugging experience and a sub-optimal development environment.</p>

<p>Now, <strong>that has all changed</strong>. Some geniune thinking time and my recent exploits inside <a href="https://wiki.mozilla.org/Paladin">Paladin</a> with <a href="https://github.com/jrburke">James Burke</a>, the creator of <a href="http://requirejs.org/">require.js</a>, has changed Popcorn Maker for the better &#8211; for developer and users alike.</p>

<p>Using <em>require.js</em>, I was able to split up Butter&#8217;s core and constituent modules into several files respectively. require&#8217;s module system makes this process natural and provides a method for producing dependency chains. So, Butter&#8217;s core relies on the existence of several smaller modules like <em>Media</em>, <em>Track</em>, and <em>TrackEvent</em>, while Butter&#8217;s Timeline module depends on some of the same (plus some others). In other words, dependency chains let a module rely on other modules which may be already shared amongst others still. Of course, this is nothing new to traditional developers who get to use <code>#import</code> to solve a similar problem.</p>

<p>Now, developers are able to isolate and fix bugs without involving a compilation process every time they make a change (since Butter is best suited as a library inside of Popcorn Maker).</p>

<h3>Onward: To 0.1</h3>

<p><img class="left" src="http://blog.robothaus.org//images/butter-pm-modules.png" width="400" title="Popcorn Maker + Butter Setup" alt="Popcorn Maker + Butter Setup"> Since Popcorn Maker is an app that wraps Butter, developers can contribute from several perspectives:</p>

<ul>
<li><strong>Popcorn Maker</strong>: developing the app itself (UI, storage, exporting, etc.)</li>
<li><strong>Templates</strong>: building general-purpose Butter templates to let users jump into editing a powerful popcorn experience quickly</li>
<li><strong>Editors</strong>: providing html-based editors for popcorn plugins which would benefit from specific UI (e.g. Google Maps)</li>
<li><strong>Butter</strong>: enhancing the core of Butter to fix bugs or provide better functionality or compatibility (standards like to change)</li>
</ul>


<p>The beauty of the approach we&#8217;ve taken in 0.1 is that these elements are as decoupled as possible because they are actually separate require projects. Then, it&#8217;s no problem at all to have them share components, and they can depend on each other to provide a nice unidirectional development flow.</p>

<p>Part of the magic is a pattern which James has worked on that circumvents the <code>data-main</code> attribute traditionally attached to a require.js script tag. Instead, a script like this is referenced (in this case, <code>butter.previewer.js</code>):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>(function () {
</span><span class='line'>    // Stub for has function.
</span><span class='line'>    function has() {
</span><span class='line'>        return true;
</span><span class='line'>    }
</span><span class='line'>
</span><span class='line'>    if ( has( 'source-config' ) ) {
</span><span class='line'>        // Get the location of the butter source.
</span><span class='line'>        // The last script tag should be the butter source
</span><span class='line'>        // tag since in dev, it will be a blocking script tag,
</span><span class='line'>        // so latest tag is the one for this script.
</span><span class='line'>        var scripts = document.getElementsByTagName( 'script' ),
</span><span class='line'>        path = scripts[scripts.length - 1].src;
</span><span class='line'>        path = path.split( '/' );
</span><span class='line'>        path.pop();
</span><span class='line'>        path = path.join( '/' ) + '/';
</span><span class='line'>
</span><span class='line'>        document.write( '&lt;script src="' + path + '../../external/require/require.js"&gt;&lt;/' + 'script&gt;' );
</span><span class='line'>
</span><span class='line'>        document.write('&lt;script&gt;' + 
</span><span class='line'>          '(function(){' + 
</span><span class='line'>          'var ctx = require.config({ ' + 
</span><span class='line'>            'baseUrl: "' + path + '../",' +
</span><span class='line'>            'context: "butter.previewer",' +
</span><span class='line'>            'paths: {' +
</span><span class='line'>              // Paths are relative to baseUrl; Notice the commas!
</span><span class='line'>            '}' +
</span><span class='line'>          '});' +
</span><span class='line'>          'ctx(["previewer/previewer"])' + 
</span><span class='line'>          '})()' +
</span><span class='line'>        '&lt;/script&gt;');
</span><span class='line'>    }
</span><span class='line'>
</span><span class='line'>    var ButterTemplate = function() {
</span><span class='line'>      if ( !ButterTemplate.__waiting ) {
</span><span class='line'>        ButterTemplate.__waiting = [];
</span><span class='line'>      } //if
</span><span class='line'>      ButterTemplate.__waiting.push( arguments );
</span><span class='line'>    }; //ButterTemplate
</span><span class='line'>
</span><span class='line'>    if ( !window.ButterTemplate ) {
</span><span class='line'>      window.ButterTemplate = ButterTemplate;
</span><span class='line'>    } //if
</span><span class='line'>
</span><span class='line'>}());</span></code></pre></td></tr></table></div></figure>


<p>This pattern, which writes its own require.js-specific script tag when necessary (when not compiled), takes advantage of require contexts (returned from a call to <code>require.config()</code>), and is easily generalized. In fact, it&#8217;s used to load event editors, and butter itself. In other words, Butter (and Popcorn Maker, by extension) contains several require projects, each of which can be compiled to provide a minified version. Butter is actually a collection of libraries which can be included independently to provide functionality specific to things like Templates or Event Editors.</p>

<p>So, to create a simple Butter Template, all you need is this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;html&gt;
</span><span class='line'>  &lt;head&gt;
</span><span class='line'>    &lt;script src="../../lib/popcorn/popcorn.js"&gt;&lt;/script&gt;
</span><span class='line'>    &lt;script src="../../lib/popcorn/popcorn.subtitle.js"&gt;&lt;/script&gt;
</span><span class='line'>    &lt;script src="../../butter/src/previewer/butter.previewer.js"&gt;&lt;/script&gt;
</span><span class='line'>    &lt;script&gt;
</span><span class='line'>      /* Uncomment to provide custom functionality
</span><span class='line'>        ButterTemplate(function() {
</span><span class='line'>          // namespace has loaded; create your custom template here
</span><span class='line'>        });
</span><span class='line'>      */
</span><span class='line'>    &lt;/script&gt;
</span><span class='line'>  &lt;/head&gt;
</span><span class='line'>  &lt;body&gt;
</span><span class='line'>    &lt;div id="main" data-butter="media"&gt;&lt;/div&gt;
</span><span class='line'>    &lt;div id="subtitle-container" data-butter="target"&gt;&lt;/div&gt;
</span><span class='line'>  &lt;/body&gt;
</span><span class='line'>&lt;/html&gt;</span></code></pre></td></tr></table></div></figure>


<p>Similarly, to create an Event Editor, you need this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;html&gt;
</span><span class='line'>  &lt;head&gt;
</span><span class='line'>    &lt;script type="text/javascript" src="../../butter/src/eventeditor/butter.editors.js"&gt;&lt;/script&gt;
</span><span class='line'>    &lt;script&gt;
</span><span class='line'>        ButterEditor(function() {
</span><span class='line'>          // namespace has loaded; create your editor here
</span><span class='line'>        });
</span><span class='line'>    &lt;/script&gt;
</span><span class='line'>  &lt;/head&gt;
</span><span class='line'>&lt;/html&gt;</span></code></pre></td></tr></table></div></figure>


<p>Finally, Butter can be imported in the same way (in Popcorn Maker, for example):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;html&gt;
</span><span class='line'>  &lt;head&gt;
</span><span class='line'>    &lt;title&gt;Popcorn Maker&lt;/title&gt;
</span><span class='line'>    &lt;script type="text/javascript" src="lib/require/require.js"&gt;&lt;/script&gt;
</span><span class='line'>    &lt;script type="text/javascript" src="butter/src/butter.js"&gt;&lt;/script&gt;
</span><span class='line'>    &lt;script type="text/javascript" src="js/main.js"&gt;&lt;/script&gt;
</span><span class='line'>  &lt;/head&gt;
</span><span class='line'>&lt;/html&gt;</span></code></pre></td></tr></table></div></figure>


<p>Notice the extra require inclusion here, which is necessary only because <strong>Popcorn Maker is also a require.js project</strong>. This way <code>butter.js</code> does not depend on Popcorn Maker to include require.js, and every layer of the project can be nicely compiled.</p>

<p>To reiterate, the very best part is that there is no need to re-compile Butter if a change needs to be made. require.js will let us compile the project only when we want to distribute it.</p>

<h2>Enjoy</h2>

<p>So, have a look at the app at <a href="http://mozillapopcorn.org/maker">mozillapopcorn.org/maker</a>(on Monday), or <a href="http://github.com/secretrobotron/popcorn-maker">clone the source</a> for yourself to see what you can do locally. As always, reporting bugs is paramount, and is worth almost as much as fixing them. Check out our <a href="https://webmademovies.lighthouseapp.com/projects/80723/milestones/130250-01">0.1 milestone on our lighthouse project</a>, where you&#8217;ll almost certainly find a plethora of things to do.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Gladius Gets a Renderer]]></title>
    <link href="http://blog.robothaus.org//2011/11/23/gladius-gets-a-renderer/"/>
    <updated>2011-11-23T23:22:00-05:00</updated>
    <id>http://blog.robothaus.org//2011/11/23/gladius-gets-a-renderer</id>
    <content type="html"><![CDATA[<p>I just finished committing and issuing a <a href="https://github.com/alankligman/Gladius/pull/66">pull request</a> for the bones of Gladius&#8217; graphics service. Alan and I have been talking about how to do this properly for at least two months now, so it really feels good to write down some code. Here&#8217;s how it looks:</p>

<!--more-->


<h2>A little explanation first&#8230;</h2>

<p>Keeping modern game engine design in mind, Gladius implements an <em>Entity/Component</em> system where <em>Components</em> such as Models or Cameras are added to <em>Entities</em> to give them functionality. Components are supported by different systems called <em>Services</em>, like the <em>Graphics</em> service. This approach circumvents the need for a bloated, all-inclusive base class from which plants, paladins, and planets all inherit to receive common functionality. Instead, with a proper dependency system, a <em>Camera</em> or <em>Model</em> component can insist that there is a <em>Transform</em> component on the entity to which it&#8217;s being added so it can move through &#8211; and interact with &#8211; space without spilling remnants of itself onto other game objects through inheritance.</p>

<p>So, making an object appear on the screen is as simple as adding a model component to an entity in a scene. Of course, a model needs data, and there are plans being forged to support all sorts of inputs for meshes. However, that&#8217;s another discussion entirely. Right now, all you need to know is that Gladius has the fundamentals in place to build basic procedural meshes (a cube, really).</p>

<h3>Rendering</h3>

<p><img class="right" src="http://blog.robothaus.org//images/graphics-test.png" title="Graphics Test" > The graphics service can achieve a complete render of the entire engine&#8217;s state by following this short, pithy, pseudo-code:</p>

<ol>
<li>For each scene in the engine, find two lists: cameras and models.</li>
<li>For each active camera in a scene, render all of the scene&#8217;s models.</li>
</ol>


<p>This tiny algorithm translates into this longer, Gladiusized-pseudo-code:</p>

<ol>
<li>Clear the current drawable object (right now, a &lt;canvas&gt;).</li>
<li>For each scene in the engine, get lists of entities with camera and model components respectively.</li>
<li>For each entry in the camera list, grab the camera component; if its an active camera, render the mesh in each model in the model list.</li>
</ol>


<p>It&#8217;s a great starting point, but there are very obvious inefficiencies and missing features in the above process, including&#8230;</p>

<ul>
<li><strong>The inability to render to anything except a canvas</strong>: In the near future, we&#8217;ll write code to present the proper abstractions to developers which will allow different renderables to be rendered for a given camera. You may want to render to a buffer, a canvas, or a textbox. We were thinking about naming these <em>Films</em>, but <em>Renderable</em> (which is less cool) might also work.</li>
<li><strong>A lack of culling</strong>: Right now, rendering from the perspective of a camera is expensive in a large scene with many objects. We need a way to slice up the scene so that we can discard what&#8217;s not in view and only render what&#8217;s important. But, don&#8217;t worry: my <a href="https://github.com/secretrobotron/octree.js">octree.js</a> library is nearing completion and will soon fill the void for a space partition culling mechanism. It&#8217;s very similar in spirit to the octree implementation in <a href="https://github.com/cjcliffe/CubicVR.js/blob/master/source/CubicVR.Octree.js">CubicVR.js</a> which I also wrote (unfortunately, with strong evidence of the job I did porting it from a Java implementation I wrote a couple of years ago).</li>
</ul>


<p>Also, since the graphics service distributes cameras, there is the potential to avoid storing a list of scenes. Camera entities are parented to a scene, so a list of models (and other important things in the future) relavent to a camera can be extracted from its parent. This approach will be useful when spatial partitioning lands, since a run through an octree with the frustum of a specific camera will yield a list of visible models to render.</p>

<h3>CubicVR.js &amp; Graphics Services</h3>

<p>And where would we be without CubicVR? Well, there would be a significant gap in the <a href="http://www.youtube.com/watch?v=QXGoPDbv3wk">Dreamcast homebrew 3D engine community</a>. But, more importantly, we wouldn&#8217;t have the talent and engineering behind the engine that drove <a href="http://videos.mozilla.org/serv/mozhacks/flight-of-the-navigator/">Flight of the Navigator</a>, <a href="https://developer.mozilla.org/en-US/demos/detail/no-comply/launch">No Comply</a>, and <a href="http://rescuefox.mozillalabs.com/">Rescuefox</a>.</p>

<p>CubicVR.js is Gladius&#8217; first rendering core, and we&#8217;re glad to have it. It gracefully handles a complex and feature-rich shader pipeline, and supports all sorts of neat mesh building and deformation techniques (now with dynamic vbo&#8217;s). Check out the <a href="http://cjcliffe.github.com/CubicVR.js/cubicvr/samples/vbo_dynamic/wobbly_cube.html">samples</a> directory for a bunch of hacker&#8217;s delight and eye candy.</p>

<p>Currently, we&#8217;re wrapping CubicVR&#8217;s Camera and Mesh objects with responsibly named components: Camera and Mesh. Since the graphics service currently in Gladius is specific to CubicVR.js, it knows about the native CubicVR.js objects which live on components and how to make them cooperate.</p>

<p>Our hope, of course, is to support a wide variety of rendering engines in the future &#8211; even 2D ones. Each would require an implementation of the graphics service and the core grpahics components to comply with the particulars of the renderer.</p>

<h2>Try it!</h2>

<p>The most important part of all of this is that <a href="https://github.com/alankligman/Gladius/pull/66"><strong>you can grab the branch and try it out right now</strong></a>. It won&#8217;t do much yet, but you can tell it&#8217;s working because <code>gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);</code> is doing its job by blacking out the canvas on the test page. You can change that by contributing :).</p>

<p>For now though, I&#8217;m just happy all the supporting infrastrcture is finally in working order to hack on this renderer. I can almost <strong>feel</strong> the games we&#8217;ll create with this engine.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Transition]]></title>
    <link href="http://blog.robothaus.org//2011/11/21/transition/"/>
    <updated>2011-11-21T23:22:00-05:00</updated>
    <id>http://blog.robothaus.org//2011/11/21/transition</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://blog.robothaus.org//images/codefeet.jpg" width="400" height="300" title="Codefeet" > Here I am, burning the midnight oil (literally), setting up a new server. So much work to do and so much catch-up to play. Since I&#8217;m not a sysadmin by trade, my working knowledge of what&#8217;s hip in the server world is relatively scant.</p>

<p>But, as I slowly copy over important files from my old setup, spin up new versions of old configs ( debian etch is &#8230; quite old now :( ), and learn what programs are coolest to serve static content now, it&#8217;s coming together again.</p>

<p>Good bye Apache, old friend. Good bye wordpress. Good bye PHP. I won&#8217;t miss you guys very much.</p>

<p>Hello modern web-development world. Should I be scared?</p>
]]></content>
  </entry>
  
</feed>

