NetworkManager/src/libnm-client-impl/nm-dbus-helpers.c
Thomas Haller 615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00

110 lines
3.3 KiB
C

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2013 Red Hat, Inc.
*/
#include "libnm-client-impl/nm-default-libnm.h"
#include "nm-dbus-helpers.h"
#include "nm-dbus-interface.h"
GBusType
_nm_dbus_bus_type(void)
{
static volatile int bus_type = G_BUS_TYPE_NONE;
int v;
v = g_atomic_int_get(&bus_type);
if (G_UNLIKELY(v == G_BUS_TYPE_NONE)) {
v = G_BUS_TYPE_SYSTEM;
if (g_getenv("LIBNM_USE_SESSION_BUS"))
v = G_BUS_TYPE_SESSION;
if (!g_atomic_int_compare_and_exchange(&bus_type, G_BUS_TYPE_NONE, v))
v = g_atomic_int_get(&bus_type);
nm_assert(v != G_BUS_TYPE_NONE);
}
return v;
}
/* Binds the properties on a generated server-side GDBus object to the
* corresponding properties on the public object.
*/
void
_nm_dbus_bind_properties(gpointer object, gpointer skeleton)
{
GParamSpec **properties;
guint n_properties;
int i;
properties = g_object_class_list_properties(G_OBJECT_GET_CLASS(skeleton), &n_properties);
for (i = 0; i < n_properties; i++) {
if (g_str_has_prefix(properties[i]->name, "g-"))
continue;
g_object_bind_property(object,
properties[i]->name,
skeleton,
properties[i]->name,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
}
g_free(properties);
}
static char *
signal_name_from_method_name(const char *method_name)
{
GString *signal_name;
const char *p;
signal_name = g_string_new("handle");
for (p = method_name; *p; p++) {
if (g_ascii_isupper(*p))
g_string_append_c(signal_name, '-');
g_string_append_c(signal_name, g_ascii_tolower(*p));
}
return g_string_free(signal_name, FALSE);
}
static void
_nm_dbus_method_meta_marshal(GClosure *closure,
GValue *return_value,
guint n_param_values,
const GValue *param_values,
gpointer invocation_hint,
gpointer marshal_data)
{
closure->marshal(closure,
return_value,
n_param_values,
param_values,
invocation_hint,
((GCClosure *) closure)->callback);
g_value_set_boolean(return_value, TRUE);
}
/* Takes (method_name, handler_func) pairs and connects the handlers to the
* signals on skeleton, with object as the user_data, but swapped so it comes
* first in the argument list, and handling the return value automatically.
*/
void
_nm_dbus_bind_methods(gpointer object, gpointer skeleton, ...)
{
va_list ap;
const char *method_name;
char *signal_name;
GCallback handler;
GClosure *closure;
va_start(ap, skeleton);
while ((method_name = va_arg(ap, const char *)) && (handler = va_arg(ap, GCallback))) {
signal_name = signal_name_from_method_name(method_name);
closure = g_cclosure_new_swap(handler, object, NULL);
g_closure_set_meta_marshal(closure, NULL, _nm_dbus_method_meta_marshal);
g_signal_connect_closure(skeleton, signal_name, closure, FALSE);
g_free(signal_name);
}
va_end(ap);
}