[Initiatives.wiki] Upload attachment hello.c
- From: Alexander Mikhaylenko <alexm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [Initiatives.wiki] Upload attachment hello.c
- Date: Tue, 28 Sep 2021 22:53:13 +0000 (UTC)
commit 28d4c8afdee6b37ebdc52ce9a444119459603b11
Author: Alexander Mikhaylenko <alexm gnome org>
Date: Tue Sep 28 22:53:13 2021 +0000
Upload attachment hello.c
uploads/76dfcc9b07d234cb8922cf800e743f02/hello.c | 197 +++++++++++++++++++++++
1 file changed, 197 insertions(+)
---
diff --git a/uploads/76dfcc9b07d234cb8922cf800e743f02/hello.c
b/uploads/76dfcc9b07d234cb8922cf800e743f02/hello.c
new file mode 100644
index 0000000..c3b0026
--- /dev/null
+++ b/uploads/76dfcc9b07d234cb8922cf800e743f02/hello.c
@@ -0,0 +1,197 @@
+#include <gio/gio.h>
+#include <SDL.h>
+
+typedef struct {
+ SDL_Window *window;
+ SDL_Renderer *renderer;
+ GMainLoop *main_loop;
+ GDBusProxy *settings_portal;
+ gboolean dark;
+ gboolean queue_redraw;
+} SampleData;
+
+typedef enum {
+ DEFAULT,
+ PREFER_DARK,
+ PREFER_LIGHT
+} ColorScheme;
+
+static void
+set_color_scheme (SampleData *data,
+ GVariant *variant)
+{
+ ColorScheme color_scheme = g_variant_get_uint32 (variant);
+
+ if (color_scheme > PREFER_LIGHT)
+ color_scheme = DEFAULT;
+
+ data->dark = color_scheme == PREFER_DARK;
+ data->queue_redraw = TRUE;
+}
+
+static void
+settings_portal_changed_cb (GDBusProxy *proxy,
+ const char *sender_name,
+ const char *signal_name,
+ GVariant *parameters,
+ SampleData *data)
+{
+ const char *namespace;
+ const char *name;
+ g_autoptr (GVariant) value = NULL;
+
+ if (g_strcmp0 (signal_name, "SettingChanged"))
+ return;
+
+ g_variant_get (parameters, "(&s&sv)", &namespace, &name, &value);
+
+ if (g_strcmp0 (namespace, "org.freedesktop.appearance") ||
+ g_strcmp0 (name, "color-scheme"))
+ return;
+
+ set_color_scheme (data, value);
+}
+
+static gboolean
+read_color_scheme (GDBusProxy *proxy,
+ GVariant **out)
+{
+ g_autoptr (GError) error = NULL;
+ g_autoptr (GVariant) ret = NULL;
+ g_autoptr (GVariant) child = NULL;
+ uint32_t test = 0;
+
+ ret = g_dbus_proxy_call_sync (proxy,
+ "Read",
+ g_variant_new ("(ss)",
+ "org.freedesktop.appearance",
+ "color-scheme"),
+ G_DBUS_CALL_FLAGS_NONE,
+ G_MAXINT,
+ NULL,
+ &error);
+
+ if (error) {
+ if (error->domain == G_DBUS_ERROR &&
+ error->code == G_DBUS_ERROR_SERVICE_UNKNOWN) {
+ g_debug ("Portal not found: %s", error->message);
+
+ return FALSE;
+ }
+
+ g_critical ("Couldn't read the color-scheme setting: %s", error->message);
+
+ return FALSE;
+ }
+
+ g_variant_get (ret, "(v)", &child);
+ g_variant_get (child, "v", out);
+
+ return TRUE;
+}
+
+static void
+init_portal (SampleData *data)
+{
+ g_autoptr (GError) error = NULL;
+ g_autoptr (GVariant) value = NULL;
+
+ data->settings_portal = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+ G_DBUS_PROXY_FLAGS_NONE,
+ NULL,
+ "org.freedesktop.portal.Desktop",
+ "/org/freedesktop/portal/desktop",
+ "org.freedesktop.portal.Settings",
+ NULL,
+ &error);
+ if (error) {
+ g_debug ("Settings portal not found: %s", error->message);
+
+ return;
+ }
+
+ if (!read_color_scheme (data->settings_portal, &value))
+ return;
+
+ set_color_scheme (data, value);
+
+ g_signal_connect (data->settings_portal, "g-signal",
+ G_CALLBACK (settings_portal_changed_cb), data);
+}
+
+static gboolean
+main_loop (SampleData *data)
+{
+ SDL_Event e;
+
+ SDL_Rect rect = { 220, 140, 200, 200 };
+
+ while (SDL_PollEvent(&e) > 0) {
+ if (e.type == SDL_QUIT) {
+ g_main_loop_quit (data->main_loop);
+ return G_SOURCE_REMOVE;
+ }
+ }
+
+ if (data->queue_redraw) {
+ SDL_RenderClear (data->renderer);
+
+ if (data->dark) {
+ SDL_SetRenderDrawColor (data->renderer, 100, 100, 100, 255);
+ SDL_RenderFillRect (data->renderer, &rect);
+ SDL_SetRenderDrawColor (data->renderer, 30, 30, 30, 255);
+ } else {
+ SDL_SetRenderDrawColor (data->renderer, 255, 0, 0, 255);
+ SDL_RenderFillRect (data->renderer, &rect);
+ SDL_SetRenderDrawColor (data->renderer, 240, 240, 240, 255);
+ }
+
+ SDL_RenderPresent (data->renderer);
+ SDL_UpdateWindowSurface (data->window);
+ }
+
+ return G_SOURCE_CONTINUE;
+}
+
+int
+main (int argc,
+ char *argv[])
+{
+ SampleData data;
+
+ if (SDL_Init (SDL_INIT_VIDEO) < 0) {
+ g_critical ("Failed to initialize SDL: %s", SDL_GetError());
+ return 1;
+ }
+
+ data.window = SDL_CreateWindow ("Example",
+ SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED,
+ 640, 480,
+ SDL_WINDOW_BORDERLESS);
+
+ if (!data.window) {
+ g_critical ("Failed to create window: %s", SDL_GetError());
+ return 1;
+ }
+
+ data.renderer = SDL_CreateRenderer (data.window, -1,
+ SDL_RENDERER_ACCELERATED |
+ SDL_RENDERER_PRESENTVSYNC);
+
+ if (!data.renderer) {
+ g_critical ("Failed to create renderer: %s", SDL_GetError());
+ return 1;
+ }
+
+ data.main_loop = g_main_loop_new (NULL, FALSE);
+ data.queue_redraw = TRUE;
+
+ init_portal (&data);
+
+ g_idle_add (G_SOURCE_FUNC (main_loop), &data);
+
+ g_main_loop_run (data.main_loop);
+
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]