From efa848d5fbf52b7f3eb4b8fed44978a4d2c37c70 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sat, 14 Nov 2026 15:33:39 +0000 Subject: [PATCH] MCDE dtfile: a copy window with bar and bytes from the child's pipe (t0155) --- AGENTS.md | 10 +++ programs/dtfile/FileManip.c | 20 ++++++ programs/dtfile/FileOp.c | 133 ++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index d4722800..7acb33c7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -155,6 +155,16 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt toggle and the text field exist unmanaged (`G_rename_toggle`, `G_rename_text`). The multi-collision and the folder-merge dialog are unchanged (follow-up). +- **Copy progress** (t0155): the child that copies a file (`FileManip.c`, + the read/write loop) writes `PIPEMSG_PROGRESS` with done/total and the + target name over the existing status pipe `pipe_s2m` (a message per + megabyte and at the end); `FileOpPipeCB` (FileOp.c) reads it and fills the + small "Copy" window -- "Copying \"NAME\"", a scale the program only sets, + "X MB of Y MB", Cancel -- created with the first message and popped up one + second later (XtAppAddTimeOut), so a short copy shows no window. Cancel + kills the child (`mcdeChildPid`) and unlinks the half-written target. + Measured: a 4 GB copy shows the window with "129.0 MB of 4295.0 MB" while + the file is written. - **dtcmprefs** (`programs/dtcmprefs`): the calendar's settings window on the shared McdePrefs pattern -- General (first day of the week, week numbers, default appointment length), Accounts (a read-only note: the diff --git a/programs/dtfile/FileManip.c b/programs/dtfile/FileManip.c index 2741cc36..b9bccbe2 100644 --- a/programs/dtfile/FileManip.c +++ b/programs/dtfile/FileManip.c @@ -68,6 +68,9 @@ #include #include #include + +/* the status pipe back to dtfile: FileOp.c */ +extern int PipeWriteString(int fd, char *s); #include #include @@ -1166,6 +1169,12 @@ FileManip( /* do the copy */ + /* MCDE: the bytes of this copy for the parent's progress window (t0155); + * W carries the pipe descriptor (see FileOpError) */ + unsigned long long mcdeDone = 0; + unsigned long long mcdeTotal = (unsigned long long) s4.st_size; + unsigned long long mcdeNext = 0; + int mcdeFd = (int) (intptr_t) w; while (n = read (fold, buf, BLOCK_SIZE)) { @@ -1187,6 +1196,17 @@ FileManip( errno = 0; result = write(fnew, buf, n); + if (mcdeFd > 2 && result > 0 && mcdeTotal > 0) { + short pmsg = 107; /* PIPEMSG_PROGRESS */ + mcdeDone += (unsigned long long) result; + if (mcdeDone - mcdeNext >= 1048576ULL || mcdeDone >= mcdeTotal) { + mcdeNext = mcdeDone; + if (write(mcdeFd, &pmsg, sizeof pmsg) == sizeof pmsg && + write(mcdeFd, &mcdeDone, sizeof mcdeDone) == sizeof mcdeDone && + write(mcdeFd, &mcdeTotal, sizeof mcdeTotal) == sizeof mcdeTotal) + (void) PipeWriteString(mcdeFd, to); + } + } if (result != n) { (void) close (fold); diff --git a/programs/dtfile/FileOp.c b/programs/dtfile/FileOp.c index dce79f5c..80f1b2e9 100644 --- a/programs/dtfile/FileOp.c +++ b/programs/dtfile/FileOp.c @@ -172,6 +172,123 @@ static Boolean IsInParentDir(char *from,char *to); /* the following messages are also defined & used in OverWrite.c */ #define PIPEMSG_CANCEL 101 +#define PIPEMSG_PROGRESS 107 /* MCDE: */ + +/* ---- MCDE: the progress of a copy/move (t0155) ------------------------- */ + +#include +#include +#include +#include + +static Widget mcdeProgShell, mcdeProgBar, mcdeProgText, mcdeProgName; +static int mcdeProgShown = -1; +static pid_t mcdeChildPid = -1; +static char mcdeProgPath[1024]; + +/* the toplevel of a widget */ +static Widget +McdeTop(Widget w) +{ + while (w != NULL && XtParent(w) != NULL) + w = XtParent(w); + return w; +} + +static void +McdeProgressPop(XtPointer data, XtIntervalId *id) +{ + (void) data; + (void) id; + if (mcdeProgShell != NULL) + XtPopup(mcdeProgShell, XtGrabNone); +} + +/* Cancel: the child stops and the half-written target goes -- the + * operation's own abort path does the rest */ +static void +McdeProgressCancel(Widget w, XtPointer data, XtPointer call) +{ + (void) w; + (void) data; + (void) call; + if (mcdeChildPid > 0) + (void) kill(mcdeChildPid, SIGTERM); + if (mcdeProgPath[0] != '\0') + (void) unlink(mcdeProgPath); + if (mcdeProgShell != NULL) { + XtPopdown(mcdeProgShell); + XtDestroyWidget(mcdeProgShell); + mcdeProgShell = NULL; + } +} + +static void +McdeProgressUpdate(Widget parent, unsigned long long done, + unsigned long long total, const char *path) +{ + char text[1100]; + int pct = total > 0 ? (int) ((done * 100) / total) : 0; + + if (mcdeProgShell == NULL) { + Widget box, row; + XmString s; + char base[512]; + const char *slash = strrchr(path != NULL ? path : "", '/'); + + (void) snprintf(base, sizeof base, "%s", slash != NULL ? slash + 1 : + (path != NULL ? path : "")); + (void) snprintf(mcdeProgPath, sizeof mcdeProgPath, "%s", + path != NULL ? path : ""); + mcdeProgShell = XtVaCreatePopupShell("mcdeProgress", + topLevelShellWidgetClass, McdeTop(parent), XmNtitle, "Copy", NULL); + box = XtVaCreateManagedWidget("progressBox", xmRowColumnWidgetClass, + mcdeProgShell, XmNorientation, XmVERTICAL, XmNspacing, 6, + XmNmarginWidth, 10, XmNmarginHeight, 10, XmNwidth, 420, NULL); + (void) snprintf(text, sizeof text, "Copying \"%s\"", base); + s = XmStringCreateLocalized(text); + mcdeProgName = XtVaCreateManagedWidget("progressName", + xmLabelWidgetClass, box, XmNlabelString, s, + XmNalignment, XmALIGNMENT_BEGINNING, NULL); + XmStringFree(s); + mcdeProgBar = XtVaCreateManagedWidget("progressBar", + xmScaleWidgetClass, box, XmNorientation, XmHORIZONTAL, + XmNminimum, 0, XmNmaximum, 100, XmNshowValue, False, + XmNscaleWidth, 380, NULL); + mcdeProgText = XtVaCreateManagedWidget("progressText", + xmLabelWidgetClass, box, XmNalignment, XmALIGNMENT_BEGINNING, + NULL); + row = XtVaCreateManagedWidget("progressButtons", + xmRowColumnWidgetClass, box, XmNorientation, XmHORIZONTAL, + XmNspacing, 8, XmNmarginWidth, 0, NULL); + { + Widget b = XtVaCreateManagedWidget("Cancel", + xmPushButtonWidgetClass, row, NULL); + XtAddCallback(b, XmNactivateCallback, McdeProgressCancel, NULL); + } + XtRealizeWidget(mcdeProgShell); + /* the reference shows the window only for a copy that takes a while */ + XtAppAddTimeOut(XtWidgetToApplicationContext(box), 1000, + McdeProgressPop, NULL); + } + if (mcdeProgShown != pct) { + mcdeProgShown = pct; + XtVaSetValues(mcdeProgBar, XmNvalue, pct, NULL); + (void) snprintf(text, sizeof text, "%.1f MB of %.1f MB", + (double) done / 1e6, (double) total / 1e6); + { + XmString s = XmStringCreateLocalized(text); + XtVaSetValues(mcdeProgText, XmNlabelString, s, NULL); + XmStringFree(s); + } + } + if (done >= total && mcdeProgShell != NULL) { + XtPopdown(mcdeProgShell); + XtDestroyWidget(mcdeProgShell); + mcdeProgShell = NULL; + } +} + #define PIPEMSG_PROCEED 102 #define PIPEMSG_MERGE 103 #define PIPEMSG_REPLACE_BUFFER 104 @@ -1874,6 +1991,21 @@ FileOpPipeCB( XtFree(target_file); target_file = NULL; break; + case PIPEMSG_PROGRESS: + { + unsigned long long done = 0, total = 0; + char *name = NULL; + + PipeRead(*fd, &done, sizeof done); + PipeRead(*fd, &total, sizeof total); + name = PipeReadString(*fd); + McdeProgressUpdate(cb_data != NULL && cb_data->file_view_data != + NULL ? cb_data->file_view_data->widget : toplevel, done, total, + name); + if (name != NULL) + XtFree(name); + break; + } case PIPEMSG_DONE: PipeRead(*fd, &rc, sizeof(int)); done = True; @@ -2237,6 +2369,7 @@ _FileMoveCopy( exit(rc); } + mcdeChildPid = pid; DBGFORK(("%s: forked child<%d>, m2s %d, s2m %d\n", pname, pid, pipe_m2s[1], pipe_s2m[0]));