/* * dock.c -- dtsystemsettings: the programs kept in the front panel's dock * * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. * * The dock is the part of the front panel left of the Trash: the programs * kept there, then the running programs that have no place in the panel * (WmMcdePanel.c). The kept ones are ~/.dt/panel-apps, one a line: * * CLASS COMMAND * * This pane shows that list, removes an entry and adds one. A change is * written at once, and the window manager is restarted with * _DtWmRestartNoConfirm so the panel shows it (the panel is dtwm's; it * reads the file when the panel is built). * * 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 "ui.h" #define MAXAPPS 64 extern int _DtWmRestartNoConfirm(Display *display, Window root); static Widget dockList; static Widget addShell, addText; static char apps[MAXAPPS][512]; static int napps; static const char * Path(void) { static char path[512]; const char *home = getenv("HOME"); (void) snprintf(path, sizeof path, "%s/.dt/panel-apps", home != NULL ? home : "."); return path; } static void Load(void) { FILE *f; char line[512]; napps = 0; if ((f = fopen(Path(), "r")) == NULL) return; while (napps < MAXAPPS && fgets(line, sizeof line, f) != NULL) { char *nl = strchr(line, '\n'); if (nl != NULL) *nl = '\0'; if (line[0] == '\0') continue; (void) snprintf(apps[napps], sizeof apps[0], "%s", line); napps++; } fclose(f); } static void Save(void) { FILE *f = fopen(Path(), "w"); int i; if (f == NULL) return; for (i = 0; i < napps; i++) fprintf(f, "%s\n", apps[i]); fclose(f); } static void Show(void) { XmString items[MAXAPPS]; char buf[600]; int i; XmListDeleteAllItems(dockList); for (i = 0; i < napps; i++) { char *tab = strchr(apps[i], '\t'); if (tab != NULL) (void) snprintf(buf, sizeof buf, "%.*s - %s", (int) (tab - apps[i]), apps[i], tab + 1); else (void) snprintf(buf, sizeof buf, "%s", apps[i]); items[i] = XmStringCreateLocalized(buf); } if (napps > 0) XmListAddItems(dockList, items, napps, 1); for (i = 0; i < napps; i++) XmStringFree(items[i]); } static void Restart(void) { _DtWmRestartNoConfirm(XtDisplay(dockList), DefaultRootWindow(XtDisplay(dockList))); } static void RemoveClicked(Widget w, XtPointer cd, XtPointer call) { int *pos = NULL, n = 0, i; (void) w; (void) cd; (void) call; if (!XmListGetSelectedPos(dockList, &pos, &n) || n < 1) { Message(dockList, "Choose a program in the list first.", 0); return; } i = pos[0] - 1; XtFree((char *) pos); if (i < 0 || i >= napps) return; memmove(&apps[i], &apps[i + 1], (size_t) (napps - i - 1) * sizeof apps[0]); napps--; Save(); Show(); Restart(); } static void AddOk(Widget w, XtPointer cd, XtPointer call) { char *cmd = XmTextFieldGetString(addText); char cls[128], *base; (void) w; (void) cd; (void) call; if (cmd == NULL) return; if (cmd[0] == '\0') { XtFree(cmd); Message(dockList, "Name the command to start.", 0); return; } base = strrchr(cmd, '/'); base = base != NULL ? base + 1 : cmd; (void) snprintf(cls, sizeof cls, "%s", base); if (cls[0] >= 'a' && cls[0] <= 'z') cls[0] = (char) (cls[0] - 'a' + 'A'); if (napps < MAXAPPS) { (void) snprintf(apps[napps], sizeof apps[0], "%s\t%s", cls, cmd); napps++; Save(); Show(); } XtFree(cmd); XtUnmanageChild(addShell); Restart(); } static void AddCancel(Widget w, XtPointer cd, XtPointer call) { (void) w; (void) cd; (void) call; XtUnmanageChild(addShell); } static void AddClicked(Widget w, XtPointer cd, XtPointer call) { Widget form, ok, cancel; (void) w; (void) cd; (void) call; if (addShell == NULL) { addShell = XmCreateDialogShell(dockList, "addProgram", NULL, 0); XtVaSetValues(addShell, XmNtitle, "Add a Program", NULL); form = XtVaCreateManagedWidget("addForm", xmFormWidgetClass, addShell, XmNmarginWidth, 12, XmNmarginHeight, 12, XmNautoUnmanage, False, NULL); (void) Text(form, "addLabel", "Command (the program to start):"); addText = XtVaCreateManagedWidget("addCommand", xmTextFieldWidgetClass, form, XmNcolumns, 40, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, XtNameToWidget(form, "addLabel"), XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, NULL); ok = Button(form, "Add", AddOk, NULL); XtVaSetValues(ok, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, addText, XmNleftAttachment, XmATTACH_FORM, NULL); cancel = Button(form, "Cancel", AddCancel, NULL); XtVaSetValues(cancel, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, addText, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, ok, NULL); } XtManageChild(addShell); XtManageChild(XtNameToWidget(addShell, "*addForm")); XmProcessTraversal(addText, XmTRAVERSE_CURRENT); } Widget MakeDock(Widget parent) { Widget box, list; box = XtVaCreateWidget("dockPane", xmRowColumnWidgetClass, parent, XmNorientation, XmVERTICAL, XmNspacing, 10, NULL); (void) Text(box, "dockLabel", "The programs kept in the dock, left of the Trash in the front " "panel. A click starts the program or brings it forward; the " "right mouse button in the panel removes it."); list = XtVaCreateManagedWidget("dockList", xmListWidgetClass, box, XmNvisibleItemCount, 10, XmNselectionPolicy, XmSINGLE_SELECT, XmNwidth, 420, NULL); dockList = list; (void) Button(box, "Remove from the Dock", RemoveClicked, NULL); (void) Button(box, "Add a Program...", AddClicked, NULL); XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box, XmNorientation, XmHORIZONTAL, XmNmarginTop, 4, XmNmarginBottom, 4, NULL); (void) Text(box, "dockNote", "The running programs that are not kept appear in the dock on " "their own; keeping one writes it to ~/.dt/panel-apps. A change " "restarts the window manager, so the panel is built again."); Load(); Show(); return box; }