From 79f4b9c0d6f8cef8664034ac8313725d178e19e5 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Thu, 6 Jul 2017 10:09:07 +0200 Subject: [PATCH] tools: add ratbagctl.devel to launch both ratbagd and ratbagctl When developing with ratbagd, we need to test against master, not the system-wide installed daemon. Allow to name space ratbagd with the creation of a new (not installed) rabtagd.devel daemon, that is tagged with the git sha of the ratbagd folder. Handle this namespace in ratbagctl by looking at an environment variable RATBAGCTL_DEVEL. Then provide a python script that launches both rabtagd.devel and ratbagctl with the correct environment variable set so that they can talk to each other. Note that we blindly overwrite the dbus .conf file as nobody else than developers should have one. Note: the tagging of ratbagd.devel (like the meson bit) won't run unless a rebuild is triggered. So we don't necessarily always get the right git tag. However, touching ratbagctl.in triggers a rebuild, so we should be OK-ish. Fixes #206 Signed-off-by: Benjamin Tissoires --- dbus/org.freedesktop.ratbag_devel1.conf.in | 26 +++++++++++ meson.build | 27 +++++++++++ ratbagd/ratbagd-button.c | 6 +-- ratbagd/ratbagd-device.c | 8 ++-- ratbagd/ratbagd-led.c | 2 +- ratbagd/ratbagd-profile.c | 24 +++++----- ratbagd/ratbagd-resolution.c | 6 +-- ratbagd/ratbagd.c | 22 ++++----- ratbagd/ratbagd.h | 7 +++ tools/ratbag-git-sha.sh | 10 ++++ tools/ratbagctl.devel.in | 53 ++++++++++++++++++++++ tools/ratbagctl.in | 14 ++++-- 12 files changed, 168 insertions(+), 37 deletions(-) create mode 100644 dbus/org.freedesktop.ratbag_devel1.conf.in create mode 100755 tools/ratbag-git-sha.sh create mode 100755 tools/ratbagctl.devel.in diff --git a/dbus/org.freedesktop.ratbag_devel1.conf.in b/dbus/org.freedesktop.ratbag_devel1.conf.in new file mode 100644 index 00000000..1d8550dd --- /dev/null +++ b/dbus/org.freedesktop.ratbag_devel1.conf.in @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/meson.build b/meson.build index c6021758..a3a676f5 100644 --- a/meson.build +++ b/meson.build @@ -5,6 +5,10 @@ project('libratbag', 'c', 'cpp', libratbag_version = meson.project_version().split('.') +git = find_program('git') +r = run_command('tools/ratbag-git-sha.sh') +ratbagd_sha = r.stdout().strip() + # We use libtool-version numbers because it's easier to understand. # Before making a release, the libratbag_so_* and liblur_so_* # numbers should be modified. The components are of the form C:R:A. @@ -440,6 +444,22 @@ executable('ratbagd', install_man('ratbagd/ratbagd.8') +#### ratbagd_devel #### + +executable('ratbagd.devel', + src_ratbagd, + dependencies : deps_ratbagd, + include_directories : include_directories('src'), + install : false, + c_args : '-DRATBAG_DBUS_INTERFACE="ratbag_devel1_@0@"'.format(ratbagd_sha), +) + +config_ratbagd_devel = configuration_data() +config_ratbagd_devel.set('ratbagd_sha', ratbagd_sha) +configure_file(input : 'dbus/org.freedesktop.ratbag_devel1.conf.in', + output : 'org.freedesktop.ratbag_devel1.conf', + configuration : config_ratbagd_devel) + #### unit file #### unitdir = get_option('systemd-unit-dir') if unitdir == '' @@ -492,6 +512,13 @@ configure_file(input : 'tools/ratbagctl.in', install_dir : get_option('bindir')) install_man('tools/ratbagctl.1') +config_ratbagctl_devel = configuration_data() +config_ratbagctl_devel.set('ratbagd_sha', ratbagd_sha) +config_ratbagctl_devel.set_quoted('MESON_BUILD_ROOT', meson.build_root()) +configure_file(input : 'tools/ratbagctl.devel.in', + output : 'ratbagctl.devel', + configuration : config_ratbagctl_devel) + #### output files #### configure_file(output: 'config.h', install: false, configuration: config_h) diff --git a/ratbagd/ratbagd-button.c b/ratbagd/ratbagd-button.c index 4956fc42..d6595520 100644 --- a/ratbagd/ratbagd-button.c +++ b/ratbagd/ratbagd-button.c @@ -175,7 +175,7 @@ static int ratbagd_button_set_button(sd_bus_message *m, sd_bus *bus = sd_bus_message_get_bus(m); sd_bus_emit_properties_changed(bus, button->path, - "org.freedesktop.ratbag1.Button", + RATBAGD_NAME_ROOT ".Button", "ButtonMapping", NULL); } @@ -316,7 +316,7 @@ static int ratbagd_button_set_special(sd_bus_message *m, sd_bus *bus = sd_bus_message_get_bus(m); sd_bus_emit_properties_changed(bus, button->path, - "org.freedesktop.ratbag1.Button", + RATBAGD_NAME_ROOT ".Button", "SpecialMapping", NULL); } @@ -536,7 +536,7 @@ int ratbagd_button_new(struct ratbagd_button **out, sprintf(profile_buffer, "p%u", ratbagd_profile_get_index(profile)); sprintf(button_buffer, "b%u", index); r = sd_bus_path_encode_many(&button->path, - "/org/freedesktop/ratbag1/button/%/%/%", + RATBAGD_OBJ_ROOT "/button/%/%/%", ratbagd_device_get_name(device), profile_buffer, button_buffer); diff --git a/ratbagd/ratbagd-device.c b/ratbagd/ratbagd-device.c index 5b3035ec..ea5f758e 100644 --- a/ratbagd/ratbagd-device.c +++ b/ratbagd/ratbagd-device.c @@ -67,7 +67,7 @@ static int ratbagd_device_find_profile(sd_bus *bus, int r; r = sd_bus_path_decode_many(path, - "/org/freedesktop/ratbag1/profile/%/p%", + RATBAGD_OBJ_ROOT "/profile/%/p%", NULL, &name); if (r <= 0) @@ -362,7 +362,7 @@ int ratbagd_device_new(struct ratbagd_device **out, if (!device->name) return -ENOMEM; - r = sd_bus_path_encode("/org/freedesktop/ratbag1/device", + r = sd_bus_path_encode(RATBAGD_OBJ_ROOT "/device", device->name, &device->path); if (r < 0) @@ -485,13 +485,13 @@ void ratbagd_device_link(struct ratbagd_device *device) /* register profile interfaces */ r = sd_bus_path_encode_many(&prefix, - "/org/freedesktop/ratbag1/profile/%", + RATBAGD_OBJ_ROOT "/profile/%", device->name); if (r >= 0) { r = sd_bus_add_fallback_vtable(device->ctx->bus, &device->profile_vtable_slot, prefix, - "org.freedesktop.ratbag1.Profile", + RATBAGD_NAME_ROOT ".Profile", ratbagd_profile_vtable, ratbagd_device_find_profile, device); diff --git a/ratbagd/ratbagd-led.c b/ratbagd/ratbagd-led.c index c0c958e6..fc984171 100644 --- a/ratbagd/ratbagd-led.c +++ b/ratbagd/ratbagd-led.c @@ -240,7 +240,7 @@ int ratbagd_led_new(struct ratbagd_led **out, sprintf(profile_buffer, "p%u", ratbagd_profile_get_index(profile)); sprintf(led_buffer, "b%u", index); r = sd_bus_path_encode_many(&led->path, - "/org/freedesktop/ratbag1/led/%/%/%", + RATBAGD_OBJ_ROOT "/led/%/%/%", ratbagd_device_get_name(device), profile_buffer, led_buffer); diff --git a/ratbagd/ratbagd-profile.c b/ratbagd/ratbagd-profile.c index 510cc9a5..0cfdb9b3 100644 --- a/ratbagd/ratbagd-profile.c +++ b/ratbagd/ratbagd-profile.c @@ -71,7 +71,7 @@ static int ratbagd_profile_find_resolution(sd_bus *bus, int r; r = sd_bus_path_decode_many(path, - "/org/freedesktop/ratbag1/resolution/%/p%/r%", + RATBAGD_OBJ_ROOT "/resolution/%/p%/r%", NULL, NULL, &name); @@ -252,7 +252,7 @@ static int ratbagd_profile_find_button(sd_bus *bus, int r; r = sd_bus_path_decode_many(path, - "/org/freedesktop/ratbag1/button/%/p%/b%", + RATBAGD_OBJ_ROOT "/button/%/p%/b%", NULL, NULL, &name); @@ -283,7 +283,7 @@ static int ratbagd_profile_find_led(sd_bus *bus, int r; r = sd_bus_path_decode_many(path, - "/org/freedesktop/ratbag1/led/%/p%/b%", + RATBAGD_OBJ_ROOT "/led/%/p%/b%", NULL, NULL, &name); @@ -313,8 +313,8 @@ static int ratbagd_profile_set_active(sd_bus_message *m, return r; (void) sd_bus_emit_signal(sd_bus_message_get_bus(m), - "/org/freedesktop/ratbag1", - "org.freedesktop.ratbag1.Profile", + RATBAGD_OBJ_ROOT, + RATBAGD_NAME_ROOT ".Profile", "ActiveProfileChanged", "u", profile->index); @@ -384,7 +384,7 @@ int ratbagd_profile_new(struct ratbagd_profile **out, sprintf(index_buffer, "p%u", index); r = sd_bus_path_encode_many(&profile->path, - "/org/freedesktop/ratbag1/profile/%/%", + RATBAGD_OBJ_ROOT "/profile/%/%", ratbagd_device_get_name(device), index_buffer); if (r < 0) @@ -559,7 +559,7 @@ int ratbagd_profile_register_resolutions(struct sd_bus *bus, /* register resolution interfaces */ r = sd_bus_path_encode_many(&prefix, - "/org/freedesktop/ratbag1/resolution/%/%", + RATBAGD_OBJ_ROOT "/resolution/%/%", ratbagd_device_get_name(device), index_buffer); @@ -567,7 +567,7 @@ int ratbagd_profile_register_resolutions(struct sd_bus *bus, r = sd_bus_add_fallback_vtable(bus, &profile->resolution_vtable_slot, prefix, - "org.freedesktop.ratbag1.Resolution", + RATBAGD_NAME_ROOT ".Resolution", ratbagd_resolution_vtable, ratbagd_profile_find_resolution, profile); @@ -635,7 +635,7 @@ int ratbagd_profile_register_buttons(struct sd_bus *bus, /* register button interfaces */ r = sd_bus_path_encode_many(&prefix, - "/org/freedesktop/ratbag1/button/%/%", + RATBAGD_OBJ_ROOT "/button/%/%", ratbagd_device_get_name(device), index_buffer); @@ -643,7 +643,7 @@ int ratbagd_profile_register_buttons(struct sd_bus *bus, r = sd_bus_add_fallback_vtable(bus, &profile->button_vtable_slot, prefix, - "org.freedesktop.ratbag1.Button", + RATBAGD_NAME_ROOT ".Button", ratbagd_button_vtable, ratbagd_profile_find_button, profile); @@ -711,7 +711,7 @@ int ratbagd_profile_register_leds(struct sd_bus *bus, /* register led interfaces */ r = sd_bus_path_encode_many(&prefix, - "/org/freedesktop/ratbag1/led/%/%", + RATBAGD_OBJ_ROOT "/led/%/%", ratbagd_device_get_name(device), index_buffer); @@ -719,7 +719,7 @@ int ratbagd_profile_register_leds(struct sd_bus *bus, r = sd_bus_add_fallback_vtable(bus, &profile->led_vtable_slot, prefix, - "org.freedesktop.ratbag1.Led", + RATBAGD_NAME_ROOT ".Led", ratbagd_led_vtable, ratbagd_profile_find_led, profile); diff --git a/ratbagd/ratbagd-resolution.c b/ratbagd/ratbagd-resolution.c index 877e5c84..20b584c2 100644 --- a/ratbagd/ratbagd-resolution.c +++ b/ratbagd/ratbagd-resolution.c @@ -108,8 +108,8 @@ static int ratbagd_resolution_set_default(sd_bus_message *m, r = ratbag_resolution_set_default(resolution->lib_resolution); (void) sd_bus_emit_signal(sd_bus_message_get_bus(m), - "/org/freedesktop/ratbag1", - "org.freedesktop.ratbag1.Resolution", + RATBAGD_OBJ_ROOT, + RATBAGD_NAME_ROOT ".Resolution", "DefaultResolutionChanged", "u", resolution->index); @@ -198,7 +198,7 @@ int ratbagd_resolution_new(struct ratbagd_resolution **out, sprintf(profile_buffer, "p%u", ratbagd_profile_get_index(profile)); sprintf(resolution_buffer, "r%u", index); r = sd_bus_path_encode_many(&resolution->path, - "/org/freedesktop/ratbag1/resolution/%/%/%", + RATBAGD_OBJ_ROOT "/resolution/%/%/%", ratbagd_device_get_name(device), profile_buffer, resolution_buffer); diff --git a/ratbagd/ratbagd.c b/ratbagd/ratbagd.c index 112b89ab..ab0e1c27 100644 --- a/ratbagd/ratbagd.c +++ b/ratbagd/ratbagd.c @@ -72,7 +72,7 @@ static int ratbagd_find_device(sd_bus *bus, int r; r = sd_bus_path_decode_many(path, - "/org/freedesktop/ratbag1/device/%", + RATBAGD_OBJ_ROOT "/device/%", &name); if (r <= 0) return r; @@ -181,8 +181,8 @@ static void ratbagd_process_device(struct ratbagd *ctx, /* device was removed, unlink it and destroy our context */ if (device) { (void) sd_bus_emit_signal(ctx->bus, - "/org/freedesktop/ratbag1", - "org.freedesktop.ratbag1.Manager", + RATBAGD_OBJ_ROOT, + RATBAGD_NAME_ROOT ".Manager", "DeviceRemoved", "o", ratbagd_device_get_path(device)); @@ -213,8 +213,8 @@ static void ratbagd_process_device(struct ratbagd *ctx, ratbagd_device_link(device); (void) sd_bus_emit_signal(ctx->bus, - "/org/freedesktop/ratbag1", - "org.freedesktop.ratbag1.Manager", + RATBAGD_OBJ_ROOT, + RATBAGD_NAME_ROOT ".Manager", "DeviceNew", "o", ratbagd_device_get_path(device)); @@ -349,8 +349,8 @@ static int ratbagd_new(struct ratbagd **out) r = sd_bus_add_object_vtable(ctx->bus, NULL, - "/org/freedesktop/ratbag1", - "org.freedesktop.ratbag1.Manager", + RATBAGD_OBJ_ROOT, + RATBAGD_NAME_ROOT ".Manager", ratbagd_vtable, ctx); if (r < 0) @@ -358,8 +358,8 @@ static int ratbagd_new(struct ratbagd **out) r = sd_bus_add_fallback_vtable(ctx->bus, NULL, - "/org/freedesktop/ratbag1/device", - "org.freedesktop.ratbag1.Device", + RATBAGD_OBJ_ROOT "/device", + RATBAGD_NAME_ROOT ".Device", ratbagd_device_vtable, ratbagd_find_device, ctx); @@ -368,13 +368,13 @@ static int ratbagd_new(struct ratbagd **out) r = sd_bus_add_node_enumerator(ctx->bus, NULL, - "/org/freedesktop/ratbag1/device", + RATBAGD_OBJ_ROOT "/device", ratbagd_list_devices, ctx); if (r < 0) return r; - r = sd_bus_request_name(ctx->bus, "org.freedesktop.ratbag1", 0); + r = sd_bus_request_name(ctx->bus, RATBAGD_NAME_ROOT, 0); if (r < 0) return r; diff --git a/ratbagd/ratbagd.h b/ratbagd/ratbagd.h index 7a70f2af..23458239 100644 --- a/ratbagd/ratbagd.h +++ b/ratbagd/ratbagd.h @@ -38,6 +38,13 @@ #include "shared-macro.h" #include "shared-rbtree.h" +#ifndef RATBAG_DBUS_INTERFACE +#define RATBAG_DBUS_INTERFACE "ratbag1" +#endif + +#define RATBAGD_OBJ_ROOT "/org/freedesktop/" RATBAG_DBUS_INTERFACE +#define RATBAGD_NAME_ROOT "org.freedesktop." RATBAG_DBUS_INTERFACE + struct ratbagd; struct ratbagd_device; struct ratbagd_profile; diff --git a/tools/ratbag-git-sha.sh b/tools/ratbag-git-sha.sh new file mode 100755 index 00000000..f762db30 --- /dev/null +++ b/tools/ratbag-git-sha.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +GIT_DIR="$MESON_SOURCE_ROOT/.git" +export GIT_DIR + +git log --format="format:%h" -1 2>/dev/null + +if test $? -ne 0; then + echo "unknown" +fi diff --git a/tools/ratbagctl.devel.in b/tools/ratbagctl.devel.in new file mode 100755 index 00000000..d638ca74 --- /dev/null +++ b/tools/ratbagctl.devel.in @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +# +# vim: set expandtab shiftwidth=4 tabstop=4: +# +# This file is part of libratbag. +# +# Copyright 2017 Red Hat, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice (including the next +# paragraph) shall be included in all copies or substantial portions of the +# Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +import os +import shutil +import subprocess +import sys +import time + +if not os.geteuid() == 0: + sys.exit('Script must be run as root') + +SCRIPT_DIR = @MESON_BUILD_ROOT@ + +# first copy the policy for the ratbagd daemon to be allowed to run +shutil.copy(os.path.join(SCRIPT_DIR, 'org.freedesktop.ratbag_devel1.conf'), + '/etc/dbus-1/system.d/') + +ratbagd = subprocess.Popen([os.path.join(SCRIPT_DIR, "ratbagd.devel")]) + +try: + ratbagctl = subprocess.Popen(" ".join([os.path.join(SCRIPT_DIR, "ratbagctl"), *sys.argv[1:]]), + shell=True, + env={"RATBAGCTL_DEVEL": "org.freedesktop.ratbag_devel1_@ratbagd_sha@"}) + + ratbagctl.wait() +except KeyboardInterrupt: + pass +ratbagd.terminate() diff --git a/tools/ratbagctl.in b/tools/ratbagctl.in index 68815d77..4d24ed28 100755 --- a/tools/ratbagctl.in +++ b/tools/ratbagctl.in @@ -27,6 +27,7 @@ from gi.repository import Gio, GLib, GObject +import os import sys import argparse @@ -46,13 +47,20 @@ class _RatbagdDBus(GObject.GObject): if _RatbagdDBus._dbus is None: raise RatbagdDBusUnavailable() + ratbag1 = "org.freedesktop.ratbag1" + if os.environ.get('RATBAGCTL_DEVEL'): + ratbag1 = os.environ['RATBAGCTL_DEVEL'] + + if object_path is None: + object_path = "/" + ratbag1.replace('.', '/') + try: self._proxy = Gio.DBusProxy.new_sync(_RatbagdDBus._dbus, Gio.DBusProxyFlags.NONE, None, - "org.freedesktop.ratbag1", + ratbag1, object_path, - "org.freedesktop.ratbag1.{}".format(interface), + "{}.{}".format(ratbag1, interface), None) except GLib.GError: raise RatbagdDBusUnavailable() @@ -91,7 +99,7 @@ class Ratbagd(_RatbagdDBus): } def __init__(self): - _RatbagdDBus.__init__(self, "Manager", "/org/freedesktop/ratbag1") + _RatbagdDBus.__init__(self, "Manager", None) self._proxy.connect("g-signal", self._on_g_signal) self._devices = [] result = self.dbus_property("Devices")