Files
lotherk 37d566dac5 mcdesoundd: the session's Start and End tones (t0043)
dtsession starts mcdesoundd at login (config/0075.mcdesoundd) and the daemon
dies with the session; the two theme events Start and End were never played.
Now the daemon plays Start as soon as it owns _MCDE_SOUND_SERVER, and End in
an X I/O error handler: at logout dtlogin resets the X server, the daemon's
connection dies there, it forks the player (which outlives it) and exits.
Both go through the theme (MCDE_SOUND_THEME), not a fixed file; the path does
not need X, so it works in the I/O error handler.

Measured (Xvfb :86, installed Base theme, PULSE_SINK on a private null sink in
the running PulseAudio -- no sound on the real sink):
- start: "mcdesoundd: theme .../themes/Base" and
  "mcdesoundd: Start -> .../themes/Base/start.wav (player)".
- kill the Xvfb (the server reset at logout):
  "mcdesoundd: End -> .../themes/Base/end.wav (player)", and the daemon is
  gone (only the session's own daemon remains).
make mcde-linux MCDE_DIRS="install programs/mcdesoundd" exit=0;
MCDE_DIRS=plist "mcde-plist-check: ok".

The AX_* events of the same task (AccessX and the Keyboard pane) are not in
this commit; t0043 stays open for them.
2026-11-16 14:36:09 +00:00

270 lines
7.5 KiB
C

/*
* mcdesoundd.c -- MCDE: the session's sound server
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The desktop's events (docs/MCDE-PROGRAMS.md A1-A4): a client sends a
* ClientMessage _MCDE_SOUND_EVENT with the event name to the selection owner
* of _MCDE_SOUND_SERVER; this server looks the name up in the theme, drops a
* repeat within 50 ms, and plays the file -- or rings the X bell when no
* player is installed.
*
* Modes:
* mcdesoundd run the server
* mcdesoundd -event NAME send one event to a running server
* mcdesoundd -selftest read the theme, check events and files
*
* The session's own two tones (t0043): the daemon plays "Start" when it has
* the selection (dtsession starts it at login), and "End" from the X I/O
* error handler -- at logout dtlogin resets the X server, every client's
* connection dies, and the player it forks lives on after this process.
*
* C99 and POSIX, Xlib and Xt, as every MCDE program.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <Xm/Xm.h>
#include <Xm/DrawingA.h>
#include <Mcde/McdeSys.h>
#include <Mcde/Sound.h>
static Display *dpy;
static Window service;
static char lastEvent[64];
static struct timeval lastTime;
/* an event of the theme, played without asking the server (Start at login,
* End when the session's display goes): no X in this path, so it works in
* the I/O error handler too */
static void
PlayThemeEvent(const char *event)
{
const char *file = McdeSoundFile(event);
char path[768];
if (file == NULL) {
fprintf(stderr, "mcdesoundd: %s has no sound in the theme\n",
event);
return;
}
snprintf(path, sizeof path, "%s/%s", McdeSoundThemeDir(), file);
if (access(path, R_OK) != 0) {
fprintf(stderr, "mcdesoundd: %s -> %s (missing)\n", event, path);
return;
}
fprintf(stderr, "mcdesoundd: %s -> %s (%s)\n", event, path,
mcde_sys_sound_play(path) == MCDE_SYS_OK ? "player" : "failed");
}
/* dtlogin resets the X server at logout: the connection dies here, and the
* session's End tone is the last thing this daemon does */
static int
EndOnXError(Display *d)
{
(void) d;
PlayThemeEvent("End");
_exit(0);
return 0; /* not reached */
}
static long
MsSinceLast(void)
{
struct timeval now;
gettimeofday(&now, NULL);
return (now.tv_sec - lastTime.tv_sec) * 1000L +
(now.tv_usec - lastTime.tv_usec) / 1000L;
}
static void
PlayEvent(const char *event)
{
const char *file;
char path[768];
if (strcmp(event, lastEvent) == 0 && MsSinceLast() < 50) {
fprintf(stderr, "mcdesoundd: %s dropped (repeat within 50 ms)\n",
event);
return;
}
snprintf(lastEvent, sizeof lastEvent, "%s", event);
gettimeofday(&lastTime, NULL);
file = McdeSoundFile(event);
if (file == NULL) {
fprintf(stderr, "mcdesoundd: %s has no sound in the theme\n", event);
return;
}
snprintf(path, sizeof path, "%s/%s", McdeSoundThemeDir(), file);
if (access(path, R_OK) != 0) {
fprintf(stderr, "mcdesoundd: %s -> %s (missing)\n", event, path);
return;
}
fprintf(stderr, "mcdesoundd: %s -> %s (%s)\n", event, path,
McdeSoundPlay(dpy, path) == 0 ? "player" : "bell");
}
static void
ReloadTheme(void)
{
Atom prop = XInternAtom(dpy, "_MCDE_SOUND_THEME", False);
Atom type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
if (XGetWindowProperty(dpy, service, prop, 0, 1024, False,
AnyPropertyType, &type, &format, &n, &after, &data) == Success &&
data != NULL && n > 0) {
if (McdeSoundLoadTheme((char *) data) == 0)
fprintf(stderr, "mcdesoundd: theme now %s\n",
McdeSoundThemeDir());
else
fprintf(stderr, "mcdesoundd: cannot read theme %s\n",
(char *) data);
XFree(data);
}
}
static void
Message(Widget w, XtPointer data, XEvent *ev, Boolean *keep)
{
(void) w; (void) data; (void) keep;
if (ev->type != ClientMessage || ev->xclient.format != 8)
return;
if (ev->xclient.message_type ==
XInternAtom(dpy, "_MCDE_SOUND_EVENT", False)) {
/* the 20 bytes of the message, ended here whatever the
* sender put into them */
char name[sizeof ev->xclient.data.b + 1];
memcpy(name, ev->xclient.data.b, sizeof ev->xclient.data.b);
name[sizeof name - 1] = '\0';
PlayEvent(name);
} else if (ev->xclient.message_type ==
XInternAtom(dpy, "_MCDE_SOUND_RELOAD", False))
ReloadTheme();
}
int
main(int argc, char **argv)
{
XtAppContext app;
Widget top, area;
Atom sel;
int i;
/* -selftest and -event do not need the widget tree */
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-selftest") == 0) {
const char *names[] = { "Launch", "OpenFolder", "Error",
"Beep", "DeskSwitch", NULL };
int ok = 1, j;
if ((dpy = XOpenDisplay(NULL)) == NULL)
return 2;
if (McdeSoundInit() != 0) {
printf("mcdesoundd selftest: FAIL no theme\n");
return 1;
}
for (j = 0; names[j] != NULL; j++) {
const char *f = McdeSoundFile(names[j]);
char p[768];
if (f == NULL) {
printf("mcdesoundd: %s missing\n",
names[j]);
ok = 0;
continue;
}
snprintf(p, sizeof p, "%s/%s",
McdeSoundThemeDir(), f);
if (access(p, R_OK) != 0) {
printf("mcdesoundd: %s -> %s missing\n",
names[j], p);
ok = 0;
}
}
printf("mcdesoundd selftest: %s (%s)\n",
ok ? "PASS" : "FAIL", McdeSoundThemeDir());
return ok ? 0 : 1;
}
if (strcmp(argv[i], "-set-theme") == 0 && i + 1 < argc) {
if ((dpy = XOpenDisplay(NULL)) == NULL)
return 2;
if (McdeSoundSetTheme(dpy, argv[i + 1]) != 0) {
fprintf(stderr,
"mcdesoundd: no server for the theme\n");
return 1;
}
fprintf(stderr, "mcdesoundd: theme %s sent\n",
argv[i + 1]);
return 0;
}
if (strcmp(argv[i], "-event") == 0 && i + 1 < argc) {
if ((dpy = XOpenDisplay(NULL)) == NULL)
return 2;
if (McdeSoundInit() != 0) {
fprintf(stderr, "mcdesoundd: no theme\n");
return 1;
}
if (McdeSoundEvent(dpy, argv[i + 1]) != 0) {
fprintf(stderr,
"mcdesoundd: no server for %s\n",
argv[i + 1]);
return 1;
}
fprintf(stderr, "mcdesoundd: %s sent\n", argv[i + 1]);
return 0;
}
}
top = XtVaAppInitialize(&app, "Mcdesoundd", NULL, 0, &argc, argv, NULL,
NULL);
dpy = XtDisplay(top);
XSetIOErrorHandler(EndOnXError);
if (McdeSoundInit() != 0) {
fprintf(stderr, "mcdesoundd: no theme found\n");
return 1;
}
area = XtVaCreateManagedWidget("sound", xmDrawingAreaWidgetClass, top,
XmNwidth, 1, XmNheight, 1, NULL);
/* the shell is only the owner of the selection: realized, so that the
* window exists, but never shown (a 1x1 window the window manager
* would frame, measured: Map State IsViewable; tasksd t0080) */
XtSetMappedWhenManaged(top, False);
XtRealizeWidget(top);
service = XtWindow(area);
XtAddEventHandler(area, NoEventMask, True, Message, NULL);
sel = XInternAtom(dpy, "_MCDE_SOUND_SERVER", False);
/* one per display, as dtbar: a second start (a login script and a
* user's) leaves the running one alone */
if (XGetSelectionOwner(dpy, sel) != None) {
fprintf(stderr, "mcdesoundd: a server runs already\n");
return 0;
}
XSetSelectionOwner(dpy, sel, service, CurrentTime);
if (XGetSelectionOwner(dpy, sel) != service) {
fprintf(stderr, "mcdesoundd: another server owns the selection\n");
return 1;
}
fprintf(stderr, "mcdesoundd: theme %s\n", McdeSoundThemeDir());
/* dtsession starts this daemon at login; the session's Start tone */
PlayThemeEvent("Start");
XtAppMainLoop(app);
return 0;
}