/* * language.c -- dtsystemsettings: the language of the desktop * * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. * * Replaces CDE's style manager here (Konrad, 2026-11-13: "das alte dtstyle * muss weg"). The list is what locale(1) -a prints -- the POSIX utility, * the same source dtlogin's language menu has (dtlslocale) -- reduced to * the UTF-8 locales and "C". "Use for Next Login" writes the name to * ~/.dt/language, which Xsession.d/0010.language reads before the desktop * starts (programs/dtlogin/config); "Use the Login Screen's" removes it. * A running session keeps its language: every program has read LANG at its * start. Input methods (dtstyle's "Input Method" dialog) are not here; * dtimsstart(1) still starts one. * * 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 "ui.h" #define MAX_LOCALES 1000 #define LOCALE_LEN 64 static char (*locales)[LOCALE_LEN]; static int nlocales; static char chosen[LOCALE_LEN]; /* ~/.dt/language, "" without one */ static Widget localeList, useButton, loginButton, langNote; /* ~/.dt/language into BUF; the path when PATH is not NULL */ static void LanguageFile(char *buf, size_t len) { const char *home = getenv("HOME"); snprintf(buf, len, "%s/.dt/language", home != NULL ? home : ""); } static void ReadChosen(void) { char path[1024]; FILE *f; chosen[0] = '\0'; LanguageFile(path, sizeof path); if ((f = fopen(path, "r")) == NULL) return; if (fgets(chosen, sizeof chosen, f) == NULL) chosen[0] = '\0'; chosen[strcspn(chosen, " \t\r\n")] = '\0'; fclose(f); } /* a locale worth offering: C, or one with a UTF-8 code set */ static int Offered(const char *name) { const char *dot = strchr(name, '.'); if (strcmp(name, "C") == 0) return 1; return dot != NULL && (strncasecmp(dot + 1, "UTF-8", 5) == 0 || strncasecmp(dot + 1, "utf8", 4) == 0); } static int Compare(const void *a, const void *b) { return strcmp((const char *) a, (const char *) b); } static void ReadLocales(void) { char line[LOCALE_LEN]; FILE *p; nlocales = 0; if ((p = popen("locale -a 2>/dev/null", "r")) == NULL) return; while (fgets(line, sizeof line, p) != NULL && nlocales < MAX_LOCALES) { line[strcspn(line, "\r\n")] = '\0'; if (line[0] != '\0' && Offered(line)) snprintf(locales[nlocales++], LOCALE_LEN, "%s", line); } (void) pclose(p); qsort(locales, (size_t) nlocales, sizeof locales[0], Compare); } /* the locale selected in the list, or NULL */ static const char * Selected(void) { int *pos = NULL, n = 0, i; if (!XmListGetSelectedPos(localeList, &pos, &n) || n < 1) { XtFree((char *) pos); return NULL; } i = pos[0] - 1; XtFree((char *) pos); return i >= 0 && i < nlocales ? locales[i] : NULL; } static void ShowState(void) { const char *lang = getenv("LANG"); const char *s = Selected(); char text[256]; if (chosen[0] != '\0') snprintf(text, sizeof text, "This session: %s. Next login: %s.", lang != NULL ? lang : "C", chosen); else snprintf(text, sizeof text, "This session: %s. Next login: the " "language chosen on the login screen.", lang != NULL ? lang : "C"); SetText(langNote, text); XtSetSensitive(useButton, s != NULL && strcmp(s, chosen) != 0); XtSetSensitive(loginButton, chosen[0] != '\0'); } static void LocaleChosen(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; ShowState(); } static void UseLanguage(Widget w, XtPointer data, XtPointer call) { const char *s = Selected(); char path[1024], tmp[1100]; FILE *f; (void) data; (void) call; if (s == NULL) return; /* written beside and renamed over, so a reader never sees half */ LanguageFile(path, sizeof path); snprintf(tmp, sizeof tmp, "%s.new", path); if ((f = fopen(tmp, "w")) == NULL || fprintf(f, "%s\n", s) < 0 || fclose(f) != 0 || rename(tmp, path) != 0) { remove(tmp); Message(w, "The language could not be written to ~/.dt/language.", 1); return; } snprintf(chosen, sizeof chosen, "%s", s); ShowState(); } static void UseLoginScreen(Widget w, XtPointer data, XtPointer call) { char path[1024]; (void) data; (void) call; LanguageFile(path, sizeof path); if (remove(path) != 0 && chosen[0] != '\0') { Message(w, "~/.dt/language could not be removed.", 1); return; } chosen[0] = '\0'; ShowState(); } static void LanguageGone(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; free(locales); locales = NULL; nlocales = 0; } Widget MakeLanguage(Widget parent) { Widget box = XtVaCreateManagedWidget("language", xmRowColumnWidgetClass, parent, XmNspacing, 6, NULL); Widget buttons; const char *lang = getenv("LANG"), *want; Arg a[4]; int n, i, sel = 0; locales = calloc(MAX_LOCALES, sizeof *locales); if (locales != NULL) ReadLocales(); ReadChosen(); XtAddCallback(box, XmNdestroyCallback, LanguageGone, NULL); (void) Text(box, "label", "The language of menus, messages, dates and " "numbers. It is used from the next login on."); n = 0; XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++; XtSetArg(a[n], XmNvisibleItemCount, 12); n++; XtSetArg(a[n], XmNwidth, 360); n++; localeList = XmCreateScrolledList(box, "locales", a, n); XtManageChild(localeList); want = chosen[0] != '\0' ? chosen : lang; for (i = 0; i < nlocales; i++) { XmString s = XmStringCreateLocalized(locales[i]); XmListAddItemUnselected(localeList, s, 0); XmStringFree(s); if (want != NULL && strcmp(locales[i], want) == 0) sel = i + 1; } if (sel > 0) { XmListSelectPos(localeList, sel, False); XmListSetPos(localeList, sel > 3 ? sel - 3 : 1); } XtAddCallback(localeList, XmNbrowseSelectionCallback, LocaleChosen, NULL); buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, box, XmNorientation, XmHORIZONTAL, XmNspacing, 8, NULL); useButton = PaneButton(buttons, "Use for Next Login", UseLanguage, NULL); loginButton = PaneButton(buttons, "Use the Login Screen's", UseLoginScreen, NULL); langNote = Text(box, "note", nlocales == 0 ? "locale -a listed no languages." : ""); ShowState(); return box; }