PluginDevelopmentGettingStarted » History » Version 13

Leonid Protasov, 06/29/2015 08:45 AM

1 5 Andreas Smas
h1. Getting Started
2 1 Andreas Smas
3 12 Leonid Protasov
To develop a plugin for Movian it's easiest to load the plugin from shell (where command line arguments mean: -d debug, -p points to folder with plugin.json):
4 1 Andreas Smas
5 1 Andreas Smas
Example:
6 1 Andreas Smas
<pre>
7 12 Leonid Protasov
./build.linux/showtime -d -p testplugin
8 1 Andreas Smas
</pre>
9 1 Andreas Smas
10 1 Andreas Smas
This will output something like:
11 1 Andreas Smas
12 1 Andreas Smas
<pre>
13 10 Leonid Protasov
plugins         [ERROR]: Unable to load development plugin: testplugin
14 10 Leonid Protasov
                         Unable to load testplugin/plugin.json -- File not found
15 1 Andreas Smas
</pre>
16 1 Andreas Smas
17 1 Andreas Smas
Go ahead and create the directory:
18 1 Andreas Smas
19 1 Andreas Smas
<pre>
20 2 Andreas Smas
mkdir testplugin
21 1 Andreas Smas
</pre>
22 1 Andreas Smas
23 1 Andreas Smas
Edit the JSON file with your favorite text editor:
24 1 Andreas Smas
25 1 Andreas Smas
<pre>
26 2 Andreas Smas
emacs testplugin/plugin.json
27 1 Andreas Smas
</pre>
28 1 Andreas Smas
29 1 Andreas Smas
And put the required fields in there:
30 1 Andreas Smas
31 1 Andreas Smas
<pre><code class="javascript">
32 2 Andreas Smas
{
33 8 Andreas Smas
  "type": "ecmascript",
34 2 Andreas Smas
  "file": "testplugin.js",
35 2 Andreas Smas
  "id": "testplugin"
36 2 Andreas Smas
}
37 1 Andreas Smas
</code></pre>
38 1 Andreas Smas
39 11 Leonid Protasov
You can reload the development plugin at any time by pressing Shift+F5 in Movian (assuming you have not mapped that key/action to something else in the keymapper setup). This works fine even if the plugin failed to load for whatever reason. All hooks and resources that are registered by the plugin is automatically removed when the plugin is reloaded.  The same procedure happens when a plugin is uninstalled from the plugin repository browser.  Pressing Shift+F5 will also reload the data model for the current loaded page. If you have a page opened that links to your plugin in development mode Movian will first reload the plugin and then reload the page itself. This makes it very easy to do rapid development of plugins.
40 1 Andreas Smas
41 1 Andreas Smas
Now go ahead and edit the javascript file
42 1 Andreas Smas
<pre>
43 2 Andreas Smas
emacs testplugin/testplugin.js
44 1 Andreas Smas
</pre>
45 1 Andreas Smas
Put one single line in that file:
46 1 Andreas Smas
47 1 Andreas Smas
<pre><code class="javascript">
48 2 Andreas Smas
showtime.print('hello world');
49 1 Andreas Smas
</code></pre>
50 1 Andreas Smas
51 11 Leonid Protasov
Press Shift+F5 and the console window from where you started Movian
52 1 Andreas Smas
should say:
53 1 Andreas Smas
54 1 Andreas Smas
<pre>
55 2 Andreas Smas
hello world
56 2 Andreas Smas
plugins [INFO]: Reloaded dev plugin testplugin/
57 1 Andreas Smas
</pre>
58 1 Andreas Smas
59 11 Leonid Protasov
Movian will just execute the plugin which only outputs 'Hello world' on the console and then exits. So, how to create something more interesting?  First the javascript code needs to create a scope where its local variables will live and also need to remember 'this' which is, when the script is invoked, a plugin object created by Movian. Change the code to:
60 1 Andreas Smas
61 1 Andreas Smas
<pre><code class="javascript">
62 1 Andreas Smas
(function(plugin) {
63 11 Leonid Protasov
  showtime.print("Hello! I'm a plugin running inside Movian " + showtime.currentVersionString);
64 1 Andreas Smas
})(this);
65 1 Andreas Smas
</code></pre>
66 1 Andreas Smas
67 1 Andreas Smas
h2. Routing an URI to the plugin
68 2 Andreas Smas
69 13 Leonid Protasov
Almost everything in Movian has an URI. This is used for crossplugin communications and interaction with user via unified search edit box at the top of the home screen. Each plugin registers URI starting with plugin's ID. URI routes registered by plugins override URI routes that Movian itself handle so it is possible to add routes for certain well known domains on the Internet (like http://www.youtube.com/ .. )
70 1 Andreas Smas
71 1 Andreas Smas
Back to our test code:
72 1 Andreas Smas
73 2 Andreas Smas
<pre><code class="javascript">
74 2 Andreas Smas
(function(plugin) {
75 2 Andreas Smas
  plugin.addURI("testplugin:hello", function(page) {
76 2 Andreas Smas
    showtime.print("I was called");
77 2 Andreas Smas
  })
78 2 Andreas Smas
})(this);
79 2 Andreas Smas
</code></pre>
80 1 Andreas Smas
81 11 Leonid Protasov
Now, reload the plugin (Shift+F5) and type "testplugin:hello" in the search input field on Movian's home page. (Maybe you didn't know, but the search field can be used to open any valid Movian URI). Movian will open a new page with nothing but a spinning throbber and just print the console message. Not very exciting but it's a good start.