diff --git a/.gitignore b/.gitignore index 80f294d..f07a5a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ power-profiles-daemon -data/net.hadess.PowerProfiles.conf __pycache__ .vscode diff --git a/README.md b/README.md index a705bd9..5f82cc5 100644 --- a/README.md +++ b/README.md @@ -84,8 +84,12 @@ reboot in `/var/lib/power-profiles-daemon/state.ini`. Those commands are also available through the D-Bus interface: ```sh -gdbus introspect --system --dest net.hadess.PowerProfiles --object-path /net/hadess/PowerProfiles -gdbus call --system --dest net.hadess.PowerProfiles --object-path /net/hadess/PowerProfiles --method org.freedesktop.DBus.Properties.Set 'net.hadess.PowerProfiles' 'ActiveProfile' "<'power-saver'>" +gdbus introspect --system --dest org.freedesktop.UPower.PowerProfiles \ + --object-path /org/freedesktop/UPower/PowerProfiles +gdbus call --system --dest org.freedesktop.UPower.PowerProfiles \ + --object-path /org/freedesktop/UPower/PowerProfiles \ + --method org.freedesktop.DBus.Properties.Set 'org.freedesktop.UPower.PowerProfiles' \ + 'ActiveProfile' "<'power-saver'>" ``` If that doesn't work, please file an issue, attach the output of: diff --git a/data/meson.build b/data/meson.build index 0434843..a42e80f 100644 --- a/data/meson.build +++ b/data/meson.build @@ -1,24 +1,32 @@ -data_conf = configuration_data() -data_conf.set('libexecdir', libexecdir) - configure_file( input: 'power-profiles-daemon.service.in', output: 'power-profiles-daemon.service', - configuration: data_conf, + configuration: { + 'libexecdir': libexecdir, + }, install_dir: systemd_system_unit_dir, ) -configure_file( - input: 'net.hadess.PowerProfiles.conf.in', - output: 'net.hadess.PowerProfiles.conf', - configuration: data_conf, - install_dir: dbusconfdir -) +foreach name, _: bus_names + config = { + 'dbus_name': name, + 'dbus_iface': name, + } -install_data( - 'net.hadess.PowerProfiles.service', - install_dir: dbusservicedir -) + configure_file( + input: 'power-profiles-daemon.dbus.conf.in', + output: name + '.conf', + configuration: config, + install_dir: dbusconfdir + ) + + configure_file( + input: 'power-profiles-daemon.dbus.service.in', + output: name + '.service', + configuration: config, + install_dir: dbusservicedir + ) +endforeach polkit_policy = 'power-profiles-daemon.policy' if xmllint.found() diff --git a/data/net.hadess.PowerProfiles.conf.in b/data/net.hadess.PowerProfiles.conf.in deleted file mode 100644 index a01d2d4..0000000 --- a/data/net.hadess.PowerProfiles.conf.in +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/data/power-profiles-daemon.dbus.conf.in b/data/power-profiles-daemon.dbus.conf.in new file mode 100644 index 0000000..844277f --- /dev/null +++ b/data/power-profiles-daemon.dbus.conf.in @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + diff --git a/data/net.hadess.PowerProfiles.service b/data/power-profiles-daemon.dbus.service.in similarity index 91% rename from data/net.hadess.PowerProfiles.service rename to data/power-profiles-daemon.dbus.service.in index 724f631..5baefa1 100644 --- a/data/net.hadess.PowerProfiles.service +++ b/data/power-profiles-daemon.dbus.service.in @@ -5,7 +5,7 @@ # the Free Software Foundation. [D-BUS Service] -Name=net.hadess.PowerProfiles +Name=@dbus_name@ Exec=/bin/false User=root SystemdService=power-profiles-daemon.service diff --git a/data/power-profiles-daemon.service.in b/data/power-profiles-daemon.service.in index dcd3503..17ec1d2 100644 --- a/data/power-profiles-daemon.service.in +++ b/data/power-profiles-daemon.service.in @@ -5,7 +5,7 @@ Before=multi-user.target display-manager.target [Service] Type=dbus -BusName=net.hadess.PowerProfiles +BusName=org.freedesktop.UPower.PowerProfiles ExecStart=@libexecdir@/power-profiles-daemon Restart=on-failure # This always corresponds to /var/lib/power-profiles-daemon diff --git a/docs/meson.build b/docs/meson.build index 1531d0c..01e8909 100644 --- a/docs/meson.build +++ b/docs/meson.build @@ -11,8 +11,8 @@ content_files += configure_file( content_files += gnome.gdbus_codegen( meson.project_name(), - sources: meson.source_root() / 'src' / 'net.hadess.PowerProfiles.xml', - interface_prefix: 'net.hadess', + sources: dbus_xml['org.freedesktop.UPower.PowerProfiles'], + interface_prefix: 'org.freedesktop.UPower', namespace: 'PowerProfiles', docbook: 'docs', build_by_default: true, diff --git a/docs/power-profiles-daemon-docs.xml b/docs/power-profiles-daemon-docs.xml index f2801b5..c3c7119 100644 --- a/docs/power-profiles-daemon-docs.xml +++ b/docs/power-profiles-daemon-docs.xml @@ -63,7 +63,7 @@ Power Profiles daemon. - + diff --git a/meson.build b/meson.build index 95c24f8..115e57d 100644 --- a/meson.build +++ b/meson.build @@ -54,6 +54,11 @@ if get_option('pylint') endif xmllint = find_program('xmllint', required: false) +bus_names = { + 'org.freedesktop.UPower.PowerProfiles': '/org/freedesktop/UPower/PowerProfiles', + 'net.hadess.PowerProfiles': '/net/hadess/PowerProfiles', +} + subdir('src') subdir('data') diff --git a/src/meson.build b/src/meson.build index f58a94e..870beba 100644 --- a/src/meson.build +++ b/src/meson.build @@ -8,11 +8,46 @@ config_h_files = configure_file( configuration: config_h ) +dbus_xml = {} +dbus_xml_sources = [] +resources_contents = [] + +foreach name, path: bus_names + config = { + 'dbus_name': name, + 'dbus_iface': name, + 'dbus_path': path, + } + + xml_source = configure_file( + input: 'power-profiles-daemon.dbus.xml.in', + output: name + '.xml', + configuration: config, + ) + dbus_xml_sources += xml_source + dbus_xml += {name: xml_source} + + resources_contents += '@0@.xml'.format(name) +endforeach + +resources_xml = configure_file( + input: 'power-profiles-daemon.gresource.xml.in', + output: '@BASENAME@', + configuration: { + 'prefix': bus_names['org.freedesktop.UPower.PowerProfiles'], + 'contents': '\n'.join(resources_contents), + }, +) + resources = gnome.compile_resources( - 'power-profiles-daemon-resources', 'power-profiles-daemon.gresource.xml', - c_name: 'power_profiles_daemon', - source_dir: '.', - export: true + 'power-profiles-daemon-resources', resources_xml, + c_name: 'power_profiles_daemon', + dependencies: dbus_xml_sources, + source_dir: [ + meson.current_source_dir(), + meson.current_build_dir(), + ], + export: true ) sources = [ diff --git a/src/org.freedesktop.UPower.PowerProfiles.xml b/src/org.freedesktop.UPower.PowerProfiles.xml new file mode 100644 index 0000000..49535b8 --- /dev/null +++ b/src/org.freedesktop.UPower.PowerProfiles.xml @@ -0,0 +1,140 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/power-profiles-daemon.c b/src/power-profiles-daemon.c index 1e8fa85..6206b59 100644 --- a/src/power-profiles-daemon.c +++ b/src/power-profiles-daemon.c @@ -23,12 +23,18 @@ #include "ppd-action.h" #include "ppd-enums.h" -#define POWER_PROFILES_DBUS_NAME "net.hadess.PowerProfiles" -#define POWER_PROFILES_DBUS_PATH "/net/hadess/PowerProfiles" +#define POWER_PROFILES_DBUS_NAME "org.freedesktop.UPower.PowerProfiles" +#define POWER_PROFILES_DBUS_PATH "/org/freedesktop/UPower/PowerProfiles" #define POWER_PROFILES_IFACE_NAME POWER_PROFILES_DBUS_NAME +#define POWER_PROFILES_LEGACY_DBUS_NAME "net.hadess.PowerProfiles" +#define POWER_PROFILES_LEGACY_DBUS_PATH "/net/hadess/PowerProfiles" +#define POWER_PROFILES_LEGACY_IFACE_NAME POWER_PROFILES_LEGACY_DBUS_NAME + #define POWER_PROFILES_POLICY_NAMESPACE "org.freedesktop.UPower.PowerProfiles" +#define POWER_PROFILES_RESOURCES_PATH "/org/freedesktop/UPower/PowerProfiles" + #ifndef POLKIT_HAS_AUTOPOINTERS /* FIXME: Remove this once we're fine to depend on polkit 0.114 */ G_DEFINE_AUTOPTR_CLEANUP_FUNC (PolkitAuthorizationResult, g_object_unref) @@ -37,9 +43,9 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC (PolkitSubject, g_object_unref) typedef struct { GMainLoop *main_loop; - GDBusNodeInfo *introspection_data; GDBusConnection *connection; guint name_id; + guint legacy_name_id; gboolean was_started; int ret; @@ -837,7 +843,8 @@ handle_method_call (GDBusConnection *connection, PpdApp *data = user_data; g_assert (data->connection); - if (g_strcmp0 (interface_name, POWER_PROFILES_IFACE_NAME) != 0) { + if (!g_str_equal (interface_name, POWER_PROFILES_IFACE_NAME) && + !g_str_equal (interface_name, POWER_PROFILES_LEGACY_IFACE_NAME)) { g_dbus_method_invocation_return_error (invocation,G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface %s", interface_name); return; @@ -870,16 +877,34 @@ static const GDBusInterfaceVTable interface_vtable = handle_set_property }; +typedef struct { + PpdApp *app; + GBusNameOwnerFlags flags; + GDBusInterfaceInfo *interface; + GDBusInterfaceInfo *legacy_interface; +} PpdBusOwnData; + +static void +ppd_bus_own_data_free (PpdBusOwnData *data) +{ + g_clear_pointer (&data->interface, g_dbus_interface_info_unref); + g_clear_pointer (&data->legacy_interface, g_dbus_interface_info_unref); + g_free (data); +} + static void name_lost_handler (GDBusConnection *connection, const gchar *name, gpointer user_data) { - PpdApp *data = user_data; + PpdBusOwnData *data = user_data; + PpdApp *app = data->app; + g_debug ("power-profiles-daemon is already running, or it cannot own its D-Bus name. Verify installation."); - if (!data->was_started) - data->ret = 1; - g_main_loop_quit (data->main_loop); + if (!app->was_started) + app->ret = 1; + + g_main_loop_quit (app->main_loop); } static void @@ -887,17 +912,33 @@ bus_acquired_handler (GDBusConnection *connection, const gchar *name, gpointer user_data) { - PpdApp *data = user_data; + PpdBusOwnData *data = user_data; g_dbus_connection_register_object (connection, POWER_PROFILES_DBUS_PATH, - data->introspection_data->interfaces[0], + data->interface, &interface_vtable, - data, + data->app, NULL, NULL); - data->connection = g_object_ref (connection); + g_dbus_connection_register_object (connection, + POWER_PROFILES_LEGACY_DBUS_PATH, + data->legacy_interface, + &interface_vtable, + data->app, + NULL, + NULL); + + data->app->legacy_name_id = g_bus_own_name_on_connection (connection, + POWER_PROFILES_LEGACY_DBUS_NAME, + data->flags, + NULL, + name_lost_handler, + data, + NULL); + + data->app->connection = g_object_ref (connection); } static gboolean @@ -1067,37 +1108,63 @@ name_acquired_handler (GDBusConnection *connection, const gchar *name, gpointer user_data) { - PpdApp *data = user_data; + PpdBusOwnData *data = user_data; - start_profile_drivers (data); + start_profile_drivers (data->app); } static gboolean -setup_dbus (PpdApp *data, - gboolean replace) +setup_dbus (PpdApp *data, + gboolean replace, + GError **error) { - GBytes *bytes; - GBusNameOwnerFlags flags; + g_autoptr(GBytes) iface_data = NULL; + g_autoptr(GBytes) legacy_iface_data = NULL; + g_autoptr(GDBusNodeInfo) introspection_data = NULL; + g_autoptr(GDBusNodeInfo) legacy_introspection_data = NULL; + PpdBusOwnData *own_data; - bytes = g_resources_lookup_data ("/net/hadess/PowerProfiles/net.hadess.PowerProfiles.xml", - G_RESOURCE_LOOKUP_FLAGS_NONE, - NULL); - data->introspection_data = g_dbus_node_info_new_for_xml (g_bytes_get_data (bytes, NULL), NULL); - g_bytes_unref (bytes); - g_assert (data->introspection_data != NULL); + iface_data = g_resources_lookup_data (POWER_PROFILES_RESOURCES_PATH "/" + POWER_PROFILES_DBUS_NAME ".xml", + G_RESOURCE_LOOKUP_FLAGS_NONE, + error); + if (!iface_data) + return FALSE; - flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT; + legacy_iface_data = g_resources_lookup_data (POWER_PROFILES_RESOURCES_PATH "/" + POWER_PROFILES_LEGACY_DBUS_NAME ".xml", + G_RESOURCE_LOOKUP_FLAGS_NONE, + error); + if (!legacy_iface_data) + return FALSE; + + introspection_data = g_dbus_node_info_new_for_xml (g_bytes_get_data (iface_data, NULL), + error); + if (!introspection_data) + return FALSE; + + legacy_introspection_data = g_dbus_node_info_new_for_xml (g_bytes_get_data (legacy_iface_data, NULL), + error); + if (!legacy_introspection_data) + return FALSE; + + own_data = g_new0 (PpdBusOwnData, 1); + own_data->app = data; + own_data->interface = g_dbus_interface_info_ref (introspection_data->interfaces[0]); + own_data->legacy_interface = g_dbus_interface_info_ref (legacy_introspection_data->interfaces[0]); + + own_data->flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT; if (replace) - flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE; + own_data->flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE; data->name_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, POWER_PROFILES_DBUS_NAME, - flags, + own_data->flags, bus_acquired_handler, name_acquired_handler, name_lost_handler, - data, - NULL); + own_data, + (GDestroyNotify) ppd_bus_own_data_free); return TRUE; } @@ -1108,10 +1175,8 @@ free_app_data (PpdApp *data) if (data == NULL) return; - if (data->name_id != 0) { - g_bus_unown_name (data->name_id); - data->name_id = 0; - } + g_clear_handle_id (&data->name_id, g_bus_unown_name); + g_clear_handle_id (&data->legacy_name_id, g_bus_unown_name); g_clear_pointer (&data->config_path, g_free); g_clear_pointer (&data->config, g_key_file_unref); @@ -1124,7 +1189,6 @@ free_app_data (PpdApp *data) g_clear_object (&data->auth); g_clear_pointer (&data->main_loop, g_main_loop_unref); - g_clear_pointer (&data->introspection_data, g_dbus_node_info_unref); g_clear_object (&data->connection); g_free (data); ppd_app = NULL; @@ -1227,7 +1291,10 @@ int main (int argc, char **argv) ppd_app = data; /* Set up D-Bus */ - setup_dbus (data, replace); + if (!setup_dbus (data, replace, &error)) { + g_error ("Failed to start dbus: %s", error->message); + return 1; + } g_main_loop_run (data->main_loop); ret = data->ret; diff --git a/src/net.hadess.PowerProfiles.xml b/src/power-profiles-daemon.dbus.xml.in similarity index 97% rename from src/net.hadess.PowerProfiles.xml rename to src/power-profiles-daemon.dbus.xml.in index fcfc1d4..0888b15 100644 --- a/src/net.hadess.PowerProfiles.xml +++ b/src/power-profiles-daemon.dbus.xml.in @@ -4,7 +4,7 @@ - +