media-session: add logind seat status support

This commit is contained in:
Pauli Virtanen 2021-04-10 16:49:14 +03:00
parent 87aa18edb1
commit 0e80a2497a
6 changed files with 162 additions and 4 deletions

View file

@ -82,6 +82,7 @@ session.modules = {
#alsa-monitor # alsa udev detection
#bluez5 # bluetooth support
#restore-stream # restore stream settings
#logind # systemd-logind seat support
]
with-audio = [
metadata
@ -100,6 +101,7 @@ session.modules = {
with-pulseaudio = [
with-audio
bluez5
logind
restore-stream
streams-follow-default
]

View file

@ -0,0 +1,127 @@
/* PipeWire
*
* Copyright © 2021 Pauli Virtanen
*
* 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.
*/
/*
* Monitor systemd-logind events for changes in session/seat status, and keep session
* manager up-to-date on whether the current session is active.
*/
#include "config.h"
#include <sys/types.h>
#include <unistd.h>
#include <systemd/sd-login.h>
#include <spa/utils/result.h>
#include "pipewire/pipewire.h"
#include "media-session.h"
#define NAME "logind"
struct impl {
struct sm_media_session *session;
struct spa_hook listener;
struct pw_context *context;
sd_login_monitor *monitor;
struct spa_source source;
};
static void update_seat_active(struct impl *impl)
{
char *state;
bool active;
if (sd_uid_get_state(getuid(), &state) < 0)
return;
active = strcmp(state, "active") == 0;
free(state);
sm_media_session_seat_active_changed(impl->session, active);
}
static void monitor_event(struct spa_source *source)
{
struct impl *impl = source->data;
sd_login_monitor_flush(impl->monitor);
update_seat_active(impl);
}
static void session_destroy(void *data)
{
struct impl *impl = data;
spa_hook_remove(&impl->listener);
if (impl->monitor) {
struct pw_loop *main_loop = pw_context_get_main_loop(impl->context);
pw_loop_remove_source(main_loop, &impl->source);
sd_login_monitor_unref(impl->monitor);
impl->monitor = NULL;
}
free(impl);
}
static const struct sm_media_session_events session_events = {
SM_VERSION_MEDIA_SESSION_EVENTS,
.destroy = session_destroy,
};
int sm_logind_start(struct sm_media_session *session)
{
struct impl *impl;
struct pw_loop *main_loop;
int res;
impl = calloc(1, sizeof(struct impl));
if (impl == NULL)
return -errno;
impl->session = session;
impl->context = session->context;
if ((res = sd_login_monitor_new(NULL, &impl->monitor)) < 0)
goto fail;
main_loop = pw_context_get_main_loop(impl->context);
impl->source.data = impl;
impl->source.fd = sd_login_monitor_get_fd(impl->monitor);
impl->source.func = monitor_event;
impl->source.mask = sd_login_monitor_get_events(impl->monitor);
impl->source.rmask = 0;
pw_loop_add_source(main_loop, &impl->source);
sm_media_session_add_listener(impl->session, &impl->listener, &session_events, impl);
update_seat_active(impl);
return 0;
fail:
pw_log_error(NAME ": failed to start systemd logind monitor: %d (%s)", res, spa_strerror(res));
free(impl);
return res;
}

View file

@ -79,6 +79,7 @@
#define sm_media_session_emit_rescan(s,seq) sm_media_session_emit(s, rescan, 0, seq)
#define sm_media_session_emit_shutdown(s) sm_media_session_emit(s, shutdown, 0)
#define sm_media_session_emit_destroy(s) sm_media_session_emit(s, destroy, 0)
#define sm_media_session_emit_seat_active(s,...) sm_media_session_emit(s, seat_active, 0, __VA_ARGS__)
int sm_access_flatpak_start(struct sm_media_session *sess);
int sm_access_portal_start(struct sm_media_session *sess);
@ -93,6 +94,9 @@ int sm_libcamera_monitor_start(struct sm_media_session *sess);
int sm_bluez5_monitor_start(struct sm_media_session *sess);
int sm_alsa_monitor_start(struct sm_media_session *sess);
int sm_suspend_node_start(struct sm_media_session *sess);
#ifdef HAVE_SYSTEMD
int sm_logind_start(struct sm_media_session *sess);
#endif
int sm_policy_node_start(struct sm_media_session *sess);
@ -157,6 +161,7 @@ struct impl {
unsigned int scanning:1;
unsigned int rescan_pending:1;
unsigned int seat_active:1;
};
struct endpoint_link {
@ -2034,6 +2039,16 @@ char *sm_media_session_sanitize_description(char *name, int size, char sub, cons
return name;
}
int sm_media_session_seat_active_changed(struct sm_media_session *sess, bool active)
{
struct impl *impl = SPA_CONTAINER_OF(sess, struct impl, this);
if (active != impl->seat_active) {
impl->seat_active = active;
sm_media_session_emit_seat_active(impl, active);
}
return 0;
}
static void monitor_core_done(void *data, uint32_t id, int seq)
{
struct impl *impl = data;
@ -2335,6 +2350,9 @@ static const struct {
{ "suspend-node", "suspend inactive nodes", sm_suspend_node_start, NULL },
{ "policy-node", "configure and link nodes", sm_policy_node_start, NULL },
{ "pulse-bridge", "accept pulseaudio clients", sm_pulse_bridge_start, NULL },
#ifdef HAVE_SYSTEMD
{ "logind", "systemd-logind seat support", sm_logind_start, NULL },
#endif
};
static bool is_module_enabled(struct impl *impl, const char *val)
@ -2363,7 +2381,7 @@ static void show_help(const char *name, struct impl *impl, const char *config_na
int main(int argc, char *argv[])
{
struct impl impl = { 0, };
struct impl impl = { .seat_active = true };
const struct spa_support *support;
const char *str, *config_name = SESSION_CONF;
bool do_show_help = false;

View file

@ -236,6 +236,8 @@ struct sm_media_session_events {
void (*rescan) (void *data, int seq);
void (*shutdown) (void *data);
void (*destroy) (void *data);
void (*seat_active) (void *data, bool active);
};
struct sm_media_session {
@ -311,6 +313,8 @@ char *sm_media_session_sanitize_name(char *name, int size, char sub,
char *sm_media_session_sanitize_description(char *name, int size, char sub,
const char *fmt, ...) SPA_PRINTF_FUNC(4, 5);
int sm_media_session_seat_active_changed(struct sm_media_session *sess, bool active);
#ifdef __cplusplus
}
#endif

View file

@ -66,6 +66,12 @@ executable('export-spa-device',
)
if not get_option('media-session').disabled() and alsa_dep.found()
sm_logind_src = []
sm_logind_dep = []
if systemd.found() and systemd_dep.found()
sm_logind_src = ['media-session/logind.c']
sm_logind_dep = [systemd_dep]
endif
executable('pipewire-media-session',
'media-session/access-flatpak.c',
'media-session/access-portal.c',
@ -90,9 +96,10 @@ if not get_option('media-session').disabled() and alsa_dep.found()
'media-session/v4l2-endpoint.c',
'media-session/libcamera-monitor.c',
'media-session/suspend-node.c',
sm_logind_src,
c_args : [ '-D_GNU_SOURCE' ],
install: true,
dependencies : [dbus_dep, pipewire_dep, alsa_dep, mathlib],
dependencies : [dbus_dep, pipewire_dep, alsa_dep, mathlib, sm_logind_dep],
)
endif

View file

@ -52,8 +52,8 @@ void
pw_loop_destroy(struct pw_loop *loop);
#define pw_loop_add_source(l,...) spa_loop_add_source((l)->loop,__VA_ARGS__)
#define pw_loop_update_source(l,...) spa_loop_update_source(__VA_ARGS__)
#define pw_loop_remove_source(l,...) spa_loop_remove_source(__VA_ARGS__)
#define pw_loop_update_source(l,...) spa_loop_update_source((l)->loop,__VA_ARGS__)
#define pw_loop_remove_source(l,...) spa_loop_remove_source((l)->loop,__VA_ARGS__)
#define pw_loop_invoke(l,...) spa_loop_invoke((l)->loop,__VA_ARGS__)
#define pw_loop_get_fd(l) spa_loop_control_get_fd((l)->control)