|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "flutter/shell/platform/linux/fl_platform_plugin.h" |
| 6 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h" |
| 7 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h" |
| 8 | + |
| 9 | +static constexpr char kChannelName[] = "flutter/platform"; |
| 10 | +static constexpr char kBadArgumentError[] = "Bad Arguments"; |
| 11 | +static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData"; |
| 12 | +static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData"; |
| 13 | +static constexpr char kPlaySoundMethod[] = "SystemSound.play"; |
| 14 | +static constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop"; |
| 15 | + |
| 16 | +struct _FlPlatformPlugin { |
| 17 | + GObject parent_instance; |
| 18 | + |
| 19 | + FlMethodChannel* channel; |
| 20 | +}; |
| 21 | + |
| 22 | +G_DEFINE_TYPE(FlPlatformPlugin, fl_platform_plugin, G_TYPE_OBJECT) |
| 23 | + |
| 24 | +// Called when Flutter wants to copy to the clipboard. |
| 25 | +static FlMethodResponse* clipboard_set_data(FlPlatformPlugin* self, |
| 26 | + FlValue* args) { |
| 27 | + // FIXME |
| 28 | + return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
| 29 | +} |
| 30 | + |
| 31 | +// Called when Flutter wants to paste from the clipboard. |
| 32 | +static FlMethodResponse* clipboard_get_data(FlPlatformPlugin* self) { |
| 33 | + // FIXME |
| 34 | + return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
| 35 | +} |
| 36 | + |
| 37 | +// Called when Flutter wants to play a sound. |
| 38 | +static FlMethodResponse* system_sound_play(FlPlatformPlugin* self, |
| 39 | + FlValue* args) { |
| 40 | + if (fl_value_get_type(args) != FL_VALUE_TYPE_STRING) { |
| 41 | + return FL_METHOD_RESPONSE(fl_method_error_response_new( |
| 42 | + kBadArgumentError, "Expected string", nullptr)); |
| 43 | + } |
| 44 | + |
| 45 | + // const gchar* type = fl_value_get_string(args); |
| 46 | + // FIXME(robert-ancell): Play sound |
| 47 | + |
| 48 | + return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
| 49 | +} |
| 50 | + |
| 51 | +// Called when Flutter wants to quit the application. |
| 52 | +static FlMethodResponse* system_navigator_pop(FlPlatformPlugin* self) { |
| 53 | + // FIXME(robert-ancell): Quit the application |
| 54 | + |
| 55 | + return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
| 56 | +} |
| 57 | + |
| 58 | +// Called when a method call is received from Flutter. |
| 59 | +static void method_call_cb(FlMethodChannel* channel, |
| 60 | + FlMethodCall* method_call, |
| 61 | + gpointer user_data) { |
| 62 | + FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(user_data); |
| 63 | + |
| 64 | + const gchar* method = fl_method_call_get_name(method_call); |
| 65 | + FlValue* args = fl_method_call_get_args(method_call); |
| 66 | + |
| 67 | + g_autoptr(FlMethodResponse) response = nullptr; |
| 68 | + if (strcmp(method, kSetClipboardDataMethod) == 0) |
| 69 | + response = clipboard_set_data(self, args); |
| 70 | + else if (strcmp(method, kGetClipboardDataMethod) == 0) |
| 71 | + response = clipboard_get_data(self, args); |
| 72 | + else if (strcmp(method, kPlaySoundMethod) == 0) |
| 73 | + response = system_sunnd_play(self, args); |
| 74 | + else if (strcmp(method, kSystemNavigatorPopMethod) == 0) |
| 75 | + response = system_navigator_pop(self); |
| 76 | + else |
| 77 | + response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
| 78 | + |
| 79 | + g_autoptr(GError) error = nullptr; |
| 80 | + if (!fl_method_call_respond(method_call, response, &error)) |
| 81 | + g_warning("Failed to send method call response: %s", error->message); |
| 82 | +} |
| 83 | + |
| 84 | +static void fl_platform_plugin_dispose(GObject* object) { |
| 85 | + FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(object); |
| 86 | + |
| 87 | + g_clear_object(&self->channel); |
| 88 | + |
| 89 | + G_OBJECT_CLASS(fl_platform_plugin_parent_class)->dispose(object); |
| 90 | +} |
| 91 | + |
| 92 | +static void fl_platform_plugin_class_init(FlPlatformPluginClass* klass) { |
| 93 | + G_OBJECT_CLASS(klass)->dispose = fl_platform_plugin_dispose; |
| 94 | +} |
| 95 | + |
| 96 | +static void fl_platform_plugin_init(FlPlatformPlugin* self) {} |
| 97 | + |
| 98 | +FlPlatformPlugin* fl_platform_plugin_new(FlBinaryMessenger* messenger) { |
| 99 | + g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr); |
| 100 | + |
| 101 | + FlPlatformPlugin* self = |
| 102 | + FL_PLATFORM_PLUGIN(g_object_new(fl_platform_plugin_get_type(), nullptr)); |
| 103 | + |
| 104 | + g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new(); |
| 105 | + self->channel = |
| 106 | + fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec)); |
| 107 | + fl_method_channel_set_method_call_handler(self->channel, method_call_cb, self, |
| 108 | + nullptr); |
| 109 | + |
| 110 | + return self; |
| 111 | +} |
0 commit comments