Remote page on Windows: Telnet and rlogin over Winsock 1.1, SSH by Windows' ssh.exe in ConPTY

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSV65geuGd6zMt2V6r
This commit is contained in:
Claude committed 2026-11-12 12:26:14 +00:00
1 parent f1451eeaaa
commit 2d974d5f1d
6 files changed
+1104 -6

No files matched your search

+2
View File
@@ -11,6 +11,8 @@ add_executable(dmodem WIN32
src/config.cpp
src/voice.cpp
src/audio.cpp
src/termview.cpp
src/remote.cpp
res/app.rc)
target_include_directories(dmodem PRIVATE src)
target_link_libraries(dmodem PRIVATE dmodemctl comctl32 wsock32 comdlg32 gdi32 winmm msacm32)
+24 -1
View File
@@ -76,7 +76,7 @@ typedef void (*ResponseFn)(const dmc_event *ev, void *data);
struct App {
HINSTANCE inst;
HWND main, tabs;
HWND pages[6];
HWND pages[7];
HFONT font, bold_font, big_font, mono_font;
Config cfg;
dmc_session *s;
@@ -104,6 +104,8 @@ void link_wait(int tag, ResponseFn fn, void *data);
#define WM_APP_SOCKET (WM_APP + 1)
#define TIMER_PUMP 1
#define TIMER_VOICE 2
#define WM_APP_REMOTE (WM_APP + 2)
#define TIMER_REMOTE 3
/* main.cpp */
void app_log(const char *line);
@@ -134,6 +136,27 @@ void term_page_layout(HWND page, int w, int h);
void term_feed(const char *data, int n);
void term_state();
/* termview.cpp: a terminal control of its own screen (dmc_vt), whose
keys go to fn */
typedef void (*TermTypedFn)(const char *data, int n, void *ctx);
HWND termview_new(HWND parent, int id, TermTypedFn fn, void *ctx);
/* bytes from the far end */
void termview_feed(HWND w, const char *data, int n);
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);
/* remote.cpp: the Remote page, a login by SSH, Telnet or rlogin */
HWND remote_page_new(HWND parent);
void remote_page_layout(HWND page, int w, int h);
/* its socket's messages (WM_APP_REMOTE) and its timer (TIMER_REMOTE) */
void remote_socket(WPARAM w, LPARAM l);
void remote_tick();
/* the focus to its first control */
bool remote_focus();
void remote_stop();
/* settings.cpp: the Settings page */
HWND settings_page_new(HWND parent);
void settings_page_layout(HWND page, int w, int h);
+17 -5
View File
@@ -2,8 +2,9 @@
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*
* main.cpp - the main window: a tab for each page (Modem, Terminal,
* Settings, Call Stats, Log, as the macOS and Linux apps
* main.cpp - the main window: a tab for each page (Modem, Phone,
* Terminal, Remote, Settings, Call Stats, Log, as the macOS
* and Linux apps
* have them) and a status bar
*/
#include <stdio.h>
@@ -16,10 +17,11 @@ App app;
static HWND status_bar;
enum { PAGE_MODEM, PAGE_PHONE, PAGE_TERM, PAGE_SETTINGS, PAGE_STATS,
PAGE_LOG, PAGES };
enum { PAGE_MODEM, PAGE_PHONE, PAGE_TERM, PAGE_REMOTE, PAGE_SETTINGS,
PAGE_STATS, PAGE_LOG, PAGES };
static const char *const page_names[PAGES] = {
"Modem", "Phone", "Terminal", "Settings", "Call Stats", "Log"
"Modem", "Phone", "Terminal", "Remote", "Settings", "Call Stats",
"Log"
};
HWND make_child(const char *cls, const char *text, DWORD style, HWND parent,
@@ -177,6 +179,8 @@ static void show_page(int i)
ShowWindow(app.pages[p], p == i ? SW_SHOW : SW_HIDE);
if (i == PAGE_TERM)
SetFocus(GetWindow(app.pages[i], GW_CHILD));
else if (i == PAGE_REMOTE)
remote_focus();
}
static void layout()
@@ -199,6 +203,7 @@ static void layout()
modem_page_layout(app.pages[PAGE_MODEM], w, h);
voice_page_layout(app.pages[PAGE_PHONE], w, h);
term_page_layout(app.pages[PAGE_TERM], w, h);
remote_page_layout(app.pages[PAGE_REMOTE], w, h);
settings_page_layout(app.pages[PAGE_SETTINGS], w, h);
stats_page_layout(app.pages[PAGE_STATS], w, h);
log_page_layout(app.pages[PAGE_LOG], w, h);
@@ -233,6 +238,7 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp)
app.pages[PAGE_MODEM] = modem_page_new(w);
app.pages[PAGE_PHONE] = voice_page_new(w);
app.pages[PAGE_TERM] = term_page_new(w);
app.pages[PAGE_REMOTE] = remote_page_new(w);
app.pages[PAGE_SETTINGS] = settings_page_new(w);
app.pages[PAGE_STATS] = stats_page_new(w);
app.pages[PAGE_LOG] = log_page_new(w);
@@ -284,14 +290,20 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp)
case WM_TIMER:
if (wp == TIMER_PUMP || wp == TIMER_VOICE)
link_pump();
else if (wp == TIMER_REMOTE)
remote_tick();
return 0;
case WM_APP_SOCKET:
link_socket(wp, lp);
return 0;
case WM_APP_REMOTE:
remote_socket(wp, lp);
return 0;
case WM_DESTROY:
KillTimer(w, TIMER_PUMP);
KillTimer(w, TIMER_VOICE);
audio_stop();
remote_stop();
if (link_running())
link_stop();
PostQuitMessage(0);
+627
View File
@@ -0,0 +1,627 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*
* remote.cpp - the Remote page: a login to a server by SSH, Telnet or
* rlogin, in a terminal of its own (termview.cpp)
*
* Telnet and rlogin: a Winsock 1.1 socket (WSAAsyncSelect, as the
* modem's link), the protocol libdmodemctl's dmc_net; every Windows from
* 95 on has them. SSH: Windows' own OpenSSH client, ssh.exe, in a
* pseudo console (ConPTY), so it needs Windows 10 1809 or later with
* the OpenSSH client feature; the ConPTY calls are looked up at run
* time, so that the app still starts on Windows 95. Its output is read
* from a timer (PeekNamedPipe), with no threads.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "app.h"
#include "resource.h"
static HWND page, view, 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 };
#define NPROTOS 3
/* what runs: its protocol (0: none) */
static int running;
static int cols, rows;
/* Telnet, rlogin */
static SOCKET sock = INVALID_SOCKET;
static dmc_net *net;
static bool sock_open;
static char *pending;
static int pending_n, pending_cap;
/* SSH: the pseudo console, its pipes, ssh.exe */
static void *hpc;
static HANDLE con_in = INVALID_HANDLE_VALUE, con_out = INVALID_HANDLE_VALUE;
static HANDLE proc;
/*
* ConPTY, looked up at run time (Windows 10 1809 and later)
*/
#ifndef EXTENDED_STARTUPINFO_PRESENT
#define EXTENDED_STARTUPINFO_PRESENT 0x00080000
#endif
#define ATTR_PSEUDOCONSOLE 0x00020016
typedef long (WINAPI *CreatePCFn)(COORD, HANDLE, HANDLE, DWORD, void **);
typedef long (WINAPI *ResizePCFn)(void *, COORD);
typedef void (WINAPI *ClosePCFn)(void *);
typedef BOOL (WINAPI *InitAttrsFn)(void *, DWORD, DWORD, size_t *);
typedef BOOL (WINAPI *UpdateAttrFn)(void *, DWORD, size_t, void *, size_t,
void *, size_t *);
typedef void (WINAPI *DeleteAttrsFn)(void *);
static CreatePCFn create_pc;
static ResizePCFn resize_pc;
static ClosePCFn close_pc;
static InitAttrsFn init_attrs;
static UpdateAttrFn update_attr;
static DeleteAttrsFn delete_attrs;
/* STARTUPINFOEX, for headers without it */
struct StartupInfoEx {
STARTUPINFOA si;
void *attrs;
};
/* (through a plain function pointer: the cast that compilers take) */
#define LOOKUP(k, name) (void (*)(void))GetProcAddress(k, name)
static bool conpty_available()
{
HMODULE k = GetModuleHandle("kernel32.dll");
if (!k)
return false;
create_pc = (CreatePCFn)LOOKUP(k, "CreatePseudoConsole");
resize_pc = (ResizePCFn)LOOKUP(k, "ResizePseudoConsole");
close_pc = (ClosePCFn)LOOKUP(k, "ClosePseudoConsole");
init_attrs = (InitAttrsFn)LOOKUP(k, "InitializeProcThreadAttributeList");
update_attr = (UpdateAttrFn)LOOKUP(k, "UpdateProcThreadAttribute");
delete_attrs = (DeleteAttrsFn)LOOKUP(k, "DeleteProcThreadAttributeList");
return create_pc && resize_pc && close_pc && init_attrs &&
update_attr && delete_attrs;
}
static bool exists(const char *p)
{
DWORD a = GetFileAttributes(p);
return a != 0xFFFFFFFF && !(a & FILE_ATTRIBUTE_DIRECTORY);
}
/* ssh.exe: on the PATH, or Windows' own (Sysnative: the 64-bit System32
seen from this 32-bit app) */
static bool find_ssh(char *out, int n)
{
char *part;
if (SearchPath(NULL, "ssh.exe", NULL, (DWORD)n, out, &part))
return true;
char root[MAX_PATH];
if (!GetEnvironmentVariable("SystemRoot", root, sizeof(root)))
return false;
static const char *const dirs[] = { "Sysnative", "System32" };
for (int i = 0; i < 2; i++) {
_snprintf(out, n, "%s\\%s\\OpenSSH\\ssh.exe", root, dirs[i]);
out[n - 1] = '\0';
if (exists(out))
return true;
}
return false;
}
/*
* the page
*/
static void set_status(const char *s)
{
char a[300];
to_ansi(s, a, sizeof(a));
SetWindowText(status_txt, a);
}
static void controls()
{
bool idle = !running;
EnableWindow(proto_box, idle);
EnableWindow(host_ed, idle);
EnableWindow(port_ed, idle);
EnableWindow(user_ed, idle);
SetWindowText(go_btn, idle ? "&Connect" : "&Disconnect");
}
static void show(const char *s, int n)
{
termview_feed(view, s, n);
}
static void show_line(const char *s)
{
show(s, (int)strlen(s));
}
static void ended(const char *why)
{
if (!running)
return;
running = 0;
KillTimer(app.main, TIMER_REMOTE);
if (sock != INVALID_SOCKET) {
WSAAsyncSelect(sock, app.main, 0, 0);
closesocket(sock);
sock = INVALID_SOCKET;
}
sock_open = false;
if (net) {
dmc_net_free(net);
net = NULL;
}
pending_n = 0;
if (proc) {
TerminateProcess(proc, 0);
CloseHandle(proc);
proc = NULL;
}
/* (our end of its output shut first, so that closing the console
can't wait on a full pipe) */
if (con_out != INVALID_HANDLE_VALUE) {
CloseHandle(con_out);
con_out = INVALID_HANDLE_VALUE;
}
if (hpc) {
close_pc(hpc);
hpc = NULL;
}
if (con_in != INVALID_HANDLE_VALUE) {
CloseHandle(con_in);
con_in = INVALID_HANDLE_VALUE;
}
char line[300];
_snprintf(line, sizeof(line), "\r\n\x1b[0m[%s]\r\n",
why ? why : "Disconnected");
line[sizeof(line) - 1] = '\0';
show_line(line);
set_status(why ? why : "Disconnected");
controls();
}
/* Telnet and rlogin: the protocol's output written, as much as goes */
static void flush()
{
char buf[2048];
int n;
while (net && (n = dmc_net_output(net, buf, sizeof(buf))) > 0) {
if (pending_n + n > pending_cap) {
pending_cap = (pending_n + n) * 2;
pending = (char *)realloc(pending, (size_t)pending_cap);
}
memcpy(pending + pending_n, buf, (size_t)n);
pending_n += n;
}
while (sock_open && pending_n > 0) {
int w = send(sock, pending, pending_n, 0);
if (w == SOCKET_ERROR) {
if (WSAGetLastError() == WSAEWOULDBLOCK)
return; /* FD_WRITE: more */
ended("The connection failed");
return;
}
memmove(pending, pending + w, (size_t)(pending_n - w));
pending_n -= w;
}
}
static void pump()
{
char buf[4096];
int n;
while (net && (n = dmc_net_screen(net, buf, sizeof(buf))) > 0)
show(buf, n);
flush();
}
/* what the terminal gives: to the far end */
static void typed(const char *data, int n, void *ctx)
{
(void)ctx;
if (!running)
return;
if (net && sock_open) {
dmc_net_type(net, data, n);
flush();
} else if (con_in != INVALID_HANDLE_VALUE) {
DWORD w;
WriteFile(con_in, data, (DWORD)n, &w, NULL);
}
}
void remote_socket(WPARAM wp, LPARAM lp)
{
if ((SOCKET)wp != sock || !net)
return;
int err = WSAGETSELECTERROR(lp);
switch (WSAGETSELECTEVENT(lp)) {
case FD_CONNECT:
if (err) {
char m[160];
_snprintf(m, sizeof(m), "Could not connect (error %d)", err);
m[sizeof(m) - 1] = '\0';
ended(m);
return;
}
sock_open = true;
set_status("Connected");
dmc_net_connected(net);
pump();
break;
case FD_OOB: {
char b;
if (recv(sock, &b, 1, MSG_OOB) == 1)
dmc_net_urgent(net, (unsigned char)b);
pump();
break;
}
case FD_READ: {
char buf[4096];
int n = recv(sock, buf, sizeof(buf), 0);
if (n > 0)
dmc_net_feed(net, buf, n);
pump();
break;
}
case FD_WRITE:
flush();
break;
case FD_CLOSE: {
char buf[4096];
int n;
while ((n = recv(sock, buf, sizeof(buf), 0)) > 0)
dmc_net_feed(net, buf, n);
pump();
ended("Connection closed by the server");
break;
}
}
}
/* the timer: ssh's output, its end, and the terminal's size */
void remote_tick()
{
if (!running)
return;
int c, r;
termview_size(view, &c, &r);
if (c != cols || r != rows) {
cols = c;
rows = r;
if (net) {
dmc_net_resize(net, c, r);
flush();
} else if (hpc) {
COORD sz;
sz.X = (SHORT)c;
sz.Y = (SHORT)r;
resize_pc(hpc, sz);
}
}
if (con_out == INVALID_HANDLE_VALUE)
return;
for (;;) {
DWORD avail = 0;
if (!PeekNamedPipe(con_out, NULL, 0, NULL, &avail, NULL) ||
!avail)
break;
char buf[4096];
DWORD n = 0;
if (!ReadFile(con_out, buf, avail < sizeof(buf) ? avail :
sizeof(buf), &n, NULL) || !n)
break;
show(buf, (int)n);
}
if (proc && WaitForSingleObject(proc, 0) == WAIT_OBJECT_0) {
DWORD code = 0;
GetExitCodeProcess(proc, &code);
char m[80];
if (code)
_snprintf(m, sizeof(m), "ssh exited with status %lu",
(unsigned long)code);
else
_snprintf(m, sizeof(m), "Connection closed");
m[sizeof(m) - 1] = '\0';
ended(m);
}
}
static bool start_ssh(const char *host, int port, const char *user)
{
if (!conpty_available()) {
app_message("SSH needs Windows 10 (1809) or later, with "
"OpenSSH's client");
return false;
}
char ssh[MAX_PATH];
if (!find_ssh(ssh, sizeof(ssh))) {
app_message("ssh.exe was not found: add Windows' OpenSSH "
"Client (Settings, Optional features)");
return false;
}
HANDLE in_r, in_w, out_r, out_w;
if (!CreatePipe(&in_r, &in_w, NULL, 0))
return false;
if (!CreatePipe(&out_r, &out_w, NULL, 0)) {
CloseHandle(in_r);
CloseHandle(in_w);
return false;
}
COORD sz;
sz.X = (SHORT)cols;
sz.Y = (SHORT)rows;
if (create_pc(sz, in_r, out_w, 0, &hpc) < 0) {
hpc = NULL;
CloseHandle(in_r);
CloseHandle(in_w);
CloseHandle(out_r);
CloseHandle(out_w);
app_message("Could not make a console for ssh");
return false;
}
/* (the console has its own copies) */
CloseHandle(in_r);
CloseHandle(out_w);
con_in = in_w;
con_out = out_r;
size_t size = 0;
init_attrs(NULL, 1, 0, &size);
void *attrs = malloc(size);
StartupInfoEx si;
memset(&si, 0, sizeof(si));
si.si.cb = sizeof(si);
si.attrs = attrs;
bool ok = attrs && init_attrs(attrs, 1, 0, &size) &&
update_attr(attrs, 0, ATTR_PSEUDOCONSOLE, hpc, sizeof(hpc),
NULL, NULL);
char cmd[1024];
if (*user)
_snprintf(cmd, sizeof(cmd), "\"%s\" -p %d -l \"%s\" -- \"%s\"",
ssh, port, user, host);
else
_snprintf(cmd, sizeof(cmd), "\"%s\" -p %d -- \"%s\"", ssh, port,
host);
cmd[sizeof(cmd) - 1] = '\0';
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(pi));
SetEnvironmentVariable("TERM", "xterm");
if (ok)
ok = CreateProcess(NULL, cmd, NULL, NULL, FALSE,
EXTENDED_STARTUPINFO_PRESENT, NULL, NULL,
(STARTUPINFOA *)&si, &pi) != 0;
if (attrs) {
delete_attrs(attrs);
free(attrs);
}
if (!ok) {
running = DMC_NET_SSH; /* (for ended() to tidy up) */
ended("Could not start ssh.exe");
return false;
}
CloseHandle(pi.hThread);
proc = pi.hProcess;
return true;
}
static bool start_socket(int proto, const char *host, int port,
const char *user)
{
sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons((u_short)port);
sa.sin_addr.s_addr = inet_addr(host);
if (sa.sin_addr.s_addr == INADDR_NONE) {
hostent *h = gethostbyname(host);
if (!h) {
app_message("That server's name was not found");
return false;
}
memcpy(&sa.sin_addr, h->h_addr, sizeof(sa.sin_addr));
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
return false;
net = dmc_net_new(proto, user, "ansi", cols, rows);
sock_open = false;
WSAAsyncSelect(sock, app.main, WM_APP_REMOTE,
FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE | FD_OOB);
connect(sock, (sockaddr *)&sa, sizeof(sa));
return true;
}
static int chosen_proto()
{
int i = (int)SendMessage(proto_box, CB_GETCURSEL, 0, 0);
return i >= 0 && i < NPROTOS ? protos[i] : DMC_NET_SSH;
}
static void ini(char *out, int n)
{
char dir[MAX_PATH];
app_data_dir(dir, sizeof(dir));
_snprintf(out, n, "%s\\dmodem.ini", dir);
out[n - 1] = '\0';
}
static void connect_clicked()
{
if (running) {
ended(NULL);
return;
}
char host[256], user[128], pt[16];
GetWindowText(host_ed, host, sizeof(host));
GetWindowText(user_ed, user, sizeof(user));
GetWindowText(port_ed, pt, sizeof(pt));
int proto = chosen_proto();
int port = pt[0] ? atoi(pt) : dmc_net_default_port(proto);
if (!host[0]) {
app_message("Enter the server to connect to");
return;
}
if (strpbrk(host, " \"") || strchr(user, '"')) {
app_message("The server's name has no spaces or quotes");
return;
}
if (port <= 0 || port > 65535) {
app_message("The port is a number from 1 to 65535");
return;
}
if (proto == DMC_NET_RLOGIN && !user[0]) {
app_message("rlogin needs a user name");
return;
}
/* kept for next time */
char path[MAX_PATH];
ini(path, sizeof(path));
WritePrivateProfileString("remote", "protocol", dmc_net_name(proto), path);
WritePrivateProfileString("remote", "host", host, path);
WritePrivateProfileString("remote", "port", pt, path);
WritePrivateProfileString("remote", "user", user, path);
termview_reset(view);
termview_size(view, &cols, &rows);
char what[400];
_snprintf(what, sizeof(what), "Connecting to %s port %d by %s", host,
port, dmc_net_name(proto));
what[sizeof(what) - 1] = '\0';
set_status(what);
char line[420];
_snprintf(line, sizeof(line), "[%s]\r\n", what);
line[sizeof(line) - 1] = '\0';
show_line(line);
bool ok = proto == DMC_NET_SSH ? start_ssh(host, port, user) :
start_socket(proto, host, port, user);
if (!ok) {
set_status("Not connected");
return;
}
if (proto == DMC_NET_SSH)
set_status("Connected by SSH");
running = proto;
SetTimer(app.main, TIMER_REMOTE, 30, NULL);
controls();
SetFocus(view);
}
static void proto_changed()
{
char p[16];
_snprintf(p, sizeof(p), "%d", dmc_net_default_port(chosen_proto()));
p[sizeof(p) - 1] = '\0';
/* (the default shown when the port is left empty) */
char cur[16];
GetWindowText(port_ed, cur, sizeof(cur));
if (!cur[0] || !strcmp(cur, "22") || !strcmp(cur, "23") ||
!strcmp(cur, "513"))
SetWindowText(port_ed, p);
}
static void remote_command(HWND p, int id, int code)
{
(void)p;
switch (id) {
case IDC_R_GO:
connect_clicked();
break;
case IDC_R_PROTO:
if (code == CBN_SELCHANGE)
proto_changed();
break;
}
}
HWND remote_page_new(HWND parent)
{
page = page_new(parent, remote_command);
proto_box = make_child("COMBOBOX", "", WS_TABSTOP | CBS_DROPDOWNLIST |
WS_VSCROLL, page, IDC_R_PROTO);
for (int i = 0; i < NPROTOS; i++)
SendMessage(proto_box, CB_ADDSTRING, 0,
(LPARAM)dmc_net_name(protos[i]));
make_child("STATIC", "&Server:", SS_RIGHT, page, IDC_R_HOSTLABEL);
host_ed = make_child("EDIT", "", WS_TABSTOP | ES_AUTOHSCROLL, page,
IDC_R_HOST, WS_EX_CLIENTEDGE);
make_child("STATIC", "&Port:", SS_RIGHT, page, IDC_R_PORTLABEL);
port_ed = make_child("EDIT", "", WS_TABSTOP | ES_NUMBER, page,
IDC_R_PORT, WS_EX_CLIENTEDGE);
make_child("STATIC", "&User:", SS_RIGHT, page, IDC_R_USERLABEL);
user_ed = make_child("EDIT", "", WS_TABSTOP | ES_AUTOHSCROLL, page,
IDC_R_USER, WS_EX_CLIENTEDGE);
go_btn = make_child("BUTTON", "&Connect", WS_TABSTOP | BS_DEFPUSHBUTTON,
page, IDC_R_GO);
view = termview_new(page, IDC_R_TERM, typed, NULL);
status_txt = make_child("STATIC", "Not connected", SS_LEFTNOWORDWRAP,
page, IDC_R_STATUS);
/* what was used last */
char path[MAX_PATH], v[256];
ini(path, sizeof(path));
GetPrivateProfileString("remote", "protocol", "SSH", v, sizeof(v), path);
int sel = 0;
for (int i = 0; i < NPROTOS; i++)
if (!lstrcmpi(v, dmc_net_name(protos[i])))
sel = i;
SendMessage(proto_box, CB_SETCURSEL, (WPARAM)sel, 0);
GetPrivateProfileString("remote", "host", "", v, sizeof(v), path);
SetWindowText(host_ed, v);
GetPrivateProfileString("remote", "port", "", v, sizeof(v), path);
SetWindowText(port_ed, v);
GetPrivateProfileString("remote", "user", "", v, sizeof(v), path);
SetWindowText(user_ed, v);
proto_changed();
controls();
return page;
}
void remote_page_layout(HWND p, int w, int h)
{
int m = 8, bh = 24, x = m;
MoveWindow(proto_box, x, m, 80, 200, TRUE);
x += 88;
MoveWindow(GetDlgItem(p, IDC_R_HOSTLABEL), x, m + 4, 44, bh, TRUE);
x += 48;
int hw = w - x - 44 - 52 - 40 - 96 - 90 - 4 * m;
if (hw < 80)
hw = 80;
MoveWindow(host_ed, x, m, hw, bh, TRUE);
x += hw + m;
MoveWindow(GetDlgItem(p, IDC_R_PORTLABEL), x, m + 4, 32, bh, TRUE);
x += 36;
MoveWindow(port_ed, x, m, 52, bh, TRUE);
x += 52 + m;
MoveWindow(GetDlgItem(p, IDC_R_USERLABEL), x, m + 4, 36, bh, TRUE);
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);
}
bool remote_focus()
{
SetFocus(running ? view : host_ed);
return true;
}
void remote_stop()
{
if (running)
ended(NULL);
}
+12
View File
@@ -39,6 +39,18 @@
#define IDC_SETUP_CT 502
#define IDC_CLEAR 503
/* the Remote page */
#define IDC_R_PROTO 1100
#define IDC_R_HOSTLABEL 1101
#define IDC_R_HOST 1102
#define IDC_R_PORTLABEL 1103
#define IDC_R_PORT 1104
#define IDC_R_USERLABEL 1105
#define IDC_R_USER 1106
#define IDC_R_GO 1107
#define IDC_R_TERM 1108
#define IDC_R_STATUS 1109
/* the Settings page */
#define IDC_GROUPS 600
#define IDC_SETTINGS 601
+422
View File
@@ -0,0 +1,422 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*
* termview.cpp - a terminal control: a screen of libdmodemctl's dmc_vt,
* drawn with GDI, its keys handed to whoever owns it.
* The Remote page's terminal; each control has a screen
* of its own, so that there can be more than one.
*
* The text is drawn with ExtTextOutW, which Windows 95 has. Keys are a
* VT100's; the right button pastes; Backspace is DEL unless the owner
* says otherwise.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "app.h"
struct TermView {
HWND w;
dmc_vt *vt;
int cw, ch; /* a cell's size */
int scroll; /* 0 the screen, -n back in history */
bool focused;
char backspace;
TermTypedFn fn;
void *ctx;
};
static const COLORREF BG = RGB(0x0a, 0x0f, 0x1c), FG = RGB(0xe8, 0xf0, 0xff);
static TermView *of(HWND w)
{
return (TermView *)GetWindowLongPtr(w, GWLP_USERDATA);
}
static COLORREF colour(int c, COLORREF def)
{
if (c < 0)
return def;
unsigned long v = dmc_vt_palette(c);
return RGB((v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF);
}
static void update_scrollbar(TermView *t)
{
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(t->vt) + dmc_vt_rows(t->vt) - 1;
si.nPage = dmc_vt_rows(t->vt);
si.nPos = dmc_vt_history(t->vt) + t->scroll;
SetScrollInfo(t->w, SB_VERT, &si, TRUE);
}
static void place_caret(TermView *t)
{
if (!t->focused)
return;
if (t->scroll == 0 && dmc_vt_cursor_visible(t->vt)) {
SetCaretPos(dmc_vt_cursor_col(t->vt) * t->cw,
dmc_vt_cursor_row(t->vt) * t->ch + t->ch - 2);
ShowCaret(t->w);
} else {
HideCaret(t->w);
}
}
static void paint(TermView *t, HDC dc, RECT *area)
{
RECT r;
GetClientRect(t->w, &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);
dmc_vt *vt = t->vt;
int rows = dmc_vt_rows(vt), cols = dmc_vt_cols(vt);
int cw = t->cw, ch = t->ch;
WCHAR text[512];
INT dx[512];
for (int y = 0; y < rows; y++) {
int row = y + t->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)'?';
dx[n] = cw;
n++;
}
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 s = fc;
fc = bc;
bc = s;
}
SetTextColor(mem, fc);
SetBkColor(mem, bc);
RECT cell = { x * cw, y * ch, (x + n) * cw, (y + 1) * ch };
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(TermView *t)
{
HDC dc = GetDC(t->w);
HGDIOBJ old = SelectObject(dc, app.mono_font);
TEXTMETRIC tm;
GetTextMetrics(dc, &tm);
t->cw = tm.tmAveCharWidth > 0 ? tm.tmAveCharWidth : 8;
t->ch = tm.tmHeight > 0 ? tm.tmHeight : 16;
SelectObject(dc, old);
ReleaseDC(t->w, dc);
}
static void send_text(TermView *t, const char *s, int n)
{
if (t->fn && n > 0)
t->fn(s, n, t->ctx);
}
/* a character the keyboard gave, in the ANSI code page, as UTF-8 */
static void send_char(TermView *t, unsigned char c)
{
if (c == 0x08)
c = (unsigned char)t->backspace;
if (c < 0x80) {
char b = (char)c;
send_text(t, &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(t, o, utf8_encode(w, o));
}
static void paste(TermView *t)
{
if (!OpenClipboard(t->w))
return;
HANDLE h = GetClipboardData(CF_TEXT);
if (h) {
const char *s = (const char *)GlobalLock(h);
if (s) {
for (const char *p = s; *p; p++) {
if (*p == '\n' && p > s && p[-1] == '\r')
continue;
send_char(t, *p == '\n' ? '\r' : (unsigned char)*p);
}
GlobalUnlock(h);
}
}
CloseClipboard();
}
static void to_bottom(TermView *t)
{
if (t->scroll) {
t->scroll = 0;
update_scrollbar(t);
InvalidateRect(t->w, NULL, FALSE);
}
}
static void send_key(TermView *t, int key)
{
char b[16];
int n = dmc_vt_key(t->vt, key, b, sizeof(b));
to_bottom(t);
send_text(t, b, n);
}
static LRESULT CALLBACK view_proc(HWND w, UINT m, WPARAM wp, LPARAM lp)
{
TermView *t = of(w);
if (!t)
return DefWindowProc(w, m, wp, lp);
switch (m) {
case WM_GETDLGCODE:
return DLGC_WANTALLKEYS | DLGC_WANTARROWS | DLGC_WANTCHARS |
DLGC_WANTTAB;
case WM_SETFOCUS:
t->focused = true;
CreateCaret(w, NULL, t->cw, 2);
place_caret(t);
return 0;
case WM_KILLFOCUS:
t->focused = false;
DestroyCaret();
return 0;
case WM_LBUTTONDOWN:
SetFocus(w);
return 0;
case WM_RBUTTONUP:
paste(t);
return 0;
case WM_ERASEBKGND:
return 1;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC dc = BeginPaint(w, &ps);
paint(t, dc, &ps.rcPaint);
EndPaint(w, &ps);
return 0;
}
case WM_SIZE: {
int cols = LOWORD(lp) / t->cw, rows = HIWORD(lp) / t->ch;
if (cols < 20)
cols = 20;
if (rows < 5)
rows = 5;
if (cols > 200)
cols = 200;
if (rows > 80)
rows = 80;
dmc_vt_resize(t->vt, cols, rows);
update_scrollbar(t);
InvalidateRect(w, NULL, FALSE);
return 0;
}
case WM_VSCROLL: {
int hist = dmc_vt_history(t->vt), pos = hist + t->scroll;
switch (LOWORD(wp)) {
case SB_LINEUP: pos--; break;
case SB_LINEDOWN: pos++; break;
case SB_PAGEUP: pos -= dmc_vt_rows(t->vt); break;
case SB_PAGEDOWN: pos += dmc_vt_rows(t->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;
t->scroll = pos - hist;
update_scrollbar(t);
place_caret(t);
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(t);
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;
send_key(t, key);
return 0;
}
case WM_SYSKEYDOWN:
if (wp == VK_F10) {
send_key(t, DMC_KEY_F10);
return 0;
}
break;
case WM_SYSCHAR:
/* Alt and a letter: ESC and it */
if (wp >= 0x20 && wp < 0x7f) {
char b[2] = { 0x1b, (char)wp };
to_bottom(t);
send_text(t, b, 2);
return 0;
}
break;
case WM_CHAR:
if (wp == '\t' && GetKeyState(VK_SHIFT) < 0)
return 0;
to_bottom(t);
send_char(t, (unsigned char)wp);
return 0;
case WM_NCDESTROY:
SetWindowLongPtr(w, GWLP_USERDATA, 0);
dmc_vt_free(t->vt);
free(t);
return 0;
}
return DefWindowProc(w, m, wp, lp);
}
HWND termview_new(HWND parent, int id, TermTypedFn fn, void *ctx)
{
static bool registered;
if (!registered) {
WNDCLASS wc;
memset(&wc, 0, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = view_proc;
wc.hInstance = app.inst;
wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
wc.lpszClassName = "DModemTermView";
RegisterClass(&wc);
registered = true;
}
TermView *t = (TermView *)calloc(1, sizeof(TermView));
t->vt = dmc_vt_new(80, 24, 2000);
t->cw = 8;
t->ch = 16;
t->backspace = 0x7f;
t->fn = fn;
t->ctx = ctx;
HWND w = make_child("DModemTermView", "", WS_TABSTOP | WS_VSCROLL,
parent, id, WS_EX_CLIENTEDGE);
t->w = w;
SetWindowLongPtr(w, GWLP_USERDATA, (LONG_PTR)t);
measure(t);
return w;
}
void termview_feed(HWND w, const char *data, int n)
{
TermView *t = of(w);
if (!t || n <= 0)
return;
int before = dmc_vt_history(t->vt);
dmc_vt_feed(t->vt, data, n);
if (t->scroll)
t->scroll -= dmc_vt_history(t->vt) - before;
char r[64];
int k;
while ((k = dmc_vt_reply(t->vt, r, sizeof(r))) > 0)
send_text(t, r, k);
update_scrollbar(t);
place_caret(t);
InvalidateRect(w, NULL, FALSE);
}
void termview_reset(HWND w)
{
TermView *t = of(w);
if (!t)
return;
dmc_vt_reset(t->vt);
t->scroll = 0;
update_scrollbar(t);
InvalidateRect(w, NULL, FALSE);
}
void termview_size(HWND w, int *cols, int *rows)
{
TermView *t = of(w);
*cols = t ? dmc_vt_cols(t->vt) : 80;
*rows = t ? dmc_vt_rows(t->vt) : 24;
}
void termview_set_backspace(HWND w, char b)
{
TermView *t = of(w);
if (t)
t->backspace = b;
}