From 3d121cd644f14c1057dbcebaae1fd6633737b152 Mon Sep 17 00:00:00 2001 From: Frederik Herzum Date: Nov 06 2020 19:37:05 +0000 Subject: Initial module support --- diff --git a/core/application/handle-local-options.cpp b/core/application/handle-local-options.cpp index 24def4f..2413dc4 100644 --- a/core/application/handle-local-options.cpp +++ b/core/application/handle-local-options.cpp @@ -13,22 +13,30 @@ namespace { auto load_modules(GApplication * application, GVariant * paths) { - g_print("loading modules\n"); - size_t size; - auto data = (char *)(g_bytes_get_data(g_variant_get_data_as_bytes(paths), &size)); + gsize size; + auto begin = g_variant_get_bytestring_array(paths, &size); + auto end = begin + size; + + std::vector files; - while (size_t n = strlen(data) != 0) + files.reserve(size); + for ( + auto i = begin; + i != end; + ++i + ) { - files.push_back(g_file_new_for_path(data)); - data += n + 1; + g_print("%s\n", *i); + files.push_back(g_file_new_for_path(*i)); } g_application_open ( - application, - files.data(), - files.size(), - "load-module" - ); + application, + files.data(), + files.size(), + "load-module" + ); + } } diff --git a/core/application/open.cpp b/core/application/open.cpp index b9562da..a792c28 100644 --- a/core/application/open.cpp +++ b/core/application/open.cpp @@ -8,7 +8,23 @@ namespace { auto load_module(GFile * file) -> void { - g_debug("module file path: %s\n", g_file_get_path(file)); + auto path = g_file_get_path(file); + g_debug("module file path: %s", path); + auto module = g_module_open(path, G_MODULE_BIND_LOCAL); + if (module == nullptr) + { + g_print("failed to load module: %s\n", path); + return; + } + + void (*install_module)(GApplication*, GModule*); + g_module_symbol( + module, + "install_module", + reinterpret_cast(&install_module) + ); + + install_module(nullptr, nullptr); } } diff --git a/core/meson.build b/core/meson.build index 313913d..894acb3 100644 --- a/core/meson.build +++ b/core/meson.build @@ -7,7 +7,8 @@ lightheart_source += files([ lightheart_exe_deps = [ - dependency('gtk4') + dependency('gtk4'), + dependency('gmodule-2.0') ] diff --git a/include/lightheart/module.h b/include/lightheart/module.h new file mode 100644 index 0000000..b376645 --- /dev/null +++ b/include/lightheart/module.h @@ -0,0 +1,26 @@ +#pragma once + +extern "C" +{ + + /** + * + */ + auto install_module( + struct GApplication*, + struct GModule* + ) + -> void + ; + + /** + * + */ + auto remove_module( + struct GApplication*, + struct GModule* + ) + -> void + ; + +} diff --git a/meson.build b/meson.build index 46dcc95..c295dc2 100644 --- a/meson.build +++ b/meson.build @@ -46,4 +46,4 @@ executable( ) subdir('quality-control') - +subdir('modules') diff --git a/modules/hello/hello.cpp b/modules/hello/hello.cpp new file mode 100644 index 0000000..ca2d485 --- /dev/null +++ b/modules/hello/hello.cpp @@ -0,0 +1,29 @@ +#include + +extern "C" +{ + + /** + * + */ + auto install_module( + struct GApplication*, + struct GModule* + ) + -> void + { + std::cout << "Hello, World.\n"; + } + + /** + * + */ + auto remove_module( + struct GApplication*, + struct GModule* + ) + -> void + { + std::cout << "Goodbye, World.\n"; + } +} diff --git a/modules/hello/meson.build b/modules/hello/meson.build new file mode 100644 index 0000000..340ade7 --- /dev/null +++ b/modules/hello/meson.build @@ -0,0 +1,7 @@ +shared_library( + 'hello', + files(['hello.cpp']), + dependencies: dependency('glib-2.0'), + include_directories: '../../include', + install: true +) diff --git a/modules/meson.build b/modules/meson.build new file mode 100644 index 0000000..4892702 --- /dev/null +++ b/modules/meson.build @@ -0,0 +1,2 @@ +subdir('hello') +