Files
mcde/programs/mcdesoundd/mcdesoundd.c
T
lotherkandClaude Opus 5.5 9fd4818051 MCDE: the review's open points -- sound through McdeSys, dtwm plug and play, the rest
From the review of 2026-11-12's commits (the points left open in
1b524eb94):

Sound behind McdeSys (ext/src/mcde/AGENTS.md, philosophy 3): libMcdeSound
named /usr/bin/paplay, aplay, /usr/pkg/bin/audioplay and ffplay itself and
waited for the player, so mcdesoundd heard nothing while a sound played.
mcde_sys_sound_play (sound.c) takes paplay where a PulseAudio server
answers, else the backend's player (mcde_sys_player: audioplay(1) on
NetBSD, aplay(1) on Linux), started in a grandchild: no wait, no zombie.
Measured against a private PulseAudio with a null sink: OpenFolder and
TerminalBell sent back to back play at the same time (pactl list short
sink-inputs: two streams), mcdesoundd -selftest PASS, a second mcdesoundd
says "a server runs already".  The /usr/pkg/dt fallbacks of Sound.c and
dtphoto.c are gone (CDE_INSTALLATION_TOP is the prefix);
McdeSoundFile's dead buffer is gone; mcdesoundd ends the 20 bytes of a
message itself; Sound.h is installed (include/Makefile.am, PLIST).

Linking: LIBMCDESOUND and LIBTERM carry the libraries below them
(configure.ac).  Debian's libtool hands no indirect library to the
linker, which then took the installed, older libMcdeSys through
libMcdeSound's RUNPATH and failed (dtterm: undefined
mcde_sys_sound_play).

dtwm plug and play (WmMcdeDisplays.c, written 2026-11-11, uncommitted and
not compiling): the ClientData fields it uses, McdeHeadsChanged (Xinerama's
list read anew, its XineramaQueryScreens memory given back with XFree),
McdePanelMonitorsChanged (the panel centres on the main display), the
RandR events taken in WmMain.c's loop, McdeDisplaysInit after the EWMH
setup, the file in Makefile.am.  Measured on Xvfb: dtwm starts with it,
the check at login leaves the outputs as they were (xrandr before and
after: screen connected 1024x700+0+0), dtwm runs on.  Plugging itself was
not measured: Xvfb cannot change its outputs, and Xorg's dummy driver
refuses a user who is not at the console.

Smaller: dttoolbox ignores SIGCHLD from the start (set after the first
fork it missed the first child) and lost its test hook
/tmp/dttoolbox-no-home; dtfile plays OpenFolder once per folder
(FileMgr.c), not twice; dtpaint's unused variable; dttoolbox frees
XGetAtomName's string without a cast-away const.  AGENTS.md describes
McdeSound, mcdesoundd, dtphoto, dtpaint, dttoolbox and the plug and play;
the WAV tones' maker was not kept, which it says.  The two stale citations
(53b852bb2, 945da0f4c) are corrected in git notes.

Built with make mcde-host, exit 0 (build/logs/mcde-host-2026-11-13q.log).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 06:41:53 +00:00

223 lines
5.9 KiB
C

/*
* mcdesoundd.c -- MCDE: the session's sound server
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* 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
*
* 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/Sound.h>
static Display *dpy;
static Window service;
static char lastEvent[64];
static struct timeval lastTime;
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);
if (McdeSoundInit() != 0) {
fprintf(stderr, "mcdesoundd: no theme found\n");
return 1;
}
area = XtVaCreateManagedWidget("sound", xmDrawingAreaWidgetClass, top,
XmNwidth, 1, XmNheight, 1, NULL);
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());
XtAppMainLoop(app);
return 0;
}