Files
mcde/programs/dtchat/ui_request.c
T
lotherk 64979ecebd dtchat: add the request, error and QR dialogs (T2.5)
ui_request.c turns one adapter request object into a Motif dialog: code, password (masked via modifyVerify, the real value kept out of the widget), notice, confirm and qr. The QR bitmap is base64 (one byte per module), drawn into a depth-1 pixmap and shown as XmNlabelPixmap; no QR library. The answer is delivered exactly once from OK/Yes/No, Cancel, the timeout or the window-manager close, and the dialog frees itself before the callback runs. dtchat_error_dialog is the one-button error/notice window. The smoke test exercises all five kinds plus the timeout; --demo-request qr|code|password|error shows one for screenshots. Standalone smoke PASS; QR and masked password verified on Xvfb (build/mcde-shots/dtchat-t25.png). make mcde-host still blocked by the foreign release build holding build/.build.lock (pid 175328).
2026-11-16 18:46:56 +00:00

649 lines
16 KiB
C

/*
* ui_request.c -- the code/password/notice/confirm/QR dialogs
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Every dialog maps one adapter "request" object of the v1 protocol. The
* answer is delivered exactly once, whether it comes from a button, from
* cancel, from the timeout or from the window manager's close box. The QR
* bitmap arrives base64-encoded (one byte per module, row-major) and is drawn
* into a depth-1 pixmap; no QR library and no terminal art.
*/
#define _POSIX_C_SOURCE 200809L
#include "ui_request.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <X11/Xlib.h>
#include <Xm/DialogS.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/TextF.h>
#include <Xm/Xm.h>
typedef enum {
R_CODE,
R_PASSWORD,
R_NOTICE,
R_CONFIRM,
R_QR
} RequestKind;
struct DtRequestDialog {
Display *dpy;
Widget shell;
Widget form;
Widget field;
Widget pixmap_widget;
Pixmap pixmap;
XtIntervalId timer;
DtRequestAnswerFn answer;
void *arg;
char *secret;
int answered;
int secret_mode;
};
static XmString
xs(const char *s)
{
return XmStringCreateLocalized((char *)s);
}
static void
set_label(Widget w, const char *text)
{
XmString x = xs(text);
XtVaSetValues(w, XmNlabelString, x, NULL);
XmStringFree(x);
}
static void
secret_verify_cb(Widget w, XtPointer client, XtPointer call)
{
DtRequestDialog *dialog = client;
XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)call;
char *old;
char *next;
size_t oldlen, start, end, ins, newlen, i;
(void)w;
if (!cbs->doit)
return;
old = dialog->secret != NULL ? dialog->secret : "";
oldlen = strlen(old);
start = (size_t)cbs->startPos;
end = (size_t)cbs->endPos;
if (start > oldlen)
start = oldlen;
if (end > oldlen)
end = oldlen;
if (end < start)
end = start;
ins = cbs->text != NULL ? (size_t)cbs->text->length : 0;
newlen = start + ins + (oldlen - end);
next = malloc(newlen + 1);
if (next == NULL)
return;
memcpy(next, old, start);
if (ins > 0 && cbs->text != NULL && cbs->text->ptr != NULL)
memcpy(next + start, cbs->text->ptr, ins);
memcpy(next + start + ins, old + end, oldlen - end);
next[newlen] = '\0';
free(dialog->secret);
dialog->secret = next;
if (ins > 0 && cbs->text != NULL && cbs->text->ptr != NULL)
for (i = 0; i < ins; i++)
cbs->text->ptr[i] = '*';
}
static int
base64_value(int c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A';
if (c >= 'a' && c <= 'z')
return c - 'a' + 26;
if (c >= '0' && c <= '9')
return c - '0' + 52;
if (c == '+')
return 62;
if (c == '/')
return 63;
return -1;
}
/* Decode base64, ignoring whitespace. Returns the number of bytes or -1. */
static ssize_t
base64_decode(const char *text, unsigned char *out, size_t outlen)
{
unsigned int acc = 0;
int bits = 0;
size_t written = 0;
if (text == NULL)
return -1;
for (; *text != '\0'; text++) {
int v;
if (isspace((unsigned char)*text))
continue;
if (*text == '=')
break;
v = base64_value((unsigned char)*text);
if (v < 0)
return -1;
acc = (acc << 6) | (unsigned int)v;
bits += 6;
if (bits >= 8) {
bits -= 8;
if (written >= outlen)
return -1;
out[written++] = (unsigned char)((acc >> bits) & 0xff);
}
}
return (ssize_t)written;
}
static Pixmap
make_qr_pixmap(DtRequestDialog *dialog, const DcJson *qr, int *size_out)
{
Display *dpy = XtDisplay(dialog->shell);
const DcJson *qr_obj = qr;
int width = (int)dc_json_int(dc_json_get(qr_obj, "width"));
int height = (int)dc_json_int(dc_json_get(qr_obj, "height"));
const char *data = dc_json_string(dc_json_get(qr_obj, "data"));
unsigned char *modules = NULL;
unsigned char *bits = NULL;
ssize_t n;
int scale, pw, ph, stride, x, y;
Pixmap pixmap = None;
GC gc = NULL;
XImage *image = NULL;
if (width <= 0 || height <= 0 || width > 200 || height > 200 ||
data == NULL)
return None;
modules = calloc((size_t)width * (size_t)height, 1);
if (modules == NULL)
return None;
n = base64_decode(data, modules, (size_t)width * (size_t)height);
if (n < (ssize_t)((size_t)width * (size_t)height)) {
free(modules);
return None;
}
scale = 240 / (width > height ? width : height);
if (scale < 1)
scale = 1;
if (scale > 12)
scale = 12;
pw = width * scale;
ph = height * scale;
stride = (pw + 7) / 8;
bits = calloc((size_t)stride * (size_t)ph, 1);
if (bits == NULL)
goto out;
pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy), (unsigned int)pw,
(unsigned int)ph, 1);
if (pixmap == None)
goto out;
image = XCreateImage(dpy, DefaultVisual(dpy, DefaultScreen(dpy)), 1,
XYBitmap, 0, (char *)bits, (unsigned int)pw, (unsigned int)ph,
8, 0);
if (image == NULL)
goto out;
for (y = 0; y < ph; y++) {
for (x = 0; x < pw; x++) {
int mx = x / scale;
int my = y / scale;
XPutPixel(image, x, y,
modules[(size_t)my * (size_t)width +
(size_t)mx] ? 1 : 0);
}
}
gc = XCreateGC(dpy, pixmap, 0, NULL);
if (gc == NULL)
goto out;
XSetForeground(dpy, gc, 1);
XSetBackground(dpy, gc, 0);
XPutImage(dpy, pixmap, gc, image, 0, 0, 0, 0, (unsigned int)pw,
(unsigned int)ph);
*size_out = pw;
out:
if (gc != NULL)
XFreeGC(dpy, gc);
if (image != NULL) {
XDestroyImage(image); /* frees bits */
bits = NULL;
}
free(bits);
free(modules);
return pixmap;
}
static void
finish(DtRequestDialog *dialog, const char *value)
{
DtRequestAnswerFn fn = dialog->answer;
void *arg = dialog->arg;
char *copy = value != NULL ? strdup(value) : NULL;
if (dialog->answered)
return;
dialog->answered = 1;
if (dialog->timer != 0) {
(void)XtRemoveTimeOut(dialog->timer);
dialog->timer = 0;
}
if (dialog->shell != NULL) {
Widget shell = dialog->shell;
dialog->shell = NULL;
XtDestroyWidget(shell);
}
dtchat_request_dialog_destroy(dialog);
if (fn != NULL)
fn(copy, arg);
free(copy);
}
static void
timeout_cb(XtPointer client, XtIntervalId *id)
{
DtRequestDialog *dialog = client;
(void)id;
dialog->timer = 0;
finish(dialog, NULL);
}
static void
ok_cb(Widget w, XtPointer client, XtPointer call)
{
DtRequestDialog *dialog = client;
char *value = NULL;
const char *answer = NULL;
(void)w;
(void)call;
if (dialog->field != NULL) {
if (dialog->secret_mode) {
answer = dialog->secret != NULL ? dialog->secret : "";
} else {
value = XmTextFieldGetString(dialog->field);
answer = value != NULL ? value : "";
}
} else {
answer = "1";
}
finish(dialog, answer);
if (value != NULL)
XtFree(value);
}
static void
cancel_cb(Widget w, XtPointer client, XtPointer call)
{
(void)w;
(void)call;
finish(client, NULL);
}
static void
confirm_no_cb(Widget w, XtPointer client, XtPointer call)
{
(void)w;
(void)call;
finish(client, "0");
}
static void
destroyed_cb(Widget w, XtPointer client, XtPointer call)
{
DtRequestDialog *dialog = client;
(void)w;
(void)call;
if (dialog != NULL) {
dialog->shell = NULL;
if (!dialog->answered) {
DtRequestAnswerFn fn = dialog->answer;
void *arg = dialog->arg;
dialog->answered = 1;
if (dialog->timer != 0) {
(void)XtRemoveTimeOut(dialog->timer);
dialog->timer = 0;
}
if (fn != NULL)
fn(NULL, arg);
}
}
}
static Widget
button(Widget parent, const char *label, const char *name,
XtCallbackProc cb, DtRequestDialog *dialog)
{
Widget b = XmCreatePushButton(parent, (char *)name, NULL, 0);
set_label(b, label);
if (cb != NULL)
XtAddCallback(b, XmNactivateCallback, cb, dialog);
XtManageChild(b);
return b;
}
static RequestKind
request_kind(const char *kind)
{
if (kind == NULL)
return R_NOTICE;
if (strcmp(kind, "code") == 0)
return R_CODE;
if (strcmp(kind, "password") == 0)
return R_PASSWORD;
if (strcmp(kind, "confirm") == 0)
return R_CONFIRM;
if (strcmp(kind, "qr") == 0)
return R_QR;
return R_NOTICE;
}
static DtRequestDialog *
dialog_new(Widget shell, DtRequestAnswerFn answer, void *arg)
{
DtRequestDialog *dialog = calloc(1, sizeof(*dialog));
if (dialog == NULL)
return NULL;
dialog->dpy = XtDisplay(shell);
dialog->shell = shell;
dialog->answer = answer;
dialog->arg = arg;
XtAddCallback(shell, XmNdestroyCallback, destroyed_cb, dialog);
return dialog;
}
DtRequestDialog *
dtchat_request_dialog_create(Widget parent, const DcJson *request,
int timeout_ms, DtRequestAnswerFn answer, void *arg)
{
DtRequestDialog *dialog;
RequestKind kind;
const char *title;
const char *prompt;
const char *hint;
Widget form, text_label = NULL, buttons;
Widget hint_w = NULL, okb = NULL, cancelb = NULL;
int timeout;
if (parent == NULL || request == NULL)
return NULL;
kind = request_kind(dc_json_string(dc_json_get(request, "kind")));
title = dc_json_string(dc_json_get(request, "title"));
prompt = dc_json_string(dc_json_get(request, "prompt"));
hint = dc_json_string(dc_json_get(request, "hint"));
form = XmCreateFormDialog(parent, "requestDialog", NULL, 0);
if (form == NULL)
return NULL;
dialog = dialog_new(XtParent(form), answer, arg);
if (dialog == NULL) {
XtDestroyWidget(form);
return NULL;
}
dialog->form = form;
XtVaSetValues(dialog->shell, XmNtitle,
(char *)(title != NULL ? title : "dtchat"), XmNwidth, 420,
XmNdeleteResponse, XmDESTROY, NULL);
if (kind == R_QR) {
const DcJson *qr = dc_json_get(request, "qr");
int side = 0;
Pixmap pixmap = None;
if (qr != NULL)
pixmap = make_qr_pixmap(dialog, qr, &side);
if (pixmap != None) {
XmString x = xs("");
dialog->pixmap = pixmap;
dialog->pixmap_widget = XmCreateLabel(form,
"qrImage", NULL, 0);
XtVaSetValues(dialog->pixmap_widget,
XmNlabelType, XmPIXMAP,
XmNlabelPixmap, pixmap,
XmNlabelString, x,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNtopOffset, 8, NULL);
XmStringFree(x);
XtManageChild(dialog->pixmap_widget);
} else {
text_label = XmCreateLabel(form, "qrFallback", NULL, 0);
set_label(text_label,
"The adapter sent no usable QR bitmap.");
XtVaSetValues(text_label,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
XtManageChild(text_label);
}
if (hint != NULL && *hint != '\0') {
hint_w = XmCreateLabel(form, "qrHint", NULL, 0);
set_label(hint_w, hint);
XtVaSetValues(hint_w,
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, dialog->pixmap_widget != NULL ?
dialog->pixmap_widget : text_label,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNtopOffset, 8, NULL);
XtManageChild(hint_w);
}
} else {
text_label = XmCreateLabel(form, "prompt", NULL, 0);
set_label(text_label, prompt != NULL ? prompt :
(kind == R_NOTICE ? "" : "Answer the request:"));
XtVaSetValues(text_label,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmarginTop, 8, XmNmarginLeft, 8,
XmNmarginRight, 8, NULL);
XtManageChild(text_label);
if (kind == R_CODE || kind == R_PASSWORD) {
dialog->field = XmCreateTextField(form, "answer", NULL, 0);
XtVaSetValues(dialog->field,
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, text_label,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNtopOffset, 8, XmNleftOffset, 8,
XmNrightOffset, 8, XmNcolumns, 28, NULL);
if (kind == R_PASSWORD) {
dialog->secret_mode = 1;
dialog->secret = strdup("");
XtAddCallback(dialog->field,
XmNmodifyVerifyCallback, secret_verify_cb,
dialog);
}
XtManageChild(dialog->field);
}
if (hint != NULL && *hint != '\0') {
hint_w = XmCreateLabel(form, "hint", NULL, 0);
set_label(hint_w, hint);
XtVaSetValues(hint_w,
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, dialog->field != NULL ?
dialog->field : text_label,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmarginLeft, 8, XmNmarginRight, 8,
XmNtopOffset, 8, NULL);
XtManageChild(hint_w);
}
}
buttons = XtVaCreateManagedWidget("buttons", xmFormWidgetClass, form,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNheight, 38, XmNmarginHeight, 4, NULL);
switch (kind) {
case R_NOTICE:
okb = button(buttons, "OK", "ok", ok_cb, dialog);
break;
case R_QR:
cancelb = button(buttons, "Cancel", "cancel", cancel_cb, dialog);
break;
case R_CONFIRM:
cancelb = button(buttons, "No", "no", confirm_no_cb, dialog);
okb = button(buttons, "Yes", "yes", ok_cb, dialog);
break;
case R_CODE:
case R_PASSWORD:
cancelb = button(buttons, "Cancel", "cancel", cancel_cb, dialog);
okb = button(buttons, "OK", "ok", ok_cb, dialog);
break;
}
if (cancelb != NULL)
XtVaSetValues(cancelb,
XmNrightAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightOffset, 6, XmNbottomOffset, 4,
XmNwidth, 88, NULL);
if (okb != NULL) {
XtVaSetValues(okb,
XmNrightAttachment, cancelb != NULL ? XmATTACH_WIDGET :
XmATTACH_FORM,
XmNrightWidget, cancelb,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightOffset, 6, XmNbottomOffset, 4,
XmNwidth, 88, NULL);
}
if (kind == R_QR && hint_w != NULL)
XtVaSetValues(hint_w,
XmNbottomAttachment, XmATTACH_WIDGET,
XmNbottomWidget, buttons, NULL);
timeout = timeout_ms;
if (timeout > 0)
dialog->timer = XtAppAddTimeOut(
XtWidgetToApplicationContext(parent), (unsigned long)timeout,
timeout_cb, dialog);
XtManageChild(form);
XtVaSetValues(dialog->shell, XmNwidth, 340,
XmNheight, kind == R_QR ? 330 : 200, NULL);
XtManageChild(form);
XtPopup(dialog->shell, XtGrabNone);
if (dialog->field != NULL)
XmProcessTraversal(dialog->field, XmTRAVERSE_CURRENT);
return dialog;
}
void
dtchat_request_dialog_answer(DtRequestDialog *dialog, const char *value)
{
if (dialog == NULL)
return;
finish(dialog, value);
}
void
dtchat_request_dialog_destroy(DtRequestDialog *dialog)
{
if (dialog == NULL)
return;
if (dialog->timer != 0)
(void)XtRemoveTimeOut(dialog->timer);
if (dialog->shell != NULL) {
Widget shell = dialog->shell;
dialog->shell = NULL;
XtDestroyWidget(shell);
}
if (dialog->pixmap != None)
XFreePixmap(dialog->dpy, dialog->pixmap);
free(dialog->secret);
free(dialog);
}
Widget
dtchat_request_dialog_shell(const DtRequestDialog *dialog)
{
return dialog != NULL ? dialog->shell : NULL;
}
static void
error_close_cb(Widget w, XtPointer client, XtPointer call)
{
DtRequestDialog *dialog = client;
(void)w;
(void)call;
if (dialog->shell != NULL) {
Widget shell = dialog->shell;
dialog->shell = NULL;
XtDestroyWidget(shell);
}
dtchat_request_dialog_destroy(dialog);
}
void
dtchat_error_dialog(Widget parent, const char *title, const char *text)
{
DtRequestDialog *dialog;
Widget form, label, buttons;
if (parent == NULL)
return;
form = XmCreateFormDialog(parent, "errorDialog", NULL, 0);
if (form == NULL)
return;
dialog = dialog_new(XtParent(form), NULL, NULL);
if (dialog == NULL) {
XtDestroyWidget(form);
return;
}
dialog->form = form;
XtVaSetValues(dialog->shell, XmNtitle,
(char *)(title != NULL ? title : "dtchat"), XmNwidth, 380,
XmNdeleteResponse, XmDESTROY, NULL);
label = XmCreateLabel(form, "errorText", NULL, 0);
set_label(label, text != NULL ? text : "Unknown error");
XtVaSetValues(label,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmarginTop, 10, XmNmarginLeft, 10, XmNmarginRight, 10, NULL);
XtManageChild(label);
buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass,
form, XmNorientation, XmHORIZONTAL,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNmarginHeight, 6, NULL);
(void)button(buttons, "Dismiss", "dismiss", error_close_cb, dialog);
XtManageChild(form);
XtPopup(dialog->shell, XtGrabNone);
}