Friday, April 29, 2011

Accessing the music library using the Banshee API

Since it is not so obvious which classes can be used to access the meta data of the files in banshee's music library, here is an example of how it can be done using ServiceManager.SourceManager.MusicLibrary.TrackModel.

private string GetMetaData(int index) {
    return ServiceManager.SourceManager.MusicLibrary.TrackModel[index].ArtistName + " - " +
    ServiceManager.SourceManager.MusicLibrary.TrackModel[index].TrackTitle + " (" +
    ServiceManager.SourceManager.MusicLibrary.TrackModel[index].AlbumTitle + ")";
}

Calling this method within a loop over all TrackModels in the MusicLibrary and outputting it to the log

for (int i = 0; i < ServiceManager.SourceManager.MusicLibrary.TrackModel.Count; i++) {
    Hyena.Log.Debug(GetMetaData(i));
} 

generates output similar to this:

Muse - Exo-Politics (Black Holes and Revelations)

Thursday, April 28, 2011

Using clutter in banshee extension

To write a banshee extension, the create-extension command in the banshee-community-extensions repository is used. This extension can then be modified in MonoDevelop. However, to use clutter-sharp, the makefile must be modified otherwise mono would not find clutter-sharp, even if it is properly referenced in MonoDevelop. The LINK variable in the makefile must contain CLUTTER_SHARP_LIBS. This is shown in my example below:
LINK = $(BANSHEE_LIBS) $(BANSHEE_NOWPLAYING_LIBS) \
 $(CLUTTER_SHARP_LIBS) -r:Mono.Cairo -pkg:gtk-sharp-2.0 \
 $(am__append_1)
Additionally, to prevent MonoDevelop to overwrite those changes I disabled the makefile integration in the project properties. However, this means that every sourcefile that is added to the project hast to be added to the SOURCES in the makefile. There is a way to link the references of the makefile in the makefile integration dialog, but this did not work out for me as desired. 

Simple clutter-sharp window


I will quickly show howto create a clutter window using clutter-sharp. Clutter-sharp can be installed using the Ubuntu packet manager.

First of all, a class is needed which provides functions to initialize clutter and call the main loop. This class is named ClutterHelper.

1. Reference clutter functions
[DllImport ("clutter")]
static extern void clutter_main();
  
[DllImport ("clutter")]
static extern void clutter_init (int argc, IntPtr argv);

2. Encapsulate those funcitons
public static void Init( ) {
   clutter_init (0, IntPtr.Zero);
}
  
public static void Main() {
   //Call clutter main loop
   clutter_main(); 
}


The name in DllImport references the dll assembly. In windows this would be "clutter.dll", however in linux the library is named otherwise. To tell mono which name should be used in linux, a config file is used. This config file must have the same name as the assembly with an appendix ".config" (for instance "MyAssembly.exe.config") and must be in the same directory. In this config file the name of the windows library and the linux library are then mapped.





After creating this helper class, a main class Main is used to create a clutter window. In the Main function of this class clutter is initialized, a new stage is created and the main-loop is called.

static void Main ()
{
   ClutterHelper.Init();

   Stage stage = Stage.Default;
   stage.Color = new Clutter.Color(0,0,0,255);
   stage.SetSize(512,512);

   stage.Show();
   
   ClutterHelper.Main();
}

Code highlighting test

protected void UpdatePositions ()
  {
   /* TODO: variable heights need to be handled */
   float x = marginX; float y = marginY;
   text.Hide ();
   foreach (Actor actor in this) {
    if (actor!=text) {
     actor.Hide ();
     actor.SetPosition (x, y);
     x += actor.Width + spacing;
     actor.Show ();
    }
   }

   UpdateTexture ();
  }

Tutorial: Adding Syntax Highlighting to Blogger