Skip to content

Commit

Permalink
Migrate from dbus-glib to gdbus
Browse files Browse the repository at this point in the history
- mate-session/Makefile.am
- mate-session/test-client-dbus.c
  • Loading branch information
yetist authored and monsta committed Dec 10, 2018
1 parent 413050e commit f1427f0
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 92 deletions.
2 changes: 1 addition & 1 deletion mate-session/Makefile.am
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test_inhibit_SOURCES = test-inhibit.c
test_inhibit_LDADD = $(MATE_SESSION_LIBS) test_inhibit_LDADD = $(MATE_SESSION_LIBS)


test_client_dbus_SOURCES = test-client-dbus.c test_client_dbus_SOURCES = test-client-dbus.c
test_client_dbus_LDADD = $(DBUS_GLIB_LIBS) test_client_dbus_LDADD = $(MATE_SESSION_LIBS)


gsm-marshal.c: gsm-marshal.list gsm-marshal.c: gsm-marshal.list
$(AM_V_GEN)echo "#include \"gsm-marshal.h\"" > $@ && \ $(AM_V_GEN)echo "#include \"gsm-marshal.h\"" > $@ && \
Expand Down
186 changes: 95 additions & 91 deletions mate-session/test-client-dbus.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,24 +27,18 @@
#include <unistd.h> #include <unistd.h>


#include <glib.h> #include <glib.h>
#include <dbus/dbus-glib.h> #include <gio/gio.h>


#define SM_DBUS_NAME "org.gnome.SessionManager" #define SM_DBUS_NAME "org.gnome.SessionManager"
#define SM_DBUS_PATH "/org/gnome/SessionManager" #define SM_DBUS_PATH "/org/gnome/SessionManager"
#define SM_DBUS_INTERFACE "org.gnome.SessionManager" #define SM_DBUS_INTERFACE "org.gnome.SessionManager"


#define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate" #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"


#ifdef __GNUC__ static GDBusConnection *bus_connection = NULL;
#define UNUSED_VARIABLE __attribute__ ((unused)) static GDBusProxy *sm_proxy = NULL;
#else
#define UNUSED_VARIABLE
#endif

static DBusGConnection *bus_connection = NULL;
static DBusGProxy *sm_proxy = NULL;
static char *client_id = NULL; static char *client_id = NULL;
static DBusGProxy *client_proxy = NULL; static GDBusProxy *client_proxy = NULL;
static GMainLoop *main_loop = NULL; static GMainLoop *main_loop = NULL;


static gboolean static gboolean
Expand All @@ -55,7 +49,7 @@ session_manager_connect (void)
GError *error; GError *error;


error = NULL; error = NULL;
bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); bus_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (bus_connection == NULL) { if (bus_connection == NULL) {
g_message ("Failed to connect to the session bus: %s", g_message ("Failed to connect to the session bus: %s",
error->message); error->message);
Expand All @@ -64,129 +58,138 @@ session_manager_connect (void)
} }
} }


sm_proxy = dbus_g_proxy_new_for_name (bus_connection, sm_proxy = g_dbus_proxy_new_sync (bus_connection,
SM_DBUS_NAME, G_DBUS_PROXY_FLAGS_NONE,
SM_DBUS_PATH, NULL,
SM_DBUS_INTERFACE); SM_DBUS_NAME,
SM_DBUS_PATH,
SM_DBUS_INTERFACE,
NULL,
NULL);
return (sm_proxy != NULL); return (sm_proxy != NULL);
} }


static void static void
on_client_query_end_session (DBusGProxy *proxy, on_client_query_end_session (GDBusProxy *proxy,
guint flags, GVariant *parameters)
gpointer data)
{ {
GError *error; GError *error;
gboolean is_ok; gboolean is_ok;
gboolean UNUSED_VARIABLE res; GVariant *res;
guint flags;
const char *reason; const char *reason;


is_ok = FALSE; is_ok = FALSE;
reason = "Unsaved files"; reason = "Unsaved files";
g_variant_get (parameters, "(u)", &flags);


g_debug ("Got query end session signal flags=%u", flags); g_debug ("Got query end session signal flags=%u", flags);


error = NULL; error = NULL;
res = dbus_g_proxy_call (proxy, res = g_dbus_proxy_call_sync (proxy,
"EndSessionResponse", "EndSessionResponse",
&error, g_variant_new ("(bs)",
G_TYPE_BOOLEAN, is_ok, is_ok,
G_TYPE_STRING, reason, reason),
G_TYPE_INVALID, G_DBUS_CALL_FLAGS_NONE,
G_TYPE_INVALID); -1,
NULL,
&error);
if (res == NULL) {
g_warning ("Failed to respond to EndSession: %s", error->message);
g_error_free (error);
} else {
g_variant_unref (res);
}
} }


static void static void
on_client_end_session (DBusGProxy *proxy, on_client_end_session (GVariant *parameters)
guint flags,
gpointer data)
{ {
guint flags;
g_variant_get (parameters, "(u)", &flags);
g_debug ("Got end session signal flags=%u", flags); g_debug ("Got end session signal flags=%u", flags);
} }


static void static void
on_client_cancel_end_session (DBusGProxy *proxy, on_client_cancel_end_session (void)
gpointer data)
{ {
g_debug ("Got end session cancelled signal"); g_debug ("Got end session cancelled signal");
} }


static void static void
on_client_stop (DBusGProxy *proxy, on_client_stop (void)
gpointer data)
{ {
g_debug ("Got client stop signal"); g_debug ("Got client stop signal");
g_main_loop_quit (main_loop); g_main_loop_quit (main_loop);
} }


static void
on_client_dbus_signal (GDBusProxy *proxy,
gchar *sender_name,
gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
if (g_strcmp0 (signal_name, "Stop") == 0) {
on_client_stop ();
} else if (g_strcmp0 (signal_name, "CancelEndSession") == 0) {
on_client_cancel_end_session ();
} else if (g_strcmp0 (signal_name, "EndSession") == 0) {
on_client_end_session (parameters);
} else if (g_strcmp0 (signal_name, "QueryEndSession") == 0) {
on_client_query_end_session (proxy, parameters);
}
}

static gboolean static gboolean
register_client (void) register_client (void)
{ {
GError *error; GError *error;
gboolean res; GVariant *res;
const char *startup_id; const char *startup_id;
const char *app_id; const char *app_id;


startup_id = g_getenv ("DESKTOP_AUTOSTART_ID"); startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
if (!startup_id) {
startup_id = "";
}
app_id = "gedit"; app_id = "gedit";


error = NULL; error = NULL;
res = dbus_g_proxy_call (sm_proxy, res = g_dbus_proxy_call_sync (sm_proxy,
"RegisterClient", "RegisterClient",
&error, g_variant_new ("(ss)",
G_TYPE_STRING, app_id, app_id,
G_TYPE_STRING, startup_id, startup_id),
G_TYPE_INVALID, G_DBUS_CALL_FLAGS_NONE,
DBUS_TYPE_G_OBJECT_PATH, &client_id, -1,
G_TYPE_INVALID); NULL,
if (! res) { &error);
if (res == NULL) {
g_warning ("Failed to register client: %s", error->message); g_warning ("Failed to register client: %s", error->message);
g_error_free (error); g_error_free (error);
return FALSE; return FALSE;
} }


g_variant_get (res, "(o)", &client_id);
g_debug ("Client registered with session manager: %s", client_id); g_debug ("Client registered with session manager: %s", client_id);
client_proxy = dbus_g_proxy_new_for_name (bus_connection,
SM_DBUS_NAME,
client_id,
SM_CLIENT_DBUS_INTERFACE);
dbus_g_proxy_add_signal (client_proxy,
"QueryEndSession",
G_TYPE_UINT,
G_TYPE_INVALID);
dbus_g_proxy_add_signal (client_proxy,
"EndSession",
G_TYPE_UINT,
G_TYPE_INVALID);
dbus_g_proxy_add_signal (client_proxy,
"CancelEndSession",
G_TYPE_UINT,
G_TYPE_INVALID);
dbus_g_proxy_add_signal (client_proxy,
"Stop",
G_TYPE_INVALID);
dbus_g_proxy_connect_signal (client_proxy,
"QueryEndSession",
G_CALLBACK (on_client_query_end_session),
NULL,
NULL);
dbus_g_proxy_connect_signal (client_proxy,
"EndSession",
G_CALLBACK (on_client_end_session),
NULL,
NULL);
dbus_g_proxy_connect_signal (client_proxy,
"CancelEndSession",
G_CALLBACK (on_client_cancel_end_session),
NULL,
NULL);
dbus_g_proxy_connect_signal (client_proxy,
"Stop",
G_CALLBACK (on_client_stop),
NULL,
NULL);


return TRUE; client_proxy = g_dbus_proxy_new_sync (bus_connection,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
SM_DBUS_NAME,
client_id,
SM_CLIENT_DBUS_INTERFACE,
NULL, NULL);
if (client_proxy != NULL) {
g_signal_connect (client_proxy, "g-signal",
G_CALLBACK (on_client_dbus_signal), NULL);
}

g_variant_unref (res);

return (client_proxy != NULL);
} }


static gboolean static gboolean
Expand All @@ -204,16 +207,15 @@ static gboolean
unregister_client (void) unregister_client (void)
{ {
GError *error; GError *error;
gboolean res; GVariant *res;


error = NULL; error = NULL;
res = dbus_g_proxy_call (sm_proxy, res = g_dbus_proxy_call_sync (sm_proxy,
"UnregisterClient", "UnregisterClient",
&error, g_variant_new ("(o)", client_id),
DBUS_TYPE_G_OBJECT_PATH, client_id, G_DBUS_CALL_FLAGS_NONE,
G_TYPE_INVALID, -1, NULL, &error);
G_TYPE_INVALID); if (res == NULL) {
if (! res) {
g_warning ("Failed to unregister client: %s", error->message); g_warning ("Failed to unregister client: %s", error->message);
g_error_free (error); g_error_free (error);
return FALSE; return FALSE;
Expand All @@ -222,6 +224,8 @@ unregister_client (void)
g_free (client_id); g_free (client_id);
client_id = NULL; client_id = NULL;


g_variant_unref (res);

return TRUE; return TRUE;
} }


Expand Down

0 comments on commit f1427f0

Please sign in to comment.