diff options
author | Matthias Clasen <mclasen@redhat.com> | 2019-12-31 10:10:49 -0500 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2020-01-08 18:48:19 -0500 |
commit | 8137dea8c1a575ed6610f59a6a0cea83d898388e (patch) | |
tree | d869f4f4364a5514ed599b29b179f7ac92506622 /gdk/gdkcontentdeserializer.c | |
parent | b15c31a3f7e38b078869e838a9123c93ff4db1e0 (diff) | |
download | gtk+-8137dea8c1a575ed6610f59a6a0cea83d898388e.tar.gz |
Add (de)serializers for GdkRGBA
This is a format that we use in various
color picker widgets, so support it in GDK.
Diffstat (limited to 'gdk/gdkcontentdeserializer.c')
-rw-r--r-- | gdk/gdkcontentdeserializer.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/gdk/gdkcontentdeserializer.c b/gdk/gdkcontentdeserializer.c index 79b7df86a2..0015520b3c 100644 --- a/gdk/gdkcontentdeserializer.c +++ b/gdk/gdkcontentdeserializer.c @@ -24,6 +24,7 @@ #include "gdkcontentformats.h" #include "filetransferportalprivate.h" #include "gdktexture.h" +#include "gdkrgbaprivate.h" #include <gdk-pixbuf/gdk-pixbuf.h> @@ -849,6 +850,63 @@ file_uri_deserializer (GdkContentDeserializer *deserializer) } static void +color_deserializer_finish (GObject *source, + GAsyncResult *result, + gpointer deserializer) +{ + GOutputStream *stream = G_OUTPUT_STREAM (source); + GError *error = NULL; + gssize written; + + written = g_output_stream_splice_finish (stream, result, &error); + if (written < 0) + { + gdk_content_deserializer_return_error (deserializer, error); + return; + } + else if (written == 0) + { + GdkRGBA black = GDK_RGBA ("000"); + + /* Never return NULL, we only return that on error */ + g_value_set_boxed (gdk_content_deserializer_get_value (deserializer), &black); + } + else + { + guint16 *data; + GdkRGBA rgba; + + data = (guint16 *)g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (stream)); + rgba.red = data[0] / 65535.0; + rgba.green = data[1] / 65535.0; + rgba.blue = data[2] / 65535.0; + rgba.alpha = data[3] / 65535.0; + + g_value_set_boxed (gdk_content_deserializer_get_value (deserializer), &rgba); + } + gdk_content_deserializer_return_success (deserializer); +} + +static void +color_deserializer (GdkContentDeserializer *deserializer) +{ + GOutputStream *output; + guint16 *data; + + data = g_new0 (guint16, 4); + output = g_memory_output_stream_new (data, 4 * sizeof (guint16), NULL, g_free); + + g_output_stream_splice_async (output, + gdk_content_deserializer_get_input_stream (deserializer), + G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, + gdk_content_deserializer_get_priority (deserializer), + gdk_content_deserializer_get_cancellable (deserializer), + color_deserializer_finish, + deserializer); + g_object_unref (output); +} + +static void init (void) { static gboolean initialized = FALSE; @@ -956,5 +1014,11 @@ init (void) string_deserializer, (gpointer) "ASCII", NULL); + + gdk_content_register_deserializer ("application/x-color", + GDK_TYPE_RGBA, + color_deserializer, + NULL, + NULL); } |