<?sphp $this->text('pagetitle') ?>
 
Home of the Squeezebox™ & Transporter® network music players.

HelloWorld

From SqueezeboxWiki

Revision as of 08:20, 28 June 2010 by Soulkeeper (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page needs editing. Needs to be updated for SqueezeCenter 7.0

Vincoz: Before anything it should imo be said that for modifications to be taken into account, or for a new plugin to show up SlimServer has to be restarted.

Newbie Jim: /Cannot get this example working with v6.x Slimserver :-(/ Also might be a good idea to include all of the code together at the end of the page for a quick & easy copy/paste.


Here is a bare-bones, yet functional "hello world" plugin, with commentary.

First, the standard header:

package Plugins::Hello; use strict; our %functions = ();

Next, we need to tell the SlimServer what to name to show in the Plugins menu by defining a !getDisplayName function. This is a token that refers to localized text returned from the strings() function.

sub getDisplayName { return 'PLUGIN_HELLO'; }

We need to return any strings we want to use so they can be localized. Strings must be returned in a well-formed manner, consisting of:

TOKEN1$LANG1$string1$LANG2$string2TOKEN2etc..
sub strings { return ' PLUGIN_HELLO DE Hallo EN Hello ES Hola FR Bonjour IT Ciao ' }

Next, we'll make a function that shows our "Hello World" message. It's customary to call this function "lines", but not necessary – we'll register this function with the server in just a moment. It should return a list of strings; they will get shown on the first and second lines of the display. If the display is using a large font, only the second line is shown.

sub lines { my $client = shift; return ("", "Hello World"); }

The setMode function is where a plugin can do initialization. Here, we just register our lines function and force the plugin to draw its output.

sub setMode { my $client = shift; $client->lines(\&lines); $client->update(); }

Next, we need to tell SlimServer to display this plugin on the Plugins menu.

sub addMenu { return "PLUGINS"; }

With that, the plugin is more or less complete. However, its nice to be able to get back to the menu without cycling the power, so a little code is needed to handle the remote buttons. The left button calls popModeRight, which goes back up one level in the menu. Just for fun, the up and down buttons invoke the nice feedback animations.

sub initPlugin { 
    %functions = ( 'up'   => sub { my $client = shift; $client->bumpUp(); }, 
                   'down' => sub { my $client = shift; $client->bumpDown(); }, 
                   'left' => sub { my $client = shift; Slim::Buttons::Common::popModeRight($client); } ); 
} 
sub getFunctions { \%functions; }

Finally, perl likes modules to actually do something on load, so the usual thing to do is just evaluate 1.

1; 

Thats it! To use it, save the code as Hello.pm and copy it to the Plugins directory on the server.


I had the same problem as Jim above, so I decided to dissect a working example until I had the bare bones. Here is a HelloWorld example

# give the package a name package Plugins::Hello; use strict; # a hash of strings and anonymous functions. 
# read it as follows: 
# when button left is pushed, execute the following code 
# for better readability, keep your code compact here and do most in subs below. 
my %functions = ( 'left' => sub { my $client = shift; Slim::Buttons::Common::popModeRight($client); # leave this one out and you're blocked 
                                }, 
                  'right' => sub { my $client = shift; $client->bumpRight(); },
                  'up' => sub { my $client = shift; $client->bumpUp(); }, 
                  'down' => sub { my $client = shift; $client->bumpDown(); }, ); 
# my display routine 
sub lines { 
    my $client = shift; 
    return ("Hello", "World");
 } 
# below the functions that the Slimserver calls in my module 
sub setMode { 
    my $client = shift; 
# connect the client's (i.e. the squeezebox) lines to our function 
    $client->lines(\&lines); 
# and update them 
    $client->update(); 
} 
sub getFunctions { \%functions; } 
sub getDisplayName { return 'PLUGIN_DISPLAY_FILE_NAME'; } 
sub strings { 
    return ' PLUGIN_DISPLAY_FILE_NAME EN Hello World NL Hallo Wereld '; 
} 
1;

Watch out for the subroutine strings; you'll need to follow the conventions as described in PluginStrings (and described above).


I couldn't get either of those to work but managed to slap together this version (a mutant of the others) which works for me.

# give the package a name 
package Plugins::Hello; 
use strict; 
# a hash of strings and anonymous functions. 
# read it as follows: # when button left is pushed, execute the following code 
# for better readability, keep your code compact here and do most in subs below. 
my %functions = ( 'left'  => sub { my $client = shift; Slim::Buttons::Common::popModeRight($client); }, # leave this one out and you're blocked
                 
                  'right' => sub { my $client = shift; $client->bumpRight(); }, 
                  'up'    => sub { my $client = shift; $client->bumpUp(); }, 
                  'down' => sub { my $client = shift; $client->bumpDown(); }, 
                ); 
# my display routine 
sub lines { 
  my $client = shift; 
  return ("Hello", "World");
} 
# below the functions that the Slimserver calls in my module 
sub setMode { 
   my $client = shift; # connect the client's (i.e. the squeezebox) lines to our function
   $client->lines(\&lines); 
# and update them 
   $client->update();
} 
sub addMenu { 
 return "PLUGINS"; 
} 
sub getFunctions { \%functions; } 
sub getDisplayName { 
  return 'PLUGIN_DISPLAY_FILE_NAME'; 
} 
sub strings { 
  return <<EOF; PLUGIN_DISPLAY_FILE_NAME EN Hello World EOF 
}; 
1;