diff options
author | 3gg <3gg@shellblade.net> | 2023-06-16 09:15:34 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2023-06-16 09:15:34 -0700 |
commit | 2f8ff39a8d95b95288875d92abb74b1428713906 (patch) | |
tree | 95c23359a86b539c4b11f42ad8694ac6682d1b41 /plugin/include | |
parent | bfabb435e5c5bf313005c4636747fce59eb4ca6f (diff) |
Add plugin library.
Diffstat (limited to 'plugin/include')
-rw-r--r-- | plugin/include/plugin.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/plugin/include/plugin.h b/plugin/include/plugin.h new file mode 100644 index 0000000..abca9b5 --- /dev/null +++ b/plugin/include/plugin.h | |||
@@ -0,0 +1,66 @@ | |||
1 | /* | ||
2 | * Plugin engine for loading plugins and watching plugin updates. | ||
3 | * | ||
4 | * The plugin engine allows the client to load plugins and call their functions. | ||
5 | * | ||
6 | * Plugins can also be associated with a state. The engine does not create the | ||
7 | * plugin's state because this may require other application-specific state. | ||
8 | * | ||
9 | * Plugin files are watched for updates. Upon an update, the engine reloads the | ||
10 | * plugin into memory and notifies the client. The client should then typically | ||
11 | * re-create the plugin's state. | ||
12 | */ | ||
13 | #pragma once | ||
14 | |||
15 | #include <stdbool.h> | ||
16 | |||
17 | #include <dlfcn.h> | ||
18 | |||
19 | typedef struct Plugin Plugin; | ||
20 | typedef struct PluginEngine PluginEngine; | ||
21 | |||
22 | /// Plugin engine creation depluginor. | ||
23 | typedef struct PluginEngineDesc { | ||
24 | const char* plugins_dir; | ||
25 | } PluginEngineDesc; | ||
26 | |||
27 | /// Create a new plugin engine. | ||
28 | PluginEngine* new_plugin_engine(const PluginEngineDesc*); | ||
29 | |||
30 | /// Destroy the plugin engine. | ||
31 | void delete_plugin_engine(PluginEngine**); | ||
32 | |||
33 | /// Update the plugin engine. | ||
34 | /// | ||
35 | /// This looks for any plugins that have been modified and reloads them. | ||
36 | void plugin_engine_update(PluginEngine*); | ||
37 | |||
38 | /// Load a plugin. | ||
39 | Plugin* load_plugin(PluginEngine*, const char* filename); | ||
40 | |||
41 | /// Delete the plugin. | ||
42 | /// | ||
43 | /// This unloads the plugin from memory and removes it from the engine. | ||
44 | void delete_plugin(Plugin**); | ||
45 | |||
46 | /// Set the plugin's state. | ||
47 | /// | ||
48 | /// The plugin's previous state is deleted if non-null. | ||
49 | void set_plugin_state(Plugin*, void* state); | ||
50 | |||
51 | /// Get the plugin's state. Return null if the plugin has no state. | ||
52 | void* get_plugin_state(Plugin*); | ||
53 | |||
54 | /// Return true if the plugin has been reloaded. | ||
55 | /// | ||
56 | /// If the plugin has been reloaded, subsequent calls to this function return | ||
57 | /// false until the plugin is reloaded again. | ||
58 | bool plugin_reloaded(Plugin*); | ||
59 | |||
60 | /// Resolve a function in the plugin. | ||
61 | #define plugin_resolve(plugin, func_sig, func_name) \ | ||
62 | (func_sig)(dlsym(*((void**)(plugin)), func_name)) | ||
63 | |||
64 | /// Call a function in the plugin. | ||
65 | #define plugin_call(plugin, func_sig, func_name, ...) \ | ||
66 | (*plugin_resolve(plugin, func_sig, func_name))(__VA_ARGS__) | ||