From d490a2ec24bf1ecc3044b09ed9e03223c5c05229 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 17 Nov 2026 11:17:41 +0000 Subject: [PATCH] dtchat: report typing from the input field (T4.3) The UI gains a typing hook: XmNmodifyVerifyCallback sends typing on for the first change (and after a pause) with the current conversation, resets a 2.5 s timeout that sends off, finishes the old conversation when the selection changes, and ignores programmatic set_input (suppress flag). accounts_run sets the hook to dc_adapter_send_typing of the account that owns the conversation, so XMPP sends XEP-0085 composing/paused; the IRC adapter ignores the command it does not implement. DTCHAT_ACCOUNTS_DEBUG logs the on/off transitions. In-tree build and a focus-based check follow. --- programs/dtchat/accounts_run.c | 16 ++++++++ programs/dtchat/ui_main.c | 72 ++++++++++++++++++++++++++++++++++ programs/dtchat/ui_main.h | 3 ++ 3 files changed, 91 insertions(+) diff --git a/programs/dtchat/accounts_run.c b/programs/dtchat/accounts_run.c index 652a189e..43f62a11 100644 --- a/programs/dtchat/accounts_run.c +++ b/programs/dtchat/accounts_run.c @@ -691,6 +691,20 @@ accounts_file_send(const char *peer_id, const char *path, void *arg) } } +static void +accounts_typing_hook(void *arg, const char *conv, int on) +{ + DtAccounts *accounts = arg; + AccountRun *run = run_current(accounts, conv); + + if (run != NULL && run->adapter != NULL) { + (void)dc_adapter_send_typing(run->adapter, conv, on); + if (debug_on()) + fprintf(stderr, "accounts %s: typing %d for %s\n", + run->id, on, conv); + } +} + static int accounts_send_hook(void *arg, const char *conv, const char *text) { @@ -909,6 +923,7 @@ dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model, dtchat_ui_refresh(ui); dtchat_ui_set_send_hook(ui, accounts_send_hook, accounts); dtchat_ui_set_file_send(ui, accounts_file_send, accounts); + dtchat_ui_set_typing_hook(ui, accounts_typing_hook, accounts); schedule_poll(accounts); return accounts; } @@ -926,6 +941,7 @@ dtchat_accounts_stop(DtAccounts *accounts) } dtchat_ui_set_send_hook(accounts->ui, NULL, NULL); dtchat_ui_set_file_send(accounts->ui, NULL, NULL); + dtchat_ui_set_typing_hook(accounts->ui, NULL, NULL); while (accounts->incoming != NULL) incoming_free(accounts, accounts->incoming, 1); for (i = 0; i < accounts->count; i++) { diff --git a/programs/dtchat/ui_main.c b/programs/dtchat/ui_main.c index 4496065e..3842dfcd 100644 --- a/programs/dtchat/ui_main.c +++ b/programs/dtchat/ui_main.c @@ -65,6 +65,12 @@ struct DtChatUi { DtAccountsDialog *accounts; DtChatSendFn send_fn; void *send_arg; + DtChatTypingFn typing_fn; + void *typing_arg; + XtIntervalId typing_timer; + int typing_on; + int suppress_typing; + char typing_conv[128]; char *history[64]; size_t nhistory; size_t hist_pos; @@ -82,6 +88,52 @@ xs(const char *s) return XmStringCreateLocalized((char *)s); } +/* typing: sent when the text changes, stopped after 2.5 s without a key */ +static void +typing_stop_cb(XtPointer client, XtIntervalId *id) +{ + DtChatUi *ui = client; + + (void)id; + ui->typing_timer = 0; + if (ui->typing_on && ui->typing_fn != NULL && + ui->typing_conv[0] != '\0') + ui->typing_fn(ui->typing_arg, ui->typing_conv, 0); + ui->typing_on = 0; +} + +static void +input_modify_cb(Widget widget, XtPointer client, XtPointer call) +{ + DtChatUi *ui = client; + XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)call; + time_t now; + + (void)widget; + if (ui->suppress_typing || ui->typing_fn == NULL || + ui->cur_conv == NULL || cbs == NULL || cbs->text == NULL || + cbs->text->length == 0) + return; + if (ui->typing_on && + strcmp(ui->typing_conv, ui->cur_conv) != 0) { + ui->typing_fn(ui->typing_arg, ui->typing_conv, 0); + ui->typing_on = 0; + } + now = time(NULL); + if (!ui->typing_on) { + snprintf(ui->typing_conv, sizeof(ui->typing_conv), "%s", + ui->cur_conv); + ui->typing_fn(ui->typing_arg, ui->cur_conv, 1); + ui->typing_on = 1; + } + if (ui->typing_timer != 0) + (void)XtRemoveTimeOut(ui->typing_timer); + ui->typing_timer = XtAppAddTimeOut( + XtWidgetToApplicationContext(ui->shell), 2500, typing_stop_cb, + ui); + (void)now; +} + static void set_status(DtChatUi *ui, const char *text) { @@ -184,9 +236,11 @@ dtchat_ui_set_input(DtChatUi *ui, const char *text) { if (ui == NULL) return; + ui->suppress_typing = 1; XmTextSetString(ui->input, (char *)(text != NULL ? text : "")); XmTextSetInsertionPosition(ui->input, XmTextGetLastPosition(ui->input)); + ui->suppress_typing = 0; } int @@ -1113,6 +1167,8 @@ dtchat_ui_create(Widget shell, DcModel *model) XmNcursorPositionVisible, False, NULL); ui->input = make_scrolled_text(chat, "input", 3, 40, 1, &input_parent); + XtAddCallback(ui->input, XmNmodifyVerifyCallback, input_modify_cb, + ui); send = XmCreatePushButton(chat, "send", NULL, 0); x = xs(dc_text(TX_SEND, "Send")); XtVaSetValues(send, XmNlabelString, x, @@ -1155,6 +1211,8 @@ dtchat_ui_destroy(DtChatUi *ui) { if (ui == NULL) return; + if (ui->typing_timer != 0) + (void)XtRemoveTimeOut(ui->typing_timer); clear_rows(ui); while (ui->nhistory > 0) free(ui->history[--ui->nhistory]); @@ -1265,6 +1323,20 @@ dtchat_ui_set_send_hook(DtChatUi *ui, DtChatSendFn fn, void *arg) ui->send_arg = arg; } +void +dtchat_ui_set_typing_hook(DtChatUi *ui, DtChatTypingFn fn, void *arg) +{ + if (ui == NULL) + return; + ui->typing_fn = fn; + ui->typing_arg = arg; + if (fn == NULL && ui->typing_timer != 0) { + (void)XtRemoveTimeOut(ui->typing_timer); + ui->typing_timer = 0; + ui->typing_on = 0; + } +} + size_t dtchat_ui_shown_messages(const DtChatUi *ui) { diff --git a/programs/dtchat/ui_main.h b/programs/dtchat/ui_main.h index 1e9e06b7..5612e5ae 100644 --- a/programs/dtchat/ui_main.h +++ b/programs/dtchat/ui_main.h @@ -14,6 +14,8 @@ typedef struct DtChatUi DtChatUi; /* Send text to CONV. Return 0 when it was accepted. */ typedef int (*DtChatSendFn)(void *arg, const char *conv, const char *text); +/* typing state of the person at the input (XEP-0085 / IRC typing) */ +typedef void (*DtChatTypingFn)(void *arg, const char *conv, int on); /* MODEL is borrowed; the caller keeps it alive until dtchat_ui_destroy(). */ DtChatUi *dtchat_ui_create(Widget shell, DcModel *model); @@ -61,6 +63,7 @@ int dtchat_ui_smoke(DtChatUi *ui); /* Route sends through FN (the demo/adapter path); NULL restores the local echo. */ void dtchat_ui_set_send_hook(DtChatUi *ui, DtChatSendFn fn, void *arg); +void dtchat_ui_set_typing_hook(DtChatUi *ui, DtChatTypingFn fn, void *arg); /* T2.8 input helpers (the key handler uses them too). */ void dtchat_ui_clear_input(DtChatUi *ui);