dbus: add new API for owning the main DBus name

This is using GDBus and is supposed to be called from core during startup.
This commit is contained in:
Kondor Dániel
2025-12-29 00:41:19 +01:00
parent 14ed77be0b
commit 3d5482b3e6
4 changed files with 123 additions and 12 deletions

View File

@@ -55,7 +55,7 @@ SET(core_lib_SRCS
cairo-dock-gui-manager.c cairo-dock-gui-manager.h
cairo-dock-gui-factory.c cairo-dock-gui-factory.h
cairo-dock-keybinder.c cairo-dock-keybinder.h
cairo-dock-dbus.c cairo-dock-dbus.h
cairo-dock-dbus.c cairo-dock-dbus.h cairo-dock-dbus-priv.h
cairo-dock-keyfile-utilities.c cairo-dock-keyfile-utilities.h
cairo-dock-packages.c cairo-dock-packages.h
cairo-dock-particle-system.c cairo-dock-particle-system.h

View File

@@ -0,0 +1,36 @@
/*
* This file is a part of the Cairo-Dock project
*
* Copyright : (C) see the 'copyright' file.
* E-mail : see the 'copyright' file.
*
* 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 __CAIRO_DOCK_DBUS_PRIV__
#define __CAIRO_DOCK_DBUS_PRIV__
#include "cairo-dock-dbus.h"
G_BEGIN_DECLS
/** Try to own our DBus name (org.cairodock.CairoDock). This is a synchronous
* function that will block until the name is acquired or rejected. It should
* be only called during initialization as it will iterate the default context.
*@return TRUE if the name has been acquired
*/
gboolean cairo_dock_dbus_own_name (void);
G_END_DECLS
#endif

View File

@@ -21,7 +21,68 @@
#include <glib.h>
#include "cairo-dock-log.h"
#include "cairo-dock-dbus.h"
#include "cairo-dock-dbus-priv.h"
/***********************************************************************
* New interface */
static GDBusConnection *s_pMainConnection = NULL;
static const gchar *s_cBusName = "org.cairodock.CairoDock";
static gboolean s_bNameOwned = FALSE;
gboolean cairo_dock_dbus_is_enabled (void)
{
return (s_pMainConnection != NULL);
}
GDBusConnection *cairo_dock_dbus_get_session_bus (void)
{
return s_pMainConnection;
}
const gchar *cairo_dock_dbus_get_owned_name (void)
{
return s_bNameOwned ? s_cBusName : NULL;
}
static void _on_bus_acquired (GDBusConnection *pConn, G_GNUC_UNUSED const gchar* cName, G_GNUC_UNUSED gpointer ptr)
{
s_pMainConnection = pConn;
}
gboolean s_bNameAcquiredOrLost = FALSE;
static void _on_name_acquired (G_GNUC_UNUSED GDBusConnection* pConn, G_GNUC_UNUSED const gchar* cName, G_GNUC_UNUSED gpointer data)
{
s_bNameOwned = TRUE;
s_bNameAcquiredOrLost = TRUE;
}
static void _on_name_lost (G_GNUC_UNUSED GDBusConnection* pConn, G_GNUC_UNUSED const gchar* cName, G_GNUC_UNUSED gpointer data)
{
s_bNameOwned = FALSE;
s_bNameAcquiredOrLost = TRUE;
}
gboolean cairo_dock_dbus_own_name (void)
{
//\____________ Register the service name (the service name is registerd once by the first gldi instance).
g_bus_own_name (G_BUS_TYPE_SESSION, s_cBusName, G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE, _on_bus_acquired, // bus acquired handler
_on_name_acquired, _on_name_lost, NULL, NULL);
// Wait until we have acquired the bus. This is necessary as we want to know whether we succeeded.
GMainContext *pContext = g_main_context_default ();
do g_main_context_iteration (pContext, TRUE);
while (!s_bNameAcquiredOrLost);
return s_bNameOwned;
}
/***********************************************************************
* Deprecated interface -- to be removed. */
static DBusGConnection *s_pSessionConnexion = NULL;
static DBusGConnection *s_pSystemConnexion = NULL;
@@ -101,12 +162,6 @@ gboolean cairo_dock_register_service_name (const gchar *cServiceName)
return TRUE;
}
gboolean cairo_dock_dbus_is_enabled (void)
{
return (cairo_dock_get_session_connection () != NULL && cairo_dock_get_system_connection () != NULL);
}
static void on_name_owner_changed (G_GNUC_UNUSED DBusGProxy *pProxy, const gchar *cName, G_GNUC_UNUSED const gchar *cPrevOwner, const gchar *cNewOwner, G_GNUC_UNUSED gpointer data)
{
//g_print ("%s (%s)\n", __func__, cName);

View File

@@ -22,6 +22,7 @@
#define __CAIRO_DOCK_DBUS__
#include <glib.h>
#include <gio/gio.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-bindings.h>
G_BEGIN_DECLS
@@ -32,6 +33,29 @@ G_BEGIN_DECLS
*/
/***********************************************************************
* New interface */
/** Say if the bus is available or not.
*@return TRUE if the connection to the bus has been established.
*/
gboolean cairo_dock_dbus_is_enabled (void);
/** Get a connection to the session bus (if connected).
*@return main DBusConnection to the session bus or NULL if unavailable
*/
GDBusConnection *cairo_dock_dbus_get_session_bus (void);
/** Get the DBus well-known name under which Cairo-Dock is registered
* on the session bus. By default, it is "org.CairoDock.cairodock".
*@return the DBus name registered or NULL if we do not own a DBus name
*/
const gchar *cairo_dock_dbus_get_owned_name (void);
/** Deprecated interface -- to be removed. */
typedef void (*CairoDockDbusNameOwnerChangedFunc) (const gchar *cName, gboolean bOwned, gpointer data);
/** Get the connection to the 'session' Bus.
@@ -50,10 +74,6 @@ DBusGProxy *cairo_dock_get_main_system_proxy (void);
*/
gboolean cairo_dock_register_service_name (const gchar *cServiceName);
/** Say if the bus is available or not.
*@return TRUE if the connection to the bus has been established.
*/
gboolean cairo_dock_dbus_is_enabled (void);
void cairo_dock_watch_dbus_name_owner (const char *cName, CairoDockDbusNameOwnerChangedFunc pCallback, gpointer data);