Files
mcde/programs/dtbar/items.c
T
lotherkandClaude Opus 5.5 2c7ca200b0 mcde: dtbar's M menu: About This Computer, Favourites, Recent Items, Force Quit, power
The M menu has About This Computer (logo, system and version, processor,
memory, startup disk, More Info with machine, kernel, host name, disk space
and the desktop's version, all from mcde_sys_info), Software Update (greyed
until the update exists), System Settings, Favourites (~/.dt/favourites, with
Edit Favourites...) and Recent Items (~/.dt/recent, written when an item is
opened, with Clear Menu) -- items.c with its test (PASS test_items) --, Force
Quit (the programs with a window, XKillClient), Sleep, Restart..., Shut
Down... (McdeSys, the last two ask first), Lock Screen and Log Out <name>.
The bar's dialogs are centred on the screen (Motif centred them on the bar).
Tested on Xvfb: About, a favourite started and listed under Recent Items,
Force Quit ended dtcalc, the Shut Down question shows (not confirmed: on the
development host it could shut the computer down).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 08:03:48 +00:00

102 lines
2.1 KiB
C

/*
* items.c -- dtbar: the lists behind Favourites and Recent Items (items.h)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include "items.h"
static char *
Trim(char *s)
{
char *e;
while (*s == ' ' || *s == '\t')
s++;
e = s + strlen(s);
while (e > s && (e[-1] == '\n' || e[-1] == '\r' || e[-1] == ' ' ||
e[-1] == '\t'))
*--e = '\0';
return s;
}
int
ItemsRead(const char *file, Item *items, int max)
{
char line[1024], *s, *tab;
FILE *fp = fopen(file, "r");
int n = 0;
if (fp == NULL)
return 0;
while (n < max && fgets(line, sizeof line, fp) != NULL) {
/* split at the tab first: trimming would eat a leading one */
tab = strchr(line, '\t');
if (tab != NULL)
*tab = '\0';
s = Trim(line);
if (tab == NULL && (*s == '\0' || *s == '#'))
continue;
if (*s == '#')
continue;
memset(&items[n], 0, sizeof items[n]);
if (tab != NULL) {
snprintf(items[n].label, sizeof items[n].label, "%s", s);
snprintf(items[n].command, sizeof items[n].command, "%s",
Trim(tab + 1));
} else {
snprintf(items[n].label, sizeof items[n].label, "%s", s);
snprintf(items[n].command, sizeof items[n].command, "%s",
s);
}
if (items[n].label[0] != '\0' && items[n].command[0] != '\0')
n++;
}
fclose(fp);
return n;
}
int
ItemsWrite(const char *file, const Item *items, int n)
{
char tmp[1024];
FILE *fp;
int i;
if ((size_t) snprintf(tmp, sizeof tmp, "%s.new", file) >= sizeof tmp ||
(fp = fopen(tmp, "w")) == NULL)
return -1;
for (i = 0; i < n; i++)
fprintf(fp, "%s\t%s\n", items[i].label, items[i].command);
if (fclose(fp) != 0 || rename(tmp, file) != 0) {
remove(tmp);
return -1;
}
return 0;
}
int
ItemsPush(Item *items, int n, int max, const Item *item)
{
Item keep = *item;
int i;
for (i = 0; i < n; i++)
if (strcmp(items[i].command, item->command) == 0) {
memmove(&items[i], &items[i + 1],
(size_t) (n - i - 1) * sizeof *items);
n--;
break;
}
if (n >= max)
n = max - 1;
memmove(&items[1], &items[0], (size_t) n * sizeof *items);
items[0] = keep;
return n + 1;
}