/* * sound.c -- dtsystemsettings: the sound of the desktop (t0163) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * Three tabs as the reference has them: Sound Effects, Output, Input. * * Sound Effects the events of the session's sound theme, a click plays * one (McdeSoundEvent, the server of mcdesoundd); the * theme itself is chosen from the directories next to the * current one. * Output the volume of the default output and Mute * (mcde_sys_volume_get/_set/_mute), and whether the bar * shows it (~/.dt/bar, "showvolume="; dtbar reads it at * start). * Input a note: McdeSys has no capture API yet, so there is no * input level and no input volume to set. * * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ui.h" static Widget soundState, soundScale, soundMute, soundBar, soundThemes; static Widget soundList; /* ---- the bar's "show the volume" (~/.dt/bar) ---------------------------- */ static void BarFile(char *buf, size_t len) { const char *home = getenv("HOME"); (void)snprintf(buf, len, "%s/.dt/bar", home != NULL ? home : "."); } static Boolean BarShowsVolume(void) { char path[1024], line[128]; FILE *f; Boolean show = True; BarFile(path, sizeof path); if ((f = fopen(path, "r")) == NULL) return True; while (fgets(line, sizeof line, f) != NULL) { line[strcspn(line, "\r\n")] = '\0'; if (strncmp(line, "showvolume=", 11) == 0) show = atoi(line + 11) != 0; } fclose(f); return show; } static void BarWrite(Boolean show) { char path[1024], keep[64][160], line[160]; FILE *f; int i, n = 0; BarFile(path, sizeof path); if ((f = fopen(path, "r")) != NULL) { while (n < 64 && fgets(line, sizeof line, f) != NULL) { line[strcspn(line, "\r\n")] = '\0'; if (line[0] == '\0' || strncmp(line, "showvolume=", 11) == 0) continue; (void)snprintf(keep[n++], sizeof keep[0], "%s", line); } fclose(f); } if ((f = fopen(path, "w")) != NULL) { fputs("# the bar (dtbar): what the status area shows\n", f); for (i = 0; i < n; i++) fprintf(f, "%s\n", keep[i]); fprintf(f, "showvolume=%d\n", show ? 1 : 0); fclose(f); } } /* ---- the pages ---------------------------------------------------------- */ static void SoundRead(void) { int vol = 0, muted = 0; if (mcde_sys_volume_get(&vol, &muted) != MCDE_SYS_OK) { SetText(soundState, "There is no sound output."); XtSetSensitive(soundScale, False); XtSetSensitive(soundMute, False); return; } SetText(soundState, "The volume of the default output:"); XmScaleSetValue(soundScale, vol > 100 ? 100 : vol); XmToggleButtonSetState(soundMute, muted, False); } static void SoundVolume(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) mcde_sys_volume_set(((XmScaleCallbackStruct *) call)->value); } static void SoundMute(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) mcde_sys_volume_mute(((XmToggleButtonCallbackStruct *) call)->set); } static void SoundBar(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; BarWrite(XmToggleButtonGetState(soundBar)); SetText(soundState, "The bar reads it when it starts (log out and in, " "or start dtbar again)."); } /* the events of the theme; a click plays one */ static void EventChosen(Widget w, XtPointer data, XtPointer call) { XmListCallbackStruct *c = (XmListCallbackStruct *) call; char *text = NULL; (void) w; (void) data; if (!XmStringGetLtoR(c->item, XmFONTLIST_DEFAULT_TAG, &text)) return; (void) McdeSoundEvent(XtDisplay(w), text); XtFree(text); } /* the themes: the directories next to the current one */ static void ThemeChosen(Widget w, XtPointer data, XtPointer call) { Widget hist = NULL, item = NULL; char *dir = NULL; (void) call; XtVaGetValues(soundThemes, XmNmenuHistory, &hist, NULL); if (hist == NULL || hist != w) return; XtVaGetValues(soundThemes, XmNmenuHistory, &item, NULL); if (item == NULL) return; XtVaGetValues(item, XmNuserData, &dir, NULL); if (dir != NULL) (void) McdeSoundSetTheme(XtDisplay(soundThemes), dir); } static void MakeThemes(Widget parent, Widget page) { char root[512], path[768], *slash; DIR *d; struct dirent *e; Widget row, pd; XmString s; (void) parent; (void) McdeSoundInit(); snprintf(root, sizeof root, "%s", McdeSoundThemeDir()); if ((slash = strrchr(root, '/')) == NULL) return; *slash = '\0'; if ((d = opendir(root)) == NULL) return; row = XtVaCreateManagedWidget("themeRow", xmRowColumnWidgetClass, page, XmNorientation, XmHORIZONTAL, XmNspacing, 6, NULL); s = XmStringCreateLocalized((char *) "Theme:"); (void) XtVaCreateManagedWidget("themeLabel", xmLabelWidgetClass, row, XmNlabelString, s, NULL); XmStringFree(s); soundThemes = XmCreateOptionMenu(row, "themes", NULL, 0); pd = XmCreatePulldownMenu(soundThemes, "themeMenu", NULL, 0); while ((e = readdir(d)) != NULL) { struct stat st; Widget b; if (e->d_name[0] == '.') continue; (void)snprintf(path, sizeof path, "%s/%s", root, e->d_name); if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) continue; s = XmStringCreateLocalized(e->d_name); b = XtVaCreateManagedWidget("themeItem", xmPushButtonGadgetClass, pd, XmNlabelString, s, XmNuserData, strdup(path), NULL); XmStringFree(s); XtAddCallback(b, XmNactivateCallback, ThemeChosen, NULL); } XtVaSetValues(soundThemes, XmNsubMenuId, pd, NULL); XtManageChild(soundThemes); closedir(d); (void) row; } static Widget MakeEffectsPage(Widget parent) { Widget page = XtVaCreateWidget("soundEffects", xmRowColumnWidgetClass, parent, XmNspacing, 6, NULL); Widget l; XmString s; int i, n; s = XmStringCreateLocalized((char *) "Choose an alert sound"); l = XtVaCreateManagedWidget("effectsLabel", xmLabelWidgetClass, page, XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING, NULL); XmStringFree(s); XtVaSetValues(l, XmNrenderTable, PaneFont(page, 11, True), NULL); soundList = XmCreateScrolledList(page, "events", NULL, 0); XtVaSetValues(soundList, XmNvisibleItemCount, 10, XmNselectionPolicy, XmBROWSE_SELECT, XmNwidth, 320, NULL); (void) McdeSoundInit(); n = McdeSoundCount(); for (i = 0; i < n; i++) { const char *name = McdeSoundEventName(i); if (name == NULL) continue; s = XmStringCreateLocalized((char *) name); XmListAddItemUnselected(soundList, s, 0); XmStringFree(s); } XtAddCallback(soundList, XmNbrowseSelectionCallback, EventChosen, NULL); XtManageChild(soundList); MakeThemes(page, page); (void) Text(page, "effectsNote", "A click plays the event through " "mcdesoundd. The list is the session's theme; \"alert\" is what " "a warning plays."); return page; } static Widget MakeOutputPage(Widget parent) { Widget page = XtVaCreateWidget("soundOutput", xmRowColumnWidgetClass, parent, XmNspacing, 8, NULL); soundState = Text(page, "state", ""); soundScale = XtVaCreateManagedWidget("volume", xmScaleWidgetClass, page, XmNorientation, XmHORIZONTAL, XmNminimum, 0, XmNmaximum, 100, XmNshowValue, True, XmNscaleWidth, 320, NULL); XtAddCallback(soundScale, XmNvalueChangedCallback, SoundVolume, NULL); soundMute = XtVaCreateManagedWidget("Mute", xmToggleButtonWidgetClass, page, NULL); XtAddCallback(soundMute, XmNvalueChangedCallback, SoundMute, NULL); soundBar = XtVaCreateManagedWidget("Show the volume in the bar", xmToggleButtonWidgetClass, page, NULL); XmToggleButtonSetState(soundBar, BarShowsVolume(), False); XtAddCallback(soundBar, XmNvalueChangedCallback, SoundBar, NULL); (void) Text(page, "outputNote", "One output: the default. McdeSys has " "no list of sound devices yet, so there is no device to choose."); SoundRead(); return page; } static Widget MakeInputPage(Widget parent) { Widget page = XtVaCreateWidget("soundInput", xmRowColumnWidgetClass, parent, XmNspacing, 8, NULL); (void) Text(page, "inputNote", "There is no input to show: McdeSys " "has no capture API.\nNo input level meter and no input volume; " "they belong to the same\naddition as the output device list."); return page; } /* ---- the pane ----------------------------------------------------------- */ static Widget soundPages[3]; static void SoundTab(Widget w, XtPointer data, XtPointer call) { int which = (int) (size_t) data, i; (void) w; if (call != NULL && !((XmToggleButtonCallbackStruct *) call)->set) return; for (i = 0; i < 3; i++) if (soundPages[i] != NULL) if (i == which) XtManageChild(soundPages[i]); else XtUnmanageChild(soundPages[i]); } Widget MakeSound(Widget parent) { Widget box = XtVaCreateWidget("sound", xmRowColumnWidgetClass, parent, XmNspacing, 8, NULL); Widget tabs, pages; static const char *const labels[3] = { "Sound Effects", "Output", "Input" }; int i; tabs = XtVaCreateManagedWidget("soundTabs", xmRowColumnWidgetClass, box, XmNorientation, XmHORIZONTAL, XmNspacing, 4, XmNmarginWidth, 0, NULL); /* vertical: a child of a vertical row column would pack its pages * side by side (the default is the parent's orientation reversed) */ pages = XtVaCreateManagedWidget("soundPages", xmRowColumnWidgetClass, box, XmNorientation, XmVERTICAL, XmNspacing, 4, NULL); for (i = 0; i < 3; i++) { XmString s = XmStringCreateLocalized((char *) labels[i]); Widget t = XtVaCreateManagedWidget("soundTab", xmToggleButtonWidgetClass, tabs, XmNlabelString, s, XmNindicatorOn, False, XmNshadowThickness, 2, XmNfillOnSelect, True, XmNmarginWidth, 10, NULL); XmStringFree(s); XtAddCallback(t, XmNvalueChangedCallback, SoundTab, (XtPointer) (size_t) i); } /* * the three pages; the first is shown, the tabs look their pages up * by name when one is chosen */ soundPages[0] = MakeEffectsPage(pages); soundPages[1] = MakeOutputPage(pages); soundPages[2] = MakeInputPage(pages); XtManageChild(soundPages[0]); { Widget first = XtNameToWidget(tabs, "soundTab"); if (first != NULL) XmToggleButtonSetState(first, True, False); } return box; }