From d642e3abdb6fa2ce4b523994c68fde4f584a0871 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Mon, 6 Jul 2020 17:52:40 -0700 Subject: [PATCH 1/8] Implement --- .../platform/linux/fl_mouse_cursor_plugin.cc | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/shell/platform/linux/fl_mouse_cursor_plugin.cc b/shell/platform/linux/fl_mouse_cursor_plugin.cc index d22f8231a3e65..1d9f2d7b9b630 100644 --- a/shell/platform/linux/fl_mouse_cursor_plugin.cc +++ b/shell/platform/linux/fl_mouse_cursor_plugin.cc @@ -14,6 +14,8 @@ static constexpr char kBadArgumentsError[] = "Bad Arguments"; static constexpr char kActivateSystemCursorMethod[] = "activateSystemCursor"; static constexpr char kKindKey[] = "kind"; +static GHashTable* systemCursorTable = nullptr; + struct _FlMouseCursorPlugin { GObject parent_instance; @@ -37,26 +39,22 @@ FlMethodResponse* activate_system_cursor(FlMouseCursorPlugin* self, if (fl_value_get_type(kind_value) == FL_VALUE_TYPE_STRING) kind = fl_value_get_string(kind_value); - const gchar* cursor_name = nullptr; - if (g_strcmp0(kind, "none") == 0) - cursor_name = "none"; - else if (g_strcmp0(kind, "basic") == 0) - cursor_name = "default"; - else if (g_strcmp0(kind, "click") == 0) - cursor_name = "pointer"; - else if (g_strcmp0(kind, "text") == 0) - cursor_name = "text"; - else if (g_strcmp0(kind, "forbidden") == 0) - cursor_name = "not-allowed"; - else if (g_strcmp0(kind, "grab") == 0) - cursor_name = "grab"; - else if (g_strcmp0(kind, "grabbing") == 0) - cursor_name = "grabbing"; - else if (g_strcmp0(kind, "resizeLeftRight") == 0) - cursor_name = "ew-resize"; - else if (g_strcmp0(kind, "resizeUpDown") == 0) - cursor_name = "ns-resize"; - else + if (systemCursorTable == nullptr) { + systemCursorTable = g_hash_table_new(g_str_hash, g_str_equal); + + // The following mapping must be kept in sync with Flutter framework's + // mouse_cursor.dart + g_hash_table_insert("none", "none"); + g_hash_table_insert("click", "pointer"); + g_hash_table_insert("text", "text"); + g_hash_table_insert("forbidden", "not-allowed"); + g_hash_table_insert("grab", "grabbing"); + g_hash_table_insert("resizeLeftRight", "ew-resize"); + g_hash_table_insert("resizeUpDown", "ns-resize"); + } + + const gchar* cursor_name = g_hash_table_lookup(systemCursorTable, kind); + if (cursor_name == nullptr) // Including "basic" kind cursor_name = "default"; GdkWindow* window = From c6f9698b2086ea7a04321016d6900921cd6366aa Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Mon, 6 Jul 2020 21:12:50 -0700 Subject: [PATCH 2/8] Fix insert --- shell/platform/linux/fl_mouse_cursor_plugin.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/shell/platform/linux/fl_mouse_cursor_plugin.cc b/shell/platform/linux/fl_mouse_cursor_plugin.cc index 1d9f2d7b9b630..d3f216255c59e 100644 --- a/shell/platform/linux/fl_mouse_cursor_plugin.cc +++ b/shell/platform/linux/fl_mouse_cursor_plugin.cc @@ -44,13 +44,13 @@ FlMethodResponse* activate_system_cursor(FlMouseCursorPlugin* self, // The following mapping must be kept in sync with Flutter framework's // mouse_cursor.dart - g_hash_table_insert("none", "none"); - g_hash_table_insert("click", "pointer"); - g_hash_table_insert("text", "text"); - g_hash_table_insert("forbidden", "not-allowed"); - g_hash_table_insert("grab", "grabbing"); - g_hash_table_insert("resizeLeftRight", "ew-resize"); - g_hash_table_insert("resizeUpDown", "ns-resize"); + g_hash_table_insert(systemCursorTable, "none", "none"); + g_hash_table_insert(systemCursorTable, "click", "pointer"); + g_hash_table_insert(systemCursorTable, "text", "text"); + g_hash_table_insert(systemCursorTable, "forbidden", "not-allowed"); + g_hash_table_insert(systemCursorTable, "grab", "grabbing"); + g_hash_table_insert(systemCursorTable, "resizeLeftRight", "ew-resize"); + g_hash_table_insert(systemCursorTable, "resizeUpDown", "ns-resize"); } const gchar* cursor_name = g_hash_table_lookup(systemCursorTable, kind); From 50be9fdf8e18b3f14863b1bd5e3f3c03810bf7c3 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Mon, 6 Jul 2020 21:35:27 -0700 Subject: [PATCH 3/8] pointer conversion --- .../platform/linux/fl_mouse_cursor_plugin.cc | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/shell/platform/linux/fl_mouse_cursor_plugin.cc b/shell/platform/linux/fl_mouse_cursor_plugin.cc index d3f216255c59e..1941348006d48 100644 --- a/shell/platform/linux/fl_mouse_cursor_plugin.cc +++ b/shell/platform/linux/fl_mouse_cursor_plugin.cc @@ -16,6 +16,10 @@ static constexpr char kKindKey[] = "kind"; static GHashTable* systemCursorTable = nullptr; +bool define_system_cursor(const gchar *key, const gchar *value) { + return g_hash_table_insert(systemCursorTable, (gpointer)key, (gpointer)value); +} + struct _FlMouseCursorPlugin { GObject parent_instance; @@ -44,16 +48,16 @@ FlMethodResponse* activate_system_cursor(FlMouseCursorPlugin* self, // The following mapping must be kept in sync with Flutter framework's // mouse_cursor.dart - g_hash_table_insert(systemCursorTable, "none", "none"); - g_hash_table_insert(systemCursorTable, "click", "pointer"); - g_hash_table_insert(systemCursorTable, "text", "text"); - g_hash_table_insert(systemCursorTable, "forbidden", "not-allowed"); - g_hash_table_insert(systemCursorTable, "grab", "grabbing"); - g_hash_table_insert(systemCursorTable, "resizeLeftRight", "ew-resize"); - g_hash_table_insert(systemCursorTable, "resizeUpDown", "ns-resize"); + define_system_cursor("none", "none"); + define_system_cursor("click", "pointer"); + define_system_cursor("text", "text"); + define_system_cursor("forbidden", "not-allowed"); + define_system_cursor("grab", "grabbing"); + define_system_cursor("resizeLeftRight", "ew-resize"); + define_system_cursor("resizeUpDown", "ns-resize"); } - const gchar* cursor_name = g_hash_table_lookup(systemCursorTable, kind); + const gchar* cursor_name = (const gchar*)g_hash_table_lookup(systemCursorTable, kind); if (cursor_name == nullptr) // Including "basic" kind cursor_name = "default"; From 587a0a1048d04a0c2895d3ca44cb03613f9cc493 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Thu, 9 Jul 2020 17:34:36 -0700 Subject: [PATCH 4/8] Address comments --- .../platform/linux/fl_mouse_cursor_plugin.cc | 54 ++++++++++++------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/shell/platform/linux/fl_mouse_cursor_plugin.cc b/shell/platform/linux/fl_mouse_cursor_plugin.cc index 1941348006d48..87b1081a3b814 100644 --- a/shell/platform/linux/fl_mouse_cursor_plugin.cc +++ b/shell/platform/linux/fl_mouse_cursor_plugin.cc @@ -14,11 +14,7 @@ static constexpr char kBadArgumentsError[] = "Bad Arguments"; static constexpr char kActivateSystemCursorMethod[] = "activateSystemCursor"; static constexpr char kKindKey[] = "kind"; -static GHashTable* systemCursorTable = nullptr; - -bool define_system_cursor(const gchar *key, const gchar *value) { - return g_hash_table_insert(systemCursorTable, (gpointer)key, (gpointer)value); -} +static constexpr char kFallbackCursor[] = "default"; struct _FlMouseCursorPlugin { GObject parent_instance; @@ -26,10 +22,35 @@ struct _FlMouseCursorPlugin { FlMethodChannel* channel; FlView* view; + + GHashTable* system_cursor_table; }; G_DEFINE_TYPE(FlMouseCursorPlugin, fl_mouse_cursor_plugin, G_TYPE_OBJECT) +// Insert a new entry into a hashtable from strings to strings. +// +// Returns whether the newly added value was already in the hash table or not. +static bool define_system_cursor(GHashTable* table, const gchar *key, const gchar *value) { + return g_hash_table_insert(table, (gpointer)key, (gpointer)value); +} + +// Populate the hash table so that it maps from Flutter's cursor kinds to GTK's +// cursor values. +// +// The table must have been created as a hashtable from strings to strings. +static void populate_system_cursor_table(GHashTable* table) { + // The following mapping must be kept in sync with Flutter framework's + // mouse_cursor.dart. + define_system_cursor(table, "none", "none"); + define_system_cursor(table, "click", "pointer"); + define_system_cursor(table, "text", "text"); + define_system_cursor(table, "forbidden", "not-allowed"); + define_system_cursor(table, "grab", "grabbing"); + define_system_cursor(table, "resizeLeftRight", "ew-resize"); + define_system_cursor(table, "resizeUpDown", "ns-resize"); +} + // Sets the mouse cursor. FlMethodResponse* activate_system_cursor(FlMouseCursorPlugin* self, FlValue* args) { @@ -43,23 +64,14 @@ FlMethodResponse* activate_system_cursor(FlMouseCursorPlugin* self, if (fl_value_get_type(kind_value) == FL_VALUE_TYPE_STRING) kind = fl_value_get_string(kind_value); - if (systemCursorTable == nullptr) { - systemCursorTable = g_hash_table_new(g_str_hash, g_str_equal); - - // The following mapping must be kept in sync with Flutter framework's - // mouse_cursor.dart - define_system_cursor("none", "none"); - define_system_cursor("click", "pointer"); - define_system_cursor("text", "text"); - define_system_cursor("forbidden", "not-allowed"); - define_system_cursor("grab", "grabbing"); - define_system_cursor("resizeLeftRight", "ew-resize"); - define_system_cursor("resizeUpDown", "ns-resize"); + if (self->system_cursor_table == nullptr) { + self->system_cursor_table = g_hash_table_new(g_str_hash, g_str_equal); + populate_system_cursor_table(self->system_cursor_table); } - const gchar* cursor_name = (const gchar*)g_hash_table_lookup(systemCursorTable, kind); - if (cursor_name == nullptr) // Including "basic" kind - cursor_name = "default"; + const gchar* cursor_name = (const gchar*)g_hash_table_lookup(self->system_cursor_table, kind); + if (cursor_name == nullptr) + cursor_name = kFallbackCursor; GdkWindow* window = gtk_widget_get_window(gtk_widget_get_toplevel(GTK_WIDGET(self->view))); @@ -104,6 +116,8 @@ static void fl_mouse_cursor_plugin_dispose(GObject* object) { self->view = nullptr; } + g_hash_table_destroy(self->system_cursor_table)'' + G_OBJECT_CLASS(fl_mouse_cursor_plugin_parent_class)->dispose(object); } From 4a65f14274c2ad11abb5e7294ee9aa02302594ba Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Thu, 9 Jul 2020 17:38:19 -0700 Subject: [PATCH 5/8] Fix compile --- shell/platform/linux/fl_mouse_cursor_plugin.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/linux/fl_mouse_cursor_plugin.cc b/shell/platform/linux/fl_mouse_cursor_plugin.cc index 87b1081a3b814..3c799c72260b8 100644 --- a/shell/platform/linux/fl_mouse_cursor_plugin.cc +++ b/shell/platform/linux/fl_mouse_cursor_plugin.cc @@ -116,7 +116,7 @@ static void fl_mouse_cursor_plugin_dispose(GObject* object) { self->view = nullptr; } - g_hash_table_destroy(self->system_cursor_table)'' + g_hash_table_destroy(self->system_cursor_table); G_OBJECT_CLASS(fl_mouse_cursor_plugin_parent_class)->dispose(object); } From a9843971a66cbd749f168bc67254955742e981f7 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Thu, 9 Jul 2020 17:47:25 -0700 Subject: [PATCH 6/8] Dont destroy unless nonnull --- shell/platform/linux/fl_mouse_cursor_plugin.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shell/platform/linux/fl_mouse_cursor_plugin.cc b/shell/platform/linux/fl_mouse_cursor_plugin.cc index 3c799c72260b8..945337ee9fda5 100644 --- a/shell/platform/linux/fl_mouse_cursor_plugin.cc +++ b/shell/platform/linux/fl_mouse_cursor_plugin.cc @@ -116,7 +116,8 @@ static void fl_mouse_cursor_plugin_dispose(GObject* object) { self->view = nullptr; } - g_hash_table_destroy(self->system_cursor_table); + if (self->system_cursor_table) + g_hash_table_destroy(self->system_cursor_table); G_OBJECT_CLASS(fl_mouse_cursor_plugin_parent_class)->dispose(object); } From e80eeafab704cfe73c4472c3e244a7853f9b8be5 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Fri, 10 Jul 2020 12:45:57 -0700 Subject: [PATCH 7/8] Address comments --- shell/platform/linux/fl_mouse_cursor_plugin.cc | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/shell/platform/linux/fl_mouse_cursor_plugin.cc b/shell/platform/linux/fl_mouse_cursor_plugin.cc index 945337ee9fda5..8d0ad89897920 100644 --- a/shell/platform/linux/fl_mouse_cursor_plugin.cc +++ b/shell/platform/linux/fl_mouse_cursor_plugin.cc @@ -31,8 +31,11 @@ G_DEFINE_TYPE(FlMouseCursorPlugin, fl_mouse_cursor_plugin, G_TYPE_OBJECT) // Insert a new entry into a hashtable from strings to strings. // // Returns whether the newly added value was already in the hash table or not. -static bool define_system_cursor(GHashTable* table, const gchar *key, const gchar *value) { - return g_hash_table_insert(table, (gpointer)key, (gpointer)value); +static bool define_system_cursor(GHashTable* table, + const gchar* key, + const gchar* value) { + return g_hash_table_insert(table, static_cast(key), + static_cast(value)); } // Populate the hash table so that it maps from Flutter's cursor kinds to GTK's @@ -69,7 +72,8 @@ FlMethodResponse* activate_system_cursor(FlMouseCursorPlugin* self, populate_system_cursor_table(self->system_cursor_table); } - const gchar* cursor_name = (const gchar*)g_hash_table_lookup(self->system_cursor_table, kind); + const gchar* cursor_name = static_cast( + g_hash_table_lookup(self->system_cursor_table, kind)); if (cursor_name == nullptr) cursor_name = kFallbackCursor; @@ -116,8 +120,7 @@ static void fl_mouse_cursor_plugin_dispose(GObject* object) { self->view = nullptr; } - if (self->system_cursor_table) - g_hash_table_destroy(self->system_cursor_table); + g_clear_pointer(&self->system_cursor_table, g_hash_table_unref); G_OBJECT_CLASS(fl_mouse_cursor_plugin_parent_class)->dispose(object); } From 24e17c53a42a7bbc9e1b2a699d1b62a73ffcf367 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Fri, 10 Jul 2020 14:35:44 -0700 Subject: [PATCH 8/8] Fix cast --- shell/platform/linux/fl_mouse_cursor_plugin.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/shell/platform/linux/fl_mouse_cursor_plugin.cc b/shell/platform/linux/fl_mouse_cursor_plugin.cc index 8d0ad89897920..e0669505a9712 100644 --- a/shell/platform/linux/fl_mouse_cursor_plugin.cc +++ b/shell/platform/linux/fl_mouse_cursor_plugin.cc @@ -34,8 +34,9 @@ G_DEFINE_TYPE(FlMouseCursorPlugin, fl_mouse_cursor_plugin, G_TYPE_OBJECT) static bool define_system_cursor(GHashTable* table, const gchar* key, const gchar* value) { - return g_hash_table_insert(table, static_cast(key), - static_cast(value)); + return g_hash_table_insert( + table, reinterpret_cast(const_cast(key)), + reinterpret_cast(const_cast(value))); } // Populate the hash table so that it maps from Flutter's cursor kinds to GTK's @@ -72,7 +73,7 @@ FlMethodResponse* activate_system_cursor(FlMouseCursorPlugin* self, populate_system_cursor_table(self->system_cursor_table); } - const gchar* cursor_name = static_cast( + const gchar* cursor_name = reinterpret_cast( g_hash_table_lookup(self->system_cursor_table, kind)); if (cursor_name == nullptr) cursor_name = kFallbackCursor;