Windows: draw the terminals from the engine, with a terminal type picker

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSahRp96gCgqn8dFBk
This commit is contained in:
Claude committed 2026-11-12 12:53:48 +00:00
1 parent 204e711efc
commit 80e2110511
6 files changed
+947 -550

No files matched your search

+3 -2
View File
@@ -712,7 +712,7 @@ static const struct vt_font *alpha_font(struct regis *g, int n)
struct vt_font *f;
if (a->font)
return a->font;
if (!a->n)
if (a->n <= 0)
return NULL;
for (i = 0; i < a->n; i++) {
if (a->g[i].nrows > h)
@@ -725,7 +725,8 @@ static const struct vt_font *alpha_font(struct regis *g, int n)
width += 8;
f = (struct vt_font *)calloc(1, sizeof(*f));
a->codes = (uint32_t *)malloc(sizeof(uint32_t) * (size_t)a->n);
a->rows = (uint32_t *)calloc((size_t)a->n * h, sizeof(uint32_t));
a->rows = (uint32_t *)calloc((size_t)a->n * (size_t)(h > 0 ? h : 1),
sizeof(uint32_t));
if (!f || !a->codes || !a->rows) {
free(f);
free(a->codes);
+6
View File
@@ -146,6 +146,12 @@ void termview_reset(HWND w);
void termview_size(HWND w, int *cols, int *rows);
/* what Backspace sends (DEL unless set) */
void termview_set_backspace(HWND w, char b);
/* the kind of terminal it is (dmc_vt_set_profile()'s id) */
void termview_set_profile(HWND w, const char *id);
const char *termview_profile(HWND w);
/* a combo box of the kinds of terminal, id chosen; the one chosen */
void term_types_fill(HWND combo, const char *id);
const char *term_types_chosen(HWND combo);
/* remote.cpp: the Remote page, a login by SSH, Telnet or rlogin */
HWND remote_page_new(HWND parent);
+30 -6
View File
@@ -20,8 +20,8 @@
#include "app.h"
#include "resource.h"
static HWND page, view, proto_box, host_ed, port_ed, user_ed, go_btn,
status_txt;
static HWND page, view, type_box, proto_box, host_ed, port_ed, user_ed,
go_btn, status_txt;
/* the protocols in the list's order */
static const int protos[] = { DMC_NET_SSH, DMC_NET_TELNET, DMC_NET_RLOGIN };
@@ -399,7 +399,8 @@ static bool start_ssh(const char *host, int port, const char *user)
cmd[sizeof(cmd) - 1] = '\0';
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(pi));
SetEnvironmentVariable("TERM", "xterm");
SetEnvironmentVariable("TERM",
dmc_vt_profile_term(termview_profile(view)));
if (ok)
ok = CreateProcess(NULL, cmd, NULL, NULL, FALSE,
EXTENDED_STARTUPINFO_PRESENT, NULL, NULL,
@@ -437,7 +438,11 @@ static bool start_socket(int proto, const char *host, int port,
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
return false;
net = dmc_net_new(proto, user, "ansi", cols, rows);
/* (Telnet's TERMINAL-TYPE, rlogin's TERM) */
const char *type = termview_profile(view);
net = dmc_net_new(proto, user, proto == DMC_NET_TELNET ?
dmc_vt_profile_telnet(type) :
dmc_vt_profile_term(type), cols, rows);
sock_open = false;
WSAAsyncSelect(sock, app.main, WM_APP_REMOTE,
FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE | FD_OOB);
@@ -494,6 +499,8 @@ static void connect_clicked()
WritePrivateProfileString("remote", "host", host, path);
WritePrivateProfileString("remote", "port", pt, path);
WritePrivateProfileString("remote", "user", user, path);
WritePrivateProfileString("remote", "terminal", termview_profile(view),
path);
termview_reset(view);
termview_size(view, &cols, &rows);
@@ -544,6 +551,11 @@ static void remote_command(HWND p, int id, int code)
if (code == CBN_SELCHANGE)
proto_changed();
break;
case IDC_R_TYPE:
/* (the far end was told the type it had when it connected) */
if (code == CBN_SELCHANGE)
termview_set_profile(view, term_types_chosen(type_box));
break;
}
}
@@ -569,6 +581,9 @@ HWND remote_page_new(HWND parent)
view = termview_new(page, IDC_R_TERM, typed, NULL);
status_txt = make_child("STATIC", "Not connected", SS_LEFTNOWORDWRAP,
page, IDC_R_STATUS);
make_child("STATIC", "&Terminal:", SS_RIGHT, page, IDC_R_TYPELABEL);
type_box = make_child("COMBOBOX", "", WS_TABSTOP | CBS_DROPDOWNLIST |
WS_VSCROLL, page, IDC_R_TYPE);
/* what was used last */
char path[MAX_PATH], v[256];
@@ -585,6 +600,10 @@ HWND remote_page_new(HWND parent)
SetWindowText(port_ed, v);
GetPrivateProfileString("remote", "user", "", v, sizeof(v), path);
SetWindowText(user_ed, v);
GetPrivateProfileString("remote", "terminal", "xterm", v, sizeof(v),
path);
term_types_fill(type_box, v);
termview_set_profile(view, term_types_chosen(type_box));
proto_changed();
controls();
return page;
@@ -610,8 +629,13 @@ void remote_page_layout(HWND p, int w, int h)
x += 40;
MoveWindow(user_ed, x, m, 96, bh, TRUE);
MoveWindow(go_btn, w - m - 90, m, 90, bh, TRUE);
MoveWindow(view, m, 2 * m + bh, w - 2 * m, h - 4 * m - 2 * bh + 6, TRUE);
MoveWindow(status_txt, m, h - m - bh + 6, w - 2 * m, bh - 6, TRUE);
MoveWindow(view, m, 2 * m + bh, w - 2 * m, h - 4 * m - 2 * bh, TRUE);
int tw = 200;
MoveWindow(status_txt, m, h - m - bh + 4, w - 3 * m - tw - 64, bh - 4,
TRUE);
MoveWindow(GetDlgItem(p, IDC_R_TYPELABEL), w - m - tw - 64, h - m - bh + 4,
60, bh - 4, TRUE);
MoveWindow(type_box, w - m - tw, h - m - bh, tw, 300, TRUE);
}
bool remote_focus()
+4
View File
@@ -38,6 +38,8 @@
#define IDC_SETUP_CF 501
#define IDC_SETUP_CT 502
#define IDC_CLEAR 503
#define IDC_TERM_TYPELABEL 504
#define IDC_TERM_TYPE 505
/* the Remote page */
#define IDC_R_PROTO 1100
@@ -50,6 +52,8 @@
#define IDC_R_GO 1107
#define IDC_R_TERM 1108
#define IDC_R_STATUS 1109
#define IDC_R_TYPELABEL 1110
#define IDC_R_TYPE 1111
/* the Settings page */
#define IDC_GROUPS 600
+49 -344
View File
@@ -2,13 +2,11 @@
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*
* term.cpp - the Terminal page: the modem's screen (libdmodemctl's
* dmc_vt, drawn with GDI), with buttons for its full-screen
* and command-line setup
*
* The text is drawn with ExtTextOutW, one of the few wide functions
* Windows 95 has, so the configurator's box drawing shows on every
* version. Keys go to the modem as a VT100's; the right button pastes.
* term.cpp - the Terminal page: the modem's screen (a terminal control,
* termview.cpp), with buttons for its full-screen and
* command-line setup and the kind of terminal it is (a BBS
* reached by modem may want ANSI-BBS, a Commodore's PETSCII,
* Viewdata ...), kept in dmodem.ini.
*/
#include <stdio.h>
#include <string.h>
@@ -16,335 +14,26 @@
#include "app.h"
#include "resource.h"
static HWND page, term, type_box;
static HWND page, term;
static dmc_vt *vt;
static int cw = 8, ch = 16; /* a cell's size */
static int scroll; /* 0 the screen, -n back in history */
static bool focused;
/* the brand's background and text (D-Modem/brand: docs/palette.md) */
static const COLORREF BG = RGB(0x0a, 0x0f, 0x1c), FG = RGB(0xe8, 0xf0, 0xff);
static COLORREF colour(int c, COLORREF def)
static void ini(char *out, int n)
{
if (c < 0)
return def;
unsigned long v = dmc_vt_palette(c);
return RGB((v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF);
char dir[MAX_PATH];
app_data_dir(dir, sizeof(dir));
_snprintf(out, n, "%s\\dmodem.ini", dir);
out[n - 1] = '\0';
}
static void update_scrollbar()
static void typed(const char *data, int n, void *ctx)
{
SCROLLINFO si;
memset(&si, 0, sizeof(si));
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = 0;
si.nMax = dmc_vt_history(vt) + dmc_vt_rows(vt) - 1;
si.nPage = dmc_vt_rows(vt);
si.nPos = dmc_vt_history(vt) + scroll;
SetScrollInfo(term, SB_VERT, &si, TRUE);
}
static void place_caret()
{
if (!focused)
return;
if (scroll == 0 && dmc_vt_cursor_visible(vt)) {
SetCaretPos(dmc_vt_cursor_col(vt) * cw,
dmc_vt_cursor_row(vt) * ch + ch - 2);
ShowCaret(term);
} else {
HideCaret(term);
}
}
static void paint(HDC dc, RECT *area)
{
RECT r;
GetClientRect(term, &r);
HDC mem = CreateCompatibleDC(dc);
HBITMAP bmp = CreateCompatibleBitmap(dc, r.right, r.bottom);
HGDIOBJ oldb = SelectObject(mem, bmp);
HGDIOBJ oldf = SelectObject(mem, app.mono_font);
HBRUSH bg = CreateSolidBrush(BG);
FillRect(mem, &r, bg);
DeleteObject(bg);
int rows = dmc_vt_rows(vt), cols = dmc_vt_cols(vt);
WCHAR text[512];
for (int y = 0; y < rows; y++) {
int row = y + scroll;
int x = 0;
while (x < cols) {
int fg = dmc_vt_fg(vt, row, x), bgc = dmc_vt_bg(vt, row, x);
int at = dmc_vt_attr(vt, row, x);
int n = 0;
while (x + n < cols && n < 511 &&
dmc_vt_fg(vt, row, x + n) == fg &&
dmc_vt_bg(vt, row, x + n) == bgc &&
dmc_vt_attr(vt, row, x + n) == at) {
unsigned long c = dmc_vt_char(vt, row, x + n);
text[n] = c < 0x10000 ? (WCHAR)c : (WCHAR)'?';
n++;
}
/* bold as the bright colours, as a PC's text mode */
int f = fg >= 0 && fg < 8 && (at & DMC_BOLD) ? fg + 8 : fg;
COLORREF fc = colour(f, FG), bc = colour(bgc, BG);
if (at & DMC_REVERSE) {
COLORREF t = fc;
fc = bc;
bc = t;
}
SetTextColor(mem, fc);
SetBkColor(mem, bc);
RECT cell = { x * cw, y * ch, (x + n) * cw, (y + 1) * ch };
/* each character in its cell, whatever the font */
INT dx[512];
for (int i = 0; i < n; i++)
dx[i] = cw;
ExtTextOutW(mem, x * cw, y * ch, ETO_OPAQUE | ETO_CLIPPED,
&cell, text, n, dx);
if (at & DMC_UNDERLINE) {
RECT u = { x * cw, (y + 1) * ch - 1, (x + n) * cw,
(y + 1) * ch };
HBRUSH b = CreateSolidBrush(fc);
FillRect(mem, &u, b);
DeleteObject(b);
}
x += n;
}
}
BitBlt(dc, area->left, area->top, area->right - area->left,
area->bottom - area->top, mem, area->left, area->top, SRCCOPY);
SelectObject(mem, oldf);
SelectObject(mem, oldb);
DeleteObject(bmp);
DeleteDC(mem);
}
static void measure()
{
HDC dc = GetDC(term);
HGDIOBJ old = SelectObject(dc, app.mono_font);
TEXTMETRIC tm;
GetTextMetrics(dc, &tm);
cw = tm.tmAveCharWidth > 0 ? tm.tmAveCharWidth : 8;
ch = tm.tmHeight > 0 ? tm.tmHeight : 16;
SelectObject(dc, old);
ReleaseDC(term, dc);
}
static void send_text(const char *s, int n)
{
link_type(s, n);
}
/* a character the keyboard gave, in the ANSI code page, as UTF-8 */
static void send_char(unsigned char c)
{
if (c < 0x80) {
char b = (char)c;
send_text(&b, 1);
return;
}
WCHAR w;
char mb = (char)c;
if (MultiByteToWideChar(CP_ACP, 0, &mb, 1, &w, 1) != 1)
return;
char o[4];
send_text(o, utf8_encode(w, o));
}
static void paste()
{
if (!OpenClipboard(term))
return;
HANDLE h = GetClipboardData(CF_TEXT);
if (h) {
const char *t = (const char *)GlobalLock(h);
if (t) {
for (const char *p = t; *p; p++) {
/* a CR LF as the one CR a terminal sends */
if (*p == '\n' && p > t && p[-1] == '\r')
continue;
send_char(*p == '\n' ? '\r' : (unsigned char)*p);
}
GlobalUnlock(h);
}
}
CloseClipboard();
}
static void to_bottom()
{
if (scroll) {
scroll = 0;
update_scrollbar();
InvalidateRect(term, NULL, FALSE);
}
}
static LRESULT CALLBACK term_proc(HWND w, UINT m, WPARAM wp, LPARAM lp)
{
switch (m) {
case WM_GETDLGCODE:
return DLGC_WANTALLKEYS | DLGC_WANTARROWS | DLGC_WANTCHARS |
DLGC_WANTTAB;
case WM_SETFOCUS:
focused = true;
CreateCaret(w, NULL, cw, 2);
place_caret();
return 0;
case WM_KILLFOCUS:
focused = false;
DestroyCaret();
return 0;
case WM_LBUTTONDOWN:
SetFocus(w);
return 0;
case WM_RBUTTONUP:
paste();
return 0;
case WM_ERASEBKGND:
return 1;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC dc = BeginPaint(w, &ps);
paint(dc, &ps.rcPaint);
EndPaint(w, &ps);
return 0;
}
case WM_SIZE: {
int cols = LOWORD(lp) / cw, rows = HIWORD(lp) / ch;
if (cols < 20)
cols = 20;
if (rows < 5)
rows = 5;
if (cols > 160)
cols = 160;
if (rows > 66)
rows = 66;
dmc_vt_resize(vt, cols, rows);
update_scrollbar();
InvalidateRect(w, NULL, FALSE);
return 0;
}
case WM_VSCROLL: {
int hist = dmc_vt_history(vt), pos = hist + scroll;
switch (LOWORD(wp)) {
case SB_LINEUP: pos--; break;
case SB_LINEDOWN: pos++; break;
case SB_PAGEUP: pos -= dmc_vt_rows(vt); break;
case SB_PAGEDOWN: pos += dmc_vt_rows(vt); break;
case SB_THUMBTRACK:
case SB_THUMBPOSITION: pos = HIWORD(wp); break;
case SB_TOP: pos = 0; break;
case SB_BOTTOM: pos = hist; break;
}
if (pos < 0)
pos = 0;
if (pos > hist)
pos = hist;
scroll = pos - hist;
update_scrollbar();
place_caret();
InvalidateRect(w, NULL, FALSE);
return 0;
}
case WM_KEYDOWN: {
int key = 0;
bool shift = GetKeyState(VK_SHIFT) < 0;
switch (wp) {
case VK_UP: key = DMC_KEY_UP; break;
case VK_DOWN: key = DMC_KEY_DOWN; break;
case VK_LEFT: key = DMC_KEY_LEFT; break;
case VK_RIGHT: key = DMC_KEY_RIGHT; break;
case VK_HOME: key = DMC_KEY_HOME; break;
case VK_END: key = DMC_KEY_END; break;
case VK_PRIOR:
if (shift) {
SendMessage(w, WM_VSCROLL, SB_PAGEUP, 0);
return 0;
}
key = DMC_KEY_PGUP;
break;
case VK_NEXT:
if (shift) {
SendMessage(w, WM_VSCROLL, SB_PAGEDOWN, 0);
return 0;
}
key = DMC_KEY_PGDN;
break;
case VK_INSERT:
if (shift) {
paste();
return 0;
}
key = DMC_KEY_INSERT;
break;
case VK_DELETE: key = DMC_KEY_DELETE; break;
case VK_TAB:
if (shift)
key = DMC_KEY_BACKTAB;
break;
default:
if (wp >= VK_F1 && wp <= VK_F10)
key = DMC_KEY_F1 + (int)(wp - VK_F1);
break;
}
if (!key)
break;
char b[16];
int n = dmc_vt_key(vt, key, b, sizeof(b));
to_bottom();
send_text(b, n);
return 0;
}
case WM_SYSKEYDOWN:
/* F10 is the configurator's menu, not Windows' */
if (wp == VK_F10) {
char b[16];
int n = dmc_vt_key(vt, DMC_KEY_F10, b, sizeof(b));
send_text(b, n);
return 0;
}
break;
case WM_SYSCHAR:
/* Alt and a letter: ESC and it, as a terminal sends them */
if (wp >= 0x20 && wp < 0x7f) {
char b[2] = { 0x1b, (char)wp };
to_bottom();
send_text(b, 2);
return 0;
}
break;
case WM_CHAR:
if (wp == '\t' && GetKeyState(VK_SHIFT) < 0)
return 0; /* (Shift-Tab went as a key) */
to_bottom();
send_char((unsigned char)wp);
return 0;
}
return DefWindowProc(w, m, wp, lp);
(void)ctx;
link_type(data, n);
}
void term_feed(const char *data, int n)
{
if (!vt)
return;
int before = dmc_vt_history(vt);
dmc_vt_feed(vt, data, n);
/* (looking back: the view stays where it was) */
if (scroll)
scroll -= dmc_vt_history(vt) - before;
char r[64];
int k;
while ((k = dmc_vt_reply(vt, r, sizeof(r))) > 0)
send_text(r, k);
update_scrollbar();
place_caret();
InvalidateRect(term, NULL, FALSE);
if (term)
termview_feed(term, data, n);
}
void term_state()
@@ -364,10 +53,18 @@ static void typed_command(const char *cmd)
SetFocus(term);
}
static void type_changed()
{
const char *id = term_types_chosen(type_box);
termview_set_profile(term, id);
char path[MAX_PATH];
ini(path, sizeof(path));
WritePrivateProfileString("terminal", "type", id, path);
}
static void term_command(HWND p, int id, int code)
{
(void)p;
(void)code;
switch (id) {
case IDC_SETUP_CF:
typed_command("AT!CF\r");
@@ -376,34 +73,34 @@ static void term_command(HWND p, int id, int code)
typed_command("AT!CT\r");
break;
case IDC_CLEAR:
dmc_vt_reset(vt);
scroll = 0;
update_scrollbar();
InvalidateRect(term, NULL, FALSE);
termview_reset(term);
SetFocus(term);
break;
case IDC_TERM_TYPE:
if (code == CBN_SELCHANGE)
type_changed();
break;
}
}
HWND term_page_new(HWND parent)
{
WNDCLASS wc;
memset(&wc, 0, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = term_proc;
wc.hInstance = app.inst;
wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
wc.lpszClassName = "DModemTerminal";
RegisterClass(&wc);
vt = dmc_vt_new(80, 25, 2000);
page = page_new(parent, term_command);
make_child("BUTTON", "&Full-Screen Setup", WS_TABSTOP, page, IDC_SETUP_CF);
make_child("BUTTON", "&Command-Line Setup", WS_TABSTOP, page,
IDC_SETUP_CT);
make_child("STATIC", "&Type:", SS_RIGHT, page, IDC_TERM_TYPELABEL);
type_box = make_child("COMBOBOX", "", WS_TABSTOP | CBS_DROPDOWNLIST |
WS_VSCROLL, page, IDC_TERM_TYPE);
make_child("BUTTON", "C&lear", WS_TABSTOP, page, IDC_CLEAR);
term = make_child("DModemTerminal", "", WS_TABSTOP | WS_VSCROLL, page,
IDC_TERM, WS_EX_CLIENTEDGE);
measure();
term = termview_new(page, IDC_TERM, typed, NULL);
/* the modem's configurator wants Backspace as BS */
termview_set_backspace(term, 0x08);
char path[MAX_PATH], v[64];
ini(path, sizeof(path));
GetPrivateProfileString("terminal", "type", "xterm", v, sizeof(v), path);
term_types_fill(type_box, v);
termview_set_profile(term, term_types_chosen(type_box));
return page;
}
@@ -412,6 +109,14 @@ void term_page_layout(HWND p, int w, int h)
int m = 8, bh = 24;
MoveWindow(GetDlgItem(p, IDC_SETUP_CF), m, m, 140, bh, TRUE);
MoveWindow(GetDlgItem(p, IDC_SETUP_CT), m + 148, m, 150, bh, TRUE);
int tx = m + 148 + 158;
int tw = w - tx - 2 * m - 80 - 44;
if (tw > 220)
tw = 220;
if (tw < 100)
tw = 100;
MoveWindow(GetDlgItem(p, IDC_TERM_TYPELABEL), tx, m + 4, 40, bh, TRUE);
MoveWindow(type_box, tx + 44, m, tw, 300, TRUE);
MoveWindow(GetDlgItem(p, IDC_CLEAR), w - m - 80, m, 80, bh, TRUE);
MoveWindow(term, m, 2 * m + bh, w - 2 * m, h - 3 * m - bh, TRUE);
}
+855 -198
View File
File diff suppressed because it is too large. Load diff