diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt index c330cd7..a5f3d24 100644 --- a/linux/CMakeLists.txt +++ b/linux/CMakeLists.txt @@ -1,21 +1,11 @@ cmake_minimum_required(VERSION 3.10) set(PROJECT_NAME "argon2_ffi") -project(${PROJECT_NAME} LANGUAGES C CXX) +project(${PROJECT_NAME} LANGUAGES CXX) set(PLUGIN_NAME "${PROJECT_NAME}_plugin") add_library(${PLUGIN_NAME} SHARED - ${PLUGIN_NAME}.cc - - ../ios/Classes/argon2_ffi.c - - ../ios/Classes/argon2src/argon2.c - ../ios/Classes/argon2src/core.c - ../ios/Classes/argon2src/encoding.c - ../ios/Classes/argon2src/genkat.c - ../ios/Classes/argon2src/ref.c - ../ios/Classes/argon2src/thread.c - ../ios/Classes/argon2src/blake2/blake2b.c + "${PLUGIN_NAME}.cc" ) apply_standard_settings(${PLUGIN_NAME}) set_target_properties(${PLUGIN_NAME} PROPERTIES @@ -23,4 +13,11 @@ set_target_properties(${PLUGIN_NAME} PROPERTIES target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL) target_include_directories(${PLUGIN_NAME} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin) +target_link_libraries(${PLUGIN_NAME} PRIVATE flutter) +target_link_libraries(${PLUGIN_NAME} PRIVATE PkgConfig::GTK) + +# List of absolute paths to libraries that should be bundled with the plugin +set(argon2_ffi_bundled_libraries + "" + PARENT_SCOPE +) diff --git a/linux/argon2_ffi_plugin.cc b/linux/argon2_ffi_plugin.cc index c4579d2..c3796ed 100644 --- a/linux/argon2_ffi_plugin.cc +++ b/linux/argon2_ffi_plugin.cc @@ -1,78 +1,68 @@ -#include "include/argon2_ffi_plugin.h" +#include "include/argon2_ffi/argon2_ffi_plugin.h" -#include -#include -#include +#include +#include #include -#include -#include -#include +#define ARGON2_FFI_PLUGIN(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), argon2_ffi_plugin_get_type(), \ + Argon2FfiPlugin)) -namespace { - -class Argon2FfiPlugin : public flutter::Plugin { - public: - static void RegisterWithRegistrar(flutter::PluginRegistrarGlfw *registrar); +struct _Argon2FfiPlugin { + GObject parent_instance; +}; - Argon2FfiPlugin(); +G_DEFINE_TYPE(Argon2FfiPlugin, argon2_ffi_plugin, g_object_get_type()) - virtual ~Argon2FfiPlugin(); +// Called when a method call is received from Flutter. +static void argon2_ffi_plugin_handle_method_call( + Argon2FfiPlugin* self, + FlMethodCall* method_call) { + g_autoptr(FlMethodResponse) response = nullptr; - private: - // Called when a method is called on this plugin's channel from Dart. - void HandleMethodCall( - const flutter::MethodCall &method_call, - std::unique_ptr> result); -}; + const gchar* method = fl_method_call_get_name(method_call); -// static -void Argon2FfiPlugin::RegisterWithRegistrar( - flutter::PluginRegistrarGlfw *registrar) { - auto channel = - std::make_unique>( - registrar->messenger(), "argon2_ffi", - &flutter::StandardMethodCodec::GetInstance()); - auto plugin = std::make_unique(); + if (strcmp(method, "getPlatformVersion") == 0) { + struct utsname uname_data = {}; + uname(&uname_data); + g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version); + g_autoptr(FlValue) result = fl_value_new_string(version); + response = FL_METHOD_RESPONSE(fl_method_success_response_new(result)); + } else { + response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); + } - channel->SetMethodCallHandler( - [plugin_pointer = plugin.get()](const auto &call, auto result) { - plugin_pointer->HandleMethodCall(call, std::move(result)); - }); + fl_method_call_respond(method_call, response, nullptr); +} - registrar->AddPlugin(std::move(plugin)); +static void argon2_ffi_plugin_dispose(GObject* object) { + G_OBJECT_CLASS(argon2_ffi_plugin_parent_class)->dispose(object); } -Argon2FfiPlugin::Argon2FfiPlugin() {} +static void argon2_ffi_plugin_class_init(Argon2FfiPluginClass* klass) { + G_OBJECT_CLASS(klass)->dispose = argon2_ffi_plugin_dispose; +} -Argon2FfiPlugin::~Argon2FfiPlugin() {} +static void argon2_ffi_plugin_init(Argon2FfiPlugin* self) {} -void Argon2FfiPlugin::HandleMethodCall( - const flutter::MethodCall &method_call, - std::unique_ptr> result) { - // Replace "getPlatformVersion" check with your plugin's method. - // See: - // https://github.com/flutter/engine/tree/master/shell/platform/common/cpp/client_wrapper/include/flutter - // and - // https://github.com/flutter/engine/tree/master/shell/platform/glfw/client_wrapper/include/flutter - // for the relevant Flutter APIs. - if (method_call.method_name().compare("getPlatformVersion") == 0) { - struct utsname uname_data = {}; - uname(&uname_data); - std::ostringstream version_stream; - version_stream << "Linux " << uname_data.version; - flutter::EncodableValue response(version_stream.str()); - result->Success(&response); - } else { - result->NotImplemented(); - } +static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call, + gpointer user_data) { + Argon2FfiPlugin* plugin = ARGON2_FFI_PLUGIN(user_data); + argon2_ffi_plugin_handle_method_call(plugin, method_call); } -} // namespace +void argon2_ffi_plugin_register_with_registrar(FlPluginRegistrar* registrar) { + Argon2FfiPlugin* plugin = ARGON2_FFI_PLUGIN( + g_object_new(argon2_ffi_plugin_get_type(), nullptr)); + + g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new(); + g_autoptr(FlMethodChannel) channel = + fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar), + "argon2_ffi", + FL_METHOD_CODEC(codec)); + fl_method_channel_set_method_call_handler(channel, method_call_cb, + g_object_ref(plugin), + g_object_unref); -void Argon2FfiPluginRegisterWithRegistrar( - FlutterDesktopPluginRegistrarRef registrar) { - Argon2FfiPlugin::RegisterWithRegistrar( - flutter::PluginRegistrarManager::GetInstance() - ->GetRegistrar(registrar)); + g_object_unref(plugin); } diff --git a/linux/include/argon2_ffi/argon2_ffi_plugin.h b/linux/include/argon2_ffi/argon2_ffi_plugin.h new file mode 100644 index 0000000..0d8fc8a --- /dev/null +++ b/linux/include/argon2_ffi/argon2_ffi_plugin.h @@ -0,0 +1,26 @@ +#ifndef FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ +#define FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ + +#include + +G_BEGIN_DECLS + +#ifdef FLUTTER_PLUGIN_IMPL +#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default"))) +#else +#define FLUTTER_PLUGIN_EXPORT +#endif + +typedef struct _Argon2FfiPlugin Argon2FfiPlugin; +typedef struct { + GObjectClass parent_class; +} Argon2FfiPluginClass; + +FLUTTER_PLUGIN_EXPORT GType argon2_ffi_plugin_get_type(); + +FLUTTER_PLUGIN_EXPORT void argon2_ffi_plugin_register_with_registrar( + FlPluginRegistrar* registrar); + +G_END_DECLS + +#endif // FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ diff --git a/linux/include/argon2_ffi_plugin.h b/linux/include/argon2_ffi_plugin.h deleted file mode 100644 index cbf7651..0000000 --- a/linux/include/argon2_ffi_plugin.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ -#define FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ - -#include - -#ifdef FLUTTER_PLUGIN_IMPL -#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default"))) -#else -#define FLUTTER_PLUGIN_EXPORT -#endif - -#if defined(__cplusplus) -extern "C" { -#endif - -FLUTTER_PLUGIN_EXPORT void Argon2FfiPluginRegisterWithRegistrar( - FlutterDesktopPluginRegistrarRef registrar); - -#if defined(__cplusplus) -} // extern "C" -#endif - -#endif // FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_