[gnome-flashback] daemons: add media keys



commit 7964fd69a54f2e9ca8b712d289515eaa6db7e9bf
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sat Mar 5 10:06:25 2022 +0200

    daemons: add media keys
    
    This will be used to handle screenshot and screencast keybindings.

 configure.ac                                       |   6 +
 daemons/Makefile.am                                |   1 +
 daemons/media-keys/Makefile.am                     |  35 ++++
 daemons/media-keys/gf-media-keys-main.c            | 219 +++++++++++++++++++++
 daemons/media-keys/gf-media-keys.c                 |  42 ++++
 daemons/media-keys/gf-media-keys.h                 |  32 +++
 data/autostart/Makefile.am                         |   4 +
 .../gnome-flashback-media-keys.desktop.in.in       |   9 +
 po/POTFILES.in                                     |   1 +
 9 files changed, 349 insertions(+)
---
diff --git a/configure.ac b/configure.ac
index 5894bd1..890aa42 100644
--- a/configure.ac
+++ b/configure.ac
@@ -192,6 +192,11 @@ PKG_CHECK_MODULES([INPUT_SOURCES], [
   xkeyboard-config
 ])
 
+PKG_CHECK_MODULES([MEDIA_KEYS], [
+  glib-2.0 >= $GLIB_REQUIRED
+  gtk+-3.0 >= $GTK_REQUIRED
+])
+
 PKG_CHECK_MODULES([NOTIFICATIONS], [
   gdk-pixbuf-2.0 >= $GDK_PIXBUF_REQUIRED
   gio-unix-2.0 >= $GLIB_REQUIRED
@@ -440,6 +445,7 @@ AC_CONFIG_FILES([
 
   daemons/Makefile
   daemons/clipboard/Makefile
+  daemons/media-keys/Makefile
 
   gnome-flashback/Makefile
   gnome-flashback/liba11y-keyboard/Makefile
diff --git a/daemons/Makefile.am b/daemons/Makefile.am
index 7054482..c501fa8 100644
--- a/daemons/Makefile.am
+++ b/daemons/Makefile.am
@@ -2,6 +2,7 @@ NULL =
 
 SUBDIRS = \
        clipboard \
+       media-keys \
        $(NULL)
 
 -include $(top_srcdir)/git.mk
diff --git a/daemons/media-keys/Makefile.am b/daemons/media-keys/Makefile.am
new file mode 100644
index 0000000..8cfb164
--- /dev/null
+++ b/daemons/media-keys/Makefile.am
@@ -0,0 +1,35 @@
+NULL =
+
+libexec_PROGRAMS = \
+       gnome-flashback-media-keys \
+       $(NULL)
+
+gnome_flashback_media_keys_CPPFLAGS = \
+       -DG_LOG_DOMAIN=\"gnome-flashback-media-keys\" \
+       -DG_LOG_USE_STRUCTURED=1 \
+       -I$(top_srcdir) \
+       $(NULL)
+
+gnome_flashback_media_keys_CFLAGS = \
+       $(MEDIA_KEYS_CFLAGS) \
+       $(WARN_CFLAGS) \
+       $(AM_CFLAGS) \
+       $(NULL)
+
+gnome_flashback_media_keys_SOURCES = \
+       gf-media-keys-main.c \
+       gf-media-keys.c \
+       gf-media-keys.h \
+       $(NULL)
+
+gnome_flashback_media_keys_LDFLAGS = \
+       $(WARN_LDFLAGS) \
+       $(AM_LDFLAGS) \
+       $(NULL)
+
+gnome_flashback_media_keys_LDADD = \
+       $(top_builddir)/dbus/libdbus.la \
+       $(MEDIA_KEYS_LIBS) \
+       $(NULL)
+
+-include $(top_srcdir)/git.mk
diff --git a/daemons/media-keys/gf-media-keys-main.c b/daemons/media-keys/gf-media-keys-main.c
new file mode 100644
index 0000000..a1fb4e4
--- /dev/null
+++ b/daemons/media-keys/gf-media-keys-main.c
@@ -0,0 +1,219 @@
+/*
+ * Copyright (C) 2022 Alberts Muktupāvels
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <glib-unix.h>
+#include <gtk/gtk.h>
+#include <stdlib.h>
+
+#include "dbus/gf-session-manager-gen.h"
+#include "dbus/gf-sm-client-private-gen.h"
+#include "gf-media-keys.h"
+
+static char *startup_id = NULL;
+static GMainLoop *loop = NULL;
+static GfSmClientPrivateGen *client_private = NULL;
+
+static gboolean
+on_term_signal (gpointer user_data)
+{
+  g_main_loop_quit (loop);
+
+  return G_SOURCE_REMOVE;
+}
+
+static gboolean
+on_int_signal (gpointer user_data)
+{
+  g_main_loop_quit (loop);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+respond_to_end_session (void)
+{
+  gf_sm_client_private_gen_call_end_session_response (client_private,
+                                                      TRUE,
+                                                      "",
+                                                      NULL,
+                                                      NULL,
+                                                      NULL);
+}
+
+static void
+end_session_cb (GfSmClientPrivateGen *object,
+                guint                 flags,
+                gpointer              user_data)
+{
+  respond_to_end_session ();
+}
+
+static void
+query_end_session_cb (GfSmClientPrivateGen *object,
+                      guint                 flags,
+                      gpointer              user_data)
+{
+  respond_to_end_session ();
+}
+
+static void
+stop_cb (GfSmClientPrivateGen *object,
+         gpointer              user_data)
+{
+  g_main_loop_quit (loop);
+}
+
+static void
+client_private_ready_cb (GObject      *source_object,
+                         GAsyncResult *res,
+                         gpointer      user_data)
+{
+  GError *error;
+
+  error = NULL;
+  client_private = gf_sm_client_private_gen_proxy_new_for_bus_finish (res,
+                                                                      &error);
+
+  if (error != NULL)
+    {
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_warning ("Failed to get a client private proxy: %s", error->message);
+
+      g_error_free (error);
+      return;
+    }
+
+  g_signal_connect (client_private,
+                    "end-session",
+                    G_CALLBACK (end_session_cb),
+                    NULL);
+
+  g_signal_connect (client_private,
+                    "query-end-session",
+                    G_CALLBACK (query_end_session_cb),
+                    NULL);
+
+  g_signal_connect (client_private,
+                    "stop",
+                    G_CALLBACK (stop_cb),
+                    NULL);
+}
+
+static void
+register_client_cb (GObject      *source_object,
+                    GAsyncResult *res,
+                    gpointer      user_data)
+{
+  GError *error;
+  char *client_id;
+
+  error = NULL;
+  gf_session_manager_gen_call_register_client_finish (GF_SESSION_MANAGER_GEN (source_object),
+                                                      &client_id,
+                                                      res,
+                                                      &error);
+
+  if (error != NULL)
+    {
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_warning ("Failed to register client: %s", error->message);
+
+      g_error_free (error);
+      return;
+    }
+
+  gf_sm_client_private_gen_proxy_new_for_bus (G_BUS_TYPE_SESSION,
+                                              G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
+                                              "org.gnome.SessionManager",
+                                              client_id,
+                                              NULL,
+                                              client_private_ready_cb,
+                                              NULL);
+
+  g_free (client_id);
+}
+
+static void
+session_manager_ready_cb (GObject      *source_object,
+                          GAsyncResult *res,
+                          gpointer      user_data)
+{
+  GError *error;
+  GfSessionManagerGen *session_manager;
+
+  error = NULL;
+  session_manager = gf_session_manager_gen_proxy_new_for_bus_finish (res,
+                                                                     &error);
+
+  if (error != NULL)
+    {
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_warning ("Failed to get session manager proxy: %s", error->message);
+
+      g_error_free (error);
+      return;
+    }
+
+  gf_session_manager_gen_call_register_client (session_manager,
+                                               "gnome-flashback-media-keys",
+                                               startup_id,
+                                               NULL,
+                                               register_client_cb,
+                                               NULL);
+
+  g_object_unref (session_manager);
+}
+
+int
+main (int argc,
+      char *argv[])
+{
+  const char *autostart_id;
+  GfMediaKeys *media_keys;
+
+  autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
+  startup_id = g_strdup (autostart_id != NULL ? autostart_id : "");
+  g_unsetenv ("DESKTOP_AUTOSTART_ID");
+
+  gtk_init (&argc, &argv);
+
+  loop = g_main_loop_new (NULL, FALSE);
+  media_keys = gf_media_keys_new ();
+
+  g_unix_signal_add (SIGTERM, on_term_signal, NULL);
+  g_unix_signal_add (SIGINT, on_int_signal, NULL);
+
+  gf_session_manager_gen_proxy_new_for_bus (G_BUS_TYPE_SESSION,
+                                            G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+                                            G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+                                            "org.gnome.SessionManager",
+                                            "/org/gnome/SessionManager",
+                                            NULL,
+                                            session_manager_ready_cb,
+                                            NULL);
+
+  g_main_loop_run (loop);
+  g_main_loop_unref (loop);
+
+  g_object_unref (media_keys);
+  g_clear_object (&client_private);
+  g_free (startup_id);
+
+  return EXIT_SUCCESS;
+}
diff --git a/daemons/media-keys/gf-media-keys.c b/daemons/media-keys/gf-media-keys.c
new file mode 100644
index 0000000..a216143
--- /dev/null
+++ b/daemons/media-keys/gf-media-keys.c
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2022 Alberts Muktupāvels
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "gf-media-keys.h"
+
+struct _GfMediaKeys
+{
+  GObject parent;
+};
+
+G_DEFINE_TYPE (GfMediaKeys, gf_media_keys, G_TYPE_OBJECT)
+
+static void
+gf_media_keys_class_init (GfMediaKeysClass *self_class)
+{
+}
+
+static void
+gf_media_keys_init (GfMediaKeys *self)
+{
+}
+
+GfMediaKeys *
+gf_media_keys_new (void)
+{
+  return g_object_new (GF_TYPE_MEDIA_KEYS, NULL);
+}
diff --git a/daemons/media-keys/gf-media-keys.h b/daemons/media-keys/gf-media-keys.h
new file mode 100644
index 0000000..38c4609
--- /dev/null
+++ b/daemons/media-keys/gf-media-keys.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2022 Alberts Muktupāvels
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GF_MEDIA_KEYS_H
+#define GF_MEDIA_KEYS_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GF_TYPE_MEDIA_KEYS (gf_media_keys_get_type ())
+G_DECLARE_FINAL_TYPE (GfMediaKeys, gf_media_keys, GF, MEDIA_KEYS, GObject)
+
+GfMediaKeys *gf_media_keys_new (void);
+
+G_END_DECLS
+
+#endif
diff --git a/data/autostart/Makefile.am b/data/autostart/Makefile.am
index a2a5392..9395aa4 100644
--- a/data/autostart/Makefile.am
+++ b/data/autostart/Makefile.am
@@ -3,6 +3,7 @@ NULL =
 autostartdir = $(sysconfdir)/xdg/autostart
 autostart_DATA = \
        gnome-flashback-clipboard.desktop \
+       gnome-flashback-media-keys.desktop \
        gnome-flashback-nm-applet.desktop \
        $(NULL)
 
@@ -15,12 +16,15 @@ autostart_DATA = \
 
 EXTRA_DIST = \
        gnome-flashback-clipboard.desktop.in.in \
+       gnome-flashback-media-keys.desktop.in.in \
        gnome-flashback-nm-applet.desktop.in \
        $(NULL)
 
 CLEANFILES = \
        gnome-flashback-clipboard.desktop.in \
        gnome-flashback-clipboard.desktop \
+       gnome-flashback-media-keys.desktop.in \
+       gnome-flashback-media-keys.desktop \
        gnome-flashback-nm-applet.desktop \
        $(NULL)
 
diff --git a/data/autostart/gnome-flashback-media-keys.desktop.in.in 
b/data/autostart/gnome-flashback-media-keys.desktop.in.in
new file mode 100644
index 0000000..495de16
--- /dev/null
+++ b/data/autostart/gnome-flashback-media-keys.desktop.in.in
@@ -0,0 +1,9 @@
+[Desktop Entry]
+Type=Application
+Name=MediaKeys (GNOME Flashback)
+Exec=@libexecdir@/gnome-flashback-media-keys
+OnlyShowIn=GNOME-Flashback;
+NoDisplay=true
+X-GNOME-Autostart-Phase=Initialization
+X-GNOME-Autostart-Notify=true
+X-GNOME-AutoRestart=true
diff --git a/po/POTFILES.in b/po/POTFILES.in
index d6e7f8f..88e8ad2 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -3,6 +3,7 @@
 backends/gf-monitor.c
 data/applications/gnome-flashback.desktop.in.in
 data/autostart/gnome-flashback-clipboard.desktop.in.in
+data/autostart/gnome-flashback-media-keys.desktop.in.in
 data/autostart/gnome-flashback-nm-applet.desktop.in
 data/directories/X-GNOME-Flashback-Settings.directory.desktop.in
 data/directories/X-GNOME-Flashback-Settings-System.directory.desktop.in


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]