mcde: dtlaunch, the launcher: hot key, programs, snippets, clipboard history, calculator
programs/dtlaunch: Alt+space (Dtlaunch*hotkey) opens a field and a list; matches programs in $PATH, ~/.dt/snippets (keyword TAB text), the clipboard history (polled CLIPBOARD, 50 entries, memory only) and calculations (=12*(3+4)). Return starts the program or puts the text on CLIPBOARD and PRIMARY. One per display (_MCDE_DTLAUNCH); a second start opens the first. C99/POSIX + Xt/Motif, no warnings with -Wpedantic. model.c (no X) with test_model.c: make check PASS. Started in every session by config/Xsession.d/0080.dtlaunch. Tested on Xvfb: snippet and result reach the clipboard (build/mcde-shots/dtlaunch-*.png). tools/mcde-linux.sh: check mode. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94f116c272
commit
07c3ce19f0
11 files changed
+1291
-2
No files matched your search
@@ -778,6 +778,7 @@ programs/dticon/Makefile
|
||||
programs/dtterm/Makefile
|
||||
|
||||
programs/dtcalc/Makefile
|
||||
programs/dtlaunch/Makefile
|
||||
|
||||
programs/dtaction/Makefile
|
||||
|
||||
|
||||
@@ -2531,6 +2531,7 @@ dt/bin/dticon
|
||||
dt/bin/dtimsstart
|
||||
dt/bin/dtinfo
|
||||
dt/bin/dtksh
|
||||
dt/bin/dtlaunch
|
||||
dt/bin/dtlogin
|
||||
dt/bin/dtlp
|
||||
dt/bin/dtlpsetup
|
||||
@@ -2596,6 +2597,7 @@ dt/config/Xsession.d/0010.dtpaths
|
||||
dt/config/Xsession.d/0020.dtims
|
||||
dt/config/Xsession.d/0030.dttmpdir
|
||||
dt/config/Xsession.d/0060.dtsysenv
|
||||
dt/config/Xsession.d/0080.dtlaunch
|
||||
dt/config/Xsetup
|
||||
dt/config/Xstartup
|
||||
dt/config/dtspcdenv
|
||||
@@ -2605,6 +2607,7 @@ dt/config/ims/start.conf
|
||||
dt/config/svc/CDE.lcx
|
||||
dt/config/sys.dtprofile
|
||||
dt/copyright
|
||||
dt/examples/dtlaunch/snippets.example
|
||||
dt/include/Dt/Action.h
|
||||
dt/include/Dt/ComboBox.h
|
||||
dt/include/Dt/Dnd.h
|
||||
|
||||
@@ -6,7 +6,7 @@ SUBDIRS = backdrops icons dsdm dtmail dtpad dtfile dtwm dtlogin \
|
||||
dtappbuilder palettes dtappintegrate dtprintegrate dtconfig \
|
||||
dtcreate dtprintinfo fontaliases dtdspmsg ttsnoop dtimsstart \
|
||||
dtpdm dtsr dtpdmd dtinfo types tttypes util dtopen localized \
|
||||
dthelp dtdocbook
|
||||
dthelp dtdocbook dtlaunch
|
||||
|
||||
install-data-hook:
|
||||
-if [ $(CDE_INSTALLATION_TOP)/bin != $(bindir) ];then \
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
MAINTAINERCLEANFILES = Makefile.in
|
||||
|
||||
# dtlaunch, MCDE's launcher (dtlaunch.c); model.c is the part without X and
|
||||
# has its own tests (make check).
|
||||
bin_PROGRAMS = dtlaunch
|
||||
|
||||
dtlaunch_SOURCES = dtlaunch.c model.c model.h
|
||||
dtlaunch_LDADD = $(XTOOLLIB) -lm
|
||||
|
||||
check_PROGRAMS = test_model
|
||||
test_model_SOURCES = test_model.c model.c model.h
|
||||
test_model_LDADD = -lm
|
||||
TESTS = test_model
|
||||
|
||||
# an example for ~/.dt/snippets
|
||||
exampledir = $(CDE_INSTALLATION_TOP)/examples/dtlaunch
|
||||
dist_example_DATA = snippets.example
|
||||
@@ -0,0 +1,654 @@
|
||||
/*
|
||||
* dtlaunch.c -- MCDE: the launcher
|
||||
*
|
||||
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
||||
*
|
||||
* One key (default Alt+space, resource Dtlaunch*hotkey) opens a field and a
|
||||
* list. What is typed is matched against the programs in $PATH, the snippets
|
||||
* of ~/.dt/snippets, the history of the clipboard, and read as a calculation.
|
||||
* Return starts the program, or puts the snippet, the clipboard entry or the
|
||||
* result on the clipboard (CLIPBOARD and PRIMARY), ready to paste; Escape
|
||||
* closes. Up and Down move in the list.
|
||||
*
|
||||
* The clipboard history: every second the owner of CLIPBOARD is asked for its
|
||||
* text (Xt selections, ICCCM); a new text goes to the top, at most HISTORY
|
||||
* entries, kept in memory only -- passwords pass through the clipboard.
|
||||
*
|
||||
* One launcher per display: the first one owns the selection _MCDE_DTLAUNCH;
|
||||
* `dtlaunch -show' asks that one to open and exits. -nograb: no hot key (for
|
||||
* tests); -show: open at once.
|
||||
*
|
||||
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md); the parts without X
|
||||
* are in model.c.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <X11/Intrinsic.h>
|
||||
#include <X11/StringDefs.h>
|
||||
#include <X11/Shell.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <X11/keysym.h>
|
||||
#include <Xm/Xm.h>
|
||||
#include <Xm/Form.h>
|
||||
#include <Xm/Frame.h>
|
||||
#include <Xm/List.h>
|
||||
#include <Xm/TextF.h>
|
||||
#include <Xm/Label.h>
|
||||
|
||||
#include "model.h"
|
||||
|
||||
#define HISTORY 50
|
||||
#define MAXITEMS 40
|
||||
#define MAXCLIP (64 * 1024)
|
||||
#define POLL_MS 1000
|
||||
|
||||
typedef enum { K_APP, K_SNIPPET, K_CLIP, K_CALC } Kind;
|
||||
|
||||
typedef struct {
|
||||
Kind kind;
|
||||
int score;
|
||||
char *label; /* what the list shows */
|
||||
char *value; /* program name, or the text for the clipboard */
|
||||
} Item;
|
||||
|
||||
static struct {
|
||||
XtAppContext app;
|
||||
Widget top, shell, field, list, hint;
|
||||
Display *dpy;
|
||||
Window root;
|
||||
Atom a_clipboard, a_utf8, a_targets, a_text, a_self,
|
||||
a_show;
|
||||
KeyCode hot_code;
|
||||
unsigned int hot_mods;
|
||||
char **programs;
|
||||
size_t nprograms;
|
||||
MxSnippet *snippets;
|
||||
size_t nsnippets;
|
||||
char *history[HISTORY];
|
||||
size_t nhistory;
|
||||
Item items[MAXITEMS];
|
||||
size_t nitems;
|
||||
char *owned; /* the text we offer as the clipboard */
|
||||
Boolean visible;
|
||||
} L;
|
||||
|
||||
typedef struct {
|
||||
String hotkey;
|
||||
} Resources;
|
||||
|
||||
static XtResource resources[] = {
|
||||
{ "hotkey", "Hotkey", XtRString, sizeof(String),
|
||||
XtOffsetOf(Resources, hotkey), XtRString, "Alt+space" },
|
||||
};
|
||||
|
||||
static XrmOptionDescRec options[] = {
|
||||
{ "-show", "*show", XrmoptionNoArg, "True" },
|
||||
{ "-nograb", "*nograb", XrmoptionNoArg, "True" },
|
||||
};
|
||||
|
||||
static String fallback[] = {
|
||||
"Dtlaunch*field.columns: 48",
|
||||
"Dtlaunch*list.visibleItemCount: 10",
|
||||
"Dtlaunch*hint.labelString: Type to find a program, a snippet, the clipboard, or =1+2",
|
||||
NULL
|
||||
};
|
||||
|
||||
static char *
|
||||
xstrdup(const char *s)
|
||||
{
|
||||
char *r = strdup(s);
|
||||
|
||||
if (r == NULL) {
|
||||
fputs("dtlaunch: out of memory\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/* a label for the list: one line, at most 70 characters */
|
||||
static char *
|
||||
oneline(const char *prefix, const char *text)
|
||||
{
|
||||
char buf[96];
|
||||
size_t i, j;
|
||||
|
||||
(void)snprintf(buf, sizeof buf, "%s", prefix);
|
||||
j = strlen(buf);
|
||||
for (i = 0; text[i] != '\0' && j < 70; i++)
|
||||
buf[j++] = (text[i] == '\n' || text[i] == '\t') ? ' ' : text[i];
|
||||
buf[j] = '\0';
|
||||
if (text[i] != '\0' && j + 3 < sizeof buf)
|
||||
(void)strcat(buf, "...");
|
||||
return xstrdup(buf);
|
||||
}
|
||||
|
||||
/* ---- the list ---------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
clear_items(void)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < L.nitems; i++) {
|
||||
free(L.items[i].label);
|
||||
free(L.items[i].value);
|
||||
}
|
||||
L.nitems = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
add_item(Kind kind, int score, char *label, const char *value)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (score <= 0) {
|
||||
free(label);
|
||||
return;
|
||||
}
|
||||
if (L.nitems == MAXITEMS) {
|
||||
/* full: replace the weakest, if this one is better */
|
||||
size_t w = 0;
|
||||
|
||||
for (i = 1; i < L.nitems; i++)
|
||||
if (L.items[i].score < L.items[w].score)
|
||||
w = i;
|
||||
if (L.items[w].score >= score) {
|
||||
free(label);
|
||||
return;
|
||||
}
|
||||
free(L.items[w].label);
|
||||
free(L.items[w].value);
|
||||
L.items[w] = L.items[--L.nitems];
|
||||
}
|
||||
L.items[L.nitems].kind = kind;
|
||||
L.items[L.nitems].score = score;
|
||||
L.items[L.nitems].label = label;
|
||||
L.items[L.nitems].value = xstrdup(value);
|
||||
L.nitems++;
|
||||
}
|
||||
|
||||
static int
|
||||
by_score(const void *a, const void *b)
|
||||
{
|
||||
const Item *x = a, *y = b;
|
||||
|
||||
if (x->score != y->score)
|
||||
return y->score - x->score;
|
||||
return strcmp(x->label, y->label);
|
||||
}
|
||||
|
||||
static void
|
||||
refresh(void)
|
||||
{
|
||||
char *q = XmTextFieldGetString(L.field);
|
||||
XmString *xs;
|
||||
double v;
|
||||
char num[64], buf[96];
|
||||
size_t i;
|
||||
|
||||
clear_items();
|
||||
if (mx_calc(q, &v) == 0) {
|
||||
mx_calc_format(v, num, sizeof num);
|
||||
(void)snprintf(buf, sizeof buf, "= %s", num);
|
||||
add_item(K_CALC, 1000, xstrdup(buf), num);
|
||||
}
|
||||
if (q[0] == '\0') {
|
||||
/* nothing typed: the clipboard history, newest first */
|
||||
for (i = 0; i < L.nhistory; i++)
|
||||
add_item(K_CLIP, 500 - (int)i,
|
||||
oneline("[clip] ", L.history[i]), L.history[i]);
|
||||
} else {
|
||||
for (i = 0; i < L.nsnippets; i++) {
|
||||
int s = mx_match(q, L.snippets[i].key);
|
||||
char pre[64];
|
||||
|
||||
if (s == 0)
|
||||
s = mx_match(q, L.snippets[i].text) / 2;
|
||||
(void)snprintf(pre, sizeof pre, "[snippet] %s: ",
|
||||
L.snippets[i].key);
|
||||
add_item(K_SNIPPET, s ? s + 5 : 0,
|
||||
oneline(pre, L.snippets[i].text),
|
||||
L.snippets[i].text);
|
||||
}
|
||||
for (i = 0; i < L.nhistory; i++)
|
||||
add_item(K_CLIP, mx_match(q, L.history[i]) / 2,
|
||||
oneline("[clip] ", L.history[i]), L.history[i]);
|
||||
for (i = 0; i < L.nprograms; i++)
|
||||
add_item(K_APP, mx_match(q, L.programs[i]),
|
||||
xstrdup(L.programs[i]), L.programs[i]);
|
||||
}
|
||||
XtFree(q);
|
||||
qsort(L.items, L.nitems, sizeof L.items[0], by_score);
|
||||
|
||||
xs = (XmString *)XtMalloc((Cardinal)((L.nitems + 1) * sizeof *xs));
|
||||
for (i = 0; i < L.nitems; i++)
|
||||
xs[i] = XmStringCreateLocalized(L.items[i].label);
|
||||
XtVaSetValues(L.list, XmNitems, xs, XmNitemCount, (int)L.nitems, NULL);
|
||||
for (i = 0; i < L.nitems; i++)
|
||||
XmStringFree(xs[i]);
|
||||
XtFree((char *)xs);
|
||||
if (L.nitems > 0)
|
||||
XmListSelectPos(L.list, 1, False);
|
||||
}
|
||||
|
||||
/* ---- the clipboard: offering text, and reading the history ------------- */
|
||||
|
||||
static Boolean
|
||||
convert(Widget w, Atom *selection, Atom *target, Atom *type,
|
||||
XtPointer *value, unsigned long *length, int *format)
|
||||
{
|
||||
(void)w; (void)selection;
|
||||
if (L.owned == NULL)
|
||||
return False;
|
||||
if (*target == L.a_targets) {
|
||||
Atom *t = (Atom *)XtMalloc(4 * sizeof(Atom));
|
||||
|
||||
t[0] = L.a_targets;
|
||||
t[1] = L.a_utf8;
|
||||
t[2] = XA_STRING;
|
||||
t[3] = L.a_text;
|
||||
*type = XA_ATOM;
|
||||
*value = (XtPointer)t;
|
||||
*length = 4;
|
||||
*format = 32;
|
||||
return True;
|
||||
}
|
||||
if (*target == L.a_utf8 || *target == XA_STRING ||
|
||||
*target == L.a_text) {
|
||||
*type = *target == L.a_text ? L.a_utf8 : *target;
|
||||
*value = XtNewString(L.owned);
|
||||
*length = strlen(L.owned);
|
||||
*format = 8;
|
||||
return True;
|
||||
}
|
||||
return False;
|
||||
}
|
||||
|
||||
static void
|
||||
offer(const char *text)
|
||||
{
|
||||
Time t = XtLastTimestampProcessed(L.dpy);
|
||||
|
||||
free(L.owned);
|
||||
L.owned = xstrdup(text);
|
||||
(void)XtOwnSelection(L.top, L.a_clipboard, t, convert, NULL, NULL);
|
||||
(void)XtOwnSelection(L.top, XA_PRIMARY, t, convert, NULL, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
remember(const char *text)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (text[0] == '\0' || strlen(text) > MAXCLIP)
|
||||
return;
|
||||
for (i = 0; i < L.nhistory; i++)
|
||||
if (strcmp(L.history[i], text) == 0)
|
||||
break;
|
||||
if (i == 0 && L.nhistory > 0)
|
||||
return; /* already the newest */
|
||||
if (i < L.nhistory) { /* move it to the top */
|
||||
char *s = L.history[i];
|
||||
|
||||
memmove(&L.history[1], &L.history[0], i * sizeof(char *));
|
||||
L.history[0] = s;
|
||||
return;
|
||||
}
|
||||
if (L.nhistory == HISTORY)
|
||||
free(L.history[--L.nhistory]);
|
||||
memmove(&L.history[1], &L.history[0], L.nhistory * sizeof(char *));
|
||||
L.history[0] = xstrdup(text);
|
||||
L.nhistory++;
|
||||
}
|
||||
|
||||
static void
|
||||
got_clipboard(Widget w, XtPointer client, Atom *selection, Atom *type,
|
||||
XtPointer value, unsigned long *length, int *format)
|
||||
{
|
||||
(void)w; (void)client; (void)selection;
|
||||
if (value != NULL && *format == 8 &&
|
||||
(*type == L.a_utf8 || *type == XA_STRING)) {
|
||||
char *s = XtMalloc((Cardinal)(*length + 1));
|
||||
|
||||
memcpy(s, value, *length);
|
||||
s[*length] = '\0';
|
||||
remember(s);
|
||||
XtFree(s);
|
||||
}
|
||||
XtFree((char *)value);
|
||||
}
|
||||
|
||||
static void
|
||||
poll_clipboard(XtPointer client, XtIntervalId *id)
|
||||
{
|
||||
Window owner = XGetSelectionOwner(L.dpy, L.a_clipboard);
|
||||
|
||||
(void)client; (void)id;
|
||||
if (owner != None && owner != XtWindow(L.top))
|
||||
XtGetSelectionValue(L.top, L.a_clipboard, L.a_utf8,
|
||||
got_clipboard, NULL, XtLastTimestampProcessed(L.dpy));
|
||||
(void)XtAppAddTimeOut(L.app, POLL_MS, poll_clipboard, NULL);
|
||||
}
|
||||
|
||||
/* ---- showing, hiding, doing -------------------------------------------- */
|
||||
|
||||
static void
|
||||
load_sources(void)
|
||||
{
|
||||
const char *home = getenv("HOME");
|
||||
char path[1024];
|
||||
|
||||
mx_strv_free(L.programs, L.nprograms);
|
||||
(void)mx_path_programs(getenv("PATH"), &L.programs, &L.nprograms);
|
||||
mx_snippets_free(L.snippets, L.nsnippets);
|
||||
(void)snprintf(path, sizeof path, "%s/.dt/snippets",
|
||||
home != NULL ? home : "");
|
||||
if (mx_snippets_load(path, &L.snippets, &L.nsnippets) != 0) {
|
||||
L.snippets = NULL;
|
||||
L.nsnippets = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
show(void)
|
||||
{
|
||||
Dimension w, h;
|
||||
Screen *scr = XtScreen(L.top);
|
||||
|
||||
load_sources(); /* PATH and snippets may have changed */
|
||||
XmTextFieldSetString(L.field, "");
|
||||
refresh();
|
||||
if (!XtIsRealized(L.shell))
|
||||
XtRealizeWidget(L.shell);
|
||||
XtVaGetValues(L.shell, XmNwidth, &w, XmNheight, &h, NULL);
|
||||
XtVaSetValues(L.shell,
|
||||
XmNx, (Position)((WidthOfScreen(scr) - w) / 2),
|
||||
XmNy, (Position)(HeightOfScreen(scr) / 4), NULL);
|
||||
XtPopup(L.shell, XtGrabNone);
|
||||
XRaiseWindow(L.dpy, XtWindow(L.shell));
|
||||
/* override-redirect: no window manager gives it the focus */
|
||||
XSetInputFocus(L.dpy, XtWindow(L.shell), RevertToParent, CurrentTime);
|
||||
XmProcessTraversal(L.field, XmTRAVERSE_CURRENT);
|
||||
L.visible = True;
|
||||
}
|
||||
|
||||
static void
|
||||
hide(void)
|
||||
{
|
||||
if (L.visible)
|
||||
XtPopdown(L.shell);
|
||||
L.visible = False;
|
||||
}
|
||||
|
||||
static void
|
||||
run(const char *program)
|
||||
{
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
/* a session of its own, so the program outlives the launcher */
|
||||
(void)setsid();
|
||||
execlp(program, program, (char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
choose(int pos)
|
||||
{
|
||||
Item *it;
|
||||
|
||||
if (pos < 1 || (size_t)pos > L.nitems)
|
||||
return;
|
||||
it = &L.items[pos - 1];
|
||||
if (it->kind == K_APP)
|
||||
run(it->value);
|
||||
else
|
||||
offer(it->value);
|
||||
hide();
|
||||
}
|
||||
|
||||
static int
|
||||
selected_pos(void)
|
||||
{
|
||||
int *pos, n, p = 1;
|
||||
|
||||
if (XmListGetSelectedPos(L.list, &pos, &n)) {
|
||||
p = pos[0];
|
||||
XtFree((char *)pos);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void
|
||||
field_changed(Widget w, XtPointer client, XtPointer call)
|
||||
{
|
||||
(void)w; (void)client; (void)call;
|
||||
refresh();
|
||||
}
|
||||
|
||||
static void
|
||||
field_activate(Widget w, XtPointer client, XtPointer call)
|
||||
{
|
||||
(void)w; (void)client; (void)call;
|
||||
choose(selected_pos());
|
||||
}
|
||||
|
||||
static void
|
||||
list_activate(Widget w, XtPointer client, XtPointer call)
|
||||
{
|
||||
XmListCallbackStruct *cbs = (XmListCallbackStruct *)call;
|
||||
|
||||
(void)w; (void)client;
|
||||
choose(cbs->item_position);
|
||||
}
|
||||
|
||||
/* Up, Down and Escape in the field */
|
||||
static void
|
||||
field_key(Widget w, XtPointer client, XEvent *ev, Boolean *cont)
|
||||
{
|
||||
KeySym ks = XLookupKeysym(&ev->xkey, 0);
|
||||
int p = selected_pos();
|
||||
|
||||
(void)w; (void)client;
|
||||
if (ks == XK_Escape) {
|
||||
hide();
|
||||
*cont = False;
|
||||
} else if (ks == XK_Down && (size_t)p < L.nitems) {
|
||||
XmListSelectPos(L.list, p + 1, False);
|
||||
XmListSetBottomPos(L.list, p + 1 > 10 ? p + 1 : 10);
|
||||
*cont = False;
|
||||
} else if (ks == XK_Up && p > 1) {
|
||||
XmListSelectPos(L.list, p - 1, False);
|
||||
*cont = False;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- the hot key ------------------------------------------------------- */
|
||||
|
||||
static int
|
||||
parse_hotkey(const char *spec)
|
||||
{
|
||||
char buf[128], *tok, *save = NULL;
|
||||
KeySym ks = NoSymbol;
|
||||
|
||||
L.hot_mods = 0;
|
||||
(void)snprintf(buf, sizeof buf, "%s", spec);
|
||||
for (tok = strtok_r(buf, "+", &save); tok != NULL;
|
||||
tok = strtok_r(NULL, "+", &save)) {
|
||||
if (strcmp(tok, "Alt") == 0 || strcmp(tok, "Meta") == 0)
|
||||
L.hot_mods |= Mod1Mask;
|
||||
else if (strcmp(tok, "Ctrl") == 0 || strcmp(tok, "Control") == 0)
|
||||
L.hot_mods |= ControlMask;
|
||||
else if (strcmp(tok, "Shift") == 0)
|
||||
L.hot_mods |= ShiftMask;
|
||||
else if (strcmp(tok, "Super") == 0)
|
||||
L.hot_mods |= Mod4Mask;
|
||||
else
|
||||
ks = XStringToKeysym(tok);
|
||||
}
|
||||
if (ks == NoSymbol || (L.hot_code = XKeysymToKeycode(L.dpy, ks)) == 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
grab_hotkey(void)
|
||||
{
|
||||
/* with and without Caps Lock (Lock) and Num Lock (Mod2) */
|
||||
static const unsigned int locks[] = { 0, LockMask, Mod2Mask,
|
||||
LockMask | Mod2Mask };
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof locks / sizeof locks[0]; i++)
|
||||
XGrabKey(L.dpy, L.hot_code, L.hot_mods | locks[i], L.root,
|
||||
False, GrabModeAsync, GrabModeAsync);
|
||||
}
|
||||
|
||||
/* ---- main -------------------------------------------------------------- */
|
||||
|
||||
static Widget
|
||||
build(void)
|
||||
{
|
||||
Widget form, frame;
|
||||
Arg a[12];
|
||||
int n;
|
||||
|
||||
n = 0;
|
||||
XtSetArg(a[n], XmNoverrideRedirect, True); n++;
|
||||
XtSetArg(a[n], XmNallowShellResize, True); n++;
|
||||
L.shell = XtCreatePopupShell("launcher", topLevelShellWidgetClass,
|
||||
L.top, a, n);
|
||||
n = 0;
|
||||
XtSetArg(a[n], XmNshadowType, XmSHADOW_OUT); n++;
|
||||
XtSetArg(a[n], XmNshadowThickness, 2); n++;
|
||||
frame = XmCreateFrame(L.shell, "frame", a, n);
|
||||
n = 0;
|
||||
XtSetArg(a[n], XmNmarginWidth, 8); n++;
|
||||
XtSetArg(a[n], XmNmarginHeight, 8); n++;
|
||||
XtSetArg(a[n], XmNverticalSpacing, 6); n++;
|
||||
form = XmCreateForm(frame, "form", a, n);
|
||||
|
||||
n = 0;
|
||||
XtSetArg(a[n], XmNtopAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(a[n], XmNleftAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(a[n], XmNrightAttachment, XmATTACH_FORM); n++;
|
||||
L.field = XmCreateTextField(form, "field", a, n);
|
||||
XtAddCallback(L.field, XmNvalueChangedCallback, field_changed, NULL);
|
||||
XtAddCallback(L.field, XmNactivateCallback, field_activate, NULL);
|
||||
XtInsertEventHandler(L.field, KeyPressMask, False, field_key, NULL,
|
||||
XtListHead);
|
||||
|
||||
n = 0;
|
||||
XtSetArg(a[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
|
||||
XtSetArg(a[n], XmNtopWidget, L.field); n++;
|
||||
XtSetArg(a[n], XmNleftAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(a[n], XmNrightAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
|
||||
XtSetArg(a[n], XmNtraversalOn, False); n++;
|
||||
L.list = XmCreateScrolledList(form, "list", a, n);
|
||||
XtAddCallback(L.list, XmNdefaultActionCallback, list_activate, NULL);
|
||||
|
||||
n = 0;
|
||||
XtSetArg(a[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
|
||||
XtSetArg(a[n], XmNtopWidget, XtParent(L.list)); n++;
|
||||
XtSetArg(a[n], XmNleftAttachment, XmATTACH_FORM); n++;
|
||||
XtSetArg(a[n], XmNbottomAttachment, XmATTACH_FORM); n++;
|
||||
L.hint = XmCreateLabel(form, "hint", a, n);
|
||||
|
||||
XtManageChild(L.field);
|
||||
XtManageChild(L.list);
|
||||
XtManageChild(L.hint);
|
||||
XtManageChild(form);
|
||||
XtManageChild(frame);
|
||||
return L.shell;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
Resources res;
|
||||
XEvent ev;
|
||||
String s;
|
||||
Boolean show_now, nograb;
|
||||
|
||||
(void)signal(SIGCHLD, SIG_IGN); /* started programs: no zombies */
|
||||
XtSetLanguageProc(NULL, NULL, NULL);
|
||||
L.top = XtVaAppInitialize(&L.app, "Dtlaunch", options,
|
||||
XtNumber(options), &argc, argv, fallback,
|
||||
XmNmappedWhenManaged, False, XmNwidth, 1, XmNheight, 1, NULL);
|
||||
L.dpy = XtDisplay(L.top);
|
||||
L.root = DefaultRootWindow(L.dpy);
|
||||
L.a_clipboard = XInternAtom(L.dpy, "CLIPBOARD", False);
|
||||
L.a_utf8 = XInternAtom(L.dpy, "UTF8_STRING", False);
|
||||
L.a_targets = XInternAtom(L.dpy, "TARGETS", False);
|
||||
L.a_text = XInternAtom(L.dpy, "TEXT", False);
|
||||
L.a_self = XInternAtom(L.dpy, "_MCDE_DTLAUNCH", False);
|
||||
L.a_show = XInternAtom(L.dpy, "_MCDE_DTLAUNCH_SHOW", False);
|
||||
XtGetApplicationResources(L.top, &res, resources, XtNumber(resources),
|
||||
NULL, 0);
|
||||
s = NULL;
|
||||
show_now = XrmGetResource(XtDatabase(L.dpy), "dtlaunch.show",
|
||||
"Dtlaunch.Show", &s, &(XrmValue){ 0, NULL }) ? True : False;
|
||||
nograb = XrmGetResource(XtDatabase(L.dpy), "dtlaunch.nograb",
|
||||
"Dtlaunch.Nograb", &s, &(XrmValue){ 0, NULL }) ? True : False;
|
||||
XtRealizeWidget(L.top);
|
||||
|
||||
/* one launcher per display: ask the running one to open */
|
||||
{
|
||||
Window other = XGetSelectionOwner(L.dpy, L.a_self);
|
||||
|
||||
if (other != None) {
|
||||
XEvent cm;
|
||||
|
||||
memset(&cm, 0, sizeof cm);
|
||||
cm.xclient.type = ClientMessage;
|
||||
cm.xclient.window = other;
|
||||
cm.xclient.message_type = L.a_show;
|
||||
cm.xclient.format = 32;
|
||||
XSendEvent(L.dpy, other, False, NoEventMask, &cm);
|
||||
XFlush(L.dpy);
|
||||
return 0;
|
||||
}
|
||||
XSetSelectionOwner(L.dpy, L.a_self, XtWindow(L.top),
|
||||
CurrentTime);
|
||||
}
|
||||
|
||||
build();
|
||||
if (!nograb) {
|
||||
if (parse_hotkey(res.hotkey) == 0)
|
||||
grab_hotkey();
|
||||
else
|
||||
fprintf(stderr, "dtlaunch: hot key \"%s\" not understood\n",
|
||||
res.hotkey);
|
||||
}
|
||||
(void)XtAppAddTimeOut(L.app, POLL_MS, poll_clipboard, NULL);
|
||||
if (show_now)
|
||||
show();
|
||||
|
||||
/* the hot key and the message from a second launcher arrive at
|
||||
* windows Xt does not know (the root) or not for a widget */
|
||||
for (;;) {
|
||||
XtAppNextEvent(L.app, &ev);
|
||||
if (ev.type == KeyPress && ev.xkey.window == L.root &&
|
||||
ev.xkey.keycode == L.hot_code) {
|
||||
if (L.visible)
|
||||
hide();
|
||||
else
|
||||
show();
|
||||
continue;
|
||||
}
|
||||
if (ev.type == ClientMessage &&
|
||||
ev.xclient.message_type == L.a_show) {
|
||||
show();
|
||||
continue;
|
||||
}
|
||||
(void)XtDispatchEvent(&ev);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,349 @@
|
||||
/*
|
||||
* model.c -- dtlaunch without X: matching, the calculator, snippets, programs
|
||||
*
|
||||
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "model.h"
|
||||
|
||||
/* ---- matching ---------------------------------------------------------- */
|
||||
|
||||
int
|
||||
mx_match(const char *query, const char *text)
|
||||
{
|
||||
const char *t = text;
|
||||
int score = 0, run = 0, first = 1;
|
||||
|
||||
if (*query == '\0')
|
||||
return 1;
|
||||
for (; *query != '\0'; query++) {
|
||||
int q = tolower((unsigned char)*query);
|
||||
int adjacent = 1;
|
||||
|
||||
if (q == ' ')
|
||||
continue;
|
||||
while (*t != '\0' && tolower((unsigned char)*t) != q) {
|
||||
t++;
|
||||
adjacent = 0;
|
||||
}
|
||||
if (*t == '\0')
|
||||
return 0;
|
||||
if (first && t == text)
|
||||
score += 20; /* the very start */
|
||||
else if (t > text && !isalnum((unsigned char)t[-1]))
|
||||
score += 8; /* the start of a word */
|
||||
run = adjacent && !first ? run + 1 : 0;
|
||||
score += 1 + 3 * run; /* letters in a row */
|
||||
first = 0;
|
||||
t++;
|
||||
}
|
||||
/* a shorter text is the better match for the same letters */
|
||||
score += 10 - (int)(strlen(text) > 10 ? 10 : strlen(text));
|
||||
return score > 0 ? score : 1;
|
||||
}
|
||||
|
||||
/* ---- the calculator: recursive descent --------------------------------- */
|
||||
|
||||
typedef struct {
|
||||
const char *p;
|
||||
int error;
|
||||
} Calc;
|
||||
|
||||
static double expr(Calc *c);
|
||||
|
||||
static void
|
||||
skip(Calc *c)
|
||||
{
|
||||
while (*c->p == ' ' || *c->p == '\t')
|
||||
c->p++;
|
||||
}
|
||||
|
||||
static double
|
||||
primary(Calc *c)
|
||||
{
|
||||
double v;
|
||||
char *end;
|
||||
|
||||
skip(c);
|
||||
if (*c->p == '(') {
|
||||
c->p++;
|
||||
v = expr(c);
|
||||
skip(c);
|
||||
if (*c->p != ')')
|
||||
c->error = 1;
|
||||
else
|
||||
c->p++;
|
||||
return v;
|
||||
}
|
||||
if (*c->p == '-') {
|
||||
c->p++;
|
||||
return -primary(c);
|
||||
}
|
||||
if (*c->p == '+') {
|
||||
c->p++;
|
||||
return primary(c);
|
||||
}
|
||||
if (!isdigit((unsigned char)*c->p) && *c->p != '.') {
|
||||
c->error = 1;
|
||||
return 0;
|
||||
}
|
||||
errno = 0;
|
||||
v = strtod(c->p, &end);
|
||||
if (end == c->p || errno != 0)
|
||||
c->error = 1;
|
||||
c->p = end;
|
||||
return v;
|
||||
}
|
||||
|
||||
static double
|
||||
power(Calc *c)
|
||||
{
|
||||
double b = primary(c);
|
||||
|
||||
skip(c);
|
||||
if (*c->p == '^') {
|
||||
c->p++;
|
||||
return pow(b, power(c)); /* right associative */
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
static double
|
||||
term(Calc *c)
|
||||
{
|
||||
double v = power(c), r;
|
||||
|
||||
for (;;) {
|
||||
skip(c);
|
||||
if (*c->p == '*' || *c->p == '/' || *c->p == '%') {
|
||||
char op = *c->p++;
|
||||
|
||||
r = power(c);
|
||||
if (op == '*')
|
||||
v *= r;
|
||||
else if (r == 0)
|
||||
c->error = 1;
|
||||
else
|
||||
v = op == '/' ? v / r : fmod(v, r);
|
||||
} else
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
static double
|
||||
expr(Calc *c)
|
||||
{
|
||||
double v = term(c);
|
||||
|
||||
for (;;) {
|
||||
skip(c);
|
||||
if (*c->p == '+') {
|
||||
c->p++;
|
||||
v += term(c);
|
||||
} else if (*c->p == '-') {
|
||||
c->p++;
|
||||
v -= term(c);
|
||||
} else
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
mx_calc(const char *s, double *out)
|
||||
{
|
||||
Calc c;
|
||||
double v;
|
||||
int ops = 0;
|
||||
const char *q;
|
||||
|
||||
while (*s == ' ')
|
||||
s++;
|
||||
if (*s == '=')
|
||||
s++;
|
||||
else {
|
||||
/* without '=', only text that is clearly a calculation counts:
|
||||
* digits and at least one operator, nothing else */
|
||||
for (q = s; *q != '\0'; q++) {
|
||||
if (strchr("+-*/%^", *q) != NULL && q != s)
|
||||
ops++;
|
||||
else if (!isdigit((unsigned char)*q) &&
|
||||
strchr(" .()+-", *q) == NULL)
|
||||
return -1;
|
||||
}
|
||||
if (ops == 0)
|
||||
return -1;
|
||||
}
|
||||
c.p = s;
|
||||
c.error = 0;
|
||||
v = expr(&c);
|
||||
skip(&c);
|
||||
if (c.error || *c.p != '\0' || isnan(v) || isinf(v))
|
||||
return -1;
|
||||
*out = v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
mx_calc_format(double v, char *buf, size_t len)
|
||||
{
|
||||
char *p;
|
||||
|
||||
(void)snprintf(buf, len, "%.10f", v);
|
||||
if (strchr(buf, '.') == NULL)
|
||||
return;
|
||||
p = buf + strlen(buf) - 1;
|
||||
while (p > buf && *p == '0')
|
||||
*p-- = '\0';
|
||||
if (*p == '.')
|
||||
*p = '\0';
|
||||
if (strcmp(buf, "-0") == 0)
|
||||
(void)snprintf(buf, len, "0");
|
||||
}
|
||||
|
||||
/* ---- snippets ---------------------------------------------------------- */
|
||||
|
||||
char *
|
||||
mx_unescape(const char *s)
|
||||
{
|
||||
char *r = malloc(strlen(s) + 1), *d = r;
|
||||
|
||||
if (r == NULL)
|
||||
return NULL;
|
||||
for (; *s != '\0'; s++) {
|
||||
if (*s == '\\' && s[1] != '\0') {
|
||||
s++;
|
||||
*d++ = *s == 'n' ? '\n' : *s == 't' ? '\t' : *s;
|
||||
} else
|
||||
*d++ = *s;
|
||||
}
|
||||
*d = '\0';
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
mx_snippets_load(const char *path, MxSnippet **out, size_t *n)
|
||||
{
|
||||
FILE *f;
|
||||
char line[4096], *tab;
|
||||
MxSnippet *v = NULL, *nv;
|
||||
size_t cnt = 0;
|
||||
|
||||
*out = NULL;
|
||||
*n = 0;
|
||||
if ((f = fopen(path, "r")) == NULL)
|
||||
return errno == ENOENT ? 0 : -1;
|
||||
while (fgets(line, sizeof line, f) != NULL) {
|
||||
line[strcspn(line, "\r\n")] = '\0';
|
||||
if (line[0] == '#' || (tab = strchr(line, '\t')) == NULL ||
|
||||
tab == line)
|
||||
continue;
|
||||
*tab = '\0';
|
||||
if ((nv = realloc(v, (cnt + 1) * sizeof *v)) == NULL)
|
||||
break;
|
||||
v = nv;
|
||||
v[cnt].key = strdup(line);
|
||||
v[cnt].text = mx_unescape(tab + 1);
|
||||
if (v[cnt].key == NULL || v[cnt].text == NULL) {
|
||||
free(v[cnt].key);
|
||||
free(v[cnt].text);
|
||||
break;
|
||||
}
|
||||
cnt++;
|
||||
}
|
||||
(void)fclose(f);
|
||||
*out = v;
|
||||
*n = cnt;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
mx_snippets_free(MxSnippet *s, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
free(s[i].key);
|
||||
free(s[i].text);
|
||||
}
|
||||
free(s);
|
||||
}
|
||||
|
||||
/* ---- programs in $PATH ------------------------------------------------- */
|
||||
|
||||
static int
|
||||
cmpstr(const void *a, const void *b)
|
||||
{
|
||||
return strcmp(*(char *const *)a, *(char *const *)b);
|
||||
}
|
||||
|
||||
int
|
||||
mx_path_programs(const char *path, char ***out, size_t *n)
|
||||
{
|
||||
char *copy, *dir, *save = NULL, file[4096];
|
||||
char **v = NULL, **nv;
|
||||
size_t cnt = 0, cap = 0, i, j;
|
||||
struct dirent *de;
|
||||
struct stat st;
|
||||
DIR *d;
|
||||
|
||||
*out = NULL;
|
||||
*n = 0;
|
||||
if (path == NULL || (copy = strdup(path)) == NULL)
|
||||
return 0;
|
||||
for (dir = strtok_r(copy, ":", &save); dir != NULL;
|
||||
dir = strtok_r(NULL, ":", &save)) {
|
||||
if ((d = opendir(dir)) == NULL)
|
||||
continue;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.')
|
||||
continue;
|
||||
(void)snprintf(file, sizeof file, "%s/%s", dir,
|
||||
de->d_name);
|
||||
if (stat(file, &st) != 0 || !S_ISREG(st.st_mode) ||
|
||||
access(file, X_OK) != 0)
|
||||
continue;
|
||||
if (cnt == cap) {
|
||||
cap = cap ? cap * 2 : 256;
|
||||
if ((nv = realloc(v, cap * sizeof *v)) == NULL)
|
||||
break;
|
||||
v = nv;
|
||||
}
|
||||
if ((v[cnt] = strdup(de->d_name)) != NULL)
|
||||
cnt++;
|
||||
}
|
||||
(void)closedir(d);
|
||||
}
|
||||
free(copy);
|
||||
if (cnt > 1)
|
||||
qsort(v, cnt, sizeof *v, cmpstr);
|
||||
/* duplicates: the first one in $PATH wins, the name is the same */
|
||||
for (i = j = 0; i < cnt; i++) {
|
||||
if (j > 0 && strcmp(v[i], v[j - 1]) == 0)
|
||||
free(v[i]);
|
||||
else
|
||||
v[j++] = v[i];
|
||||
}
|
||||
*out = v;
|
||||
*n = j;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
mx_strv_free(char **v, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
free(v[i]);
|
||||
free(v);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* model.h -- dtlaunch without X: matching, the calculator, snippets, programs
|
||||
*
|
||||
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
||||
*
|
||||
* C99 and POSIX only (ext/src/mcde/AGENTS.md); tested by test_model.c.
|
||||
*/
|
||||
|
||||
#ifndef DTLAUNCH_MODEL_H
|
||||
#define DTLAUNCH_MODEL_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
* How well QUERY matches TEXT, case-insensitively: 0 for no match, otherwise
|
||||
* a score that is higher for a match at the start, for a match at the start
|
||||
* of a word, and for letters that follow each other. Every letter of QUERY
|
||||
* must appear in TEXT in this order.
|
||||
*/
|
||||
int mx_match(const char *query, const char *text);
|
||||
|
||||
/*
|
||||
* Evaluate an arithmetic expression: numbers, + - * / % ^, parentheses,
|
||||
* unary minus. A leading '=' is ignored. Returns 0 and sets *out, or -1
|
||||
* if the text is not an expression (or divides by zero).
|
||||
*/
|
||||
int mx_calc(const char *expr, double *out);
|
||||
/* format a result the way a person writes it: no trailing zeros */
|
||||
void mx_calc_format(double v, char *buf, size_t len);
|
||||
|
||||
/*
|
||||
* Snippets: a text file, one snippet a line, "keyword<TAB>text"; in the text
|
||||
* \n is a newline, \t a tab and \\ a backslash. Lines starting with '#' and
|
||||
* lines without a tab are skipped.
|
||||
*/
|
||||
typedef struct {
|
||||
char *key;
|
||||
char *text;
|
||||
} MxSnippet;
|
||||
|
||||
int mx_snippets_load(const char *path, MxSnippet **out, size_t *n);
|
||||
void mx_snippets_free(MxSnippet *s, size_t n);
|
||||
/* the escapes above, decoded; the result is malloc'd */
|
||||
char *mx_unescape(const char *s);
|
||||
|
||||
/*
|
||||
* The programs in $PATH (executable regular files), sorted, without
|
||||
* duplicates. Returns 0; *out is a malloc'd array of malloc'd names.
|
||||
*/
|
||||
int mx_path_programs(const char *path, char ***out, size_t *n);
|
||||
void mx_strv_free(char **v, size_t n);
|
||||
|
||||
#endif /* DTLAUNCH_MODEL_H */
|
||||
@@ -0,0 +1,8 @@
|
||||
# ~/.dt/snippets -- text snippets for dtlaunch (MCDE)
|
||||
#
|
||||
# One snippet a line: the keyword, a TAB, the text. In the text \n is a
|
||||
# newline, \t a tab, \\ a backslash. Type the keyword in the launcher and
|
||||
# press Return: the text is on the clipboard, ready to paste.
|
||||
#
|
||||
sig -- \nsent from Makronix
|
||||
date YYYY-MM-DD
|
||||
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
* test_model.c -- the tests of model.c (make check)
|
||||
*
|
||||
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "model.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static int
|
||||
calc_is(const char *e, double want)
|
||||
{
|
||||
double v;
|
||||
|
||||
return mx_calc(e, &v) == 0 && v > want - 1e-9 && v < want + 1e-9;
|
||||
}
|
||||
|
||||
static int
|
||||
calc_fails(const char *e)
|
||||
{
|
||||
double v;
|
||||
|
||||
return mx_calc(e, &v) != 0;
|
||||
}
|
||||
|
||||
static void
|
||||
test_match(void)
|
||||
{
|
||||
CHECK(mx_match("", "anything") > 0);
|
||||
CHECK(mx_match("dtf", "dtfile") > 0);
|
||||
CHECK(mx_match("DTF", "dtfile") > 0);
|
||||
CHECK(mx_match("xyz", "dtfile") == 0);
|
||||
CHECK(mx_match("fd", "dtfile") == 0); /* order matters */
|
||||
/* the start beats the middle, a word start beats a letter inside */
|
||||
CHECK(mx_match("term", "dtterm") < mx_match("term", "terminal"));
|
||||
CHECK(mx_match("fm", "file manager") > mx_match("fm", "pathfmt"));
|
||||
/* letters in a row beat scattered ones */
|
||||
CHECK(mx_match("cal", "calendar") > mx_match("cal", "cxaxl"));
|
||||
}
|
||||
|
||||
static void
|
||||
test_calc(void)
|
||||
{
|
||||
char buf[64];
|
||||
|
||||
CHECK(calc_is("1+2", 3));
|
||||
CHECK(calc_is("=2*3+4", 10));
|
||||
CHECK(calc_is("2*(3+4)", 14));
|
||||
CHECK(calc_is("= -3 + 5", 2));
|
||||
CHECK(calc_is("2^3^2", 512)); /* right associative */
|
||||
CHECK(calc_is("7 % 4", 3));
|
||||
CHECK(calc_is("=10/4", 2.5));
|
||||
CHECK(calc_fails("1/0"));
|
||||
CHECK(calc_fails("12")); /* a number is no calculation */
|
||||
CHECK(calc_fails("firefox"));
|
||||
CHECK(calc_fails("=2+"));
|
||||
CHECK(calc_fails("=(1+2"));
|
||||
CHECK(calc_fails("-5")); /* a leading minus alone */
|
||||
mx_calc_format(2.5, buf, sizeof buf);
|
||||
CHECK(strcmp(buf, "2.5") == 0);
|
||||
mx_calc_format(3.0, buf, sizeof buf);
|
||||
CHECK(strcmp(buf, "3") == 0);
|
||||
mx_calc_format(-0.0, buf, sizeof buf);
|
||||
CHECK(strcmp(buf, "0") == 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_snippets(const char *dir)
|
||||
{
|
||||
char path[1024];
|
||||
FILE *f;
|
||||
MxSnippet *s;
|
||||
size_t n;
|
||||
char *u;
|
||||
|
||||
u = mx_unescape("a\\nb\\tc\\\\d");
|
||||
CHECK(u != NULL && strcmp(u, "a\nb\tc\\d") == 0);
|
||||
free(u);
|
||||
|
||||
(void)snprintf(path, sizeof path, "%s/snippets", dir);
|
||||
CHECK(mx_snippets_load(path, &s, &n) == 0 && n == 0); /* no file */
|
||||
|
||||
f = fopen(path, "w");
|
||||
CHECK(f != NULL);
|
||||
if (f == NULL)
|
||||
return;
|
||||
fputs("# a comment\n", f);
|
||||
fputs("addr\tRua Nova 1\\n1000 Lisboa\n", f);
|
||||
fputs("no tab on this line\n", f);
|
||||
fputs("\tno keyword\n", f);
|
||||
fputs("sig\t-- \\nKonrad\r\n", f);
|
||||
fclose(f);
|
||||
CHECK(mx_snippets_load(path, &s, &n) == 0);
|
||||
CHECK(n == 2);
|
||||
if (n == 2) {
|
||||
CHECK(strcmp(s[0].key, "addr") == 0);
|
||||
CHECK(strcmp(s[0].text, "Rua Nova 1\n1000 Lisboa") == 0);
|
||||
CHECK(strcmp(s[1].key, "sig") == 0);
|
||||
CHECK(strcmp(s[1].text, "-- \nKonrad") == 0);
|
||||
}
|
||||
mx_snippets_free(s, n);
|
||||
(void)unlink(path);
|
||||
}
|
||||
|
||||
static void
|
||||
test_programs(const char *dir)
|
||||
{
|
||||
char d1[1024], d2[1024], f[1100], p[2200];
|
||||
char **v;
|
||||
size_t n, i;
|
||||
FILE *fp;
|
||||
const char *names[] = { "beta", "alpha", ".hidden" };
|
||||
|
||||
(void)snprintf(d1, sizeof d1, "%s/bin1", dir);
|
||||
(void)snprintf(d2, sizeof d2, "%s/bin2", dir);
|
||||
(void)mkdir(d1, 0755);
|
||||
(void)mkdir(d2, 0755);
|
||||
for (i = 0; i < 3; i++) {
|
||||
(void)snprintf(f, sizeof f, "%s/%s", d1, names[i]);
|
||||
fp = fopen(f, "w");
|
||||
if (fp != NULL)
|
||||
fclose(fp);
|
||||
(void)chmod(f, 0755);
|
||||
}
|
||||
(void)snprintf(f, sizeof f, "%s/alpha", d2); /* a duplicate */
|
||||
fp = fopen(f, "w");
|
||||
if (fp != NULL)
|
||||
fclose(fp);
|
||||
(void)chmod(f, 0755);
|
||||
(void)snprintf(f, sizeof f, "%s/notexec", d2);
|
||||
fp = fopen(f, "w");
|
||||
if (fp != NULL)
|
||||
fclose(fp);
|
||||
(void)chmod(f, 0644);
|
||||
|
||||
(void)snprintf(p, sizeof p, "%s:%s:/nonexistent", d1, d2);
|
||||
CHECK(mx_path_programs(p, &v, &n) == 0);
|
||||
CHECK(n == 2);
|
||||
if (n == 2) {
|
||||
CHECK(strcmp(v[0], "alpha") == 0);
|
||||
CHECK(strcmp(v[1], "beta") == 0);
|
||||
}
|
||||
mx_strv_free(v, n);
|
||||
}
|
||||
|
||||
/* remove what test_programs made; test_snippets removes its own file */
|
||||
static void
|
||||
cleanup(const char *dir)
|
||||
{
|
||||
const char *files[] = { "bin1/alpha", "bin1/beta", "bin1/.hidden",
|
||||
"bin2/alpha", "bin2/notexec", "bin1", "bin2" };
|
||||
char p[1100];
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof files / sizeof files[0]; i++) {
|
||||
(void)snprintf(p, sizeof p, "%s/%s", dir, files[i]);
|
||||
if (unlink(p) != 0)
|
||||
(void)rmdir(p);
|
||||
}
|
||||
(void)rmdir(dir);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
char dir[] = "/tmp/dtlaunch-test.XXXXXX";
|
||||
|
||||
if (mkdtemp(dir) == NULL) {
|
||||
perror("mkdtemp");
|
||||
return 2;
|
||||
}
|
||||
test_match();
|
||||
test_calc();
|
||||
test_snippets(dir);
|
||||
test_programs(dir);
|
||||
cleanup(dir);
|
||||
printf("%s: %d failures\n", failures ? "FAIL" : "PASS", failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/bin/ksh
|
||||
#
|
||||
# 0080.dtlaunch -- MCDE: the launcher runs in every session
|
||||
#
|
||||
# Sourced by Xsession (Xsession.d). dtlaunch is one per display: started a
|
||||
# second time it only opens the running one, so this does no harm in a
|
||||
# session that already has it. Remove the execute bit to switch it off.
|
||||
#
|
||||
if [ -x "$DT_BINPATH/dtlaunch" ]; then
|
||||
"$DT_BINPATH/dtlaunch" &
|
||||
fi
|
||||
@@ -76,7 +76,7 @@ dist_configs_DATA = Xaccess Xconfig Xfailsafe Xservers
|
||||
dist_configs_SCRIPTS = sys.dtprofile Xfailsafe Xreset Xsetup Xstartup
|
||||
|
||||
xsessdir=$(CDE_INSTALLATION_TOP)/config/Xsession.d
|
||||
dist_xsess_SCRIPTS = 0060.dtsysenv
|
||||
dist_xsess_SCRIPTS = 0060.dtsysenv 0080.dtlaunch
|
||||
|
||||
dist_bin_SCRIPTS = Xsession
|
||||
|
||||
|
||||
Reference in new issue
Block a user