sound.c's four error sentences are messages 39-42 of mcdesys.cat: the sound server tells no volume, there is no sound file "%s", no player is installed, cannot start %s. The German copy in programs/localized/de_DE.UTF-8/msg/mcdesys.msg keeps the numbers. Measured with a runner (setlocale + mcde_sys_sound_play on a missing file): LANG=C "There is no sound file \"/nonexistent/ding.wav\"."; LANG=de_DE.UTF-8 "Es gibt keine Tondatei \"/nonexistent/ding.wav\"." make mcde-linux MCDE_DIRS="install lib/McdeSys" and "install programs/localized/C/msg" exit=0; "check lib/McdeSys": # FAIL: 0, exit=0; MCDE_DIRS=plist: ok.
170 lines
4.7 KiB
C
170 lines
4.7 KiB
C
/*
|
|
* sound.c -- MCDE: the volume of the default output (McdeSys.h, "Sound")
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* Where a PulseAudio server runs, its default sink is the output: pactl(1)
|
|
* gets and sets it, the same on every system (pkgsrc has audio/pulseaudio).
|
|
* Without one, the backend asks the system's mixer (mcde_sys_mixer_*:
|
|
* mixerctl(1) on NetBSD). A sound is played the same way: paplay(1)
|
|
* where the server runs, else the system's player (mcde_sys_player:
|
|
* audioplay(1) on NetBSD, aplay(1) on Linux). C99 and POSIX only.
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
static const char *const pactlPaths[] = {
|
|
"/usr/bin/pactl", MCDE_PKG_PREFIX "/bin/pactl", "/usr/local/bin/pactl",
|
|
NULL
|
|
};
|
|
|
|
/* pactl, if a server answers it. The answer is kept: asked once, a "yes"
|
|
* stays until a read fails (the server went away or hangs), a "no" for 30 s --
|
|
* the bar read the volume every few seconds and ran `pactl info' first each
|
|
* time, and a stopped server made that wait for ever (tasksd t0080). The
|
|
* question itself is limited to MCDE_SYS_STATUS_SECONDS. */
|
|
static const char *pactlKnown; /* the pactl that answered */
|
|
static time_t pactlNone; /* when nobody did; 0 = not asked */
|
|
|
|
static void
|
|
PactlForget(void)
|
|
{
|
|
pactlKnown = NULL;
|
|
pactlNone = time(NULL); /* no new question for 30 s */
|
|
}
|
|
|
|
static const char *
|
|
Pactl(void)
|
|
{
|
|
const char *p = mcde_sys_which(pactlPaths);
|
|
char out[256];
|
|
char *argv[] = { NULL, "info", NULL };
|
|
|
|
if (p == NULL)
|
|
return NULL;
|
|
if (pactlKnown != NULL)
|
|
return pactlKnown;
|
|
if (pactlNone != 0 && time(NULL) - pactlNone < 30)
|
|
return NULL;
|
|
argv[0] = (char *) p;
|
|
if (mcde_sys_capture_limited(argv, out, sizeof out,
|
|
MCDE_SYS_STATUS_SECONDS) == MCDE_SYS_OK) {
|
|
pactlKnown = p;
|
|
pactlNone = 0;
|
|
return p;
|
|
}
|
|
pactlNone = time(NULL);
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
mcde_sys_volume_get(int *percent, int *muted)
|
|
{
|
|
const char *p = Pactl();
|
|
char out[512], *pc;
|
|
|
|
if (p == NULL)
|
|
return mcde_sys_mixer_get(percent, muted);
|
|
{
|
|
char *v[] = { (char *) p, "get-sink-volume", "@DEFAULT_SINK@",
|
|
NULL };
|
|
char *m[] = { (char *) p, "get-sink-mute", "@DEFAULT_SINK@",
|
|
NULL };
|
|
|
|
/* "Volume: front-left: 26214 / 40% / -23.88 dB, ..." */
|
|
if (mcde_sys_capture_limited(v, out, sizeof out,
|
|
MCDE_SYS_STATUS_SECONDS) != MCDE_SYS_OK ||
|
|
(pc = strchr(out, '%')) == NULL) {
|
|
PactlForget(); /* gone or hung: the mixer, for a while */
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL,
|
|
mcde_sys_msg(MSG_SND_NO_VOLUME, "The sound server tells no volume."));
|
|
}
|
|
while (pc > out && pc[-1] >= '0' && pc[-1] <= '9')
|
|
pc--;
|
|
*percent = atoi(pc);
|
|
/* "Mute: no" */
|
|
*muted = mcde_sys_capture_limited(m, out, sizeof out,
|
|
MCDE_SYS_STATUS_SECONDS) == MCDE_SYS_OK &&
|
|
strstr(out, "yes") != NULL;
|
|
}
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_volume_set(int percent)
|
|
{
|
|
const char *p = Pactl();
|
|
char val[16];
|
|
char *argv[] = { NULL, "set-sink-volume", "@DEFAULT_SINK@", val, NULL };
|
|
|
|
if (percent < 0)
|
|
percent = 0;
|
|
if (percent > 100)
|
|
percent = 100;
|
|
if (p == NULL)
|
|
return mcde_sys_mixer_set(percent);
|
|
argv[0] = (char *) p;
|
|
snprintf(val, sizeof val, "%d%%", percent);
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
int
|
|
mcde_sys_volume_mute(int muted)
|
|
{
|
|
const char *p = Pactl();
|
|
char *argv[] = { NULL, "set-sink-mute", "@DEFAULT_SINK@", NULL, NULL };
|
|
|
|
if (p == NULL)
|
|
return mcde_sys_mixer_mute(muted);
|
|
argv[0] = (char *) p;
|
|
argv[3] = muted ? "1" : "0";
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
static const char *const paplayPaths[] = {
|
|
"/usr/bin/paplay", MCDE_PKG_PREFIX "/bin/paplay", "/usr/local/bin/paplay",
|
|
NULL
|
|
};
|
|
|
|
/* the player started in a grandchild: the caller (mcdesoundd's event loop)
|
|
* neither waits while the sound plays nor leaves a zombie */
|
|
int
|
|
mcde_sys_sound_play(const char *file)
|
|
{
|
|
const char *player = Pactl() != NULL ? mcde_sys_which(paplayPaths) :
|
|
NULL;
|
|
pid_t pid;
|
|
int status;
|
|
|
|
if (file == NULL || access(file, R_OK) != 0)
|
|
return mcde_sys_fail(MCDE_SYS_ENOENT, mcde_sys_msg(MSG_SND_NO_FILE, "There is no sound file \"%s\"."), file != NULL ? file : "");
|
|
if (player == NULL)
|
|
player = mcde_sys_player();
|
|
if (player == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_SND_NO_PLAYER, "No player for sounds is installed."));
|
|
if ((pid = fork()) < 0)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_SND_CANNOT_START, "Cannot start %s."), player);
|
|
if (pid == 0) {
|
|
if (fork() == 0) {
|
|
(void) setsid();
|
|
execl(player, player, file, (char *) NULL);
|
|
_exit(127);
|
|
}
|
|
_exit(0);
|
|
}
|
|
(void) waitpid(pid, &status, 0);
|
|
return MCDE_SYS_OK;
|
|
}
|