Skip to content

Guidance on writing new plugins #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mortenboye opened this issue Mar 19, 2020 · 2 comments
Closed

Guidance on writing new plugins #47

mortenboye opened this issue Mar 19, 2020 · 2 comments

Comments

@mortenboye
Copy link

What is the recommended approach to writing new plugins for flutter-pi?
Looking at the gpiod plugin, it looks like a dart package (no native code) combined with a gpiod.c file in the plugins folder.
What are the steps necessary to link and build the .c file and make flutter-pi aware of new plugins?

@ardera
Copy link
Owner

ardera commented Mar 19, 2020

Looking at the gpiod plugin, it looks like a dart package (no native code) combined with a gpiod.c file in the plugins folder.

Exactly, a flutter-pi plugin is just:

  • a dart package for the dart-side implementation
  • typically, a my_plugin.c file inside src/plugins and a my_plugin.h file inside include/plugins (if you need more files, just create a my_plugin folder inside src/plugins and include/plugins which contains your plugin source files)
  • some code inside src/plugin_registry.c to make your plugin known to the plugin registry
  • of course, you also need to update the Makefile so it compiles your src/plugins/my_plugin.c file

It's also "convention" to implement a flag, like BUILD_MY_PLUGIN, so people can choose when compiling flutter-pi whether your plugin should be built and included in the finished binary. (To be more precise, the BUILD_MY_PLUGIN flag should do nothing more than enable/disable the registration of your plugin to the plugin_registry, see here and here)

What are the steps necessary to link and build the .c file and make flutter-pi aware of new plugins?

So, in practice, you'll have to add 6 lines to pluginregistry.c to make your plugin known to flutter-pi:

// my_plugin.h should contain the definitions of `my_plugin_init`
// and `my_plugin_deinit`
#ifdef BUILD_MY_PLUGIN
#    include <plugins/my_plugin.h>
#endif

// . . .

struct flutterpi_plugin hardcoded_plugins[] = {

// . . . the other plugins' registrations here

#ifdef BUILD_MY_PLUGIN
    {.name = "my_plugin", .init = my_plugin_init, .deinit = my_plugin_deinit},
#endif
};

Then you need to update the Makefile so your plugin gets built:

  1. Add src/plugins/my_plugin.c to the definition of the SOURCES variable
  2. Add the -DBUILD_MY_PLUGIN c-flag to the definition of the REAL_CFLAGS variable

@mortenboye
Copy link
Author

Perfect, works! Thanks, closing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants