Files
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

211 lines
5.5 KiB
C

/*
* WmSlideshow.c -- MCDE: backdrops from a picture file, and a folder of
* pictures that change by themselves
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* McdeBackdropFromFile loads a picture with McdeImageGetPixmap (libDtSvc:
* PNG through libpng, XPM, XBM and JPEG through Motif) and gives it to SetNewBackdrop
* (WmBackdrop.c), which scales it with Xrender for the styles Fit and Fill
* and keeps a copy of its own; the loaded original is then let go. A
* request "f.change_backdrop PATH 0 STYLE" (WmEvent.c) comes here: dtwm
* reads the file itself instead of borrowing a pixmap of the client.
*
* The slideshow: "INTERVAL RANDOM STYLE FOLDER" -- minutes (0: off), 1 for a
* random order, the backdrop style (DtWsmBackdropImageType), the folder.
* It comes from the resource Dtwm*mcdeSlideshow at the start (dtsession
* saved it with the other resources) and from the request "f.mcde_slideshow
* SPEC" when System Settings changes it. Every INTERVAL minutes the next
* picture of the folder becomes the backdrop of every workspace of the
* screen. The folder is read anew at each change, so pictures put into it
* take part without a restart.
*/
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include "WmGlobal.h"
#include "WmBackdrop.h"
#include "WmSlideshow.h"
#include <Xm/Xm.h>
#include <Dt/Wsm.h>
#include <Mcde/Image.h>
/* ---- one picture -------------------------------------------------------- */
void
McdeBackdropFromFile(WmWorkspaceData *pWS, const char *path, int imageType)
{
Screen *scr = XtScreen(pWS->workspaceTopLevelW);
Pixmap pm;
pm = McdeImageGetPixmap(scr, path, pWS->backdrop.foreground,
pWS->backdrop.background);
if (pm == XmUNSPECIFIED_PIXMAP)
return;
SetNewBackdrop(pWS, pm, (String) path,
(DtWsmBackdropImageType) imageType);
/* SetNewBackdrop made a pixmap of its own from it (WmBackdrop.c,
* ProcessBackdropResources with CHANGE_BACKDROP) */
if (pWS->backdrop.imagePixmap != pm)
McdeImageFreePixmap(scr, pm);
}
/* ---- the slideshow ------------------------------------------------------ */
static struct {
int interval; /* minutes; 0: off */
int random;
int style;
char folder[1024];
char last[256]; /* the picture shown last */
XtIntervalId timer;
} show;
static int
IsPicture(const char *name)
{
static const char *const ext[] = {
".jpg", ".jpeg", ".png", ".xpm", ".pm", ".xbm", ".bm", NULL
};
const char *dot = strrchr(name, '.');
int i;
if (name[0] == '.' || dot == NULL)
return 0;
for (i = 0; ext[i] != NULL; i++)
if (strcasecmp(dot, ext[i]) == 0)
return 1;
return 0;
}
static int
NameCompare(const void *a, const void *b)
{
return strcmp(*(char *const *) a, *(char *const *) b);
}
/* the next picture after show.last, or a random one other than it; into
* NAME. 0 when the folder has none. */
static int
NextPicture(char *name, size_t len)
{
DIR *d = opendir(show.folder);
struct dirent *de;
char **names = NULL;
int n = 0, cap = 0, i, pick = 0;
if (d == NULL)
return 0;
while ((de = readdir(d)) != NULL) {
if (!IsPicture(de->d_name))
continue;
if (n == cap) {
char **more = realloc(names,
(size_t) (cap = cap ? 2 * cap : 64) * sizeof *names);
if (more == NULL)
break;
names = more;
}
if ((names[n] = strdup(de->d_name)) != NULL)
n++;
}
closedir(d);
if (n == 0) {
free(names);
return 0;
}
qsort(names, (size_t) n, sizeof *names, NameCompare);
if (show.random && n > 1) {
do
pick = (int) (random() % n);
while (strcmp(names[pick], show.last) == 0);
} else {
for (i = 0; i < n; i++)
if (strcmp(names[i], show.last) > 0)
break;
pick = i < n ? i : 0;
}
snprintf(name, len, "%s", names[pick]);
for (i = 0; i < n; i++)
free(names[i]);
free(names);
return 1;
}
static void
ShowNext(void)
{
WmScreenData *pSD = ACTIVE_PSD;
char name[256], path[1400];
int i;
if (!NextPicture(name, sizeof name))
return;
snprintf(show.last, sizeof show.last, "%s", name);
snprintf(path, sizeof path, "%s/%s", show.folder, name);
for (i = 0; i < pSD->numWorkspaces; i++)
McdeBackdropFromFile(&pSD->pWS[i], path, show.style);
}
static void
Tick(XtPointer data, XtIntervalId *id)
{
(void) data;
(void) id;
show.timer = 0;
ShowNext();
if (show.interval > 0)
show.timer = XtAppAddTimeOut(wmGD.mwmAppContext,
(unsigned long) show.interval * 60000UL, Tick, NULL);
}
void
McdeSlideshowSet(const char *spec)
{
int interval = 0, random_ = 0, style = DtWSM_BACKDROP_IMAGETYPE_FIT;
int used = 0;
if (show.timer != 0) {
XtRemoveTimeOut(show.timer);
show.timer = 0;
}
show.interval = 0;
if (spec == NULL ||
sscanf(spec, "%d %d %d %n", &interval, &random_, &style, &used) < 3 ||
used == 0 || spec[used] == '\0')
return;
show.interval = interval > 0 ? interval : 0;
show.random = random_ != 0;
show.style = style;
snprintf(show.folder, sizeof show.folder, "%s", spec + used);
/* no blanks or line ends after the folder */
while (show.folder[0] != '\0' &&
strchr(" \t\r\n", show.folder[strlen(show.folder) - 1]) != NULL)
show.folder[strlen(show.folder) - 1] = '\0';
if (show.interval > 0)
Tick(NULL, NULL);
}
void
McdeSlideshowInit(void)
{
XrmDatabase db = XtDatabase(DISPLAY);
char *type = NULL;
XrmValue v;
char spec[1200];
srandom((unsigned int) time(NULL));
if (db == NULL || !XrmGetResource(db, "dtwm.mcdeSlideshow",
"Dtwm.McdeSlideshow", &type, &v) || v.addr == NULL)
return;
snprintf(spec, sizeof spec, "%.*s", (int) v.size, (char *) v.addr);
McdeSlideshowSet(spec);
}