| Flutter Linux Embedder |

fl_mouse_cursor_channel.cc
Go to the documentation of this file.
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_mouse_cursor_channel.h"
6
7 #include <cstring>
8
9 #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
10 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
11
12 static constexpr char kChannelName[] = "flutter/mousecursor";
13 static constexpr char kBadArgumentsError[] = "Bad Arguments";
14 static constexpr char kActivateSystemCursorMethod[] = "activateSystemCursor";
15 static constexpr char kKindKey[] = "kind";
16
17 struct _FlMouseCursorChannel {
18 GObject parent_instance;
19
20 FlMethodChannel* channel;
21
22 FlMouseCursorChannelVTable* vtable;
23
24 gpointer user_data;
25 };
26
27 G_DEFINE_TYPE(FlMouseCursorChannel, fl_mouse_cursor_channel, G_TYPE_OBJECT)
28
29 // Sets the mouse cursor.
30 static FlMethodResponse* activate_system_cursor(FlMouseCursorChannel* self,
31 FlValue* args) {
32 if (fl_value_get_type(args) != FL_VALUE_TYPE_MAP) {
33 return FL_METHOD_RESPONSE(fl_method_error_response_new(
34 kBadArgumentsError, "Argument map missing or malformed", nullptr));
35 }
36
37 FlValue* kind_value = fl_value_lookup_string(args, kKindKey);
38 const gchar* kind = nullptr;
39 if (fl_value_get_type(kind_value) == FL_VALUE_TYPE_STRING) {
40 kind = fl_value_get_string(kind_value);
41 }
42
43 self->vtable->activate_system_cursor(kind, self->user_data);
44
45 return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
46 }
47
48 // Called when a method call is received from Flutter.
49 static void method_call_cb(FlMethodChannel* channel,
50 FlMethodCall* method_call,
51 gpointer user_data) {
52 FlMouseCursorChannel* self = FL_MOUSE_CURSOR_CHANNEL(user_data);
53
54 const gchar* method = fl_method_call_get_name(method_call);
55 FlValue* args = fl_method_call_get_args(method_call);
56
57 g_autoptr(FlMethodResponse) response = nullptr;
58 if (strcmp(method, kActivateSystemCursorMethod) == 0) {
59 response = activate_system_cursor(self, args);
60 } else {
61 response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
62 }
63
64 g_autoptr(GError) error = nullptr;
65 if (!fl_method_call_respond(method_call, response, &error)) {
66 g_warning("Failed to send method call response: %s", error->message);
67 }
68 }
69
70 static void fl_mouse_cursor_channel_dispose(GObject* object) {
71 FlMouseCursorChannel* self = FL_MOUSE_CURSOR_CHANNEL(object);
72
73 g_clear_object(&self->channel);
74
75 G_OBJECT_CLASS(fl_mouse_cursor_channel_parent_class)->dispose(object);
76 }
77
78 static void fl_mouse_cursor_channel_class_init(
79 FlMouseCursorChannelClass* klass) {
80 G_OBJECT_CLASS(klass)->dispose = fl_mouse_cursor_channel_dispose;
81 }
82
83 static void fl_mouse_cursor_channel_init(FlMouseCursorChannel* self) {}
84
85 FlMouseCursorChannel* fl_mouse_cursor_channel_new(
86 FlBinaryMessenger* messenger,
87 FlMouseCursorChannelVTable* vtable,
88 gpointer user_data) {
89 g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
90
91 FlMouseCursorChannel* self = FL_MOUSE_CURSOR_CHANNEL(
92 g_object_new(fl_mouse_cursor_channel_get_type(), nullptr));
93
94 self->vtable = vtable;
95 self->user_data = user_data;
96
97 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
98 self->channel =
99 fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec));
100 fl_method_channel_set_method_call_handler(self->channel, method_call_cb, self,
101 nullptr);
102
103 return self;
104 }
FL_VALUE_TYPE_MAP
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:74
fl_mouse_cursor_channel.h
_FlMouseCursorChannel
Definition: fl_mouse_cursor_channel.cc:17
fl_method_channel_new
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
Definition: fl_method_channel.cc:112
fl_method_error_response_new
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
Definition: fl_method_response.cc:144
_FlMouseCursorChannel::vtable
FlMouseCursorChannelVTable * vtable
Definition: fl_mouse_cursor_channel.cc:22
_FlMouseCursorChannel::user_data
gpointer user_data
Definition: fl_mouse_cursor_channel.cc:24
fl_method_not_implemented_response_new
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
Definition: fl_method_response.cc:179
fl_standard_method_codec_new
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new()
Definition: fl_standard_method_codec.cc:291
fl_mouse_cursor_channel_new
FlMouseCursorChannel * fl_mouse_cursor_channel_new(FlBinaryMessenger *messenger, FlMouseCursorChannelVTable *vtable, gpointer user_data)
Definition: fl_mouse_cursor_channel.cc:85
fl_method_channel.h
_FlMouseCursorChannel::parent_instance
GObject parent_instance
Definition: fl_mouse_cursor_channel.cc:18
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
activate_system_cursor
static FlMethodResponse * activate_system_cursor(FlMouseCursorChannel *self, FlValue *args)
Definition: fl_mouse_cursor_channel.cc:30
kActivateSystemCursorMethod
static constexpr char kActivateSystemCursorMethod[]
Definition: fl_mouse_cursor_channel.cc:14
fl_value_lookup_string
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:811
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
fl_method_success_response_new
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
Definition: fl_method_response.cc:126
method_call_cb
static void method_call_cb(FlMethodChannel *channel, FlMethodCall *method_call, gpointer user_data)
Definition: fl_mouse_cursor_channel.cc:49
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
fl_method_call_respond
G_MODULE_EXPORT gboolean fl_method_call_respond(FlMethodCall *self, FlMethodResponse *response, GError **error)
Definition: fl_method_call.cc:77
FlMouseCursorChannelVTable
Definition: fl_mouse_cursor_channel.h:25
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
kBadArgumentsError
static constexpr char kBadArgumentsError[]
Definition: fl_mouse_cursor_channel.cc:13
method_call
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
Definition: fl_method_channel.h:120
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:68
fl_method_call_get_name
const G_MODULE_EXPORT gchar * fl_method_call_get_name(FlMethodCall *self)
Definition: fl_method_call.cc:67
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
fl_mouse_cursor_channel_dispose
static void fl_mouse_cursor_channel_dispose(GObject *object)
Definition: fl_mouse_cursor_channel.cc:70
kKindKey
static constexpr char kKindKey[]
Definition: fl_mouse_cursor_channel.cc:15
fl_standard_method_codec.h
kChannelName
static constexpr char kChannelName[]
Definition: fl_mouse_cursor_channel.cc:12
fl_method_channel_set_method_call_handler
G_MODULE_EXPORT void fl_method_channel_set_method_call_handler(FlMethodChannel *self, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_method_channel.cc:134
fl_mouse_cursor_channel_init
static void fl_mouse_cursor_channel_init(FlMouseCursorChannel *self)
Definition: fl_mouse_cursor_channel.cc:83
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_method_call_get_args
G_MODULE_EXPORT FlValue * fl_method_call_get_args(FlMethodCall *self)
Definition: fl_method_call.cc:72
_FlMouseCursorChannel::channel
FlMethodChannel * channel
Definition: fl_mouse_cursor_channel.cc:20
fl_mouse_cursor_channel_class_init
static void fl_mouse_cursor_channel_class_init(FlMouseCursorChannelClass *klass)
Definition: fl_mouse_cursor_channel.cc:78