Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Plugin development tutorial (Read 27605 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Plugin development tutorial

As mentioned in another thread, I wanted to do a tutorial on creating a plugin for foobar2000 v0.8. So here it is.

The tutorial will be about creating a general purpose plugin, input/output/DSP/etc. will not be covered. The tutorial will be organized into several steps.


Step 0: Creating a new component project
I don't really want to say much here, but the simplest way is to make a copy of an existing component and edit the project file to change the component name. You'll also have to set up the project dependencies correctly: each component project needs to have dependencies on foobar2000_sdk and foobar2000_component_client added. You may need to add a dependency on foobar2000_sdk_helpers, if you are using that library. Direct dependencies on the pfc or utf8api libraries are not needed.

For those of you, who do not add their project to the foobar2000.dsw workspace from the SDK, here is a little list of projects and dependencies that you'll need to get things working (paths are relative to the folder where the SDK was unpacked):
  • pfc (pfc/pfc.dsp): no dependencies
  • utf8api (foobar2000/utf8api/utf8api.dsp): dependency on pfc
  • foobar2000_component_client (foobar2000/foobar2000_component_client/foobar2000_component_client.dsp): no dependencies
  • foobar2000_sdk (foobar2000/SDK/foobar2000_SDK.dsp): dependencies on pfc and utf8api
  • foobar2000_sdk_helpers (foobar2000/helpers/foobar2000_sdk_helpers.dsp): no dependencies
Remember that all projects must link the C runtime dynamically ("Multithreaded DLL" in Project/Settings/C++/Code Generation).


Step 1: Basics
The plugin shows a window (w00t). It remembers the position and whether the window was open. A menu command is provided to open and close the window.

Used SDK features:
menu_item_main, initquit, cfg_var subclasses (private configuration variables), functions from utf8api.dll to create a (custom) window


Step 2: Now Playing
The plugin now displays the foobar2000 icon and the currently playing song using the system tray formatting string. The font can be configured by right clicking on the window.

Used SDK features (in addition to those of the previous step):
play_control (for getting the current track), play_callback, config_var_string (for accessing the system tray format), ui_control (to get the icon), custom cfg_var_notify subclass (for tracking changes in the font setting), functions from utf8api.dll for drawing text


Step 3: Context menu and hotkeys
The plugin now passes key presses to the core for evaluation. User can use normal context menu hotkeys to run commands on the currently playing track. Right-clicking the window now shows a context menu for the currently playing item with a command for choosing the window font at the top. Context menu can also be opened by pressing the context menu key on the keyboard.

Used SDK features (in addition to those of the previous step):
keyboard_shortcut_manager, menu_manager


Step 4: Drag&Drop
The plugin now allows the user to drag the currently playing track.

Used SDK features (in addition to those of the previous step):
IDropSource (commented and slightly modified version of foo_albumlist's implementation), playlist_oper::get_dataobject


Download
As of version 0.2.0, the entire tutorial is contained in a single ZIP archive. Both the source code and precompiled binaries are included.
Link: foo_tutorial1-0.2.zip


Stuff that was left out
  • New callbacks for things dropped on the playlist.
    I simply don't know what that could be used for in this plugin.
Feel free to post, if you have suggestions or questions or if you found an error somewhere in this tutorial.

Plugin development tutorial

Reply #1
First thank you for that tutorial.
I've downloaded it, and downloaded sdk0.8.
Open the C++ Projectfile. Then C++ asked me alot of things. Were the foobar2000_SDK.dsp is and so on. I pointed him to every file.
After that I tried to create the foo_tutorial1.dll. It quits with the error that the include file is missing. "'../SDK/foobar2000.h': No such file or directory"
What does it mean?

Plugin development tutorial

Reply #2
Quote
First thank you for that tutorial.
I've downloaded it, and downloaded sdk0.8.
Open the C++ Projectfile. Then C++ asked me alot of things. Were the foobar2000_SDK.dsp is and so on. I pointed him to every file.
After that I tried to create the foo_tutorial1.dll. It quits with the error that the include file is missing. "'../SDK/foobar2000.h': No such file or directory"
What does it mean?

Your directory tree after unpacking the SDK and the tutorial should look like this:
Code: [Select]
SDK0.8b1
   foobar2000
       ...
       foo_tutorial1
       ...
       foobar2000_component_client
       helpers
       SDK
       utf8api
   pfc

The paths in the project and workspace files are relative, so if you have the above directory layout, they should work without problems.

Plugin development tutorial

Reply #3
Thank you foosion, that saved the Path error.
But now I'm getting 13 errors. All with "error LNK2001".

I've vc++ 6.0

Plugin development tutorial

Reply #4
Quote
Thank you foosion, that saved the Path error.
But now I'm getting 13 errors. All with "error LNK2001".

I've the vc++ 6.0

Did you open the foo_tutorial1.dsw workspace? It should have all the dependencies set up correctly. You can manually check them under "Project/Dependencies..." in the menu.
They should be as follows:
- foo_tutorial1: foobar2000_component_client, foobar2000_SDK, foobar2000_sdk_helpers
- foobar2000_SDK: utf8api
- utf8api: pfc

Plugin development tutorial

Reply #5
 hmm the dependencies for pfc and utf8api weren't enabled


Works fine now! Thank you.

Plugin development tutorial

Reply #6
The tutorial is finished (for now). The plugin isn't all that useful. You can't even use it as a "now playing" display, since it doesn't have an "Always on top" toggle.

However, that wasn't my goal. The goal was to help people getting started with the foobar2000 SDK and the (new) features in 0.8. If someone draws inspiration from this plugin, well, even better.

Plugin development tutorial

Reply #7
Thank you so much for this.  This is EXACTLY what I was looking for.

For some reason though, I get

Code: [Select]
sdk0.8b5\foobar2000\utf8api\utf8.cpp(27) error C2065: 'setlocale' : undeclared identifier
sdk0.8b5\foobar2000\utf8api\utf8.cpp(27) error C2065: 'LC_CTYPE' : undeclared identifier


when I try to compile.  MSVC 6.0.

Plugin development tutorial

Reply #8
@manoliisfat:
Have you made any changes, or do these errors also occur when compiling my unmodified code? I'm a little confused, as it works just fine here...


General note:
The tutorial was written for 0.8 beta 1 SDK, I'll update the  code for 0.8 beta 3 SDK ASAP.

Plugin development tutorial

Reply #9
Quote
@manoliisfat:
Have you made any changes, or do these errors also occur when compiling my unmodified code? I'm a little confused, as it works just fine here...


General note:
The tutorial was written for 0.8 beta 1 SDK, I'll update the  code for 0.8 beta 3 SDK ASAP.

Ya, completely unmodified.  I did have the new SDK though.

Plugin development tutorial

Reply #10
Quote
Ya, completely unmodified.  I did have the new SDK though.

This still seems a little strange to me. Your compile errors are in the utf8api.dll source code, which is part of the normal SDK. Hm, try inserting the following line in utf8.cpp:
Code: [Select]
#include <locale.h>


Compilation of the tutorial with beta 3 SDK should fail at a different point...

Plugin development tutorial

Reply #11
Quote
Quote
Ya, completely unmodified.  I did have the new SDK though.

This still seems a little strange to me. Your compile errors are in the utf8api.dll source code, which is part of the normal SDK. Hm, try inserting the following line in utf8.cpp:
Code: [Select]
#include <locale.h>


Compilation of the tutorial with beta 3 SDK should fail at a different point...

I had the same problem as manoliisfat. Adding locale.h sorted that out and the linker got as far as the tutorial itself before crashing out with this:

Quote
c:\foo\foo_tutorial1.cpp(263) : error C2660: 'on_keydown_auto_context' : function does not take 2 parameters


Any ideas? I'm using the latest SDK.

EDIT: The definition of on_keydown_auto_context has 3 parameters. I take it this has changed in recent SDKs?

Plugin development tutorial

Reply #12
Quote
Quote
c:\foo\foo_tutorial1.cpp(263) : error C2660: 'on_keydown_auto_context' : function does not take 2 parameters


Any ideas? I'm using the latest SDK.

EDIT: The definition of on_keydown_auto_context has 3 parameters. I take it this has changed in recent SDKs?

That's exactly why I'll have to update the code. In the meantime, you can solve it by passing menu_item::caller_undefined as the third parameter.


Plugin development tutorial

Reply #14
Nice job foosion, thank you very much!

This might be usefull to add the Foobar wiki as well.
Are you planning on adding it or are you ok with it if it get's added (once it's completely finished after the foobar 0.8 final release)?

Plugin development tutorial

Reply #15
I updated the tutorial for the final 0.8 SDK at last. Project layout has changed a bit as well. I anyone feels like adding this to the Wiki, just do it.


Note: I've just started working on another tutorial about implementing and supporting ui_drop_item_callback, which will eventually get its own thread. For now, you can check out an early version of it: foo_tutorial2-0.1.zip

Plugin development tutorial

Reply #16
I want to make my foobar plugin in Delphi or C++ Builder. Where can I download heads/units files?

Plugin development tutorial

Reply #17
Quote
I want to make my foobar plugin in Delphi or C++ Builder. Where can I download heads/units files?

The SDK contains all necessary C++ headers, though only project files for Visual Studio. There is no official support for Delphi (and no inofficial support that I know of).

Plugin development tutorial

Reply #18
Edit first post to include information on required dependencies and C runtime.

Plugin development tutorial

Reply #19
Found my problem, sorry for the post (I don't think I can delete it).

Plugin development tutorial

Reply #20
Firstly, the plugin tutorial is much appreciated. I had no idea where to start until I saw this.

My question is how do you find and figure out how to use functions in the SDK? Is there any kind of documentation at all? I know the header file gives you the function prototypes, but the names don't always tell the whole story, and it seems difficult to figure out what each parameter is. Then I guess I could search source code of other plugins and try to learn by example...

Is there an easier way, or is that it?

Plugin development tutorial

Reply #21
Is anyone else having trouble accessing the foo_tutorial1.0-2.zip file in the original post?

Plugin development tutorial

Reply #22
The webserver hosting the file is currently down for unknown reasons.

Plugin development tutorial

Reply #23
why is it that when I compile my own dll's from the tutorial the size of the dll is 136k yet the dll that comes with the tutorial is only 9k?

Plugin development tutorial

Reply #24
The size of the resulting DLL depends on a lot of things, like the compiler you are using, whether you are compiling a release or a debug build, whether the C runtime is linked statically or dynamically, etc. The DLLs that come with the tutorial are compiled with Visual C++ 6.0 in release mode and link dynamically to the C runtime.