flowy_board_plugin.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "include/flowy_board/flowy_board_plugin.h"
  2. #include <flutter_linux/flutter_linux.h>
  3. #include <gtk/gtk.h>
  4. #include <sys/utsname.h>
  5. #include <cstring>
  6. #define FLOWY_BOARD_PLUGIN(obj) \
  7. (G_TYPE_CHECK_INSTANCE_CAST((obj), flowy_board_plugin_get_type(), \
  8. FlowyBoardPlugin))
  9. struct _FlowyBoardPlugin {
  10. GObject parent_instance;
  11. };
  12. G_DEFINE_TYPE(FlowyBoardPlugin, flowy_board_plugin, g_object_get_type())
  13. // Called when a method call is received from Flutter.
  14. static void flowy_board_plugin_handle_method_call(
  15. FlowyBoardPlugin* self,
  16. FlMethodCall* method_call) {
  17. g_autoptr(FlMethodResponse) response = nullptr;
  18. const gchar* method = fl_method_call_get_name(method_call);
  19. if (strcmp(method, "getPlatformVersion") == 0) {
  20. struct utsname uname_data = {};
  21. uname(&uname_data);
  22. g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version);
  23. g_autoptr(FlValue) result = fl_value_new_string(version);
  24. response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
  25. } else {
  26. response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
  27. }
  28. fl_method_call_respond(method_call, response, nullptr);
  29. }
  30. static void flowy_board_plugin_dispose(GObject* object) {
  31. G_OBJECT_CLASS(flowy_board_plugin_parent_class)->dispose(object);
  32. }
  33. static void flowy_board_plugin_class_init(FlowyBoardPluginClass* klass) {
  34. G_OBJECT_CLASS(klass)->dispose = flowy_board_plugin_dispose;
  35. }
  36. static void flowy_board_plugin_init(FlowyBoardPlugin* self) {}
  37. static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
  38. gpointer user_data) {
  39. FlowyBoardPlugin* plugin = FLOWY_BOARD_PLUGIN(user_data);
  40. flowy_board_plugin_handle_method_call(plugin, method_call);
  41. }
  42. void flowy_board_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
  43. FlowyBoardPlugin* plugin = FLOWY_BOARD_PLUGIN(
  44. g_object_new(flowy_board_plugin_get_type(), nullptr));
  45. g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
  46. g_autoptr(FlMethodChannel) channel =
  47. fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
  48. "flowy_board",
  49. FL_METHOD_CODEC(codec));
  50. fl_method_channel_set_method_call_handler(channel, method_call_cb,
  51. g_object_ref(plugin),
  52. g_object_unref);
  53. g_object_unref(plugin);
  54. }