Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd9881ee5c | ||
|
|
35d66c85e9 | ||
|
|
17260c78e7 | ||
|
|
1732160f0b | ||
|
|
db55628aee | ||
|
|
3aa0e30269 | ||
|
|
bcf6ff67a2 | ||
|
|
199fe1693d | ||
|
|
10d09fa159 | ||
|
|
bb08cb7d9c | ||
|
|
60b9aa1357 | ||
|
|
15ffd368bf | ||
|
|
6a73746079 | ||
|
|
ce91016223 | ||
|
|
db1c9cd82e | ||
|
|
cc48b23ede | ||
|
|
08008f6341 | ||
|
|
c7c99e64c9 | ||
|
|
3ab5a917e1 | ||
|
|
01fba1b3c4 | ||
|
|
fd53bbb1e0 | ||
|
|
503edd645e | ||
|
|
059e6a88ae | ||
|
|
bdea9ed1cd | ||
|
|
57002b4f3d | ||
|
|
22d7b4d2cd | ||
|
|
f6011c94e6 | ||
|
|
501705dda3 | ||
|
|
5145ac6de2 | ||
|
|
6c0f2134d8 | ||
|
|
7a42c7b97f | ||
|
|
dcdbb6d015 | ||
|
|
45d59037e6 | ||
|
|
74cec8a5cd | ||
|
|
41aaff8ac2 | ||
|
|
697e5b864d | ||
|
|
b0fa5fcf7f | ||
|
|
0d552e258c | ||
|
|
d924bf8860 | ||
|
|
6c52db33cd | ||
|
|
8c4f8a281e | ||
|
|
29c579eff8 | ||
|
|
0c8defb1bd | ||
|
|
8bf35c5724 | ||
|
|
fe5e9af869 | ||
|
|
70b26500b0 | ||
|
|
ab2f587fd1 | ||
|
|
28bb5f1a9e | ||
|
|
187aac18f3 | ||
|
|
631acc0b7b | ||
|
|
275d038c91 | ||
|
|
2be6ffb43f | ||
|
|
e71bb8c84f | ||
|
|
0e09778bb0 | ||
|
|
4c7123dae6 | ||
|
|
d5569eb3ea | ||
|
|
f50966d6cb | ||
|
|
6f60f6b86e | ||
|
|
f62715c44d | ||
|
|
cc166fdbf1 | ||
|
|
9594f748f3 | ||
|
|
6ef8b467a4 | ||
|
|
c19362570d | ||
|
|
f1162b4652 | ||
|
|
3fb3496a0d | ||
|
|
ac0d04d023 | ||
|
|
1aa040a8bd | ||
|
|
b04c3484be | ||
|
|
41412fa903 | ||
|
|
34a3fa6a25 | ||
|
|
f5df154309 | ||
|
|
2a62e169ca | ||
|
|
58bb20579b | ||
|
|
6bcad7896f | ||
|
|
2750a32e44 | ||
|
|
01b742b3af | ||
|
|
33a8d781c3 | ||
|
|
642a9fb78b | ||
|
|
63a730b705 | ||
|
|
128a630a05 | ||
|
|
965fc37f10 | ||
|
|
68ce7b7880 | ||
|
|
a526788487 | ||
|
|
d290f58410 | ||
|
|
a2d76520bb | ||
|
|
582e70615b | ||
|
|
0eaf2c2262 | ||
|
|
36b4b87f01 | ||
|
|
b23e7e6e71 | ||
|
|
dda886c0c9 | ||
|
|
5a72f9480f | ||
|
|
64c34eabb6 | ||
|
|
d9504f7832 | ||
|
|
71ff294a6e | ||
|
|
60570df580 | ||
|
|
e355362606 | ||
|
|
0f6a687ff7 | ||
|
|
1359ca3d57 | ||
|
|
b4777c0155 | ||
|
|
e0a4719aff | ||
|
|
2a541de663 | ||
|
|
a2ea02173e | ||
|
|
65f9352452 | ||
|
|
54d62776e7 | ||
|
|
406644dbc5 | ||
|
|
f201d28b2f | ||
|
|
6e306417bd | ||
|
|
54b9159198 | ||
|
|
2502165f05 | ||
|
|
828bdac037 | ||
|
|
be0af12441 | ||
|
|
661a9cb066 | ||
|
|
d67c460485 | ||
|
|
1ca2115f4d | ||
|
|
2a2f1fed62 | ||
|
|
3f7f8e8662 | ||
|
|
93520c3413 | ||
|
|
37b73c16a9 | ||
|
|
7363ab73a3 | ||
|
|
917cd53b5f | ||
|
|
bf33dcb6d5 | ||
|
|
66d27a9702 | ||
|
|
2b0eed5d16 | ||
|
|
b8cafb5018 | ||
|
|
7342c374d6 | ||
|
|
0d28b76fa4 | ||
|
|
ca2518ac62 | ||
|
|
b80e912303 | ||
|
|
7e05e1a952 | ||
|
|
0e0cde36ab | ||
|
|
c614bfb364 | ||
|
|
8dd715bd2b | ||
|
|
3371175fec | ||
|
|
526dd567f8 | ||
|
|
c6dba26742 | ||
|
|
504038af21 | ||
|
|
dbe952b308 | ||
|
|
702ee0e72c | ||
|
|
739e16c9e0 | ||
|
|
f6dd29f952 | ||
|
|
bfcee79d52 | ||
|
|
ed35ad2f70 | ||
|
|
7ebf6e3c99 | ||
|
|
970890b3f6 | ||
|
|
cb35cb2eb5 | ||
|
|
46585abadd | ||
|
|
66232933d4 | ||
|
|
15df4fa46a | ||
|
|
4cd7fc8f56 | ||
|
|
afa2c42587 | ||
|
|
8d6316e7ce | ||
|
|
4f7e90f598 | ||
|
|
77a487fe4f | ||
|
|
ce1fa43f5c | ||
|
|
fcbee27d5b | ||
|
|
804e52c4d4 | ||
|
|
294cd25f5c | ||
|
|
e6a3f4ad89 | ||
|
|
f1664a95ad | ||
|
|
3f33ae3065 | ||
|
|
e5842d2223 | ||
|
|
64b4bb595f | ||
|
|
ce67853916 | ||
|
|
12c578d77f | ||
|
|
b4ca7d5b4c | ||
|
|
cea7aec05e | ||
|
|
f354ab01d0 | ||
|
|
d44aadb1ac | ||
|
|
e4190ee1c3 | ||
|
|
6916b16111 | ||
|
|
83cdbd2665 | ||
|
|
579fc9a706 | ||
|
|
fd8be5cce4 | ||
|
|
1eee72ae9e | ||
|
|
47a11a3f1d | ||
|
|
5a99e31910 | ||
|
|
6324b7fff4 | ||
|
|
f9cdab9b95 | ||
|
|
56370b0ced | ||
|
|
73c988c678 | ||
|
|
4f09eabbea | ||
|
|
66c4b39401 | ||
|
|
b71ec471b6 | ||
|
|
efe498686e | ||
|
|
622dab60b1 | ||
|
|
6483b867c1 | ||
|
|
50844a1e6f | ||
|
|
258d0abf59 | ||
|
|
bc449e67ea | ||
|
|
86d41bfcc0 | ||
|
|
9886e736a4 | ||
|
|
5e1a895245 | ||
|
|
089d9e3555 | ||
|
|
34610baca6 | ||
|
|
a630d0b455 | ||
|
|
ba4b83ab83 | ||
|
|
f224e445aa | ||
|
|
4471cd3274 | ||
|
|
fe56f25743 | ||
|
|
0a933675a0 | ||
|
|
7e0fba59a3 | ||
|
|
e8b3522124 | ||
|
|
c9456be42a | ||
|
|
a771d03af4 | ||
|
|
14865afcd8 | ||
|
|
80e2110511 | ||
|
|
204e711efc | ||
|
|
2a7c4492d1 | ||
|
|
a5fc3de632 | ||
|
|
bbfc82b0e2 | ||
|
|
8b2ba3e58c | ||
|
|
2d974d5f1d | ||
|
|
f1451eeaaa | ||
|
|
24c05d658b | ||
|
|
ae974b3851 | ||
|
|
70650f2ab8 | ||
|
|
b9e34ceca1 | ||
|
|
9b2206e29a | ||
|
|
8fae0f797a | ||
|
|
3be1b49e86 | ||
|
|
decaf7c425 | ||
|
|
f7a9b12c0e | ||
|
|
eb6ecf573d | ||
|
|
18918151c4 |
No files matched your search
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env bash
|
||||
# nightly-pick.sh WORKFLOW...
|
||||
#
|
||||
# Finds the newest commit on main that has a completed run of each
|
||||
# WORKFLOW (a file name in .github/workflows). It writes these lines to
|
||||
# $GITHUB_OUTPUT:
|
||||
#
|
||||
# sha=<the commit>
|
||||
# run_<workflow>=<the run ID> (for each WORKFLOW, "." and "-" as "_")
|
||||
# skip=true|false (true: the nightly release has this commit)
|
||||
#
|
||||
# A run that is not complete is not used. Thus a run that is in progress
|
||||
# does not stop the nightly release: the release uses the run before it.
|
||||
# Set FORCE=true to publish again when the commit did not change.
|
||||
#
|
||||
# It needs GH_TOKEN and GH_REPO.
|
||||
set -euo pipefail
|
||||
|
||||
[ $# -ge 1 ] || { echo "usage: $0 WORKFLOW..." >&2; exit 2; }
|
||||
out=${GITHUB_OUTPUT:-/dev/stdout}
|
||||
|
||||
# the completed runs of a workflow on main (pushes and manual runs), newest first: "sha id"
|
||||
runs() {
|
||||
gh run list --workflow "$1" --branch main --status completed --limit 50 \
|
||||
--json headSha,databaseId,event \
|
||||
--jq '.[] | select(.event == "push" or .event == "workflow_dispatch") | "\(.headSha) \(.databaseId)"'
|
||||
}
|
||||
|
||||
declare -A list
|
||||
for w in "$@"; do
|
||||
list[$w]=$(runs "$w")
|
||||
done
|
||||
|
||||
sha=
|
||||
while read -r s _; do
|
||||
[ -n "$s" ] || continue
|
||||
ok=1
|
||||
for w in "$@"; do
|
||||
grep -q "^$s " <<<"${list[$w]}" || { ok=0; break; }
|
||||
done
|
||||
if [ "$ok" = 1 ]; then sha=$s; break; fi
|
||||
done <<<"${list[$1]}"
|
||||
|
||||
if [ -z "$sha" ]; then
|
||||
echo "::error::No commit on main has a completed run of each of: $*"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "sha=$sha" >> "$out"
|
||||
for w in "$@"; do
|
||||
id=$(grep -m1 "^$sha " <<<"${list[$w]}" | cut -d' ' -f2)
|
||||
echo "run_$(tr '.-' '__' <<<"$w")=$id" >> "$out"
|
||||
echo "$w: run $id"
|
||||
done
|
||||
|
||||
# (gh writes the error body of a 404 to stdout: no tag is no commit)
|
||||
old=$(gh api "repos/$GH_REPO/git/ref/tags/nightly" --jq .object.sha 2>/dev/null) || old=
|
||||
echo "Commit: $sha (the nightly release has: ${old:-none})"
|
||||
if [ "$sha" = "$old" ] && [ "${FORCE:-false}" != true ]; then
|
||||
echo "skip=true" >> "$out"
|
||||
echo "No new commits on main. The nightly release does not change."
|
||||
else
|
||||
echo "skip=false" >> "$out"
|
||||
fi
|
||||
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env bash
|
||||
# nightly-publish.sh SHA NOTES DIR
|
||||
#
|
||||
# Updates the "nightly" pre-release: its tag to SHA, its text from the
|
||||
# file NOTES, and its files to all the files in DIR. If there is no
|
||||
# release, it makes one.
|
||||
#
|
||||
# The script does not delete the release. If a step fails, the old
|
||||
# release stays (with its old files or some new files), and the next run
|
||||
# tries again.
|
||||
#
|
||||
# It needs GH_TOKEN and GH_REPO.
|
||||
set -euo pipefail
|
||||
|
||||
sha=$1 notes=$2 dir=$3
|
||||
shopt -s nullglob
|
||||
files=("$dir"/*)
|
||||
[ ${#files[@]} -gt 0 ] || { echo "::error::No files to publish in $dir"; exit 1; }
|
||||
title="Nightly build ($(date -u +%Y-%m-%d), ${sha:0:7})"
|
||||
|
||||
# release_id: prints the ID of the "nightly" release, or nothing. (gh
|
||||
# writes the error body of a 404 to stdout: on an error, print nothing.)
|
||||
release_id() {
|
||||
local id
|
||||
id=$(gh api "repos/$GH_REPO/releases/tags/nightly" --jq .id 2>/dev/null) || id=
|
||||
echo "$id"
|
||||
}
|
||||
|
||||
# move_tag: puts the "nightly" tag at $sha, and makes the tag if it is
|
||||
# not there
|
||||
move_tag() {
|
||||
if gh api "repos/$GH_REPO/git/ref/tags/nightly" --jq .object.sha >/dev/null 2>&1; then
|
||||
gh api -X PATCH "repos/$GH_REPO/git/refs/tags/nightly" \
|
||||
-f sha="$sha" -F force=true >/dev/null
|
||||
else
|
||||
gh api -X POST "repos/$GH_REPO/git/refs" \
|
||||
-f ref=refs/tags/nightly -f sha="$sha" >/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
id=$(release_id)
|
||||
if [ -z "$id" ]; then
|
||||
# (no release: a new one, its tag made at $sha)
|
||||
gh release create nightly "${files[@]}" \
|
||||
--prerelease --latest=false --target "$sha" \
|
||||
--title "$title" --notes-file "$notes"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
move_tag
|
||||
gh release edit nightly --prerelease --latest=false \
|
||||
--title "$title" --notes-file "$notes"
|
||||
gh release upload nightly "${files[@]}" --clobber
|
||||
|
||||
# the old files that this build did not make
|
||||
keep=$(for f in "${files[@]}"; do basename "$f"; done)
|
||||
gh api "repos/$GH_REPO/releases/$id/assets" --paginate --jq '.[] | "\(.id) \(.name)"' |
|
||||
while read -r aid name; do
|
||||
if ! grep -qxF "$name" <<<"$keep"; then
|
||||
echo "Removing the old file $name"
|
||||
gh api -X DELETE "repos/$GH_REPO/releases/assets/$aid"
|
||||
fi
|
||||
done
|
||||
@@ -0,0 +1,26 @@
|
||||
# Auto-assign new issues.
|
||||
#
|
||||
# This workflow starts when a person opens an issue.
|
||||
# It adds the project owner (Neo-Desktop) as an assignee.
|
||||
# To change the assignee, change the ASSIGNEE value below.
|
||||
name: Auto-assign issues
|
||||
|
||||
on:
|
||||
issues:
|
||||
types: [opened]
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
jobs:
|
||||
# Add the assignee to the issue that started this run.
|
||||
assign:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Add assignee
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
ASSIGNEE: Neo-Desktop
|
||||
REPO: ${{ github.repository }}
|
||||
ISSUE: ${{ github.event.issue.number }}
|
||||
run: gh issue edit "$ISSUE" --repo "$REPO" --add-assignee "$ASSIGNEE"
|
||||
@@ -1,6 +1,11 @@
|
||||
# Builds and tests the three apps and the library they share
|
||||
# Builds and tests the app (windows/) and the library that it uses
|
||||
# (core/, ssh/): on Windows 95 to Windows 11, and on Linux, macOS and
|
||||
# Haiku over clang-retro MFC's portable Win32 layer (winport/)
|
||||
name: Build
|
||||
|
||||
# The nightly release (nightly.yml) takes its files from this
|
||||
# workflow's artifacts on main. Each artifact is a .tar.gz or a .zip,
|
||||
# because an artifact does not keep the executable bit of a file.
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
@@ -11,8 +16,10 @@ permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
linux:
|
||||
name: Linux (GTK 4)
|
||||
# libdmodemctl (core/) and libdmodemssh (ssh/), with no app: the
|
||||
# tests, the tests with the sanitizers, and SSH against a real sshd
|
||||
core:
|
||||
name: Library and SSH tests (Linux)
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
@@ -20,8 +27,7 @@ jobs:
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
cmake ninja-build pkg-config libgtk-4-dev libadwaita-1-dev \
|
||||
libvte-2.91-gtk4-dev
|
||||
cmake ninja-build pkg-config
|
||||
- name: Build
|
||||
run: |
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo
|
||||
@@ -33,60 +39,344 @@ jobs:
|
||||
cmake -S . -B build-asan -G Ninja -DCMAKE_BUILD_TYPE=Debug \
|
||||
-DCMAKE_C_FLAGS="-fsanitize=address,undefined -fno-sanitize-recover=all"
|
||||
cmake --build build-asan --target dmodemctl_test
|
||||
ctest --test-dir build-asan --output-on-failure
|
||||
ctest --test-dir build-asan --output-on-failure -R dmodemctl
|
||||
- name: SSH against a real sshd
|
||||
run: |
|
||||
sudo apt-get install -y --no-install-recommends openssh-server putty-tools
|
||||
sudo useradd -m dmtest
|
||||
echo 'dmtest:Sesame-42' | sudo chpasswd
|
||||
# a key in a file, one of PuTTY's (.ppk) and one in an agent
|
||||
ssh-keygen -q -t ed25519 -N '' -f "$RUNNER_TEMP/key"
|
||||
: > "$RUNNER_TEMP/empty"
|
||||
puttygen -t rsa -b 2048 -o "$RUNNER_TEMP/key.ppk" --new-passphrase "$RUNNER_TEMP/empty"
|
||||
ssh-keygen -q -t ecdsa -N '' -f "$RUNNER_TEMP/agentkey"
|
||||
eval "$(ssh-agent -a "$RUNNER_TEMP/agent.sock")"
|
||||
ssh-add "$RUNNER_TEMP/agentkey"
|
||||
unset SSH_AUTH_SOCK
|
||||
puttygen "$RUNNER_TEMP/key.ppk" -O public-openssh -o "$RUNNER_TEMP/ppk.pub"
|
||||
cat "$RUNNER_TEMP/key.pub" "$RUNNER_TEMP/agentkey.pub" "$RUNNER_TEMP/ppk.pub" > "$RUNNER_TEMP/authorized_keys"
|
||||
sudo install -d -m 700 -o dmtest ~dmtest/.ssh
|
||||
sudo install -m 600 -o dmtest "$RUNNER_TEMP/authorized_keys" ~dmtest/.ssh/authorized_keys
|
||||
sudo mkdir -p /run/sshd
|
||||
sudo /usr/sbin/sshd -p 2222 -o ListenAddress=127.0.0.1 -o PasswordAuthentication=yes
|
||||
DMODEM_SSH_TEST=127.0.0.1:2222:dmtest:Sesame-42 DMODEM_SSH_TEST_KEY="$RUNNER_TEMP/key" \
|
||||
DMODEM_SSH_TEST_PPK="$RUNNER_TEMP/key.ppk" DMODEM_SSH_TEST_AGENT="$RUNNER_TEMP/agent.sock" \
|
||||
build/ssh/dmodemssh_test
|
||||
|
||||
# Both Windows builds use clang-retro (https://clang-retro.github.io/),
|
||||
# its toolchain package from its setup-toolchain action, on Linux
|
||||
windows-9x:
|
||||
name: Windows 95 and later (Open Watcom)
|
||||
name: Windows 95 and later, the modem built in (clang-retro, win32-picolibc)
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: open-watcom/setup-watcom@v0
|
||||
with:
|
||||
version: "2.0"
|
||||
submodules: true
|
||||
- uses: clang-retro/dos-sdk/.github/actions/setup-toolchain@main
|
||||
id: clang-dos
|
||||
- name: Packages
|
||||
run: |
|
||||
sudo dpkg --add-architecture i386
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends ninja-build wine wine32:i386
|
||||
- name: Build
|
||||
run: sh windows/build-watcom.sh build/watcom
|
||||
run: |
|
||||
cmake -S . -B build/win9x -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_TOOLCHAIN_FILE=cmake/clang-retro.cmake -DCLANG_RETRO_CONFIG=win32-picolibc \
|
||||
-DCMAKE_CROSSCOMPILING_EMULATOR=wine
|
||||
cmake --build build/win9x
|
||||
- name: Test (Wine)
|
||||
run: |
|
||||
export WINEDEBUG=-all WINEPREFIX="$RUNNER_TEMP/wine"
|
||||
wineboot -i >/dev/null 2>&1 || true
|
||||
ctest --test-dir build/win9x --output-on-failure
|
||||
- name: Imports (Windows 95, 98 and ME, and NT 4.0)
|
||||
run: |
|
||||
python3 "$CLANG_DOS/share/clang-dos/check-imports.py" build/win9x/windows/dmodem.exe
|
||||
python3 "$CLANG_DOS/share/clang-dos/check-imports.py" --nt 4.0 build/win9x/windows/dmodem.exe
|
||||
# (no iphlpapi.dll, which Windows 95 does not have, and no msvcrt.dll)
|
||||
if "$CLANG_DOS/bin/llvm-objdump" -p build/win9x/windows/dmodem.exe |
|
||||
grep -i "DLL Name: \(msvcrt\|iphlpapi\)"; then
|
||||
exit 1
|
||||
fi
|
||||
"$CLANG_DOS/bin/llvm-nm" build/win9x/windows/dmodem.exe | grep -q dmodem_embed_start
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dmodem-windows-9x
|
||||
path: build/watcom/dmodem.exe
|
||||
path: build/win9x/windows/dmodem.exe
|
||||
|
||||
windows:
|
||||
name: Windows (MSVC)
|
||||
runs-on: windows-latest
|
||||
# The app with the modem built in (external/D-Modem), on msvcrt:
|
||||
# Windows 98 SE, ME, 2000 and later. (The Windows 95 build above has
|
||||
# the modem too, on picolibc and libc++.)
|
||||
windows-98:
|
||||
name: Windows 98 SE and later, the modem built in (clang-retro, win98)
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- uses: clang-retro/dos-sdk/.github/actions/setup-toolchain@main
|
||||
id: clang-dos
|
||||
- name: Packages
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends ninja-build
|
||||
- name: Build
|
||||
run: |
|
||||
cmake -S . -B build -A Win32
|
||||
cmake --build build --config Release
|
||||
- name: Test
|
||||
run: ctest --test-dir build -C Release --output-on-failure
|
||||
cmake -S . -B build/win98 -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_TOOLCHAIN_FILE=cmake/clang-retro.cmake -DCLANG_RETRO_CONFIG=win98
|
||||
cmake --build build/win98
|
||||
- name: Imports (Windows 98 SE)
|
||||
run: python3 "$CLANG_DOS/share/clang-dos/check-imports.py" build/win98/windows/dmodem.exe
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dmodem-windows-98
|
||||
path: build/win98/windows/dmodem.exe
|
||||
|
||||
windows:
|
||||
name: Windows 7 and later (clang-retro)
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- uses: clang-retro/dos-sdk/.github/actions/setup-toolchain@main
|
||||
id: clang-dos
|
||||
- name: Packages
|
||||
run: |
|
||||
sudo dpkg --add-architecture i386
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends ninja-build wine wine32:i386
|
||||
- name: Build
|
||||
run: |
|
||||
cmake -S . -B build/win7 -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_TOOLCHAIN_FILE=cmake/clang-retro.cmake -DCLANG_RETRO_CONFIG=win7 \
|
||||
-DCMAKE_CROSSCOMPILING_EMULATOR=wine
|
||||
cmake --build build/win7
|
||||
- name: Test (Wine)
|
||||
run: |
|
||||
export WINEDEBUG=-all WINEPREFIX="$RUNNER_TEMP/wine"
|
||||
wineboot -i >/dev/null 2>&1 || true
|
||||
ctest --test-dir build/win7 --output-on-failure
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dmodem-windows
|
||||
path: build/windows/Release/dmodem.exe
|
||||
path: build/win7/windows/dmodem.exe
|
||||
|
||||
macos:
|
||||
name: macOS (SwiftUI)
|
||||
runs-on: macos-15
|
||||
# The Windows app for Linux and macOS (winport/), over the portable
|
||||
# Win32 layer of clang-retro MFC, with the modem built in
|
||||
# (external/D-Modem)
|
||||
winport-linux:
|
||||
name: Windows app on Linux, the modem built in (clang-retro MFC's Win32 layer)
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Library tests
|
||||
with:
|
||||
submodules: true
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
repository: clang-retro/mfc
|
||||
path: mfc
|
||||
submodules: recursive
|
||||
- name: Packages
|
||||
run: |
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo
|
||||
cmake --build build
|
||||
ctest --test-dir build --output-on-failure
|
||||
- name: Project
|
||||
run: |
|
||||
brew install xcodegen
|
||||
cd macos && xcodegen
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
cmake ninja-build clang llvm libx11-dev xvfb xauth \
|
||||
libusb-1.0-0-dev libasound2-dev
|
||||
- name: Build
|
||||
run: |
|
||||
cd macos
|
||||
xcodebuild -project DModem.xcodeproj -scheme DModem \
|
||||
-configuration Release -derivedDataPath ../build/xcode \
|
||||
CODE_SIGN_IDENTITY=- build
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo \
|
||||
-DDMODEM_GUI_WINPORT=ON -DCLANG_RETRO_MFC="$PWD/mfc" \
|
||||
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
|
||||
cmake --build build --target dmodem-win32
|
||||
- name: The built-in modem is in the app
|
||||
run: nm build/winport/dmodem-win32 | grep -q dmodem_embed_start
|
||||
- name: Smoke test (Xvfb)
|
||||
run: xvfb-run -a winport/test/smoke.sh build/winport/dmodem-win32
|
||||
- name: Package
|
||||
run: tar -C build/winport -czf dmodem-win32-linux-x86_64.tar.gz dmodem-win32
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dmodem-macos
|
||||
path: build/xcode/Build/Products/Release/D-Modem.app
|
||||
name: dmodem-win32-linux
|
||||
path: dmodem-win32-linux-x86_64.tar.gz
|
||||
|
||||
# arm64 for a Mac with Apple silicon, and x86_64 for an Intel Mac. The
|
||||
# runner has Apple silicon: it builds the x86_64 app with
|
||||
# CMAKE_OSX_ARCHITECTURES=x86_64 (winport/CMakeLists.txt).
|
||||
winport-macos:
|
||||
name: Windows app on macOS (${{ matrix.arch }}), the modem built in (clang-retro MFC's Win32 layer)
|
||||
runs-on: macos-15
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- arch: arm64
|
||||
artifact: dmodem-win32-macos
|
||||
- arch: x86_64
|
||||
artifact: dmodem-win32-macos-x86_64
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
repository: clang-retro/mfc
|
||||
path: mfc
|
||||
submodules: recursive
|
||||
- name: Packages
|
||||
run: brew install llvm
|
||||
- name: Build
|
||||
run: |
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo \
|
||||
-DDMODEM_GUI_WINPORT=ON -DCLANG_RETRO_MFC="$PWD/mfc" \
|
||||
-DCMAKE_OSX_ARCHITECTURES=${{ matrix.arch }} \
|
||||
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
|
||||
cmake --build build --target dmodem-win32 -j"$(sysctl -n hw.ncpu)"
|
||||
- name: The app is for ${{ matrix.arch }} only
|
||||
run: |
|
||||
lipo -archs build/winport/dmodem-win32
|
||||
test "$(lipo -archs build/winport/dmodem-win32)" = "${{ matrix.arch }}"
|
||||
- name: The built-in modem is in the app
|
||||
run: nm build/winport/dmodem-win32 | grep -q dmodem_embed_start
|
||||
- name: Library tests
|
||||
run: |
|
||||
cmake --build build --target dmodemctl_test xfer_test -j"$(sysctl -n hw.ncpu)"
|
||||
ctest --test-dir build --output-on-failure -R '^(dmodemctl|xfer)$'
|
||||
- name: Package
|
||||
run: tar -C build/winport -czf dmodem-win32-macos-${{ matrix.arch }}.tar.gz dmodem-win32
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.artifact }}
|
||||
path: dmodem-win32-macos-${{ matrix.arch }}.tar.gz
|
||||
|
||||
# The app for Haiku (x86_64), cross-built on Linux with the runner's
|
||||
# clang against a sysroot of Haiku's own packages (D-Modem's
|
||||
# platforms/haiku/haiku_sysroot.py, which checks them against the
|
||||
# repositories' SHA-256). Nothing here runs the app: that needs a
|
||||
# Haiku guest. Refer to docs/haiku.md.
|
||||
winport-haiku:
|
||||
name: Windows app on Haiku, the modem built in (clang-retro MFC's Win32 layer)
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
repository: clang-retro/mfc
|
||||
path: mfc
|
||||
submodules: recursive
|
||||
- name: Packages
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
cmake ninja-build clang lld llvm libzstd1 python3
|
||||
- name: Haiku packages cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: haiku-packages
|
||||
key: haiku-packages-${{ hashFiles('external/D-Modem/platforms/haiku/haiku_sysroot.py') }}
|
||||
- name: Sysroot
|
||||
run: python3 external/D-Modem/platforms/haiku/haiku_sysroot.py haiku-sysroot --cache haiku-packages
|
||||
- name: Build
|
||||
run: |
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_TOOLCHAIN_FILE="$PWD/cmake/haiku.cmake" \
|
||||
-DHAIKU_SYSROOT="$PWD/haiku-sysroot" \
|
||||
-DDMODEM_GUI_WINPORT=ON -DCLANG_RETRO_MFC="$PWD/mfc"
|
||||
cmake --build build --target dmodem-win32
|
||||
- name: The built-in modem is in the app
|
||||
run: llvm-nm build/winport/dmodem-win32 | grep -q dmodem_embed_start
|
||||
- name: Package
|
||||
run: tar -C build/winport -czf dmodem-win32-haiku-x86_64.tar.gz dmodem-win32
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dmodem-win32-haiku
|
||||
path: dmodem-win32-haiku-x86_64.tar.gz
|
||||
|
||||
# The app for Mac OS X 10.4 (Tiger) and later, PowerPC and Intel
|
||||
# (i386), as one fat program, over the Mac OS X 10.0-10.5 port of
|
||||
# clang-retro MFC (cmake/macosx-legacy.cmake). All of it builds on
|
||||
# Linux: clang of clang-retro/llvm (its llvm-prebuilt release, which
|
||||
# has emulated TLS for 10.4), the macosx10.4 sysroot of dos-sdk (libc++
|
||||
# and Apple's MacOSX10.4u.sdk, under Apple's license), and Apple's ld64
|
||||
# and ar for PowerPC (dos-sdk's scripts/build-cctools-ppc.sh, kept in
|
||||
# the cache).
|
||||
winport-macosx-legacy:
|
||||
name: Windows app on Mac OS X 10.4 and later (PowerPC and i386), the modem built in
|
||||
runs-on: ubuntu-24.04
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
repository: clang-retro/mfc
|
||||
path: mfc
|
||||
submodules: recursive
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
repository: clang-retro/dos-sdk
|
||||
path: dos-sdk
|
||||
- name: Packages
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
cmake ninja-build clang automake autoconf libtool uuid-dev libssl-dev zstd
|
||||
- name: clang (clang-retro/llvm, llvm-prebuilt)
|
||||
run: |
|
||||
asset=$(gh api repos/clang-retro/llvm/releases/tags/llvm-prebuilt --jq \
|
||||
'[.assets[] | select(.name | startswith("llvm-linux-"))] | sort_by(.created_at) | last | .name')
|
||||
echo "Using $asset"
|
||||
gh release download llvm-prebuilt -R clang-retro/llvm -p "$asset" -O llvm.tar.zst
|
||||
mkdir -p "$RUNNER_TEMP/llvm"
|
||||
tar --zstd -xf llvm.tar.zst -C "$RUNNER_TEMP/llvm"
|
||||
rm llvm.tar.zst
|
||||
"$RUNNER_TEMP/llvm/bin/clang" --version | head -1
|
||||
echo "MACOSX_LEGACY_LLVM=$RUNNER_TEMP/llvm" >> "$GITHUB_ENV"
|
||||
- name: The macosx10.4 sysroot (libc++ and MacOSX10.4u.sdk)
|
||||
run: |
|
||||
sh dos-sdk/packaging/get-toolchain.sh -s macosx10.4 "$RUNNER_TEMP/sysroots"
|
||||
echo "MACOSX_LEGACY_SYSROOT=$RUNNER_TEMP/sysroots/macosx10.4" >> "$GITHUB_ENV"
|
||||
- uses: actions/cache@v4
|
||||
id: cctools
|
||||
with:
|
||||
path: ${{ runner.temp }}/cctools-ppc
|
||||
key: cctools-ppc-${{ hashFiles('dos-sdk/scripts/build-cctools-ppc.sh', 'dos-sdk/scripts/cctools-ppc-old-objects.patch') }}
|
||||
- name: cctools (ld64 and ar for PowerPC)
|
||||
if: steps.cctools.outputs.cache-hit != 'true'
|
||||
run: |
|
||||
PREFIX="$RUNNER_TEMP/cctools-ppc" SRC="$RUNNER_TEMP/cctools-port" \
|
||||
sh dos-sdk/scripts/build-cctools-ppc.sh > cctools.log 2>&1 || { tail -50 cctools.log; exit 1; }
|
||||
- name: Build (PowerPC and i386)
|
||||
run: |
|
||||
export MACOSX_LEGACY_CCTOOLS="$RUNNER_TEMP/cctools-ppc"
|
||||
for arch in powerpc i386; do
|
||||
cmake -S . -B build/$arch -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_TOOLCHAIN_FILE="$PWD/cmake/macosx-legacy.cmake" \
|
||||
-DMACOSX_LEGACY_ARCH=$arch -DCLANG_RETRO_MFC="$PWD/mfc" \
|
||||
-DDMODEM_GUI_WINPORT=ON
|
||||
cmake --build build/$arch --target dmodem-win32
|
||||
done
|
||||
- name: The fat program, with the built-in modem for each processor
|
||||
run: |
|
||||
cct="$RUNNER_TEMP/cctools-ppc/bin/powerpc-apple-darwin8-"
|
||||
mkdir -p out
|
||||
"${cct}lipo" -create build/powerpc/winport/dmodem-win32 build/i386/winport/dmodem-win32 \
|
||||
-output out/dmodem-win32
|
||||
"${cct}lipo" -info out/dmodem-win32
|
||||
for arch in ppc i386; do
|
||||
"${cct}lipo" out/dmodem-win32 -verify_arch $arch
|
||||
"${cct}nm" -arch $arch out/dmodem-win32 | grep -q dmodem_embed_start
|
||||
done
|
||||
# (each part is for Mac OS X 10.4)
|
||||
"${cct}otool" -arch all -l out/dmodem-win32 | grep -A2 LC_VERSION_MIN_MACOSX | grep -c "version 10.4" | grep -qx 2
|
||||
- name: Package
|
||||
run: tar -C out -czf dmodem-win32-macosx10.4.tar.gz dmodem-win32
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dmodem-win32-macosx-legacy
|
||||
path: dmodem-win32-macosx10.4.tar.gz
|
||||
@@ -0,0 +1,68 @@
|
||||
# The contributor agreement (CONTRIBUTING.md): every commit in a pull
|
||||
# request carries a "Signed-off-by:" line with its author's email, which
|
||||
# says the author agrees to it for that commit. Merge commits are
|
||||
# skipped. Pull requests opened by the repository's owner or the D-Modem
|
||||
# organization's members, and by bots, are not checked. Nor are commits
|
||||
# by Claude (noreply@anthropic.com) on a branch of this repository: only
|
||||
# those who can push here can put them there, and the owner takes them in
|
||||
# by merging (a fork's commits with that email are still checked).
|
||||
#
|
||||
# pull_request_target, so that the check is the base branch's copy and a
|
||||
# pull request can't change it. It reads the commits through the API and
|
||||
# never checks out or runs the pull request's code.
|
||||
name: Contributor agreement
|
||||
|
||||
on:
|
||||
pull_request_target:
|
||||
types: [opened, synchronize, reopened]
|
||||
|
||||
permissions:
|
||||
pull-requests: read
|
||||
|
||||
concurrency:
|
||||
group: contributor-agreement-${{ github.event.pull_request.number }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
sign-off:
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- uses: actions/github-script@v8
|
||||
with:
|
||||
script: |
|
||||
const pr = context.payload.pull_request;
|
||||
const exempt = ['OWNER', 'MEMBER'];
|
||||
if (pr.user.type === 'Bot' || exempt.includes(pr.author_association)) {
|
||||
core.info(`Opened by ${pr.user.login} (${pr.author_association}): not checked.`);
|
||||
return;
|
||||
}
|
||||
const commits = await github.paginate(github.rest.pulls.listCommits, {
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: pr.number,
|
||||
per_page: 100,
|
||||
});
|
||||
const sameRepo = pr.head.repo && pr.head.repo.full_name === pr.base.repo.full_name;
|
||||
const exemptAuthors = ['noreply@anthropic.com'];
|
||||
const missing = [];
|
||||
for (const c of commits) {
|
||||
if (c.parents.length > 1) continue;
|
||||
const email = (c.commit.author.email || '').toLowerCase();
|
||||
if (sameRepo && exemptAuthors.includes(email)) continue;
|
||||
const signed = [...c.commit.message.matchAll(/^Signed-off-by:.*<([^>]+)>\s*$/gim)]
|
||||
.some(m => m[1].trim().toLowerCase() === email);
|
||||
if (!signed) {
|
||||
missing.push(`${c.sha.slice(0, 7)} ${c.commit.message.split('\n')[0]} (author ${email})`);
|
||||
}
|
||||
}
|
||||
if (missing.length) {
|
||||
core.setFailed(
|
||||
'These commits have no "Signed-off-by:" line with their author\'s email, ' +
|
||||
'which says you agree to the contributor agreement in CONTRIBUTING.md:\n ' +
|
||||
missing.join('\n ') +
|
||||
'\n\nTo add it: git rebase --signoff ' + pr.base.ref +
|
||||
' (or git commit --amend -s for the last commit), then force-push.');
|
||||
} else {
|
||||
core.info(`All ${commits.length} commits are signed off.`);
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
# D-Modem bump: moves the external/D-Modem submodule (the built-in
|
||||
# modem) to the latest main of D-Modem. It opens a pull request for the
|
||||
# change, or updates the open one. Build (build.yml) builds and tests
|
||||
# the app in that pull request. The workflow merges the pull request
|
||||
# when all of its checks pass. Build then runs on main, and the next
|
||||
# nightly release (nightly.yml) has the new app.
|
||||
#
|
||||
# It starts:
|
||||
# - from D-Modem, when its main changes (a repository_dispatch event of
|
||||
# type "d-modem-main", from D-Modem's .github/workflows/dependents.yml);
|
||||
# - each day (for an event that did not come);
|
||||
# - manually (Actions > D-Modem bump > Run workflow).
|
||||
#
|
||||
# It needs the DMODEM_BUMP_TOKEN secret (an organization secret): a
|
||||
# fine-grained personal access token with Contents: read and write and
|
||||
# Pull requests: read and write on D-Modem/gui. Do not use the
|
||||
# workflow's own GITHUB_TOKEN: a pull request that this token opens
|
||||
# starts no other workflow, thus Build does not run on it. Without the
|
||||
# secret, the run gives an error and stops.
|
||||
#
|
||||
# The workflow waits for the checks before it merges: with no required
|
||||
# checks on main, GitHub's auto-merge merges immediately. If a check
|
||||
# fails, the pull request stays open for a person.
|
||||
name: D-Modem bump
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "47 6 * * *"
|
||||
repository_dispatch:
|
||||
types: [d-modem-main]
|
||||
workflow_dispatch:
|
||||
|
||||
# (one bump at a time: a second run waits for the first)
|
||||
concurrency:
|
||||
group: d-modem-bump
|
||||
cancel-in-progress: false
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
BRANCH: bot/d-modem-bump
|
||||
|
||||
jobs:
|
||||
bump:
|
||||
runs-on: ubuntu-24.04
|
||||
# (Build takes about 35 minutes; a hung step stops here, not at
|
||||
# GitHub's 6 hours)
|
||||
timeout-minutes: 90
|
||||
steps:
|
||||
- name: Token present
|
||||
env:
|
||||
TOKEN: ${{ secrets.DMODEM_BUMP_TOKEN }}
|
||||
run: |
|
||||
if [ -z "$TOKEN" ]; then
|
||||
echo "::error::No DMODEM_BUMP_TOKEN secret (see this workflow's header)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.DMODEM_BUMP_TOKEN }}
|
||||
fetch-depth: 0
|
||||
|
||||
- name: D-Modem's latest main
|
||||
id: bump
|
||||
run: |
|
||||
git submodule update --init external/D-Modem
|
||||
old=$(git -C external/D-Modem rev-parse HEAD)
|
||||
git -C external/D-Modem fetch --quiet origin main
|
||||
new=$(git -C external/D-Modem rev-parse FETCH_HEAD)
|
||||
echo "old=$old" >> "$GITHUB_OUTPUT"
|
||||
echo "new=$new" >> "$GITHUB_OUTPUT"
|
||||
if [ "$old" = "$new" ]; then
|
||||
echo "Already at D-Modem main ${new:0:7}"
|
||||
echo "changed=false" >> "$GITHUB_OUTPUT"
|
||||
exit 0
|
||||
fi
|
||||
git -C external/D-Modem checkout --quiet "$new"
|
||||
echo "changed=true" >> "$GITHUB_OUTPUT"
|
||||
{
|
||||
echo 'log<<EOF'
|
||||
git -C external/D-Modem log --oneline --no-merges "$old..$new" | head -50
|
||||
echo 'EOF'
|
||||
} >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Commit and push
|
||||
id: commit
|
||||
if: steps.bump.outputs.changed == 'true'
|
||||
env:
|
||||
OLD: ${{ steps.bump.outputs.old }}
|
||||
NEW: ${{ steps.bump.outputs.new }}
|
||||
run: |
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
git checkout -B "$BRANCH"
|
||||
git add external/D-Modem
|
||||
git commit -m "Bump D-Modem to ${NEW:0:7}" \
|
||||
-m "external/D-Modem: ${OLD:0:7} -> ${NEW:0:7} (D-Modem's main)." \
|
||||
-m "Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
|
||||
git push --force origin "$BRANCH"
|
||||
echo "head=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Pull request
|
||||
id: pr
|
||||
if: steps.bump.outputs.changed == 'true'
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.DMODEM_BUMP_TOKEN }}
|
||||
OLD: ${{ steps.bump.outputs.old }}
|
||||
NEW: ${{ steps.bump.outputs.new }}
|
||||
LOG: ${{ steps.bump.outputs.log }}
|
||||
run: |
|
||||
body=$(printf 'Moves `external/D-Modem` to D-Modem main: [`%s..%s`](https://github.com/D-Modem/D-Modem/compare/%s...%s)\n\n```\n%s\n```\n\nOpened by the D-Modem bump workflow (`.github/workflows/d-modem-bump.yml`). It merges when the checks pass.\n' \
|
||||
"${OLD:0:7}" "${NEW:0:7}" "$OLD" "$NEW" "$LOG")
|
||||
title="Bump D-Modem to ${NEW:0:7}"
|
||||
pr=$(gh pr list --head "$BRANCH" --state open --json number --jq '.[0].number')
|
||||
if [ -n "$pr" ]; then
|
||||
gh pr edit "$pr" --title "$title" --body "$body"
|
||||
else
|
||||
gh pr create --base main --head "$BRANCH" --title "$title" --body "$body"
|
||||
pr=$(gh pr list --head "$BRANCH" --state open --json number --jq '.[0].number')
|
||||
fi
|
||||
echo "number=$pr" >> "$GITHUB_OUTPUT"
|
||||
|
||||
# (the checks need some seconds to start; then the step waits for
|
||||
# all of them)
|
||||
- name: Merge when the checks pass
|
||||
if: steps.bump.outputs.changed == 'true'
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.DMODEM_BUMP_TOKEN }}
|
||||
PR: ${{ steps.pr.outputs.number }}
|
||||
HEAD: ${{ steps.commit.outputs.head }}
|
||||
run: |
|
||||
for i in $(seq 1 20); do
|
||||
n=$(gh pr checks "$PR" --json name --jq length 2>/dev/null || true)
|
||||
[ "${n:-0}" -gt 0 ] && break
|
||||
sleep 15
|
||||
done
|
||||
sleep 30
|
||||
if ! gh pr checks "$PR" --watch --interval 30; then
|
||||
echo "::warning::A check failed on pull request #$PR: it waits for a person"
|
||||
exit 1
|
||||
fi
|
||||
gh pr merge "$PR" --merge --match-head-commit "$HEAD"
|
||||
@@ -0,0 +1,121 @@
|
||||
# Nightly: the "nightly" pre-release of the app, from the artifacts of
|
||||
# the newest complete run of build.yml on main. It does not build the
|
||||
# app again. It runs each time a run of build.yml on main completes,
|
||||
# and once a day. Each run updates the release in place and moves its
|
||||
# tag: a run that fails does not delete the release. When main has no
|
||||
# new commits, the run stops and the release does not change. A manual
|
||||
# run (Actions > Nightly > Run workflow) can publish again with "force".
|
||||
#
|
||||
# The files are not signed: Windows SmartScreen and macOS Gatekeeper
|
||||
# show a warning for them. The release has only the files of this
|
||||
# build: nightly-publish.sh removes the files that the build did not
|
||||
# make (for example the old Linux GTK app and the old macOS SwiftUI app).
|
||||
name: Nightly
|
||||
|
||||
on:
|
||||
# (each change to main: when its build completes)
|
||||
workflow_run:
|
||||
workflows: [Build]
|
||||
types: [completed]
|
||||
branches: [main]
|
||||
schedule:
|
||||
- cron: "23 5 * * *"
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
force:
|
||||
description: Publish again, also when main has no new commits
|
||||
type: boolean
|
||||
default: false
|
||||
|
||||
# (one at a time: a second run waits for the first)
|
||||
concurrency:
|
||||
group: nightly
|
||||
cancel-in-progress: false
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
actions: read
|
||||
|
||||
jobs:
|
||||
nightly:
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 20
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
GH_REPO: ${{ github.repository }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: The newest build of main
|
||||
id: pick
|
||||
run: .github/scripts/nightly-pick.sh build.yml
|
||||
env:
|
||||
FORCE: ${{ inputs.force || 'false' }}
|
||||
|
||||
- uses: actions/download-artifact@v4
|
||||
if: steps.pick.outputs.skip == 'false'
|
||||
with:
|
||||
run-id: ${{ steps.pick.outputs.run_build_yml }}
|
||||
github-token: ${{ github.token }}
|
||||
path: dl
|
||||
|
||||
- name: Files
|
||||
if: steps.pick.outputs.skip == 'false'
|
||||
env:
|
||||
SHA: ${{ steps.pick.outputs.sha }}
|
||||
RUN: ${{ steps.pick.outputs.run_build_yml }}
|
||||
run: |
|
||||
mkdir out
|
||||
take() { # take ARTIFACT FILE [NAME]: copy it if the build made it
|
||||
if [ -f "dl/$1/$2" ]; then cp "dl/$1/$2" "out/${3:-$2}"; else echo "- \`${3:-$2}\`" >> missing; fi
|
||||
}
|
||||
take dmodem-windows-9x dmodem.exe dmodem-win9x.exe
|
||||
take dmodem-windows-98 dmodem.exe dmodem-win98.exe
|
||||
take dmodem-windows dmodem.exe dmodem-win7.exe
|
||||
take dmodem-win32-linux dmodem-win32-linux-x86_64.tar.gz
|
||||
take dmodem-win32-macos dmodem-win32-macos-arm64.tar.gz
|
||||
take dmodem-win32-macos-x86_64 dmodem-win32-macos-x86_64.tar.gz
|
||||
take dmodem-win32-macosx-legacy dmodem-win32-macosx10.4.tar.gz
|
||||
take dmodem-win32-haiku dmodem-win32-haiku-x86_64.tar.gz
|
||||
(cd out && sha256sum * > SHA256SUMS)
|
||||
ls -l out
|
||||
{
|
||||
echo "The newest build of \`main\`, from commit $SHA ([build run]($GITHUB_SERVER_URL/$GH_REPO/actions/runs/$RUN))."
|
||||
echo "This is not a stable release. The files are not signed."
|
||||
echo
|
||||
echo "| File | What it is |"
|
||||
echo "| --- | --- |"
|
||||
echo "| \`dmodem-win9x.exe\` | The Windows app with the modem built in, for Windows 95, 98, ME, NT 4.0 and later (no C runtime DLL; Windows 95 needs Winsock 2) |"
|
||||
echo "| \`dmodem-win98.exe\` | The Windows app with the modem built in, for Windows 98 SE, ME, 2000 and later (on msvcrt.dll) |"
|
||||
echo "| \`dmodem-win7.exe\` | The Windows app for Windows 7 and later, with the modem built in |"
|
||||
echo "| \`dmodem-win32-linux-x86_64.tar.gz\` | The app on Linux (X11) with the modem built in, x86_64 |"
|
||||
echo "| \`dmodem-win32-macos-arm64.tar.gz\` | The app on macOS with the modem built in, arm64 (Apple silicon) |"
|
||||
echo "| \`dmodem-win32-macos-x86_64.tar.gz\` | The app on macOS with the modem built in, x86_64 (Intel Mac) |"
|
||||
echo "| \`dmodem-win32-macosx10.4.tar.gz\` | The app on Mac OS X 10.4 (Tiger) and later with the modem built in, one program for PowerPC and Intel (i386). Not tested on a Mac yet |"
|
||||
echo "| \`dmodem-win32-haiku-x86_64.tar.gz\` | The app on Haiku R1/beta6 and later with the modem built in, x86_64. Needs \`pkgman install libusb\` |"
|
||||
echo "| \`SHA256SUMS\` | The SHA-256 of each file |"
|
||||
if [ -f missing ]; then
|
||||
echo
|
||||
echo "This build did not make these files:"
|
||||
cat missing
|
||||
fi
|
||||
} > notes.md
|
||||
|
||||
# The step uses the DMODEM_BUMP_TOKEN organization secret
|
||||
# (Contents: read and write on D-Modem/gui, refer to
|
||||
# d-modem-bump.yml) if it is available. If that token fails (for
|
||||
# example "HTTP 403: Resource not accessible by personal access
|
||||
# token"), the next step tries again with the GITHUB_TOKEN of this
|
||||
# workflow. The script updates the release in place, so a failed
|
||||
# try does not delete it.
|
||||
- name: Publish
|
||||
id: publish
|
||||
if: steps.pick.outputs.skip == 'false' && env.HAS_BUMP_TOKEN == 'true'
|
||||
continue-on-error: true
|
||||
env:
|
||||
HAS_BUMP_TOKEN: ${{ secrets.DMODEM_BUMP_TOKEN != '' }}
|
||||
GH_TOKEN: ${{ secrets.DMODEM_BUMP_TOKEN }}
|
||||
run: .github/scripts/nightly-publish.sh "${{ steps.pick.outputs.sha }}" notes.md out
|
||||
- name: Publish (GITHUB_TOKEN)
|
||||
if: steps.pick.outputs.skip == 'false' && steps.publish.outcome != 'success'
|
||||
run: .github/scripts/nightly-publish.sh "${{ steps.pick.outputs.sha }}" notes.md out
|
||||
@@ -0,0 +1,18 @@
|
||||
# The gui site at https://d-modem.github.io/gui/, built with brand's shared
|
||||
# theme and workflow (brand's docs/sites.md).
|
||||
name: Pages
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
permissions:
|
||||
contents: read
|
||||
pages: write
|
||||
id-token: write
|
||||
concurrency:
|
||||
group: pages-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
jobs:
|
||||
site:
|
||||
uses: D-Modem/brand/.github/workflows/site.yml@main
|
||||
@@ -4,8 +4,7 @@ build-*/
|
||||
bin/
|
||||
obj/
|
||||
*.user
|
||||
.build/
|
||||
.swiftpm/
|
||||
*.xcodeproj/xcuserdata/
|
||||
DerivedData/
|
||||
.flatpak-builder/
|
||||
|
||||
# the site (brand's tools/site.sh): the theme it copies in, and the built site
|
||||
.site/
|
||||
site/
|
||||
@@ -0,0 +1,4 @@
|
||||
[submodule "external/D-Modem"]
|
||||
path = external/D-Modem
|
||||
url = https://github.com/D-Modem/D-Modem.git
|
||||
branch = main
|
||||
@@ -1,32 +1,92 @@
|
||||
# D-Modem's desktop apps: the shared C core (libdmodemctl), and the
|
||||
# Linux app (GTK 4 and libadwaita) when its libraries are there, or the
|
||||
# Windows app with MinGW or MSVC. The Windows 95 build of the Windows
|
||||
# app (windows/build-watcom.sh) and the macOS app (macos/) build with
|
||||
# their own tools and take the core from here.
|
||||
# D-Modem's desktop app: the shared C core (libdmodemctl), and the
|
||||
# Win32 app (windows/). On Windows, it builds with clang-retro
|
||||
# (cmake/clang-retro.cmake), MinGW or MSVC. With DMODEM_GUI_WINPORT, the
|
||||
# same app builds for Linux and macOS, over clang-retro MFC's portable
|
||||
# Win32 layer (winport/). On Linux and macOS with no DMODEM_GUI_WINPORT,
|
||||
# only the core, libdmodemssh and their tests build.
|
||||
# With DMODEM_GUI_EMBED, the app (Windows 95 and later, and the app on
|
||||
# Linux and macOS) has the modem in it (libdmodem_embed from the D-Modem
|
||||
# submodule, external/D-Modem): it needs no dmodemd.
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(dmodem-gui VERSION 1.0.0 LANGUAGES C)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
option(DMODEM_GUI_LINUX "Build the GTK 4 app (linux/)" ON)
|
||||
option(DMODEM_GUI_SSH "Build libdmodemssh (libssh) for the Remote page" ON)
|
||||
option(DMODEM_GUI_WINPORT "Build the Windows app for Linux or macOS (winport/)" OFF)
|
||||
set(CLANG_RETRO_MFC "" CACHE PATH "A clang-retro MFC checkout, for DMODEM_GUI_WINPORT")
|
||||
|
||||
# the modem built in (docs/embedded-modem.md): the app on Windows, and
|
||||
# the app on Linux and macOS (winport/), when the submodule is there
|
||||
# (git submodule update --init)
|
||||
set(DMODEM_GUI_DMODEM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/D-Modem
|
||||
CACHE PATH "The D-Modem tree, for DMODEM_GUI_EMBED")
|
||||
# (all the Windows builds: clang-retro's win32-picolibc, the Windows 95
|
||||
# build, has libc++ for the modem's C++17, as win98 and win7 do)
|
||||
set(_embed_default OFF)
|
||||
if(EXISTS ${DMODEM_GUI_DMODEM_DIR}/CMakeLists.txt)
|
||||
if(WIN32 OR DMODEM_GUI_WINPORT)
|
||||
set(_embed_default ON)
|
||||
endif()
|
||||
endif()
|
||||
option(DMODEM_GUI_EMBED "Build the modem into the app (libdmodem_embed)"
|
||||
${_embed_default})
|
||||
if(DMODEM_GUI_EMBED AND (WIN32 OR DMODEM_GUI_WINPORT))
|
||||
if(NOT EXISTS ${DMODEM_GUI_DMODEM_DIR}/CMakeLists.txt)
|
||||
message(FATAL_ERROR "DMODEM_GUI_EMBED: no D-Modem in "
|
||||
"${DMODEM_GUI_DMODEM_DIR} (git submodule update --init)")
|
||||
endif()
|
||||
enable_language(CXX)
|
||||
# (D-Modem's library and its SIP alone: no programs, modules or tests)
|
||||
set(DMODEM_DMODEMD ON CACHE BOOL "" FORCE)
|
||||
set(DMODEM_EMBED ON CACHE BOOL "" FORCE)
|
||||
set(DMODEM_TESTS OFF CACHE BOOL "" FORCE)
|
||||
set(DMODEM_ASTERISK OFF CACHE BOOL "" FORCE)
|
||||
set(DMODEM_FREESWITCH OFF CACHE BOOL "" FORCE)
|
||||
set(DMODEM_ARI OFF CACHE BOOL "" FORCE)
|
||||
# (clang-retro: D-Modem's own toolchain file has its sources take
|
||||
# dmodem_retro.h, the clock_gettime() that the sysroots lack; here
|
||||
# its C++ sources alone, as there)
|
||||
get_directory_property(_opts COMPILE_OPTIONS)
|
||||
if(CLANG_DOS)
|
||||
# (the oldest Windows of the target, as D-Modem's toolchain file
|
||||
# has it: the .cfg's _WIN32_WINNT, else its triple's
|
||||
# windows<major>.<minor>, 9x and NT 4.0 all 0x0400)
|
||||
if(NOT DMODEM_WINNT)
|
||||
file(STRINGS ${CLANG_DOS}/bin/${CLANG_RETRO_CONFIG}.cfg _cfg_lines)
|
||||
foreach(_l ${_cfg_lines})
|
||||
if(_l MATCHES "^-D_WIN32_WINNT=(.*)$")
|
||||
set(DMODEM_WINNT ${CMAKE_MATCH_1})
|
||||
elseif(NOT DMODEM_WINNT AND _l MATCHES "windows([0-9]+)\\.([0-9]+)")
|
||||
if(CMAKE_MATCH_1 EQUAL 4)
|
||||
set(DMODEM_WINNT 0x0400)
|
||||
else()
|
||||
math(EXPR DMODEM_WINNT "${CMAKE_MATCH_1} * 256 + ${CMAKE_MATCH_2}"
|
||||
OUTPUT_FORMAT HEXADECIMAL)
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
set(DMODEM_WINNT ${DMODEM_WINNT} CACHE STRING
|
||||
"_WIN32_WINNT: the oldest Windows for the modem built in")
|
||||
endif()
|
||||
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-include>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:${DMODEM_GUI_DMODEM_DIR}/platforms/windows/clang-retro/dmodem_retro.h>")
|
||||
set(ENV{CLANG_DOS} ${CLANG_DOS})
|
||||
endif()
|
||||
add_subdirectory(${DMODEM_GUI_DMODEM_DIR} d-modem EXCLUDE_FROM_ALL)
|
||||
set_directory_properties(PROPERTIES COMPILE_OPTIONS "${_opts}")
|
||||
endif()
|
||||
|
||||
add_subdirectory(core)
|
||||
enable_testing()
|
||||
add_subdirectory(core/test)
|
||||
if(DMODEM_GUI_SSH)
|
||||
add_subdirectory(ssh)
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
add_subdirectory(windows)
|
||||
endif()
|
||||
|
||||
if(DMODEM_GUI_LINUX AND UNIX AND NOT APPLE)
|
||||
find_package(PkgConfig)
|
||||
if(PkgConfig_FOUND)
|
||||
pkg_check_modules(GTK IMPORTED_TARGET gtk4>=4.10 libadwaita-1>=1.5 vte-2.91-gtk4)
|
||||
endif()
|
||||
if(GTK_FOUND)
|
||||
add_subdirectory(linux)
|
||||
else()
|
||||
message(STATUS "gtk4, libadwaita-1 or vte-2.91-gtk4 not found: the Linux app is not built")
|
||||
endif()
|
||||
elseif(DMODEM_GUI_WINPORT)
|
||||
add_subdirectory(winport)
|
||||
endif()
|
||||
@@ -0,0 +1,44 @@
|
||||
# Commercial license
|
||||
|
||||
The D-Modem GUI is Copyright (C) 2026 D-Modem Authors and is offered
|
||||
under two licenses; you choose one.
|
||||
|
||||
## The GNU Affero General Public License, version 3 (the default)
|
||||
|
||||
[LICENSE](LICENSE), AGPL-3.0-only. Free of charge, for any purpose,
|
||||
commercial ones included, on the AGPL's terms. In short:
|
||||
|
||||
- If you distribute the D-Modem GUI, modified or not, you give the recipients
|
||||
its complete source code under the AGPL.
|
||||
- If you modify the D-Modem GUI and let people use it remotely (anyone using it
|
||||
over a network), you offer those users
|
||||
the complete source code of your modified version under the AGPL
|
||||
(section 13).
|
||||
|
||||
## A commercial license
|
||||
|
||||
For those who cannot or will not meet the AGPL's terms, for example to:
|
||||
|
||||
- ship the D-Modem GUI, or a modified version, inside a product whose source
|
||||
you do not publish;
|
||||
- run a modified version of the D-Modem GUI as a service without offering its source to
|
||||
the people who use it;
|
||||
- combine it with code under licenses that do not mix with the AGPL.
|
||||
|
||||
A commercial license is granted by the D-Modem Authors, the copyright holders,
|
||||
on terms agreed with you (scope, term and fee).
|
||||
|
||||
Neo-Desktop (<https://github.com/Neo-Desktop>) is the point of contact
|
||||
for commercial licenses.
|
||||
|
||||
**To ask for one**, contact Neo-Desktop, or open an issue at
|
||||
<https://github.com/D-Modem/gui/issues> titled "Commercial
|
||||
license", saying who you are and roughly what you want to do. Keep
|
||||
anything confidential out of the issue: a private channel follows from
|
||||
there.
|
||||
|
||||
Until a commercial license is signed, the AGPL is the only license you
|
||||
have.
|
||||
|
||||
This file explains the licensing; it is not itself a license or a
|
||||
contract.
|
||||
@@ -0,0 +1,60 @@
|
||||
# Contributing
|
||||
|
||||
We accept contributions: issues, fixes and features. Before you send a
|
||||
change, build it on the platforms that it affects ([README.md](README.md)).
|
||||
|
||||
Write all technical documentation in ASD-STE100 Simplified Technical English. Follow docs/STYLE.md.
|
||||
|
||||
## The contributor agreement
|
||||
|
||||
The GUI is offered under the AGPL-3.0 and, by the D-Modem Authors, under
|
||||
commercial licenses ([COMMERCIAL-LICENSE.md](COMMERCIAL-LICENSE.md)).
|
||||
For contributions to be part of both, each one comes with this agreement.
|
||||
By submitting a contribution (a pull request, a patch or
|
||||
any other code or text meant for inclusion), you agree that:
|
||||
|
||||
1. The contribution is your own original work, or you otherwise have
|
||||
the right to submit it under these terms, and to your knowledge it
|
||||
infringes no one's rights. If your employer has rights in what you
|
||||
create, you have its permission to contribute, or it has waived those
|
||||
rights.
|
||||
2. You assign to D-Modem all copyright in your contribution, worldwide,
|
||||
for the whole term of those rights. "D-Modem" here is the D-Modem
|
||||
organization (<https://github.com/D-Modem>), whose rights its owner,
|
||||
Neo-Desktop, holds on its behalf; the "D-Modem Authors" in the
|
||||
project's copyright notices are D-Modem in this sense. Where a right
|
||||
can't be assigned (moral rights, for example), you waive it, or where
|
||||
it can't be waived, you agree not to assert it against D-Modem or
|
||||
those who receive the project from it. If the assignment is not
|
||||
effective anywhere, you instead grant D-Modem there an exclusive,
|
||||
perpetual, worldwide, no-charge, royalty-free, irrevocable license to
|
||||
do anything the copyright holder could do with your contribution,
|
||||
including licensing it to others.
|
||||
3. D-Modem may license your contribution, and works based on it, under
|
||||
the AGPL-3.0 and under any other terms, commercial licenses included.
|
||||
It licenses your contribution back to you: a perpetual, worldwide,
|
||||
non-exclusive, no-charge, royalty-free, irrevocable license to use,
|
||||
copy, modify, publicly display and perform, sublicense and distribute
|
||||
it, on any terms.
|
||||
4. You grant D-Modem, and those who receive the project from it, a
|
||||
perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable license under any patent claims of yours that your
|
||||
contribution would infringe, to make, use, sell and otherwise
|
||||
transfer it as part of the project.
|
||||
5. Your contribution is provided "as is", without warranties or
|
||||
conditions of any kind.
|
||||
|
||||
Please add this line to each commit you contribute (`git commit -s`
|
||||
does it):
|
||||
|
||||
Signed-off-by: Your Name <your@email>
|
||||
|
||||
It says that you agree to the above for that commit. A contribution
|
||||
without it cannot be merged.
|
||||
|
||||
A check on each pull request looks for that line, with the email of the
|
||||
commit author, on each commit. If the line is missing, use
|
||||
`git rebase --signoff main` to add it. For the last commit only, you can
|
||||
use `git commit --amend -s`. Then force-push. The check does not examine
|
||||
pull requests from the D-Modem Authors (the owner of the repository and
|
||||
the members of the D-Modem organization).
|
||||
@@ -1,28 +1,661 @@
|
||||
BSD 3-Clause License
|
||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright (c) 2026, D-Modem
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
Preamble
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU Affero General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
@@ -1,48 +1,78 @@
|
||||
<img src=".github/banner.png" alt="D-Modem/gui: D-Modem on the desktop" width="100%">
|
||||
|
||||
# D-Modem desktop apps
|
||||
# D-Modem desktop app
|
||||
|
||||
Native apps for D-Modem on Windows, macOS and Linux.
|
||||
The D-Modem desktop app is one Win32 app for Windows, macOS, Linux and
|
||||
Haiku. The
|
||||
site of the app is <https://d-modem.github.io/gui/>.
|
||||
|
||||
Each app starts dmodemd (or reaches one that already runs), and shows the
|
||||
modem as a desktop program should: dial and answer from a window, change
|
||||
its settings without AT commands, and see how the last call went. All three
|
||||
are thin native shells over one small C library, libdmodemctl, so they
|
||||
behave the same.
|
||||
The app has the modem built in, so it needs no dmodemd (refer to
|
||||
[The built-in modem](docs/embedded-modem.md)). The app shows the modem as a
|
||||
desktop program. You can dial and answer from a window, change the settings
|
||||
without AT commands, and see the results of the last call. The app is a
|
||||
thin Win32 shell over one small C library, libdmodemctl.
|
||||
|
||||
- **Modem**: the call's state, dial, answer and hang up, and the line it uses
|
||||
- **Terminal**: the AT console, and the modem's own setup screens (AT!CF, AT!CT)
|
||||
- **Settings**: every modem setting as a native control, from AT!M
|
||||
- **Call Stats**: the last call's rate, modulation, error control and line figures (AT#UD, ATI11)
|
||||
- **Line settings**: a SIP account, a USB modem, or a modem that already runs
|
||||
- **Windows 95 and later**: plain Win32, built with Open Watcom; on Windows 9x it reaches a modem over TCP or a COM port
|
||||
- **Modem**: the state of the call, dial, answer and hang up, and the line that the modem uses
|
||||
- **Phone**: voice calls through the sound card (dial, answer, keypad), and an
|
||||
answering machine that takes messages on this computer when you enable it (disabled by default)
|
||||
- **Terminal**: the AT console, and the setup screens of the modem (AT!CF, AT!CT)
|
||||
- **Remote**: log in to a server by SSH, Telnet or rlogin, in a separate terminal. SSH is built in, on libssh, and it uses the keys and the agent of OpenSSH and PuTTY
|
||||
- **Terminal types**: the terminal emulator of the D-Modem app, ported to C: xterm, DEC VT52 to VT525, ANSI-BBS, PETSCII, ATASCII, Viewdata, Minitel and Tektronix 4014, with sixel, ReGIS and Tektronix graphics
|
||||
- **HyperTerminal items**: file transfers (ZMODEM, YMODEM, XMODEM and Kermit), copy and capture, and the ASCII Setup, Terminal Settings and Call menu of HyperTerminal. Refer to [Features](docs/features.md)
|
||||
- **Settings**: each modem setting as a native control, from AT!M
|
||||
- **Call Stats**: the rate, modulation, error control and line figures of the last call (AT#UD, ATI11)
|
||||
- **Line settings**: a SIP account, a USB modem, or a modem that already runs (dmodemd, or a hardware modem such as the MultiTech MT9234MU). **Look for a modem** finds the modem and its port
|
||||
|
||||
| | Windows | macOS | Linux |
|
||||
| --- | --- | --- | --- |
|
||||
| Toolkit | Win32, C++ | SwiftUI | GTK 4, libadwaita, VTE |
|
||||
| Runs on | Windows 95 and later | macOS 13 and later | GTK 4.10, libadwaita 1.5 and VTE 0.76 or later (Ubuntu 24.04, Fedora 40) |
|
||||
| | Windows | macOS | Linux | Haiku |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| Toolkit | Win32, C++ | Win32, over the Cocoa layer of [clang-retro MFC](https://github.com/clang-retro/mfc) | Win32, over the X11 and Wayland layer of clang-retro MFC | Win32, over the Interface Kit layer of clang-retro MFC |
|
||||
| Built with | [clang-retro](https://clang-retro.github.io/), MinGW or MSVC | clang | clang | clang (cross-built on Linux) |
|
||||
| Runs on | Windows 95 and later | arm64 (Apple silicon) and x86_64 (Intel Mac). Also Mac OS X 10.4 and later on PowerPC and i386 | x86_64 | R1/beta6 and later, x86_64 |
|
||||
|
||||
<img src="docs/images/linux-settings.png" width="49%" alt="The Linux app's Settings page"> <img src="docs/images/windows-setup-screen.png" width="49%" alt="The Windows app's Terminal with the modem's setup screen">
|
||||
The source code of the app is the same on each system. Refer to "The
|
||||
Windows app on Linux and macOS" in [Design](docs/design.md).
|
||||
|
||||
<img src="docs/images/windows-settings.png" width="49%" alt="The Settings page of the app"> <img src="docs/images/windows-setup-screen.png" width="49%" alt="The Terminal of the app with the setup screen of the modem">
|
||||
|
||||
<img src="docs/images/windows-modem.png" width="49%" alt="The Modem page of the app"> <img src="docs/images/windows-phone.png" width="49%" alt="The Phone page of the app as it takes a message">
|
||||
|
||||
## Nightly builds
|
||||
|
||||
The [nightly release](https://github.com/D-Modem/gui/releases/tag/nightly)
|
||||
has the app from the newest build of `main`: for Windows 95 and later, for
|
||||
Windows 7 and later, for Linux, for macOS, and for Haiku. A workflow
|
||||
(`.github/workflows/nightly.yml`) replaces the release one time each day.
|
||||
When `main` has no new commits, the release does not change. When the
|
||||
main branch of D-Modem changes, a workflow moves the built-in modem to it
|
||||
and builds the app again (refer to [Building](docs/building.md)).
|
||||
|
||||
These builds are not stable releases. The files are not signed. Thus
|
||||
Windows and macOS can show a warning before the app starts.
|
||||
|
||||
## Quick start
|
||||
|
||||
Linux:
|
||||
Linux (get [clang-retro MFC](https://github.com/clang-retro/mfc) first):
|
||||
|
||||
sudo apt install cmake libgtk-4-dev libadwaita-1-dev libvte-2.91-gtk4-dev
|
||||
cmake -S . -B build && cmake --build build && ./build/linux/dmodem-gui
|
||||
git submodule update --init
|
||||
sudo apt install cmake ninja-build clang llvm libx11-dev \
|
||||
libusb-1.0-0-dev libasound2-dev
|
||||
cmake -S . -B build -DDMODEM_GUI_WINPORT=ON -DCLANG_RETRO_MFC=/path/to/mfc \
|
||||
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
|
||||
cmake --build build --target dmodem-win32 && ./build/winport/dmodem-win32
|
||||
|
||||
Windows: `cmake -S . -B build -A Win32 && cmake --build build --config Release`,
|
||||
or for Windows 95 and later, `windows/build-watcom.sh` with Open Watcom 2.0.
|
||||
|
||||
macOS: `brew install xcodegen && cd macos && xcodegen && open DModem.xcodeproj`.
|
||||
|
||||
The Settings page needs a dmodemd with AT!M (D-Modem's main branch since
|
||||
D-Modem/D-Modem#103); the other pages work with any D-Modem 1.0.
|
||||
For Windows (with clang-retro) and macOS, refer to
|
||||
[Building](docs/building.md).
|
||||
|
||||
## Documentation
|
||||
|
||||
- [Design](docs/design.md): how the apps are built, what they share, and the Windows 9x notes
|
||||
- [libdmodemctl](core/include/dmodemctl.h): the library's API
|
||||
- [The site](https://d-modem.github.io/gui/): the home page, the downloads and all the pages below
|
||||
- [Features](docs/features.md): each page and menu of the app
|
||||
- [Building](docs/building.md): Windows with clang-retro, and the app on Linux and macOS
|
||||
- [The app on Haiku](docs/haiku.md): install, build, serial ports and limits
|
||||
- [The built-in modem](docs/embedded-modem.md): the modem inside the app, with no dmodemd
|
||||
- [Design](docs/design.md): how the app is built, hardware modems (the MultiTech MT9234MU), the Windows 9x notes, and the app on Linux and macOS
|
||||
- [libdmodemctl](core/include/dmodemctl.h): the API of the library
|
||||
- [Documentation style](docs/STYLE.md): the rules for ASD-STE100 Simplified Technical English
|
||||
|
||||
## The D-Modem suite
|
||||
|
||||
@@ -50,13 +80,12 @@ D-Modem/D-Modem#103); the other pages work with any D-Modem 1.0.
|
||||
| --- | --- |
|
||||
| [D-Modem](https://github.com/D-Modem/D-Modem) | The open V.92 softmodem: dmodemd, libdmodem and the Asterisk, FreeSWITCH and ARI modules |
|
||||
| [app](https://github.com/D-Modem/app) | The D-Modem app for Android and iOS: a dial-up modem on a SIP call |
|
||||
| [gui](https://github.com/D-Modem/gui) | D-Modem on the desktop: native apps for Windows, macOS and Linux |
|
||||
| [gui](https://github.com/D-Modem/gui) | D-Modem on the desktop: one Win32 app for Windows, macOS and Linux |
|
||||
| [hardware](https://github.com/D-Modem/hardware) | pico-serial: RP2040 serial adapters and phone-line USB sticks |
|
||||
| [menu](https://github.com/D-Modem/menu) | modemmenu: the login and menu dial-up callers reach |
|
||||
| [login](https://github.com/D-Modem/login) | dmodem-login: the login, menu and LDAP provider for dial-up callers |
|
||||
| [bbs](https://github.com/D-Modem/bbs) | A bulletin board for telnet, SSH, TN3270 and modem callers |
|
||||
| [go3270](https://github.com/D-Modem/go3270) | A Go library for 3270 terminal servers, used by bbs |
|
||||
| [brand](https://github.com/D-Modem/brand) | Logos, colours, type and the naming guide |
|
||||
|
||||
## Licence
|
||||
|
||||
BSD 3-Clause ([LICENSE](LICENSE)).
|
||||
AGPL-3.0-only ([LICENSE](LICENSE)), or a commercial licence from the D-Modem Authors ([COMMERCIAL-LICENSE.md](COMMERCIAL-LICENSE.md)). Contributions come with a contributor agreement ([CONTRIBUTING.md](CONTRIBUTING.md)).
|
||||
@@ -1,5 +1,15 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="14 14 80 80">
|
||||
<!-- D-Modem's app icon (D-Modem/brand, tools/app_icon.py) on a desktop tile -->
|
||||
<rect x="16" y="16" width="76" height="76" rx="17" fill="#0a0f1c"/>
|
||||
<defs><linearGradient id="stripes" x1="0" y1="0" x2="0" y2="1"><stop offset="0.0000" stop-color="#ff4d5e"/><stop offset="0.1667" stop-color="#ff4d5e"/><stop offset="0.1667" stop-color="#ff8a3d"/><stop offset="0.3333" stop-color="#ff8a3d"/><stop offset="0.3333" stop-color="#ffd23d"/><stop offset="0.5000" stop-color="#ffd23d"/><stop offset="0.5000" stop-color="#3ddc84"/><stop offset="0.6667" stop-color="#3ddc84"/><stop offset="0.6667" stop-color="#3d8bff"/><stop offset="0.8333" stop-color="#3d8bff"/><stop offset="0.8333" stop-color="#a66bff"/><stop offset="1.0000" stop-color="#a66bff"/></linearGradient></defs><path d="M34,34 H74 V66 H62.0 V75.6 H46.0 V66 H34 Z" fill="none" stroke="url(#stripes)" stroke-width="3.4"/><path d="M39.0,50.0 L39.1,49.6 L39.2,49.2 L39.4,48.8 L39.5,48.4 L39.6,48.1 L39.8,47.7 L39.9,47.3 L40.0,47.0 L40.1,46.7 L40.2,46.3 L40.4,46.0 L40.5,45.8 L40.6,45.5 L40.8,45.2 L40.9,45.0 L41.0,44.8 L41.1,44.6 L41.2,44.5 L41.4,44.3 L41.5,44.2 L41.6,44.1 L41.8,44.1 L41.9,44.0 L42.0,44.0 L42.1,44.0 L42.2,44.1 L42.4,44.1 L42.5,44.2 L42.6,44.3 L42.8,44.5 L42.9,44.6 L43.0,44.8 L43.1,45.0 L43.2,45.2 L43.4,45.5 L43.5,45.8 L43.6,46.0 L43.8,46.3 L43.9,46.7 L44.0,47.0 L44.1,47.3 L44.2,47.7 L44.4,48.1 L44.5,48.4 L44.6,48.8 L44.8,49.2 L44.9,49.6 L45.0,50.0 L45.1,50.4 L45.2,50.8 L45.4,51.2 L45.5,51.6 L45.6,51.9 L45.8,52.3 L45.9,52.7 L46.0,53.0 L46.1,53.3 L46.2,53.7 L46.4,54.0 L46.5,54.2 L46.6,54.5 L46.8,54.8 L46.9,55.0 L47.0,55.2 L47.1,55.4 L47.2,55.5 L47.4,55.7 L47.5,55.8 L47.6,55.9 L47.8,55.9 L47.9,56.0 L48.0,56.0 L48.1,56.0 L48.2,55.9 L48.4,55.9 L48.5,55.8 L48.6,55.7 L48.8,55.5 L48.9,55.4 L49.0,55.2 L49.1,55.0 L49.2,54.8 L49.4,54.5 L49.5,54.2 L49.6,54.0 L49.8,53.7 L49.9,53.3 L50.0,53.0 L50.1,52.7 L50.2,52.3 L50.4,51.9 L50.5,51.6 L50.6,51.2 L50.8,50.8 L50.9,50.4 L51.0,50.0 L51.1,49.6 L51.2,49.2 L51.4,48.8 L51.5,48.4 L51.6,48.1 L51.8,47.7 L51.9,47.3 L52.0,47.0 L52.1,46.7 L52.2,46.3 L52.4,46.0 L52.5,45.8 L52.6,45.5 L52.8,45.2 L52.9,45.0 L53.0,44.8 L53.1,44.6 L53.2,44.5 L53.4,44.3 L53.5,44.2 L53.6,44.1 L53.8,44.1 L53.9,44.0 L54.0,44.0 H57.0 V56.0 H63.0 V44.0 H69.0" fill="none" stroke="#e8f0ff" stroke-width="3.4" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 500 500">
|
||||
<!-- D-Modem's mark (D-Modem/brand, avatar/org-avatar.svg) on a desktop tile -->
|
||||
<defs>
|
||||
<linearGradient id="books" gradientUnits="userSpaceOnUse" x1="0" y1="81" x2="0" y2="419"><stop offset="0.0000" stop-color="#ff4d5e"/><stop offset="0.1667" stop-color="#ff4d5e"/><stop offset="0.1667" stop-color="#ff8a3d"/><stop offset="0.3333" stop-color="#ff8a3d"/><stop offset="0.3333" stop-color="#ffd23d"/><stop offset="0.5000" stop-color="#ffd23d"/><stop offset="0.5000" stop-color="#3ddc84"/><stop offset="0.6667" stop-color="#3ddc84"/><stop offset="0.6667" stop-color="#3d8bff"/><stop offset="0.8333" stop-color="#3d8bff"/><stop offset="0.8333" stop-color="#a66bff"/><stop offset="1.0000" stop-color="#a66bff"/></linearGradient>
|
||||
<filter id="glow" x="-30%" y="-30%" width="160%" height="160%">
|
||||
<feGaussianBlur stdDeviation="6" result="b"/>
|
||||
<feMerge><feMergeNode in="b"/><feMergeNode in="SourceGraphic"/></feMerge>
|
||||
</filter>
|
||||
</defs>
|
||||
<rect x="10" y="10" width="480" height="480" rx="108" fill="#0a0f1c"/>
|
||||
<g transform="translate(-15 0)">
|
||||
<path d="M140 100 H240 A150 150 0 0 1 240 400 H140 Z" fill="none" stroke="url(#books)" stroke-width="38" stroke-linejoin="round" filter="url(#glow)"/>
|
||||
<path d="M178.0,250.0 L178.8,246.3 L179.5,242.7 L180.3,239.1 L181.1,235.5 L181.8,232.0 L182.6,228.6 L183.4,225.2 L184.1,222.0 L184.9,218.9 L185.7,215.9 L186.4,213.1 L187.2,210.4 L188.0,207.9 L188.7,205.6 L189.5,203.4 L190.3,201.5 L191.0,199.8 L191.8,198.3 L192.6,197.0 L193.3,195.9 L194.1,195.1 L194.9,194.5 L195.6,194.1 L196.4,194.0 L197.2,194.1 L197.9,194.5 L198.7,195.1 L199.5,195.9 L200.2,197.0 L201.0,198.3 L201.8,199.8 L202.5,201.5 L203.3,203.4 L204.1,205.6 L204.8,207.9 L205.6,210.4 L206.4,213.1 L207.1,215.9 L207.9,218.9 L208.7,222.0 L209.4,225.2 L210.2,228.6 L211.0,232.0 L211.7,235.5 L212.5,239.1 L213.3,242.7 L214.0,246.3 L214.8,250.0 L215.6,253.7 L216.3,257.3 L217.1,260.9 L217.9,264.5 L218.6,268.0 L219.4,271.4 L220.2,274.8 L220.9,278.0 L221.7,281.1 L222.5,284.1 L223.2,286.9 L224.0,289.6 L224.8,292.1 L225.5,294.4 L226.3,296.6 L227.1,298.5 L227.8,300.2 L228.6,301.7 L229.4,303.0 L230.1,304.1 L230.9,304.9 L231.7,305.5 L232.4,305.9 L233.2,306.0 L234.0,305.9 L234.7,305.5 L235.5,304.9 L236.3,304.1 L237.0,303.0 L237.8,301.7 L238.6,300.2 L239.3,298.5 L240.1,296.6 L240.9,294.4 L241.6,292.1 L242.4,289.6 L243.2,286.9 L243.9,284.1 L244.7,281.1 L245.5,278.0 L246.2,274.8 L247.0,271.4 L247.8,268.0 L248.5,264.5 L249.3,260.9 L250.1,257.3 L250.8,253.7 L251.6,250.0 L252.4,246.3 L253.1,242.7 L253.9,239.1 L254.7,235.5 L255.4,232.0 L256.2,228.6 L257.0,225.2 L257.7,222.0 L258.5,218.9 L259.3,215.9 L260.0,213.1 L260.8,210.4 L261.6,207.9 L262.3,205.6 L263.1,203.4 L263.9,201.5 L264.6,199.8 L265.4,198.3 L266.2,197.0 L266.9,195.9 L267.7,195.1 L268.5,194.5 L269.2,194.1 L270.0,194.0 H288.4 V306.0 H325.2 V194.0 H362.0" fill="none" stroke="#e8f0ff" stroke-width="16" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 2.9 KiB |
@@ -0,0 +1,46 @@
|
||||
# CMake toolchain file for clang-retro (https://clang-retro.github.io/):
|
||||
# the Windows app for Windows 95 and later, built on Linux or Windows.
|
||||
#
|
||||
# cmake -S . -B build/retro -DCLANG_DOS=/opt/clang-dos \
|
||||
# -DCMAKE_TOOLCHAIN_FILE=cmake/clang-retro.cmake
|
||||
#
|
||||
# CLANG_DOS is the unpacked clang-dos toolchain package (the environment
|
||||
# variable CLANG_DOS also sets it). CLANG_RETRO_CONFIG is the --config
|
||||
# file of the target (default win32-picolibc: Windows 95, 98, ME, NT 4.0
|
||||
# and later, with no C runtime DLL).
|
||||
set(CMAKE_SYSTEM_NAME Windows)
|
||||
set(CMAKE_SYSTEM_VERSION 4.0)
|
||||
set(CMAKE_SYSTEM_PROCESSOR i386)
|
||||
|
||||
if(NOT CLANG_DOS AND DEFINED ENV{CLANG_DOS})
|
||||
set(CLANG_DOS "$ENV{CLANG_DOS}")
|
||||
endif()
|
||||
if(NOT CLANG_DOS)
|
||||
message(FATAL_ERROR "Set CLANG_DOS to the clang-dos toolchain package")
|
||||
endif()
|
||||
set(CLANG_DOS "${CLANG_DOS}" CACHE PATH "The clang-dos toolchain package")
|
||||
set(CLANG_RETRO_CONFIG win32-picolibc CACHE STRING "The clang-retro --config file (without .cfg)")
|
||||
# (try_compile projects see these two)
|
||||
list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES CLANG_DOS CLANG_RETRO_CONFIG)
|
||||
|
||||
if(CMAKE_HOST_WIN32)
|
||||
set(_exe .exe)
|
||||
else()
|
||||
set(_exe "")
|
||||
endif()
|
||||
# (the --config file is a mandatory argument of the compilers, not a flag,
|
||||
# so that a -DCMAKE_C_FLAGS of a project, or of an ExternalProject, keeps it)
|
||||
set(_cfg "--config=${CLANG_DOS}/bin/${CLANG_RETRO_CONFIG}.cfg")
|
||||
set(CMAKE_C_COMPILER "${CLANG_DOS}/bin/clang${_exe}" "${_cfg}")
|
||||
set(CMAKE_CXX_COMPILER "${CLANG_DOS}/bin/clang++${_exe}" "${_cfg}")
|
||||
set(CMAKE_RC_COMPILER "${CLANG_DOS}/bin/llvm-windres${_exe}")
|
||||
set(CMAKE_AR "${CLANG_DOS}/bin/llvm-ar${_exe}")
|
||||
set(CMAKE_RANLIB "${CLANG_DOS}/bin/llvm-ranlib${_exe}")
|
||||
# windres runs the preprocessor of clang for the target
|
||||
set(CMAKE_RC_FLAGS_INIT "--target=pe-i386 --preprocessor-arg=${_cfg}")
|
||||
|
||||
list(APPEND CMAKE_FIND_ROOT_PATH "${CLANG_DOS}")
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
|
||||
@@ -0,0 +1,20 @@
|
||||
# haiku.cmake - the toolchain file for the app on Haiku (x86_64), built
|
||||
# on Linux with Clang and lld. It is the toolchain file of D-Modem
|
||||
# (external/D-Modem/platforms/haiku/haiku.cmake), which the modem in the
|
||||
# app uses too.
|
||||
#
|
||||
# python3 external/D-Modem/platforms/haiku/haiku_sysroot.py haiku-sysroot
|
||||
# cmake -S . -B build/haiku -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
# -DCMAKE_TOOLCHAIN_FILE=cmake/haiku.cmake -DHAIKU_SYSROOT=$PWD/haiku-sysroot \
|
||||
# -DDMODEM_GUI_WINPORT=ON -DCLANG_RETRO_MFC=/path/to/mfc
|
||||
#
|
||||
# Refer to docs/haiku.md.
|
||||
if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/../external/D-Modem/platforms/haiku/haiku.cmake")
|
||||
message(FATAL_ERROR "cmake/haiku.cmake: no external/D-Modem (git submodule update --init)")
|
||||
endif()
|
||||
# (the roots that a project gives, as libssh's build gets the prefix of
|
||||
# mbedTLS, stay with the sysroot, which D-Modem's file sets)
|
||||
set(_dmodem_find_roots ${CMAKE_FIND_ROOT_PATH})
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/../external/D-Modem/platforms/haiku/haiku.cmake")
|
||||
list(APPEND CMAKE_FIND_ROOT_PATH ${_dmodem_find_roots})
|
||||
list(REMOVE_DUPLICATES CMAKE_FIND_ROOT_PATH)
|
||||
@@ -0,0 +1,99 @@
|
||||
# CMake toolchain file for Mac OS X 10.4 (Tiger) and later, on 32-bit
|
||||
# PowerPC or 32-bit Intel. It builds the Windows app (winport/) over the
|
||||
# Mac OS X 10.0-10.5 port of clang-retro MFC, on Linux.
|
||||
#
|
||||
# cmake -S . -B build/ppc -DCMAKE_TOOLCHAIN_FILE=cmake/macosx-legacy.cmake \
|
||||
# -DMACOSX_LEGACY_ARCH=powerpc -DMACOSX_LEGACY_LLVM=/opt/llvm \
|
||||
# -DMACOSX_LEGACY_SYSROOT=/opt/clang-dos/macosx10.4 \
|
||||
# -DMACOSX_LEGACY_CCTOOLS=/opt/cctools-ppc \
|
||||
# -DCLANG_RETRO_MFC=/path/to/mfc -DDMODEM_GUI_WINPORT=ON
|
||||
#
|
||||
# You need these:
|
||||
# MACOSX_LEGACY_ARCH powerpc or i386
|
||||
# MACOSX_LEGACY_LLVM clang of clang-retro/llvm (the llvm-prebuilt
|
||||
# release), with emulated TLS for Mac OS X 10.4
|
||||
# MACOSX_LEGACY_SYSROOT the macosx10.4 sysroot of clang-retro/dos-sdk
|
||||
# (packaging/get-toolchain.sh -s macosx10.4): libc++,
|
||||
# and MacOSX10.4u.sdk in it
|
||||
# MACOSX_LEGACY_CCTOOLS Apple's ld64 and ar for PowerPC
|
||||
# (dos-sdk's scripts/build-cctools-ppc.sh)
|
||||
# The environment variables of the same names also set them.
|
||||
set(CMAKE_SYSTEM_NAME Darwin)
|
||||
set(CMAKE_SYSTEM_VERSION 8.0)
|
||||
|
||||
foreach(_v ARCH LLVM SYSROOT CCTOOLS)
|
||||
if(NOT MACOSX_LEGACY_${_v} AND DEFINED ENV{MACOSX_LEGACY_${_v}})
|
||||
set(MACOSX_LEGACY_${_v} "$ENV{MACOSX_LEGACY_${_v}}")
|
||||
endif()
|
||||
if(NOT MACOSX_LEGACY_${_v})
|
||||
message(FATAL_ERROR "Set MACOSX_LEGACY_${_v} (cmake/macosx-legacy.cmake)")
|
||||
endif()
|
||||
set(MACOSX_LEGACY_${_v} "${MACOSX_LEGACY_${_v}}" CACHE STRING "" FORCE)
|
||||
# (the builds that D-Modem configures with this file see them too)
|
||||
set(ENV{MACOSX_LEGACY_${_v}} "${MACOSX_LEGACY_${_v}}")
|
||||
endforeach()
|
||||
# (try_compile projects see these)
|
||||
list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
|
||||
MACOSX_LEGACY_ARCH MACOSX_LEGACY_LLVM MACOSX_LEGACY_SYSROOT MACOSX_LEGACY_CCTOOLS)
|
||||
|
||||
set(CMAKE_SYSTEM_PROCESSOR ${MACOSX_LEGACY_ARCH})
|
||||
set(MACOSX_LEGACY_TARGET "${MACOSX_LEGACY_ARCH}-apple-macosx10.4")
|
||||
set(MACOSX_LEGACY_SDK "${MACOSX_LEGACY_SYSROOT}/MacOSX10.4u.sdk")
|
||||
set(_cct "${MACOSX_LEGACY_CCTOOLS}/bin/powerpc-apple-darwin8-")
|
||||
|
||||
set(CMAKE_OSX_SYSROOT "${MACOSX_LEGACY_SDK}")
|
||||
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.4)
|
||||
set(CMAKE_OSX_ARCHITECTURES "")
|
||||
# ld64 cannot read the symbol table that llvm-ar writes for PowerPC
|
||||
set(CMAKE_AR "${_cct}ar")
|
||||
set(CMAKE_RANLIB "${_cct}ranlib")
|
||||
set(CMAKE_LIPO "${_cct}lipo")
|
||||
set(CMAKE_INSTALL_NAME_TOOL "${_cct}install_name_tool")
|
||||
|
||||
# The C library functions that Mac OS X 10.4 does not have, such as
|
||||
# clock_gettime: port/compat/macosx of clang-retro MFC (CLANG_RETRO_MFC),
|
||||
# and cmake/macosx-legacy (the fixes of headers of the 10.4 SDK).
|
||||
# libwinport.a has their code. _DARWIN_C_SOURCE: gmtime_r and the other
|
||||
# POSIX functions also with -std=c99. And libc++ of the sysroot, with
|
||||
# threads.
|
||||
if(NOT CLANG_RETRO_MFC AND DEFINED ENV{CLANG_RETRO_MFC})
|
||||
set(CLANG_RETRO_MFC "$ENV{CLANG_RETRO_MFC}")
|
||||
endif()
|
||||
if(NOT CLANG_RETRO_MFC)
|
||||
message(FATAL_ERROR "Set CLANG_RETRO_MFC (cmake/macosx-legacy.cmake)")
|
||||
endif()
|
||||
set(ENV{CLANG_RETRO_MFC} "${CLANG_RETRO_MFC}")
|
||||
list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES CLANG_RETRO_MFC)
|
||||
# These are mandatory arguments of the compilers, with the target, not
|
||||
# flags: a -DCMAKE_C_FLAGS of a project or an ExternalProject keeps them.
|
||||
set(_compat -isystem ${CMAKE_CURRENT_LIST_DIR}/macosx-legacy
|
||||
-isystem ${CLANG_RETRO_MFC}/port/compat/macosx
|
||||
-DWINPORT_MACOSX_COMPAT -D_DARWIN_C_SOURCE)
|
||||
set(_cxx -nostdinc++ -isystem ${MACOSX_LEGACY_SYSROOT}/include/c++/v1
|
||||
-D__STDC_FORMAT_MACROS)
|
||||
set(CMAKE_C_COMPILER "${MACOSX_LEGACY_LLVM}/bin/clang"
|
||||
"--target=${MACOSX_LEGACY_TARGET}" ${_compat})
|
||||
set(CMAKE_CXX_COMPILER "${MACOSX_LEGACY_LLVM}/bin/clang++"
|
||||
"--target=${MACOSX_LEGACY_TARGET}" ${_cxx} ${_compat})
|
||||
set(CMAKE_OBJCXX_COMPILER ${CMAKE_CXX_COMPILER})
|
||||
# The builtins of the sysroot have emulated TLS and the atomic library
|
||||
# functions (__atomic_fetch_add_8 and others), which libc++ and libre need
|
||||
if(MACOSX_LEGACY_ARCH STREQUAL "powerpc")
|
||||
set(_rt "${MACOSX_LEGACY_SYSROOT}/lib/darwin/libclang_rt.ppc_osx.a")
|
||||
else()
|
||||
set(_rt "${MACOSX_LEGACY_SYSROOT}/lib/darwin/libclang_rt.osx.a")
|
||||
endif()
|
||||
set(_ld "-fuse-ld=${_cct}ld -nostdlib++ -L${MACOSX_LEGACY_SYSROOT}/lib -lc++ -lc++abi -lunwind ${_rt}")
|
||||
# (a program exports no symbols of libc++: on i386, ld64 stops at a
|
||||
# pointer difference to a global weak symbol, such as std::to_string's
|
||||
# to __itoa::__pow10_32 in the libc++ of the sysroot, when another object
|
||||
# also has that symbol)
|
||||
set(CMAKE_EXE_LINKER_FLAGS_INIT "${_ld} -Wl,-unexported_symbol,__ZNSt3__1*")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS_INIT "${_ld}")
|
||||
set(CMAKE_MODULE_LINKER_FLAGS_INIT "${_ld}")
|
||||
|
||||
list(APPEND CMAKE_FIND_ROOT_PATH "${MACOSX_LEGACY_SDK}" "${MACOSX_LEGACY_SYSROOT}")
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* net/if.h for Mac OS X 10.4 (cmake/macosx-legacy.cmake): sys/socket.h,
|
||||
* then the net/if.h of the SDK.
|
||||
*
|
||||
* The net/if.h of the 10.4 SDK uses struct sockaddr and does not include
|
||||
* sys/socket.h. POSIX lets a program include net/if.h first, and libssh
|
||||
* does.
|
||||
*/
|
||||
#ifndef DMODEM_MACOSX_LEGACY_NET_IF_H
|
||||
#define DMODEM_MACOSX_LEGACY_NET_IF_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include_next <net/if.h>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* signal.h for Mac OS X 10.4 (cmake/macosx-legacy.cmake): the signal.h
|
||||
* of the SDK, with SIG_DFL, SIG_IGN, SIG_HOLD and SIG_ERR as pointers to
|
||||
* a function with a prototype.
|
||||
*
|
||||
* Without _POSIX_C_SOURCE, the 10.4 SDK gives them as pointers to a
|
||||
* function with no prototype. libssh compiles with
|
||||
* -Werror=strict-prototypes, which stops at these.
|
||||
*/
|
||||
#ifndef DMODEM_MACOSX_LEGACY_SIGNAL_H
|
||||
#define DMODEM_MACOSX_LEGACY_SIGNAL_H
|
||||
|
||||
#include_next <signal.h>
|
||||
|
||||
#undef SIG_DFL
|
||||
#undef SIG_IGN
|
||||
#undef SIG_HOLD
|
||||
#undef SIG_ERR
|
||||
#define SIG_DFL ((void (*)(int))0)
|
||||
#define SIG_IGN ((void (*)(int))1)
|
||||
#define SIG_HOLD ((void (*)(int))5)
|
||||
#define SIG_ERR ((void (*)(int))-1)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* time.h for Mac OS X 10.4 (cmake/macosx-legacy.cmake): the time.h of
|
||||
* clang-retro MFC (port/compat/macosx) and of the SDK, and the
|
||||
* reentrant functions of POSIX.
|
||||
*
|
||||
* The time.h of the 10.4 SDK does not declare gmtime_r, localtime_r,
|
||||
* asctime_r and ctime_r when _POSIX_C_SOURCE is set. These functions are
|
||||
* POSIX functions, and libSystem of 10.4 has them. Mbed TLS sets
|
||||
* _POSIX_C_SOURCE and calls gmtime_r.
|
||||
*/
|
||||
#ifndef DMODEM_MACOSX_LEGACY_TIME_H
|
||||
#define DMODEM_MACOSX_LEGACY_TIME_H
|
||||
|
||||
#include_next <time.h>
|
||||
|
||||
#if defined(_POSIX_C_SOURCE) && !defined(_ANSI_SOURCE)
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/* Converts a time to UTC in the given structure. */
|
||||
struct tm *gmtime_r(const time_t *, struct tm *);
|
||||
/* Converts a time to local time in the given structure. */
|
||||
struct tm *localtime_r(const time_t *, struct tm *);
|
||||
/* Writes a time structure as text into the given buffer. */
|
||||
char *asctime_r(const struct tm *, char *);
|
||||
/* Writes a time as text into the given buffer. */
|
||||
char *ctime_r(const time_t *, char *);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,15 +1,35 @@
|
||||
# libdmodemctl: what the desktop apps share, in plain C with no
|
||||
# dependencies. Static for the Linux app; a DLL for the Windows app's
|
||||
# P/Invoke (-DDMODEMCTL_SHARED=ON).
|
||||
# libdmodemctl: the part of the desktop app that does not draw, in plain
|
||||
# C with no dependencies. Static by default; a DLL for a P/Invoke caller
|
||||
# (-DDMODEMCTL_SHARED=ON).
|
||||
option(DMODEMCTL_SHARED "Build libdmodemctl as a shared library" OFF)
|
||||
|
||||
set(DMODEMCTL_SOURCES
|
||||
src/session.c
|
||||
src/model.c
|
||||
src/diag.c
|
||||
src/path.c
|
||||
src/vt.c
|
||||
src/vt_charsets.c
|
||||
src/vt_data.c
|
||||
src/vt_dec.c
|
||||
src/vt_fonts.c
|
||||
src/vt_gfx.c
|
||||
src/vt_keys.c
|
||||
src/vt_machine.c
|
||||
src/vt_profiles.c
|
||||
src/vt_regis.c
|
||||
src/vt_rip.c
|
||||
src/vt_vt52.c
|
||||
src/daemon.c
|
||||
src/util.c)
|
||||
src/voice.c
|
||||
src/net.c
|
||||
src/util.c
|
||||
src/xfer.c
|
||||
src/xfer_hex.c
|
||||
src/xfer_kermit.c
|
||||
src/xfer_xy.c
|
||||
src/xfer_z.c
|
||||
src/zstd_codec.c)
|
||||
|
||||
if(DMODEMCTL_SHARED)
|
||||
add_library(dmodemctl SHARED ${DMODEMCTL_SOURCES})
|
||||
@@ -24,3 +44,7 @@ if(MSVC)
|
||||
else()
|
||||
target_compile_options(dmodemctl PRIVATE -Wall -Wextra -Wshadow)
|
||||
endif()
|
||||
if(UNIX)
|
||||
find_package(Threads REQUIRED)
|
||||
target_link_libraries(dmodemctl PRIVATE m Threads::Threads)
|
||||
endif()
|
||||
@@ -1,15 +1,15 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* dmodemctl.h - the part of D-Modem's desktop apps that is not drawing.
|
||||
* dmodemctl.h - the part of D-Modem's desktop app that is not drawing.
|
||||
*
|
||||
* The Windows, macOS and Linux apps are thin native shells over this
|
||||
* small C library, so that each of them is only views. It does no I/O
|
||||
* of its own and starts no threads: each app opens the modem's DTE (a
|
||||
* pty, a TCP socket) in its own way, hands the bytes it reads to
|
||||
* dmc_feed(), writes what dmc_output() gives it, and calls dmc_poll()
|
||||
* from a timer. That keeps it simple to call from C (GTK), C#
|
||||
* The Win32 app (on Windows, and on Linux and macOS over clang-retro
|
||||
* MFC's Win32 layer) is a thin shell over this small C library, so that
|
||||
* the app is only views. The library does no I/O of its own and
|
||||
* starts no threads: the app opens the modem's DTE (a TCP socket, a COM
|
||||
* port), hands the bytes it reads to dmc_feed(), writes what
|
||||
* dmc_output() gives it, and calls dmc_poll() from a timer. That keeps it simple to call from C, C++ (Win32), C#
|
||||
* (P/Invoke) and Swift.
|
||||
*
|
||||
* Its parts:
|
||||
@@ -21,10 +21,17 @@
|
||||
* page.
|
||||
* - dmc_diag: the last call's statistics, from AT#UD (Microsoft's
|
||||
* Unimodem diagnostics), as labelled rows.
|
||||
* - dmc_vt: a small ANSI terminal screen, for the apps whose toolkit
|
||||
* has no terminal widget (Windows and macOS): the AT console and the
|
||||
* full-screen configurator (AT!CF).
|
||||
* - dmc_vt: a terminal emulator, the app's ported to C: the AT
|
||||
* console and the full-screen configurator (AT!CF), and the BBSes and
|
||||
* hosts reached, as any of the terminals of the DEC VT line, xterm, a
|
||||
* PC's ANSI-BBS, the Commodores', the Ataris', Viewdata, the Minitel
|
||||
* and the Tektronix 4014.
|
||||
* - dmc_daemon_*: starting dmodemd and reading what it prints.
|
||||
* - dmc_net: a remote login's protocol (Telnet, rlogin) on a TCP
|
||||
* connection the app makes, for the Remote page.
|
||||
* - dmc_voice_*: voice calls on the session (V.253's voice mode: the
|
||||
* call's audio both ways, 8 kHz), the answering machine that takes
|
||||
* a message, and WAV files for greetings and messages.
|
||||
*
|
||||
* Strings are UTF-8. A const char * the library returns stays valid
|
||||
* until the next call on the same object.
|
||||
@@ -49,7 +56,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/* the library's version: major * 10000 + minor * 100 + patch */
|
||||
#define DMC_VERSION 10000
|
||||
#define DMC_VERSION 10300
|
||||
DMC_API int dmc_version(void);
|
||||
|
||||
/*
|
||||
@@ -71,7 +78,9 @@ enum {
|
||||
DMC_ONLINE, /* CONNECT: data mode */
|
||||
DMC_ONLINE_COMMAND, /* escaped (+++) during a call */
|
||||
DMC_HANGING_UP, /* going back on hook */
|
||||
DMC_CONFIGURATOR /* AT!C, AT!CF or AT!CT runs */
|
||||
DMC_CONFIGURATOR, /* AT!C, AT!CF or AT!CT runs */
|
||||
DMC_VOICE, /* a voice call: its audio both ways */
|
||||
DMC_VOICEMAIL /* the answering machine has the call */
|
||||
};
|
||||
|
||||
/* what dmc_next_event() reports */
|
||||
@@ -86,7 +95,15 @@ enum {
|
||||
/* a result code nobody waited for (RING, NO CARRIER...): result */
|
||||
DMC_EV_RESULT,
|
||||
/* caller ID after a RING: text "NMBR=5551234" and the like */
|
||||
DMC_EV_CALLER
|
||||
DMC_EV_CALLER,
|
||||
/* in a voice call, what the modem heard on the line: text its
|
||||
<DLE> code ("5" a DTMF digit, "l" the far end hung up, "b" a busy
|
||||
tone), result its name ("digit", "hangup", "busy") */
|
||||
DMC_EV_VOICE,
|
||||
/* the answering machine is done: ok 1 with a message
|
||||
(dmc_voice_message()), 0 if the caller left none; text the
|
||||
caller's number, if caller ID gave it */
|
||||
DMC_EV_MESSAGE
|
||||
};
|
||||
|
||||
typedef struct dmc_event {
|
||||
@@ -119,6 +136,9 @@ DMC_API void dmc_feed(dmc_session *s, const char *data, int n);
|
||||
session reads the command lines in them, to follow ATD, ATA, ATH and
|
||||
AT!C typed by hand) */
|
||||
DMC_API void dmc_type(dmc_session *s, const char *data, int n);
|
||||
/* bytes to the far end as they are, during a call (a file transfer's):
|
||||
not taken as typed (no escape, no command) */
|
||||
DMC_API void dmc_send(dmc_session *s, const char *data, int n);
|
||||
/* the time now: runs the timers (a command's timeout, the escape's
|
||||
guard times); returns the ms until it next needs to be called, -1 if
|
||||
only when something happens */
|
||||
@@ -126,6 +146,8 @@ DMC_API int dmc_poll(dmc_session *s, int64_t now_ms);
|
||||
|
||||
/* bytes to write to the modem: up to max into buf, how many */
|
||||
DMC_API int dmc_output(dmc_session *s, char *buf, int max);
|
||||
/* how many bytes dmc_output has for the DTE now */
|
||||
DMC_API int dmc_output_waiting(const dmc_session *s);
|
||||
/* bytes for the terminal view: up to max into buf, how many (what the
|
||||
modem sent, less the DMC_QUIET commands') */
|
||||
DMC_API int dmc_terminal(dmc_session *s, char *buf, int max);
|
||||
@@ -155,6 +177,86 @@ DMC_API int dmc_connect_rate(const dmc_session *s);
|
||||
DMC_API const char *dmc_caller_number(const dmc_session *s);
|
||||
DMC_API const char *dmc_caller_name(const dmc_session *s);
|
||||
|
||||
/*
|
||||
* Voice calls
|
||||
*
|
||||
* A voice call is the modem in voice mode (AT+FCLASS=8) with its audio
|
||||
* both ways (AT+VTR), G.711 mu-law on the DTE: the library sends ours
|
||||
* at the line's pace from dmc_poll() (call it every 20 ms or so in a
|
||||
* call: dmc_poll() says so) and gives the far end's to dmc_voice_read(),
|
||||
* both as 16-bit samples at 8000 a second. dmc_hangup() ends a call,
|
||||
* and the far end's hanging up does too (DMC_EV_VOICE "l"); either way
|
||||
* the modem goes back to data mode.
|
||||
*/
|
||||
|
||||
/* the sample rate of voice calls, messages and greetings */
|
||||
#define DMC_VOICE_RATE 8000
|
||||
|
||||
/* dial a voice call (OK when the far end answers, BUSY, NO ANSWER);
|
||||
answer the ringing call as a voice call: a tag as dmc_command()'s,
|
||||
-1 if the modem is not idle (or ringing, to answer) */
|
||||
DMC_API int dmc_voice_dial(dmc_session *s, const char *number);
|
||||
DMC_API int dmc_voice_answer(dmc_session *s);
|
||||
|
||||
/* the far end's samples: up to max into pcm, how many (a second at
|
||||
most is kept: the oldest are dropped if they are not read) */
|
||||
DMC_API int dmc_voice_read(dmc_session *s, int16_t *pcm, int max);
|
||||
/* our samples (the microphone's), sent at the line's pace; silence
|
||||
when there are none, and the oldest dropped when more than a third
|
||||
of a second waits */
|
||||
DMC_API void dmc_voice_write(dmc_session *s, const int16_t *pcm, int n);
|
||||
/* a DTMF digit (0-9, *, #, A-D) played into the call, after any
|
||||
others still playing; 0, or -1 for no call or no digit */
|
||||
DMC_API int dmc_voice_dtmf(dmc_session *s, char digit);
|
||||
|
||||
/*
|
||||
* The answering machine
|
||||
*
|
||||
* It answers the ringing call in voice mode, plays the greeting, a
|
||||
* beep, and records the caller's message: until they hang up, are
|
||||
* silent for silence_s seconds, or max_s seconds have gone. Then it
|
||||
* hangs up, with DMC_EV_MESSAGE. While it runs (DMC_VOICEMAIL) the
|
||||
* caller can be heard with dmc_voice_read() (screening), and
|
||||
* dmc_voice_pickup() makes it a voice call.
|
||||
*/
|
||||
|
||||
/* the greeting (8 kHz samples, copied; none: the beep alone) and the
|
||||
message's limits (0: 120 s and 7 s) */
|
||||
DMC_API void dmc_voicemail_set(dmc_session *s, const int16_t *greeting,
|
||||
int n, int max_s, int silence_s);
|
||||
/* answer the ringing call with the answering machine: a tag as
|
||||
dmc_voice_answer()'s */
|
||||
DMC_API int dmc_voicemail_answer(dmc_session *s);
|
||||
/* the answering machine answers by itself after rings RINGs (0 never,
|
||||
the default) */
|
||||
DMC_API void dmc_voicemail_auto(dmc_session *s, int rings);
|
||||
/* the call the answering machine has, taken as a voice call (its
|
||||
message dropped): 0, or -1 if it has none */
|
||||
DMC_API int dmc_voice_pickup(dmc_session *s);
|
||||
/* the last message's samples into *pcm (valid until the next message):
|
||||
how many */
|
||||
DMC_API int dmc_voice_message(const dmc_session *s, const int16_t **pcm);
|
||||
/* the RINGs of the call ringing now */
|
||||
DMC_API int dmc_ring_count(const dmc_session *s);
|
||||
|
||||
/*
|
||||
* Sound files (greetings and messages)
|
||||
*/
|
||||
|
||||
/* the 44-byte header of a WAV file of n 16-bit samples, 8 kHz, mono,
|
||||
into buf (44 bytes): 44 */
|
||||
DMC_API int dmc_wav_header(char *buf, int n);
|
||||
/* a sound file's samples at 8 kHz, mono, 16-bit: how many, *pcm then
|
||||
allocated (dmc_free() it); -1 if it is not one this reads. It reads
|
||||
WAV (PCM of 8, 16, 24 or 32 bits, 32 or 64-bit floats, A-law, mu-law,
|
||||
IMA and Microsoft ADPCM), AIFF and AIFF-C (PCM, floats, A-law,
|
||||
mu-law) and Sun/NeXT .au, of any rate and channels. (The app reads
|
||||
MP3 and the like with the ACM codecs.) */
|
||||
DMC_API int dmc_audio_decode(const char *data, int n, int16_t **pcm);
|
||||
/* (dmc_audio_decode's old name) */
|
||||
DMC_API int dmc_wav_decode(const char *data, int n, int16_t **pcm);
|
||||
DMC_API void dmc_free(void *p);
|
||||
|
||||
/*
|
||||
* The settings model (AT!M)
|
||||
*/
|
||||
@@ -224,59 +326,297 @@ DMC_API const char *dmc_diag_raw(const dmc_diag *d, int key);
|
||||
/* there was a call to report (key 1 not "no previous call") */
|
||||
DMC_API int dmc_diag_has_call(const dmc_diag *d);
|
||||
|
||||
/*
|
||||
* The connection path
|
||||
*
|
||||
* The picture at the top of the Settings page of how a call is made, as
|
||||
* PCOMM's "Connection Overview" draws its link: four steps, left to right,
|
||||
* each with its role above its icon and what it is below, one line
|
||||
* joining them. Idle, each step is as the line settings set it up (ready,
|
||||
* or something still to set); starting, calling and online, as it is now.
|
||||
* The words are here so that every app says the same; each draws them.
|
||||
*/
|
||||
|
||||
typedef struct dmc_path dmc_path;
|
||||
|
||||
/* the line settings' kinds of line */
|
||||
enum {
|
||||
DMC_LINE_SIP = 0, /* dmodemd on a SIP account (or none) */
|
||||
DMC_LINE_USB, /* dmodemd on a USB modem's phone line */
|
||||
DMC_LINE_CODEC, /* dmodemd on a softmodem codec (ALSA) */
|
||||
DMC_LINE_DEVICE, /* a modem that already runs, at a device */
|
||||
DMC_LINE_TCP, /* dmodemd that already runs, over TCP */
|
||||
DMC_LINE_COM /* a modem on a COM port */
|
||||
};
|
||||
|
||||
/* a step's state: ready (set up, idle), something to set first, not in
|
||||
use, starting or connecting, in the call */
|
||||
enum {
|
||||
DMC_STEP_READY = 0, DMC_STEP_SETUP, DMC_STEP_OFF, DMC_STEP_WORKING,
|
||||
DMC_STEP_ACTIVE
|
||||
};
|
||||
|
||||
/* what a step is, for its icon */
|
||||
enum {
|
||||
DMC_KIND_TERMINAL = 0, DMC_KIND_SOFTMODEM, DMC_KIND_MODEM,
|
||||
DMC_KIND_SIP, DMC_KIND_USB, DMC_KIND_CODEC, DMC_KIND_SERIAL,
|
||||
DMC_KIND_NETWORK, DMC_KIND_PHONE_NETWORK, DMC_KIND_SIP_ADDRESS,
|
||||
DMC_KIND_PHONE_LINE
|
||||
};
|
||||
|
||||
DMC_API dmc_path *dmc_path_new(void);
|
||||
DMC_API void dmc_path_free(dmc_path *p);
|
||||
/* the line settings: the kind of line (DMC_LINE_*), where it is (--usb's
|
||||
device, the codec, the device's path, the TCP address, the COM port;
|
||||
NULL or "" for none), and the SIP account: none (anonymous), its user
|
||||
and server, taking calls (registering) */
|
||||
DMC_API void dmc_path_set_line(dmc_path *p, int line, const char *where,
|
||||
int sip_anonymous, const char *sip_user,
|
||||
const char *sip_server, int listen);
|
||||
/* now: dmodemd (or the device) started, the session's state
|
||||
(dmc_state(), DMC_CLOSED for none), the CONNECT line online */
|
||||
DMC_API void dmc_path_set_live(dmc_path *p, int running, int state,
|
||||
const char *connect);
|
||||
/* the steps: how many, each one's kind, state (DMC_STEP_*), role ("Modem",
|
||||
the label above it), title ("D-Modem") and what it is ("Registers as
|
||||
alice@example.com") */
|
||||
DMC_API int dmc_path_steps(const dmc_path *p);
|
||||
DMC_API int dmc_path_kind(const dmc_path *p, int i);
|
||||
DMC_API int dmc_path_state(const dmc_path *p, int i);
|
||||
DMC_API const char *dmc_path_role(const dmc_path *p, int i);
|
||||
DMC_API const char *dmc_path_title(const dmc_path *p, int i);
|
||||
DMC_API const char *dmc_path_detail(const dmc_path *p, int i);
|
||||
/* the line from step i to the next: the lesser of the two's states
|
||||
(off, then to set, working, ready, active) */
|
||||
DMC_API int dmc_path_link(const dmc_path *p, int i);
|
||||
/* the whole in a line: what needs setting, or what it's doing, or that
|
||||
it's ready */
|
||||
DMC_API const char *dmc_path_headline(const dmc_path *p);
|
||||
/* some step is still to set */
|
||||
DMC_API int dmc_path_needs_setup(const dmc_path *p);
|
||||
|
||||
/*
|
||||
* The terminal screen
|
||||
*
|
||||
* A terminal emulator: the app's (D-Modem/app) ported to C, as one of
|
||||
* many kinds of terminal (a profile): DEC's VT52 to VT525, xterm, a PC's
|
||||
* ANSI-BBS (with Avatar, RIPscrip and Amiga ANSI), the Commodores'
|
||||
* PETSCII, the Atari's ATASCII and ST, Viewdata, the Minitel and the
|
||||
* Tektronix 4014. Bytes from the remote go to dmc_vt_feed(); what it
|
||||
* answers (dmc_vt_reply()) and what keys send (dmc_vt_key_mods(),
|
||||
* dmc_vt_char_bytes()) go back to it. A view draws each cell from
|
||||
* dmc_vt_cell(): its character, its colours resolved, its attributes, and
|
||||
* where the profile has its machine's font, the glyph's bitmap.
|
||||
*/
|
||||
|
||||
typedef struct dmc_vt dmc_vt;
|
||||
|
||||
/* a cell's attributes */
|
||||
/* a cell's attributes (dmc_vt_attr) */
|
||||
enum {
|
||||
DMC_BOLD = 1,
|
||||
DMC_UNDERLINE = 2,
|
||||
DMC_REVERSE = 4
|
||||
};
|
||||
|
||||
/* the keys dmc_vt_key() encodes */
|
||||
/* the keys dmc_vt_key() encodes (the values stay as they are: new ones
|
||||
are added at the end) */
|
||||
enum {
|
||||
DMC_KEY_UP = 1, DMC_KEY_DOWN, DMC_KEY_RIGHT, DMC_KEY_LEFT,
|
||||
DMC_KEY_HOME, DMC_KEY_END, DMC_KEY_PGUP, DMC_KEY_PGDN,
|
||||
DMC_KEY_INSERT, DMC_KEY_DELETE, DMC_KEY_BACKTAB,
|
||||
DMC_KEY_F1, DMC_KEY_F2, DMC_KEY_F3, DMC_KEY_F4, DMC_KEY_F5,
|
||||
DMC_KEY_F6, DMC_KEY_F7, DMC_KEY_F8, DMC_KEY_F9, DMC_KEY_F10,
|
||||
DMC_KEY_ENTER, DMC_KEY_BACKSPACE, DMC_KEY_TAB, DMC_KEY_ESCAPE
|
||||
DMC_KEY_ENTER, DMC_KEY_BACKSPACE, DMC_KEY_TAB, DMC_KEY_ESCAPE,
|
||||
DMC_KEY_F11, DMC_KEY_F12,
|
||||
/* an LK201's F13-F20 (F15 its Help, F16 its Do), Find and Select */
|
||||
DMC_KEY_F13, DMC_KEY_F14, DMC_KEY_F15, DMC_KEY_F16, DMC_KEY_F17,
|
||||
DMC_KEY_F18, DMC_KEY_F19, DMC_KEY_F20, DMC_KEY_FIND, DMC_KEY_SELECT,
|
||||
/* the DEC keypad */
|
||||
DMC_KEY_PF1, DMC_KEY_PF2, DMC_KEY_PF3, DMC_KEY_PF4,
|
||||
DMC_KEY_KP_0, DMC_KEY_KP_1, DMC_KEY_KP_2, DMC_KEY_KP_3, DMC_KEY_KP_4,
|
||||
DMC_KEY_KP_5, DMC_KEY_KP_6, DMC_KEY_KP_7, DMC_KEY_KP_8, DMC_KEY_KP_9,
|
||||
DMC_KEY_KP_MINUS, DMC_KEY_KP_COMMA, DMC_KEY_KP_PERIOD, DMC_KEY_KP_ENTER
|
||||
};
|
||||
|
||||
/* a screen of cols by rows, with scrollback lines kept above it */
|
||||
/* the modifiers held with a key */
|
||||
enum { DMC_MOD_SHIFT = 1, DMC_MOD_ALT = 2, DMC_MOD_CTRL = 4 };
|
||||
|
||||
/* the character sets the remote may speak (a profile can have its own) */
|
||||
enum { DMC_CHARSET_UTF8, DMC_CHARSET_CP437, DMC_CHARSET_LATIN1 };
|
||||
|
||||
/* a cell as a view draws it (dmc_vt_cell) */
|
||||
enum {
|
||||
DMC_CELL_BOLD = 1, /* a heavy face (bold not shown as bright) */
|
||||
DMC_CELL_ITALIC = 2,
|
||||
DMC_CELL_UNDERLINE = 4,
|
||||
DMC_CELL_DOUBLE_UNDERLINE = 8,
|
||||
DMC_CELL_CURLY_UNDERLINE = 16,
|
||||
DMC_CELL_STRIKE = 32,
|
||||
DMC_CELL_OVERLINE = 64,
|
||||
DMC_CELL_BLINK = 128, /* drawn blank in the blink's off phase */
|
||||
DMC_CELL_INVISIBLE = 256, /* not drawn (its background is) */
|
||||
DMC_CELL_WIDE = 512, /* two cells wide: the next is its tail */
|
||||
DMC_CELL_WIDE_TAIL = 1024, /* the right half of the one before */
|
||||
DMC_CELL_IMAGE = 2048, /* a picture's tile (dmc_vt_cell_image) */
|
||||
DMC_CELL_GLYPH = 4096, /* drawn from a bitmap (dmc_vt_glyph_row) */
|
||||
DMC_CELL_COMBINING = 8192, /* has combining marks (dmc_vt_cell_text) */
|
||||
DMC_CELL_LINK = 16384 /* a hyperlink (dmc_vt_cell_link) */
|
||||
};
|
||||
|
||||
typedef struct dmc_cell {
|
||||
uint32_t ch; /* a code point (32 blank, 0 a wide tail) */
|
||||
uint32_t fg, bg; /* 0xRRGGBB, resolved (bold, inverse ...) */
|
||||
int flags; /* DMC_CELL_* */
|
||||
} dmc_cell;
|
||||
|
||||
/* where a cell's tile is in a picture */
|
||||
typedef struct dmc_image {
|
||||
uint32_t id; /* the same while the picture is */
|
||||
int width, height; /* the picture's pixels */
|
||||
int cell_width, cell_height; /* its tiles' size */
|
||||
int tx, ty; /* this cell's tile */
|
||||
} dmc_image;
|
||||
|
||||
/* the line sizes (DECDWL, DECDHL) */
|
||||
enum {
|
||||
DMC_LINE_SINGLE, DMC_LINE_DOUBLE_WIDTH, DMC_LINE_DOUBLE_TOP,
|
||||
DMC_LINE_DOUBLE_BOTTOM
|
||||
};
|
||||
|
||||
/* a screen of cols by rows, with scrollback lines kept above it; an
|
||||
xterm until dmc_vt_set_profile() */
|
||||
DMC_API dmc_vt *dmc_vt_new(int cols, int rows, int scrollback);
|
||||
DMC_API void dmc_vt_free(dmc_vt *t);
|
||||
DMC_API void dmc_vt_resize(dmc_vt *t, int cols, int rows);
|
||||
/* everything as at the start, the scrollback cleared */
|
||||
DMC_API void dmc_vt_reset(dmc_vt *t);
|
||||
DMC_API int dmc_vt_cols(const dmc_vt *t);
|
||||
DMC_API int dmc_vt_rows(const dmc_vt *t);
|
||||
/* bytes from the modem (dmc_terminal()'s) */
|
||||
/* bytes from the remote (dmc_terminal()'s) */
|
||||
DMC_API void dmc_vt_feed(dmc_vt *t, const char *data, int n);
|
||||
/* the screen's answers to the modem's questions (where the cursor is:
|
||||
ESC [6n), to write to the modem as if typed */
|
||||
/* the screen's answers to the remote's questions (where the cursor is,
|
||||
what terminal it is), to write to it as if typed */
|
||||
DMC_API int dmc_vt_reply(dmc_vt *t, char *buf, int max);
|
||||
/* the scrollback's size: the newest lines kept, the oldest dropped */
|
||||
DMC_API void dmc_vt_set_scrollback(dmc_vt *t, int lines);
|
||||
/* how many lines of scrollback there are now */
|
||||
DMC_API int dmc_vt_history(const dmc_vt *t);
|
||||
/* a cell: row 0 the screen's top, -1 the newest scrollback line and so
|
||||
on; its character (a Unicode code point, 32 for none), colours (0 to
|
||||
15 the ANSI palette, -1 the default) and attributes */
|
||||
255 the palette, -1 the default or a 24-bit one) and attributes */
|
||||
DMC_API uint32_t dmc_vt_char(const dmc_vt *t, int row, int col);
|
||||
DMC_API int dmc_vt_fg(const dmc_vt *t, int row, int col);
|
||||
DMC_API int dmc_vt_bg(const dmc_vt *t, int row, int col);
|
||||
DMC_API int dmc_vt_attr(const dmc_vt *t, int row, int col);
|
||||
/* a cell as it is drawn: 0 off the screen (c then blank) */
|
||||
DMC_API int dmc_vt_cell(const dmc_vt *t, int row, int col, dmc_cell *c);
|
||||
/* a cell's character with its combining marks, as UTF-8 */
|
||||
DMC_API const char *dmc_vt_cell_text(dmc_vt *t, int row, int col);
|
||||
/* a cell's hyperlink (OSC 8), NULL for none */
|
||||
DMC_API const char *dmc_vt_cell_link(const dmc_vt *t, int row, int col);
|
||||
/* the picture a cell shows a tile of: its pixels (0xAARRGGBB, width *
|
||||
height, not premultiplied), NULL for none */
|
||||
DMC_API const uint32_t *dmc_vt_cell_image(const dmc_vt *t, int row, int col,
|
||||
dmc_image *im);
|
||||
/* a row's size (DMC_LINE_*): its cells twice as wide, and for double
|
||||
height the top or bottom half of them */
|
||||
DMC_API int dmc_vt_line_size(const dmc_vt *t, int row);
|
||||
/* a row as UTF-8 text, trailing blanks dropped */
|
||||
DMC_API const char *dmc_vt_text(dmc_vt *t, int row);
|
||||
/* the text from (row, col) to (row2, col2), inclusive: wrapped lines
|
||||
joined, the others ended with a newline */
|
||||
DMC_API const char *dmc_vt_text_range(dmc_vt *t, int row, int col, int row2,
|
||||
int col2);
|
||||
DMC_API int dmc_vt_cursor_row(const dmc_vt *t);
|
||||
DMC_API int dmc_vt_cursor_col(const dmc_vt *t);
|
||||
DMC_API int dmc_vt_cursor_visible(const dmc_vt *t);
|
||||
/* DECSCUSR: 0 the view's; 1, 2 block; 3, 4 underline; 5, 6 bar */
|
||||
DMC_API int dmc_vt_cursor_style(const dmc_vt *t);
|
||||
/* the screen changed since the last call (and clears the mark) */
|
||||
DMC_API int dmc_vt_changed(dmc_vt *t);
|
||||
/* the bytes a key sends, into buf: how many */
|
||||
|
||||
/* the bytes a key sends, into buf: how many (dmc_vt_key: no modifiers) */
|
||||
DMC_API int dmc_vt_key(const dmc_vt *t, int key, char *buf, int max);
|
||||
DMC_API int dmc_vt_key_mods(dmc_vt *t, int key, int mods, char *buf, int max);
|
||||
/* the bytes a character typed (or pasted) sends, as this terminal sends
|
||||
it (a Commodore's PETSCII, Ctrl's control codes, Alt as ESC) */
|
||||
DMC_API int dmc_vt_char_bytes(dmc_vt *t, uint32_t cp, int mods, char *buf,
|
||||
int max);
|
||||
/* what Enter sends (0 CR, 1 CR LF, 2 LF) and Backspace (0x08 or 0x7f) */
|
||||
DMC_API void dmc_vt_set_enter(dmc_vt *t, int mode);
|
||||
DMC_API void dmc_vt_set_backspace(dmc_vt *t, int code);
|
||||
/* HyperTerminal's ASCII Setup, for the received data: an LF added after
|
||||
each CR (the LF of a CR LF is not doubled), each byte set to 7 bits,
|
||||
and no wrap at the right margin (the last column is written again) */
|
||||
#define DMC_VT_ASCII_ADD_LF 1
|
||||
#define DMC_VT_ASCII_7BIT 2
|
||||
#define DMC_VT_ASCII_NO_WRAP 4
|
||||
DMC_API void dmc_vt_set_ascii(dmc_vt *t, int flags);
|
||||
DMC_API int dmc_vt_get_ascii(const dmc_vt *t);
|
||||
/* a mouse event, when the remote asked for them: button 0-2, 64 and 65
|
||||
the wheel; the bytes to send (0: none) */
|
||||
DMC_API int dmc_vt_mouse_wanted(const dmc_vt *t);
|
||||
DMC_API int dmc_vt_mouse(dmc_vt *t, int button, int col, int row, int press,
|
||||
int motion, int mods, char *buf, int max);
|
||||
/* a click at (fx, fy), fractions of the screen from its top left: what
|
||||
it sends (a RIPscrip button's), 0 for nothing */
|
||||
DMC_API int dmc_vt_tap(dmc_vt *t, double fx, double fy, char *buf, int max);
|
||||
/* the remote asked for pastes bracketed (ESC [200~ ... ESC [201~) */
|
||||
DMC_API int dmc_vt_bracketed_paste(const dmc_vt *t);
|
||||
/* the focus's change, when the remote asked to be told */
|
||||
DMC_API int dmc_vt_focus(dmc_vt *t, int focused, char *buf, int max);
|
||||
|
||||
/* the kinds of terminal, in the pickers' order: their ids (kept in
|
||||
settings), names, the TERM for SSH and the TERMINAL-TYPE for telnet */
|
||||
DMC_API int dmc_vt_profile_count(void);
|
||||
DMC_API const char *dmc_vt_profile_id(int i);
|
||||
DMC_API const char *dmc_vt_profile_label(int i);
|
||||
DMC_API const char *dmc_vt_profile_term(const char *id);
|
||||
DMC_API const char *dmc_vt_profile_telnet(const char *id);
|
||||
/* the kind of terminal it is (1), or 0 for an id not known */
|
||||
DMC_API int dmc_vt_set_profile(dmc_vt *t, const char *id);
|
||||
DMC_API const char *dmc_vt_get_profile(const dmc_vt *t);
|
||||
/* the user's character set, for the profiles that haven't their own */
|
||||
DMC_API void dmc_vt_set_charset(dmc_vt *t, int charset);
|
||||
/* the columns and rows the view should fit (DECCOLM's 80 or 132, a
|
||||
Commodore's 40x25), 0 for as many as fit */
|
||||
DMC_API int dmc_vt_requested_cols(const dmc_vt *t);
|
||||
DMC_API int dmc_vt_requested_rows(const dmc_vt *t);
|
||||
/* a cell's size in pixels as the view draws it (sixel images are
|
||||
measured in it, and it is reported to the remote) */
|
||||
DMC_API void dmc_vt_set_cell_pixels(dmc_vt *t, int w, int h);
|
||||
/* the theme: the default foreground, background and cursor (0xRRGGBB) and
|
||||
the 16 colours (NULL: kept); bold shown as the bright colours */
|
||||
DMC_API void dmc_vt_set_theme(dmc_vt *t, uint32_t fg, uint32_t bg,
|
||||
uint32_t cursor, const uint32_t *palette16);
|
||||
DMC_API void dmc_vt_set_bold_bright(dmc_vt *t, int on);
|
||||
/* the screen's background, foreground and cursor as shown now (a
|
||||
machine's own, or the remote's, or the theme's) */
|
||||
DMC_API uint32_t dmc_vt_background(const dmc_vt *t);
|
||||
DMC_API uint32_t dmc_vt_foreground(const dmc_vt *t);
|
||||
DMC_API uint32_t dmc_vt_cursor_color(const dmc_vt *t);
|
||||
/* the bitmap font the profile's machine had (its cell's size: 1), 0 for
|
||||
the view's own font */
|
||||
DMC_API int dmc_vt_glyph_cell(const dmc_vt *t, int *w, int *h);
|
||||
/* whether cp is drawn from a bitmap (the machine's font, a soft
|
||||
character), and its size */
|
||||
DMC_API int dmc_vt_glyph(const dmc_vt *t, uint32_t cp, int *w, int *h);
|
||||
/* row (0 top) of cp's bitmap, its leftmost pixel bit w - 1 */
|
||||
DMC_API uint32_t dmc_vt_glyph_row(const dmc_vt *t, uint32_t cp, int row);
|
||||
/* what the status line below the screen shows ("" for none: no line) */
|
||||
DMC_API const char *dmc_vt_status_line(dmc_vt *t);
|
||||
/* the window's title as the remote set it, and whether it changed */
|
||||
DMC_API const char *dmc_vt_title(dmc_vt *t);
|
||||
DMC_API int dmc_vt_title_changed(const dmc_vt *t);
|
||||
/* how many times the bell rang since the last call */
|
||||
DMC_API int dmc_vt_bells(dmc_vt *t);
|
||||
/* ANSI music: the next note to play (1), its pitch in hundredths of a
|
||||
hertz (0 a rest), how long it sounds and the silence after it */
|
||||
DMC_API int dmc_vt_music(dmc_vt *t, uint32_t *hz100, int *ms, int *rest_ms);
|
||||
/* what the remote sent to the printer (UTF-8), taken */
|
||||
DMC_API int dmc_vt_printed(dmc_vt *t, char *buf, int max);
|
||||
/* the line speed the remote asked its text be shown at (CSI * r), 0 */
|
||||
DMC_API int dmc_vt_emulated_bps(const dmc_vt *t);
|
||||
/* the RGB of palette colour c (0 to 15), as 0xRRGGBB */
|
||||
DMC_API uint32_t dmc_vt_palette(int c);
|
||||
|
||||
@@ -291,6 +631,149 @@ DMC_API int dmc_daemon_ready(const char *line, char *buf, int max);
|
||||
/* a line that is an error of dmodemd's ("error: ..."): 1 */
|
||||
DMC_API int dmc_daemon_error(const char *line);
|
||||
|
||||
/*
|
||||
* Remote logins
|
||||
*
|
||||
* Telnet and rlogin on a TCP connection the app makes (SSH is the
|
||||
* system's ssh in a pty, so it needs nothing here). Like the session it
|
||||
* does no I/O: hand it what the socket read, write what dmc_net_output()
|
||||
* gives, show what dmc_net_screen() gives.
|
||||
*/
|
||||
|
||||
typedef struct dmc_net dmc_net;
|
||||
|
||||
enum { DMC_NET_TELNET = 1, DMC_NET_RLOGIN, DMC_NET_SSH };
|
||||
|
||||
/* the protocol's port (23, 513, 22) and its name ("Telnet") */
|
||||
DMC_API int dmc_net_default_port(int proto);
|
||||
DMC_API const char *dmc_net_name(int proto);
|
||||
/* a login by Telnet or rlogin: user is rlogin's (Telnet's may be ""),
|
||||
term the terminal type the server is told, the screen's size */
|
||||
DMC_API dmc_net *dmc_net_new(int proto, const char *user, const char *term,
|
||||
int cols, int rows);
|
||||
DMC_API void dmc_net_free(dmc_net *n);
|
||||
/* the connection made: rlogin's handshake, Telnet's first offers */
|
||||
DMC_API void dmc_net_connected(dmc_net *n);
|
||||
/* bytes the socket read */
|
||||
DMC_API void dmc_net_feed(dmc_net *n, const char *data, int len);
|
||||
/* an urgent (out-of-band) byte the socket read: rlogin's control byte */
|
||||
DMC_API void dmc_net_urgent(dmc_net *n, int byte);
|
||||
/* the bytes for the terminal: how many */
|
||||
DMC_API int dmc_net_screen(dmc_net *n, char *buf, int max);
|
||||
/* bytes typed */
|
||||
DMC_API void dmc_net_type(dmc_net *n, const char *data, int len);
|
||||
/* the bytes to write to the socket: how many */
|
||||
DMC_API int dmc_net_output(dmc_net *n, char *buf, int max);
|
||||
/* the screen's new size, told to the server */
|
||||
DMC_API void dmc_net_resize(dmc_net *n, int cols, int rows);
|
||||
/* a break to the server (Telnet's IAC BRK): 1, or 0 when the protocol has
|
||||
none (rlogin) */
|
||||
DMC_API int dmc_net_break(dmc_net *n);
|
||||
|
||||
/* ------------------------------------------------------------------
|
||||
* File transfers on a terminal's line, as the Android app has them:
|
||||
* ZMODEM, YMODEM, XMODEM, XMODEM-1K, Kermit, Intel HEX, Motorola
|
||||
* S-records and plain text. A transfer runs on a thread of its own; the
|
||||
* app feeds it what comes from the remote (in place of the terminal),
|
||||
* writes what it gives to the remote, and reads its status.
|
||||
*/
|
||||
enum {
|
||||
DMC_XFER_ZMODEM,
|
||||
DMC_XFER_YMODEM,
|
||||
DMC_XFER_YMODEM_G, /* YMODEM with no ACKs (an error-free line) */
|
||||
DMC_XFER_XMODEM,
|
||||
DMC_XFER_XMODEM_1K,
|
||||
DMC_XFER_KERMIT,
|
||||
DMC_XFER_INTEL_HEX,
|
||||
DMC_XFER_SREC,
|
||||
DMC_XFER_TEXT,
|
||||
DMC_XFER_PROTOCOLS
|
||||
};
|
||||
|
||||
/* a protocol's name ("ZMODEM"); whether it sends more than one file */
|
||||
DMC_API const char *dmc_xfer_label(int protocol);
|
||||
DMC_API int dmc_xfer_batch(int protocol);
|
||||
/* what to type at the remote's shell for its end: remote_sends, its send
|
||||
(the files' names go after it, "sz -b "), else its receive ("rz -b") */
|
||||
DMC_API const char *dmc_xfer_command(int protocol, int remote_sends);
|
||||
|
||||
typedef struct dmc_xfer dmc_xfer;
|
||||
|
||||
/* how a transfer goes (all 0: as the protocol does by default) */
|
||||
typedef struct dmc_xfer_options {
|
||||
/* ZMODEM crash recovery: a part of a file that is in the folder is
|
||||
continued, and a part received is kept when the transfer stops
|
||||
(receiving); the receiver is asked to continue (sending) */
|
||||
int resume;
|
||||
/* plain text sent: a wait after each line, and after each
|
||||
character, in ms */
|
||||
int line_delay_ms, char_delay_ms;
|
||||
/* ZMODEM with zstd stopped: files are not compressed (sending), and
|
||||
compressed files are not taken (receiving). When it is not
|
||||
stopped, zstd is used only where the other side can use it too
|
||||
(D-Modem's BBS, GUI and app) */
|
||||
int no_zstd;
|
||||
} dmc_xfer_options;
|
||||
|
||||
/* files from the remote into folder (name: XMODEM's file, which it does
|
||||
not name; Intel HEX's, S-records' and plain text's too) */
|
||||
DMC_API dmc_xfer *dmc_xfer_receive(int protocol, const char *folder,
|
||||
const char *name);
|
||||
/* files to the remote (XMODEM, the hex records and plain text: the first) */
|
||||
DMC_API dmc_xfer *dmc_xfer_send(int protocol, const char *const *paths,
|
||||
int n);
|
||||
/* the same, with options (NULL: the defaults) */
|
||||
DMC_API dmc_xfer *dmc_xfer_receive_opt(int protocol, const char *folder,
|
||||
const char *name,
|
||||
const dmc_xfer_options *o);
|
||||
DMC_API dmc_xfer *dmc_xfer_send_opt(int protocol, const char *const *paths,
|
||||
int n, const dmc_xfer_options *o);
|
||||
/* the file being received skipped, and the transfer goes on to the next
|
||||
(ZMODEM and Kermit; the others ignore it) */
|
||||
DMC_API void dmc_xfer_skip(dmc_xfer *x);
|
||||
/* whether the protocol can skip a file (receiving) */
|
||||
DMC_API int dmc_xfer_can_skip(int protocol);
|
||||
/* whether the file now goes compressed with zstd (ZMODEM with zstd) */
|
||||
DMC_API int dmc_xfer_zstd(dmc_xfer *x);
|
||||
/* bytes from the remote */
|
||||
DMC_API void dmc_xfer_feed(dmc_xfer *x, const char *data, int n);
|
||||
/* bytes for the remote: how many */
|
||||
DMC_API int dmc_xfer_output(dmc_xfer *x, char *buf, int max);
|
||||
/* stopped: the other side told (ZMODEM's CANs, Kermit's E packet) */
|
||||
DMC_API void dmc_xfer_cancel(dmc_xfer *x);
|
||||
/* fn(ctx) called, on the transfer's thread, when it has bytes for the
|
||||
remote or has ended (once until the next dmc_xfer_output): for an app
|
||||
that posts itself a message to write them at once */
|
||||
DMC_API void dmc_xfer_set_notify(dmc_xfer *x, void (*fn)(void *ctx), void *ctx);
|
||||
|
||||
typedef struct dmc_xfer_status {
|
||||
int protocol, sending;
|
||||
int running; /* 0: ended, result says how */
|
||||
char file[256]; /* the file now */
|
||||
long long bytes, size; /* size -1: not known */
|
||||
int files; /* files done */
|
||||
int errors; /* blocks sent again */
|
||||
char result[256]; /* "" it went well, else why not */
|
||||
} dmc_xfer_status;
|
||||
|
||||
DMC_API void dmc_xfer_get_status(dmc_xfer *x, dmc_xfer_status *st);
|
||||
/* the names the received files got (i from 0; NULL past the last) */
|
||||
DMC_API const char *dmc_xfer_received(dmc_xfer *x, int i);
|
||||
/* once ended: what came after the transfer, for the terminal */
|
||||
DMC_API int dmc_xfer_leftover(dmc_xfer *x, char *buf, int max);
|
||||
/* the thread waited for, all freed */
|
||||
DMC_API void dmc_xfer_free(dmc_xfer *x);
|
||||
|
||||
/* what a remote starts in what came: its sz (receive here), its rz (it
|
||||
waits for files) or its kermit -s; *at where it starts */
|
||||
enum {
|
||||
DMC_XFER_DETECT_NONE,
|
||||
DMC_XFER_DETECT_ZMODEM_SEND,
|
||||
DMC_XFER_DETECT_ZMODEM_RECEIVE,
|
||||
DMC_XFER_DETECT_KERMIT_SEND
|
||||
};
|
||||
DMC_API int dmc_xfer_detect(const char *data, int n, int *at);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,480 @@
|
||||
GNU LIBRARY GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1991 Free Software Foundation, Inc.
|
||||
<https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
[This is the first released version of the library GPL. It is
|
||||
numbered 2 because it goes with version 2 of the ordinary GPL.]
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
Licenses are intended to guarantee your freedom to share and change
|
||||
free software--to make sure the software is free for all its users.
|
||||
|
||||
This license, the Library General Public License, applies to some
|
||||
specially designated Free Software Foundation software, and to any
|
||||
other libraries whose authors decide to use it. You can use it for
|
||||
your libraries, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if
|
||||
you distribute copies of the library, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of the library, whether gratis
|
||||
or for a fee, you must give the recipients all the rights that we gave
|
||||
you. You must make sure that they, too, receive or can get the source
|
||||
code. If you link a program with the library, you must provide
|
||||
complete object files to the recipients so that they can relink them
|
||||
with the library, after making changes to the library and recompiling
|
||||
it. And you must show them these terms so they know their rights.
|
||||
|
||||
Our method of protecting your rights has two steps: (1) copyright
|
||||
the library, and (2) offer you this license which gives you legal
|
||||
permission to copy, distribute and/or modify the library.
|
||||
|
||||
Also, for each distributor's protection, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
library. If the library is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original
|
||||
version, so that any problems introduced by others will not reflect on
|
||||
the original authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that companies distributing free
|
||||
software will individually obtain patent licenses, thus in effect
|
||||
transforming the program into proprietary software. To prevent this,
|
||||
we have made it clear that any patent must be licensed for everyone's
|
||||
free use or not licensed at all.
|
||||
|
||||
Most GNU software, including some libraries, is covered by the ordinary
|
||||
GNU General Public License, which was designed for utility programs. This
|
||||
license, the GNU Library General Public License, applies to certain
|
||||
designated libraries. This license is quite different from the ordinary
|
||||
one; be sure to read it in full, and don't assume that anything in it is
|
||||
the same as in the ordinary license.
|
||||
|
||||
The reason we have a separate public license for some libraries is that
|
||||
they blur the distinction we usually make between modifying or adding to a
|
||||
program and simply using it. Linking a program with a library, without
|
||||
changing the library, is in some sense simply using the library, and is
|
||||
analogous to running a utility program or application program. However, in
|
||||
a textual and legal sense, the linked executable is a combined work, a
|
||||
derivative of the original library, and the ordinary General Public License
|
||||
treats it as such.
|
||||
|
||||
Because of this blurred distinction, using the ordinary General
|
||||
Public License for libraries did not effectively promote software
|
||||
sharing, because most developers did not use the libraries. We
|
||||
concluded that weaker conditions might promote sharing better.
|
||||
|
||||
However, unrestricted linking of non-free programs would deprive the
|
||||
users of those programs of all benefit from the free status of the
|
||||
libraries themselves. This Library General Public License is intended to
|
||||
permit developers of non-free programs to use free libraries, while
|
||||
preserving your freedom as a user of such programs to change the free
|
||||
libraries that are incorporated in them. (We have not seen how to achieve
|
||||
this as regards changes in header files, but we have achieved it as regards
|
||||
changes in the actual functions of the Library.) The hope is that this
|
||||
will lead to faster development of free libraries.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow. Pay close attention to the difference between a
|
||||
"work based on the library" and a "work that uses the library". The
|
||||
former contains code derived from the library, while the latter only
|
||||
works together with the library.
|
||||
|
||||
Note that it is possible for a library to be covered by the ordinary
|
||||
General Public License rather than by this special one.
|
||||
|
||||
GNU LIBRARY GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License Agreement applies to any software library which
|
||||
contains a notice placed by the copyright holder or other authorized
|
||||
party saying it may be distributed under the terms of this Library
|
||||
General Public License (also called "this License"). Each licensee is
|
||||
addressed as "you".
|
||||
|
||||
A "library" means a collection of software functions and/or data
|
||||
prepared so as to be conveniently linked with application programs
|
||||
(which use some of those functions and data) to form executables.
|
||||
|
||||
The "Library", below, refers to any such software library or work
|
||||
which has been distributed under these terms. A "work based on the
|
||||
Library" means either the Library or any derivative work under
|
||||
copyright law: that is to say, a work containing the Library or a
|
||||
portion of it, either verbatim or with modifications and/or translated
|
||||
straightforwardly into another language. (Hereinafter, translation is
|
||||
included without limitation in the term "modification".)
|
||||
|
||||
"Source code" for a work means the preferred form of the work for
|
||||
making modifications to it. For a library, complete source code means
|
||||
all the source code for all modules it contains, plus any associated
|
||||
interface definition files, plus the scripts used to control compilation
|
||||
and installation of the library.
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running a program using the Library is not restricted, and output from
|
||||
such a program is covered only if its contents constitute a work based
|
||||
on the Library (independent of the use of the Library in a tool for
|
||||
writing it). Whether that is true depends on what the Library does
|
||||
and what the program that uses the Library does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Library's
|
||||
complete source code as you receive it, in any medium, provided that
|
||||
you conspicuously and appropriately publish on each copy an
|
||||
appropriate copyright notice and disclaimer of warranty; keep intact
|
||||
all the notices that refer to this License and to the absence of any
|
||||
warranty; and distribute a copy of this License along with the
|
||||
Library.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy,
|
||||
and you may at your option offer warranty protection in exchange for a
|
||||
fee.
|
||||
|
||||
2. You may modify your copy or copies of the Library or any portion
|
||||
of it, thus forming a work based on the Library, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) The modified work must itself be a software library.
|
||||
|
||||
b) You must cause the files modified to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
c) You must cause the whole of the work to be licensed at no
|
||||
charge to all third parties under the terms of this License.
|
||||
|
||||
d) If a facility in the modified Library refers to a function or a
|
||||
table of data to be supplied by an application program that uses
|
||||
the facility, other than as an argument passed when the facility
|
||||
is invoked, then you must make a good faith effort to ensure that,
|
||||
in the event an application does not supply such function or
|
||||
table, the facility still operates, and performs whatever part of
|
||||
its purpose remains meaningful.
|
||||
|
||||
(For example, a function in a library to compute square roots has
|
||||
a purpose that is entirely well-defined independent of the
|
||||
application. Therefore, Subsection 2d requires that any
|
||||
application-supplied function or table used by this function must
|
||||
be optional: if the application does not supply it, the square
|
||||
root function must still compute square roots.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Library,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Library, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote
|
||||
it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Library.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Library
|
||||
with the Library (or with a work based on the Library) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may opt to apply the terms of the ordinary GNU General Public
|
||||
License instead of this License to a given copy of the Library. To do
|
||||
this, you must alter all the notices that refer to this License, so
|
||||
that they refer to the ordinary GNU General Public License, version 2,
|
||||
instead of to this License. (If a newer version than version 2 of the
|
||||
ordinary GNU General Public License has appeared, then you can specify
|
||||
that version instead if you wish.) Do not make any other change in
|
||||
these notices.
|
||||
|
||||
Once this change is made in a given copy, it is irreversible for
|
||||
that copy, so the ordinary GNU General Public License applies to all
|
||||
subsequent copies and derivative works made from that copy.
|
||||
|
||||
This option is useful when you wish to copy part of the code of
|
||||
the Library into a program that is not a library.
|
||||
|
||||
4. You may copy and distribute the Library (or a portion or
|
||||
derivative of it, under Section 2) in object code or executable form
|
||||
under the terms of Sections 1 and 2 above provided that you accompany
|
||||
it with the complete corresponding machine-readable source code, which
|
||||
must be distributed under the terms of Sections 1 and 2 above on a
|
||||
medium customarily used for software interchange.
|
||||
|
||||
If distribution of object code is made by offering access to copy
|
||||
from a designated place, then offering equivalent access to copy the
|
||||
source code from the same place satisfies the requirement to
|
||||
distribute the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
5. A program that contains no derivative of any portion of the
|
||||
Library, but is designed to work with the Library by being compiled or
|
||||
linked with it, is called a "work that uses the Library". Such a
|
||||
work, in isolation, is not a derivative work of the Library, and
|
||||
therefore falls outside the scope of this License.
|
||||
|
||||
However, linking a "work that uses the Library" with the Library
|
||||
creates an executable that is a derivative of the Library (because it
|
||||
contains portions of the Library), rather than a "work that uses the
|
||||
library". The executable is therefore covered by this License.
|
||||
Section 6 states terms for distribution of such executables.
|
||||
|
||||
When a "work that uses the Library" uses material from a header file
|
||||
that is part of the Library, the object code for the work may be a
|
||||
derivative work of the Library even though the source code is not.
|
||||
Whether this is true is especially significant if the work can be
|
||||
linked without the Library, or if the work is itself a library. The
|
||||
threshold for this to be true is not precisely defined by law.
|
||||
|
||||
If such an object file uses only numerical parameters, data
|
||||
structure layouts and accessors, and small macros and small inline
|
||||
functions (ten lines or less in length), then the use of the object
|
||||
file is unrestricted, regardless of whether it is legally a derivative
|
||||
work. (Executables containing this object code plus portions of the
|
||||
Library will still fall under Section 6.)
|
||||
|
||||
Otherwise, if the work is a derivative of the Library, you may
|
||||
distribute the object code for the work under the terms of Section 6.
|
||||
Any executables containing that work also fall under Section 6,
|
||||
whether or not they are linked directly with the Library itself.
|
||||
|
||||
6. As an exception to the Sections above, you may also compile or
|
||||
link a "work that uses the Library" with the Library to produce a
|
||||
work containing portions of the Library, and distribute that work
|
||||
under terms of your choice, provided that the terms permit
|
||||
modification of the work for the customer's own use and reverse
|
||||
engineering for debugging such modifications.
|
||||
|
||||
You must give prominent notice with each copy of the work that the
|
||||
Library is used in it and that the Library and its use are covered by
|
||||
this License. You must supply a copy of this License. If the work
|
||||
during execution displays copyright notices, you must include the
|
||||
copyright notice for the Library among them, as well as a reference
|
||||
directing the user to the copy of this License. Also, you must do one
|
||||
of these things:
|
||||
|
||||
a) Accompany the work with the complete corresponding
|
||||
machine-readable source code for the Library including whatever
|
||||
changes were used in the work (which must be distributed under
|
||||
Sections 1 and 2 above); and, if the work is an executable linked
|
||||
with the Library, with the complete machine-readable "work that
|
||||
uses the Library", as object code and/or source code, so that the
|
||||
user can modify the Library and then relink to produce a modified
|
||||
executable containing the modified Library. (It is understood
|
||||
that the user who changes the contents of definitions files in the
|
||||
Library will not necessarily be able to recompile the application
|
||||
to use the modified definitions.)
|
||||
|
||||
b) Accompany the work with a written offer, valid for at
|
||||
least three years, to give the same user the materials
|
||||
specified in Subsection 6a, above, for a charge no more
|
||||
than the cost of performing this distribution.
|
||||
|
||||
c) If distribution of the work is made by offering access to copy
|
||||
from a designated place, offer equivalent access to copy the above
|
||||
specified materials from the same place.
|
||||
|
||||
d) Verify that the user has already received a copy of these
|
||||
materials or that you have already sent this user a copy.
|
||||
|
||||
For an executable, the required form of the "work that uses the
|
||||
Library" must include any data and utility programs needed for
|
||||
reproducing the executable from it. However, as a special exception,
|
||||
the source code distributed need not include anything that is normally
|
||||
distributed (in either source or binary form) with the major
|
||||
components (compiler, kernel, and so on) of the operating system on
|
||||
which the executable runs, unless that component itself accompanies
|
||||
the executable.
|
||||
|
||||
It may happen that this requirement contradicts the license
|
||||
restrictions of other proprietary libraries that do not normally
|
||||
accompany the operating system. Such a contradiction means you cannot
|
||||
use both them and the Library together in an executable that you
|
||||
distribute.
|
||||
|
||||
7. You may place library facilities that are a work based on the
|
||||
Library side-by-side in a single library together with other library
|
||||
facilities not covered by this License, and distribute such a combined
|
||||
library, provided that the separate distribution of the work based on
|
||||
the Library and of the other library facilities is otherwise
|
||||
permitted, and provided that you do these two things:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work
|
||||
based on the Library, uncombined with any other library
|
||||
facilities. This must be distributed under the terms of the
|
||||
Sections above.
|
||||
|
||||
b) Give prominent notice with the combined library of the fact
|
||||
that part of it is a work based on the Library, and explaining
|
||||
where to find the accompanying uncombined form of the same work.
|
||||
|
||||
8. You may not copy, modify, sublicense, link with, or distribute
|
||||
the Library except as expressly provided under this License. Any
|
||||
attempt otherwise to copy, modify, sublicense, link with, or
|
||||
distribute the Library is void, and will automatically terminate your
|
||||
rights under this License. However, parties who have received copies,
|
||||
or rights, from you under this License will not have their licenses
|
||||
terminated so long as such parties remain in full compliance.
|
||||
|
||||
9. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Library or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Library (or any work based on the
|
||||
Library), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Library or works based on it.
|
||||
|
||||
10. Each time you redistribute the Library (or any work based on the
|
||||
Library), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute, link with or modify the Library
|
||||
subject to these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
11. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Library at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Library by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Library.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under any
|
||||
particular circumstance, the balance of the section is intended to apply,
|
||||
and the section as a whole is intended to apply in other circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
12. If the distribution and/or use of the Library is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Library under this License may add
|
||||
an explicit geographical distribution limitation excluding those countries,
|
||||
so that distribution is permitted only in or among countries not thus
|
||||
excluded. In such case, this License incorporates the limitation as if
|
||||
written in the body of this License.
|
||||
|
||||
13. The Free Software Foundation may publish revised and/or new
|
||||
versions of the Library General Public License from time to time.
|
||||
Such new versions will be similar in spirit to the present version,
|
||||
but may differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Library
|
||||
specifies a version number of this License which applies to it and
|
||||
"any later version", you have the option of following the terms and
|
||||
conditions either of that version or of any later version published by
|
||||
the Free Software Foundation. If the Library does not specify a
|
||||
license version number, you may choose any version ever published by
|
||||
the Free Software Foundation.
|
||||
|
||||
14. If you wish to incorporate parts of the Library into other free
|
||||
programs whose distribution conditions are incompatible with these,
|
||||
write to the author to ask for permission. For software which is
|
||||
copyrighted by the Free Software Foundation, write to the Free
|
||||
Software Foundation; we sometimes make exceptions for this. Our
|
||||
decision will be guided by the two goals of preserving the free status
|
||||
of all derivatives of our free software and of promoting the sharing
|
||||
and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
|
||||
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
|
||||
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
|
||||
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
|
||||
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
|
||||
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
|
||||
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
|
||||
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
|
||||
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
|
||||
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
|
||||
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
|
||||
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
|
||||
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
|
||||
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
|
||||
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Libraries
|
||||
|
||||
If you develop a new library, and you want it to be of the greatest
|
||||
possible use to the public, we recommend making it free software that
|
||||
everyone can redistribute and change. You can do so by permitting
|
||||
redistribution under these terms (or, alternatively, under the terms of the
|
||||
ordinary General Public License).
|
||||
|
||||
To apply these terms, attach the following notices to the library. It is
|
||||
safest to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least the
|
||||
"copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the library's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the library, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the
|
||||
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
|
||||
|
||||
<signature of Moe Ghoul>, 1 April 1990
|
||||
Moe Ghoul, President of Vice
|
||||
|
||||
That's all there is to it!
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* daemon.c - reading what dmodemd prints as it starts (dmodemctl.h)
|
||||
*
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* diag.c - the last call's statistics from AT#UD (dmodemctl.h)
|
||||
*
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* model.c - the modem's settings from AT!M (dmodemctl.h)
|
||||
*
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* net.c - a remote login's protocol over a TCP connection the app makes:
|
||||
* Telnet (RFC 854) and rlogin (RFC 1282). Like the session, it
|
||||
* does no I/O: the app hands it what the socket read
|
||||
* (dmc_net_feed), writes what dmc_net_output gives, shows what
|
||||
* dmc_net_screen gives and passes on what is typed (dmc_net_type).
|
||||
*
|
||||
* Telnet: the server's options answered (BINARY, ECHO and
|
||||
* SUPPRESS-GO-AHEAD taken; TERMINAL-TYPE and NAWS offered; the rest
|
||||
* refused), each option answered once per change so that no two ends
|
||||
* loop; IAC doubled both ways; a CR typed sent as CR NUL outside binary
|
||||
* mode; the window's size sent (NAWS) as it changes.
|
||||
*
|
||||
* rlogin: the handshake (NUL, the user twice, "term/speed"), the
|
||||
* server's NUL that answers it taken off the stream, and the window's
|
||||
* size sent ("\377\377ss" and four 16-bit numbers) once the server has
|
||||
* asked for it with the urgent byte 0x80.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "util.h"
|
||||
|
||||
enum {
|
||||
IAC = 255, DONT = 254, DO = 253, WONT = 252, WILL = 251, SB = 250,
|
||||
SE = 240, BRK = 243,
|
||||
O_BINARY = 0, O_ECHO = 1, O_SGA = 3, O_TTYPE = 24, O_NAWS = 31
|
||||
};
|
||||
|
||||
/* where the Telnet reader is */
|
||||
enum { T_DATA, T_IAC, T_OPT, T_SB, T_SB_IAC };
|
||||
|
||||
struct dmc_net {
|
||||
int proto;
|
||||
char *user, *term;
|
||||
int cols, rows;
|
||||
dmc_buf screen, out;
|
||||
/* Telnet */
|
||||
int st, verb, last_cr;
|
||||
unsigned char remote[256], local[256];
|
||||
dmc_buf sb;
|
||||
/* Telnet: the server asked for our size (DO NAWS) */
|
||||
int naws;
|
||||
/* rlogin: the server's NUL seen; it asked for the window's size */
|
||||
int ready, want_size;
|
||||
};
|
||||
|
||||
DMC_API int dmc_net_default_port(int proto)
|
||||
{
|
||||
switch (proto) {
|
||||
case DMC_NET_TELNET:
|
||||
return 23;
|
||||
case DMC_NET_RLOGIN:
|
||||
return 513;
|
||||
case DMC_NET_SSH:
|
||||
return 22;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
DMC_API const char *dmc_net_name(int proto)
|
||||
{
|
||||
switch (proto) {
|
||||
case DMC_NET_TELNET:
|
||||
return "Telnet";
|
||||
case DMC_NET_RLOGIN:
|
||||
return "rlogin";
|
||||
case DMC_NET_SSH:
|
||||
return "SSH";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
DMC_API dmc_net *dmc_net_new(int proto, const char *user, const char *term,
|
||||
int cols, int rows)
|
||||
{
|
||||
dmc_net *n = calloc(1, sizeof(*n));
|
||||
if (!n)
|
||||
return NULL;
|
||||
n->proto = proto;
|
||||
n->user = dup_str(user ? user : "");
|
||||
n->term = dup_str(term && *term ? term : "xterm");
|
||||
n->cols = cols;
|
||||
n->rows = rows;
|
||||
return n;
|
||||
}
|
||||
|
||||
DMC_API void dmc_net_free(dmc_net *n)
|
||||
{
|
||||
if (!n)
|
||||
return;
|
||||
free(n->user);
|
||||
free(n->term);
|
||||
buf_free(&n->screen);
|
||||
buf_free(&n->out);
|
||||
buf_free(&n->sb);
|
||||
free(n);
|
||||
}
|
||||
|
||||
static void put(dmc_net *n, int a, int b, int c)
|
||||
{
|
||||
char v[3];
|
||||
v[0] = (char)a;
|
||||
v[1] = (char)b;
|
||||
v[2] = (char)c;
|
||||
buf_add(&n->out, v, 3);
|
||||
}
|
||||
|
||||
/* a 16-bit number, big-endian; for Telnet an 0xff in it doubled */
|
||||
static void put16(dmc_net *n, int v, int telnet)
|
||||
{
|
||||
int i;
|
||||
if (v < 0)
|
||||
v = 0;
|
||||
if (v > 65535)
|
||||
v = 65535;
|
||||
for (i = 0; i < 2; i++) {
|
||||
int b = i ? v & 0xff : v >> 8;
|
||||
buf_addc(&n->out, (char)b);
|
||||
if (telnet && b == IAC)
|
||||
buf_addc(&n->out, (char)IAC);
|
||||
}
|
||||
}
|
||||
|
||||
static void send_naws(dmc_net *n)
|
||||
{
|
||||
put(n, IAC, SB, O_NAWS);
|
||||
put16(n, n->cols, 1);
|
||||
put16(n, n->rows, 1);
|
||||
buf_addc(&n->out, (char)IAC);
|
||||
buf_addc(&n->out, (char)SE);
|
||||
}
|
||||
|
||||
static void send_window(dmc_net *n)
|
||||
{
|
||||
buf_add(&n->out, "\377\377ss", 4);
|
||||
put16(n, n->rows, 0);
|
||||
put16(n, n->cols, 0);
|
||||
put16(n, n->cols * 8, 0);
|
||||
put16(n, n->rows * 16, 0);
|
||||
}
|
||||
|
||||
DMC_API void dmc_net_connected(dmc_net *n)
|
||||
{
|
||||
if (n->proto == DMC_NET_RLOGIN) {
|
||||
/* NUL, the local and the remote user, "term/speed" */
|
||||
buf_addc(&n->out, '\0');
|
||||
buf_add(&n->out, n->user, (int)strlen(n->user) + 1);
|
||||
buf_add(&n->out, n->user, (int)strlen(n->user) + 1);
|
||||
buf_adds(&n->out, n->term);
|
||||
buf_add(&n->out, "/38400", 7);
|
||||
} else if (n->proto == DMC_NET_TELNET) {
|
||||
/* what a Telnet client offers at once: its type and size,
|
||||
and no go-aheads; marked as asked, so that the server's
|
||||
yes is not answered again */
|
||||
put(n, IAC, WILL, O_TTYPE);
|
||||
put(n, IAC, WILL, O_NAWS);
|
||||
put(n, IAC, DO, O_SGA);
|
||||
n->local[O_TTYPE] = n->local[O_NAWS] = 1;
|
||||
n->remote[O_SGA] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* the server's WILL, WONT, DO or DONT */
|
||||
static void option(dmc_net *n, int verb, int opt)
|
||||
{
|
||||
int ok;
|
||||
switch (verb) {
|
||||
case WILL:
|
||||
ok = opt == O_BINARY || opt == O_ECHO || opt == O_SGA;
|
||||
if (ok != n->remote[opt] || !ok) {
|
||||
n->remote[opt] = (unsigned char)ok;
|
||||
put(n, IAC, ok ? DO : DONT, opt);
|
||||
}
|
||||
break;
|
||||
case WONT:
|
||||
if (n->remote[opt]) {
|
||||
n->remote[opt] = 0;
|
||||
put(n, IAC, DONT, opt);
|
||||
}
|
||||
break;
|
||||
case DO:
|
||||
ok = opt == O_BINARY || opt == O_SGA || opt == O_TTYPE ||
|
||||
opt == O_NAWS;
|
||||
if (ok != n->local[opt] || !ok) {
|
||||
n->local[opt] = (unsigned char)ok;
|
||||
put(n, IAC, ok ? WILL : WONT, opt);
|
||||
}
|
||||
/* (its size, each time it asks) */
|
||||
if (ok && opt == O_NAWS) {
|
||||
n->naws = 1;
|
||||
send_naws(n);
|
||||
}
|
||||
break;
|
||||
case DONT:
|
||||
if (opt == O_NAWS)
|
||||
n->naws = 0;
|
||||
if (n->local[opt]) {
|
||||
n->local[opt] = 0;
|
||||
put(n, IAC, WONT, opt);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* the server's subnegotiation: TERMINAL-TYPE SEND answered with IS */
|
||||
static void subneg(dmc_net *n)
|
||||
{
|
||||
const unsigned char *p = (const unsigned char *)n->sb.p;
|
||||
if (n->sb.n >= 2 && p[0] == O_TTYPE && p[1] == 1) {
|
||||
put(n, IAC, SB, O_TTYPE);
|
||||
buf_addc(&n->out, '\0');
|
||||
buf_adds(&n->out, n->term);
|
||||
buf_addc(&n->out, (char)IAC);
|
||||
buf_addc(&n->out, (char)SE);
|
||||
}
|
||||
}
|
||||
|
||||
static void feed_telnet(dmc_net *n, const unsigned char *p, int len)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
int b = p[i];
|
||||
switch (n->st) {
|
||||
case T_DATA:
|
||||
if (b == IAC)
|
||||
n->st = T_IAC;
|
||||
/* CR NUL is a CR: the NUL goes */
|
||||
else if (!(b == 0 && n->last_cr && !n->remote[O_BINARY]))
|
||||
buf_addc(&n->screen, (char)b);
|
||||
break;
|
||||
case T_IAC:
|
||||
if (b == IAC) {
|
||||
buf_addc(&n->screen, (char)IAC);
|
||||
n->st = T_DATA;
|
||||
} else if (b == WILL || b == WONT || b == DO ||
|
||||
b == DONT) {
|
||||
n->verb = b;
|
||||
n->st = T_OPT;
|
||||
} else if (b == SB) {
|
||||
buf_clear(&n->sb);
|
||||
n->st = T_SB;
|
||||
} else {
|
||||
/* NOP, GA, AYT and the rest: nothing to do */
|
||||
n->st = T_DATA;
|
||||
}
|
||||
break;
|
||||
case T_OPT:
|
||||
option(n, n->verb, b);
|
||||
n->st = T_DATA;
|
||||
break;
|
||||
case T_SB:
|
||||
if (b == IAC)
|
||||
n->st = T_SB_IAC;
|
||||
else if (n->sb.n < 1024)
|
||||
buf_addc(&n->sb, (char)b);
|
||||
break;
|
||||
case T_SB_IAC:
|
||||
if (b == SE) {
|
||||
subneg(n);
|
||||
n->st = T_DATA;
|
||||
} else {
|
||||
if (b == IAC && n->sb.n < 1024)
|
||||
buf_addc(&n->sb, (char)IAC);
|
||||
n->st = T_SB;
|
||||
}
|
||||
break;
|
||||
}
|
||||
n->last_cr = n->st == T_DATA && b == '\r';
|
||||
}
|
||||
}
|
||||
|
||||
DMC_API void dmc_net_feed(dmc_net *n, const char *data, int len)
|
||||
{
|
||||
if (len <= 0)
|
||||
return;
|
||||
if (n->proto == DMC_NET_TELNET) {
|
||||
feed_telnet(n, (const unsigned char *)data, len);
|
||||
return;
|
||||
}
|
||||
if (n->proto == DMC_NET_RLOGIN && !n->ready) {
|
||||
/* the server's NUL: the handshake taken */
|
||||
n->ready = 1;
|
||||
if (data[0] == '\0') {
|
||||
data++;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
buf_add(&n->screen, data, len);
|
||||
}
|
||||
|
||||
DMC_API void dmc_net_urgent(dmc_net *n, int byte)
|
||||
{
|
||||
if (n->proto != DMC_NET_RLOGIN)
|
||||
return;
|
||||
/* TIOCPKT_WINDOW: the server takes window sizes, and wants ours */
|
||||
if (byte & 0x80) {
|
||||
n->want_size = 1;
|
||||
send_window(n);
|
||||
}
|
||||
/* TIOCPKT_FLUSHWRITE: what it sent before is to be dropped */
|
||||
if (byte & 0x02)
|
||||
buf_clear(&n->screen);
|
||||
}
|
||||
|
||||
DMC_API int dmc_net_screen(dmc_net *n, char *buf, int max)
|
||||
{
|
||||
return buf_take(&n->screen, buf, max);
|
||||
}
|
||||
|
||||
DMC_API void dmc_net_type(dmc_net *n, const char *data, int len)
|
||||
{
|
||||
int i;
|
||||
if (n->proto != DMC_NET_TELNET) {
|
||||
buf_add(&n->out, data, len);
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < len; i++) {
|
||||
unsigned char b = (unsigned char)data[i];
|
||||
buf_addc(&n->out, (char)b);
|
||||
if (b == IAC)
|
||||
buf_addc(&n->out, (char)IAC);
|
||||
/* a lone CR is CR NUL, outside binary mode */
|
||||
if (b == '\r' && !n->local[O_BINARY] &&
|
||||
(i + 1 >= len || data[i + 1] != '\n'))
|
||||
buf_addc(&n->out, '\0');
|
||||
}
|
||||
}
|
||||
|
||||
DMC_API int dmc_net_output(dmc_net *n, char *buf, int max)
|
||||
{
|
||||
return buf_take(&n->out, buf, max);
|
||||
}
|
||||
|
||||
DMC_API int dmc_net_break(dmc_net *n)
|
||||
{
|
||||
/* Telnet's BRK; rlogin has none */
|
||||
if (n->proto != DMC_NET_TELNET)
|
||||
return 0;
|
||||
buf_addc(&n->out, (char)IAC);
|
||||
buf_addc(&n->out, (char)BRK);
|
||||
return 1;
|
||||
}
|
||||
|
||||
DMC_API void dmc_net_resize(dmc_net *n, int cols, int rows)
|
||||
{
|
||||
if (cols == n->cols && rows == n->rows)
|
||||
return;
|
||||
n->cols = cols;
|
||||
n->rows = rows;
|
||||
if (n->proto == DMC_NET_TELNET && n->naws)
|
||||
send_naws(n);
|
||||
else if (n->proto == DMC_NET_RLOGIN && n->want_size)
|
||||
send_window(n);
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* path.c - the connection path: the picture at the top of the Settings
|
||||
* page of how a call is made, step by step (the D-Modem app's
|
||||
* ConnectionPath, for the desktop's lines)
|
||||
*
|
||||
* dmodemd's own line: Terminal -> D-Modem -> SIP, USB modem or codec
|
||||
* -> the phone network, SIP addresses, a phone line
|
||||
* a modem that runs: Terminal -> its device, TCP or COM port -> the
|
||||
* modem -> its phone line
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
|
||||
#define STEPS 4
|
||||
|
||||
typedef struct Step {
|
||||
int kind, state;
|
||||
const char *role;
|
||||
char title[32], detail[400];
|
||||
} Step;
|
||||
|
||||
struct dmc_path {
|
||||
/* the settings */
|
||||
int line, anonymous, listen;
|
||||
char where[256], user[128], server[256];
|
||||
/* now */
|
||||
int running, state;
|
||||
char connect[128];
|
||||
/* what they make */
|
||||
Step step[STEPS];
|
||||
char headline[512];
|
||||
};
|
||||
|
||||
static void copy(char *d, size_t n, const char *s)
|
||||
{
|
||||
snprintf(d, n, "%s", s ? s : "");
|
||||
}
|
||||
|
||||
static void step(Step *s, int kind, const char *title, int state,
|
||||
const char *detail)
|
||||
{
|
||||
s->kind = kind;
|
||||
s->state = state;
|
||||
copy(s->title, sizeof(s->title), title);
|
||||
copy(s->detail, sizeof(s->detail), detail);
|
||||
}
|
||||
|
||||
static int rank(int state)
|
||||
{
|
||||
switch (state) {
|
||||
case DMC_STEP_OFF: return 0;
|
||||
case DMC_STEP_SETUP: return 1;
|
||||
case DMC_STEP_WORKING: return 2;
|
||||
case DMC_STEP_READY: return 3;
|
||||
default: return 4;
|
||||
}
|
||||
}
|
||||
|
||||
/* the steps and the headline, from the settings and now */
|
||||
static void make(dmc_path *p)
|
||||
{
|
||||
static const char *const roles[] = { "Program", "Modem", "Line", "Reaches" };
|
||||
static const char *const external[] = { "Program", "Port", "Modem", "Line" };
|
||||
Step *s = p->step;
|
||||
char b[400];
|
||||
int own = p->line == DMC_LINE_SIP || p->line == DMC_LINE_USB ||
|
||||
p->line == DMC_LINE_CODEC;
|
||||
|
||||
step(&s[0], DMC_KIND_TERMINAL, "Terminal", DMC_STEP_READY,
|
||||
"AT commands and calls");
|
||||
if (own) {
|
||||
int st = p->running && p->state == DMC_CLOSED ? DMC_STEP_WORKING :
|
||||
DMC_STEP_READY;
|
||||
step(&s[1], DMC_KIND_SOFTMODEM, "D-Modem", st,
|
||||
st == DMC_STEP_WORKING ? "Starting" :
|
||||
p->running ? "Running" : "dmodemd, the V.92 softmodem");
|
||||
}
|
||||
switch (p->line) {
|
||||
case DMC_LINE_USB:
|
||||
snprintf(b, sizeof(b), "%s", *p->where && strcmp(p->where, "auto") ?
|
||||
p->where : "The first one found");
|
||||
step(&s[2], DMC_KIND_USB, "USB modem", DMC_STEP_READY, b);
|
||||
step(&s[3], DMC_KIND_PHONE_LINE, "Phone line", DMC_STEP_READY,
|
||||
"The USB modem's");
|
||||
break;
|
||||
case DMC_LINE_CODEC:
|
||||
step(&s[2], DMC_KIND_CODEC, "Codec", *p->where ? DMC_STEP_READY :
|
||||
DMC_STEP_SETUP, *p->where ? p->where : "No ALSA device set");
|
||||
step(&s[3], DMC_KIND_PHONE_LINE, "Phone line", DMC_STEP_READY,
|
||||
"The codec's");
|
||||
break;
|
||||
case DMC_LINE_DEVICE:
|
||||
case DMC_LINE_TCP:
|
||||
case DMC_LINE_COM: {
|
||||
int set = *p->where != '\0';
|
||||
int st = !set ? DMC_STEP_SETUP :
|
||||
p->running && p->state == DMC_CLOSED ? DMC_STEP_WORKING :
|
||||
DMC_STEP_READY;
|
||||
const char *title = p->line == DMC_LINE_TCP ? "TCP" :
|
||||
p->line == DMC_LINE_COM ? "COM port" : "Device";
|
||||
step(&s[1], p->line == DMC_LINE_TCP ? DMC_KIND_NETWORK :
|
||||
DMC_KIND_SERIAL, title, st,
|
||||
set ? p->where : "Not set yet");
|
||||
step(&s[2], p->line == DMC_LINE_TCP ? DMC_KIND_SOFTMODEM :
|
||||
DMC_KIND_MODEM, p->line == DMC_LINE_TCP ? "D-Modem" : "Modem",
|
||||
DMC_STEP_READY,
|
||||
p->running && p->state != DMC_CLOSED ? "Running" :
|
||||
"Already running");
|
||||
step(&s[3], DMC_KIND_PHONE_LINE, "Phone line", DMC_STEP_READY,
|
||||
"The modem's own");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if (p->anonymous) {
|
||||
snprintf(b, sizeof(b), "Anonymous: nothing registered%s",
|
||||
p->listen ? ", takes direct calls" : "");
|
||||
step(&s[2], DMC_KIND_SIP, "SIP", DMC_STEP_READY, b);
|
||||
step(&s[3], DMC_KIND_SIP_ADDRESS, "SIP addresses",
|
||||
DMC_STEP_READY, "Dialled directly (sip:user@host)");
|
||||
} else if (!*p->user || !*p->server) {
|
||||
step(&s[2], DMC_KIND_SIP, "SIP", DMC_STEP_SETUP,
|
||||
"No account set up");
|
||||
step(&s[3], DMC_KIND_PHONE_NETWORK, "Phone network",
|
||||
DMC_STEP_SETUP, "Through a SIP provider");
|
||||
} else {
|
||||
snprintf(b, sizeof(b), "%s %s@%s",
|
||||
p->listen ? "Registers as" : "Account",
|
||||
p->user, p->server);
|
||||
step(&s[2], DMC_KIND_SIP, "SIP", DMC_STEP_READY, b);
|
||||
snprintf(b, sizeof(b), "Through %s", p->server);
|
||||
step(&s[3], DMC_KIND_PHONE_NETWORK, "Phone network",
|
||||
DMC_STEP_READY, b);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* a call: each step in use lights; idle, as set up */
|
||||
int now = -1;
|
||||
switch (p->state) {
|
||||
case DMC_ONLINE:
|
||||
case DMC_ONLINE_COMMAND:
|
||||
case DMC_VOICE:
|
||||
case DMC_VOICEMAIL:
|
||||
now = DMC_STEP_ACTIVE;
|
||||
break;
|
||||
case DMC_DIALING:
|
||||
case DMC_ANSWERING:
|
||||
case DMC_RINGING:
|
||||
case DMC_HANGING_UP:
|
||||
now = DMC_STEP_WORKING;
|
||||
break;
|
||||
}
|
||||
int setup = 0;
|
||||
for (int i = 0; i < STEPS; i++) {
|
||||
s[i].role = own ? roles[i] : external[i];
|
||||
if (now >= 0 && s[i].state != DMC_STEP_OFF &&
|
||||
s[i].state != DMC_STEP_SETUP)
|
||||
s[i].state = now;
|
||||
if (s[i].state == DMC_STEP_SETUP && !setup) {
|
||||
setup = 1;
|
||||
char d[400];
|
||||
copy(d, sizeof(d), s[i].detail);
|
||||
if (d[0] >= 'A' && d[0] <= 'Z')
|
||||
d[0] = (char)(d[0] - 'A' + 'a');
|
||||
snprintf(p->headline, sizeof(p->headline),
|
||||
"Not ready: %s (%s)", s[i].title, d);
|
||||
}
|
||||
}
|
||||
if (now == DMC_STEP_ACTIVE && (p->state == DMC_VOICE ||
|
||||
p->state == DMC_VOICEMAIL))
|
||||
copy(p->headline, sizeof(p->headline), "In a voice call");
|
||||
else if (now == DMC_STEP_ACTIVE)
|
||||
snprintf(p->headline, sizeof(p->headline), "Online%s%s",
|
||||
*p->connect ? ": " : "", p->connect);
|
||||
else if (p->state == DMC_RINGING)
|
||||
copy(p->headline, sizeof(p->headline), "Ringing");
|
||||
else if (p->state == DMC_HANGING_UP)
|
||||
copy(p->headline, sizeof(p->headline), "Hanging up");
|
||||
else if (now == DMC_STEP_WORKING)
|
||||
copy(p->headline, sizeof(p->headline), "Connecting");
|
||||
else if (!setup && p->running && p->state == DMC_CLOSED)
|
||||
copy(p->headline, sizeof(p->headline), "Starting the modem");
|
||||
else if (!setup && p->running)
|
||||
copy(p->headline, sizeof(p->headline), "Ready: the modem is running");
|
||||
else if (!setup)
|
||||
copy(p->headline, sizeof(p->headline),
|
||||
"Ready: start the modem to dial");
|
||||
}
|
||||
|
||||
dmc_path *dmc_path_new(void)
|
||||
{
|
||||
dmc_path *p = calloc(1, sizeof(*p));
|
||||
if (p)
|
||||
make(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
void dmc_path_free(dmc_path *p)
|
||||
{
|
||||
free(p);
|
||||
}
|
||||
|
||||
void dmc_path_set_line(dmc_path *p, int line, const char *where,
|
||||
int sip_anonymous, const char *sip_user,
|
||||
const char *sip_server, int listen)
|
||||
{
|
||||
p->line = line;
|
||||
copy(p->where, sizeof(p->where), where);
|
||||
p->anonymous = sip_anonymous != 0;
|
||||
copy(p->user, sizeof(p->user), sip_user);
|
||||
copy(p->server, sizeof(p->server), sip_server);
|
||||
p->listen = listen != 0;
|
||||
make(p);
|
||||
}
|
||||
|
||||
void dmc_path_set_live(dmc_path *p, int running, int state,
|
||||
const char *connect)
|
||||
{
|
||||
p->running = running != 0;
|
||||
p->state = state;
|
||||
copy(p->connect, sizeof(p->connect), connect);
|
||||
make(p);
|
||||
}
|
||||
|
||||
int dmc_path_steps(const dmc_path *p)
|
||||
{
|
||||
(void)p;
|
||||
return STEPS;
|
||||
}
|
||||
|
||||
static const Step *at(const dmc_path *p, int i)
|
||||
{
|
||||
static const Step none = { 0, DMC_STEP_OFF, "", "", "" };
|
||||
return i >= 0 && i < STEPS ? &p->step[i] : &none;
|
||||
}
|
||||
|
||||
int dmc_path_kind(const dmc_path *p, int i) { return at(p, i)->kind; }
|
||||
int dmc_path_state(const dmc_path *p, int i) { return at(p, i)->state; }
|
||||
const char *dmc_path_role(const dmc_path *p, int i) { return at(p, i)->role; }
|
||||
const char *dmc_path_title(const dmc_path *p, int i) { return at(p, i)->title; }
|
||||
const char *dmc_path_detail(const dmc_path *p, int i) { return at(p, i)->detail; }
|
||||
|
||||
int dmc_path_link(const dmc_path *p, int i)
|
||||
{
|
||||
int a = at(p, i)->state, b = at(p, i + 1)->state;
|
||||
return rank(a) <= rank(b) ? a : b;
|
||||
}
|
||||
|
||||
const char *dmc_path_headline(const dmc_path *p)
|
||||
{
|
||||
return p->headline;
|
||||
}
|
||||
|
||||
int dmc_path_needs_setup(const dmc_path *p)
|
||||
{
|
||||
for (int i = 0; i < STEPS; i++)
|
||||
if (p->step[i].state == DMC_STEP_SETUP)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* session.c - the AT conversation with dmodemd (dmodemctl.h)
|
||||
*
|
||||
@@ -15,6 +15,13 @@
|
||||
* for; while a configurator runs, only the OK that ends it (or the
|
||||
* ERROR that refuses it). Results need the modem's verbose result
|
||||
* codes (V1, its default).
|
||||
*
|
||||
* A voice call is AT+FCLASS=8 and AT+VSM=4 (G.711 mu-law), then ATD
|
||||
* (its OK the far end's answer) or AT+VLS=1 (the ringing call
|
||||
* answered), then AT+VTR: after its CONNECT the bytes both ways are
|
||||
* samples, <DLE> doubled, and the modem's <DLE> codes are what it heard
|
||||
* on the line. <DLE>^ from us ends the stream (the modem's <DLE><ETX>
|
||||
* and OK), and ATH puts the modem on hook, in data mode again.
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
@@ -22,6 +29,7 @@
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "util.h"
|
||||
#include "voice.h"
|
||||
|
||||
/* the usual time a command may take, and dialling's or answering's */
|
||||
#define COMMAND_MS 5000
|
||||
@@ -35,6 +43,60 @@
|
||||
/* a line longer than this keeps only its end (a configurator's
|
||||
screens come with no line ends, and its OK after the last one) */
|
||||
#define LINE_MAX 4096
|
||||
/* the most pages of one response that the session pages on */
|
||||
#define PAGES_MAX 8
|
||||
|
||||
/* voice: how far ahead of the line our samples go (60 ms), how many of
|
||||
the far end's are kept (1 s), how many of ours may wait (a third of
|
||||
a second: then they are cut to 100 ms) */
|
||||
#define VOICE_LEAD (DMC_VOICE_RATE * 60 / 1000)
|
||||
#define VOICE_RX_MAX DMC_VOICE_RATE
|
||||
#define VOICE_TX_MAX (DMC_VOICE_RATE / 3)
|
||||
#define VOICE_TX_KEEP (DMC_VOICE_RATE / 10)
|
||||
/* how long the modem may take to end the stream (<DLE><ETX>, OK) */
|
||||
#define VOICE_END_MS 3000
|
||||
/* a DTMF digit's tone and the gap after it, in samples, and its
|
||||
level; the answering machine's beep */
|
||||
#define DTMF_ON (DMC_VOICE_RATE / 10)
|
||||
#define DTMF_OFF (DMC_VOICE_RATE * 6 / 100)
|
||||
#define DTMF_AMP 16000
|
||||
#define BEEP_HZ 1000
|
||||
#define BEEP_LEN (DMC_VOICE_RATE / 2)
|
||||
#define BEEP_AMP 8000
|
||||
/* the answering machine: quieter than this is silence; its usual
|
||||
longest message and silence (s); the shortest message kept (1 s);
|
||||
how much of a busy or dial tone to cut from a message's end (2 s);
|
||||
the silence kept at its end (0.5 s) */
|
||||
#define QUIET_LEVEL 400
|
||||
#define MESSAGE_MAX_S 120
|
||||
#define MESSAGE_SILENCE_S 7
|
||||
#define MESSAGE_MIN (DMC_VOICE_RATE)
|
||||
#define TONE_TRIM (2 * DMC_VOICE_RATE)
|
||||
#define QUIET_KEEP (DMC_VOICE_RATE / 2)
|
||||
|
||||
#define DLE 0x10
|
||||
#define ETX 0x03
|
||||
|
||||
/* the session's own flags on its commands (above dmc_command()'s) */
|
||||
enum {
|
||||
CMD_VSETUP = 0x100, /* AT+FCLASS=8, AT+VSM: voice mode */
|
||||
CMD_VCALL = 0x200, /* ATD or AT+VLS=1: the call */
|
||||
CMD_VTR = 0x400, /* AT+VTR: the stream */
|
||||
CMD_VHANG = 0x800 /* ATH: the call over, data mode */
|
||||
};
|
||||
|
||||
/* what a voice call is for, and where it is */
|
||||
enum { V_NONE, V_LIVE, V_MACHINE };
|
||||
enum {
|
||||
VS_NONE,
|
||||
VS_SETUP, /* the commands before the stream */
|
||||
VS_STREAM, /* samples both ways */
|
||||
VS_ENDING, /* <DLE>^ sent, the modem's <DLE><ETX> awaited */
|
||||
VS_ENDED, /* <DLE><ETX> come, its OK awaited */
|
||||
VS_HANGUP /* ATH */
|
||||
};
|
||||
/* the answering machine's steps */
|
||||
enum { MP_GREETING, MP_BEEP, MP_RECORD };
|
||||
|
||||
struct command {
|
||||
int tag; /* 0: the session's own */
|
||||
@@ -60,6 +122,10 @@ struct dmc_session {
|
||||
struct command *queue, *active;
|
||||
int64_t active_until;
|
||||
int echo_seen;
|
||||
/* the pages of its response that the session paged on (a key sent
|
||||
to "Press any key") */
|
||||
int pages;
|
||||
int prompt; /* the rest of the prompt's line: dropped */
|
||||
dmc_buf response;
|
||||
int next_tag;
|
||||
/* the events, and the one dmc_next_event() last gave */
|
||||
@@ -72,6 +138,22 @@ struct dmc_session {
|
||||
int64_t ring_until;
|
||||
char *connect_text, *caller_number, *caller_name;
|
||||
int connect_rate;
|
||||
int rings;
|
||||
/* a voice call */
|
||||
int voice, vstep;
|
||||
int vdle, vpacket; /* a <DLE> came; inside a <DLE>X packet */
|
||||
int vhang; /* to be ended once its stream starts */
|
||||
int64_t vstart, vsent, vend_at;
|
||||
dmc_ring rx, tx;
|
||||
char dtmf[32];
|
||||
int dtmf_n, tone_gap;
|
||||
dmc_tone tone, beep;
|
||||
/* the answering machine */
|
||||
int16_t *greeting;
|
||||
int greeting_n, max_s, silence_s, auto_rings;
|
||||
int mphase, gpos, mdone;
|
||||
int16_t *rec, *msg;
|
||||
int rec_n, rec_cap, quiet, msg_n;
|
||||
};
|
||||
|
||||
static void command_free(struct command *c)
|
||||
@@ -112,10 +194,18 @@ static void push_event(dmc_session *s, int type, int tag, int ok,
|
||||
s->events_tail = e;
|
||||
}
|
||||
|
||||
static void machine_done(dmc_session *s, int trim);
|
||||
static void voice_reset(dmc_session *s);
|
||||
|
||||
static void set_state(dmc_session *s, int state)
|
||||
{
|
||||
if (s->state == state)
|
||||
return;
|
||||
if (state == DMC_IDLE || state == DMC_CLOSED) {
|
||||
if (s->voice == V_MACHINE && !s->mdone)
|
||||
machine_done(s, 0);
|
||||
voice_reset(s);
|
||||
}
|
||||
s->state = state;
|
||||
if (state != DMC_RINGING) {
|
||||
s->ring_until = 0;
|
||||
@@ -127,6 +217,23 @@ static void set_state(dmc_session *s, int state)
|
||||
push_event(s, DMC_EV_STATE, 0, 1, "", "");
|
||||
}
|
||||
|
||||
/* the voice call's state: the far end's samples, ours, the tones */
|
||||
static void voice_reset(dmc_session *s)
|
||||
{
|
||||
s->voice = V_NONE;
|
||||
s->vstep = VS_NONE;
|
||||
s->vdle = s->vpacket = s->vhang = 0;
|
||||
ring_clear(&s->rx);
|
||||
ring_clear(&s->tx);
|
||||
s->dtmf_n = s->tone_gap = 0;
|
||||
s->tone.left = s->beep.left = 0;
|
||||
free(s->rec);
|
||||
s->rec = NULL;
|
||||
s->rec_n = s->rec_cap = s->quiet = 0;
|
||||
s->mphase = MP_GREETING;
|
||||
s->gpos = s->mdone = 0;
|
||||
}
|
||||
|
||||
static void clear_queue(dmc_session *s)
|
||||
{
|
||||
while (s->queue) {
|
||||
@@ -145,6 +252,8 @@ dmc_session *dmc_session_new(void)
|
||||
if (!s)
|
||||
abort();
|
||||
s->next_tag = 1;
|
||||
ring_init(&s->rx, VOICE_RX_MAX);
|
||||
ring_init(&s->tx, DMC_VOICE_RATE);
|
||||
s->connect_text = dup_str("");
|
||||
s->caller_number = dup_str("");
|
||||
s->caller_name = dup_str("");
|
||||
@@ -170,6 +279,11 @@ void dmc_session_free(dmc_session *s)
|
||||
free(s->connect_text);
|
||||
free(s->caller_number);
|
||||
free(s->caller_name);
|
||||
free(s->rec);
|
||||
free(s->msg);
|
||||
free(s->greeting);
|
||||
ring_free(&s->rx);
|
||||
ring_free(&s->tx);
|
||||
free(s);
|
||||
}
|
||||
|
||||
@@ -181,6 +295,7 @@ void dmc_open(dmc_session *s, int64_t now_ms)
|
||||
buf_clear(&s->typed);
|
||||
s->now = now_ms;
|
||||
s->hang = HANG_NONE;
|
||||
voice_reset(s);
|
||||
set_state(s, DMC_IDLE);
|
||||
}
|
||||
|
||||
@@ -260,16 +375,24 @@ static void pump(dmc_session *s)
|
||||
return;
|
||||
if (s->hang == HANG_GUARD || s->hang == HANG_ESCAPED)
|
||||
return;
|
||||
/* (the stream's bytes are samples, not commands) */
|
||||
if (s->vstep == VS_STREAM || s->vstep == VS_ENDING)
|
||||
return;
|
||||
struct command *c = s->queue;
|
||||
s->queue = c->next;
|
||||
c->next = NULL;
|
||||
s->active = c;
|
||||
s->active_until = s->now + c->timeout_ms;
|
||||
s->echo_seen = 0;
|
||||
s->pages = 0;
|
||||
s->prompt = 0;
|
||||
buf_clear(&s->response);
|
||||
buf_adds(&s->out, c->cmd);
|
||||
buf_addc(&s->out, '\r');
|
||||
command_sent(s, c->cmd);
|
||||
/* (AT+VLS=1 answers the ringing call) */
|
||||
if ((c->flags & CMD_VCALL) && s->state != DMC_DIALING)
|
||||
set_state(s, DMC_ANSWERING);
|
||||
}
|
||||
|
||||
static int enqueue(dmc_session *s, const char *cmd, int flags,
|
||||
@@ -316,7 +439,8 @@ static int enqueue(dmc_session *s, const char *cmd, int flags,
|
||||
int dmc_command(dmc_session *s, const char *cmd, int flags, int timeout_ms)
|
||||
{
|
||||
if (!cmd || s->state == DMC_CLOSED || s->state == DMC_ONLINE ||
|
||||
s->state == DMC_CONFIGURATOR)
|
||||
s->state == DMC_CONFIGURATOR || s->state == DMC_VOICE ||
|
||||
s->state == DMC_VOICEMAIL || s->voice)
|
||||
return -1;
|
||||
int tag = s->next_tag++;
|
||||
if (s->next_tag <= 0)
|
||||
@@ -341,8 +465,17 @@ int dmc_answer(dmc_session *s)
|
||||
return dmc_command(s, "ATA", 0, 0);
|
||||
}
|
||||
|
||||
static void voice_end(dmc_session *s);
|
||||
|
||||
void dmc_hangup(dmc_session *s)
|
||||
{
|
||||
if (s->voice && s->state != DMC_DIALING) {
|
||||
if (s->voice == V_MACHINE && !s->mdone &&
|
||||
s->mphase == MP_RECORD)
|
||||
machine_done(s, 0);
|
||||
voice_end(s);
|
||||
return;
|
||||
}
|
||||
switch (s->state) {
|
||||
case DMC_ONLINE:
|
||||
if (s->hang == HANG_NONE) {
|
||||
@@ -435,11 +568,18 @@ static void unsolicited(dmc_session *s, const char *l, int kind)
|
||||
if (s->state == DMC_IDLE) {
|
||||
set_str(&s->caller_number, "");
|
||||
set_str(&s->caller_name, "");
|
||||
s->rings = 0;
|
||||
}
|
||||
set_state(s, DMC_RINGING);
|
||||
s->ring_until = s->now + RING_GAP_MS;
|
||||
s->rings++;
|
||||
}
|
||||
push_event(s, DMC_EV_RESULT, 0, 1, "", l);
|
||||
/* the answering machine's turn */
|
||||
if (s->state == DMC_RINGING && s->auto_rings > 0 &&
|
||||
s->rings >= s->auto_rings && !s->voice && !s->active &&
|
||||
!s->queue)
|
||||
dmc_voicemail_answer(s);
|
||||
return;
|
||||
}
|
||||
if (caller_line(s, l))
|
||||
@@ -470,11 +610,52 @@ static void unsolicited(dmc_session *s, const char *l, int kind)
|
||||
"", l);
|
||||
}
|
||||
|
||||
static void voice_fail(dmc_session *s, const char *result);
|
||||
static void stream_start(dmc_session *s);
|
||||
|
||||
/* a voice command's final result: 1 if it was one */
|
||||
static int voice_finish(dmc_session *s, struct command *c, const char *result,
|
||||
int kind)
|
||||
{
|
||||
int ok;
|
||||
if (!(c->flags & (CMD_VSETUP | CMD_VCALL | CMD_VTR | CMD_VHANG)))
|
||||
return 0;
|
||||
ok = kind == R_OK || ((c->flags & CMD_VTR) && kind == R_CONNECT);
|
||||
if (c->tag)
|
||||
push_event(s, DMC_EV_RESPONSE, c->tag, ok,
|
||||
buf_str(&s->response), result);
|
||||
buf_clear(&s->response);
|
||||
if (c->flags & CMD_VHANG) {
|
||||
command_free(c);
|
||||
set_state(s, DMC_IDLE);
|
||||
} else if (!s->voice) {
|
||||
/* (a call given up while its command ran) */
|
||||
command_free(c);
|
||||
} else if (!ok) {
|
||||
command_free(c);
|
||||
voice_fail(s, result);
|
||||
} else if (c->flags & CMD_VCALL) {
|
||||
command_free(c);
|
||||
set_state(s, s->voice == V_MACHINE ? DMC_VOICEMAIL : DMC_VOICE);
|
||||
enqueue(s, "AT+VTR", DMC_QUIET | CMD_VTR, COMMAND_MS, 0, 1);
|
||||
return 1;
|
||||
} else if (c->flags & CMD_VTR) {
|
||||
command_free(c);
|
||||
stream_start(s);
|
||||
} else {
|
||||
command_free(c);
|
||||
}
|
||||
pump(s);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* the active command's final result */
|
||||
static void finish(dmc_session *s, const char *result, int kind)
|
||||
{
|
||||
struct command *c = s->active;
|
||||
s->active = NULL;
|
||||
if (voice_finish(s, c, result, kind))
|
||||
return;
|
||||
char first = first_command(c->cmd);
|
||||
switch (kind) {
|
||||
case R_CONNECT:
|
||||
@@ -509,6 +690,14 @@ static void finish(dmc_session *s, const char *result, int kind)
|
||||
/* a whole line from the modem (no CR or LF) */
|
||||
static void line_in(dmc_session *s, const char *l)
|
||||
{
|
||||
/* the stream over: its OK, then ATH */
|
||||
if (s->vstep == VS_ENDED) {
|
||||
if (!strcmp(l, "OK") || !strcmp(l, "ERROR")) {
|
||||
s->vstep = VS_HANGUP;
|
||||
enqueue(s, "ATH", DMC_QUIET | CMD_VHANG, 0, 0, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* online: the call's data, but for its end */
|
||||
if (s->state == DMC_ONLINE ||
|
||||
(s->state == DMC_HANGING_UP && s->hang == HANG_GUARD)) {
|
||||
@@ -587,25 +776,61 @@ static void line_in(dmc_session *s, const char *l)
|
||||
unsolicited(s, l, kind);
|
||||
}
|
||||
|
||||
static void voice_byte(dmc_session *s, unsigned char c);
|
||||
|
||||
/* whether l is a pager's prompt: a modem that shows a long response in
|
||||
pages (ATI11 on the Agere modems, the MultiTech MT9234 among them)
|
||||
stops after each page at "Press any key to continue; ESC to quit" */
|
||||
static int pager_prompt(const char *l)
|
||||
{
|
||||
for (; *l; l++)
|
||||
if (starts_ci(l, "any key"))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dmc_feed(dmc_session *s, const char *data, int n)
|
||||
{
|
||||
if (s->state == DMC_CLOSED)
|
||||
return;
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = data[i];
|
||||
if (s->vstep == VS_STREAM || s->vstep == VS_ENDING) {
|
||||
voice_byte(s, (unsigned char)c);
|
||||
continue;
|
||||
}
|
||||
int quiet = s->active && (s->active->flags & DMC_QUIET);
|
||||
if (!quiet)
|
||||
buf_addc(&s->term, c);
|
||||
if (c == '\n' && s->prompt) {
|
||||
s->prompt = 0;
|
||||
continue;
|
||||
}
|
||||
if (c == '\n') {
|
||||
line_in(s, buf_str(&s->line));
|
||||
buf_clear(&s->line);
|
||||
continue;
|
||||
}
|
||||
if (c == '\r')
|
||||
if (c == '\r') {
|
||||
s->prompt = 0;
|
||||
continue;
|
||||
}
|
||||
if (s->prompt)
|
||||
continue;
|
||||
if (s->line.n >= LINE_MAX)
|
||||
buf_drop(&s->line, s->line.n - LINE_MAX / 2);
|
||||
buf_addc(&s->line, c);
|
||||
/* a command's response in pages: the next page asked for
|
||||
(a space), the prompt kept out of the response */
|
||||
if (s->active && (s->state == DMC_IDLE ||
|
||||
s->state == DMC_ONLINE_COMMAND) &&
|
||||
s->pages < PAGES_MAX && pager_prompt(buf_str(&s->line))) {
|
||||
buf_addc(&s->out, ' ');
|
||||
buf_clear(&s->line);
|
||||
s->prompt = 1;
|
||||
s->pages++;
|
||||
s->active_until = s->now + s->active->timeout_ms;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -613,6 +838,9 @@ void dmc_type(dmc_session *s, const char *data, int n)
|
||||
{
|
||||
if (s->state == DMC_CLOSED)
|
||||
return;
|
||||
/* (nothing typed gets into a voice call's samples) */
|
||||
if (s->voice)
|
||||
return;
|
||||
buf_add(&s->out, data, n);
|
||||
if (s->state == DMC_ONLINE) {
|
||||
/* the escape typed by hand: its OK is command mode */
|
||||
@@ -637,6 +865,8 @@ void dmc_type(dmc_session *s, const char *data, int n)
|
||||
}
|
||||
}
|
||||
|
||||
static void voice_send(dmc_session *s);
|
||||
|
||||
int dmc_poll(dmc_session *s, int64_t now_ms)
|
||||
{
|
||||
s->now = now_ms;
|
||||
@@ -654,8 +884,31 @@ int dmc_poll(dmc_session *s, int64_t now_ms)
|
||||
}
|
||||
if (s->hang != HANG_NONE)
|
||||
next = s->hang_at - now_ms;
|
||||
if (s->vstep == VS_STREAM) {
|
||||
voice_send(s);
|
||||
next = 20;
|
||||
} else if ((s->vstep == VS_ENDING || s->vstep == VS_ENDED) &&
|
||||
now_ms >= s->vend_at) {
|
||||
/* no end from the modem: on hook all the same */
|
||||
if (s->vstep == VS_ENDING) {
|
||||
buf_addc(&s->out, DLE);
|
||||
buf_addc(&s->out, '^');
|
||||
}
|
||||
s->vstep = VS_HANGUP;
|
||||
enqueue(s, "ATH", DMC_QUIET | CMD_VHANG, 0, 0, 1);
|
||||
} else if (s->vstep == VS_ENDING || s->vstep == VS_ENDED) {
|
||||
int64_t d = s->vend_at - now_ms;
|
||||
if (next < 0 || d < next)
|
||||
next = d;
|
||||
}
|
||||
if (s->state == DMC_RINGING && s->ring_until && now_ms >= s->ring_until)
|
||||
set_state(s, DMC_IDLE);
|
||||
/* (the answering machine's turn, if a command was in the way at
|
||||
the RING) */
|
||||
if (s->state == DMC_RINGING && s->auto_rings > 0 &&
|
||||
s->rings >= s->auto_rings && !s->voice && !s->active &&
|
||||
!s->queue)
|
||||
dmc_voicemail_answer(s);
|
||||
if (s->state == DMC_RINGING && s->ring_until) {
|
||||
int64_t d = s->ring_until - now_ms;
|
||||
if (next < 0 || d < next)
|
||||
@@ -664,16 +917,23 @@ int dmc_poll(dmc_session *s, int64_t now_ms)
|
||||
if (s->active) {
|
||||
if (now_ms >= s->active_until) {
|
||||
struct command *c = s->active;
|
||||
int voice = c->flags & (CMD_VSETUP | CMD_VCALL |
|
||||
CMD_VTR | CMD_VHANG);
|
||||
s->active = NULL;
|
||||
if (s->state == DMC_DIALING || s->state == DMC_ANSWERING) {
|
||||
buf_addc(&s->out, '\r');
|
||||
set_state(s, DMC_IDLE);
|
||||
if (!voice)
|
||||
set_state(s, DMC_IDLE);
|
||||
}
|
||||
if (c->tag)
|
||||
push_event(s, DMC_EV_RESPONSE, c->tag, 0,
|
||||
buf_str(&s->response), "timeout");
|
||||
command_free(c);
|
||||
buf_clear(&s->response);
|
||||
if (voice & CMD_VHANG)
|
||||
set_state(s, DMC_IDLE);
|
||||
else if (voice && s->voice)
|
||||
voice_fail(s, "timeout");
|
||||
} else {
|
||||
int64_t d = s->active_until - now_ms;
|
||||
if (next < 0 || d < next)
|
||||
@@ -688,11 +948,23 @@ int dmc_poll(dmc_session *s, int64_t now_ms)
|
||||
return (int)next;
|
||||
}
|
||||
|
||||
void dmc_send(dmc_session *s, const char *data, int n)
|
||||
{
|
||||
if (s->state == DMC_CLOSED || s->voice || n <= 0)
|
||||
return;
|
||||
buf_add(&s->out, data, n);
|
||||
}
|
||||
|
||||
int dmc_output(dmc_session *s, char *buf, int max)
|
||||
{
|
||||
return buf_take(&s->out, buf, max);
|
||||
}
|
||||
|
||||
int dmc_output_waiting(const dmc_session *s)
|
||||
{
|
||||
return s->out.n;
|
||||
}
|
||||
|
||||
int dmc_terminal(dmc_session *s, char *buf, int max)
|
||||
{
|
||||
return buf_take(&s->term, buf, max);
|
||||
@@ -733,6 +1005,8 @@ const char *dmc_state_name(int state)
|
||||
case DMC_ONLINE_COMMAND: return "Online (command mode)";
|
||||
case DMC_HANGING_UP: return "Hanging up";
|
||||
case DMC_CONFIGURATOR: return "Configurator";
|
||||
case DMC_VOICE: return "Voice call";
|
||||
case DMC_VOICEMAIL: return "Taking a message";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -761,3 +1035,386 @@ int dmc_version(void)
|
||||
{
|
||||
return DMC_VERSION;
|
||||
}
|
||||
|
||||
/*
|
||||
* Voice calls
|
||||
*/
|
||||
|
||||
/* the voice call given up (a command of it failed): its commands still
|
||||
queued answered with result, the modem on hook in data mode */
|
||||
static void voice_fail(dmc_session *s, const char *result)
|
||||
{
|
||||
struct command **q = &s->queue;
|
||||
while (*q) {
|
||||
struct command *c = *q;
|
||||
if (!(c->flags & (CMD_VSETUP | CMD_VCALL | CMD_VTR))) {
|
||||
q = &c->next;
|
||||
continue;
|
||||
}
|
||||
*q = c->next;
|
||||
if (c->tag)
|
||||
push_event(s, DMC_EV_RESPONSE, c->tag, 0, "", result);
|
||||
command_free(c);
|
||||
}
|
||||
if (s->state == DMC_IDLE) {
|
||||
if (s->voice == V_MACHINE && !s->mdone)
|
||||
machine_done(s, 0);
|
||||
voice_reset(s);
|
||||
} else {
|
||||
set_state(s, DMC_IDLE);
|
||||
}
|
||||
enqueue(s, "ATH", DMC_QUIET | CMD_VHANG, 0, 0, 1);
|
||||
}
|
||||
|
||||
/* AT+VTR's CONNECT: the samples flow */
|
||||
static void stream_start(dmc_session *s)
|
||||
{
|
||||
s->vstep = VS_STREAM;
|
||||
s->vstart = s->now;
|
||||
s->vsent = 0;
|
||||
s->vdle = s->vpacket = 0;
|
||||
ring_clear(&s->rx);
|
||||
ring_clear(&s->tx);
|
||||
s->mphase = MP_GREETING;
|
||||
s->gpos = 0;
|
||||
if (s->vhang)
|
||||
voice_end(s);
|
||||
}
|
||||
|
||||
/* the call ended from here: the stream (<DLE>^), or a call still being
|
||||
set up once its stream starts */
|
||||
static void voice_end(dmc_session *s)
|
||||
{
|
||||
switch (s->vstep) {
|
||||
case VS_SETUP:
|
||||
s->vhang = 1;
|
||||
break;
|
||||
case VS_STREAM:
|
||||
buf_addc(&s->out, DLE);
|
||||
buf_addc(&s->out, '^');
|
||||
s->vstep = VS_ENDING;
|
||||
s->vend_at = s->now + VOICE_END_MS;
|
||||
set_state(s, DMC_HANGING_UP);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* the answering machine's message over: trim samples of its end cut
|
||||
(a busy tone's), then its quiet end but for QUIET_KEEP; the
|
||||
DMC_EV_MESSAGE */
|
||||
static void machine_done(dmc_session *s, int trim)
|
||||
{
|
||||
int n = s->rec_n - trim, ok;
|
||||
if (s->mdone)
|
||||
return;
|
||||
s->mdone = 1;
|
||||
if (s->mphase == MP_RECORD && s->quiet > QUIET_KEEP) {
|
||||
int q = s->quiet - QUIET_KEEP;
|
||||
if (trim < q)
|
||||
n = s->rec_n - q;
|
||||
}
|
||||
ok = s->mphase == MP_RECORD && n >= MESSAGE_MIN;
|
||||
free(s->msg);
|
||||
s->msg = NULL;
|
||||
s->msg_n = 0;
|
||||
if (ok) {
|
||||
s->msg = s->rec;
|
||||
s->msg_n = n;
|
||||
s->rec = NULL;
|
||||
s->rec_n = s->rec_cap = 0;
|
||||
}
|
||||
push_event(s, DMC_EV_MESSAGE, 0, ok, s->caller_number, "");
|
||||
}
|
||||
|
||||
/* a sample of the caller's for the message: the message over when it is
|
||||
long enough, or the caller silent long enough */
|
||||
static void record(dmc_session *s, int16_t x)
|
||||
{
|
||||
int max = (s->max_s > 0 ? s->max_s : MESSAGE_MAX_S) * DMC_VOICE_RATE;
|
||||
int quiet = (s->silence_s > 0 ? s->silence_s : MESSAGE_SILENCE_S) *
|
||||
DMC_VOICE_RATE;
|
||||
if (s->mdone)
|
||||
return;
|
||||
if (s->rec_n == s->rec_cap) {
|
||||
int cap = s->rec_cap ? s->rec_cap * 2 : DMC_VOICE_RATE * 8;
|
||||
int16_t *p;
|
||||
if (cap > max)
|
||||
cap = max;
|
||||
p = realloc(s->rec, (size_t)cap * sizeof(int16_t));
|
||||
if (!p)
|
||||
abort();
|
||||
s->rec = p;
|
||||
s->rec_cap = cap;
|
||||
}
|
||||
s->rec[s->rec_n++] = x;
|
||||
if (x > -QUIET_LEVEL && x < QUIET_LEVEL)
|
||||
s->quiet++;
|
||||
else
|
||||
s->quiet = 0;
|
||||
if (s->rec_n >= max || s->quiet >= quiet) {
|
||||
machine_done(s, 0);
|
||||
voice_end(s);
|
||||
}
|
||||
}
|
||||
|
||||
/* a sample of the far end's */
|
||||
static void voice_sample(dmc_session *s, unsigned char u)
|
||||
{
|
||||
int16_t x = ulaw_decode(u);
|
||||
ring_put(&s->rx, &x, 1);
|
||||
if (s->voice == V_MACHINE && s->mphase == MP_RECORD)
|
||||
record(s, x);
|
||||
}
|
||||
|
||||
/* a <DLE> code from the modem: what it heard on the line */
|
||||
static void voice_event(dmc_session *s, unsigned char c)
|
||||
{
|
||||
const char *name;
|
||||
char code[2];
|
||||
if ((c >= '0' && c <= '9') || c == '*' || c == '#' ||
|
||||
(c >= 'A' && c <= 'D'))
|
||||
name = "digit";
|
||||
else switch (c) {
|
||||
case 'l': name = "hangup"; break;
|
||||
case 'b': name = "busy"; break;
|
||||
case 'd': name = "dial tone"; break;
|
||||
case 'r': name = "ringing"; break;
|
||||
case 'a': case 'f': name = "answer tone"; break;
|
||||
case 'c': name = "fax"; break;
|
||||
case 'e': name = "data"; break;
|
||||
case 's': name = "silence"; break;
|
||||
case 'q': name = "quiet"; break;
|
||||
default: return;
|
||||
}
|
||||
code[0] = (char)c;
|
||||
code[1] = '\0';
|
||||
push_event(s, DMC_EV_VOICE, 0, 1, code, name);
|
||||
if (c == 'l' || c == 'b' || c == 'd') {
|
||||
/* the far end has gone (a busy or dial tone: the
|
||||
exchange's word for it) */
|
||||
if (s->voice == V_MACHINE && s->mphase == MP_RECORD)
|
||||
machine_done(s, c == 'l' ? 0 : TONE_TRIM);
|
||||
voice_end(s);
|
||||
}
|
||||
}
|
||||
|
||||
/* a byte of the stream from the modem */
|
||||
static void voice_byte(dmc_session *s, unsigned char c)
|
||||
{
|
||||
if (s->vdle) {
|
||||
s->vdle = 0;
|
||||
switch (c) {
|
||||
case DLE:
|
||||
if (!s->vpacket)
|
||||
voice_sample(s, c);
|
||||
break;
|
||||
case ETX:
|
||||
/* the stream's end: its OK to come */
|
||||
s->vstep = VS_ENDED;
|
||||
s->vend_at = s->now + VOICE_END_MS;
|
||||
if (s->state != DMC_HANGING_UP)
|
||||
set_state(s, DMC_HANGING_UP);
|
||||
buf_clear(&s->line);
|
||||
break;
|
||||
case 'X':
|
||||
s->vpacket = 1;
|
||||
break;
|
||||
case '.':
|
||||
s->vpacket = 0;
|
||||
break;
|
||||
default:
|
||||
voice_event(s, c);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (c == DLE) {
|
||||
s->vdle = 1;
|
||||
return;
|
||||
}
|
||||
if (!s->vpacket)
|
||||
voice_sample(s, c);
|
||||
}
|
||||
|
||||
/* our next sample: the answering machine's greeting and beep, or the
|
||||
microphone's; the DTMF digits over it */
|
||||
static int next_sample(dmc_session *s)
|
||||
{
|
||||
int x = 0;
|
||||
int16_t m;
|
||||
if (s->voice == V_MACHINE) {
|
||||
if (s->mphase == MP_GREETING) {
|
||||
if (s->gpos < s->greeting_n) {
|
||||
x = s->greeting[s->gpos++];
|
||||
} else {
|
||||
s->mphase = MP_BEEP;
|
||||
tone_start(&s->beep, BEEP_HZ, 0, BEEP_LEN,
|
||||
BEEP_AMP);
|
||||
}
|
||||
}
|
||||
if (s->mphase == MP_BEEP) {
|
||||
if (s->beep.left > 0) {
|
||||
x = tone_next(&s->beep);
|
||||
} else {
|
||||
s->mphase = MP_RECORD;
|
||||
s->rec_n = 0;
|
||||
s->quiet = 0;
|
||||
}
|
||||
}
|
||||
} else if (ring_get(&s->tx, &m, 1)) {
|
||||
x = m;
|
||||
}
|
||||
if (s->tone.left > 0) {
|
||||
x += tone_next(&s->tone);
|
||||
} else if (s->tone_gap > 0) {
|
||||
s->tone_gap--;
|
||||
} else if (s->dtmf_n) {
|
||||
double lo, hi;
|
||||
if (!dtmf_freqs(s->dtmf[0], &lo, &hi))
|
||||
tone_start(&s->tone, lo, hi, DTMF_ON, DTMF_AMP);
|
||||
s->tone_gap = DTMF_OFF;
|
||||
memmove(s->dtmf, s->dtmf + 1, (size_t)--s->dtmf_n);
|
||||
}
|
||||
return x > 32767 ? 32767 : x < -32768 ? -32768 : x;
|
||||
}
|
||||
|
||||
/* our samples to the modem, as many as the time says, a little
|
||||
ahead */
|
||||
static void voice_send(dmc_session *s)
|
||||
{
|
||||
int64_t due = (s->now - s->vstart) * DMC_VOICE_RATE / 1000 +
|
||||
VOICE_LEAD;
|
||||
/* (a second behind: the app stalled, and the line went on) */
|
||||
if (due - s->vsent > DMC_VOICE_RATE)
|
||||
s->vsent = due - VOICE_LEAD;
|
||||
while (s->vsent < due && s->vstep == VS_STREAM) {
|
||||
unsigned char u = ulaw_encode((int16_t)next_sample(s));
|
||||
buf_addc(&s->out, (char)u);
|
||||
if (u == DLE)
|
||||
buf_addc(&s->out, (char)DLE);
|
||||
s->vsent++;
|
||||
}
|
||||
}
|
||||
|
||||
/* the voice call's start: voice mode, mu-law, then the call's command
|
||||
(with the tag) */
|
||||
static int voice_begin(dmc_session *s, int voice, const char *cmd,
|
||||
int timeout_ms)
|
||||
{
|
||||
int tag = s->next_tag++;
|
||||
if (s->next_tag <= 0)
|
||||
s->next_tag = 1;
|
||||
s->voice = voice;
|
||||
s->vstep = VS_SETUP;
|
||||
s->vhang = 0;
|
||||
s->mdone = 0;
|
||||
enqueue(s, "AT+FCLASS=8", DMC_QUIET | CMD_VSETUP, 0, 0, 0);
|
||||
enqueue(s, "AT+VSM=4", DMC_QUIET | CMD_VSETUP, 0, 0, 0);
|
||||
return enqueue(s, cmd, CMD_VCALL, timeout_ms, tag, 0);
|
||||
}
|
||||
|
||||
int dmc_voice_dial(dmc_session *s, const char *number)
|
||||
{
|
||||
dmc_buf b = { 0 };
|
||||
int tag;
|
||||
if (s->state != DMC_IDLE || s->voice)
|
||||
return -1;
|
||||
buf_adds(&b, "ATD");
|
||||
for (const char *p = number ? number : ""; *p; p++)
|
||||
if (*p != '\r' && *p != '\n' && *p != ';')
|
||||
buf_addc(&b, *p);
|
||||
tag = voice_begin(s, V_LIVE, buf_str(&b), CALL_MS);
|
||||
buf_free(&b);
|
||||
return tag;
|
||||
}
|
||||
|
||||
static int answer_as(dmc_session *s, int voice)
|
||||
{
|
||||
if (s->state != DMC_RINGING || s->voice)
|
||||
return -1;
|
||||
return voice_begin(s, voice, "AT+VLS=1", 30000);
|
||||
}
|
||||
|
||||
int dmc_voice_answer(dmc_session *s)
|
||||
{
|
||||
return answer_as(s, V_LIVE);
|
||||
}
|
||||
|
||||
int dmc_voicemail_answer(dmc_session *s)
|
||||
{
|
||||
return answer_as(s, V_MACHINE);
|
||||
}
|
||||
|
||||
int dmc_voice_read(dmc_session *s, int16_t *pcm, int max)
|
||||
{
|
||||
return max > 0 ? ring_get(&s->rx, pcm, max) : 0;
|
||||
}
|
||||
|
||||
void dmc_voice_write(dmc_session *s, const int16_t *pcm, int n)
|
||||
{
|
||||
if (s->vstep != VS_STREAM || n <= 0)
|
||||
return;
|
||||
ring_put(&s->tx, pcm, n);
|
||||
if (s->tx.n > VOICE_TX_MAX)
|
||||
ring_keep(&s->tx, VOICE_TX_KEEP);
|
||||
}
|
||||
|
||||
int dmc_voice_dtmf(dmc_session *s, char digit)
|
||||
{
|
||||
double lo, hi;
|
||||
if (!s->voice || s->dtmf_n >= (int)sizeof(s->dtmf) ||
|
||||
dtmf_freqs(digit, &lo, &hi))
|
||||
return -1;
|
||||
s->dtmf[s->dtmf_n++] = digit;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dmc_voicemail_set(dmc_session *s, const int16_t *greeting, int n,
|
||||
int max_s, int silence_s)
|
||||
{
|
||||
free(s->greeting);
|
||||
s->greeting = NULL;
|
||||
s->greeting_n = 0;
|
||||
if (greeting && n > 0) {
|
||||
s->greeting = malloc((size_t)n * sizeof(int16_t));
|
||||
if (!s->greeting)
|
||||
abort();
|
||||
memcpy(s->greeting, greeting, (size_t)n * sizeof(int16_t));
|
||||
s->greeting_n = n;
|
||||
}
|
||||
s->max_s = max_s;
|
||||
s->silence_s = silence_s;
|
||||
}
|
||||
|
||||
void dmc_voicemail_auto(dmc_session *s, int rings)
|
||||
{
|
||||
s->auto_rings = rings > 0 ? rings : 0;
|
||||
}
|
||||
|
||||
int dmc_voice_pickup(dmc_session *s)
|
||||
{
|
||||
if (s->voice != V_MACHINE || s->mdone)
|
||||
return -1;
|
||||
s->voice = V_LIVE;
|
||||
s->mdone = 1;
|
||||
s->beep.left = 0;
|
||||
free(s->rec);
|
||||
s->rec = NULL;
|
||||
s->rec_n = s->rec_cap = 0;
|
||||
if (s->state == DMC_VOICEMAIL)
|
||||
set_state(s, DMC_VOICE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dmc_voice_message(const dmc_session *s, const int16_t **pcm)
|
||||
{
|
||||
*pcm = s->msg;
|
||||
return s->msg_n;
|
||||
}
|
||||
|
||||
int dmc_ring_count(const dmc_session *s)
|
||||
{
|
||||
return s->state == DMC_RINGING ? s->rings : 0;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* util.c - growable byte buffers and string helpers
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* util.h - growable byte buffers and string helpers shared by the
|
||||
* library's parts
|
||||
|
||||
@@ -0,0 +1,669 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* voice.c - G.711, rings of samples, tones, and WAV files (dmodemctl.h)
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "voice.h"
|
||||
|
||||
/*
|
||||
* G.711
|
||||
*/
|
||||
|
||||
unsigned char ulaw_encode(int16_t x)
|
||||
{
|
||||
int v = x, sign = 0, exp, mant;
|
||||
if (v < 0) {
|
||||
v = -v;
|
||||
sign = 0x80;
|
||||
}
|
||||
v += 0x84;
|
||||
if (v > 0x7fff)
|
||||
v = 0x7fff;
|
||||
for (exp = 7; exp > 0 && !(v & (0x4000 >> (7 - exp))); exp--)
|
||||
;
|
||||
mant = (v >> (exp + 3)) & 0x0f;
|
||||
return (unsigned char)~(sign | (exp << 4) | mant);
|
||||
}
|
||||
|
||||
int16_t ulaw_decode(unsigned char u)
|
||||
{
|
||||
int v, exp, mant;
|
||||
u = (unsigned char)~u;
|
||||
exp = (u >> 4) & 7;
|
||||
mant = u & 0x0f;
|
||||
v = ((mant << 3) + 0x84) << exp;
|
||||
v -= 0x84;
|
||||
return (int16_t)((u & 0x80) ? -v : v);
|
||||
}
|
||||
|
||||
int16_t alaw_decode(unsigned char a)
|
||||
{
|
||||
int v, exp, mant;
|
||||
a ^= 0x55;
|
||||
exp = (a >> 4) & 7;
|
||||
mant = a & 0x0f;
|
||||
v = exp ? ((mant << 4) + 0x108) << (exp - 1) : (mant << 4) + 8;
|
||||
return (int16_t)((a & 0x80) ? v : -v);
|
||||
}
|
||||
|
||||
/*
|
||||
* Rings
|
||||
*/
|
||||
|
||||
void ring_init(dmc_ring *r, int cap)
|
||||
{
|
||||
memset(r, 0, sizeof(*r));
|
||||
r->p = calloc((size_t)cap, sizeof(int16_t));
|
||||
if (!r->p)
|
||||
abort();
|
||||
r->cap = cap;
|
||||
}
|
||||
|
||||
void ring_free(dmc_ring *r)
|
||||
{
|
||||
free(r->p);
|
||||
memset(r, 0, sizeof(*r));
|
||||
}
|
||||
|
||||
void ring_clear(dmc_ring *r)
|
||||
{
|
||||
r->head = r->n = 0;
|
||||
}
|
||||
|
||||
void ring_put(dmc_ring *r, const int16_t *x, int n)
|
||||
{
|
||||
int i;
|
||||
if (!r->cap)
|
||||
return;
|
||||
for (i = 0; i < n; i++) {
|
||||
int at = (r->head + r->n) % r->cap;
|
||||
r->p[at] = x ? x[i] : 0;
|
||||
if (r->n < r->cap)
|
||||
r->n++;
|
||||
else
|
||||
r->head = (r->head + 1) % r->cap;
|
||||
}
|
||||
}
|
||||
|
||||
int ring_get(dmc_ring *r, int16_t *out, int max)
|
||||
{
|
||||
int i, n = r->n < max ? r->n : max;
|
||||
for (i = 0; i < n; i++) {
|
||||
out[i] = r->p[r->head];
|
||||
r->head = (r->head + 1) % r->cap;
|
||||
}
|
||||
r->n -= n;
|
||||
return n;
|
||||
}
|
||||
|
||||
void ring_keep(dmc_ring *r, int n)
|
||||
{
|
||||
if (r->n <= n)
|
||||
return;
|
||||
r->head = (r->head + r->n - n) % r->cap;
|
||||
r->n = n;
|
||||
}
|
||||
|
||||
/*
|
||||
* Tones
|
||||
*/
|
||||
|
||||
#define PI 3.14159265358979323846
|
||||
|
||||
void tone_start(dmc_tone *t, double f1, double f2, int samples,
|
||||
double amp)
|
||||
{
|
||||
memset(t, 0, sizeof(*t));
|
||||
t->k1 = 2 * PI * f1 / DMC_VOICE_RATE;
|
||||
t->k2 = 2 * PI * f2 / DMC_VOICE_RATE;
|
||||
t->c1 = t->c2 = 1;
|
||||
t->two = f2 > 0;
|
||||
t->left = samples;
|
||||
t->amp = amp;
|
||||
}
|
||||
|
||||
/* (each oscillator a unit vector turned by its step each sample) */
|
||||
static double turn(double *c, double *s, double k)
|
||||
{
|
||||
double c0 = *c, s0 = *s, ck = cos(k), sk = sin(k), m;
|
||||
*c = c0 * ck - s0 * sk;
|
||||
*s = c0 * sk + s0 * ck;
|
||||
/* (kept on the circle) */
|
||||
m = sqrt(*c * *c + *s * *s);
|
||||
if (m > 0) {
|
||||
*c /= m;
|
||||
*s /= m;
|
||||
}
|
||||
return s0;
|
||||
}
|
||||
|
||||
int tone_next(dmc_tone *t)
|
||||
{
|
||||
double y;
|
||||
if (t->left <= 0)
|
||||
return 0;
|
||||
t->left--;
|
||||
y = turn(&t->c1, &t->s1, t->k1);
|
||||
if (t->two)
|
||||
y = (y + turn(&t->c2, &t->s2, t->k2)) / 2;
|
||||
return (int)(y * t->amp);
|
||||
}
|
||||
|
||||
int dtmf_freqs(char digit, double *lo, double *hi)
|
||||
{
|
||||
static const char keys[] = "123A456B789C*0#D";
|
||||
static const double row[] = { 697, 770, 852, 941 };
|
||||
static const double col[] = { 1209, 1336, 1477, 1633 };
|
||||
const char *k;
|
||||
char d = digit;
|
||||
if (d >= 'a' && d <= 'd')
|
||||
d = (char)(d - 'a' + 'A');
|
||||
k = d ? strchr(keys, d) : NULL;
|
||||
if (!k)
|
||||
return -1;
|
||||
*lo = row[(k - keys) / 4];
|
||||
*hi = col[(k - keys) % 4];
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* WAV files
|
||||
*/
|
||||
|
||||
static void put16(char *p, unsigned v)
|
||||
{
|
||||
p[0] = (char)(v & 0xff);
|
||||
p[1] = (char)((v >> 8) & 0xff);
|
||||
}
|
||||
|
||||
static void put32(char *p, unsigned long v)
|
||||
{
|
||||
put16(p, (unsigned)(v & 0xffff));
|
||||
put16(p + 2, (unsigned)((v >> 16) & 0xffff));
|
||||
}
|
||||
|
||||
int dmc_wav_header(char *buf, int n)
|
||||
{
|
||||
unsigned long bytes = n > 0 ? (unsigned long)n * 2 : 0;
|
||||
memcpy(buf, "RIFF", 4);
|
||||
put32(buf + 4, 36 + bytes);
|
||||
memcpy(buf + 8, "WAVEfmt ", 8);
|
||||
put32(buf + 16, 16);
|
||||
put16(buf + 20, 1); /* PCM */
|
||||
put16(buf + 22, 1); /* mono */
|
||||
put32(buf + 24, DMC_VOICE_RATE);
|
||||
put32(buf + 28, DMC_VOICE_RATE * 2);
|
||||
put16(buf + 32, 2);
|
||||
put16(buf + 34, 16);
|
||||
memcpy(buf + 36, "data", 4);
|
||||
put32(buf + 40, bytes);
|
||||
return 44;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sound files read: WAV (PCM of 8 to 32 bits, floats, A-law, mu-law,
|
||||
* IMA and Microsoft ADPCM), AIFF and AIFF-C (PCM, floats, A-law,
|
||||
* mu-law), Sun/NeXT .au; any rate and channels, made 8 kHz mono
|
||||
*/
|
||||
|
||||
/* what a file's samples are */
|
||||
enum { E_PCM, E_FLOAT, E_ALAW, E_ULAW, E_IMA, E_MSADPCM };
|
||||
|
||||
struct sound {
|
||||
int enc, bits, big, channels, block;
|
||||
double rate;
|
||||
const unsigned char *p;
|
||||
long bytes;
|
||||
/* (MS ADPCM's coefficient pairs, from its fmt chunk) */
|
||||
int ncoef;
|
||||
int coef[64][2];
|
||||
};
|
||||
|
||||
static unsigned long get32be(const unsigned char *p)
|
||||
{
|
||||
return ((unsigned long)p[0] << 24) | ((unsigned long)p[1] << 16) |
|
||||
((unsigned long)p[2] << 8) | p[3];
|
||||
}
|
||||
|
||||
static unsigned ug16(const unsigned char *p)
|
||||
{
|
||||
return p[0] | ((unsigned)p[1] << 8);
|
||||
}
|
||||
|
||||
static unsigned long ug32(const unsigned char *p)
|
||||
{
|
||||
return ug16(p) | ((unsigned long)ug16(p + 2) << 16);
|
||||
}
|
||||
|
||||
/* an 80-bit IEEE extended (AIFF's rate) as a double */
|
||||
static double extended(const unsigned char *p)
|
||||
{
|
||||
int e = ((p[0] & 0x7f) << 8 | p[1]) - 16383 - 63;
|
||||
double m = (double)get32be(p + 2) * 4294967296.0 +
|
||||
(double)get32be(p + 6);
|
||||
double v = ldexp(m, e);
|
||||
return (p[0] & 0x80) ? -v : v;
|
||||
}
|
||||
|
||||
/* a RIFF WAVE's: 0, or -1 if it isn't one this reads */
|
||||
static int parse_wav(const unsigned char *d, long n, struct sound *f)
|
||||
{
|
||||
const unsigned char *p = d + 12, *end = d + n;
|
||||
int fmt = 0, tag = 0;
|
||||
while (end - p >= 8) {
|
||||
unsigned long len = ug32(p + 4);
|
||||
const unsigned char *body = p + 8;
|
||||
long left = (long)(end - body);
|
||||
if (!memcmp(p, "fmt ", 4) && len >= 16 && left >= 16) {
|
||||
tag = (int)ug16(body);
|
||||
f->channels = (int)ug16(body + 2);
|
||||
f->rate = ug32(body + 4);
|
||||
f->block = (int)ug16(body + 12);
|
||||
f->bits = (int)ug16(body + 14);
|
||||
/* WAVE_FORMAT_EXTENSIBLE: its subformat's code */
|
||||
if (tag == 0xfffe && len >= 26 && left >= 26)
|
||||
tag = (int)ug16(body + 24);
|
||||
if (tag == 2 && len >= 22 && left >= 22) {
|
||||
int i, k = (int)ug16(body + 20);
|
||||
if (k > 64 || (long)(22 + 4 * k) > left)
|
||||
return -1;
|
||||
f->ncoef = k;
|
||||
for (i = 0; i < k; i++) {
|
||||
f->coef[i][0] = (int16_t)ug16(body + 22 + 4 * i);
|
||||
f->coef[i][1] = (int16_t)ug16(body + 24 + 4 * i);
|
||||
}
|
||||
}
|
||||
fmt = 1;
|
||||
} else if (!memcmp(p, "data", 4)) {
|
||||
f->p = body;
|
||||
f->bytes = len > (unsigned long)left ? left : (long)len;
|
||||
break;
|
||||
}
|
||||
if (len > (unsigned long)left)
|
||||
break;
|
||||
p = body + len + (len & 1);
|
||||
}
|
||||
if (!fmt || !f->p)
|
||||
return -1;
|
||||
switch (tag) {
|
||||
case 1: f->enc = E_PCM; break;
|
||||
case 3: f->enc = E_FLOAT; break;
|
||||
case 6: f->enc = E_ALAW; break;
|
||||
case 7: f->enc = E_ULAW; break;
|
||||
case 0x11: f->enc = E_IMA; break;
|
||||
case 2: f->enc = E_MSADPCM; break;
|
||||
default: return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* an AIFF or AIFF-C's */
|
||||
static int parse_aiff(const unsigned char *d, long n, struct sound *f)
|
||||
{
|
||||
const unsigned char *p = d + 12, *end = d + n;
|
||||
int comm = 0, aifc = !memcmp(d + 8, "AIFC", 4);
|
||||
char comp[4] = { 'N', 'O', 'N', 'E' };
|
||||
while (end - p >= 8) {
|
||||
unsigned long len = get32be(p + 4);
|
||||
const unsigned char *body = p + 8;
|
||||
long left = (long)(end - body);
|
||||
if (!memcmp(p, "COMM", 4) && len >= 18 && left >= 18) {
|
||||
f->channels = (int)(body[0] << 8 | body[1]);
|
||||
f->bits = (int)(body[6] << 8 | body[7]);
|
||||
f->rate = extended(body + 8);
|
||||
if (aifc && len >= 22 && left >= 22)
|
||||
memcpy(comp, body + 18, 4);
|
||||
comm = 1;
|
||||
} else if (!memcmp(p, "SSND", 4) && len >= 8 && left >= 8) {
|
||||
unsigned long off = get32be(body);
|
||||
long l = len > (unsigned long)left ? left : (long)len;
|
||||
if ((long)off > l - 8)
|
||||
return -1;
|
||||
f->p = body + 8 + off;
|
||||
f->bytes = l - 8 - (long)off;
|
||||
}
|
||||
if (len > (unsigned long)left)
|
||||
break;
|
||||
p = body + len + (len & 1);
|
||||
}
|
||||
if (!comm || !f->p)
|
||||
return -1;
|
||||
f->big = 1;
|
||||
if (!memcmp(comp, "NONE", 4) || !memcmp(comp, "twos", 4)) {
|
||||
f->enc = E_PCM;
|
||||
} else if (!memcmp(comp, "sowt", 4)) {
|
||||
f->enc = E_PCM;
|
||||
f->big = 0;
|
||||
} else if (!memcmp(comp, "fl32", 4) || !memcmp(comp, "FL32", 4)) {
|
||||
f->enc = E_FLOAT;
|
||||
f->bits = 32;
|
||||
} else if (!memcmp(comp, "fl64", 4) || !memcmp(comp, "FL64", 4)) {
|
||||
f->enc = E_FLOAT;
|
||||
f->bits = 64;
|
||||
} else if (!memcmp(comp, "ulaw", 4) || !memcmp(comp, "ULAW", 4)) {
|
||||
f->enc = E_ULAW;
|
||||
f->bits = 8;
|
||||
} else if (!memcmp(comp, "alaw", 4) || !memcmp(comp, "ALAW", 4)) {
|
||||
f->enc = E_ALAW;
|
||||
f->bits = 8;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
/* (AIFF's 8-bit samples are signed) */
|
||||
if (f->enc == E_PCM && f->bits == 8)
|
||||
f->big = 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* a Sun/NeXT .au's */
|
||||
static int parse_au(const unsigned char *d, long n, struct sound *f)
|
||||
{
|
||||
unsigned long off, len, enc;
|
||||
if (n < 24)
|
||||
return -1;
|
||||
off = get32be(d + 4);
|
||||
len = get32be(d + 8);
|
||||
enc = get32be(d + 12);
|
||||
f->rate = get32be(d + 16);
|
||||
f->channels = (int)get32be(d + 20);
|
||||
if (off < 24 || (long)off > n)
|
||||
return -1;
|
||||
f->p = d + off;
|
||||
f->bytes = n - (long)off;
|
||||
if (len != 0xffffffffUL && (long)len < f->bytes)
|
||||
f->bytes = (long)len;
|
||||
f->big = 1;
|
||||
switch (enc) {
|
||||
case 1: f->enc = E_ULAW; f->bits = 8; break;
|
||||
case 2: f->enc = E_PCM; f->bits = 8; f->big = 2; break;
|
||||
case 3: f->enc = E_PCM; f->bits = 16; break;
|
||||
case 4: f->enc = E_PCM; f->bits = 24; break;
|
||||
case 5: f->enc = E_PCM; f->bits = 32; break;
|
||||
case 6: f->enc = E_FLOAT; f->bits = 32; break;
|
||||
case 7: f->enc = E_FLOAT; f->bits = 64; break;
|
||||
case 27: f->enc = E_ALAW; f->bits = 8; break;
|
||||
default: return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* a sample of width w bytes at q as 16 bits (big: 1 big-endian, 2
|
||||
big-endian with 8-bit samples signed) */
|
||||
static int pcm_at(const unsigned char *q, int w, int big)
|
||||
{
|
||||
unsigned long v = 0;
|
||||
int i;
|
||||
if (w == 1)
|
||||
return big == 2 ? (int)(signed char)q[0] * 256 :
|
||||
((int)q[0] - 128) * 256;
|
||||
for (i = 0; i < w; i++)
|
||||
v |= (unsigned long)q[big ? i : w - 1 - i] << (8 * (w - 1 - i));
|
||||
/* (the top two bytes: the sample's sign is the top bit) */
|
||||
v >>= 8 * (w - 2);
|
||||
return (int16_t)(v & 0xffff);
|
||||
}
|
||||
|
||||
static int float_at(const unsigned char *q, int w, int big)
|
||||
{
|
||||
unsigned char b[8];
|
||||
double x;
|
||||
int i;
|
||||
for (i = 0; i < w; i++)
|
||||
b[i] = q[big ? w - 1 - i : i];
|
||||
/* (b is little-endian now, as the machines this runs on are) */
|
||||
if (w == 4) {
|
||||
float y;
|
||||
memcpy(&y, b, 4);
|
||||
x = y;
|
||||
} else {
|
||||
memcpy(&x, b, 8);
|
||||
}
|
||||
if (x != x)
|
||||
return 0;
|
||||
x *= 32767.0;
|
||||
return x > 32767 ? 32767 : x < -32768 ? -32768 : (int)x;
|
||||
}
|
||||
|
||||
static const int ima_steps[89] = {
|
||||
7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34,
|
||||
37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
|
||||
157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494,
|
||||
544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552,
|
||||
1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428,
|
||||
4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
|
||||
12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086,
|
||||
29794, 32767
|
||||
};
|
||||
static const int ima_index[16] = { -1, -1, -1, -1, 2, 4, 6, 8,
|
||||
-1, -1, -1, -1, 2, 4, 6, 8 };
|
||||
|
||||
static int ima_nibble(int *pred, int *idx, int nib)
|
||||
{
|
||||
int step = ima_steps[*idx], diff = step >> 3;
|
||||
if (nib & 4) diff += step;
|
||||
if (nib & 2) diff += step >> 1;
|
||||
if (nib & 1) diff += step >> 2;
|
||||
*pred += (nib & 8) ? -diff : diff;
|
||||
if (*pred > 32767) *pred = 32767;
|
||||
if (*pred < -32768) *pred = -32768;
|
||||
*idx += ima_index[nib];
|
||||
if (*idx < 0) *idx = 0;
|
||||
if (*idx > 88) *idx = 88;
|
||||
return *pred;
|
||||
}
|
||||
|
||||
/* one channel's sample of frame i: the files' samples mixed to mono
|
||||
into out (frames of them): how many frames */
|
||||
static long decode_mono(const struct sound *f, int16_t *out, long cap)
|
||||
{
|
||||
int c, ch = f->channels;
|
||||
long fr = 0;
|
||||
if (f->enc == E_IMA) {
|
||||
/* blocks: per channel a header (sample, index), then 4-byte
|
||||
groups of 8 nibbles per channel in turn */
|
||||
const unsigned char *b = f->p, *end = f->p + f->bytes;
|
||||
int per = f->block > 4 * ch ? (f->block - 4 * ch) * 2 / ch + 1 : 0;
|
||||
if (per <= 0 || per > 65536)
|
||||
return -1;
|
||||
while (end - b >= f->block && fr < cap) {
|
||||
int pred[8], idx[8], i;
|
||||
int16_t *o = out + fr;
|
||||
long n = per < cap - fr ? per : cap - fr;
|
||||
for (i = 0; i < n; i++)
|
||||
o[i] = 0;
|
||||
for (c = 0; c < ch; c++) {
|
||||
const unsigned char *h = b + 4 * c;
|
||||
pred[c] = (int16_t)ug16(h);
|
||||
idx[c] = h[2] > 88 ? 88 : h[2];
|
||||
}
|
||||
for (c = 0; c < ch; c++) {
|
||||
long k = 0;
|
||||
const unsigned char *q = b + 4 * ch + 4 * c;
|
||||
if (k < n)
|
||||
o[k] = (int16_t)(o[k] + pred[c] / ch);
|
||||
k = 1;
|
||||
while (k < per && q + 4 <= b + f->block) {
|
||||
int j;
|
||||
for (j = 0; j < 8 && k < per; j++, k++) {
|
||||
int nib = (q[j / 2] >> ((j & 1) * 4)) & 15;
|
||||
int v = ima_nibble(&pred[c], &idx[c], nib);
|
||||
if (k < n)
|
||||
o[k] = (int16_t)(o[k] + v / ch);
|
||||
}
|
||||
q += 4 * ch;
|
||||
}
|
||||
}
|
||||
fr += n;
|
||||
b += f->block;
|
||||
}
|
||||
return fr;
|
||||
}
|
||||
if (f->enc == E_MSADPCM) {
|
||||
static const int adapt[16] = { 230, 230, 230, 230, 307, 409,
|
||||
512, 614, 768, 614, 512, 409,
|
||||
307, 230, 230, 230 };
|
||||
const unsigned char *b = f->p, *end = f->p + f->bytes;
|
||||
int per;
|
||||
if (ch > 2 || !f->ncoef || f->block < 7 * ch)
|
||||
return -1;
|
||||
per = (f->block - 7 * ch) * 2 / ch + 2;
|
||||
while (end - b >= f->block && fr < cap) {
|
||||
int pred[2], delta[2], s1[2], s2[2];
|
||||
const unsigned char *q = b;
|
||||
long n = per < cap - fr ? per : cap - fr, k;
|
||||
int16_t *o = out + fr;
|
||||
for (c = 0; c < ch; c++)
|
||||
pred[c] = q[c] < f->ncoef ? q[c] : 0;
|
||||
q += ch;
|
||||
for (c = 0; c < ch; c++, q += 2)
|
||||
delta[c] = (int16_t)ug16(q);
|
||||
for (c = 0; c < ch; c++, q += 2)
|
||||
s1[c] = (int16_t)ug16(q);
|
||||
for (c = 0; c < ch; c++, q += 2)
|
||||
s2[c] = (int16_t)ug16(q);
|
||||
/* (the header's two samples: the older first) */
|
||||
if (n > 0) {
|
||||
long v = 0;
|
||||
for (c = 0; c < ch; c++) v += s2[c];
|
||||
o[0] = (int16_t)(v / ch);
|
||||
}
|
||||
if (n > 1) {
|
||||
long v = 0;
|
||||
for (c = 0; c < ch; c++) v += s1[c];
|
||||
o[1] = (int16_t)(v / ch);
|
||||
}
|
||||
for (k = 2; k < per; k++) {
|
||||
long v = 0;
|
||||
for (c = 0; c < ch; c++) {
|
||||
long nib_i = (k - 2) * ch + c;
|
||||
int nib = (q[nib_i / 2] >> ((nib_i & 1) ? 0 : 4)) & 15;
|
||||
int sn = nib & 8 ? nib - 16 : nib;
|
||||
long long p = ((long long)s1[c] * f->coef[pred[c]][0] +
|
||||
(long long)s2[c] * f->coef[pred[c]][1]) / 256;
|
||||
p += (long long)sn * delta[c];
|
||||
if (p > 32767) p = 32767;
|
||||
if (p < -32768) p = -32768;
|
||||
s2[c] = s1[c];
|
||||
s1[c] = (int)p;
|
||||
delta[c] = (int)((long)delta[c] * adapt[nib] / 256);
|
||||
/* (a broken file's grows without end) */
|
||||
if (delta[c] > 0x100000) delta[c] = 0x100000;
|
||||
if (delta[c] < 16) delta[c] = 16;
|
||||
v += (long)p;
|
||||
}
|
||||
if (k < n)
|
||||
o[k] = (int16_t)(v / ch);
|
||||
}
|
||||
fr += n;
|
||||
b += f->block;
|
||||
}
|
||||
return fr;
|
||||
}
|
||||
{
|
||||
int w = f->bits / 8;
|
||||
long frames, i;
|
||||
if (w < 1 || w > 8 || (f->enc == E_PCM && w > 4) ||
|
||||
(f->enc == E_FLOAT && w != 4 && w != 8) ||
|
||||
((f->enc == E_ALAW || f->enc == E_ULAW) && w != 1))
|
||||
return -1;
|
||||
frames = f->bytes / ((long)w * ch);
|
||||
if (frames > cap)
|
||||
frames = cap;
|
||||
for (i = 0; i < frames; i++) {
|
||||
long sum = 0;
|
||||
for (c = 0; c < ch; c++) {
|
||||
const unsigned char *q = f->p + (i * ch + c) * w;
|
||||
switch (f->enc) {
|
||||
case E_ULAW: sum += ulaw_decode(*q); break;
|
||||
case E_ALAW: sum += alaw_decode(*q); break;
|
||||
case E_FLOAT: sum += float_at(q, w, f->big); break;
|
||||
default: sum += pcm_at(q, w, f->big); break;
|
||||
}
|
||||
}
|
||||
out[i] = (int16_t)(sum / ch);
|
||||
}
|
||||
return frames;
|
||||
}
|
||||
}
|
||||
|
||||
/* the most frames f could hold */
|
||||
static long most_frames(const struct sound *f)
|
||||
{
|
||||
if (f->enc == E_IMA || f->enc == E_MSADPCM) {
|
||||
if (f->block <= 0)
|
||||
return 0;
|
||||
return (f->bytes / f->block + 1) * ((long)f->block * 2 + 2);
|
||||
}
|
||||
if (f->bits < 8)
|
||||
return 0;
|
||||
return f->bytes / ((long)(f->bits / 8) * f->channels);
|
||||
}
|
||||
|
||||
int dmc_audio_decode(const char *data, int n, int16_t **pcm)
|
||||
{
|
||||
const unsigned char *d = (const unsigned char *)data;
|
||||
struct sound f;
|
||||
int16_t *mono, *out;
|
||||
long frames, cap, out_n, i;
|
||||
int r = -1;
|
||||
|
||||
*pcm = NULL;
|
||||
if (!data || n < 12)
|
||||
return -1;
|
||||
memset(&f, 0, sizeof(f));
|
||||
if (!memcmp(d, "RIFF", 4) && !memcmp(d + 8, "WAVE", 4))
|
||||
r = parse_wav(d, n, &f);
|
||||
else if (!memcmp(d, "FORM", 4) &&
|
||||
(!memcmp(d + 8, "AIFF", 4) || !memcmp(d + 8, "AIFC", 4)))
|
||||
r = parse_aiff(d, n, &f);
|
||||
else if (!memcmp(d, ".snd", 4))
|
||||
r = parse_au(d, n, &f);
|
||||
if (r || f.channels < 1 || f.channels > 8 || !(f.rate >= 1000) ||
|
||||
f.rate > 384000)
|
||||
return -1;
|
||||
cap = most_frames(&f);
|
||||
if (cap < 0 || cap > 100000000L)
|
||||
return -1;
|
||||
mono = malloc((size_t)(cap > 0 ? cap : 1) * sizeof(int16_t));
|
||||
if (!mono)
|
||||
return -1;
|
||||
frames = decode_mono(&f, mono, cap);
|
||||
if (frames < 0) {
|
||||
free(mono);
|
||||
return -1;
|
||||
}
|
||||
out_n = (long)((double)frames * DMC_VOICE_RATE / f.rate);
|
||||
out = malloc((size_t)(out_n > 0 ? out_n : 1) * sizeof(int16_t));
|
||||
if (!out) {
|
||||
free(mono);
|
||||
return -1;
|
||||
}
|
||||
for (i = 0; i < out_n; i++) {
|
||||
/* (linear between the two frames around it) */
|
||||
double at = (double)i * f.rate / DMC_VOICE_RATE;
|
||||
long k = (long)at;
|
||||
double frac = at - (double)k;
|
||||
long k1 = k + 1 < frames ? k + 1 : frames - 1;
|
||||
double y = (1 - frac) * mono[k] + frac * mono[k1];
|
||||
out[i] = (int16_t)(y > 32767 ? 32767 : y < -32768 ? -32768 : y);
|
||||
}
|
||||
free(mono);
|
||||
*pcm = out;
|
||||
return (int)out_n;
|
||||
}
|
||||
|
||||
int dmc_wav_decode(const char *data, int n, int16_t **pcm)
|
||||
{
|
||||
return dmc_audio_decode(data, n, pcm);
|
||||
}
|
||||
|
||||
void dmc_free(void *p)
|
||||
{
|
||||
free(p);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* voice.h - what the session's voice calls are made of: G.711 mu-law,
|
||||
* rings of samples, the tones we play (DTMF, the beep)
|
||||
*/
|
||||
#ifndef DMC_VOICE_H
|
||||
#define DMC_VOICE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* G.711 mu-law, the samples on the DTE in a call (AT+VSM=4) */
|
||||
unsigned char ulaw_encode(int16_t x);
|
||||
int16_t ulaw_decode(unsigned char u);
|
||||
/* and A-law, for WAV files */
|
||||
int16_t alaw_decode(unsigned char a);
|
||||
|
||||
/* a ring of samples that keeps the newest cap of them */
|
||||
typedef struct dmc_ring {
|
||||
int16_t *p;
|
||||
int cap, head, n;
|
||||
} dmc_ring;
|
||||
|
||||
void ring_init(dmc_ring *r, int cap);
|
||||
void ring_free(dmc_ring *r);
|
||||
void ring_clear(dmc_ring *r);
|
||||
/* n samples in: the oldest dropped when it is full */
|
||||
void ring_put(dmc_ring *r, const int16_t *x, int n);
|
||||
/* up to max out of it: how many */
|
||||
int ring_get(dmc_ring *r, int16_t *out, int max);
|
||||
/* the oldest taken out until n are left */
|
||||
void ring_keep(dmc_ring *r, int n);
|
||||
|
||||
/* a tone of one or two frequencies, n samples long */
|
||||
typedef struct dmc_tone {
|
||||
double c1, s1, k1, c2, s2, k2; /* each oscillator's state, step */
|
||||
int two, left;
|
||||
double amp;
|
||||
} dmc_tone;
|
||||
|
||||
void tone_start(dmc_tone *t, double f1, double f2, int samples,
|
||||
double amp);
|
||||
/* its next sample (0 when it has ended) */
|
||||
int tone_next(dmc_tone *t);
|
||||
/* a DTMF digit's two frequencies: 0, or -1 for none */
|
||||
int dtmf_freqs(char digit, double *lo, double *hi);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,325 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* vt_charsets.c - the character sets a remote may speak (UTF-8, code
|
||||
* page 437, Latin-1), the G sets a DEC terminal designates (DEC Special
|
||||
* Graphics, Supplemental, Technical, the national sets), and how many
|
||||
* cells a character takes (wcwidth). A port of the app's Charsets.kt
|
||||
* and Wcwidth.kt.
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "vtint.h"
|
||||
|
||||
static const uint32_t cp437_high[128] = {
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
|
||||
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
|
||||
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,
|
||||
0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,
|
||||
0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
|
||||
0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
|
||||
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,
|
||||
0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,
|
||||
0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,
|
||||
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0,
|
||||
};
|
||||
static const uint32_t dec_graphics_tab[32] = {
|
||||
0x00A0, 0x25C6, 0x2592, 0x2409, 0x240C, 0x240D, 0x240A, 0x00B0,
|
||||
0x00B1, 0x2424, 0x240B, 0x2518, 0x2510, 0x250C, 0x2514, 0x253C,
|
||||
0x23BA, 0x23BB, 0x2500, 0x23BC, 0x23BD, 0x251C, 0x2524, 0x2534,
|
||||
0x252C, 0x2502, 0x2264, 0x2265, 0x03C0, 0x2260, 0x00A3, 0x00B7,
|
||||
};
|
||||
static const uint32_t technical_tab[94] = {
|
||||
0x23B7, 0x250C, 0x2500, 0x2320, 0x2321, 0x2502, 0x23A1, 0x23A3,
|
||||
0x23A4, 0x23A6, 0x239B, 0x239D, 0x239E, 0x23A0, 0x23A8, 0x23AC,
|
||||
0x23B2, 0x23B3, 0x2572, 0x2571, 0x231D, 0x231F, 0x27E9, 0x0020,
|
||||
0x0020, 0x0020, 0x0020, 0x2264, 0x2260, 0x2265, 0x222B, 0x2234,
|
||||
0x221D, 0x221E, 0x00F7, 0x0394, 0x2207, 0x03A6, 0x0393, 0x223C,
|
||||
0x2243, 0x0398, 0x00D7, 0x039B, 0x21D4, 0x21D2, 0x2261, 0x03A0,
|
||||
0x03A8, 0x0020, 0x03A3, 0x0020, 0x0020, 0x221A, 0x03A9, 0x039E,
|
||||
0x03A5, 0x2282, 0x2283, 0x2229, 0x222A, 0x2227, 0x2228, 0x00AC,
|
||||
0x03B1, 0x03B2, 0x03C7, 0x03B4, 0x03B5, 0x03C6, 0x03B3, 0x03B7,
|
||||
0x03B9, 0x03B8, 0x03BA, 0x03BB, 0x0020, 0x03BD, 0x2202, 0x03C0,
|
||||
0x03C8, 0x03C1, 0x03C3, 0x03C4, 0x0020, 0x0192, 0x03C9, 0x03BE,
|
||||
0x03C5, 0x03B6, 0x2190, 0x2191, 0x2192, 0x2193,
|
||||
};
|
||||
static const uint32_t vt52_tab[33] = {
|
||||
0x0020, 0x0020, 0x0020, 0x2588, 0x215F, 0x00B3, 0x2075, 0x2077,
|
||||
0x00B0, 0x00B1, 0x2192, 0x2026, 0x00F7, 0x2193, 0x2594, 0x1FB76,
|
||||
0x1FB77, 0x1FB78, 0x1FB79, 0x1FB7A, 0x1FB7B, 0x2581, 0x2080, 0x2081,
|
||||
0x2082, 0x2083, 0x2084, 0x2085, 0x2086, 0x2087, 0x2088, 0x2089,
|
||||
0x00B6,
|
||||
};
|
||||
|
||||
uint32_t cp437_decode(int b)
|
||||
{
|
||||
return b < 0x80 ? (uint32_t)b : cp437_high[(b - 0x80) & 0x7f];
|
||||
}
|
||||
|
||||
int cp437_encode(uint32_t cp)
|
||||
{
|
||||
int i;
|
||||
if (cp < 0x80)
|
||||
return (int)cp;
|
||||
for (i = 0; i < 128; i++)
|
||||
if (cp437_high[i] == cp)
|
||||
return 0x80 + i;
|
||||
return '?';
|
||||
}
|
||||
|
||||
void charset_encode(int cs, const uint32_t *cps, int n, dmc_buf *out)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
uint32_t c = cps[i];
|
||||
if (cs == CS_UTF8)
|
||||
utf8_put(out, c);
|
||||
else if (cs == CS_LATIN1)
|
||||
buf_addc(out, (char)(c < 256 ? c : '?'));
|
||||
else
|
||||
buf_addc(out, (char)cp437_encode(c));
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t dec_graphics(uint32_t cp)
|
||||
{
|
||||
return cp >= 0x5f && cp <= 0x7e ? dec_graphics_tab[cp - 0x5f] : cp;
|
||||
}
|
||||
|
||||
/*
|
||||
* The G sets: their designators (what follows ESC ( to designate them),
|
||||
* size, the VT level that has them, and a national set's replacements
|
||||
* (pairs: the ASCII character and what it is there, ended by 0)
|
||||
*/
|
||||
struct gset_info {
|
||||
const char *designators[3];
|
||||
int is96, level;
|
||||
const uint32_t *table;
|
||||
};
|
||||
|
||||
static const uint32_t nat_uk[] = { 0x23, 0xA3, 0 };
|
||||
static const uint32_t nat_dutch[] = { 0x23, 0xA3, 0x40, 0xBE, 0x5B, 0x133, 0x5C, 0xBD, 0x5D, 0x7C, 0x7B, 0xA8, 0x7C, 0x192, 0x7D, 0xBC, 0x7E, 0xB4, 0 };
|
||||
static const uint32_t nat_finnish[] = { 0x5B, 0xC4, 0x5C, 0xD6, 0x5D, 0xC5, 0x5E, 0xDC, 0x60, 0xE9, 0x7B, 0xE4, 0x7C, 0xF6, 0x7D, 0xE5, 0x7E, 0xFC, 0 };
|
||||
static const uint32_t nat_french[] = { 0x23, 0xA3, 0x40, 0xE0, 0x5B, 0xB0, 0x5C, 0xE7, 0x5D, 0xA7, 0x7B, 0xE9, 0x7C, 0xF9, 0x7D, 0xE8, 0x7E, 0xA8, 0 };
|
||||
static const uint32_t nat_french_canadian[] = { 0x40, 0xE0, 0x5B, 0xE2, 0x5C, 0xE7, 0x5D, 0xEA, 0x5E, 0xEE, 0x60, 0xF4, 0x7B, 0xE9, 0x7C, 0xF9, 0x7D, 0xE8, 0x7E, 0xFB, 0 };
|
||||
static const uint32_t nat_german[] = { 0x40, 0xA7, 0x5B, 0xC4, 0x5C, 0xD6, 0x5D, 0xDC, 0x7B, 0xE4, 0x7C, 0xF6, 0x7D, 0xFC, 0x7E, 0xDF, 0 };
|
||||
static const uint32_t nat_italian[] = { 0x23, 0xA3, 0x40, 0xA7, 0x5B, 0xB0, 0x5C, 0xE7, 0x5D, 0xE9, 0x60, 0xF9, 0x7B, 0xE0, 0x7C, 0xF2, 0x7D, 0xE8, 0x7E, 0xEC, 0 };
|
||||
static const uint32_t nat_norwegian_danish[] = { 0x40, 0xC4, 0x5B, 0xC6, 0x5C, 0xD8, 0x5D, 0xC5, 0x5E, 0xDC, 0x60, 0xE4, 0x7B, 0xE6, 0x7C, 0xF8, 0x7D, 0xE5, 0x7E, 0xFC, 0 };
|
||||
static const uint32_t nat_spanish[] = { 0x23, 0xA3, 0x40, 0xA7, 0x5B, 0xA1, 0x5C, 0xD1, 0x5D, 0xBF, 0x7B, 0xB0, 0x7C, 0xF1, 0x7D, 0xE7, 0 };
|
||||
static const uint32_t nat_swedish[] = { 0x40, 0xC9, 0x5B, 0xC4, 0x5C, 0xD6, 0x5D, 0xC5, 0x5E, 0xDC, 0x60, 0xE9, 0x7B, 0xE4, 0x7C, 0xF6, 0x7D, 0xE5, 0x7E, 0xFC, 0 };
|
||||
static const uint32_t nat_swiss[] = { 0x23, 0xF9, 0x40, 0xE0, 0x5B, 0xE9, 0x5C, 0xE7, 0x5D, 0xEA, 0x5E, 0xEE, 0x5F, 0xE8, 0x60, 0xF4, 0x7B, 0xE4, 0x7C, 0xF6, 0x7D, 0xFC, 0x7E, 0xFB, 0 };
|
||||
static const uint32_t nat_portuguese[] = { 0x5B, 0xC3, 0x5C, 0xC7, 0x5D, 0xD5, 0x7B, 0xE3, 0x7C, 0xE7, 0x7D, 0xF5, 0 };
|
||||
|
||||
static const struct gset_info gsets[G_COUNT] = {
|
||||
{ { "B", NULL, NULL }, 0, 1, NULL }, /* ASCII */
|
||||
{ { "0", NULL, NULL }, 0, 1, NULL }, /* DEC_GRAPHICS */
|
||||
{ { "A", NULL, NULL }, 0, 1, nat_uk }, /* UK */
|
||||
{ { NULL, NULL, NULL }, 0, 1, NULL }, /* VT52_GRAPHICS */
|
||||
{ { "%5", "<", NULL }, 0, 2, NULL }, /* DEC_SUPPLEMENTAL */
|
||||
{ { ">", NULL, NULL }, 0, 3, NULL }, /* DEC_TECHNICAL */
|
||||
{ { "A", NULL, NULL }, 1, 3, NULL }, /* LATIN1 */
|
||||
{ { "4", NULL, NULL }, 0, 2, nat_dutch }, /* DUTCH */
|
||||
{ { "C", "5", NULL }, 0, 2, nat_finnish }, /* FINNISH */
|
||||
{ { "R", "f", NULL }, 0, 2, nat_french }, /* FRENCH */
|
||||
{ { "Q", "9", NULL }, 0, 2, nat_french_canadian }, /* FRENCH_CANADIAN */
|
||||
{ { "K", NULL, NULL }, 0, 2, nat_german }, /* GERMAN */
|
||||
{ { "Y", NULL, NULL }, 0, 2, nat_italian }, /* ITALIAN */
|
||||
{ { "E", "6", "`" }, 0, 2, nat_norwegian_danish }, /* NORWEGIAN_DANISH */
|
||||
{ { "Z", NULL, NULL }, 0, 2, nat_spanish }, /* SPANISH */
|
||||
{ { "H", "7", NULL }, 0, 2, nat_swedish }, /* SWEDISH */
|
||||
{ { "=", NULL, NULL }, 0, 2, nat_swiss }, /* SWISS */
|
||||
{ { "%6", NULL, NULL }, 0, 3, nat_portuguese }, /* PORTUGUESE */
|
||||
{ { NULL, NULL, NULL }, 0, 2, NULL }, /* SOFT */
|
||||
};
|
||||
|
||||
int gset_by_designator(const char *d, int is96)
|
||||
{
|
||||
int g, k;
|
||||
for (g = 0; g < G_COUNT; g++) {
|
||||
if (gsets[g].is96 != is96)
|
||||
continue;
|
||||
for (k = 0; k < 3; k++)
|
||||
if (gsets[g].designators[k] &&
|
||||
strcmp(gsets[g].designators[k], d) == 0)
|
||||
return g;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int gset_is96(int g)
|
||||
{
|
||||
return gsets[g].is96;
|
||||
}
|
||||
|
||||
int gset_level(int g)
|
||||
{
|
||||
return gsets[g].level;
|
||||
}
|
||||
|
||||
const char *gset_designator(int g)
|
||||
{
|
||||
return gsets[g].designators[0] ? gsets[g].designators[0] : "";
|
||||
}
|
||||
|
||||
/* where DEC Supplemental isn't Latin-1: ¤ at 0x28, Œ Ÿ œ ÿ, and the codes
|
||||
it left empty (shown blank) */
|
||||
static uint32_t supplemental(uint32_t cp)
|
||||
{
|
||||
switch (cp) {
|
||||
case 0x28: return 0xa4;
|
||||
case 0x57: return 0x152;
|
||||
case 0x5d: return 0x178;
|
||||
case 0x77: return 0x153;
|
||||
case 0x7d: return 0xff;
|
||||
case 0x24: case 0x26: case 0x2c: case 0x2d: case 0x2e: case 0x2f:
|
||||
case 0x34: case 0x38: case 0x3e: case 0x50: case 0x5e: case 0x70:
|
||||
case 0x7e:
|
||||
return 0x20;
|
||||
}
|
||||
return cp + 0x80;
|
||||
}
|
||||
|
||||
uint32_t gset_map(int g, uint32_t cp)
|
||||
{
|
||||
const uint32_t *t;
|
||||
switch (g) {
|
||||
case G_ASCII:
|
||||
return cp;
|
||||
case G_DEC_GRAPHICS:
|
||||
return dec_graphics(cp);
|
||||
case G_VT52_GRAPHICS:
|
||||
return cp >= 0x5e && cp <= 0x7e ? vt52_tab[cp - 0x5e] : cp;
|
||||
case G_DEC_SUPPLEMENTAL:
|
||||
return cp >= 0x21 && cp <= 0x7e ? supplemental(cp) : cp;
|
||||
case G_DEC_TECHNICAL:
|
||||
return cp >= 0x21 && cp <= 0x7e ? technical_tab[cp - 0x21] : cp;
|
||||
case G_LATIN1:
|
||||
return cp >= 0x20 && cp <= 0x7f ? cp + 0x80 : cp;
|
||||
}
|
||||
for (t = gsets[g].table; t && *t; t += 2)
|
||||
if (t[0] == cp)
|
||||
return t[1];
|
||||
return cp;
|
||||
}
|
||||
|
||||
/* wcwidth: Unicode 15's tables, in (first, last) pairs */
|
||||
static const uint32_t wc_zero[452] = {
|
||||
0x00300, 0x0036F, 0x00483, 0x00489, 0x00591, 0x005BD, 0x005BF, 0x005BF,
|
||||
0x005C1, 0x005C2, 0x005C4, 0x005C5, 0x005C7, 0x005C7, 0x00610, 0x0061A,
|
||||
0x0061C, 0x0061C, 0x0064B, 0x0065F, 0x00670, 0x00670, 0x006D6, 0x006DC,
|
||||
0x006DF, 0x006E4, 0x006E7, 0x006E8, 0x006EA, 0x006ED, 0x00711, 0x00711,
|
||||
0x00730, 0x0074A, 0x007A6, 0x007B0, 0x007EB, 0x007F3, 0x007FD, 0x007FD,
|
||||
0x00816, 0x00819, 0x0081B, 0x00823, 0x00825, 0x00827, 0x00829, 0x0082D,
|
||||
0x00859, 0x0085B, 0x00898, 0x0089F, 0x008CA, 0x008E1, 0x008E3, 0x00902,
|
||||
0x0093A, 0x0093A, 0x0093C, 0x0093C, 0x00941, 0x00948, 0x0094D, 0x0094D,
|
||||
0x00951, 0x00957, 0x00962, 0x00963, 0x00981, 0x00981, 0x009BC, 0x009BC,
|
||||
0x009C1, 0x009C4, 0x009CD, 0x009CD, 0x009E2, 0x009E3, 0x009FE, 0x009FE,
|
||||
0x00A01, 0x00A02, 0x00A3C, 0x00A3C, 0x00A41, 0x00A51, 0x00A70, 0x00A71,
|
||||
0x00A75, 0x00A75, 0x00A81, 0x00A82, 0x00ABC, 0x00ABC, 0x00AC1, 0x00AC8,
|
||||
0x00ACD, 0x00ACD, 0x00AE2, 0x00AE3, 0x00AFA, 0x00AFF, 0x00B01, 0x00B01,
|
||||
0x00B3C, 0x00B3C, 0x00B3F, 0x00B3F, 0x00B41, 0x00B44, 0x00B4D, 0x00B4D,
|
||||
0x00B55, 0x00B56, 0x00B62, 0x00B63, 0x00B82, 0x00B82, 0x00BC0, 0x00BC0,
|
||||
0x00BCD, 0x00BCD, 0x00C00, 0x00C00, 0x00C04, 0x00C04, 0x00C3C, 0x00C3C,
|
||||
0x00C3E, 0x00C40, 0x00C46, 0x00C56, 0x00C62, 0x00C63, 0x00C81, 0x00C81,
|
||||
0x00CBC, 0x00CBC, 0x00CBF, 0x00CBF, 0x00CC6, 0x00CC6, 0x00CCC, 0x00CCD,
|
||||
0x00CE2, 0x00CE3, 0x00D00, 0x00D01, 0x00D3B, 0x00D3C, 0x00D41, 0x00D44,
|
||||
0x00D4D, 0x00D4D, 0x00D62, 0x00D63, 0x00D81, 0x00D81, 0x00DCA, 0x00DCA,
|
||||
0x00DD2, 0x00DD6, 0x00E31, 0x00E31, 0x00E34, 0x00E3A, 0x00E47, 0x00E4E,
|
||||
0x00EB1, 0x00EB1, 0x00EB4, 0x00EBC, 0x00EC8, 0x00ECE, 0x00F18, 0x00F19,
|
||||
0x00F35, 0x00F35, 0x00F37, 0x00F37, 0x00F39, 0x00F39, 0x00F71, 0x00F7E,
|
||||
0x00F80, 0x00F84, 0x00F86, 0x00F87, 0x00F8D, 0x00FBC, 0x00FC6, 0x00FC6,
|
||||
0x0102D, 0x01030, 0x01032, 0x01037, 0x01039, 0x0103A, 0x0103D, 0x0103E,
|
||||
0x01058, 0x01059, 0x0105E, 0x01060, 0x01071, 0x01074, 0x01082, 0x01082,
|
||||
0x01085, 0x01086, 0x0108D, 0x0108D, 0x0109D, 0x0109D, 0x01160, 0x011FF,
|
||||
0x0135D, 0x0135F, 0x01712, 0x01714, 0x01732, 0x01733, 0x01752, 0x01753,
|
||||
0x01772, 0x01773, 0x017B4, 0x017B5, 0x017B7, 0x017BD, 0x017C6, 0x017C6,
|
||||
0x017C9, 0x017D3, 0x017DD, 0x017DD, 0x0180B, 0x0180F, 0x01885, 0x01886,
|
||||
0x018A9, 0x018A9, 0x01920, 0x01922, 0x01927, 0x01928, 0x01932, 0x01932,
|
||||
0x01939, 0x0193B, 0x01A17, 0x01A18, 0x01A1B, 0x01A1B, 0x01A56, 0x01A56,
|
||||
0x01A58, 0x01A60, 0x01A62, 0x01A62, 0x01A65, 0x01A6C, 0x01A73, 0x01A7F,
|
||||
0x01AB0, 0x01ACE, 0x01B00, 0x01B03, 0x01B34, 0x01B34, 0x01B36, 0x01B3A,
|
||||
0x01B3C, 0x01B3C, 0x01B42, 0x01B42, 0x01B6B, 0x01B73, 0x01B80, 0x01B81,
|
||||
0x01BA2, 0x01BA5, 0x01BA8, 0x01BA9, 0x01BAB, 0x01BAD, 0x01BE6, 0x01BE6,
|
||||
0x01BE8, 0x01BE9, 0x01BED, 0x01BED, 0x01BEF, 0x01BF1, 0x01C2C, 0x01C33,
|
||||
0x01C36, 0x01C37, 0x01CD0, 0x01CD2, 0x01CD4, 0x01CE0, 0x01CE2, 0x01CE8,
|
||||
0x01CED, 0x01CED, 0x01CF4, 0x01CF4, 0x01CF8, 0x01CF9, 0x01DC0, 0x01DFF,
|
||||
0x0200B, 0x0200F, 0x0202A, 0x0202E, 0x02060, 0x02064, 0x020D0, 0x020F0,
|
||||
0x02CEF, 0x02CF1, 0x02D7F, 0x02D7F, 0x02DE0, 0x02DFF, 0x0302A, 0x0302D,
|
||||
0x03099, 0x0309A, 0x0A66F, 0x0A672, 0x0A674, 0x0A67D, 0x0A69E, 0x0A69F,
|
||||
0x0A6F0, 0x0A6F1, 0x0A802, 0x0A802, 0x0A806, 0x0A806, 0x0A80B, 0x0A80B,
|
||||
0x0A825, 0x0A826, 0x0A82C, 0x0A82C, 0x0A8C4, 0x0A8C5, 0x0A8E0, 0x0A8F1,
|
||||
0x0A8FF, 0x0A8FF, 0x0A926, 0x0A92D, 0x0A947, 0x0A951, 0x0A980, 0x0A982,
|
||||
0x0A9B3, 0x0A9B3, 0x0A9B6, 0x0A9B9, 0x0A9BC, 0x0A9BD, 0x0A9E5, 0x0A9E5,
|
||||
0x0AA29, 0x0AA2E, 0x0AA31, 0x0AA32, 0x0AA35, 0x0AA36, 0x0AA43, 0x0AA43,
|
||||
0x0AA4C, 0x0AA4C, 0x0AA7C, 0x0AA7C, 0x0AAB0, 0x0AAB0, 0x0AAB2, 0x0AAB4,
|
||||
0x0AAB7, 0x0AAB8, 0x0AABE, 0x0AABF, 0x0AAC1, 0x0AAC1, 0x0AAEC, 0x0AAED,
|
||||
0x0AAF6, 0x0AAF6, 0x0ABE5, 0x0ABE5, 0x0ABE8, 0x0ABE8, 0x0ABED, 0x0ABED,
|
||||
0x0D7B0, 0x0D7FF, 0x0FB1E, 0x0FB1E, 0x0FE00, 0x0FE0F, 0x0FE20, 0x0FE2F,
|
||||
0x0FEFF, 0x0FEFF, 0x0FFF9, 0x0FFFB, 0x101FD, 0x101FD, 0x10376, 0x1037A,
|
||||
0x10A01, 0x10A0F, 0x10A38, 0x10A3F, 0x11001, 0x11001, 0x11038, 0x11046,
|
||||
0x1107F, 0x11081, 0x110B3, 0x110B6, 0x110B9, 0x110BA, 0x11100, 0x11102,
|
||||
0x11127, 0x1112B, 0x1112D, 0x11134, 0x1D167, 0x1D169, 0x1D173, 0x1D182,
|
||||
0x1D185, 0x1D18B, 0x1D1AA, 0x1D1AD, 0x1F3FB, 0x1F3FF, 0xE0001, 0xE0001,
|
||||
0xE0020, 0xE007F, 0xE0100, 0xE01EF,
|
||||
};
|
||||
static const uint32_t wc_wide[206] = {
|
||||
0x01100, 0x0115F, 0x0231A, 0x0231B, 0x02329, 0x0232A, 0x023E9, 0x023EC,
|
||||
0x023F0, 0x023F0, 0x023F3, 0x023F3, 0x025FD, 0x025FE, 0x02614, 0x02615,
|
||||
0x02648, 0x02653, 0x0267F, 0x0267F, 0x02693, 0x02693, 0x026A1, 0x026A1,
|
||||
0x026AA, 0x026AB, 0x026BD, 0x026BE, 0x026C4, 0x026C5, 0x026CE, 0x026CE,
|
||||
0x026D4, 0x026D4, 0x026EA, 0x026EA, 0x026F2, 0x026F3, 0x026F5, 0x026F5,
|
||||
0x026FA, 0x026FA, 0x026FD, 0x026FD, 0x02705, 0x02705, 0x0270A, 0x0270B,
|
||||
0x02728, 0x02728, 0x0274C, 0x0274C, 0x0274E, 0x0274E, 0x02753, 0x02755,
|
||||
0x02757, 0x02757, 0x02795, 0x02797, 0x027B0, 0x027B0, 0x027BF, 0x027BF,
|
||||
0x02B1B, 0x02B1C, 0x02B50, 0x02B50, 0x02B55, 0x02B55, 0x02E80, 0x03029,
|
||||
0x0302E, 0x0303E, 0x03041, 0x03096, 0x0309B, 0x033FF, 0x03400, 0x04DBF,
|
||||
0x04E00, 0x09FFF, 0x0A000, 0x0A4CF, 0x0A960, 0x0A97F, 0x0AC00, 0x0D7A3,
|
||||
0x0F900, 0x0FAFF, 0x0FE10, 0x0FE19, 0x0FE30, 0x0FE6F, 0x0FF00, 0x0FF60,
|
||||
0x0FFE0, 0x0FFE6, 0x16FE0, 0x16FE4, 0x16FF0, 0x16FF1, 0x17000, 0x18CD5,
|
||||
0x18D00, 0x18D08, 0x1AFF0, 0x1B2FB, 0x1F004, 0x1F004, 0x1F0CF, 0x1F0CF,
|
||||
0x1F18E, 0x1F18E, 0x1F191, 0x1F19A, 0x1F200, 0x1F202, 0x1F210, 0x1F23B,
|
||||
0x1F240, 0x1F248, 0x1F250, 0x1F251, 0x1F260, 0x1F265, 0x1F300, 0x1F320,
|
||||
0x1F32D, 0x1F335, 0x1F337, 0x1F37C, 0x1F37E, 0x1F393, 0x1F3A0, 0x1F3CA,
|
||||
0x1F3CF, 0x1F3D3, 0x1F3E0, 0x1F3F0, 0x1F3F4, 0x1F3F4, 0x1F3F8, 0x1F3FA,
|
||||
0x1F400, 0x1F43E, 0x1F440, 0x1F440, 0x1F442, 0x1F4FC, 0x1F4FF, 0x1F53D,
|
||||
0x1F54B, 0x1F54E, 0x1F550, 0x1F567, 0x1F57A, 0x1F57A, 0x1F595, 0x1F596,
|
||||
0x1F5A4, 0x1F5A4, 0x1F5FB, 0x1F64F, 0x1F680, 0x1F6C5, 0x1F6CC, 0x1F6CC,
|
||||
0x1F6D0, 0x1F6D2, 0x1F6D5, 0x1F6D7, 0x1F6DC, 0x1F6DF, 0x1F6EB, 0x1F6EC,
|
||||
0x1F6F4, 0x1F6FC, 0x1F7E0, 0x1F7EB, 0x1F7F0, 0x1F7F0, 0x1F90C, 0x1F93A,
|
||||
0x1F93C, 0x1F945, 0x1F947, 0x1F9FF, 0x1FA70, 0x1FA7C, 0x1FA80, 0x1FA88,
|
||||
0x1FA90, 0x1FABD, 0x1FABF, 0x1FAC5, 0x1FACE, 0x1FADB, 0x1FAE0, 0x1FAE8,
|
||||
0x1FAF0, 0x1FAF8, 0x20000, 0x2FFFD, 0x30000, 0x3FFFD,
|
||||
};
|
||||
|
||||
static int in_ranges(uint32_t cp, const uint32_t *r, int pairs)
|
||||
{
|
||||
int lo = 0, hi = pairs - 1;
|
||||
while (lo <= hi) {
|
||||
int mid = (lo + hi) / 2;
|
||||
if (cp < r[mid * 2])
|
||||
hi = mid - 1;
|
||||
else if (cp > r[mid * 2 + 1])
|
||||
lo = mid + 1;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wcwidth_of(uint32_t cp)
|
||||
{
|
||||
if (cp < 0x300)
|
||||
return 1;
|
||||
if (in_ranges(cp, wc_zero, (int)(sizeof(wc_zero) / sizeof(wc_zero[0]) / 2)))
|
||||
return 0;
|
||||
if (in_ranges(cp, wc_wide, (int)(sizeof(wc_wide) / sizeof(wc_wide[0]) / 2)))
|
||||
return 2;
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* vt_fonts.c - the terminal types' fonts (the app's MachineFonts): each
|
||||
* machine's own character ROM (vt_data.c's, SyncTERM's set) read in its
|
||||
* own set, so a cell's code point is drawn with the glyph its machine
|
||||
* drew for it; what a ROM hasn't got from Unscii's. Made when first
|
||||
* asked for and kept.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "vtint.h"
|
||||
|
||||
/* the Atari ST's set from 0x80: Latin letters, Hebrew, Greek and
|
||||
mathematics */
|
||||
static const uint32_t st_high[128] = {
|
||||
0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
|
||||
0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
|
||||
0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
|
||||
0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0xdf, 0x192,
|
||||
0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
|
||||
0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
|
||||
0xe3, 0xf5, 0xd8, 0xf8, 0x153, 0x152, 0xc0, 0xc3,
|
||||
0xd5, 0xa8, 0xb4, 0x2020, 0xb6, 0xa9, 0xae, 0x2122,
|
||||
0x133, 0x132, 0x5d0, 0x5d1, 0x5d2, 0x5d3, 0x5d4, 0x5d5,
|
||||
0x5d6, 0x5d7, 0x5d8, 0x5d9, 0x5db, 0x5dc, 0x5de, 0x5e0,
|
||||
0x5e1, 0x5e2, 0x5e4, 0x5e6, 0x5e7, 0x5e8, 0x5e9, 0x5ea,
|
||||
0x5df, 0x5da, 0x5dd, 0x5e3, 0x5e5, 0xa7, 0x2227, 0x221e,
|
||||
0x3b1, 0x3b2, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
|
||||
0x3a6, 0x398, 0x3a9, 0x3b4, 0x222e, 0x3d5, 0x2208, 0x2229,
|
||||
0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
|
||||
0xb0, 0x2022, 0xb7, 0x221a, 0x207f, 0xb2, 0xb3, 0xaf,
|
||||
};
|
||||
|
||||
uint32_t atari_st_char(int b)
|
||||
{
|
||||
return b >= 0x80 && b <= 0xff ? st_high[b - 0x80] : (uint32_t)b;
|
||||
}
|
||||
|
||||
/* teletext's English set, 0x20-0x7f: £ for #, arrows, fractions, # for
|
||||
_, a dash, ‖, ÷ and a block */
|
||||
uint32_t viewdata_g0(int c)
|
||||
{
|
||||
switch (c) {
|
||||
case 0x23: return 0xa3;
|
||||
case 0x5b: return 0x2190;
|
||||
case 0x5c: return 0xbd;
|
||||
case 0x5d: return 0x2192;
|
||||
case 0x5e: return 0x2191;
|
||||
case 0x5f: return '#';
|
||||
case 0x60: return 0x2014;
|
||||
case 0x7b: return 0xbc;
|
||||
case 0x7c: return 0x2016;
|
||||
case 0x7d: return 0xbe;
|
||||
case 0x7e: return 0xf7;
|
||||
case 0x7f: return 0x25a0;
|
||||
}
|
||||
return (uint32_t)c;
|
||||
}
|
||||
|
||||
/* a font being made: its glyphs by code point, the first one put kept */
|
||||
struct builder {
|
||||
int h, n, cap;
|
||||
uint32_t *codes;
|
||||
uint8_t *rows;
|
||||
};
|
||||
|
||||
static int b_has(const struct builder *b, uint32_t cp)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < b->n; i++)
|
||||
if (b->codes[i] == cp)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void b_put(struct builder *b, uint32_t cp, const uint8_t *rows)
|
||||
{
|
||||
if (cp == 0 || b_has(b, cp))
|
||||
return;
|
||||
if (b->n == b->cap) {
|
||||
int cap = b->cap ? b->cap * 2 : 512;
|
||||
uint32_t *c = (uint32_t *)realloc(b->codes, cap * sizeof(*c));
|
||||
uint8_t *r = (uint8_t *)realloc(b->rows, (size_t)cap * b->h);
|
||||
if (!c || !r) {
|
||||
free(c ? c : b->codes);
|
||||
free(r ? r : b->rows);
|
||||
b->codes = NULL;
|
||||
b->rows = NULL;
|
||||
b->n = b->cap = 0;
|
||||
return;
|
||||
}
|
||||
b->codes = c;
|
||||
b->rows = r;
|
||||
b->cap = cap;
|
||||
}
|
||||
b->codes[b->n] = cp;
|
||||
memcpy(b->rows + (size_t)b->n * b->h, rows, b->h);
|
||||
b->n++;
|
||||
}
|
||||
|
||||
/* code's glyph of ROM r, h (8 or 16) rows: its own, or its 8 doubled or
|
||||
its 16 halved (both rows' pixels, so a thin stroke stays) */
|
||||
static void rom_glyph(int r, int code, int h, uint8_t *out)
|
||||
{
|
||||
const struct vt_rom *rom = &vt_roms[r];
|
||||
int i;
|
||||
if (h == 16 && rom->r16) {
|
||||
memcpy(out, rom->r16 + code * 16, 16);
|
||||
} else if (h == 8 && rom->r8) {
|
||||
memcpy(out, rom->r8 + code * 8, 8);
|
||||
} else if (h == 16) {
|
||||
for (i = 0; i < 16; i++)
|
||||
out[i] = rom->r8[code * 8 + i / 2];
|
||||
} else {
|
||||
for (i = 0; i < 8; i++)
|
||||
out[i] = rom->r16[code * 16 + 2 * i] |
|
||||
rom->r16[code * 16 + 2 * i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* ROM r's codes from..to, each as the code point map gives it (0: none) */
|
||||
static void b_rom(struct builder *b, int r, int from, int to,
|
||||
uint32_t (*map)(int, const void *), const void *ctx)
|
||||
{
|
||||
uint8_t g[16];
|
||||
int c;
|
||||
for (c = from; c <= to; c++) {
|
||||
uint32_t cp = map(c, ctx);
|
||||
if (cp == 0 || b_has(b, cp))
|
||||
continue;
|
||||
rom_glyph(r, c, b->h, g);
|
||||
b_put(b, cp, g);
|
||||
}
|
||||
}
|
||||
|
||||
static void b_font(struct builder *b, const struct vt_font *f)
|
||||
{
|
||||
int i;
|
||||
if (!f || f->h != b->h)
|
||||
return;
|
||||
for (i = 0; i < f->n; i++)
|
||||
b_put(b, f->codes[i], f->rows8 + (size_t)i * f->h);
|
||||
}
|
||||
|
||||
static int cmp_code(const void *a, const void *b)
|
||||
{
|
||||
uint32_t x = *(const uint32_t *)a, y = *(const uint32_t *)b;
|
||||
return x < y ? -1 : x > y;
|
||||
}
|
||||
|
||||
/* the font made: its glyphs sorted by code point */
|
||||
static const struct vt_font *b_done(struct builder *b)
|
||||
{
|
||||
struct vt_font *f = (struct vt_font *)calloc(1, sizeof(*f));
|
||||
uint32_t *idx, *codes;
|
||||
uint8_t *rows;
|
||||
int i;
|
||||
/* (each code and its place, sorted together) */
|
||||
idx = (uint32_t *)malloc(sizeof(uint32_t) * 2 * (b->n ? b->n : 1));
|
||||
codes = (uint32_t *)malloc(sizeof(uint32_t) * (b->n ? b->n : 1));
|
||||
rows = (uint8_t *)malloc((size_t)(b->n ? b->n : 1) * b->h);
|
||||
if (!f || !idx || !codes || !rows) {
|
||||
free(f);
|
||||
free(idx);
|
||||
free(codes);
|
||||
free(rows);
|
||||
free(b->codes);
|
||||
free(b->rows);
|
||||
return NULL;
|
||||
}
|
||||
for (i = 0; i < b->n; i++) {
|
||||
idx[2 * i] = b->codes[i];
|
||||
idx[2 * i + 1] = (uint32_t)i;
|
||||
}
|
||||
qsort(idx, b->n, 2 * sizeof(uint32_t), cmp_code);
|
||||
for (i = 0; i < b->n; i++) {
|
||||
codes[i] = idx[2 * i];
|
||||
memcpy(rows + (size_t)i * b->h,
|
||||
b->rows + (size_t)idx[2 * i + 1] * b->h, b->h);
|
||||
}
|
||||
free(idx);
|
||||
free(b->codes);
|
||||
free(b->rows);
|
||||
f->w = 8;
|
||||
f->h = b->h;
|
||||
f->n = b->n;
|
||||
f->codes = codes;
|
||||
f->rows8 = rows;
|
||||
f->rows32 = NULL;
|
||||
return f;
|
||||
}
|
||||
|
||||
/* octet b's code point in a charset (0 for a control) */
|
||||
static uint32_t octet(int b, const void *ctx)
|
||||
{
|
||||
int cs = *(const int *)ctx;
|
||||
if (b < 0x20 || b == 0x7f)
|
||||
return 0;
|
||||
if (cs == CS_CP437)
|
||||
return cp437_decode(b);
|
||||
if (b >= 0x80 && b <= 0x9f)
|
||||
return 0;
|
||||
if (cs == CS_LATIN1)
|
||||
return (uint32_t)b;
|
||||
return b < 0x80 ? (uint32_t)b : 0;
|
||||
}
|
||||
|
||||
static uint32_t in_set(int b, const void *ctx)
|
||||
{
|
||||
return ((const uint32_t *)ctx)[b];
|
||||
}
|
||||
|
||||
static uint32_t st_map(int b, const void *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
return atari_st_char(b);
|
||||
}
|
||||
|
||||
static uint32_t g0_map(int b, const void *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
return viewdata_g0(b);
|
||||
}
|
||||
|
||||
static const struct vt_font *fallback(int h)
|
||||
{
|
||||
return h == 16 ? &vt_font_vga : &vt_font_8x8;
|
||||
}
|
||||
|
||||
/* ROM r read in charset cs, h rows */
|
||||
static const struct vt_font *in_charset(int r, int h, int cs)
|
||||
{
|
||||
struct builder b;
|
||||
memset(&b, 0, sizeof(b));
|
||||
b.h = h;
|
||||
b_rom(&b, r, 0x20, 0xff, octet, &cs);
|
||||
b_font(&b, fallback(h));
|
||||
return b_done(&b);
|
||||
}
|
||||
|
||||
static const struct vt_font *make(int which)
|
||||
{
|
||||
struct builder b;
|
||||
memset(&b, 0, sizeof(b));
|
||||
b.h = 8;
|
||||
switch (which) {
|
||||
case MF_VGA:
|
||||
return in_charset(ROM_VGA, 16, CS_CP437);
|
||||
case MF_RIPTERM:
|
||||
return in_charset(ROM_RIPTERM, 8, CS_CP437);
|
||||
case MF_TOPAZ:
|
||||
return in_charset(ROM_TOPAZ, 8, CS_LATIN1);
|
||||
case MF_C64:
|
||||
case MF_C128:
|
||||
b_rom(&b, which == MF_C64 ? ROM_C64_UPPER : ROM_C128_UPPER,
|
||||
0, 0xff, in_set, vt_set_c64_upper);
|
||||
b_rom(&b, which == MF_C64 ? ROM_C64_LOWER : ROM_C128_LOWER,
|
||||
0, 0xff, in_set, vt_set_c64_lower);
|
||||
break;
|
||||
case MF_ATARI:
|
||||
b_rom(&b, ROM_ATARI, 0, 0x7f, in_set, vt_set_atari_graphics);
|
||||
break;
|
||||
case MF_ATARI_ST:
|
||||
b_rom(&b, ROM_ATARI_ST, 0x20, 0xff, st_map, NULL);
|
||||
break;
|
||||
case MF_PRESTEL:
|
||||
b.h = 16;
|
||||
b_rom(&b, ROM_PRESTEL, 0x20, 0x7f, g0_map, NULL);
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
b_font(&b, fallback(b.h));
|
||||
return b_done(&b);
|
||||
}
|
||||
|
||||
static const struct vt_font *made[MF_COUNT];
|
||||
|
||||
const struct vt_font *machine_font(int which)
|
||||
{
|
||||
if (which < 0 || which >= MF_COUNT)
|
||||
return NULL;
|
||||
if (!made[which])
|
||||
made[which] = make(which);
|
||||
return made[which];
|
||||
}
|
||||
|
||||
/* SyncTERM's numbers (CSI SP D's) and the ROMs */
|
||||
static const int syncterm_rom[][2] = {
|
||||
{ 0, ROM_VGA }, { 26, ROM_VGA_THIN }, { 32, ROM_C64_UPPER },
|
||||
{ 33, ROM_C64_LOWER }, { 34, ROM_C128_UPPER }, { 35, ROM_C128_LOWER },
|
||||
{ 36, ROM_ATARI }, { 37, ROM_POT_NOODLE }, { 38, ROM_MOSOUL },
|
||||
{ 39, ROM_MICROKNIGHT_PLUS }, { 40, ROM_TOPAZ_PLUS },
|
||||
{ 41, ROM_MICROKNIGHT }, { 42, ROM_TOPAZ }, { 43, ROM_PRESTEL },
|
||||
{ 44, ROM_ATARI_ST }
|
||||
};
|
||||
#define N_SYNCTERM ((int)(sizeof(syncterm_rom) / sizeof(syncterm_rom[0])))
|
||||
|
||||
/* each one made, by ROM, height (8 or 16) and charset */
|
||||
static const struct vt_font *synctermed[ROM_COUNT][2][3];
|
||||
|
||||
const struct vt_font *syncterm_font(int n, int height, int charset)
|
||||
{
|
||||
int i, r = -1, hi = height == 16 ? 1 : 0;
|
||||
for (i = 0; i < N_SYNCTERM; i++)
|
||||
if (syncterm_rom[i][0] == n)
|
||||
r = syncterm_rom[i][1];
|
||||
if (r < 0 || charset < 0 || charset > 2)
|
||||
return NULL;
|
||||
if (!synctermed[r][hi][charset])
|
||||
synctermed[r][hi][charset] = in_charset(r, hi ? 16 : 8, charset);
|
||||
return synctermed[r][hi][charset];
|
||||
}
|
||||
@@ -0,0 +1,970 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* vt_gfx.c - the terminal's graphics: sixel images (the app's Sixel), a
|
||||
* graphics terminal's picture over the screen (Raster, PictureLayer) and
|
||||
* the Tektronix 4010/4014 (TektronixInterpreter).
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "vtint.h"
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
static int clampi(int v, int lo, int hi)
|
||||
{
|
||||
return v < lo ? lo : v > hi ? hi : v;
|
||||
}
|
||||
|
||||
static int maxi(int a, int b)
|
||||
{
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
static int mini(int a, int b)
|
||||
{
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
static int roundi(double v)
|
||||
{
|
||||
return (int)floor(v + 0.5);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* sixel */
|
||||
|
||||
struct sixel {
|
||||
int transparent;
|
||||
uint32_t own[256];
|
||||
uint32_t *palette;
|
||||
int max_w, max_h;
|
||||
int color, x, y, repeat;
|
||||
uint32_t *buf;
|
||||
int buf_w, buf_h;
|
||||
int width, height;
|
||||
int command, params[5], nparams;
|
||||
int failed;
|
||||
};
|
||||
|
||||
static int pct(int v)
|
||||
{
|
||||
return (clampi(v, 0, 100) * 255 + 50) / 100;
|
||||
}
|
||||
|
||||
static uint32_t rgb(int r, int g, int b)
|
||||
{
|
||||
return 0xff000000u | ((uint32_t)r << 16) | ((uint32_t)g << 8) | (uint32_t)b;
|
||||
}
|
||||
|
||||
static int hue(double p, double q, double t)
|
||||
{
|
||||
double v;
|
||||
if (t < 0)
|
||||
t += 1.0;
|
||||
if (t > 1)
|
||||
t -= 1.0;
|
||||
if (t < 1.0 / 6)
|
||||
v = p + (q - p) * 6 * t;
|
||||
else if (t < 1.0 / 2)
|
||||
v = q;
|
||||
else if (t < 2.0 / 3)
|
||||
v = p + (q - p) * (2.0 / 3 - t) * 6;
|
||||
else
|
||||
v = p;
|
||||
return clampi(roundi(v * 255), 0, 255);
|
||||
}
|
||||
|
||||
uint32_t sixel_hls(int hdeg, int lpct, int spct)
|
||||
{
|
||||
double l = clampi(lpct, 0, 100) / 100.0, s = clampi(spct, 0, 100) / 100.0;
|
||||
/* DEC's hue is the usual one turned by 240 degrees (its 0 is blue) */
|
||||
double h = (((hdeg + 240) % 360) + 360) % 360 / 360.0, q, p;
|
||||
if (s == 0.0) {
|
||||
int v = roundi(l * 255);
|
||||
return rgb(v, v, v);
|
||||
}
|
||||
q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
p = 2 * l - q;
|
||||
return rgb(hue(p, q, h + 1.0 / 3), hue(p, q, h), hue(p, q, h - 1.0 / 3));
|
||||
}
|
||||
|
||||
void sixel_default_palette(uint32_t p[256])
|
||||
{
|
||||
static const uint8_t vt340[48] = {
|
||||
0, 0, 0, 20, 20, 80, 80, 13, 13, 20, 80, 20, 80, 20, 80, 20, 80, 80,
|
||||
80, 80, 20, 53, 53, 53, 26, 26, 26, 33, 33, 60, 60, 26, 26, 33, 60, 33,
|
||||
60, 33, 60, 33, 60, 60, 60, 60, 33, 80, 80, 80
|
||||
};
|
||||
static const int steps[6] = { 0, 95, 135, 175, 215, 255 };
|
||||
int i;
|
||||
for (i = 0; i < 16; i++)
|
||||
p[i] = rgb(pct(vt340[i * 3]), pct(vt340[i * 3 + 1]), pct(vt340[i * 3 + 2]));
|
||||
for (i = 16; i < 232; i++) {
|
||||
int n = i - 16;
|
||||
p[i] = rgb(steps[n / 36], steps[(n / 6) % 6], steps[n % 6]);
|
||||
}
|
||||
for (i = 232; i < 256; i++) {
|
||||
int v = 8 + (i - 232) * 10;
|
||||
p[i] = rgb(v, v, v);
|
||||
}
|
||||
}
|
||||
|
||||
struct sixel *sixel_new(int transparent, uint32_t *shared, int max_w, int max_h)
|
||||
{
|
||||
struct sixel *s = (struct sixel *)calloc(1, sizeof(*s));
|
||||
if (!s)
|
||||
return NULL;
|
||||
s->transparent = transparent;
|
||||
if (shared) {
|
||||
s->palette = shared;
|
||||
} else {
|
||||
sixel_default_palette(s->own);
|
||||
s->palette = s->own;
|
||||
}
|
||||
s->max_w = max_w > 0 ? max_w : 1;
|
||||
s->max_h = max_h > 0 ? max_h : 1;
|
||||
s->repeat = 1;
|
||||
return s;
|
||||
}
|
||||
|
||||
void sixel_free(struct sixel *s)
|
||||
{
|
||||
if (s) {
|
||||
free(s->buf);
|
||||
free(s);
|
||||
}
|
||||
}
|
||||
|
||||
/* the buffer made at least w x h (doubling, so that growing is cheap) */
|
||||
static int grow(struct sixel *s, int w, int h)
|
||||
{
|
||||
int nw, nh, r;
|
||||
uint32_t *nb;
|
||||
if (w <= s->buf_w && h <= s->buf_h)
|
||||
return 1;
|
||||
if (s->failed)
|
||||
return 0;
|
||||
nw = maxi(s->buf_w, mini(s->max_w, maxi(maxi(w, s->buf_w * 2), 64)));
|
||||
nh = maxi(s->buf_h, mini(s->max_h + 6, maxi(maxi(h, s->buf_h * 2), 64)));
|
||||
nb = (uint32_t *)calloc((size_t)nw * nh, sizeof(uint32_t));
|
||||
if (!nb) {
|
||||
s->failed = 1;
|
||||
return 0;
|
||||
}
|
||||
for (r = 0; r < s->buf_h; r++)
|
||||
memcpy(nb + (size_t)r * nw, s->buf + (size_t)r * s->buf_w,
|
||||
(size_t)s->buf_w * sizeof(uint32_t));
|
||||
free(s->buf);
|
||||
s->buf = nb;
|
||||
s->buf_w = nw;
|
||||
s->buf_h = nh;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void end_command(struct sixel *s)
|
||||
{
|
||||
int n;
|
||||
switch (s->command) {
|
||||
case '!':
|
||||
s->repeat = s->params[0] > 1 ? s->params[0] : 1;
|
||||
break;
|
||||
case '#':
|
||||
n = s->params[0] & 0xff;
|
||||
if (s->nparams >= 5)
|
||||
s->palette[n] = s->params[1] == 1 ?
|
||||
sixel_hls(s->params[2], s->params[3], s->params[4]) :
|
||||
rgb(pct(s->params[2]), pct(s->params[3]), pct(s->params[4]));
|
||||
s->color = n;
|
||||
break;
|
||||
case '"':
|
||||
if (s->nparams >= 4 && s->params[2] > 0 && s->params[3] > 0) {
|
||||
int w = mini(s->params[2], s->max_w), h = mini(s->params[3], s->max_h);
|
||||
if (grow(s, w, h)) {
|
||||
s->width = maxi(s->width, w);
|
||||
s->height = maxi(s->height, h);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
s->command = 0;
|
||||
}
|
||||
|
||||
static void sixel_bits(struct sixel *s, int bits)
|
||||
{
|
||||
int n = s->repeat, b, right, px;
|
||||
s->repeat = 1;
|
||||
if (bits && s->y < s->max_h) {
|
||||
right = mini(s->x + n, s->max_w);
|
||||
if (right > s->x && grow(s, right, s->y + 6)) {
|
||||
uint32_t c = s->palette[s->color];
|
||||
for (b = 0; b < 6; b++) {
|
||||
int row = s->y + b;
|
||||
uint32_t *base;
|
||||
if (!(bits & (1 << b)))
|
||||
continue;
|
||||
if (row >= s->max_h)
|
||||
break;
|
||||
base = s->buf + (size_t)row * s->buf_w;
|
||||
for (px = s->x; px < right; px++)
|
||||
base[px] = c;
|
||||
if (row + 1 > s->height)
|
||||
s->height = row + 1;
|
||||
}
|
||||
if (right > s->width)
|
||||
s->width = right;
|
||||
}
|
||||
}
|
||||
if (s->x < 1 << 20)
|
||||
s->x += n;
|
||||
}
|
||||
|
||||
void sixel_feed(struct sixel *s, int32_t cp)
|
||||
{
|
||||
if (s->command) {
|
||||
if (cp >= '0' && cp <= '9') {
|
||||
if (s->nparams == 0)
|
||||
s->nparams = 1;
|
||||
if (s->nparams <= 5) {
|
||||
int *v = &s->params[s->nparams - 1];
|
||||
*v = mini(*v * 10 + (cp - '0'), 99999);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (cp == ';') {
|
||||
if (s->nparams == 0)
|
||||
s->nparams = 1;
|
||||
if (s->nparams < 5) {
|
||||
s->params[s->nparams] = 0;
|
||||
s->nparams++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
end_command(s);
|
||||
}
|
||||
if (cp >= 0x3f && cp <= 0x7e) {
|
||||
sixel_bits(s, cp - 0x3f);
|
||||
} else if (cp == '#' || cp == '!' || cp == '"') {
|
||||
s->command = cp;
|
||||
s->nparams = 0;
|
||||
memset(s->params, 0, sizeof(s->params));
|
||||
} else if (cp == '$') {
|
||||
s->x = 0;
|
||||
} else if (cp == '-') {
|
||||
s->x = 0;
|
||||
if (s->y < 1 << 20)
|
||||
s->y += 6;
|
||||
}
|
||||
}
|
||||
|
||||
struct vt_image *sixel_finish(dmc_vt *t, struct sixel *s, int cw, int ch)
|
||||
{
|
||||
int h, r, i;
|
||||
struct vt_image *img;
|
||||
(void)t;
|
||||
if (s->command)
|
||||
end_command(s);
|
||||
h = mini(s->height, s->max_h);
|
||||
if (s->width <= 0 || h <= 0 || !s->buf)
|
||||
return NULL;
|
||||
img = image_new(s->width, h, cw, ch, 0);
|
||||
for (r = 0; r < h; r++)
|
||||
memcpy(img->pixels + (size_t)r * s->width, s->buf + (size_t)r * s->buf_w,
|
||||
(size_t)s->width * sizeof(uint32_t));
|
||||
if (!s->transparent) {
|
||||
/* the background: register 0 as the image left it */
|
||||
uint32_t bg = s->palette[0];
|
||||
for (i = 0; i < s->width * h; i++)
|
||||
if (!img->pixels[i])
|
||||
img->pixels[i] = bg;
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* the raster */
|
||||
|
||||
struct raster *raster_new(int w, int h, int lw, int lh)
|
||||
{
|
||||
struct raster *r = (struct raster *)calloc(1, sizeof(*r));
|
||||
if (!r)
|
||||
return NULL;
|
||||
r->width = w > 0 ? w : 1;
|
||||
r->height = h > 0 ? h : 1;
|
||||
r->lw = lw > 0 ? lw : 1;
|
||||
r->lh = lh > 0 ? lh : 1;
|
||||
r->px = (uint32_t *)calloc((size_t)r->width * r->height, sizeof(uint32_t));
|
||||
if (!r->px) {
|
||||
free(r);
|
||||
return NULL;
|
||||
}
|
||||
r->mode = R_OVERLAY;
|
||||
r->color = 0xffffffffu;
|
||||
r->pattern = 0xff;
|
||||
return r;
|
||||
}
|
||||
|
||||
void raster_free(struct raster *r)
|
||||
{
|
||||
if (r) {
|
||||
free(r->px);
|
||||
free(r);
|
||||
}
|
||||
}
|
||||
|
||||
int raster_px(const struct raster *r, int x)
|
||||
{
|
||||
return (int)((long long)x * r->width / r->lw);
|
||||
}
|
||||
|
||||
int raster_py(const struct raster *r, int y)
|
||||
{
|
||||
return (int)((long long)y * r->height / r->lh);
|
||||
}
|
||||
|
||||
void raster_clear(struct raster *r, uint32_t c)
|
||||
{
|
||||
int i, n = r->width * r->height;
|
||||
for (i = 0; i < n; i++)
|
||||
r->px[i] = c;
|
||||
r->dirty = 1;
|
||||
}
|
||||
|
||||
static void put(struct raster *r, int x, int y, int on)
|
||||
{
|
||||
uint32_t *p;
|
||||
if (x < 0 || x >= r->width || y < 0 || y >= r->height)
|
||||
return;
|
||||
if (r->has_clip && (x < r->clip[0] || y < r->clip[1] || x > r->clip[2] ||
|
||||
y > r->clip[3]))
|
||||
return;
|
||||
p = &r->px[y * r->width + x];
|
||||
switch (r->mode) {
|
||||
case R_OVERLAY: if (on) *p = r->color; break;
|
||||
case R_REPLACE: *p = on ? r->color : r->background; break;
|
||||
case R_COMPLEMENT: if (on) *p = (*p ^ 0x00ffffffu) | 0xff000000u; break;
|
||||
case R_ERASE: *p = r->background; break;
|
||||
}
|
||||
r->dirty = 1;
|
||||
}
|
||||
|
||||
void raster_point(struct raster *r, int x, int y)
|
||||
{
|
||||
put(r, raster_px(r, x), raster_py(r, y), 1);
|
||||
}
|
||||
|
||||
static void pixel_line(struct raster *r, int ax, int ay, int bx, int by)
|
||||
{
|
||||
int x = ax, y = ay, dx = abs(bx - ax), dy = -abs(by - ay);
|
||||
int sx = ax < bx ? 1 : -1, sy = ay < by ? 1 : -1, err = dx + dy, e2;
|
||||
for (;;) {
|
||||
put(r, x, y, (r->pattern & (0x80 >> (r->pattern_step & 7))) != 0);
|
||||
r->pattern_step++;
|
||||
if (x == bx && y == by)
|
||||
break;
|
||||
e2 = 2 * err;
|
||||
if (e2 >= dy) {
|
||||
err += dy;
|
||||
x += sx;
|
||||
}
|
||||
if (e2 <= dx) {
|
||||
err += dx;
|
||||
y += sy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void raster_line(struct raster *r, int x0, int y0, int x1, int y1)
|
||||
{
|
||||
pixel_line(r, raster_px(r, x0), raster_py(r, y0), raster_px(r, x1),
|
||||
raster_py(r, y1));
|
||||
}
|
||||
|
||||
static void pixel_arc(struct raster *r, int cx, int cy, double rx, double ry,
|
||||
double start, double sweep)
|
||||
{
|
||||
double big = rx > ry ? rx : ry;
|
||||
int steps = (int)(fabs(sweep) / 360 * (big * 2 > 8.0 ? big * 2 : 8.0) * M_PI);
|
||||
int lx = 0, ly = 0, i;
|
||||
if (steps < 16)
|
||||
steps = 16;
|
||||
if (steps > 100000)
|
||||
steps = 100000;
|
||||
for (i = 0; i <= steps; i++) {
|
||||
double a = (start + sweep * i / steps) * M_PI / 180;
|
||||
int x = cx + roundi(rx * cos(a)), y = cy - roundi(ry * sin(a));
|
||||
if (i > 0)
|
||||
pixel_line(r, lx, ly, x, y);
|
||||
lx = x;
|
||||
ly = y;
|
||||
}
|
||||
}
|
||||
|
||||
void raster_arc(struct raster *r, int cx, int cy, int rad, double start,
|
||||
double sweep)
|
||||
{
|
||||
double pr = raster_px(r, rad);
|
||||
pixel_arc(r, raster_px(r, cx), raster_py(r, cy), pr, pr, start, sweep);
|
||||
}
|
||||
|
||||
void raster_ellipse(struct raster *r, int cx, int cy, int rx, int ry,
|
||||
double start, double sweep)
|
||||
{
|
||||
pixel_arc(r, raster_px(r, cx), raster_py(r, cy),
|
||||
(double)rx * r->width / r->lw, (double)ry * r->height / r->lh,
|
||||
start, sweep);
|
||||
}
|
||||
|
||||
int raster_ellipse_points(int cx, int cy, int rx, int ry, double start,
|
||||
double sweep, int *xy, int max)
|
||||
{
|
||||
int n = (int)(fabs(sweep) / 360 * 64), i, k = 0;
|
||||
if (n < 16)
|
||||
n = 16;
|
||||
for (i = 0; i <= n && k < max; i++) {
|
||||
double a = (start + sweep * i / n) * M_PI / 180;
|
||||
xy[2 * k] = cx + roundi(rx * cos(a));
|
||||
xy[2 * k + 1] = cy - roundi(ry * sin(a));
|
||||
k++;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
static void fill_pixel(struct raster *r, int x, int y)
|
||||
{
|
||||
const uint8_t *p = r->fill_pattern;
|
||||
if (!p) {
|
||||
put(r, x, y, 1);
|
||||
return;
|
||||
}
|
||||
if ((p[y & 7] >> (7 - (x & 7))) & 1) {
|
||||
put(r, x, y, 1);
|
||||
} else if (r->fill_background) {
|
||||
uint32_t c = r->color;
|
||||
r->color = r->fill_background;
|
||||
put(r, x, y, 1);
|
||||
r->color = c;
|
||||
}
|
||||
}
|
||||
|
||||
static int cmp_int(const void *a, const void *b)
|
||||
{
|
||||
int x = *(const int *)a, y = *(const int *)b;
|
||||
return x < y ? -1 : x > y;
|
||||
}
|
||||
|
||||
void raster_fill(struct raster *r, const int *xy, int n)
|
||||
{
|
||||
int *p, *xs, top, bottom, i, y, k, x;
|
||||
if (n < 3)
|
||||
return;
|
||||
p = (int *)malloc(sizeof(int) * 2 * (size_t)n);
|
||||
xs = (int *)malloc(sizeof(int) * (size_t)n);
|
||||
if (!p || !xs) {
|
||||
free(p);
|
||||
free(xs);
|
||||
return;
|
||||
}
|
||||
top = 1 << 30;
|
||||
bottom = -(1 << 30);
|
||||
for (i = 0; i < n; i++) {
|
||||
p[2 * i] = raster_px(r, xy[2 * i]);
|
||||
p[2 * i + 1] = raster_py(r, xy[2 * i + 1]);
|
||||
top = mini(top, p[2 * i + 1]);
|
||||
bottom = maxi(bottom, p[2 * i + 1]);
|
||||
}
|
||||
top = maxi(top, 0);
|
||||
bottom = mini(bottom, r->height - 1);
|
||||
for (y = top; y <= bottom; y++) {
|
||||
int nx = 0;
|
||||
for (i = 0; i < n; i++) {
|
||||
int x0 = p[2 * i], y0 = p[2 * i + 1];
|
||||
int x1 = p[2 * ((i + 1) % n)], y1 = p[2 * ((i + 1) % n) + 1];
|
||||
if (y0 == y1)
|
||||
continue;
|
||||
if (y >= mini(y0, y1) && y < maxi(y0, y1))
|
||||
xs[nx++] = x0 + (int)((long long)(y - y0) * (x1 - x0) / (y1 - y0));
|
||||
}
|
||||
qsort(xs, nx, sizeof(int), cmp_int);
|
||||
for (k = 0; k + 1 < nx; k += 2) {
|
||||
int a = maxi(xs[k], -1), b = mini(xs[k + 1], r->width);
|
||||
for (x = a; x <= b; x++)
|
||||
fill_pixel(r, x, y);
|
||||
}
|
||||
}
|
||||
free(p);
|
||||
free(xs);
|
||||
}
|
||||
|
||||
void raster_flood(struct raster *r, int x, int y, uint32_t border)
|
||||
{
|
||||
int sx = raster_px(r, x), sy = raster_py(r, y), w = r->width, h = r->height;
|
||||
uint8_t *seen;
|
||||
int *stack, n = 0, cap;
|
||||
if (sx < 0 || sx >= w || sy < 0 || sy >= h || r->px[sy * w + sx] == border)
|
||||
return;
|
||||
seen = (uint8_t *)calloc((size_t)w * h, 1);
|
||||
cap = 4096;
|
||||
stack = (int *)malloc(sizeof(int) * (size_t)cap);
|
||||
if (!seen || !stack) {
|
||||
free(seen);
|
||||
free(stack);
|
||||
return;
|
||||
}
|
||||
#define INSIDE(i) (!seen[i] && r->px[i] != border && \
|
||||
(!r->has_clip || ((i) % w >= r->clip[0] && (i) / w >= r->clip[1] && \
|
||||
(i) % w <= r->clip[2] && (i) / w <= r->clip[3])))
|
||||
stack[n++] = sy * w + sx;
|
||||
while (n > 0) {
|
||||
int i = stack[--n], row, l, rr, xx, k;
|
||||
if (!INSIDE(i))
|
||||
continue;
|
||||
row = i / w;
|
||||
l = rr = i % w;
|
||||
while (l > 0 && INSIDE(row * w + l - 1))
|
||||
l--;
|
||||
while (rr < w - 1 && INSIDE(row * w + rr + 1))
|
||||
rr++;
|
||||
for (xx = l; xx <= rr; xx++)
|
||||
seen[row * w + xx] = 1;
|
||||
for (xx = l; xx <= rr; xx++)
|
||||
fill_pixel(r, xx, row);
|
||||
for (k = -1; k <= 1; k += 2) {
|
||||
int ny = row + k;
|
||||
if (ny < 0 || ny >= h)
|
||||
continue;
|
||||
for (xx = l; xx <= rr; xx++)
|
||||
if (INSIDE(ny * w + xx)) {
|
||||
if (n == cap) {
|
||||
int *ns;
|
||||
cap *= 2;
|
||||
ns = (int *)realloc(stack, sizeof(int) * (size_t)cap);
|
||||
if (!ns) {
|
||||
free(seen);
|
||||
free(stack);
|
||||
return;
|
||||
}
|
||||
stack = ns;
|
||||
}
|
||||
stack[n++] = ny * w + xx;
|
||||
}
|
||||
}
|
||||
}
|
||||
#undef INSIDE
|
||||
free(seen);
|
||||
free(stack);
|
||||
r->dirty = 1;
|
||||
}
|
||||
|
||||
void raster_grab(const struct raster *r, int x0, int y0, int x1, int y1,
|
||||
struct raster_grab *g)
|
||||
{
|
||||
int l = clampi(raster_px(r, mini(x0, x1)), 0, r->width - 1);
|
||||
int t = clampi(raster_py(r, mini(y0, y1)), 0, r->height - 1);
|
||||
int rr = clampi(raster_px(r, maxi(x0, x1) + 1), l + 1, r->width);
|
||||
int b = clampi(raster_py(r, maxi(y0, y1) + 1), t + 1, r->height);
|
||||
int yy;
|
||||
g->width = rr - l;
|
||||
g->height = b - t;
|
||||
g->px = (uint32_t *)malloc(sizeof(uint32_t) * (size_t)g->width * g->height);
|
||||
if (!g->px) {
|
||||
g->width = g->height = 0;
|
||||
return;
|
||||
}
|
||||
for (yy = 0; yy < g->height; yy++)
|
||||
memcpy(g->px + (size_t)yy * g->width, r->px + (size_t)(t + yy) * r->width + l,
|
||||
sizeof(uint32_t) * (size_t)g->width);
|
||||
}
|
||||
|
||||
void raster_put(struct raster *r, const struct raster_grab *g, int x, int y,
|
||||
int xor_)
|
||||
{
|
||||
int l = raster_px(r, x), t = raster_py(r, y), xx, yy;
|
||||
for (yy = 0; yy < g->height; yy++)
|
||||
for (xx = 0; xx < g->width; xx++) {
|
||||
int dx = l + xx, dy = t + yy;
|
||||
uint32_t v, *p;
|
||||
if (dx < 0 || dx >= r->width || dy < 0 || dy >= r->height)
|
||||
continue;
|
||||
if (r->has_clip && (dx < r->clip[0] || dy < r->clip[1] ||
|
||||
dx > r->clip[2] || dy > r->clip[3]))
|
||||
continue;
|
||||
v = g->px[yy * g->width + xx];
|
||||
p = &r->px[dy * r->width + dx];
|
||||
*p = xor_ ? (*p ^ v) | ((*p | v) ? 0xff000000u : 0) : v;
|
||||
}
|
||||
r->dirty = 1;
|
||||
}
|
||||
|
||||
void raster_pixel_rect(const struct raster *r, int x0, int y0, int x1, int y1,
|
||||
int out[4])
|
||||
{
|
||||
out[0] = raster_px(r, mini(x0, x1));
|
||||
out[1] = raster_py(r, mini(y0, y1));
|
||||
out[2] = raster_px(r, maxi(x0, x1) + 1) - 1;
|
||||
out[3] = raster_py(r, maxi(y0, y1) + 1) - 1;
|
||||
}
|
||||
|
||||
void raster_clear_rect(struct raster *r, int x0, int y0, int x1, int y1,
|
||||
uint32_t c)
|
||||
{
|
||||
int q[4], xx, yy;
|
||||
raster_pixel_rect(r, x0, y0, x1, y1, q);
|
||||
for (yy = maxi(0, q[1]); yy <= mini(r->height - 1, q[3]); yy++)
|
||||
for (xx = maxi(0, q[0]); xx <= mini(r->width - 1, q[2]); xx++)
|
||||
r->px[yy * r->width + xx] = c;
|
||||
r->dirty = 1;
|
||||
}
|
||||
|
||||
static void glyph(struct raster *r, uint32_t cp, int x, int y,
|
||||
const struct vt_font *f, int cw, int ch)
|
||||
{
|
||||
int left, top, w, h, xx, yy;
|
||||
if (font_index(f, cp) < 0)
|
||||
return;
|
||||
left = raster_px(r, x);
|
||||
top = raster_py(r, y);
|
||||
w = maxi(1, raster_px(r, x + cw) - left);
|
||||
h = maxi(1, raster_py(r, y + ch) - top);
|
||||
for (yy = 0; yy < h; yy++) {
|
||||
/* (shrunk: the rows it covers merged, so a thin stroke stays) */
|
||||
int r0 = yy * f->h / h, r1 = maxi(r0 + 1, (yy + 1) * f->h / h), k;
|
||||
uint32_t row = 0;
|
||||
for (k = r0; k < r1; k++)
|
||||
row |= font_row(f, cp, k);
|
||||
if (!row)
|
||||
continue;
|
||||
for (xx = 0; xx < w; xx++) {
|
||||
int c0 = xx * f->w / w, c1 = maxi(c0 + 1, (xx + 1) * f->w / w), on = 0;
|
||||
for (k = c0; k < c1; k++)
|
||||
if ((row >> (f->w - 1 - k)) & 1)
|
||||
on = 1;
|
||||
if (on)
|
||||
put(r, left + xx, top + yy, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int raster_text(struct raster *r, int x, int y, const uint32_t *cps, int n,
|
||||
const struct vt_font *f, int cw, int ch)
|
||||
{
|
||||
int i;
|
||||
if (!f)
|
||||
return x + n * cw;
|
||||
for (i = 0; i < n; i++) {
|
||||
glyph(r, cps[i], x, y, f, cw, ch);
|
||||
x += cw;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
struct vt_image *raster_take(dmc_vt *t, struct raster *r, int cw, int ch)
|
||||
{
|
||||
struct vt_image *img = image_new(r->width, r->height, cw, ch, 1);
|
||||
(void)t;
|
||||
memcpy(img->pixels, r->px, sizeof(uint32_t) * (size_t)r->width * r->height);
|
||||
r->dirty = 0;
|
||||
return img;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* the picture layer */
|
||||
|
||||
static int pixel_w(const dmc_vt *t)
|
||||
{
|
||||
return maxi(1, t->cols * t->cell_pw);
|
||||
}
|
||||
|
||||
static int pixel_h(const dmc_vt *t)
|
||||
{
|
||||
return maxi(1, t->rows * t->cell_ph);
|
||||
}
|
||||
|
||||
static struct raster *make(struct picture_layer *l)
|
||||
{
|
||||
struct raster *r = raster_new(pixel_w(l->t), pixel_h(l->t), l->lw, l->lh);
|
||||
if (r && l->setup)
|
||||
l->setup(r, l->ctx);
|
||||
return r;
|
||||
}
|
||||
|
||||
void layer_init(struct picture_layer *l, dmc_vt *t, int lw, int lh,
|
||||
void (*setup)(struct raster *, void *), void *ctx)
|
||||
{
|
||||
l->t = t;
|
||||
l->lw = lw;
|
||||
l->lh = lh;
|
||||
l->setup = setup;
|
||||
l->ctx = ctx;
|
||||
l->r = make(l);
|
||||
}
|
||||
|
||||
void layer_reset(struct picture_layer *l)
|
||||
{
|
||||
struct raster *r = make(l);
|
||||
if (!r)
|
||||
return;
|
||||
raster_free(l->r);
|
||||
l->r = r;
|
||||
raster_clear(r, 0);
|
||||
}
|
||||
|
||||
struct raster *layer_raster(struct picture_layer *l)
|
||||
{
|
||||
if (!l->r || pixel_w(l->t) != l->r->width || pixel_h(l->t) != l->r->height)
|
||||
layer_reset(l);
|
||||
return l->r;
|
||||
}
|
||||
|
||||
void layer_flush(struct picture_layer *l)
|
||||
{
|
||||
struct raster *r = layer_raster(l);
|
||||
if (r && r->dirty)
|
||||
vt_show_picture(l->t, raster_take(l->t, r, l->t->cell_pw, l->t->cell_ph));
|
||||
}
|
||||
|
||||
void layer_free(struct picture_layer *l)
|
||||
{
|
||||
raster_free(l->r);
|
||||
l->r = NULL;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* the Tektronix 4010/4014 */
|
||||
|
||||
#define TEK_W 4096
|
||||
#define TEK_H 3120
|
||||
#define TEK_STEP 4
|
||||
#define TEK_INK 0xffc8ffc8u
|
||||
|
||||
enum { TM_ALPHA, TM_GRAPH, TM_POINT, TM_INCREMENTAL };
|
||||
|
||||
static const int char_w[4] = { 56, 51, 34, 31 };
|
||||
static const int char_h[4] = { 88, 82, 53, 48 };
|
||||
static const int styles[8] = { 0xff, 0xaa, 0xe4, 0xcc, 0xf0, 0xff, 0xff, 0xff };
|
||||
|
||||
struct tek {
|
||||
struct vt_interp in;
|
||||
void (*on_exit)(void *);
|
||||
void *ctx;
|
||||
struct picture_layer layer;
|
||||
int mode, x, y, dark, pen, size, escaped;
|
||||
int hi_x, hi_y, lo_x, lo_y, extra, last_lo_y;
|
||||
};
|
||||
|
||||
static void tek_setup(struct raster *r, void *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
r->color = TEK_INK;
|
||||
}
|
||||
|
||||
static void tek_home(struct tek *k)
|
||||
{
|
||||
k->mode = TM_ALPHA;
|
||||
k->x = 0;
|
||||
k->y = TEK_H - char_h[k->size];
|
||||
}
|
||||
|
||||
static int row(int ty)
|
||||
{
|
||||
return TEK_H - 1 - ty;
|
||||
}
|
||||
|
||||
static void tek_process(struct vt_interp *in, int32_t cp);
|
||||
|
||||
static void tek_report(struct tek *k)
|
||||
{
|
||||
int sx = k->x >> 2, sy = k->y >> 2;
|
||||
char s[8];
|
||||
s[0] = (char)(k->mode == TM_ALPHA ? 0x34 : 0x30);
|
||||
s[1] = (char)(0x20 | ((sx >> 5) & 0x1f));
|
||||
s[2] = (char)(0x20 | (sx & 0x1f));
|
||||
s[3] = (char)(0x20 | ((sy >> 5) & 0x1f));
|
||||
s[4] = (char)(0x20 | (sy & 0x1f));
|
||||
s[5] = '\r';
|
||||
s[6] = 0;
|
||||
vt_reply(k->in.t, s);
|
||||
}
|
||||
|
||||
static void tek_esc(struct tek *k, int c)
|
||||
{
|
||||
if (c == 0x0c) {
|
||||
raster_clear(layer_raster(&k->layer), 0);
|
||||
tek_home(k);
|
||||
} else if (c == 0x05) {
|
||||
tek_report(k);
|
||||
} else if (c >= '8' && c <= ';') {
|
||||
k->size = c - '8';
|
||||
} else if (c >= 0x60 && c <= 0x67) {
|
||||
layer_raster(&k->layer)->pattern = styles[(c - 0x60) & 7];
|
||||
} else if (c == 0x03) {
|
||||
if (k->on_exit)
|
||||
k->on_exit(k->ctx);
|
||||
} else if (c < 0x20) {
|
||||
tek_process(&k->in, c);
|
||||
}
|
||||
}
|
||||
|
||||
static void tek_alpha(struct tek *k, int c)
|
||||
{
|
||||
int cw = char_w[k->size], ch = char_h[k->size];
|
||||
uint32_t cp = (uint32_t)c;
|
||||
switch (c) {
|
||||
case 0x08: k->x = maxi(k->x - cw, 0); break;
|
||||
case 0x09: k->x += cw; break;
|
||||
case 0x0a:
|
||||
k->y -= ch;
|
||||
if (k->y < 0)
|
||||
k->y = TEK_H - ch;
|
||||
break;
|
||||
case 0x0b: k->y = mini(k->y + ch, TEK_H - ch); break;
|
||||
default:
|
||||
if (c < 0x20 || c > 0x7e)
|
||||
break;
|
||||
if (k->x + cw > TEK_W) {
|
||||
k->x = 0;
|
||||
k->y -= ch;
|
||||
if (k->y < 0)
|
||||
k->y = TEK_H - ch;
|
||||
}
|
||||
if (c != 0x20)
|
||||
raster_text(layer_raster(&k->layer), k->x, row(k->y + ch), &cp, 1,
|
||||
&vt_font_vga, cw, ch);
|
||||
k->x += cw;
|
||||
}
|
||||
}
|
||||
|
||||
static void tek_to(struct tek *k, int nx, int ny)
|
||||
{
|
||||
if (k->mode == TM_POINT)
|
||||
raster_point(layer_raster(&k->layer), nx, row(ny));
|
||||
else if (k->dark)
|
||||
k->dark = 0;
|
||||
else
|
||||
raster_line(layer_raster(&k->layer), k->x, row(k->y), nx, row(ny));
|
||||
k->x = nx;
|
||||
k->y = ny;
|
||||
}
|
||||
|
||||
static void tek_address(struct tek *k, int c)
|
||||
{
|
||||
int v = c & 0x1f, nx, ny;
|
||||
switch (c >> 5) {
|
||||
case 1:
|
||||
if (k->last_lo_y)
|
||||
k->hi_x = v;
|
||||
else
|
||||
k->hi_y = v;
|
||||
k->last_lo_y = 0;
|
||||
break;
|
||||
case 3:
|
||||
if (k->last_lo_y)
|
||||
k->extra = k->lo_y;
|
||||
k->lo_y = v;
|
||||
k->last_lo_y = 1;
|
||||
break;
|
||||
case 2:
|
||||
k->lo_x = v;
|
||||
k->last_lo_y = 0;
|
||||
nx = (k->hi_x << 7) | (k->lo_x << 2) | (k->extra & 3);
|
||||
ny = (k->hi_y << 7) | (k->lo_y << 2) | ((k->extra >> 2) & 3);
|
||||
k->extra = 0;
|
||||
tek_to(k, nx, ny);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void tek_incremental(struct tek *k, int c)
|
||||
{
|
||||
int dx, dy, nx, ny;
|
||||
switch (c) {
|
||||
case ' ': k->pen = 0; return;
|
||||
case 'P': k->pen = 1; return;
|
||||
case 'D': dx = 0; dy = 1; break;
|
||||
case 'E': dx = 1; dy = 1; break;
|
||||
case 'A': dx = 1; dy = 0; break;
|
||||
case 'I': dx = 1; dy = -1; break;
|
||||
case 'H': dx = 0; dy = -1; break;
|
||||
case 'J': dx = -1; dy = -1; break;
|
||||
case 'B': dx = -1; dy = 0; break;
|
||||
case 'F': dx = -1; dy = 1; break;
|
||||
default: return;
|
||||
}
|
||||
nx = clampi(k->x + dx * TEK_STEP, 0, TEK_W - 1);
|
||||
ny = clampi(k->y + dy * TEK_STEP, 0, TEK_H - 1);
|
||||
if (k->pen)
|
||||
raster_line(layer_raster(&k->layer), k->x, row(k->y), nx, row(ny));
|
||||
k->x = nx;
|
||||
k->y = ny;
|
||||
}
|
||||
|
||||
static void tek_process(struct vt_interp *in, int32_t cp)
|
||||
{
|
||||
struct tek *k = (struct tek *)in;
|
||||
int c = cp & 0x7f;
|
||||
if (k->escaped) {
|
||||
k->escaped = 0;
|
||||
tek_esc(k, c);
|
||||
return;
|
||||
}
|
||||
switch (c) {
|
||||
case 0x1b: k->escaped = 1; break;
|
||||
case 0x1d: k->mode = TM_GRAPH; k->dark = 1; break;
|
||||
case 0x1c: k->mode = TM_POINT; break;
|
||||
case 0x1e: k->mode = TM_INCREMENTAL; k->pen = 0; break;
|
||||
case 0x1f: k->mode = TM_ALPHA; break;
|
||||
case 0x07: vt_bell(in->t); break;
|
||||
case 0x0d: k->mode = TM_ALPHA; k->x = 0; break;
|
||||
default:
|
||||
switch (k->mode) {
|
||||
case TM_ALPHA: tek_alpha(k, c); break;
|
||||
case TM_GRAPH: case TM_POINT: if (c >= 0x20) tek_address(k, c); break;
|
||||
case TM_INCREMENTAL: tek_incremental(k, c); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void tek_flush(struct vt_interp *in)
|
||||
{
|
||||
layer_flush(&((struct tek *)in)->layer);
|
||||
}
|
||||
|
||||
static void tek_reset(struct vt_interp *in)
|
||||
{
|
||||
struct tek *k = (struct tek *)in;
|
||||
layer_reset(&k->layer);
|
||||
k->size = 0;
|
||||
k->escaped = 0;
|
||||
tek_home(k);
|
||||
}
|
||||
|
||||
static void tek_destroy(struct vt_interp *in)
|
||||
{
|
||||
struct tek *k = (struct tek *)in;
|
||||
layer_free(&k->layer);
|
||||
free(k);
|
||||
}
|
||||
|
||||
static const struct vt_interp_ops tek_ops = {
|
||||
tek_process, tek_reset, NULL, NULL, NULL, NULL, NULL, NULL, tek_flush,
|
||||
NULL, tek_destroy
|
||||
};
|
||||
|
||||
struct vt_interp *tek_new(dmc_vt *t, void (*on_exit)(void *), void *ctx)
|
||||
{
|
||||
struct tek *k = (struct tek *)calloc(1, sizeof(*k));
|
||||
if (!k)
|
||||
return NULL;
|
||||
k->in.ops = &tek_ops;
|
||||
k->in.t = t;
|
||||
k->on_exit = on_exit;
|
||||
k->ctx = ctx;
|
||||
layer_init(&k->layer, t, TEK_W, TEK_H, tek_setup, NULL);
|
||||
tek_home(k);
|
||||
return &k->in;
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* vt_keys.c - keys as the octets a VT220 or xterm sends: the cursor keys
|
||||
* in normal or application mode, xterm's modifier parameters (ESC[1;5A
|
||||
* for Ctrl+Up), the DEC keypad, a VT52's, Ctrl and Alt on characters.
|
||||
* A port of the app's KeyEncoder.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "vtint.h"
|
||||
|
||||
static const char *enter_bytes(const dmc_vt *t)
|
||||
{
|
||||
switch (t->enter_mode) {
|
||||
case 1: return "\r\n";
|
||||
case 2: return "\n";
|
||||
}
|
||||
return "\r";
|
||||
}
|
||||
|
||||
/* a keypad key's (NULL: not one): PF1-PF4 as SS3 P-S (a VT52's ESC
|
||||
P-S); the others their characters in numeric mode, SS3 and a letter in
|
||||
application mode (a VT52's ESC ? and the letter) */
|
||||
static int keypad(const dmc_vt *t, int k, dmc_buf *out)
|
||||
{
|
||||
int app = t->app_keypad, vt52 = t->vt52_keys, letter;
|
||||
char s[8];
|
||||
const char *numeric;
|
||||
char digit[2];
|
||||
if (k >= DMC_KEY_PF1 && k <= DMC_KEY_PF4) {
|
||||
snprintf(s, sizeof(s), vt52 ? "\x1b%c" : "\x1bO%c",
|
||||
'P' + (k - DMC_KEY_PF1));
|
||||
buf_adds(out, s);
|
||||
return 1;
|
||||
}
|
||||
if (k >= DMC_KEY_KP_0 && k <= DMC_KEY_KP_9) {
|
||||
digit[0] = (char)('0' + (k - DMC_KEY_KP_0));
|
||||
digit[1] = 0;
|
||||
numeric = digit;
|
||||
letter = 'p' + (k - DMC_KEY_KP_0);
|
||||
} else if (k == DMC_KEY_KP_MINUS) {
|
||||
numeric = "-";
|
||||
letter = 'm';
|
||||
} else if (k == DMC_KEY_KP_COMMA) {
|
||||
numeric = ",";
|
||||
letter = 'l';
|
||||
} else if (k == DMC_KEY_KP_PERIOD) {
|
||||
numeric = ".";
|
||||
letter = 'n';
|
||||
} else if (k == DMC_KEY_KP_ENTER) {
|
||||
numeric = enter_bytes(t);
|
||||
letter = 'M';
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
if (!app) {
|
||||
buf_adds(out, numeric);
|
||||
return 1;
|
||||
}
|
||||
snprintf(s, sizeof(s), vt52 ? "\x1b?%c" : "\x1bO%c", letter);
|
||||
buf_adds(out, s);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char *vt52_key(int k)
|
||||
{
|
||||
switch (k) {
|
||||
case DMC_KEY_UP: return "\x1b" "A";
|
||||
case DMC_KEY_DOWN: return "\x1b" "B";
|
||||
case DMC_KEY_RIGHT: return "\x1b" "C";
|
||||
case DMC_KEY_LEFT: return "\x1b" "D";
|
||||
case DMC_KEY_F1: return "\x1bP";
|
||||
case DMC_KEY_F2: return "\x1bQ";
|
||||
case DMC_KEY_F3: return "\x1bR";
|
||||
case DMC_KEY_F4: return "\x1bS";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* a CSI n ~ key's number, 0 for none */
|
||||
static int tilde_number(int k)
|
||||
{
|
||||
switch (k) {
|
||||
case DMC_KEY_INSERT: return 2;
|
||||
case DMC_KEY_DELETE: return 3;
|
||||
case DMC_KEY_PGUP: return 5;
|
||||
case DMC_KEY_PGDN: return 6;
|
||||
case DMC_KEY_F5: return 15;
|
||||
case DMC_KEY_F6: return 17;
|
||||
case DMC_KEY_F7: return 18;
|
||||
case DMC_KEY_F8: return 19;
|
||||
case DMC_KEY_F9: return 20;
|
||||
case DMC_KEY_F10: return 21;
|
||||
case DMC_KEY_F11: return 23;
|
||||
case DMC_KEY_F12: return 24;
|
||||
case DMC_KEY_F13: return 25;
|
||||
case DMC_KEY_F14: return 26;
|
||||
case DMC_KEY_F15: return 28;
|
||||
case DMC_KEY_F16: return 29;
|
||||
case DMC_KEY_F17: return 31;
|
||||
case DMC_KEY_F18: return 32;
|
||||
case DMC_KEY_F19: return 33;
|
||||
case DMC_KEY_F20: return 34;
|
||||
case DMC_KEY_FIND: return 1;
|
||||
case DMC_KEY_SELECT: return 4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void keys_key(const dmc_vt *t, int k, int mods, dmc_buf *out)
|
||||
{
|
||||
int shift = mods & M_SHIFT, alt = mods & M_ALT, ctrl = mods & M_CTRL;
|
||||
int m = 1 + (shift ? 1 : 0) + (alt ? 2 : 0) + (ctrl ? 4 : 0), n;
|
||||
const char *s;
|
||||
char b[24];
|
||||
int cursor = 0, ss3 = 0;
|
||||
if (keypad(t, k, out))
|
||||
return;
|
||||
if (t->vt52_keys && (s = vt52_key(k)) != NULL) {
|
||||
buf_adds(out, s);
|
||||
return;
|
||||
}
|
||||
switch (k) {
|
||||
case DMC_KEY_UP: cursor = 'A'; break;
|
||||
case DMC_KEY_DOWN: cursor = 'B'; break;
|
||||
case DMC_KEY_RIGHT: cursor = 'C'; break;
|
||||
case DMC_KEY_LEFT: cursor = 'D'; break;
|
||||
case DMC_KEY_HOME: cursor = 'H'; break;
|
||||
case DMC_KEY_END: cursor = 'F'; break;
|
||||
case DMC_KEY_F1: ss3 = 'P'; break;
|
||||
case DMC_KEY_F2: ss3 = 'Q'; break;
|
||||
case DMC_KEY_F3: ss3 = 'R'; break;
|
||||
case DMC_KEY_F4: ss3 = 'S'; break;
|
||||
}
|
||||
if (cursor) {
|
||||
if (m > 1)
|
||||
snprintf(b, sizeof(b), "\x1b[1;%d%c", m, cursor);
|
||||
else if (t->app_cursor)
|
||||
snprintf(b, sizeof(b), "\x1bO%c", cursor);
|
||||
else
|
||||
snprintf(b, sizeof(b), "\x1b[%c", cursor);
|
||||
buf_adds(out, b);
|
||||
return;
|
||||
}
|
||||
if (ss3) {
|
||||
if (m > 1)
|
||||
snprintf(b, sizeof(b), "\x1b[1;%d%c", m, ss3);
|
||||
else
|
||||
snprintf(b, sizeof(b), "\x1bO%c", ss3);
|
||||
buf_adds(out, b);
|
||||
return;
|
||||
}
|
||||
n = tilde_number(k);
|
||||
if (n) {
|
||||
if (m > 1)
|
||||
snprintf(b, sizeof(b), "\x1b[%d;%d~", n, m);
|
||||
else
|
||||
snprintf(b, sizeof(b), "\x1b[%d~", n);
|
||||
buf_adds(out, b);
|
||||
return;
|
||||
}
|
||||
switch (k) {
|
||||
case DMC_KEY_TAB:
|
||||
if (shift) {
|
||||
buf_adds(out, "\x1b[Z");
|
||||
return;
|
||||
}
|
||||
if (alt)
|
||||
buf_addc(out, 0x1b);
|
||||
buf_addc(out, '\t');
|
||||
return;
|
||||
case DMC_KEY_BACKTAB:
|
||||
buf_adds(out, "\x1b[Z");
|
||||
return;
|
||||
case DMC_KEY_ENTER:
|
||||
if (alt)
|
||||
buf_addc(out, 0x1b);
|
||||
buf_adds(out, enter_bytes(t));
|
||||
return;
|
||||
case DMC_KEY_BACKSPACE:
|
||||
if (alt)
|
||||
buf_addc(out, 0x1b);
|
||||
buf_addc(out, (char)(ctrl ? 0x08 : t->backspace_code));
|
||||
return;
|
||||
case DMC_KEY_ESCAPE:
|
||||
if (alt)
|
||||
buf_addc(out, 0x1b);
|
||||
buf_addc(out, 0x1b);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void keys_char(uint32_t cp, int mods, dmc_buf *out)
|
||||
{
|
||||
uint32_t c = cp;
|
||||
if (mods & M_CTRL) {
|
||||
if (c >= 'a' && c <= 'z')
|
||||
c = c - 'a' + 1;
|
||||
else if (c >= '@' && c <= '_')
|
||||
c = c - '@';
|
||||
else if (c == ' ' || c == '2')
|
||||
c = 0;
|
||||
else if (c == '3')
|
||||
c = 0x1b;
|
||||
else if (c == '4')
|
||||
c = 0x1c;
|
||||
else if (c == '5')
|
||||
c = 0x1d;
|
||||
else if (c == '6')
|
||||
c = 0x1e;
|
||||
else if (c == '7' || c == '/')
|
||||
c = 0x1f;
|
||||
else if (c == '8' || c == '?')
|
||||
c = 0x7f;
|
||||
}
|
||||
if (mods & M_ALT)
|
||||
buf_addc(out, 0x1b);
|
||||
utf8_put(out, c);
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* vt_profiles.c - the kinds of terminal there are (the app's
|
||||
* TerminalProfiles): what each calls itself to SSH, Telnet and the
|
||||
* programs that ask, its answers, its machine's screen, palette and
|
||||
* font, and the interpreter that reads it.
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "vtint.h"
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* the palettes (opaque ARGB) */
|
||||
|
||||
/* an Amiga's ANSI colours, from its 4-bit levels */
|
||||
static const uint32_t amiga_colors[16] = {
|
||||
0xff000000, 0xffcc2222, 0xff22cc22, 0xffcccc22,
|
||||
0xff2222cc, 0xffcc22cc, 0xff22cccc, 0xffcccccc,
|
||||
0xff666666, 0xffff5555, 0xff55ff55, 0xffffff55,
|
||||
0xff5555ff, 0xffff55ff, 0xff55ffff, 0xffffffff
|
||||
};
|
||||
|
||||
/* Philip Timmermann's "Pepto" measurements of the VIC-II */
|
||||
static const uint32_t pepto[16] = {
|
||||
0xff000000, 0xffffffff, 0xff68372b, 0xff70a4b2,
|
||||
0xff6f3d86, 0xff588d43, 0xff352879, 0xffb8c76f,
|
||||
0xff6f4f25, 0xff433900, 0xff9a6759, 0xff444444,
|
||||
0xff6c6c6c, 0xff9ad284, 0xff6c5eb5, 0xff959595
|
||||
};
|
||||
|
||||
/* the VIC-20's: eight, then the lighter ones */
|
||||
static const uint32_t vic20[16] = {
|
||||
0xff000000, 0xffffffff, 0xffb61f21, 0xff4df0ff,
|
||||
0xffb43fff, 0xff44e237, 0xff1a34ff, 0xffdcd71b,
|
||||
0xffca5400, 0xffe9b072, 0xffe79293, 0xff9af7fd,
|
||||
0xffe09fff, 0xff8fe493, 0xff8290ff, 0xffe5de85
|
||||
};
|
||||
|
||||
/* the C128's VDC (RGBI), each VIC-II colour as the 80-column screen
|
||||
shows it */
|
||||
static const uint32_t vdc[16] = {
|
||||
0xff000000, 0xffffffff, 0xffaa0000, 0xff55ffff,
|
||||
0xffff55ff, 0xff00aa00, 0xff0000aa, 0xffffff55,
|
||||
0xffaa00aa, 0xffaa5500, 0xffff5555, 0xff555555,
|
||||
0xff00aaaa, 0xff55ff55, 0xff5555ff, 0xffaaaaaa
|
||||
};
|
||||
|
||||
/* the Plus/4's TED */
|
||||
static const uint32_t ted[16] = {
|
||||
0xff000000, 0xffffffff, 0xff9c3e3e, 0xff5ab5b5,
|
||||
0xff9f4ba8, 0xff5aa33e, 0xff5250c4, 0xffb5b54a,
|
||||
0xffa36a2b, 0xff7d5a1b, 0xff85a33e, 0xffc7637a,
|
||||
0xff3ea383, 0xff6a8cd9, 0xff4f5fb3, 0xff7ec26b
|
||||
};
|
||||
|
||||
/* a PET's phosphor */
|
||||
static const uint32_t pet_green[2] = { 0xff000000, 0xff41ff00 };
|
||||
|
||||
/* an Atari 8-bit's blue and light text */
|
||||
static const uint32_t atari_blue[2] = { 0xff1d5dae, 0xffa8d8ff };
|
||||
|
||||
/* TOS's sixteen at power-up (3-bit levels) */
|
||||
static const uint32_t tos[16] = {
|
||||
0xffffffff, 0xffff0000, 0xff00ff00, 0xffffff00,
|
||||
0xff0000ff, 0xffff00ff, 0xff00ffff, 0xffb6b6b6,
|
||||
0xff6d6d6d, 0xffff6d6d, 0xff6dff6d, 0xffffff6d,
|
||||
0xff6d6dff, 0xffff6dff, 0xff6dffff, 0xff000000
|
||||
};
|
||||
/* the ST's high resolution colour mode: TOS's first three and black */
|
||||
static const uint32_t tos_med[4] = {
|
||||
0xffffffff, 0xffff0000, 0xff00ff00, 0xff000000
|
||||
};
|
||||
|
||||
/* teletext's eight */
|
||||
static const uint32_t teletext[8] = {
|
||||
0xff000000, 0xffff0000, 0xff00ff00, 0xffffff00,
|
||||
0xff0000ff, 0xffff00ff, 0xff00ffff, 0xffffffff
|
||||
};
|
||||
|
||||
/* a Commodore's CTRL 1-8 and C= 1-8 */
|
||||
static const uint8_t cbm_ctrl[8] = {
|
||||
0x90, 0x05, 0x1c, 0x9f, 0x9c, 0x1e, 0x1f, 0x9e
|
||||
};
|
||||
static const uint8_t cbm_cbm[8] = {
|
||||
0x81, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* the profiles (in the struct's order: no designated initialisers, for
|
||||
older compilers) */
|
||||
|
||||
/* a DEC terminal */
|
||||
#define DEC(id, label, term, telnet, level, features, da1, da2, home) \
|
||||
{ id, label, term, telnet, K_DEC, level, features, da1, da2, -1, \
|
||||
home, MF_NONE, 0, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, \
|
||||
0, 0, 0, 0, 0, NULL, NULL }
|
||||
|
||||
#define XTERM_FEATURES (F_PRINTER | F_COLOR | F_SIXEL | F_XTERM | F_TEK)
|
||||
#define XTERM_DA1 "\x1b[?62;4;22c"
|
||||
#define XTERM_DA2 "\x1b[>41;390;0c"
|
||||
|
||||
/* a BBS terminal: xterm with CTerm's additions */
|
||||
#define BBS(id, label, term, kind, charset, font, syncterm, colors, n) \
|
||||
{ id, label, term, "ANSI", kind, 2, XTERM_FEATURES, XTERM_DA1, \
|
||||
XTERM_DA2, charset, 1, font, syncterm, colors, n, 0, 0, 0, 0, \
|
||||
NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL }
|
||||
|
||||
/* a Commodore */
|
||||
#define CBM(id, label, font, pal, cols, rows, fg, up, lo, nctrl, ncbm, \
|
||||
esc, attr, tablf) \
|
||||
{ id, label, "petscii", "PETSCII", K_PETSCII, 0, 0, NULL, NULL, \
|
||||
-1, 0, font, 0, pal, (int)(sizeof(pal) / sizeof(pal[0])), cols, \
|
||||
rows, fg, 0, up, lo, cbm_ctrl, cbm_cbm, nctrl, ncbm, esc, attr, \
|
||||
tablf, NULL, ".seq" }
|
||||
|
||||
/* another machine */
|
||||
#define MACH(id, label, term, telnet, kind, font, pal, cols, rows, fg, \
|
||||
atascii, ext) \
|
||||
{ id, label, term, telnet, kind, 0, 0, NULL, NULL, -1, 0, font, \
|
||||
0, pal, (int)(sizeof(pal) / sizeof(pal[0])), cols, rows, fg, 0, \
|
||||
NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, atascii, ext }
|
||||
|
||||
static const struct vt_profile xterm = DEC("xterm", "xterm",
|
||||
"xterm-256color", "XTERM-256COLOR", 2, XTERM_FEATURES, XTERM_DA1,
|
||||
XTERM_DA2, 1);
|
||||
static const struct vt_profile ansi_bbs = BBS("ansi-bbs", "ANSI-BBS (PC)",
|
||||
"ansi", K_ANSI_BBS, CS_CP437, MF_VGA, 0, NULL, 0);
|
||||
static const struct vt_profile rip = BBS("rip", "RIPscrip", "rip", K_RIP,
|
||||
CS_CP437, MF_RIPTERM, 45, NULL, 0);
|
||||
static const struct vt_profile avatar = BBS("avatar", "Avatar (AVT/0+)",
|
||||
"avatar", K_AVATAR, CS_CP437, MF_VGA, 0, NULL, 0);
|
||||
static const struct vt_profile amiga = BBS("amiga", "Amiga ANSI", "amiga",
|
||||
K_ANSI_BBS, CS_LATIN1, MF_TOPAZ, 42, amiga_colors, 16);
|
||||
|
||||
static const struct vt_profile vt52 = {
|
||||
"vt52", "DEC VT52", "vt52", "DEC-VT52", K_VT52, 0, 0, NULL, NULL, -1,
|
||||
0, MF_NONE, 0, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, 0, 0, 0,
|
||||
0, 0, NULL, NULL
|
||||
};
|
||||
const struct vt_profile vt_profile_vt100 = DEC("vt100", "DEC VT100",
|
||||
"vt100", "DEC-VT100", 1, 0, "\x1b[?1;2c", NULL, 0);
|
||||
static const struct vt_profile vt102 = DEC("vt102", "DEC VT102", "vt102",
|
||||
"DEC-VT102", 1, F_PRINTER, "\x1b[?6c", NULL, 0);
|
||||
static const struct vt_profile vt220 = DEC("vt220", "DEC VT220", "vt220",
|
||||
"DEC-VT220", 2, F_PRINTER, "\x1b[?62;1;2;6;7;8;9c", "\x1b[>1;10;0c", 0);
|
||||
static const struct vt_profile vt320 = DEC("vt320", "DEC VT320", "vt320",
|
||||
"DEC-VT320", 3, F_PRINTER, "\x1b[?63;1;2;6;7;8;9c", "\x1b[>24;10;0c",
|
||||
0);
|
||||
static const struct vt_profile vt340 = DEC("vt340", "DEC VT340", "vt340",
|
||||
"DEC-VT340", 3, F_PRINTER | F_SIXEL | F_REGIS | F_TEK,
|
||||
"\x1b[?63;1;2;3;4;6;7;8;9c", "\x1b[>19;10;0c", 0);
|
||||
static const struct vt_profile vt420 = DEC("vt420", "DEC VT420", "vt420",
|
||||
"DEC-VT420", 4, F_PRINTER, "\x1b[?64;1;2;6;7;8;9;15;18;21;28c",
|
||||
"\x1b[>41;10;0c", 0);
|
||||
static const struct vt_profile vt510 = DEC("vt510", "DEC VT510", "vt510",
|
||||
"DEC-VT510", 5, F_PRINTER, "\x1b[?65;1;2;6;7;8;9;15;18;21;28c",
|
||||
"\x1b[>61;10;0c", 0);
|
||||
static const struct vt_profile vt520 = DEC("vt520", "DEC VT520", "vt520",
|
||||
"DEC-VT520", 5, F_PRINTER, "\x1b[?65;1;2;6;7;8;9;15;18;21;28c",
|
||||
"\x1b[>64;10;0c", 0);
|
||||
static const struct vt_profile vt525 = DEC("vt525", "DEC VT525", "vt525",
|
||||
"DEC-VT525", 5, F_PRINTER | F_COLOR,
|
||||
"\x1b[?65;1;2;6;7;8;9;15;18;21;22;28c", "\x1b[>65;10;0c", 0);
|
||||
|
||||
static const struct vt_profile c64 = CBM("c64", "Commodore 64", MF_C64,
|
||||
pepto, 40, 25, 14, vt_set_c64_upper, vt_set_c64_lower, 8, 8, 0,
|
||||
AC_NONE, 0);
|
||||
static const struct vt_profile c128 = CBM("c128",
|
||||
"Commodore 128 (40 col.)", MF_C128, pepto, 40, 25, 14,
|
||||
vt_set_c64_upper, vt_set_c64_lower, 8, 8, 1, AC_NONE, 1);
|
||||
static const struct vt_profile c128_80 = CBM("c128-80",
|
||||
"Commodore 128 (80 col.)", MF_C128, vdc, 80, 25, 14,
|
||||
vt_set_c64_upper, vt_set_c64_lower, 8, 8, 1, AC_VDC, 1);
|
||||
static const struct vt_profile plus4 = CBM("plus4", "Commodore Plus/4",
|
||||
MF_C64, ted, 40, 25, 14, vt_set_c64_upper, vt_set_c64_lower, 8, 8, 1,
|
||||
AC_PLUS4, 0);
|
||||
static const struct vt_profile vic20p = CBM("vic20", "Commodore VIC-20",
|
||||
MF_C64, vic20, 22, 23, 1, vt_set_vic_upper, vt_set_vic_lower, 8, 0, 0,
|
||||
AC_NONE, 0);
|
||||
static const struct vt_profile pet = CBM("pet", "Commodore PET (40 col.)",
|
||||
MF_C64, pet_green, 40, 25, 1, vt_set_pet_upper, vt_set_pet_lower, 0, 0,
|
||||
0, AC_NONE, 0);
|
||||
static const struct vt_profile pet80 = CBM("pet80",
|
||||
"Commodore PET (80 col.)", MF_C64, pet_green, 80, 25, 1,
|
||||
vt_set_pet_upper, vt_set_pet_lower, 0, 0, 0, AC_NONE, 0);
|
||||
|
||||
static const struct vt_profile atari = MACH("atari", "Atari 8-bit",
|
||||
"atascii", "ATASCII", K_ATASCII, MF_ATARI, atari_blue, 40, 24, 1,
|
||||
vt_set_atari_graphics, NULL);
|
||||
static const struct vt_profile atari_intl = MACH("atari-intl",
|
||||
"Atari 8-bit (international)", "atascii", "ATASCII", K_ATASCII,
|
||||
MF_ATARI, atari_blue, 40, 24, 1, vt_set_atari_international, NULL);
|
||||
static const struct vt_profile atari_st = MACH("atari-st",
|
||||
"Atari ST (80 col.)", "st52", "VT52", K_ATARI_ST, MF_ATARI_ST,
|
||||
tos_med, 80, 25, 3, NULL, NULL);
|
||||
static const struct vt_profile atari_st_low = MACH("atari-st-low",
|
||||
"Atari ST (40 col., 16 colours)", "st52", "VT52", K_ATARI_ST,
|
||||
MF_ATARI_ST, tos, 40, 25, 15, NULL, NULL);
|
||||
static const struct vt_profile viewdata = MACH("viewdata",
|
||||
"Viewdata (Prestel)", "viewdata", "VIEWDATA", K_VIEWDATA, MF_PRESTEL,
|
||||
teletext, 40, 24, 7, NULL, ".vdt");
|
||||
static const struct vt_profile minitel = MACH("minitel", "Minitel",
|
||||
"minitel", "MINITEL", K_MINITEL, MF_NONE, teletext, 40, 25, 7, NULL,
|
||||
".vdt");
|
||||
|
||||
static const struct vt_profile tek4014 = {
|
||||
"tek4014", "Tektronix 4014", "tek4014", "TEK4014", K_TEK, 0, 0, NULL,
|
||||
NULL, -1, 0, MF_NONE, 0, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL,
|
||||
0, 0, 0, 0, 0, NULL, NULL
|
||||
};
|
||||
|
||||
/* in the pickers' order */
|
||||
const struct vt_profile *const vt_profiles[] = {
|
||||
&xterm, &ansi_bbs, &rip, &avatar, &amiga, &vt52, &vt_profile_vt100,
|
||||
&vt102, &vt220, &vt320, &vt340, &vt420, &vt510, &vt520, &vt525, &c64,
|
||||
&c128, &c128_80, &plus4, &vic20p, &pet, &pet80, &atari, &atari_intl,
|
||||
&atari_st, &atari_st_low, &viewdata, &minitel, &tek4014
|
||||
};
|
||||
const int vt_profile_count =
|
||||
(int)(sizeof(vt_profiles) / sizeof(vt_profiles[0]));
|
||||
|
||||
const struct vt_profile *vt_profile_by_id(const char *id)
|
||||
{
|
||||
int i;
|
||||
if (!id)
|
||||
return NULL;
|
||||
for (i = 0; i < vt_profile_count; i++)
|
||||
if (!strcmp(vt_profiles[i]->id, id))
|
||||
return vt_profiles[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* the default: what the terminal was before profiles */
|
||||
const struct vt_profile *vt_profile_default(void)
|
||||
{
|
||||
return &xterm;
|
||||
}
|
||||
|
||||
struct vt_interp *profile_interp(dmc_vt *t, const struct vt_profile *p)
|
||||
{
|
||||
switch (p->kind) {
|
||||
case K_VT52:
|
||||
return vt52_new(t, G_VT52_GRAPHICS, "\x1b/K", NULL, NULL);
|
||||
case K_ATARI_ST: return atari_st_new(t, p);
|
||||
case K_PETSCII: return petscii_new(t, p);
|
||||
case K_ATASCII: return atascii_new(t, p);
|
||||
case K_VIEWDATA: return viewdata_new(t, p);
|
||||
case K_MINITEL: return minitel_new(t, p);
|
||||
case K_TEK: return tek_new(t, NULL, NULL);
|
||||
default: return dec_new(t, p);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,997 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* vt_rip.c - RIPscrip 1.54 (the app's RipInterpreter), TeleGrafix's
|
||||
* graphics for BBSes: ANSI-BBS whose lines that start with !| are
|
||||
* drawing commands on a 640x350 EGA screen (lines, rectangles, circles,
|
||||
* ellipses, arcs, pies, Béziers, polygons, flood and patterned fills,
|
||||
* text, the clipboard) and mouse regions and buttons that a tap sends
|
||||
* the host. Ordinary text and ANSI go back to ANSI-BBS's parser (vt_dec.c)
|
||||
* for the text window, 80x43 of 8x8 cells.
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "vtint.h"
|
||||
|
||||
#define WIDTH 640
|
||||
#define HEIGHT 350
|
||||
#define ASPECT 0.775
|
||||
#define MAX_LINE (1 << 16)
|
||||
|
||||
/* a run of code points */
|
||||
struct cps {
|
||||
uint32_t *v;
|
||||
int n, cap;
|
||||
};
|
||||
|
||||
static void cps_add(struct cps *s, uint32_t c)
|
||||
{
|
||||
if (s->n == s->cap) {
|
||||
int cap = s->cap ? s->cap * 2 : 64;
|
||||
uint32_t *v = (uint32_t *)realloc(s->v, sizeof(uint32_t) * (size_t)cap);
|
||||
if (!v)
|
||||
return;
|
||||
s->v = v;
|
||||
s->cap = cap;
|
||||
}
|
||||
s->v[s->n++] = c;
|
||||
}
|
||||
|
||||
struct region {
|
||||
int x0, y0, x1, y1;
|
||||
uint32_t *send;
|
||||
int n;
|
||||
};
|
||||
|
||||
struct rip {
|
||||
dmc_vt *t;
|
||||
void (*replay)(void *, int32_t);
|
||||
void *ctx;
|
||||
struct picture_layer layer;
|
||||
|
||||
int line_start, bang, in_line, escaped, swallow_lf, replaying, enabled;
|
||||
struct cps line;
|
||||
int32_t held[8];
|
||||
int held_n;
|
||||
|
||||
int color, x, y, xor_, line_pattern, thick, fill_pattern;
|
||||
uint8_t user_fill[8];
|
||||
int fill_color, font, font_size, vertical;
|
||||
uint32_t palette[16];
|
||||
int viewport[4], has_viewport;
|
||||
struct raster_grab clipboard;
|
||||
int has_clipboard;
|
||||
|
||||
struct region *regions;
|
||||
int nregions, cap_regions;
|
||||
|
||||
int bevel, b_fore, b_surface, b_bright, b_dark;
|
||||
int block[5], has_block;
|
||||
};
|
||||
|
||||
/* an EGA colour (0-63: rgbRGB, the lower case the 1/3 bits) as ARGB */
|
||||
static uint32_t ega(int v)
|
||||
{
|
||||
#define CH(hi, lo) ((((v) >> (hi)) & 1 ? 0xaa : 0) + (((v) >> (lo)) & 1 ? 0x55 : 0))
|
||||
return 0xff000000u | ((uint32_t)CH(2, 5) << 16) |
|
||||
((uint32_t)CH(1, 4) << 8) | (uint32_t)CH(0, 3);
|
||||
#undef CH
|
||||
}
|
||||
|
||||
static const int ega_start[16] = {
|
||||
0, 1, 2, 3, 4, 5, 20, 7, 56, 57, 58, 59, 60, 61, 62, 63
|
||||
};
|
||||
|
||||
/* BGI's fill patterns 2-11 */
|
||||
static const uint8_t fills[10][8] = {
|
||||
{ 0xff, 0xff, 0, 0, 0xff, 0xff, 0, 0 },
|
||||
{ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 },
|
||||
{ 0xe0, 0xc1, 0x83, 0x07, 0x0e, 0x1c, 0x38, 0x70 },
|
||||
{ 0xf0, 0x78, 0x3c, 0x1e, 0x0f, 0x87, 0xc3, 0xe1 },
|
||||
{ 0xa5, 0xd2, 0x69, 0xb4, 0x5a, 0x2d, 0x96, 0x4b },
|
||||
{ 0xff, 0x88, 0x88, 0x88, 0xff, 0x88, 0x88, 0x88 },
|
||||
{ 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 },
|
||||
{ 0xcc, 0x33, 0xcc, 0x33, 0xcc, 0x33, 0xcc, 0x33 },
|
||||
{ 0x80, 0x00, 0x08, 0x00, 0x80, 0x00, 0x08, 0x00 },
|
||||
{ 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00 },
|
||||
};
|
||||
static const uint8_t hollow[8];
|
||||
|
||||
static int clampi(int v, int lo, int hi)
|
||||
{
|
||||
return v < lo ? lo : v > hi ? hi : v;
|
||||
}
|
||||
|
||||
static int roundi(double v)
|
||||
{
|
||||
return (int)floor(v + 0.5);
|
||||
}
|
||||
|
||||
static struct raster *R(struct rip *r)
|
||||
{
|
||||
return layer_raster(&r->layer);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* a command's arguments: MegaNums (base 36), then the text left */
|
||||
|
||||
struct args {
|
||||
const uint32_t *s;
|
||||
int n, i;
|
||||
};
|
||||
|
||||
static int arg_w(struct args *a, int width)
|
||||
{
|
||||
int v = 0;
|
||||
while (width-- > 0) {
|
||||
uint32_t c = a->i < a->n ? a->s[a->i] : '0';
|
||||
a->i++;
|
||||
v *= 36;
|
||||
if (c >= '0' && c <= '9')
|
||||
v += (int)(c - '0');
|
||||
else if (c >= 'A' && c <= 'Z')
|
||||
v += (int)(c - 'A') + 10;
|
||||
else if (c >= 'a' && c <= 'z')
|
||||
v += (int)(c - 'a') + 10;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static int arg(struct args *a)
|
||||
{
|
||||
return arg_w(a, 2);
|
||||
}
|
||||
|
||||
static const uint32_t *rest(struct args *a, int *n)
|
||||
{
|
||||
*n = a->i < a->n ? a->n - a->i : 0;
|
||||
return a->i < a->n ? a->s + a->i : a->s;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* drawing */
|
||||
|
||||
/* colour register c as drawn: black transparent (the text shows through) */
|
||||
static uint32_t argb(struct rip *r, int c)
|
||||
{
|
||||
uint32_t v = r->palette[c & 15];
|
||||
return (v & 0xffffff) == 0 ? 0 : v;
|
||||
}
|
||||
|
||||
static void set_up(struct rip *r)
|
||||
{
|
||||
struct raster *g = R(r);
|
||||
if (!g)
|
||||
return;
|
||||
g->mode = r->xor_ ? R_COMPLEMENT : R_OVERLAY;
|
||||
g->color = argb(r, r->color);
|
||||
g->pattern = r->line_pattern;
|
||||
g->fill_pattern = NULL;
|
||||
g->has_clip = r->has_viewport;
|
||||
if (r->has_viewport)
|
||||
raster_pixel_rect(g, r->viewport[0], r->viewport[1],
|
||||
r->viewport[2], r->viewport[3], g->clip);
|
||||
if (g->color == 0)
|
||||
g->mode = R_ERASE;
|
||||
g->background = 0;
|
||||
}
|
||||
|
||||
static void set_up_fill(struct rip *r)
|
||||
{
|
||||
struct raster *g;
|
||||
set_up(r);
|
||||
g = R(r);
|
||||
if (!g)
|
||||
return;
|
||||
g->color = argb(r, r->fill_color);
|
||||
g->mode = g->color == 0 ? R_ERASE : R_OVERLAY;
|
||||
switch (r->fill_pattern) {
|
||||
case 0: g->fill_pattern = hollow; break;
|
||||
case 1: g->fill_pattern = NULL; break;
|
||||
case 12:
|
||||
memcpy(g->fill_pattern_buf, r->user_fill, 8);
|
||||
g->fill_pattern = g->fill_pattern_buf;
|
||||
break;
|
||||
default:
|
||||
g->fill_pattern = r->fill_pattern >= 2 && r->fill_pattern <= 11
|
||||
? fills[r->fill_pattern - 2] : NULL;
|
||||
}
|
||||
g->fill_background = 0;
|
||||
}
|
||||
|
||||
static void line(struct rip *r, int x0, int y0, int x1, int y1)
|
||||
{
|
||||
struct raster *g;
|
||||
set_up(r);
|
||||
g = R(r);
|
||||
if (!g)
|
||||
return;
|
||||
raster_line(g, x0, y0, x1, y1);
|
||||
if (r->thick == 3) {
|
||||
raster_line(g, x0 + 1, y0, x1 + 1, y1);
|
||||
raster_line(g, x0, y0 + 1, x1, y1 + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* lines through n points (xy pairs) */
|
||||
static void polyline(struct rip *r, const int *xy, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < n; i++)
|
||||
line(r, xy[2 * i - 2], xy[2 * i - 1], xy[2 * i], xy[2 * i + 1]);
|
||||
}
|
||||
|
||||
static void fill(struct rip *r, const int *xy, int n)
|
||||
{
|
||||
set_up_fill(r);
|
||||
if (R(r))
|
||||
raster_fill(R(r), xy, n);
|
||||
}
|
||||
|
||||
static void box_fill(struct rip *r, int x0, int y0, int x1, int y1)
|
||||
{
|
||||
int q[8];
|
||||
q[0] = x0; q[1] = y0;
|
||||
q[2] = x1 + 1; q[3] = y0;
|
||||
q[4] = x1 + 1; q[5] = y1 + 1;
|
||||
q[6] = x0; q[7] = y1 + 1;
|
||||
fill(r, q, 4);
|
||||
}
|
||||
|
||||
static double sweep(int s, int e)
|
||||
{
|
||||
int d = ((e - s) % 360 + 360) % 360;
|
||||
return d == 0 ? 360.0 : (double)d;
|
||||
}
|
||||
|
||||
static int aspect(int rad)
|
||||
{
|
||||
return roundi(rad * ASPECT);
|
||||
}
|
||||
|
||||
static void arc(struct rip *r, int cx, int cy, int s, int e, int rx, int ry)
|
||||
{
|
||||
set_up(r);
|
||||
if (R(r))
|
||||
raster_ellipse(R(r), cx, cy, rx, ry, (double)s, sweep(s, e));
|
||||
}
|
||||
|
||||
#define MAX_ARC 80
|
||||
|
||||
static void pie(struct rip *r, int cx, int cy, int s, int e, int rx, int ry)
|
||||
{
|
||||
int xy[2 * (MAX_ARC + 2)], n;
|
||||
xy[0] = cx;
|
||||
xy[1] = cy;
|
||||
n = 1 + raster_ellipse_points(cx, cy, rx, ry, (double)s, sweep(s, e),
|
||||
xy + 2, MAX_ARC);
|
||||
xy[2 * n] = cx;
|
||||
xy[2 * n + 1] = cy;
|
||||
n++;
|
||||
fill(r, xy, n);
|
||||
polyline(r, xy, n);
|
||||
}
|
||||
|
||||
static void bezier(struct rip *r, const int *p, int n)
|
||||
{
|
||||
int *xy = (int *)malloc(sizeof(int) * 2 * (size_t)(n + 1)), i;
|
||||
if (!xy)
|
||||
return;
|
||||
for (i = 0; i <= n; i++) {
|
||||
double u = (double)i / n, v = 1 - u;
|
||||
double a = v * v * v, b = 3 * v * v * u, c = 3 * v * u * u,
|
||||
d = u * u * u;
|
||||
xy[2 * i] = roundi(a * p[0] + b * p[2] + c * p[4] + d * p[6]);
|
||||
xy[2 * i + 1] = roundi(a * p[1] + b * p[3] + c * p[5] + d * p[7]);
|
||||
}
|
||||
polyline(r, xy, n + 1);
|
||||
free(xy);
|
||||
}
|
||||
|
||||
static int cell_height(struct rip *r)
|
||||
{
|
||||
return r->font == 0 ? 8 * r->font_size : 10 * r->font_size;
|
||||
}
|
||||
|
||||
/* text drawn from the position in RIPterm's 8x8 font (bigger for the
|
||||
stroke fonts' sizes), the position left after it */
|
||||
static void text(struct rip *r, const uint32_t *s, int n)
|
||||
{
|
||||
struct raster *g;
|
||||
const struct vt_font *f = machine_font(MF_RIPTERM);
|
||||
int cw = r->font == 0 ? 8 * r->font_size : 6 * r->font_size;
|
||||
int ch = cell_height(r), i;
|
||||
set_up(r);
|
||||
g = R(r);
|
||||
if (!g)
|
||||
return;
|
||||
g->mode = r->xor_ ? R_COMPLEMENT : R_OVERLAY;
|
||||
for (i = 0; i < n; i++) {
|
||||
raster_text(g, r->x, r->y, s + i, 1, f, cw, ch);
|
||||
if (r->vertical)
|
||||
r->y += ch;
|
||||
else
|
||||
r->x += cw;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* windows and regions */
|
||||
|
||||
static void add_region(struct rip *r, int x0, int y0, int x1, int y1,
|
||||
const uint32_t *s, int n)
|
||||
{
|
||||
struct region *g;
|
||||
if (r->nregions == r->cap_regions) {
|
||||
int cap = r->cap_regions ? r->cap_regions * 2 : 16;
|
||||
struct region *v = (struct region *)realloc(
|
||||
r->regions, sizeof(*v) * (size_t)cap);
|
||||
if (!v)
|
||||
return;
|
||||
r->regions = v;
|
||||
r->cap_regions = cap;
|
||||
}
|
||||
g = &r->regions[r->nregions];
|
||||
g->send = (uint32_t *)malloc(sizeof(uint32_t) * (size_t)(n ? n : 1));
|
||||
if (!g->send)
|
||||
return;
|
||||
memcpy(g->send, s, sizeof(uint32_t) * (size_t)n);
|
||||
g->n = n;
|
||||
g->x0 = x0;
|
||||
g->y0 = y0;
|
||||
g->x1 = x1;
|
||||
g->y1 = y1;
|
||||
r->nregions++;
|
||||
}
|
||||
|
||||
static void clear_regions(struct rip *r)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < r->nregions; i++)
|
||||
free(r->regions[i].send);
|
||||
r->nregions = 0;
|
||||
}
|
||||
|
||||
static void drop_clipboard(struct rip *r)
|
||||
{
|
||||
if (r->has_clipboard)
|
||||
free(r->clipboard.px);
|
||||
r->has_clipboard = 0;
|
||||
memset(&r->clipboard, 0, sizeof(r->clipboard));
|
||||
}
|
||||
|
||||
static void text_window(struct rip *r, int x0, int y0, int x1, int y1)
|
||||
{
|
||||
dmc_vt *t = r->t;
|
||||
int top, bottom;
|
||||
(void)x1;
|
||||
if (!x0 && !y0 && !x1 && !y1)
|
||||
return;
|
||||
top = clampi(y0, 0, t->rows - 1);
|
||||
bottom = clampi(y1, top, t->rows - 1);
|
||||
vt_set_region(t, top, bottom);
|
||||
vt_move_cursor(t, clampi(x0, 0, t->cols - 1), top, 0);
|
||||
}
|
||||
|
||||
/* RIP_RESET_WINDOWS: the picture, text, windows, palette and regions as
|
||||
at the start */
|
||||
static void reset_windows(struct rip *r)
|
||||
{
|
||||
dmc_vt *t = r->t;
|
||||
int i;
|
||||
layer_reset(&r->layer);
|
||||
vt_set_region(t, 0, t->rows - 1);
|
||||
vt_erase_display(t, 2, 0);
|
||||
vt_move_cursor(t, 0, 0, 0);
|
||||
for (i = 0; i < 16; i++)
|
||||
r->palette[i] = ega(ega_start[i]);
|
||||
r->has_viewport = 0;
|
||||
clear_regions(r);
|
||||
drop_clipboard(r);
|
||||
r->color = 15;
|
||||
r->x = r->y = 0;
|
||||
}
|
||||
|
||||
/* a button (1U): its bevel, surface and label in the buttons' style, and
|
||||
a region sending its host text ("icon<>label<>host") */
|
||||
static void button(struct rip *r, int x0, int y0, int x1, int y1,
|
||||
const uint32_t *s, int n)
|
||||
{
|
||||
const uint32_t *part[3] = { NULL, NULL, NULL };
|
||||
int plen[3] = { 0, 0, 0 }, k = 0, i, start = 0, b;
|
||||
int sc, sp, scolor, sfont, ssize, svert, q[6];
|
||||
for (i = 0; i <= n && k < 3; i++) {
|
||||
if (i == n || (s[i] == '<' && i + 1 < n && s[i + 1] == '>')) {
|
||||
part[k] = s + start;
|
||||
plen[k] = i - start;
|
||||
k++;
|
||||
start = i + 2;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
sc = r->fill_color;
|
||||
sp = r->fill_pattern;
|
||||
r->fill_color = r->b_surface;
|
||||
r->fill_pattern = 1;
|
||||
box_fill(r, x0, y0, x1, y1);
|
||||
r->fill_color = sc;
|
||||
r->fill_pattern = sp;
|
||||
scolor = r->color;
|
||||
for (b = 0; b < r->bevel; b++) {
|
||||
r->color = r->b_bright;
|
||||
q[0] = x0 - b - 1; q[1] = y1 + b + 1;
|
||||
q[2] = x0 - b - 1; q[3] = y0 - b - 1;
|
||||
q[4] = x1 + b + 1; q[5] = y0 - b - 1;
|
||||
polyline(r, q, 3);
|
||||
r->color = r->b_dark;
|
||||
q[0] = x1 + b + 1; q[1] = y0 - b - 1;
|
||||
q[2] = x1 + b + 1; q[3] = y1 + b + 1;
|
||||
q[4] = x0 - b - 1; q[5] = y1 + b + 1;
|
||||
polyline(r, q, 3);
|
||||
}
|
||||
r->color = r->b_fore;
|
||||
sfont = r->font;
|
||||
ssize = r->font_size;
|
||||
svert = r->vertical;
|
||||
r->font = 0;
|
||||
r->font_size = 1;
|
||||
r->vertical = 0;
|
||||
r->x = (x0 + x1) / 2 - plen[1] * 4;
|
||||
r->y = (y0 + y1) / 2 - 4;
|
||||
if (part[1])
|
||||
text(r, part[1], plen[1]);
|
||||
r->font = sfont;
|
||||
r->font_size = ssize;
|
||||
r->vertical = svert;
|
||||
r->color = scolor;
|
||||
if (plen[2] > 0)
|
||||
add_region(r, x0, y0, x1, y1, part[2], plen[2]);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* the commands */
|
||||
|
||||
static void level0(struct rip *r, uint32_t c, struct args *a)
|
||||
{
|
||||
dmc_vt *t = r->t;
|
||||
struct raster *g;
|
||||
const uint32_t *s;
|
||||
int n, x0, y0, x1, y1, i, v;
|
||||
switch (c) {
|
||||
case 'w':
|
||||
x0 = arg(a); y0 = arg(a); x1 = arg(a); y1 = arg(a);
|
||||
text_window(r, x0, y0, x1, y1);
|
||||
break;
|
||||
case 'v':
|
||||
for (i = 0; i < 4; i++)
|
||||
r->viewport[i] = arg(a);
|
||||
r->has_viewport = 1;
|
||||
break;
|
||||
case '*':
|
||||
reset_windows(r);
|
||||
break;
|
||||
case 'e':
|
||||
vt_erase_display(t, 2, 0);
|
||||
vt_move_cursor(t, 0, t->top, 0);
|
||||
break;
|
||||
case 'E':
|
||||
if (!(g = R(r)))
|
||||
break;
|
||||
if (r->has_viewport)
|
||||
raster_clear_rect(g, r->viewport[0], r->viewport[1],
|
||||
r->viewport[2], r->viewport[3], 0);
|
||||
else
|
||||
raster_clear_rect(g, 0, 0, WIDTH - 1, HEIGHT - 1, 0);
|
||||
break;
|
||||
case 'g':
|
||||
x0 = arg(a);
|
||||
y0 = arg(a);
|
||||
vt_move_cursor(t, x0, t->top + y0, 0);
|
||||
break;
|
||||
case 'H': vt_move_cursor(t, 0, t->top, 0); break;
|
||||
case '>': vt_erase_line(t, 0, 0); break;
|
||||
case 'c': r->color = arg(a) & 15; break;
|
||||
case 'Q':
|
||||
for (i = 0; i < 16; i++)
|
||||
r->palette[i] = ega(arg(a));
|
||||
break;
|
||||
case 'a':
|
||||
i = arg(a);
|
||||
v = arg(a);
|
||||
if (i >= 0 && i <= 15)
|
||||
r->palette[i] = ega(v);
|
||||
break;
|
||||
case 'W': r->xor_ = arg(a) == 1; break;
|
||||
case 'm': r->x = arg(a); r->y = arg(a); break;
|
||||
case 'T':
|
||||
s = rest(a, &n);
|
||||
text(r, s, n);
|
||||
break;
|
||||
case '@':
|
||||
r->x = arg(a);
|
||||
r->y = arg(a);
|
||||
s = rest(a, &n);
|
||||
text(r, s, n);
|
||||
break;
|
||||
case 'Y':
|
||||
r->font = arg(a);
|
||||
r->vertical = arg(a) == 1;
|
||||
r->font_size = clampi(arg(a), 1, 10);
|
||||
break;
|
||||
case 'X':
|
||||
x0 = arg(a);
|
||||
y0 = arg(a);
|
||||
set_up(r);
|
||||
if ((g = R(r)))
|
||||
raster_point(g, x0, y0);
|
||||
break;
|
||||
case 'L':
|
||||
x0 = arg(a); y0 = arg(a); x1 = arg(a); y1 = arg(a);
|
||||
line(r, x0, y0, x1, y1);
|
||||
break;
|
||||
case 'R': {
|
||||
int q[10];
|
||||
x0 = arg(a); y0 = arg(a); x1 = arg(a); y1 = arg(a);
|
||||
q[0] = x0; q[1] = y0; q[2] = x1; q[3] = y0;
|
||||
q[4] = x1; q[5] = y1; q[6] = x0; q[7] = y1;
|
||||
q[8] = x0; q[9] = y0;
|
||||
polyline(r, q, 5);
|
||||
break;
|
||||
}
|
||||
case 'B':
|
||||
x0 = arg(a); y0 = arg(a); x1 = arg(a); y1 = arg(a);
|
||||
box_fill(r, x0, y0, x1, y1);
|
||||
break;
|
||||
case 'C':
|
||||
x0 = arg(a); y0 = arg(a); v = arg(a);
|
||||
set_up(r);
|
||||
if ((g = R(r)))
|
||||
raster_ellipse(g, x0, y0, v, aspect(v), 0, 360);
|
||||
break;
|
||||
case 'A': {
|
||||
int cx = arg(a), cy = arg(a), st = arg(a), e = arg(a), rad = arg(a);
|
||||
arc(r, cx, cy, st, e, rad, aspect(rad));
|
||||
break;
|
||||
}
|
||||
case 'O': case 'V': {
|
||||
int cx = arg(a), cy = arg(a), st = arg(a), e = arg(a), rx = arg(a);
|
||||
arc(r, cx, cy, st, e, rx, arg(a));
|
||||
break;
|
||||
}
|
||||
case 'o': {
|
||||
int cx = arg(a), cy = arg(a), rx = arg(a), ry = arg(a);
|
||||
int xy[2 * MAX_ARC];
|
||||
n = raster_ellipse_points(cx, cy, rx, ry, 0, 360, xy, MAX_ARC);
|
||||
fill(r, xy, n);
|
||||
set_up(r);
|
||||
if ((g = R(r)))
|
||||
raster_ellipse(g, cx, cy, rx, ry, 0, 360);
|
||||
break;
|
||||
}
|
||||
case 'I': {
|
||||
int cx = arg(a), cy = arg(a), st = arg(a), e = arg(a), rad = arg(a);
|
||||
pie(r, cx, cy, st, e, rad, aspect(rad));
|
||||
break;
|
||||
}
|
||||
case 'i': {
|
||||
int cx = arg(a), cy = arg(a), st = arg(a), e = arg(a), rx = arg(a);
|
||||
pie(r, cx, cy, st, e, rx, arg(a));
|
||||
break;
|
||||
}
|
||||
case 'Z': {
|
||||
int p[8];
|
||||
for (i = 0; i < 8; i++)
|
||||
p[i] = arg(a);
|
||||
bezier(r, p, clampi(arg(a), 1, 256));
|
||||
break;
|
||||
}
|
||||
case 'P': case 'p': case 'l': {
|
||||
int *xy;
|
||||
n = arg(a);
|
||||
if (n < 2)
|
||||
break;
|
||||
xy = (int *)malloc(sizeof(int) * 2 * (size_t)(n + 1));
|
||||
if (!xy)
|
||||
break;
|
||||
for (i = 0; i < 2 * n; i++)
|
||||
xy[i] = arg(a);
|
||||
if (c == 'p')
|
||||
fill(r, xy, n);
|
||||
if (c != 'l') {
|
||||
xy[2 * n] = xy[0];
|
||||
xy[2 * n + 1] = xy[1];
|
||||
polyline(r, xy, n + 1);
|
||||
} else {
|
||||
polyline(r, xy, n);
|
||||
}
|
||||
free(xy);
|
||||
break;
|
||||
}
|
||||
case 'F':
|
||||
x0 = arg(a); y0 = arg(a); v = arg(a);
|
||||
set_up_fill(r);
|
||||
if ((g = R(r)))
|
||||
raster_flood(g, x0, y0, argb(r, v));
|
||||
break;
|
||||
case '=': {
|
||||
int style = arg(a), user = arg_w(a, 4);
|
||||
r->thick = arg(a) >= 3 ? 3 : 1;
|
||||
switch (style) {
|
||||
case 1: r->line_pattern = 0xcc; break;
|
||||
case 2: r->line_pattern = 0xe4; break;
|
||||
case 3: r->line_pattern = 0xf0; break;
|
||||
case 4: r->line_pattern = (user >> 8) & 0xff; break;
|
||||
default: r->line_pattern = 0xff;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'S':
|
||||
r->fill_pattern = arg(a);
|
||||
r->fill_color = arg(a) & 15;
|
||||
break;
|
||||
case 's':
|
||||
for (i = 0; i < 8; i++)
|
||||
r->user_fill[i] = (uint8_t)(arg(a) & 0xff);
|
||||
r->fill_color = arg(a) & 15;
|
||||
r->fill_pattern = 12;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void level1(struct rip *r, uint32_t c, struct args *a)
|
||||
{
|
||||
struct raster *g;
|
||||
const uint32_t *s;
|
||||
int n, x0, y0, x1, y1;
|
||||
switch (c) {
|
||||
case 'M':
|
||||
arg(a);
|
||||
x0 = arg(a); y0 = arg(a); x1 = arg(a); y1 = arg(a);
|
||||
arg(a); arg_w(a, 1); arg_w(a, 5);
|
||||
s = rest(a, &n);
|
||||
add_region(r, x0, y0, x1, y1, s, n);
|
||||
break;
|
||||
case 'K':
|
||||
clear_regions(r);
|
||||
break;
|
||||
case 'B': {
|
||||
int bevel, fore, bright, dark, surface;
|
||||
arg(a); arg(a); arg(a); arg_w(a, 4);
|
||||
bevel = arg(a);
|
||||
fore = arg(a);
|
||||
arg(a);
|
||||
bright = arg(a);
|
||||
dark = arg(a);
|
||||
surface = arg(a);
|
||||
r->bevel = bevel;
|
||||
r->b_fore = fore & 15;
|
||||
r->b_surface = surface & 15;
|
||||
r->b_bright = bright & 15;
|
||||
r->b_dark = dark & 15;
|
||||
break;
|
||||
}
|
||||
case 'U':
|
||||
x0 = arg(a); y0 = arg(a); x1 = arg(a); y1 = arg(a);
|
||||
arg(a); arg_w(a, 1); arg_w(a, 1);
|
||||
s = rest(a, &n);
|
||||
button(r, x0, y0, x1, y1, s, n);
|
||||
break;
|
||||
case 'T':
|
||||
r->block[0] = arg(a);
|
||||
r->block[1] = arg(a);
|
||||
r->block[2] = arg(a);
|
||||
r->block[3] = arg(a);
|
||||
r->block[4] = r->block[1];
|
||||
r->has_block = 1;
|
||||
break;
|
||||
case 't':
|
||||
if (!r->has_block)
|
||||
break;
|
||||
arg_w(a, 1);
|
||||
r->x = r->block[0];
|
||||
r->y = r->block[4];
|
||||
s = rest(a, &n);
|
||||
text(r, s, n);
|
||||
r->block[4] += cell_height(r);
|
||||
break;
|
||||
case 'E':
|
||||
r->has_block = 0;
|
||||
break;
|
||||
case 'C':
|
||||
x0 = arg(a); y0 = arg(a); x1 = arg(a); y1 = arg(a);
|
||||
if (!(g = R(r)))
|
||||
break;
|
||||
drop_clipboard(r);
|
||||
raster_grab(g, x0, y0, x1, y1, &r->clipboard);
|
||||
r->has_clipboard = 1;
|
||||
break;
|
||||
case 'P': {
|
||||
int px = arg(a), py = arg(a), mode = arg(a);
|
||||
if (r->has_clipboard && (g = R(r)))
|
||||
raster_put(g, &r->clipboard, px, py, mode == 1);
|
||||
break;
|
||||
}
|
||||
case 'G': {
|
||||
struct raster_grab gr;
|
||||
int dest;
|
||||
x0 = arg(a); y0 = arg(a); x1 = arg(a); y1 = arg(a);
|
||||
arg(a);
|
||||
dest = arg(a);
|
||||
if (!(g = R(r)))
|
||||
break;
|
||||
raster_grab(g, x0, y0, x1, y1, &gr);
|
||||
raster_put(g, &gr, x0, dest, 0);
|
||||
free(gr.px);
|
||||
break;
|
||||
}
|
||||
case 'F':
|
||||
/* a file query: none of the host's icons are here */
|
||||
vt_reply(r->t, "0\r\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* one command: its level (a digit) and letter, then its MegaNums and text */
|
||||
static void run(struct rip *r, const uint32_t *cmd, int n)
|
||||
{
|
||||
struct args a;
|
||||
int level = 0, at;
|
||||
if (n == 0)
|
||||
return;
|
||||
if (cmd[0] >= '1' && cmd[0] <= '9')
|
||||
level = (int)(cmd[0] - '0');
|
||||
at = level > 0 ? 1 : 0;
|
||||
if (at >= n)
|
||||
return;
|
||||
a.s = cmd;
|
||||
a.n = n;
|
||||
a.i = at + 1;
|
||||
if (level == 1)
|
||||
level1(r, cmd[at], &a);
|
||||
else if (level == 0)
|
||||
level0(r, cmd[at], &a);
|
||||
}
|
||||
|
||||
/* a RIP line's commands, split at the |s that aren't escaped, each run */
|
||||
static void run_line(struct rip *r, const uint32_t *s, int n)
|
||||
{
|
||||
struct cps cmd = { NULL, 0, 0 };
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
uint32_t c = s[i];
|
||||
if (c == '\\' && i + 1 < n) {
|
||||
cps_add(&cmd, s[i + 1]);
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (c == '|') {
|
||||
run(r, cmd.v, cmd.n);
|
||||
cmd.n = 0;
|
||||
} else {
|
||||
cps_add(&cmd, c);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
run(r, cmd.v, cmd.n);
|
||||
free(cmd.v);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* reading */
|
||||
|
||||
/* code points given to ANSI-BBS's parser after all */
|
||||
static void give_back(struct rip *r, const int32_t *s, int n)
|
||||
{
|
||||
int i;
|
||||
r->replaying = 1;
|
||||
for (i = 0; i < n; i++)
|
||||
r->replay(r->ctx, s[i]);
|
||||
r->replaying = 0;
|
||||
r->line_start = 0;
|
||||
}
|
||||
|
||||
/* a RIP line's character: kept (a backslash's escape or continuation
|
||||
honoured) until the line ends, then run */
|
||||
static void gather(struct rip *r, int32_t cp)
|
||||
{
|
||||
if (r->escaped) {
|
||||
r->escaped = 0;
|
||||
if (cp == 0x0d) {
|
||||
r->swallow_lf = 1;
|
||||
} else if (cp != 0x0a) {
|
||||
cps_add(&r->line, '\\');
|
||||
cps_add(&r->line, (uint32_t)cp);
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (cp) {
|
||||
case '\\':
|
||||
r->escaped = 1;
|
||||
break;
|
||||
case 0x0d: case 0x0a: {
|
||||
struct cps l = r->line;
|
||||
r->in_line = 0;
|
||||
r->line_start = 1;
|
||||
r->swallow_lf = cp == 0x0d;
|
||||
memset(&r->line, 0, sizeof(r->line));
|
||||
run_line(r, l.v, l.n);
|
||||
free(l.v);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if (r->line.n < MAX_LINE)
|
||||
cps_add(&r->line, (uint32_t)cp);
|
||||
}
|
||||
}
|
||||
|
||||
static int digit02(int32_t c)
|
||||
{
|
||||
return c >= '0' && c <= '2';
|
||||
}
|
||||
|
||||
/* ESC [ !, ESC [ 0 !, 1 !, 2 !: held until it is one (answered: RIP's
|
||||
version; off; on), else given back */
|
||||
static int query(struct rip *r, int32_t cp)
|
||||
{
|
||||
int n, i, full;
|
||||
int32_t h[8];
|
||||
if (r->held_n < 8)
|
||||
r->held[r->held_n++] = cp;
|
||||
n = r->held_n;
|
||||
full = n >= 3 && r->held[n - 1] == '!' && r->held[0] == 0x1b &&
|
||||
r->held[1] == '[';
|
||||
for (i = 2; full && i < n - 1; i++)
|
||||
if (!digit02(r->held[i]))
|
||||
full = 0;
|
||||
if (full) {
|
||||
int arg = n == 4 ? r->held[2] : '0';
|
||||
r->held_n = 0;
|
||||
if (arg == '0')
|
||||
rip_query(r);
|
||||
else if (arg == '1')
|
||||
r->enabled = 0;
|
||||
else
|
||||
r->enabled = 1;
|
||||
return 1;
|
||||
}
|
||||
if ((n == 1 && r->held[0] == 0x1b) ||
|
||||
(n == 2 && r->held[1] == '[') ||
|
||||
(n == 3 && digit02(r->held[2])))
|
||||
return 1;
|
||||
memcpy(h, r->held, sizeof(int32_t) * (size_t)n);
|
||||
r->held_n = 0;
|
||||
give_back(r, h, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int rip_intercept(struct rip *r, int32_t cp)
|
||||
{
|
||||
if (r->replaying)
|
||||
return 0;
|
||||
if (r->swallow_lf) {
|
||||
r->swallow_lf = 0;
|
||||
if (cp == 0x0a)
|
||||
return 1;
|
||||
}
|
||||
if (r->in_line) {
|
||||
gather(r, cp);
|
||||
return 1;
|
||||
}
|
||||
if (r->held_n > 0 || cp == 0x1b)
|
||||
return query(r, cp);
|
||||
if (r->bang) {
|
||||
static const int32_t bang = '!';
|
||||
r->bang = 0;
|
||||
if (cp == '|') {
|
||||
r->in_line = 1;
|
||||
r->line.n = 0;
|
||||
return 1;
|
||||
}
|
||||
give_back(r, &bang, 1);
|
||||
}
|
||||
if (r->enabled && r->line_start && (cp == '!' || cp == 0x01 || cp == 0x02)) {
|
||||
r->bang = 1;
|
||||
return 1;
|
||||
}
|
||||
r->line_start = cp == 0x0d || cp == 0x0a;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rip_query(struct rip *r)
|
||||
{
|
||||
vt_reply(r->t, "RIPSCRIP015400");
|
||||
}
|
||||
|
||||
/* a tap on a region or button (the last defined first): its host text,
|
||||
^ and a letter a control */
|
||||
int rip_tap(struct rip *r, double fx, double fy, dmc_buf *out)
|
||||
{
|
||||
int px = (int)(fx * WIDTH), py = (int)(fy * HEIGHT), k, i;
|
||||
for (k = r->nregions - 1; k >= 0; k--) {
|
||||
const struct region *g = &r->regions[k];
|
||||
if (px < g->x0 || px > g->x1 || py < g->y0 || py > g->y1)
|
||||
continue;
|
||||
for (i = 0; i < g->n; i++) {
|
||||
uint32_t c = g->send[i];
|
||||
if (c == '^' && i + 1 < g->n) {
|
||||
uint32_t u = g->send[i + 1];
|
||||
if (u >= 'a' && u <= 'z')
|
||||
u -= 0x20;
|
||||
buf_addc(out, (char)(u & 0x1f));
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
buf_addc(out, (char)cp437_encode(c));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rip_flush(struct rip *r)
|
||||
{
|
||||
layer_flush(&r->layer);
|
||||
}
|
||||
|
||||
/* RIS: the picture, windows and regions as at the start, RIP on, the
|
||||
80x43 asked for again */
|
||||
void rip_reset(struct rip *r)
|
||||
{
|
||||
r->in_line = 0;
|
||||
r->line.n = 0;
|
||||
r->bang = 0;
|
||||
r->escaped = 0;
|
||||
r->swallow_lf = 0;
|
||||
r->held_n = 0;
|
||||
r->enabled = 1;
|
||||
r->line_start = 1;
|
||||
reset_windows(r);
|
||||
r->t->requested_cols = 80;
|
||||
r->t->requested_rows = 43;
|
||||
}
|
||||
|
||||
struct rip *rip_new(dmc_vt *t, void (*replay)(void *, int32_t), void *ctx)
|
||||
{
|
||||
struct rip *r = (struct rip *)calloc(1, sizeof(*r));
|
||||
int i;
|
||||
if (!r)
|
||||
return NULL;
|
||||
r->t = t;
|
||||
r->replay = replay;
|
||||
r->ctx = ctx;
|
||||
layer_init(&r->layer, t, WIDTH, HEIGHT, NULL, NULL);
|
||||
r->line_start = 1;
|
||||
r->enabled = 1;
|
||||
r->color = 15;
|
||||
r->line_pattern = 0xff;
|
||||
r->thick = 1;
|
||||
r->fill_pattern = 1;
|
||||
memset(r->user_fill, 0xff, sizeof(r->user_fill));
|
||||
r->fill_color = 15;
|
||||
r->font_size = 1;
|
||||
for (i = 0; i < 16; i++)
|
||||
r->palette[i] = ega(ega_start[i]);
|
||||
r->bevel = 2;
|
||||
r->b_fore = 0;
|
||||
r->b_surface = 7;
|
||||
r->b_bright = 15;
|
||||
r->b_dark = 8;
|
||||
t->requested_cols = 80;
|
||||
t->requested_rows = 43;
|
||||
return r;
|
||||
}
|
||||
|
||||
void rip_free(struct rip *r)
|
||||
{
|
||||
if (!r)
|
||||
return;
|
||||
clear_regions(r);
|
||||
free(r->regions);
|
||||
drop_clipboard(r);
|
||||
free(r->line.v);
|
||||
layer_free(&r->layer);
|
||||
free(r);
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* vt_vt52.c - the DEC VT52's escape sequences (the app's Vt52Interpreter):
|
||||
* one character after ESC (ESC Y and two more for the cursor's place),
|
||||
* no parameters, no attributes. A VT52 profile has it as its
|
||||
* interpreter; a VT100 and later run one in VT52 mode until ESC < hands
|
||||
* back. And an Atari ST's TOS VT52 (AtariStInterpreter), with the ST's
|
||||
* own additions and its character set.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "vtint.h"
|
||||
|
||||
enum { V_GROUND, V_ESC, V_ROW, V_COL, V_ARG };
|
||||
|
||||
struct vt52 {
|
||||
struct vt_interp in;
|
||||
int graphics_set;
|
||||
const char *identity;
|
||||
void (*on_ansi)(void *);
|
||||
void *ctx;
|
||||
int state, arg_for, graphics, row;
|
||||
struct vt_printer printer;
|
||||
/* an Atari ST's */
|
||||
const struct vt_profile *st;
|
||||
int fg, bg, reverse;
|
||||
};
|
||||
|
||||
#define T(v) ((v)->in.t)
|
||||
|
||||
static void control(struct vt52 *v, int32_t cp)
|
||||
{
|
||||
dmc_vt *t = T(v);
|
||||
switch (cp) {
|
||||
case 0x07: vt_bell(t); break;
|
||||
case 0x08: vt_backspace(t); break;
|
||||
case 0x09: vt_tab(t, 1); break;
|
||||
case 0x0a:
|
||||
if (v->printer.autop)
|
||||
printer_line(&v->printer, t->cy);
|
||||
vt_index(t);
|
||||
break;
|
||||
case 0x0d: vt_carriage_return(t); break;
|
||||
}
|
||||
}
|
||||
|
||||
static void st_restyle(struct vt52 *v)
|
||||
{
|
||||
T(v)->style = st_of(v->fg, v->bg, v->reverse ? A_INVERSE : 0);
|
||||
}
|
||||
|
||||
static void esc(struct vt52 *v, int32_t cp)
|
||||
{
|
||||
dmc_vt *t = T(v);
|
||||
if (v->st) {
|
||||
switch (cp) {
|
||||
case 'E':
|
||||
vt_erase_display(t, 2, 0);
|
||||
vt_move_cursor(t, 0, 0, 0);
|
||||
return;
|
||||
case 'd': vt_erase_display(t, 1, 0); return;
|
||||
case 'e': t->cursor_visible = 1; return;
|
||||
case 'f': t->cursor_visible = 0; return;
|
||||
case 'j': vt_save_cursor(t); return;
|
||||
case 'k': vt_restore_cursor(t); return;
|
||||
case 'l':
|
||||
vt_erase_line(t, 2, 0);
|
||||
vt_carriage_return(t);
|
||||
return;
|
||||
case 'o': vt_erase_line(t, 1, 0); return;
|
||||
case 'p': v->reverse = 1; st_restyle(v); return;
|
||||
case 'q': v->reverse = 0; st_restyle(v); return;
|
||||
case 'v': t->autowrap = 1; return;
|
||||
case 'w': t->autowrap = 0; return;
|
||||
case 'L':
|
||||
vt_insert_lines(t, 1);
|
||||
vt_carriage_return(t);
|
||||
return;
|
||||
case 'M':
|
||||
vt_delete_lines(t, 1);
|
||||
vt_carriage_return(t);
|
||||
return;
|
||||
case 'b': case 'c':
|
||||
v->arg_for = cp;
|
||||
v->state = V_ARG;
|
||||
return;
|
||||
}
|
||||
}
|
||||
switch (cp) {
|
||||
case 'A': vt_move_cursor(t, t->cx, t->cy - 1, 0); break;
|
||||
case 'B': vt_move_cursor(t, t->cx, t->cy + 1, 0); break;
|
||||
case 'C': vt_move_cursor(t, t->cx + 1, t->cy, 0); break;
|
||||
case 'D': vt_move_cursor(t, t->cx - 1, t->cy, 0); break;
|
||||
case 'F': v->graphics = 1; break;
|
||||
case 'G': v->graphics = 0; break;
|
||||
case 'H': vt_move_cursor(t, 0, 0, 0); break;
|
||||
case 'I': vt_reverse_index(t); break;
|
||||
case 'J': vt_erase_display(t, 0, 0); break;
|
||||
case 'K': vt_erase_line(t, 0, 0); break;
|
||||
case 'Y': v->state = V_ROW; break;
|
||||
case 'Z': vt_reply(t, v->identity); break;
|
||||
case '=': t->app_keypad = 1; break;
|
||||
case '>': t->app_keypad = 0; break;
|
||||
case '<': if (v->on_ansi) v->on_ansi(v->ctx); break;
|
||||
case '[': case '\\': break;
|
||||
case '^': v->printer.autop = 1; break;
|
||||
case '_': v->printer.autop = 0; break;
|
||||
case 'W': printer_start(&v->printer); break;
|
||||
case 'V': printer_line(&v->printer, t->cy); break;
|
||||
case ']': printer_screen(&v->printer); break;
|
||||
default:
|
||||
if (cp == 0x1b)
|
||||
v->state = V_ESC;
|
||||
else if (cp < 0x20)
|
||||
control(v, cp);
|
||||
}
|
||||
}
|
||||
|
||||
static void process(struct vt_interp *in, int32_t cp)
|
||||
{
|
||||
struct vt52 *v = (struct vt52 *)in;
|
||||
dmc_vt *t = T(v);
|
||||
int y, pen;
|
||||
if (v->printer.controller) {
|
||||
printer_feed(&v->printer, cp);
|
||||
return;
|
||||
}
|
||||
switch (v->state) {
|
||||
case V_GROUND:
|
||||
if (cp == 0x1b)
|
||||
v->state = V_ESC;
|
||||
else if (cp < 0x20)
|
||||
control(v, cp);
|
||||
else if (cp == 0x7f)
|
||||
;
|
||||
else if (v->graphics)
|
||||
vt_print(t, (int32_t)gset_map(v->graphics_set, (uint32_t)cp));
|
||||
else
|
||||
vt_print(t, v->st ? (int32_t)atari_st_char(cp) : cp);
|
||||
break;
|
||||
case V_ESC:
|
||||
v->state = V_GROUND;
|
||||
esc(v, cp);
|
||||
break;
|
||||
case V_ROW:
|
||||
v->row = cp - 0x20;
|
||||
v->state = V_COL;
|
||||
break;
|
||||
case V_ARG:
|
||||
v->state = V_GROUND;
|
||||
pen = cp & (v->st->ncolors - 1);
|
||||
if (v->arg_for == 'b')
|
||||
v->fg = pen;
|
||||
else
|
||||
v->bg = pen;
|
||||
st_restyle(v);
|
||||
break;
|
||||
case V_COL:
|
||||
v->state = V_GROUND;
|
||||
/* (a row off the screen leaves the cursor's) */
|
||||
y = v->row >= 0 && v->row < t->rows ? v->row : t->cy;
|
||||
vt_move_cursor(t, cp - 0x20, y, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void st_attach(struct vt52 *v)
|
||||
{
|
||||
dmc_vt *t = T(v);
|
||||
const struct vt_profile *m = v->st;
|
||||
vt_use_machine(t, m->columns, m->rows, m->colors, m->ncolors,
|
||||
m->default_color, m->background);
|
||||
t->autowrap = 1;
|
||||
t->cursor_visible = 1;
|
||||
st_restyle(v);
|
||||
}
|
||||
|
||||
static void reset(struct vt_interp *in)
|
||||
{
|
||||
struct vt52 *v = (struct vt52 *)in;
|
||||
v->state = V_GROUND;
|
||||
v->graphics = 0;
|
||||
printer_reset(&v->printer);
|
||||
T(v)->vt52_keys = 1;
|
||||
if (v->st) {
|
||||
v->fg = v->st->default_color;
|
||||
v->bg = v->st->background;
|
||||
v->reverse = 0;
|
||||
st_attach(v);
|
||||
}
|
||||
}
|
||||
|
||||
static void detach(struct vt_interp *in)
|
||||
{
|
||||
struct vt52 *v = (struct vt52 *)in;
|
||||
if (v->st)
|
||||
vt_release_machine(T(v));
|
||||
T(v)->vt52_keys = 0;
|
||||
}
|
||||
|
||||
static void destroy(struct vt_interp *in)
|
||||
{
|
||||
struct vt52 *v = (struct vt52 *)in;
|
||||
printer_free(&v->printer);
|
||||
free(v);
|
||||
}
|
||||
|
||||
static const struct vt_interp_ops vt52_ops = {
|
||||
process, reset, NULL, NULL, NULL, NULL, NULL, NULL, NULL, detach,
|
||||
destroy
|
||||
};
|
||||
|
||||
struct vt_interp *vt52_new(dmc_vt *t, int graphics_set, const char *identity,
|
||||
void (*on_ansi)(void *), void *ctx)
|
||||
{
|
||||
struct vt52 *v = (struct vt52 *)calloc(1, sizeof(*v));
|
||||
if (!v)
|
||||
return NULL;
|
||||
v->in.ops = &vt52_ops;
|
||||
v->in.t = t;
|
||||
v->graphics_set = graphics_set;
|
||||
v->identity = identity;
|
||||
v->on_ansi = on_ansi;
|
||||
v->ctx = ctx;
|
||||
printer_init(&v->printer, t, "\x1bX", NULL);
|
||||
t->vt52_keys = 1;
|
||||
return &v->in;
|
||||
}
|
||||
|
||||
struct vt_interp *atari_st_new(dmc_vt *t, const struct vt_profile *p)
|
||||
{
|
||||
struct vt_interp *in = vt52_new(t, G_VT52_GRAPHICS, "\x1b/K", NULL, NULL);
|
||||
struct vt52 *v = (struct vt52 *)in;
|
||||
if (!v)
|
||||
return NULL;
|
||||
in->reads_octets = 1;
|
||||
v->st = p;
|
||||
v->fg = p->default_color;
|
||||
v->bg = p->background;
|
||||
st_attach(v);
|
||||
return in;
|
||||
}
|
||||
@@ -0,0 +1,615 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* vtint.h - the terminal emulator's insides (dmc_vt), shared by its
|
||||
* files: the screen (vt.c), the interpreters of each family of
|
||||
* terminals (vt_dec.c, vt_vt52.c, vt_machine.c, vt_gfx.c,
|
||||
* vt_regis.c, vt_rip.c), the character sets and keys
|
||||
* (vt_charsets.c, vt_keys.c), the profiles (vt_profiles.c) and
|
||||
* the machines' fonts (vt_data.c).
|
||||
*
|
||||
* A C port of the D-Modem app's shared Kotlin emulator
|
||||
* (D-Modem/app: shared/src/commonMain/kotlin/org/dmodem/app/terminal),
|
||||
* kept close to it so that a change there can be carried here: the same
|
||||
* parts, the same names where C allows, the same behaviour. C99 with
|
||||
* no designated initialisers and no VLAs, for older compilers too.
|
||||
*/
|
||||
#ifndef DMC_VTINT_H
|
||||
#define DMC_VTINT_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "util.h"
|
||||
|
||||
/*
|
||||
* A cell's look, packed in 64 bits as the app's Style is: the foreground
|
||||
* (bits 0-24), the background (25-49) and the attributes (50-63). A
|
||||
* colour is an index of the 256-colour palette, ST_DEFAULT_FG or
|
||||
* ST_DEFAULT_BG, or ST_RGB or'ed with a 24-bit colour.
|
||||
*/
|
||||
typedef uint64_t vt_style;
|
||||
|
||||
#define ST_DEFAULT_FG 256
|
||||
#define ST_DEFAULT_BG 257
|
||||
#define ST_RGB 0x1000000
|
||||
|
||||
enum {
|
||||
A_BOLD = 1, A_FAINT = 2, A_ITALIC = 4, A_UNDERLINE = 8, A_BLINK = 16,
|
||||
A_INVERSE = 32, A_INVISIBLE = 64, A_STRIKE = 128,
|
||||
A_DUNDER = 256, /* SGR 21, 4:2 */
|
||||
A_CURLY = 512, /* SGR 4:3 (and 4:4, 4:5) */
|
||||
A_OVERLINE = 1024,
|
||||
A_PROTECTED = 2048 /* DECSCA's (not a rendition) */
|
||||
};
|
||||
#define A_ANY_UNDER (A_UNDERLINE | A_DUNDER | A_CURLY)
|
||||
|
||||
#define ST_COLOR_MASK 0x1ffffffULL
|
||||
|
||||
#define st_of(fg, bg, attrs) \
|
||||
(((vt_style)(unsigned)(fg) & ST_COLOR_MASK) | \
|
||||
(((vt_style)(unsigned)(bg) & ST_COLOR_MASK) << 25) | \
|
||||
((vt_style)(unsigned)(attrs) << 50))
|
||||
#define st_fg(s) ((int)((s) & ST_COLOR_MASK))
|
||||
#define st_bg(s) ((int)(((s) >> 25) & ST_COLOR_MASK))
|
||||
#define st_attrs(s) ((int)((s) >> 50))
|
||||
#define st_with_fg(s, f) st_of((f), st_bg(s), st_attrs(s))
|
||||
#define st_with_bg(s, b) st_of(st_fg(s), (b), st_attrs(s))
|
||||
#define st_with_attrs(s, a) st_of(st_fg(s), st_bg(s), (a))
|
||||
/* the look of erased cells: only the background stays (xterm's BCE) */
|
||||
#define st_erased(s) st_of(ST_DEFAULT_FG, st_bg(s), 0)
|
||||
#define ST_NORMAL st_of(ST_DEFAULT_FG, ST_DEFAULT_BG, 0)
|
||||
|
||||
/* the right half of a wide character, in a line's chars */
|
||||
#define WIDE_TAIL (-1)
|
||||
|
||||
/* a picture (a sixel image, a graphics terminal's screen), its pixels
|
||||
ARGB (0: transparent), shared by the cells that show it */
|
||||
struct vt_image {
|
||||
int refs;
|
||||
uint32_t id;
|
||||
int width, height;
|
||||
uint32_t *pixels;
|
||||
/* the cell size it was placed at: its tiles are cells of that size */
|
||||
int cell_w, cell_h;
|
||||
/* a graphics terminal's picture over the screen, not an image in
|
||||
the text */
|
||||
int picture;
|
||||
};
|
||||
|
||||
/* a hyperlink (OSC 8), shared by the cells that carry it */
|
||||
struct vt_link {
|
||||
int refs;
|
||||
char *uri;
|
||||
};
|
||||
|
||||
/* what few cells carry besides their character */
|
||||
struct vt_extra {
|
||||
uint32_t *comb; /* combining marks */
|
||||
int ncomb;
|
||||
struct vt_image *img; /* a picture's tile */
|
||||
int tx, ty;
|
||||
struct vt_link *link;
|
||||
};
|
||||
|
||||
enum { LS_SINGLE, LS_DOUBLE_WIDTH, LS_DOUBLE_TOP, LS_DOUBLE_BOTTOM };
|
||||
|
||||
/* a line of cells: code points (0 never written, WIDE_TAIL), their
|
||||
looks, and the few cells' extras */
|
||||
struct vt_line {
|
||||
int cols;
|
||||
int32_t *ch;
|
||||
vt_style *st;
|
||||
struct vt_extra **ex; /* NULL until a cell needs one */
|
||||
int wrapped;
|
||||
int size; /* LS_* */
|
||||
};
|
||||
|
||||
struct vt_line *line_new(int cols, vt_style st);
|
||||
void line_free(struct vt_line *l);
|
||||
struct vt_extra *line_extra(const struct vt_line *l, int i);
|
||||
struct vt_extra *line_extra_for(struct vt_line *l, int i);
|
||||
void line_set(struct vt_line *l, int i, int32_t cp, vt_style st);
|
||||
void line_clear(struct vt_line *l, int from, int to, vt_style st);
|
||||
void line_blank(struct vt_line *l, vt_style st);
|
||||
void line_insert_cells(struct vt_line *l, int at, int n, vt_style st, int end);
|
||||
void line_delete_cells(struct vt_line *l, int at, int n, vt_style st, int end);
|
||||
void line_resize(struct vt_line *l, int n);
|
||||
void line_copy_from(struct vt_line *l, const struct vt_line *o);
|
||||
/* cell i's extras made those of e (copies; the picture and link shared) */
|
||||
void extra_assign(struct vt_line *l, int i, const struct vt_extra *e);
|
||||
/* the text from..to, trailing blanks dropped, as UTF-8 into b */
|
||||
void line_text(const struct vt_line *l, int from, int to, dmc_buf *b);
|
||||
|
||||
struct vt_image *image_new(int w, int h, int cell_w, int cell_h, int picture);
|
||||
void image_unref(struct vt_image *img);
|
||||
struct vt_link *link_new(const char *uri);
|
||||
void link_unref(struct vt_link *l);
|
||||
|
||||
void utf8_put(dmc_buf *b, uint32_t c);
|
||||
|
||||
/*
|
||||
* Bitmap glyphs (BitmapFont): w pixels wide (8, or up to 24 for a soft
|
||||
* font), h high; a row's leftmost pixel bit w - 1. The codes are sorted.
|
||||
*/
|
||||
struct vt_font {
|
||||
int w, h, n;
|
||||
const uint32_t *codes;
|
||||
const uint8_t *rows8; /* n * h, when w <= 8 */
|
||||
const uint32_t *rows32; /* n * h, otherwise */
|
||||
};
|
||||
int font_index(const struct vt_font *f, uint32_t cp);
|
||||
uint32_t font_row(const struct vt_font *f, uint32_t cp, int row);
|
||||
|
||||
/* the machines' fonts and sets (vt_data.c, generated) */
|
||||
extern const struct vt_font vt_font_8x8; /* Unscii's 8x8 */
|
||||
extern const struct vt_font vt_font_vga; /* code page 437, 8x16 */
|
||||
extern const uint32_t vt_set_c64_upper[256], vt_set_c64_lower[256];
|
||||
extern const uint32_t vt_set_vic_upper[256], vt_set_vic_lower[256];
|
||||
extern const uint32_t vt_set_pet_upper[256], vt_set_pet_lower[256];
|
||||
extern const uint32_t vt_set_atari_graphics[256];
|
||||
extern const uint32_t vt_set_atari_international[256];
|
||||
|
||||
/* the machines' character ROMs (SyncTERM's), 256 glyphs 8 wide in the
|
||||
machine's own order, 8 rows, 16 or both (NULL: not that size) */
|
||||
enum {
|
||||
ROM_VGA, ROM_VGA_THIN, ROM_C64_UPPER, ROM_C64_LOWER, ROM_C128_UPPER,
|
||||
ROM_C128_LOWER, ROM_ATARI, ROM_POT_NOODLE, ROM_MOSOUL,
|
||||
ROM_MICROKNIGHT_PLUS, ROM_TOPAZ_PLUS, ROM_MICROKNIGHT, ROM_TOPAZ,
|
||||
ROM_PRESTEL, ROM_ATARI_ST, ROM_RIPTERM, ROM_COUNT
|
||||
};
|
||||
struct vt_rom {
|
||||
const uint8_t *r8, *r16;
|
||||
};
|
||||
extern const struct vt_rom vt_roms[ROM_COUNT];
|
||||
|
||||
/* the terminal types' fonts (vt_fonts.c): each ROM read in its machine's
|
||||
set, Unscii's for what it hasn't got */
|
||||
enum {
|
||||
MF_NONE = -1, MF_VGA, MF_RIPTERM, MF_C64, MF_C128, MF_ATARI, MF_TOPAZ,
|
||||
MF_ATARI_ST, MF_PRESTEL, MF_COUNT
|
||||
};
|
||||
const struct vt_font *machine_font(int which);
|
||||
/* SyncTERM's font n (CSI SP D's), height 8 or 16 rows, its codes read as
|
||||
octets of a charset (CS_*); NULL if it isn't here */
|
||||
const struct vt_font *syncterm_font(int n, int height, int charset);
|
||||
/* an octet in the Atari ST's set as its code point */
|
||||
uint32_t atari_st_char(int b);
|
||||
/* teletext's English set, 0x20-0x7f */
|
||||
uint32_t viewdata_g0(int c);
|
||||
|
||||
/* the mosaics (teletext's 2x3 blocks): their code points and glyphs */
|
||||
#define MOSAIC_BASE 0xf1000
|
||||
uint32_t mosaic_code(int mask, int separated);
|
||||
int mosaic_bits(int c);
|
||||
extern const struct vt_font vt_font_mosaic;
|
||||
|
||||
/*
|
||||
* Character sets (vt_charsets.c)
|
||||
*/
|
||||
enum { CS_UTF8, CS_CP437, CS_LATIN1 };
|
||||
uint32_t cp437_decode(int b);
|
||||
int cp437_encode(uint32_t cp);
|
||||
/* text into octets of a set (what it can't have: '?') */
|
||||
void charset_encode(int cs, const uint32_t *cps, int n, dmc_buf *out);
|
||||
|
||||
/* the G sets a slot can hold */
|
||||
enum {
|
||||
G_ASCII, G_DEC_GRAPHICS, G_UK, G_VT52_GRAPHICS, G_DEC_SUPPLEMENTAL,
|
||||
G_DEC_TECHNICAL, G_LATIN1, G_DUTCH, G_FINNISH, G_FRENCH,
|
||||
G_FRENCH_CANADIAN, G_GERMAN, G_ITALIAN, G_NORWEGIAN_DANISH,
|
||||
G_SPANISH, G_SWEDISH, G_SWISS, G_PORTUGUESE, G_SOFT, G_COUNT
|
||||
};
|
||||
int gset_by_designator(const char *d, int is96);
|
||||
int gset_is96(int g);
|
||||
int gset_level(int g);
|
||||
/* its first designator ("" for none) */
|
||||
const char *gset_designator(int g);
|
||||
uint32_t gset_map(int g, uint32_t cp);
|
||||
uint32_t dec_graphics(uint32_t cp);
|
||||
|
||||
/* the cells a character takes: 0, 1 or 2 */
|
||||
int wcwidth_of(uint32_t cp);
|
||||
|
||||
/*
|
||||
* The emulator
|
||||
*/
|
||||
struct vt_interp;
|
||||
struct vt_profile;
|
||||
|
||||
/* G0-G3 and the shifts, as DECSC keeps them */
|
||||
struct vt_sets_saved {
|
||||
uint8_t g[4];
|
||||
int gl, gr;
|
||||
};
|
||||
|
||||
struct vt_saved {
|
||||
int set;
|
||||
int x, y;
|
||||
vt_style style;
|
||||
int wrap, origin;
|
||||
int have_sets;
|
||||
struct vt_sets_saved sets;
|
||||
};
|
||||
|
||||
/* a VT320's host-writable status line */
|
||||
struct vt_status {
|
||||
struct vt_line *line;
|
||||
int x;
|
||||
};
|
||||
|
||||
enum { MOUSE_NONE, MOUSE_X10, MOUSE_NORMAL, MOUSE_BUTTON, MOUSE_ANY };
|
||||
|
||||
#define VT_PAGES 6
|
||||
#define VT_MAX_IMAGE 2048
|
||||
#define VT_NO_COLOR 0
|
||||
|
||||
struct dmc_vt {
|
||||
int cols, rows;
|
||||
/* the scrollback: a ring, hist_n lines from hist_first */
|
||||
struct vt_line **hist;
|
||||
int hist_n, hist_max, hist_first;
|
||||
struct vt_line **main_s, **alt_s, **screen;
|
||||
/* the main screen's pages not shown (VT420), by number */
|
||||
struct vt_line **pages[VT_PAGES];
|
||||
int page;
|
||||
|
||||
int cx, cy, pending_wrap;
|
||||
vt_style style;
|
||||
|
||||
int cursor_visible, app_cursor, app_keypad, bracketed_paste;
|
||||
int reverse_video, mouse_mode, mouse_sgr, mouse_urxvt, mouse_utf8;
|
||||
int focus_events, cursor_style;
|
||||
int sixel_scrolling, sixel_cursor_right;
|
||||
int cell_pw, cell_ph;
|
||||
/* the remote's colours and the machines' palettes: 0-255, 256 fg,
|
||||
257 bg, 258 cursor; VT_NO_COLOR or opaque ARGB */
|
||||
uint32_t color_over[259];
|
||||
int requested_cols, requested_rows;
|
||||
int ice_colors, emulated_bps, leds, vt52_keys;
|
||||
struct vt_link *link;
|
||||
int join_next;
|
||||
int autowrap, origin_mode, insert_mode, newline_mode;
|
||||
/* ASCII Setup's receive items (DMC_VT_ASCII_*), and a CR just fed */
|
||||
int ascii, ascii_cr;
|
||||
int top, bottom;
|
||||
uint8_t *tabs;
|
||||
int lr_margins, left, right;
|
||||
int32_t last_char;
|
||||
struct vt_saved saved_main, saved_alt;
|
||||
int home_on_clear;
|
||||
int eight_bit_replies;
|
||||
int status_type;
|
||||
struct vt_status status;
|
||||
const struct vt_font *soft_glyphs;
|
||||
/* the font the host chose (CSI SP D), NULL: the profile's */
|
||||
const struct vt_font *host_font;
|
||||
|
||||
const struct vt_profile *profile;
|
||||
struct vt_interp *interp;
|
||||
|
||||
/* the decoder */
|
||||
int charset; /* in use */
|
||||
int user_charset; /* the user's, for profiles without their own */
|
||||
int u_need;
|
||||
uint32_t u_cp, u_min;
|
||||
|
||||
/* what the app reads */
|
||||
dmc_buf reply, text, title;
|
||||
int title_changed;
|
||||
int bells;
|
||||
uint32_t *music; /* hz * 100, ms, rest ms */
|
||||
int music_n, music_cap;
|
||||
dmc_buf print;
|
||||
char answerback[32];
|
||||
int changed;
|
||||
uint32_t image_ids;
|
||||
|
||||
/* the keys */
|
||||
int enter_mode; /* 0 CR, 1 CR LF, 2 LF */
|
||||
int backspace_code; /* 0x7f or 0x08 */
|
||||
/* the theme */
|
||||
uint32_t theme_fg, theme_bg, theme_cursor;
|
||||
uint32_t theme_pal[16];
|
||||
int bold_bright;
|
||||
};
|
||||
|
||||
/* the glyphs the cells are drawn with: the host's font, else the
|
||||
profile's (NULL: the view's font) */
|
||||
const struct vt_font *vt_glyphs(const dmc_vt *t);
|
||||
|
||||
/* the screen's operations (vt.c), as TerminalEmulator's */
|
||||
struct vt_line *vt_line(const dmc_vt *t, int row);
|
||||
void vt_print(dmc_vt *t, int32_t cp);
|
||||
int vt_line_width(const dmc_vt *t, int y);
|
||||
void vt_set_line_size(dmc_vt *t, int size);
|
||||
void vt_set_column_mode(dmc_vt *t, int wide);
|
||||
void vt_carriage_return(dmc_vt *t);
|
||||
void vt_backspace(dmc_vt *t);
|
||||
void vt_index(dmc_vt *t);
|
||||
void vt_reverse_index(dmc_vt *t);
|
||||
void vt_scroll_up(dmc_vt *t, int top, int bottom);
|
||||
void vt_scroll_down(dmc_vt *t, int top, int bottom);
|
||||
void vt_move_cursor(dmc_vt *t, int x, int y, int clamp_to_region);
|
||||
void vt_cursor_position(dmc_vt *t, int x, int y);
|
||||
void vt_set_margins(dmc_vt *t, int l, int r);
|
||||
void vt_set_lr_margins(dmc_vt *t, int on);
|
||||
void vt_set_region(dmc_vt *t, int top, int bottom);
|
||||
void vt_tab(dmc_vt *t, int n);
|
||||
void vt_back_tab(dmc_vt *t, int n);
|
||||
void vt_set_tab(dmc_vt *t);
|
||||
void vt_clear_tab(dmc_vt *t);
|
||||
void vt_clear_tabs(dmc_vt *t);
|
||||
void vt_erase_display(dmc_vt *t, int mode, int selective);
|
||||
void vt_erase_line(dmc_vt *t, int mode, int selective);
|
||||
void vt_erase_chars(dmc_vt *t, int n);
|
||||
void vt_insert_chars(dmc_vt *t, int n);
|
||||
void vt_delete_chars(dmc_vt *t, int n);
|
||||
void vt_insert_lines(dmc_vt *t, int n);
|
||||
void vt_delete_lines(dmc_vt *t, int n);
|
||||
void vt_set_alt_screen(dmc_vt *t, int on, int clear);
|
||||
int vt_alt_screen(const dmc_vt *t);
|
||||
void vt_save_cursor(dmc_vt *t);
|
||||
void vt_restore_cursor(dmc_vt *t);
|
||||
void vt_alignment_test(dmc_vt *t);
|
||||
void vt_soft_reset(dmc_vt *t);
|
||||
void vt_full_reset(dmc_vt *t);
|
||||
void vt_select_page(dmc_vt *t, int n, int home);
|
||||
int vt_within_margins(const dmc_vt *t);
|
||||
void vt_set_color_override(dmc_vt *t, int i, uint32_t argb);
|
||||
void vt_reset_colors(dmc_vt *t, int from, int to);
|
||||
uint32_t vt_color_override(const dmc_vt *t, int i);
|
||||
/* the colour the screen shows for i (0-255, 256 fg, 257 bg, 258 cursor) */
|
||||
uint32_t vt_shown_color(const dmc_vt *t, int i);
|
||||
/* s to the remote as Latin-1 octets (C1 forms when eight_bit_replies) */
|
||||
void vt_reply(dmc_vt *t, const char *s);
|
||||
void vt_replyf(dmc_vt *t, const char *fmt, ...);
|
||||
void vt_bell(dmc_vt *t);
|
||||
void vt_add_music(dmc_vt *t, double hz, int ms, int rest_ms);
|
||||
void vt_print_out(dmc_vt *t, const char *utf8);
|
||||
void vt_set_title(dmc_vt *t, const char *s);
|
||||
void vt_place_image(dmc_vt *t, struct vt_image *img);
|
||||
void vt_show_picture(dmc_vt *t, struct vt_image *img);
|
||||
void vt_tab_stops(const dmc_vt *t, dmc_buf *out);
|
||||
/* a machine's size and palette on the screen, and back */
|
||||
void vt_use_machine(dmc_vt *t, int cols, int rows, const uint32_t *pal,
|
||||
int npal, int fg, int bg);
|
||||
void vt_release_machine(dmc_vt *t);
|
||||
/* the status line's text as the view shows it */
|
||||
void vt_status_text(const dmc_vt *t, dmc_buf *out);
|
||||
void status_print(dmc_vt *t, int32_t cp, vt_style st);
|
||||
void status_move(dmc_vt *t, int col);
|
||||
void status_erase(dmc_vt *t, int mode, vt_style st);
|
||||
|
||||
/*
|
||||
* The interpreters: what reads the remote's characters
|
||||
*/
|
||||
struct vt_interp_ops {
|
||||
void (*process)(struct vt_interp *, int32_t cp);
|
||||
void (*reset)(struct vt_interp *);
|
||||
void (*soft_reset)(struct vt_interp *);
|
||||
/* G0-G3 for DECSC (NULL: none) */
|
||||
int (*save_sets)(struct vt_interp *, struct vt_sets_saved *);
|
||||
void (*restore_sets)(struct vt_interp *, const struct vt_sets_saved *);
|
||||
/* a key's octets into out: 1 if it has its own (even none), 0 the
|
||||
VT sequences */
|
||||
int (*key)(struct vt_interp *, int key, int mods, dmc_buf *out);
|
||||
int (*chr)(struct vt_interp *, uint32_t cp, int mods, dmc_buf *out);
|
||||
int (*tap)(struct vt_interp *, double fx, double fy, dmc_buf *out);
|
||||
void (*flush)(struct vt_interp *);
|
||||
void (*detach)(struct vt_interp *);
|
||||
void (*destroy)(struct vt_interp *);
|
||||
};
|
||||
|
||||
struct vt_interp {
|
||||
const struct vt_interp_ops *ops;
|
||||
dmc_vt *t;
|
||||
int reads_octets;
|
||||
};
|
||||
|
||||
void interp_free(struct vt_interp *in);
|
||||
|
||||
/* a DEC terminal's printer port (vt_dec.c): what the remote sends to the
|
||||
printer until ends[] (controller mode), lines as they're left (auto) */
|
||||
struct vt_printer {
|
||||
dmc_vt *t;
|
||||
int controller, autop;
|
||||
dmc_buf buf;
|
||||
const char *ends[2];
|
||||
};
|
||||
void printer_init(struct vt_printer *p, dmc_vt *t, const char *end1,
|
||||
const char *end2);
|
||||
void printer_start(struct vt_printer *p);
|
||||
void printer_feed(struct vt_printer *p, int32_t cp);
|
||||
void printer_line(struct vt_printer *p, int y);
|
||||
void printer_screen(struct vt_printer *p);
|
||||
void printer_reset(struct vt_printer *p);
|
||||
void printer_free(struct vt_printer *p);
|
||||
|
||||
/* the key modifiers */
|
||||
enum { M_SHIFT = 1, M_ALT = 2, M_CTRL = 4 };
|
||||
|
||||
/* the interpreters there are */
|
||||
struct vt_interp *dec_new(dmc_vt *t, const struct vt_profile *p);
|
||||
struct vt_interp *vt52_new(dmc_vt *t, int graphics_set, const char *identity,
|
||||
void (*on_ansi)(void *), void *ctx);
|
||||
struct vt_interp *atari_st_new(dmc_vt *t, const struct vt_profile *p);
|
||||
struct vt_interp *petscii_new(dmc_vt *t, const struct vt_profile *p);
|
||||
struct vt_interp *atascii_new(dmc_vt *t, const struct vt_profile *p);
|
||||
struct vt_interp *viewdata_new(dmc_vt *t, const struct vt_profile *p);
|
||||
struct vt_interp *minitel_new(dmc_vt *t, const struct vt_profile *p);
|
||||
struct vt_interp *tek_new(dmc_vt *t, void (*on_exit)(void *), void *ctx);
|
||||
|
||||
/*
|
||||
* Profiles: the kinds of terminal
|
||||
*/
|
||||
enum {
|
||||
K_VT52, K_DEC, K_ANSI_BBS, K_AVATAR, K_RIP, K_PETSCII, K_ATASCII,
|
||||
K_ATARI_ST, K_VIEWDATA, K_MINITEL, K_TEK
|
||||
};
|
||||
|
||||
/* what a DEC-style terminal has beyond its level's text */
|
||||
enum {
|
||||
F_COLOR = 1, F_SIXEL = 2, F_PRINTER = 4, F_XTERM = 8, F_TEK = 16,
|
||||
F_REGIS = 32
|
||||
};
|
||||
|
||||
/* a Commodore's attribute codes */
|
||||
enum { AC_NONE, AC_VDC, AC_PLUS4 };
|
||||
|
||||
struct vt_profile {
|
||||
const char *id, *label, *term, *telnet;
|
||||
int kind;
|
||||
/* a DEC terminal's */
|
||||
int level;
|
||||
unsigned features;
|
||||
const char *da1, *da2;
|
||||
int charset; /* CS_*, or -1: the user's */
|
||||
int home_on_clear;
|
||||
/* its cells' bitmap font (MF_*): its machine's ROM; MF_NONE: the
|
||||
view's font */
|
||||
int font;
|
||||
/* ANSI-BBS: SyncTERM's number for its font, what CSI SP D's font 0
|
||||
goes back to */
|
||||
int syncterm;
|
||||
/* ANSI-BBS: its sixteen colours over the theme's; a machine: its
|
||||
palette */
|
||||
const uint32_t *colors;
|
||||
int ncolors;
|
||||
/* a machine's screen */
|
||||
int columns, rows, default_color, background;
|
||||
/* a Commodore's */
|
||||
const uint32_t *upper, *lower;
|
||||
const uint8_t *ctrl_colors, *cbm_colors;
|
||||
int n_ctrl, n_cbm;
|
||||
int escapes, attr_codes, tab_lf;
|
||||
/* an Atari's */
|
||||
const uint32_t *atascii;
|
||||
/* what a capture's file is called (".seq"), NULL for the app's */
|
||||
const char *capture_ext;
|
||||
};
|
||||
|
||||
extern const struct vt_profile *const vt_profiles[];
|
||||
extern const int vt_profile_count;
|
||||
const struct vt_profile *vt_profile_by_id(const char *id);
|
||||
const struct vt_profile *vt_profile_default(void);
|
||||
/* the VT100 a Minitel's Téléinformatique mode is */
|
||||
extern const struct vt_profile vt_profile_vt100;
|
||||
/* the interpreter for a profile */
|
||||
struct vt_interp *profile_interp(dmc_vt *t, const struct vt_profile *p);
|
||||
|
||||
/*
|
||||
* Keys (vt_keys.c): what a key sends as a VT220/xterm
|
||||
*/
|
||||
void keys_key(const dmc_vt *t, int key, int mods, dmc_buf *out);
|
||||
void keys_char(uint32_t cp, int mods, dmc_buf *out);
|
||||
|
||||
/*
|
||||
* Graphics (vt_gfx.c)
|
||||
*/
|
||||
struct sixel;
|
||||
struct sixel *sixel_new(int transparent, uint32_t *shared, int max_w,
|
||||
int max_h);
|
||||
void sixel_feed(struct sixel *s, int32_t cp);
|
||||
struct vt_image *sixel_finish(dmc_vt *t, struct sixel *s, int cw, int ch);
|
||||
void sixel_free(struct sixel *s);
|
||||
uint32_t sixel_hls(int h, int l, int s);
|
||||
void sixel_default_palette(uint32_t p[256]);
|
||||
|
||||
/* a graphics terminal's picture: logical coordinates into pixels */
|
||||
enum { R_OVERLAY, R_REPLACE, R_COMPLEMENT, R_ERASE };
|
||||
struct raster {
|
||||
int width, height, lw, lh;
|
||||
uint32_t *px;
|
||||
int mode;
|
||||
uint32_t color, background;
|
||||
int pattern;
|
||||
const uint8_t *fill_pattern; /* 8 rows, NULL solid */
|
||||
uint8_t fill_pattern_buf[8];
|
||||
uint32_t fill_background;
|
||||
int clip[4], has_clip;
|
||||
int pattern_step;
|
||||
int dirty;
|
||||
};
|
||||
struct raster *raster_new(int w, int h, int lw, int lh);
|
||||
void raster_free(struct raster *r);
|
||||
int raster_px(const struct raster *r, int x);
|
||||
int raster_py(const struct raster *r, int y);
|
||||
void raster_clear(struct raster *r, uint32_t c);
|
||||
void raster_point(struct raster *r, int x, int y);
|
||||
void raster_line(struct raster *r, int x0, int y0, int x1, int y1);
|
||||
void raster_arc(struct raster *r, int cx, int cy, int rad, double start,
|
||||
double sweep);
|
||||
void raster_ellipse(struct raster *r, int cx, int cy, int rx, int ry,
|
||||
double start, double sweep);
|
||||
/* an elliptical arc's points (logical) into xy (pairs): how many */
|
||||
int raster_ellipse_points(int cx, int cy, int rx, int ry, double start,
|
||||
double sweep, int *xy, int max);
|
||||
void raster_fill(struct raster *r, const int *xy, int n);
|
||||
void raster_flood(struct raster *r, int x, int y, uint32_t border);
|
||||
struct raster_grab {
|
||||
int width, height;
|
||||
uint32_t *px;
|
||||
};
|
||||
void raster_grab(const struct raster *r, int x0, int y0, int x1, int y1,
|
||||
struct raster_grab *g);
|
||||
void raster_put(struct raster *r, const struct raster_grab *g, int x, int y,
|
||||
int xor_);
|
||||
void raster_pixel_rect(const struct raster *r, int x0, int y0, int x1, int y1,
|
||||
int out[4]);
|
||||
void raster_clear_rect(struct raster *r, int x0, int y0, int x1, int y1,
|
||||
uint32_t c);
|
||||
int raster_text(struct raster *r, int x, int y, const uint32_t *cps, int n,
|
||||
const struct vt_font *f, int cw, int ch);
|
||||
struct vt_image *raster_take(dmc_vt *t, struct raster *r, int cw, int ch);
|
||||
|
||||
/* a picture kept the screen's size, put on it when drawn on */
|
||||
struct picture_layer {
|
||||
dmc_vt *t;
|
||||
int lw, lh;
|
||||
struct raster *r;
|
||||
void (*setup)(struct raster *, void *);
|
||||
void *ctx;
|
||||
};
|
||||
void layer_init(struct picture_layer *l, dmc_vt *t, int lw, int lh,
|
||||
void (*setup)(struct raster *, void *), void *ctx);
|
||||
struct raster *layer_raster(struct picture_layer *l);
|
||||
void layer_reset(struct picture_layer *l);
|
||||
void layer_flush(struct picture_layer *l);
|
||||
void layer_free(struct picture_layer *l);
|
||||
|
||||
/* ReGIS (vt_regis.c) */
|
||||
struct regis;
|
||||
struct regis *regis_new(dmc_vt *t, uint32_t *registers);
|
||||
void regis_begin(struct regis *g, int p1);
|
||||
void regis_feed(struct regis *g, int32_t cp);
|
||||
void regis_end(struct regis *g);
|
||||
void regis_abort(struct regis *g);
|
||||
void regis_flush(struct regis *g);
|
||||
void regis_reset(struct regis *g);
|
||||
void regis_free(struct regis *g);
|
||||
|
||||
/* RIPscrip (vt_rip.c), over ANSI-BBS: what it doesn't take given back
|
||||
to ANSI-BBS's parser through replay */
|
||||
struct rip;
|
||||
struct rip *rip_new(dmc_vt *t, void (*replay)(void *, int32_t), void *ctx);
|
||||
/* a code point to RIP: 1 if it took it */
|
||||
int rip_intercept(struct rip *r, int32_t cp);
|
||||
int rip_tap(struct rip *r, double fx, double fy, dmc_buf *out);
|
||||
void rip_flush(struct rip *r);
|
||||
void rip_reset(struct rip *r);
|
||||
void rip_free(struct rip *r);
|
||||
/* ESC [ ! : RIP's version asked */
|
||||
void rip_query(struct rip *r);
|
||||
|
||||
/* ANSI music (vt_dec.c) */
|
||||
struct ansi_music {
|
||||
int tempo, octave, length;
|
||||
double sounding;
|
||||
};
|
||||
void music_reset(struct ansi_music *m);
|
||||
void music_play(dmc_vt *t, struct ansi_music *m, const char *s);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* xfer.h - the file transfers' parts (xfer*.c): a transfer runs on a
|
||||
* thread of its own, its line two queues (what came from the
|
||||
* remote, what goes to it) that the app's thread fills and
|
||||
* drains; a failure or a cancel goes back to the thread's start
|
||||
* with longjmp, the files it had open closed there.
|
||||
*/
|
||||
#ifndef DMC_XFER_H
|
||||
#define DMC_XFER_H
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "util.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#define XFER_PATH 1024
|
||||
#define XFER_NAME 256
|
||||
/* what may wait to go to the remote before the protocol's thread waits */
|
||||
#define XFER_OUT_MAX (32 * 1024)
|
||||
|
||||
/* a file to send */
|
||||
struct xfer_file {
|
||||
char *path;
|
||||
char name[XFER_NAME];
|
||||
long long size; /* -1 not known */
|
||||
long long mtime; /* seconds, 0 not known */
|
||||
};
|
||||
|
||||
struct dmc_xfer {
|
||||
int protocol, sending;
|
||||
char folder[XFER_PATH]; /* received files' */
|
||||
char xname[XFER_NAME]; /* XMODEM's (it sends no name) */
|
||||
struct xfer_file *files;
|
||||
int nfiles;
|
||||
|
||||
/* the line, and the status: the app's thread and the protocol's */
|
||||
#ifdef _WIN32
|
||||
CRITICAL_SECTION cs;
|
||||
HANDLE ev_in, ev_out, thread;
|
||||
#else
|
||||
pthread_mutex_t mu;
|
||||
pthread_cond_t cv_in, cv_out;
|
||||
pthread_t thread;
|
||||
#endif
|
||||
int started;
|
||||
void (*notify)(void *);
|
||||
void *notify_ctx;
|
||||
int notified;
|
||||
dmc_buf in, out;
|
||||
volatile int cancelled;
|
||||
dmc_xfer_status st;
|
||||
char **names;
|
||||
int nnames;
|
||||
|
||||
/* the protocol's thread's alone */
|
||||
unsigned char rbuf[4096];
|
||||
int rpos, rlen;
|
||||
jmp_buf jb;
|
||||
char why[256];
|
||||
FILE *src; /* the file being sent */
|
||||
FILE *dst; /* the file being received */
|
||||
char dst_path[XFER_PATH], dst_name[XFER_NAME + 16];
|
||||
int keep_on_cancel; /* plain text: what came is kept */
|
||||
int keep_partial; /* crash recovery: a part is kept */
|
||||
dmc_xfer_options opt;
|
||||
volatile int skip; /* the user skipped the file now */
|
||||
volatile int zstd; /* the file now goes with zstd */
|
||||
void *heap; /* freed at the end */
|
||||
void *proto; /* the protocol's state (Kermit's) */
|
||||
void (*proto_free)(void *); /* how it's freed (NULL: free) */
|
||||
};
|
||||
|
||||
/* plain text's quiet that ends a receive (the tests' shorter) */
|
||||
extern int xfer_text_quiet_ms;
|
||||
|
||||
/* the time in ms (monotonic) and a pause */
|
||||
long long xfer_now(void);
|
||||
void xfer_sleep(int ms);
|
||||
|
||||
/* a byte from the remote (0-255), -1 when none came within ms; a cancel
|
||||
jumps out */
|
||||
int xfer_read(dmc_xfer *x, int ms);
|
||||
/* the same, -2 for a cancel instead of the jump */
|
||||
int xfer_read_nc(dmc_xfer *x, int ms);
|
||||
/* the next byte left waiting, -1 for none: no wait */
|
||||
int xfer_peek(dmc_xfer *x);
|
||||
int xfer_available(dmc_xfer *x);
|
||||
/* what is waiting dropped */
|
||||
void xfer_purge(dmc_xfer *x);
|
||||
/* bytes to the remote (a cancel jumps out) */
|
||||
void xfer_write(dmc_xfer *x, const void *data, int n);
|
||||
void xfer_put(dmc_xfer *x, int c);
|
||||
int xfer_is_cancelled(dmc_xfer *x);
|
||||
|
||||
/* the transfer failed (why) or was cancelled: back to the start */
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
__attribute__((noreturn, format(printf, 2, 3)))
|
||||
#endif
|
||||
void xfer_fail(dmc_xfer *x, const char *fmt, ...);
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
__attribute__((noreturn))
|
||||
#endif
|
||||
void xfer_cancelled(dmc_xfer *x, const char *why);
|
||||
|
||||
/* the status as it goes */
|
||||
void xfer_file_start(dmc_xfer *x, const char *name, long long size);
|
||||
void xfer_bytes(dmc_xfer *x, long long bytes);
|
||||
void xfer_size(dmc_xfer *x, long long size);
|
||||
void xfer_file_done(dmc_xfer *x);
|
||||
void xfer_error(dmc_xfer *x);
|
||||
long long xfer_size_now(dmc_xfer *x);
|
||||
|
||||
/* a received file made in the folder (its name made safe and unique):
|
||||
x->dst open, x->dst_name the name it got */
|
||||
void xfer_create(dmc_xfer *x, const char *name);
|
||||
/* the same for crash recovery: a part of the file in the folder
|
||||
continued (its length, appended to), or a new one (0); -2 when the
|
||||
file is there whole (size known), and nothing opened */
|
||||
long long xfer_create_resume(dmc_xfer *x, const char *name, long long size);
|
||||
/* whether the file now goes compressed with zstd (ZMODEM) */
|
||||
void xfer_zstd(dmc_xfer *x, int on);
|
||||
/* the user's skip of the file now, taken (once) */
|
||||
int xfer_skip_wanted(dmc_xfer *x);
|
||||
/* the received file written */
|
||||
void xfer_dst_write(dmc_xfer *x, const void *data, int n);
|
||||
/* the received file whole (its time set, if mtime > 0), or dropped */
|
||||
void xfer_done(dmc_xfer *x, long long mtime);
|
||||
void xfer_abandon(dmc_xfer *x);
|
||||
/* a file to send opened (x->src), and closed */
|
||||
void xfer_open(dmc_xfer *x, const struct xfer_file *f);
|
||||
void xfer_close(dmc_xfer *x);
|
||||
/* up to n bytes of x->src: how many (0 at its end) */
|
||||
int xfer_src_read(dmc_xfer *x, void *buf, int n);
|
||||
|
||||
/* the checks: CRC-16/XMODEM, and CRC-32 kept inverted (ZMODEM's) */
|
||||
unsigned xfer_crc16(unsigned crc, const unsigned char *p, int n);
|
||||
unsigned xfer_crc16_byte(unsigned crc, int b);
|
||||
unsigned long xfer_crc32(unsigned long crc, const unsigned char *p, int n);
|
||||
unsigned long xfer_crc32_byte(unsigned long crc, int b);
|
||||
|
||||
/* the protocols (xfer_xy.c, xfer_z.c, xfer_kermit.c, xfer_hex.c) */
|
||||
void xy_receive_x(dmc_xfer *x);
|
||||
void xy_receive_y(dmc_xfer *x, int g);
|
||||
void xy_send_x(dmc_xfer *x, int one_k);
|
||||
void xy_send_y(dmc_xfer *x, int g);
|
||||
void z_receive(dmc_xfer *x);
|
||||
void z_send(dmc_xfer *x);
|
||||
void kermit_receive(dmc_xfer *x);
|
||||
void kermit_send(dmc_xfer *x);
|
||||
/* Kermit's E packet that ends it, for a cancel: its length */
|
||||
int kermit_cancel_bytes(dmc_xfer *x, char *out, int max);
|
||||
void hex_receive(dmc_xfer *x, int srec);
|
||||
void hex_send(dmc_xfer *x, int srec);
|
||||
void text_receive(dmc_xfer *x);
|
||||
void text_send(dmc_xfer *x);
|
||||
|
||||
/* ZMODEM's starts, and what cancels it */
|
||||
extern const char z_sz_start[6], z_rz_start[6];
|
||||
extern const char z_cancel[20];
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,522 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* xfer_hex.c - Intel HEX and Motorola S-records: a binary image as lines
|
||||
* of text, each a record of an address, its data and a
|
||||
* checksum, for EPROM programmers, monitor ROMs and boot
|
||||
* loaders, which take them on a plain serial line. No
|
||||
* protocol but the records' own checksums.
|
||||
*
|
||||
* Intel HEX: ":" count, address, type, data, checksum (the two's
|
||||
* complement of the bytes' sum); types 00 data, 01 the end, 02 an
|
||||
* extended segment address (x16), 04 an extended linear address (the
|
||||
* upper 16 bits), 03 and 05 a start address (taken, unused).
|
||||
*
|
||||
* S-records: "S" type, count (address, data and checksum's bytes),
|
||||
* address, data, checksum (the one's complement of the bytes' sum); S0 a
|
||||
* header, S1/S2/S3 data at 16/24/32-bit addresses, S5/S6 the count of
|
||||
* data records, S7/S8/S9 the end (with a start address).
|
||||
*
|
||||
* Sent: the file as records, a line each (CR LF), or as it is if it is
|
||||
* the format's records already. Received: the records that come, until
|
||||
* the end record (or a pause once some have come), as the binary image
|
||||
* they hold (gaps 0xFF, as an erased EPROM has them).
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "xfer.h"
|
||||
|
||||
#define RECORD_BYTES 16
|
||||
/* the largest image put together (64 MB) */
|
||||
#define MAX_IMAGE (64LL << 20)
|
||||
#define FIRST_MS 60000
|
||||
#define PAUSE_MS 10000
|
||||
/* a line longer than any record: the rest of it dropped */
|
||||
#define MAX_LINE 600
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* reading records */
|
||||
|
||||
struct chunk {
|
||||
long long addr;
|
||||
size_t off;
|
||||
int len;
|
||||
};
|
||||
|
||||
struct decoder {
|
||||
int srec;
|
||||
int ended, records;
|
||||
long long bytes, upper;
|
||||
struct chunk *chunks;
|
||||
int nchunks, cap;
|
||||
unsigned char *data;
|
||||
size_t dn, dcap;
|
||||
char why[200];
|
||||
};
|
||||
|
||||
static void dec_free(void *p)
|
||||
{
|
||||
struct decoder *d = (struct decoder *)p;
|
||||
if (!d)
|
||||
return;
|
||||
free(d->chunks);
|
||||
free(d->data);
|
||||
free(d);
|
||||
}
|
||||
|
||||
static int put(struct decoder *d, long long addr, const unsigned char *b, int n)
|
||||
{
|
||||
if (d->nchunks == d->cap) {
|
||||
int cap = d->cap ? d->cap * 2 : 64;
|
||||
struct chunk *c = (struct chunk *)realloc(d->chunks, sizeof(*c) * (size_t)cap);
|
||||
if (!c)
|
||||
return 0;
|
||||
d->chunks = c;
|
||||
d->cap = cap;
|
||||
}
|
||||
if (d->dn + (size_t)n > d->dcap) {
|
||||
size_t cap = d->dcap ? d->dcap * 2 : 4096;
|
||||
while (cap < d->dn + (size_t)n)
|
||||
cap *= 2;
|
||||
unsigned char *p = (unsigned char *)realloc(d->data, cap);
|
||||
if (!p)
|
||||
return 0;
|
||||
d->data = p;
|
||||
d->dcap = cap;
|
||||
}
|
||||
if (n > 0)
|
||||
memcpy(d->data + d->dn, b, (size_t)n);
|
||||
d->chunks[d->nchunks].addr = addr;
|
||||
d->chunks[d->nchunks].off = d->dn;
|
||||
d->chunks[d->nchunks].len = n;
|
||||
d->nchunks++;
|
||||
d->dn += (size_t)n;
|
||||
d->records++;
|
||||
d->bytes += n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* the bytes the hex digits s spell, into b: how many, -1 if not hex */
|
||||
static int hex_bytes(const char *s, unsigned char *b, int max)
|
||||
{
|
||||
int n = (int)strlen(s);
|
||||
if (n % 2)
|
||||
return -1;
|
||||
for (int i = 0; i < n; i++)
|
||||
if (!isxdigit((unsigned char)s[i]))
|
||||
return -1;
|
||||
if (n / 2 > max)
|
||||
return -1;
|
||||
for (int i = 0; i < n / 2; i++) {
|
||||
char t[3] = { s[2 * i], s[2 * i + 1], 0 };
|
||||
b[i] = (unsigned char)strtol(t, NULL, 16);
|
||||
}
|
||||
return n / 2;
|
||||
}
|
||||
|
||||
/* a line: 1 a record of the format, 0 not one, -1 a bad one (d->why) */
|
||||
static int dec_line(struct decoder *d, const char *raw)
|
||||
{
|
||||
char l[MAX_LINE + 1];
|
||||
while (*raw && isspace((unsigned char)*raw))
|
||||
raw++;
|
||||
snprintf(l, sizeof(l), "%s", raw);
|
||||
int ln = (int)strlen(l);
|
||||
while (ln && isspace((unsigned char)l[ln - 1]))
|
||||
l[--ln] = '\0';
|
||||
if (d->ended || !ln)
|
||||
return 0;
|
||||
unsigned char b[MAX_LINE / 2 + 2];
|
||||
if (!d->srec) {
|
||||
if (l[0] != ':')
|
||||
return 0;
|
||||
int n = hex_bytes(l + 1, b, (int)sizeof(b));
|
||||
if (n < 0) {
|
||||
snprintf(d->why, sizeof(d->why),
|
||||
"Intel HEX record %d: not hex digits", d->records + 1);
|
||||
return -1;
|
||||
}
|
||||
if (n < 5 || n != b[0] + 5) {
|
||||
snprintf(d->why, sizeof(d->why),
|
||||
"Intel HEX: a record's length is wrong: %.80s", l);
|
||||
return -1;
|
||||
}
|
||||
int sum = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
sum += b[i];
|
||||
if (sum & 0xff) {
|
||||
snprintf(d->why, sizeof(d->why),
|
||||
"Intel HEX: a checksum is wrong: %.80s", l);
|
||||
return -1;
|
||||
}
|
||||
int addr = (b[1] << 8) | b[2];
|
||||
switch (b[3]) {
|
||||
case 0:
|
||||
if (!put(d, d->upper + addr, b + 4, b[0])) {
|
||||
snprintf(d->why, sizeof(d->why), "out of memory");
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
d->ended = 1;
|
||||
break;
|
||||
case 2:
|
||||
d->upper = (long long)((b[4] << 8) | b[5]) << 4;
|
||||
break;
|
||||
case 4:
|
||||
d->upper = (long long)((b[4] << 8) | b[5]) << 16;
|
||||
break;
|
||||
case 3:
|
||||
case 5:
|
||||
break;
|
||||
default:
|
||||
snprintf(d->why, sizeof(d->why),
|
||||
"Intel HEX: an unknown record type: %.80s", l);
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (ln < 2 || (l[0] != 'S' && l[0] != 's') || !isdigit((unsigned char)l[1]))
|
||||
return 0;
|
||||
int type = l[1] - '0';
|
||||
int n = hex_bytes(l + 2, b, (int)sizeof(b));
|
||||
if (n < 0) {
|
||||
snprintf(d->why, sizeof(d->why), "S-record %d: not hex digits",
|
||||
d->records + 1);
|
||||
return -1;
|
||||
}
|
||||
if (n == 0 || n != b[0] + 1) {
|
||||
snprintf(d->why, sizeof(d->why),
|
||||
"S-record: a record's length is wrong: %.80s", l);
|
||||
return -1;
|
||||
}
|
||||
int sum = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
sum += b[i];
|
||||
if ((sum & 0xff) != 0xff) {
|
||||
snprintf(d->why, sizeof(d->why), "S-record: a checksum is wrong: %.80s", l);
|
||||
return -1;
|
||||
}
|
||||
int ab;
|
||||
switch (type) {
|
||||
case 0: case 1: case 5: case 9: ab = 2; break;
|
||||
case 2: case 6: case 8: ab = 3; break;
|
||||
case 3: case 7: ab = 4; break;
|
||||
default:
|
||||
snprintf(d->why, sizeof(d->why), "S-record: an unknown type: %.80s", l);
|
||||
return -1;
|
||||
}
|
||||
if (n < 1 + ab + 1) {
|
||||
snprintf(d->why, sizeof(d->why), "S-record: too short: %.80s", l);
|
||||
return -1;
|
||||
}
|
||||
long long addr = 0;
|
||||
for (int k = 0; k < ab; k++)
|
||||
addr = (addr << 8) | b[1 + k];
|
||||
if (type >= 1 && type <= 3) {
|
||||
if (!put(d, addr, b + 1 + ab, n - 1 - ab - 1)) {
|
||||
snprintf(d->why, sizeof(d->why), "out of memory");
|
||||
return -1;
|
||||
}
|
||||
} else if (type >= 7) {
|
||||
d->ended = 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* whether text is the format's records already (every line one, the end
|
||||
record among them) */
|
||||
static int is_records(const char *text, size_t n, int srec)
|
||||
{
|
||||
struct decoder d;
|
||||
memset(&d, 0, sizeof(d));
|
||||
d.srec = srec;
|
||||
size_t i = 0;
|
||||
int ok = 1;
|
||||
while (i < n && ok) {
|
||||
size_t e = i;
|
||||
while (e < n && text[e] != '\n' && text[e] != '\r')
|
||||
e++;
|
||||
if (e - i <= MAX_LINE) {
|
||||
char l[MAX_LINE + 1];
|
||||
memcpy(l, text + i, e - i);
|
||||
l[e - i] = '\0';
|
||||
int empty = 1;
|
||||
for (char *p = l; *p; p++)
|
||||
if (!isspace((unsigned char)*p))
|
||||
empty = 0;
|
||||
if (!empty && dec_line(&d, l) != 1 && !d.ended)
|
||||
ok = 0;
|
||||
} else {
|
||||
ok = 0;
|
||||
}
|
||||
i = e + 1;
|
||||
}
|
||||
int ended = d.ended;
|
||||
free(d.chunks);
|
||||
free(d.data);
|
||||
return ok && ended;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* writing records */
|
||||
|
||||
static const char digits[] = "0123456789ABCDEF";
|
||||
|
||||
static int hex_out(char *o, const unsigned char *b, int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
o[2 * i] = digits[b[i] >> 4];
|
||||
o[2 * i + 1] = digits[b[i] & 15];
|
||||
}
|
||||
return 2 * n;
|
||||
}
|
||||
|
||||
/* a line sent (count: only its length counted), CR LF after it */
|
||||
static long long emit(dmc_xfer *x, const char *line, int n, int count,
|
||||
long long *sent, long long total, const char *name)
|
||||
{
|
||||
(void)total;
|
||||
(void)name;
|
||||
if (!count) {
|
||||
if (xfer_is_cancelled(x))
|
||||
xfer_cancelled(x, "cancelled");
|
||||
xfer_write(x, line, n);
|
||||
xfer_write(x, "\r\n", 2);
|
||||
*sent += n + 2;
|
||||
xfer_bytes(x, *sent);
|
||||
}
|
||||
return n + 2;
|
||||
}
|
||||
|
||||
static int intel_record(char *o, int addr, int type, const unsigned char *data, int n)
|
||||
{
|
||||
unsigned char b[4 + 255 + 1];
|
||||
b[0] = (unsigned char)n;
|
||||
b[1] = (unsigned char)(addr >> 8);
|
||||
b[2] = (unsigned char)addr;
|
||||
b[3] = (unsigned char)type;
|
||||
if (n > 0)
|
||||
memcpy(b + 4, data, (size_t)n);
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 4 + n; i++)
|
||||
sum += b[i];
|
||||
b[4 + n] = (unsigned char)(-sum);
|
||||
o[0] = ':';
|
||||
return 1 + hex_out(o + 1, b, 5 + n);
|
||||
}
|
||||
|
||||
static int srec_record(char *o, int type, int ab, long long addr,
|
||||
const unsigned char *data, int n)
|
||||
{
|
||||
unsigned char b[1 + 4 + 255 + 1];
|
||||
int k = 0;
|
||||
b[k++] = (unsigned char)(ab + n + 1);
|
||||
for (int i = 0; i < ab; i++)
|
||||
b[k++] = (unsigned char)(addr >> (8 * (ab - 1 - i)));
|
||||
if (n > 0)
|
||||
memcpy(b + k, data, (size_t)n);
|
||||
k += n;
|
||||
int sum = 0;
|
||||
for (int i = 0; i < k; i++)
|
||||
sum += b[i];
|
||||
b[k++] = (unsigned char)~sum;
|
||||
o[0] = 'S';
|
||||
o[1] = (char)('0' + type);
|
||||
return 2 + hex_out(o + 2, b, k);
|
||||
}
|
||||
|
||||
/* data as the format's records (S-records: a header of name first),
|
||||
sent, or only counted: their length with CR LFs */
|
||||
static long long encode(dmc_xfer *x, const unsigned char *data, long long dn,
|
||||
int srec, const char *name, int count, long long total)
|
||||
{
|
||||
char l[MAX_LINE];
|
||||
long long len = 0, sent = 0;
|
||||
if (!srec) {
|
||||
long long upper = -1;
|
||||
long long i = 0;
|
||||
while (i < dn) {
|
||||
long long addr = i;
|
||||
/* (a record doesn't cross a 64 KB boundary) */
|
||||
if ((addr >> 16) != upper) {
|
||||
upper = addr >> 16;
|
||||
unsigned char u[2] = { (unsigned char)(upper >> 8),
|
||||
(unsigned char)upper };
|
||||
len += emit(x, l, intel_record(l, 0, 4, u, 2), count,
|
||||
&sent, total, name);
|
||||
}
|
||||
long long room = 0x10000 - (addr & 0xffff);
|
||||
int n = RECORD_BYTES;
|
||||
if (dn - i < n)
|
||||
n = (int)(dn - i);
|
||||
if (room < n)
|
||||
n = (int)room;
|
||||
len += emit(x, l, intel_record(l, (int)(addr & 0xffff), 0,
|
||||
data + i, n), count, &sent,
|
||||
total, name);
|
||||
i += n;
|
||||
}
|
||||
len += emit(x, ":00000001FF", 11, count, &sent, total, name);
|
||||
return len;
|
||||
}
|
||||
long long last = (dn > 0 ? dn : 1) - 1;
|
||||
int type, ab, end;
|
||||
if (last <= 0xffff) {
|
||||
type = 1; ab = 2; end = 9;
|
||||
} else if (last <= 0xffffff) {
|
||||
type = 2; ab = 3; end = 8;
|
||||
} else {
|
||||
type = 3; ab = 4; end = 7;
|
||||
}
|
||||
int nl = (int)strlen(name);
|
||||
if (nl > 60)
|
||||
nl = 60;
|
||||
len += emit(x, l, srec_record(l, 0, 2, 0, (const unsigned char *)name, nl),
|
||||
count, &sent, total, name);
|
||||
long long i = 0, records = 0;
|
||||
while (i < dn) {
|
||||
int n = dn - i < RECORD_BYTES ? (int)(dn - i) : RECORD_BYTES;
|
||||
len += emit(x, l, srec_record(l, type, ab, i, data + i, n), count,
|
||||
&sent, total, name);
|
||||
i += n;
|
||||
records++;
|
||||
}
|
||||
if (records <= 0xffff)
|
||||
len += emit(x, l, srec_record(l, 5, 2, records, NULL, 0), count,
|
||||
&sent, total, name);
|
||||
len += emit(x, l, srec_record(l, end, ab, 0, NULL, 0), count, &sent,
|
||||
total, name);
|
||||
return len;
|
||||
}
|
||||
|
||||
/* the file's records, sent as they are: their length, or sent */
|
||||
static long long send_as_is(dmc_xfer *x, const char *text, size_t n, int count,
|
||||
long long total, const char *name)
|
||||
{
|
||||
long long len = 0, sent = 0;
|
||||
size_t i = 0;
|
||||
while (i < n) {
|
||||
size_t e = i;
|
||||
while (e < n && text[e] != '\n' && text[e] != '\r')
|
||||
e++;
|
||||
size_t a = i, b = e;
|
||||
while (a < b && isspace((unsigned char)text[a]))
|
||||
a++;
|
||||
while (b > a && isspace((unsigned char)text[b - 1]))
|
||||
b--;
|
||||
if (b > a)
|
||||
len += emit(x, text + a, (int)(b - a), count, &sent, total, name);
|
||||
i = e + 1;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
void hex_send(dmc_xfer *x, int srec)
|
||||
{
|
||||
const struct xfer_file *f = &x->files[0];
|
||||
const char *label = srec ? "S-records" : "Intel HEX";
|
||||
xfer_open(x, f);
|
||||
size_t cap = 65536, n = 0;
|
||||
unsigned char *data = (unsigned char *)malloc(cap + 1);
|
||||
if (!data)
|
||||
xfer_fail(x, "out of memory");
|
||||
x->heap = data;
|
||||
for (;;) {
|
||||
if (n == cap) {
|
||||
if ((long long)cap >= MAX_IMAGE)
|
||||
xfer_fail(x, "%s: too big for %s", f->name, label);
|
||||
unsigned char *p = (unsigned char *)realloc(data, cap * 2 + 1);
|
||||
if (!p)
|
||||
xfer_fail(x, "out of memory");
|
||||
data = p;
|
||||
x->heap = data;
|
||||
cap *= 2;
|
||||
}
|
||||
int r = xfer_src_read(x, data + n, (int)(cap - n > 65536 ? 65536 : cap - n));
|
||||
if (r <= 0)
|
||||
break;
|
||||
n += (size_t)r;
|
||||
}
|
||||
xfer_close(x);
|
||||
data[n] = 0;
|
||||
long long total;
|
||||
if (is_records((const char *)data, n, srec)) {
|
||||
total = send_as_is(x, (const char *)data, n, 1, 0, f->name);
|
||||
xfer_file_start(x, f->name, total);
|
||||
send_as_is(x, (const char *)data, n, 0, total, f->name);
|
||||
} else {
|
||||
total = encode(x, data, (long long)n, srec, f->name, 1, 0);
|
||||
xfer_file_start(x, f->name, total);
|
||||
encode(x, data, (long long)n, srec, f->name, 0, total);
|
||||
}
|
||||
xfer_bytes(x, total);
|
||||
xfer_file_done(x);
|
||||
}
|
||||
|
||||
void hex_receive(dmc_xfer *x, int srec)
|
||||
{
|
||||
const char *label = srec ? "S-record" : "Intel HEX";
|
||||
struct decoder *d = (struct decoder *)calloc(1, sizeof(*d));
|
||||
if (!d)
|
||||
xfer_fail(x, "out of memory");
|
||||
d->srec = srec;
|
||||
x->proto = d;
|
||||
x->proto_free = dec_free;
|
||||
char line[MAX_LINE + 1];
|
||||
int ln = 0;
|
||||
xfer_file_start(x, x->xname, -1);
|
||||
while (!d->ended) {
|
||||
int c = xfer_read(x, d->records == 0 ? FIRST_MS : PAUSE_MS);
|
||||
if (c < 0) {
|
||||
if (d->records == 0)
|
||||
xfer_fail(x, "no %s records came", label);
|
||||
/* (a sender that sends no end record: done once it
|
||||
pauses) */
|
||||
break;
|
||||
}
|
||||
if (c == '\r' || c == '\n') {
|
||||
if (ln) {
|
||||
line[ln] = '\0';
|
||||
if (dec_line(d, line) < 0)
|
||||
xfer_fail(x, "%s", d->why);
|
||||
ln = 0;
|
||||
xfer_bytes(x, d->bytes);
|
||||
}
|
||||
} else if (ln < MAX_LINE) {
|
||||
line[ln++] = (char)c;
|
||||
}
|
||||
}
|
||||
/* the image from the lowest address to the end of the highest
|
||||
record */
|
||||
long long first = 0, end = 0;
|
||||
for (int i = 0; i < d->nchunks; i++) {
|
||||
long long a = d->chunks[i].addr, e = a + d->chunks[i].len;
|
||||
if (i == 0 || a < first)
|
||||
first = a;
|
||||
if (i == 0 || e > end)
|
||||
end = e;
|
||||
}
|
||||
if (end - first > MAX_IMAGE)
|
||||
xfer_fail(x, "the records span %lld bytes: too many for an image",
|
||||
end - first);
|
||||
size_t size = (size_t)(end - first);
|
||||
unsigned char *img = (unsigned char *)malloc(size ? size : 1);
|
||||
if (!img)
|
||||
xfer_fail(x, "out of memory");
|
||||
x->heap = img;
|
||||
memset(img, 0xff, size);
|
||||
/* (in the order they came: a record written again wins) */
|
||||
for (int i = 0; i < d->nchunks; i++)
|
||||
memcpy(img + (d->chunks[i].addr - first), d->data + d->chunks[i].off,
|
||||
(size_t)d->chunks[i].len);
|
||||
xfer_create(x, x->xname);
|
||||
xfer_dst_write(x, img, (int)size);
|
||||
xfer_done(x, 0);
|
||||
xfer_file_start(x, x->dst_name, (long long)size);
|
||||
xfer_bytes(x, (long long)size);
|
||||
xfer_file_done(x);
|
||||
}
|
||||
@@ -0,0 +1,822 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* xfer_kermit.c - Kermit, both ways, stop-and-wait: packets of printable
|
||||
* characters, control characters prefixed, the 8th bit
|
||||
* prefixed when the line is 7-bit, runs of a byte
|
||||
* counted; long packets, 1-, 2- and 3-character (CRC)
|
||||
* checks, file attributes (size and time), as both sides
|
||||
* agree in the S/Y exchange. A bad or missing packet is
|
||||
* asked for again (NAK) up to MAX_RETRIES times; an E
|
||||
* packet ends it.
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "xfer.h"
|
||||
|
||||
#define MARK 1
|
||||
#define CR 13
|
||||
#define OUR_QCTL '#'
|
||||
#define REPT '~'
|
||||
#define CAP_LP 2
|
||||
#define CAP_AT 8
|
||||
/* the long packets asked for */
|
||||
#define OUR_LONG 4000
|
||||
#define MAX_RETRIES 10
|
||||
#define TIMEOUT 10000
|
||||
#define MAX_PACKET 9100
|
||||
|
||||
#define tochar(v) ((v) + 32)
|
||||
#define unchar(v) ((v) - 32)
|
||||
#define ctl(v) ((v) ^ 64)
|
||||
|
||||
struct kermit {
|
||||
dmc_xfer *x;
|
||||
int seven_bit;
|
||||
/* what's agreed: the defaults until the S/Y exchange */
|
||||
int send_max, send_long, eol, npad, padc, their_qctl, qbin, rept;
|
||||
int check, attributes;
|
||||
volatile int seq; /* the next packet's (a cancel's E too) */
|
||||
volatile int force_check1; /* during the S/Y exchange: type 1 */
|
||||
int last_sent_type;
|
||||
/* the packet that came */
|
||||
int pseq, ptype, plen;
|
||||
unsigned char pkt[MAX_PACKET + 16];
|
||||
/* an ACK's data, kept */
|
||||
unsigned char ack[MAX_PACKET + 16];
|
||||
int ack_len;
|
||||
unsigned char obuf[MAX_PACKET + 64];
|
||||
};
|
||||
|
||||
/* CRC-16/KERMIT (CCITT, bits reflected, from 0) */
|
||||
static unsigned kcrc(const unsigned char *b, int n)
|
||||
{
|
||||
unsigned c = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
c ^= b[i];
|
||||
for (int k = 0; k < 8; k++)
|
||||
c = c & 1 ? (c >> 1) ^ 0x8408 : c >> 1;
|
||||
}
|
||||
return c & 0xffff;
|
||||
}
|
||||
|
||||
/* the block check of type 1, 2 or 3 over n bytes into out: its length */
|
||||
static int check_chars(int type, const unsigned char *b, int n, unsigned char *out)
|
||||
{
|
||||
int s = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
s += b[i];
|
||||
if (type == 1) {
|
||||
out[0] = (unsigned char)tochar((s + ((s & 192) >> 6)) & 63);
|
||||
return 1;
|
||||
}
|
||||
if (type == 2) {
|
||||
out[0] = (unsigned char)tochar((s >> 6) & 63);
|
||||
out[1] = (unsigned char)tochar(s & 63);
|
||||
return 2;
|
||||
}
|
||||
unsigned c = kcrc(b, n);
|
||||
out[0] = (unsigned char)tochar((c >> 12) & 0x0f);
|
||||
out[1] = (unsigned char)tochar((c >> 6) & 63);
|
||||
out[2] = (unsigned char)tochar(c & 63);
|
||||
return 3;
|
||||
}
|
||||
|
||||
static int check1(const unsigned char *b, int n)
|
||||
{
|
||||
unsigned char c[3];
|
||||
check_chars(1, b, n, c);
|
||||
return c[0];
|
||||
}
|
||||
|
||||
/* the check type a packet goes with: 1 for S, I and R (and in the S/Y
|
||||
exchange), else the agreed one */
|
||||
static int check_len(int check, int force1, int type)
|
||||
{
|
||||
return type == 'S' || type == 'I' || type == 'R' || force1 ? 1 : check;
|
||||
}
|
||||
|
||||
/* a packet's bytes into out: padding, MARK, its header (an extended one
|
||||
for long data), data, its check and EOL; their length */
|
||||
static int packet_bytes(unsigned char *out, int npad, int padc, int eol,
|
||||
int check, int force1, int seq, int type,
|
||||
const unsigned char *data, int n)
|
||||
{
|
||||
int ct = check_len(check, force1, type), k = 0;
|
||||
for (int i = 0; i < npad; i++)
|
||||
out[k++] = (unsigned char)padc;
|
||||
out[k++] = MARK;
|
||||
int body = k;
|
||||
if (n + 2 + ct <= 94) {
|
||||
out[k++] = (unsigned char)tochar(n + 2 + ct);
|
||||
out[k++] = (unsigned char)tochar(seq);
|
||||
out[k++] = (unsigned char)type;
|
||||
} else {
|
||||
int lx = n + ct;
|
||||
out[k++] = (unsigned char)tochar(0);
|
||||
out[k++] = (unsigned char)tochar(seq);
|
||||
out[k++] = (unsigned char)type;
|
||||
out[k++] = (unsigned char)tochar(lx / 95);
|
||||
out[k++] = (unsigned char)tochar(lx % 95);
|
||||
out[k] = (unsigned char)check1(out + body, 5);
|
||||
k++;
|
||||
}
|
||||
if (n > 0)
|
||||
memcpy(out + k, data, (size_t)n);
|
||||
k += n;
|
||||
k += check_chars(ct, out + body, k - body, out + k);
|
||||
out[k++] = (unsigned char)eol;
|
||||
return k;
|
||||
}
|
||||
|
||||
static void send_packet(struct kermit *k, int seq, int type,
|
||||
const unsigned char *data, int n)
|
||||
{
|
||||
int len = packet_bytes(k->obuf, k->npad, k->padc, k->eol, k->check,
|
||||
k->force_check1, seq, type, data, n);
|
||||
xfer_write(k->x, k->obuf, len);
|
||||
}
|
||||
|
||||
/* the next good packet into k (1), or 0 on a timeout or a bad one
|
||||
(garbage before its mark skipped; a mark in the middle starts over) */
|
||||
static int read_packet(struct kermit *k, int wait)
|
||||
{
|
||||
long long deadline = xfer_now() + wait;
|
||||
unsigned char *buf = k->pkt;
|
||||
#define NEXT() (xfer_now() >= deadline ? -1 : xfer_read(k->x, (int)(deadline - xfer_now())))
|
||||
int c = NEXT();
|
||||
for (;;) {
|
||||
while (c != MARK) {
|
||||
if (c < 0)
|
||||
return 0;
|
||||
c = NEXT();
|
||||
}
|
||||
int n = 0, v, restart = 0;
|
||||
int hdr[3];
|
||||
for (int i = 0; i < 3 && !restart; i++) {
|
||||
v = NEXT();
|
||||
if (v < 0)
|
||||
return 0;
|
||||
if (v == MARK)
|
||||
restart = 1;
|
||||
else
|
||||
hdr[i] = buf[n++] = (unsigned char)v;
|
||||
}
|
||||
if (restart) {
|
||||
c = MARK;
|
||||
continue;
|
||||
}
|
||||
int len = unchar(hdr[0]), pseq = unchar(hdr[1]), type = hdr[2], rest;
|
||||
if (len == 0) {
|
||||
/* extended: LENX1 LENX2 HCHECK */
|
||||
int xx[3];
|
||||
for (int i = 0; i < 3 && !restart; i++) {
|
||||
v = NEXT();
|
||||
if (v < 0)
|
||||
return 0;
|
||||
if (v == MARK)
|
||||
restart = 1;
|
||||
else
|
||||
xx[i] = buf[n++] = (unsigned char)v;
|
||||
}
|
||||
if (restart) {
|
||||
c = MARK;
|
||||
continue;
|
||||
}
|
||||
if (check1(buf, 5) != xx[2])
|
||||
return 0;
|
||||
rest = unchar(xx[0]) * 95 + unchar(xx[1]);
|
||||
} else {
|
||||
if (len < 3)
|
||||
return 0;
|
||||
rest = len - 2;
|
||||
}
|
||||
if (rest > MAX_PACKET || rest < 0)
|
||||
return 0;
|
||||
int header_len = n;
|
||||
for (int i = 0; i < rest && !restart; i++) {
|
||||
v = NEXT();
|
||||
if (v < 0)
|
||||
return 0;
|
||||
if (v == MARK)
|
||||
restart = 1;
|
||||
else
|
||||
buf[n++] = (unsigned char)v;
|
||||
}
|
||||
if (restart) {
|
||||
c = MARK;
|
||||
continue;
|
||||
}
|
||||
int ct;
|
||||
if (type == 'S' || type == 'I' || type == 'R' || k->force_check1)
|
||||
ct = 1;
|
||||
else if (type == 'Y' && pseq == 0 && k->last_sent_type == 'S')
|
||||
ct = 1; /* (an S packet's ACK: type 1 too) */
|
||||
else
|
||||
ct = k->check;
|
||||
if (rest < ct)
|
||||
return 0;
|
||||
int data_end = n - ct;
|
||||
unsigned char want[3];
|
||||
check_chars(ct, buf, data_end, want);
|
||||
if (memcmp(buf + data_end, want, (size_t)ct) != 0) {
|
||||
/* (an E or N packet sent before the check type changed) */
|
||||
if ((type == 'N' || type == 'E') && ct != 1 && rest >= 1) {
|
||||
unsigned char w1[3];
|
||||
check_chars(1, buf, n - 1, w1);
|
||||
if (buf[n - 1] == w1[0]) {
|
||||
k->pseq = pseq;
|
||||
k->ptype = type;
|
||||
k->plen = n - 1 - header_len;
|
||||
memmove(buf, buf + header_len, (size_t)k->plen);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
k->pseq = pseq;
|
||||
k->ptype = type;
|
||||
k->plen = data_end - header_len;
|
||||
memmove(buf, buf + header_len, (size_t)k->plen);
|
||||
return 1;
|
||||
}
|
||||
#undef NEXT
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* the data's encoding */
|
||||
|
||||
static void encode_byte(struct kermit *k, unsigned char *out, int *n, int v)
|
||||
{
|
||||
int b = v;
|
||||
if (k->qbin && (b & 0x80)) {
|
||||
out[(*n)++] = (unsigned char)k->qbin;
|
||||
b &= 0x7f;
|
||||
}
|
||||
int b7 = b & 0x7f;
|
||||
if (b7 < 32 || b7 == 127) {
|
||||
out[(*n)++] = OUR_QCTL;
|
||||
out[(*n)++] = (unsigned char)ctl(b);
|
||||
} else if (b7 == OUR_QCTL || (k->qbin && b7 == k->qbin) ||
|
||||
(k->rept && b7 == k->rept)) {
|
||||
out[(*n)++] = OUR_QCTL;
|
||||
out[(*n)++] = (unsigned char)b;
|
||||
} else {
|
||||
out[(*n)++] = (unsigned char)b;
|
||||
}
|
||||
}
|
||||
|
||||
/* src's len bytes encoded into at most max characters of out (*on of
|
||||
them): how many bytes they hold */
|
||||
static int encode(struct kermit *k, const unsigned char *src, int len,
|
||||
unsigned char *out, int max, int *on)
|
||||
{
|
||||
int i = 0, n = 0;
|
||||
while (i < len) {
|
||||
int v = src[i], run = 1;
|
||||
if (k->rept)
|
||||
while (i + run < len && run < 94 && src[i + run] == src[i])
|
||||
run++;
|
||||
unsigned char unit[8];
|
||||
int un = 0;
|
||||
if (run >= 3) {
|
||||
unit[un++] = (unsigned char)k->rept;
|
||||
unit[un++] = (unsigned char)tochar(run);
|
||||
encode_byte(k, unit, &un, v);
|
||||
} else {
|
||||
run = 1;
|
||||
encode_byte(k, unit, &un, v);
|
||||
}
|
||||
if (n + un > max)
|
||||
break;
|
||||
memcpy(out + n, unit, (size_t)un);
|
||||
n += un;
|
||||
i += run;
|
||||
}
|
||||
*on = n;
|
||||
return i;
|
||||
}
|
||||
|
||||
/* a packet's data decoded: into the received file, else into text (up
|
||||
to tmax bytes); the bytes it held */
|
||||
static long long decode(struct kermit *k, const unsigned char *d, int dn,
|
||||
char *text, int tmax)
|
||||
{
|
||||
unsigned char out[4096];
|
||||
int on = 0, tn = 0;
|
||||
long long total = 0;
|
||||
int i = 0;
|
||||
while (i < dn) {
|
||||
int run = 1, c = d[i++];
|
||||
if (k->rept && c == k->rept && i < dn) {
|
||||
run = unchar(d[i++]);
|
||||
if (i >= dn)
|
||||
break;
|
||||
c = d[i++];
|
||||
}
|
||||
int hi = 0;
|
||||
if (k->qbin && c == k->qbin && i < dn) {
|
||||
hi = 0x80;
|
||||
c = d[i++];
|
||||
}
|
||||
if (c == k->their_qctl && i < dn) {
|
||||
c = d[i++];
|
||||
int c7 = c & 0x7f;
|
||||
if (c7 >= 63 && c7 <= 95)
|
||||
c = ctl(c);
|
||||
}
|
||||
c |= hi;
|
||||
for (int r = 0; r < run; r++) {
|
||||
if (text) {
|
||||
if (tn < tmax - 1)
|
||||
text[tn++] = (char)c;
|
||||
} else {
|
||||
out[on++] = (unsigned char)c;
|
||||
if (on == (int)sizeof(out)) {
|
||||
xfer_dst_write(k->x, out, on);
|
||||
on = 0;
|
||||
}
|
||||
}
|
||||
total++;
|
||||
}
|
||||
}
|
||||
if (text)
|
||||
text[tn] = '\0';
|
||||
else if (on)
|
||||
xfer_dst_write(k->x, out, on);
|
||||
return total;
|
||||
}
|
||||
|
||||
/* an E packet with why sent (if it can be), and the transfer failed */
|
||||
static void kfail(struct kermit *k, const char *why)
|
||||
{
|
||||
unsigned char e[600];
|
||||
int n;
|
||||
encode(k, (const unsigned char *)why, (int)strlen(why), e, (int)sizeof(e), &n);
|
||||
if (!xfer_is_cancelled(k->x))
|
||||
send_packet(k, k->seq, 'E', e, n);
|
||||
xfer_fail(k->x, "%s", why);
|
||||
}
|
||||
|
||||
int kermit_cancel_bytes(dmc_xfer *x, char *out, int max)
|
||||
{
|
||||
struct kermit *k = (struct kermit *)x->proto;
|
||||
int check = k ? k->check : 1, force1 = k ? k->force_check1 : 1;
|
||||
int eol = k ? k->eol : CR, seq = k ? k->seq : 0;
|
||||
unsigned char b[64];
|
||||
int n = packet_bytes(b, 0, 0, eol, check, force1, seq, 'E',
|
||||
(const unsigned char *)"cancelled", 9);
|
||||
if (n > max)
|
||||
n = max;
|
||||
memcpy(out, b, (size_t)n);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* the S/Y exchange */
|
||||
|
||||
/* our Send-Init: MAXL, TIME, NPAD, PADC, EOL, QCTL, QBIN, CHKT, REPT,
|
||||
CAPAS, WINDO, MAXLX1, MAXLX2 */
|
||||
static int our_params(struct kermit *k, unsigned char *b)
|
||||
{
|
||||
int n = 0, t = TIMEOUT / 1000;
|
||||
b[n++] = tochar(94);
|
||||
b[n++] = (unsigned char)tochar(t < 1 ? 1 : t > 94 ? 94 : t);
|
||||
b[n++] = tochar(0);
|
||||
b[n++] = ctl(0);
|
||||
b[n++] = tochar(CR);
|
||||
b[n++] = OUR_QCTL;
|
||||
b[n++] = k->seven_bit ? '&' : 'Y';
|
||||
b[n++] = '3';
|
||||
b[n++] = REPT;
|
||||
b[n++] = tochar(CAP_LP | CAP_AT);
|
||||
b[n++] = tochar(1);
|
||||
b[n++] = tochar(OUR_LONG / 95);
|
||||
b[n++] = tochar(OUR_LONG % 95);
|
||||
return n;
|
||||
}
|
||||
|
||||
static int is_prefix(int c)
|
||||
{
|
||||
return (c >= 33 && c <= 62) || (c >= 96 && c <= 126);
|
||||
}
|
||||
|
||||
/* their parameters, agreed with ours */
|
||||
static void agree(struct kermit *k, const unsigned char *d, int dn)
|
||||
{
|
||||
#define AT(i) ((i) < dn ? (int)d[i] : -1)
|
||||
if (AT(0) > 32) {
|
||||
int m = unchar(AT(0));
|
||||
k->send_max = m < 10 ? 10 : m > 94 ? 94 : m;
|
||||
}
|
||||
k->npad = AT(2) > 32 ? unchar(AT(2)) : 0;
|
||||
k->padc = AT(3) >= 0 ? ctl(AT(3)) : 0;
|
||||
k->eol = AT(4) > 32 ? unchar(AT(4)) : CR;
|
||||
k->their_qctl = AT(5) > 32 ? AT(5) : '#';
|
||||
int our_qbin = k->seven_bit ? '&' : 'Y', their_qbin = AT(6);
|
||||
if (their_qbin == 'Y' && our_qbin != 'Y')
|
||||
k->qbin = our_qbin;
|
||||
else if (is_prefix(their_qbin) && (our_qbin == 'Y' || our_qbin == their_qbin))
|
||||
k->qbin = their_qbin;
|
||||
else
|
||||
k->qbin = 0;
|
||||
int their_check = AT(7) >= '1' && AT(7) <= '3' ? AT(7) - '0' : 1;
|
||||
k->check = their_check;
|
||||
k->rept = AT(8) == REPT ? REPT : 0;
|
||||
/* the capabilities (more bytes follow while the lowest bit is set) */
|
||||
int i = 9, caps = 0;
|
||||
if (AT(i) > 32) {
|
||||
caps = unchar(AT(i));
|
||||
while (AT(i) >= 0 && (unchar(AT(i)) & 1))
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
k->attributes = (caps & CAP_AT) != 0;
|
||||
k->send_long = 0;
|
||||
if (caps & CAP_LP) {
|
||||
/* WINDO, then MAXLX1 MAXLX2 (none: 500) */
|
||||
int x1 = AT(i + 1), x2 = AT(i + 2);
|
||||
k->send_long = x1 > 32 || x2 > 32 ?
|
||||
unchar(x1 > 32 ? x1 : 32) * 95 + unchar(x2 > 32 ? x2 : 32) : 500;
|
||||
if (k->send_long < 95)
|
||||
k->send_long = 0;
|
||||
if (k->send_long > OUR_LONG)
|
||||
k->send_long = OUR_LONG;
|
||||
}
|
||||
if (k->qbin && k->qbin == k->rept)
|
||||
k->rept = 0;
|
||||
#undef AT
|
||||
}
|
||||
|
||||
/* room for data in a packet (an encoded byte is up to 5 characters) */
|
||||
static int room(struct kermit *k)
|
||||
{
|
||||
return k->send_long > 0 ? k->send_long - 5 - k->check - 2 :
|
||||
k->send_max - 2 - k->check;
|
||||
}
|
||||
|
||||
static struct kermit *k_new(dmc_xfer *x)
|
||||
{
|
||||
struct kermit *k = (struct kermit *)calloc(1, sizeof(*k));
|
||||
if (!k)
|
||||
xfer_fail(x, "out of memory");
|
||||
k->x = x;
|
||||
k->send_max = 80;
|
||||
k->eol = CR;
|
||||
k->their_qctl = '#';
|
||||
k->check = 1;
|
||||
k->force_check1 = 1;
|
||||
k->last_sent_type = ' ';
|
||||
x->proto = k;
|
||||
return k;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* sending */
|
||||
|
||||
/* a packet sent until its ACK comes (a NAK of the next one counts): the
|
||||
ACK's data in k->ack */
|
||||
static void exchange(struct kermit *k, int type, const unsigned char *data, int n)
|
||||
{
|
||||
int tries = 0;
|
||||
k->last_sent_type = type;
|
||||
for (;;) {
|
||||
send_packet(k, k->seq, type, data, n);
|
||||
while (read_packet(k, TIMEOUT)) {
|
||||
if (k->ptype == 'E') {
|
||||
char t[512];
|
||||
decode(k, k->pkt, k->plen, t, (int)sizeof(t));
|
||||
xfer_fail(k->x, "the receiver: %s", t);
|
||||
}
|
||||
if (k->ptype == 'Y' && k->pseq == k->seq) {
|
||||
k->seq = (k->seq + 1) & 63;
|
||||
memcpy(k->ack, k->pkt, (size_t)k->plen);
|
||||
k->ack_len = k->plen;
|
||||
return;
|
||||
}
|
||||
if (k->ptype == 'N' && k->pseq == ((k->seq + 1) & 63)) {
|
||||
k->seq = (k->seq + 1) & 63;
|
||||
k->ack_len = 0;
|
||||
return;
|
||||
}
|
||||
if (k->ptype == 'N')
|
||||
break;
|
||||
/* (an old ACK, repeated: the next one waited for) */
|
||||
}
|
||||
if (++tries > MAX_RETRIES)
|
||||
kfail(k, "too many retries");
|
||||
xfer_error(k->x);
|
||||
}
|
||||
}
|
||||
|
||||
static void attr(unsigned char *a, int *n, int tag, const char *v)
|
||||
{
|
||||
int l = (int)strlen(v);
|
||||
a[(*n)++] = (unsigned char)tag;
|
||||
a[(*n)++] = (unsigned char)tochar(l);
|
||||
memcpy(a + *n, v, (size_t)l);
|
||||
*n += l;
|
||||
}
|
||||
|
||||
/* seconds as an attribute's date in local time: yyyymmdd hh:mm:ss */
|
||||
static void format_date(long long sec, char *out, int n)
|
||||
{
|
||||
time_t t = (time_t)sec;
|
||||
struct tm *tm = localtime(&t);
|
||||
if (!tm) {
|
||||
out[0] = '\0';
|
||||
return;
|
||||
}
|
||||
snprintf(out, n, "%04d%02d%02d %02d:%02d:%02d", tm->tm_year + 1900,
|
||||
tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
}
|
||||
|
||||
/* "[yy]yymmdd[ hh:mm[:ss]]", local time: seconds, 0 when it isn't one */
|
||||
static long long parse_date(const char *v)
|
||||
{
|
||||
while (*v == ' ')
|
||||
v++;
|
||||
int digits = 0;
|
||||
while (isdigit((unsigned char)v[digits]))
|
||||
digits++;
|
||||
if (digits != 8 && digits != 6)
|
||||
return 0;
|
||||
struct tm tm;
|
||||
memset(&tm, 0, sizeof(tm));
|
||||
const char *d = v;
|
||||
if (digits == 8) {
|
||||
tm.tm_year = (d[0] - '0') * 1000 + (d[1] - '0') * 100 +
|
||||
(d[2] - '0') * 10 + (d[3] - '0') - 1900;
|
||||
d += 4;
|
||||
} else {
|
||||
int yy = (d[0] - '0') * 10 + (d[1] - '0');
|
||||
tm.tm_year = yy < 70 ? yy + 100 : yy;
|
||||
d += 2;
|
||||
}
|
||||
tm.tm_mon = (d[0] - '0') * 10 + (d[1] - '0') - 1;
|
||||
tm.tm_mday = (d[2] - '0') * 10 + (d[3] - '0');
|
||||
const char *p = v + digits;
|
||||
while (*p == ' ' || *p == '\t')
|
||||
p++;
|
||||
int h = 0, m = 0, s = 0;
|
||||
if (p != v + digits && sscanf(p, "%d:%d:%d", &h, &m, &s) < 2) {
|
||||
h = m = s = 0;
|
||||
}
|
||||
tm.tm_hour = h;
|
||||
tm.tm_min = m;
|
||||
tm.tm_sec = s;
|
||||
tm.tm_isdst = -1;
|
||||
time_t t = mktime(&tm);
|
||||
return t == (time_t)-1 ? 0 : (long long)t;
|
||||
}
|
||||
|
||||
/* a file sent: F, its attributes if agreed (A), D packets, Z (the file
|
||||
skipped if the receiver refuses or stops it) */
|
||||
static void send_file(struct kermit *k, const struct xfer_file *f)
|
||||
{
|
||||
dmc_xfer *x = k->x;
|
||||
/* (the encoded packet's and the file's: in x->heap, freed at the end) */
|
||||
unsigned char *enc = (unsigned char *)x->heap, *buf = enc + OUR_LONG + 64;
|
||||
int n;
|
||||
xfer_file_start(x, f->name, f->size);
|
||||
encode(k, (const unsigned char *)f->name, (int)strlen(f->name), enc,
|
||||
OUR_LONG, &n);
|
||||
exchange(k, 'F', enc, n);
|
||||
if (k->attributes) {
|
||||
unsigned char a[200];
|
||||
int an = 0;
|
||||
char v[64];
|
||||
attr(a, &an, '"', "B8"); /* binary, 8-bit */
|
||||
if (f->size >= 0) {
|
||||
snprintf(v, sizeof(v), "%lld", (f->size + 1023) / 1024);
|
||||
attr(a, &an, '!', v);
|
||||
snprintf(v, sizeof(v), "%lld", f->size);
|
||||
attr(a, &an, '1', v);
|
||||
}
|
||||
if (f->mtime > 0) {
|
||||
format_date(f->mtime, v, sizeof(v));
|
||||
if (v[0])
|
||||
attr(a, &an, '#', v);
|
||||
}
|
||||
exchange(k, 'A', a, an);
|
||||
if (k->ack_len && k->ack[0] == 'N') {
|
||||
/* refused: the file skipped */
|
||||
exchange(k, 'Z', (const unsigned char *)"D", 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
xfer_open(x, f);
|
||||
int have = 0, eof = 0;
|
||||
long long sent = 0;
|
||||
for (;;) {
|
||||
while (!eof && have < OUR_LONG) {
|
||||
int r = xfer_src_read(x, buf + have, OUR_LONG - have);
|
||||
if (r <= 0)
|
||||
eof = 1;
|
||||
else
|
||||
have += r;
|
||||
}
|
||||
if (!have)
|
||||
break;
|
||||
int used = encode(k, buf, have, enc, room(k), &n);
|
||||
exchange(k, 'D', enc, n);
|
||||
memmove(buf, buf + used, (size_t)(have - used));
|
||||
have -= used;
|
||||
sent += used;
|
||||
xfer_bytes(x, sent);
|
||||
if (k->ack_len && (k->ack[0] == 'X' || k->ack[0] == 'Z')) {
|
||||
/* the receiver wants no more of this file (or of any) */
|
||||
int all = k->ack[0] == 'Z';
|
||||
exchange(k, 'Z', (const unsigned char *)"D", 1);
|
||||
xfer_close(x);
|
||||
if (all)
|
||||
xfer_cancelled(x, "cancelled by the receiver");
|
||||
return;
|
||||
}
|
||||
}
|
||||
xfer_close(x);
|
||||
exchange(k, 'Z', NULL, 0);
|
||||
xfer_file_done(x);
|
||||
}
|
||||
|
||||
void kermit_send(dmc_xfer *x)
|
||||
{
|
||||
struct kermit *k = k_new(x);
|
||||
unsigned char p[32];
|
||||
x->heap = malloc(OUR_LONG + 64 + OUR_LONG);
|
||||
if (!x->heap)
|
||||
xfer_fail(x, "out of memory");
|
||||
k->seq = 0;
|
||||
k->force_check1 = 1;
|
||||
exchange(k, 'S', p, our_params(k, p));
|
||||
agree(k, k->ack, k->ack_len);
|
||||
k->force_check1 = 0;
|
||||
for (int i = 0; i < x->nfiles; i++)
|
||||
send_file(k, &x->files[i]);
|
||||
exchange(k, 'B', NULL, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* receiving */
|
||||
|
||||
static void ack(struct kermit *k, int seq, const unsigned char *data, int n)
|
||||
{
|
||||
k->last_sent_type = 'Y';
|
||||
send_packet(k, seq, 'Y', data, n);
|
||||
}
|
||||
|
||||
static void nak(struct kermit *k, int seq)
|
||||
{
|
||||
k->last_sent_type = 'N';
|
||||
send_packet(k, seq, 'N', NULL, 0);
|
||||
}
|
||||
|
||||
void kermit_receive(dmc_xfer *x)
|
||||
{
|
||||
struct kermit *k = k_new(x);
|
||||
int tries = 0;
|
||||
char text[600];
|
||||
k->seq = 0;
|
||||
k->force_check1 = 1;
|
||||
/* waiting for the S (a NAK now and then, for a sender waiting) */
|
||||
for (;;) {
|
||||
int got = read_packet(k, TIMEOUT);
|
||||
if (got && k->ptype == 'S')
|
||||
break;
|
||||
if (got && k->ptype == 'E') {
|
||||
decode(k, k->pkt, k->plen, text, (int)sizeof(text));
|
||||
xfer_fail(x, "the sender: %s", text);
|
||||
}
|
||||
if (++tries > MAX_RETRIES)
|
||||
xfer_fail(x, "the sender didn't start");
|
||||
nak(k, 0);
|
||||
}
|
||||
agree(k, k->pkt, k->plen);
|
||||
unsigned char ours[32];
|
||||
int on = our_params(k, ours);
|
||||
int init_seq = k->pseq;
|
||||
ack(k, init_seq, ours, on);
|
||||
/* our last ACK, for when it is lost */
|
||||
int last_seq = init_seq;
|
||||
memcpy(k->ack, ours, (size_t)on);
|
||||
k->ack_len = on;
|
||||
k->force_check1 = 0;
|
||||
k->seq = (init_seq + 1) & 63;
|
||||
long long mtime = 0, got_bytes = 0;
|
||||
tries = 0;
|
||||
unsigned char reply[XFER_NAME * 2 + 16];
|
||||
for (;;) {
|
||||
int got = read_packet(k, TIMEOUT);
|
||||
if (!got || k->pseq != k->seq) {
|
||||
if (got && k->pseq == last_seq) {
|
||||
/* our ACK lost: sent again */
|
||||
if (k->ptype == 'S')
|
||||
k->force_check1 = 1;
|
||||
ack(k, last_seq, k->ack, k->ack_len);
|
||||
k->force_check1 = 0;
|
||||
} else if (got && k->ptype == 'E') {
|
||||
decode(k, k->pkt, k->plen, text, (int)sizeof(text));
|
||||
xfer_fail(x, "the sender: %s", text);
|
||||
} else {
|
||||
nak(k, k->seq);
|
||||
}
|
||||
if (++tries > MAX_RETRIES)
|
||||
kfail(k, "too many retries");
|
||||
xfer_error(x);
|
||||
continue;
|
||||
}
|
||||
tries = 0;
|
||||
int rn = 0;
|
||||
switch (k->ptype) {
|
||||
case 'F': {
|
||||
decode(k, k->pkt, k->plen, text, XFER_NAME);
|
||||
/* (a name sent "converted", all capitals, as C-Kermit
|
||||
does: back in lower case) */
|
||||
int letters = 0, lower = 0;
|
||||
for (char *p = text; *p; p++) {
|
||||
if (isalpha((unsigned char)*p))
|
||||
letters = 1;
|
||||
if (islower((unsigned char)*p))
|
||||
lower = 1;
|
||||
}
|
||||
if (letters && !lower)
|
||||
for (char *p = text; *p; p++)
|
||||
*p = (char)tolower((unsigned char)*p);
|
||||
xfer_create(x, text);
|
||||
mtime = 0;
|
||||
got_bytes = 0;
|
||||
xfer_file_start(x, x->dst_name, -1);
|
||||
encode(k, (const unsigned char *)x->dst_name,
|
||||
(int)strlen(x->dst_name), reply, (int)sizeof(reply), &rn);
|
||||
break;
|
||||
}
|
||||
case 'A': {
|
||||
const unsigned char *a = k->pkt;
|
||||
int i = 0;
|
||||
while (i + 1 < k->plen) {
|
||||
int tag = a[i], n = unchar(a[i + 1]);
|
||||
if (n < 0 || i + 2 + n > k->plen)
|
||||
break;
|
||||
char v[128];
|
||||
snprintf(v, sizeof(v), "%.*s", n, (const char *)a + i + 2);
|
||||
if (tag == '1') {
|
||||
xfer_size(x, strtoll(v, NULL, 10));
|
||||
} else if (tag == '!') {
|
||||
if (xfer_size_now(x) < 0)
|
||||
xfer_size(x, strtoll(v, NULL, 10) * 1024);
|
||||
} else if (tag == '#') {
|
||||
mtime = parse_date(v);
|
||||
}
|
||||
i += 2 + n;
|
||||
}
|
||||
reply[rn++] = 'Y';
|
||||
break;
|
||||
}
|
||||
case 'D':
|
||||
if (!x->dst)
|
||||
kfail(k, "data before a file's name");
|
||||
got_bytes += decode(k, k->pkt, k->plen, NULL, 0);
|
||||
xfer_bytes(x, got_bytes);
|
||||
/* the user's skip: X in the ACK (the sender ends the
|
||||
file with Z D, and the file is dropped) */
|
||||
if (xfer_skip_wanted(x))
|
||||
reply[rn++] = 'X';
|
||||
break;
|
||||
case 'Z':
|
||||
if (x->dst) {
|
||||
if (k->plen && k->pkt[0] == 'D') {
|
||||
xfer_abandon(x);
|
||||
} else {
|
||||
xfer_done(x, mtime);
|
||||
xfer_file_done(x);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'B':
|
||||
ack(k, k->pseq, NULL, 0);
|
||||
return;
|
||||
case 'E':
|
||||
decode(k, k->pkt, k->plen, text, (int)sizeof(text));
|
||||
xfer_fail(x, "the sender: %s", text);
|
||||
case 'X':
|
||||
/* text to show (a server's): taken like a file with no
|
||||
name */
|
||||
break;
|
||||
default: {
|
||||
char why[64];
|
||||
snprintf(why, sizeof(why), "unexpected %c packet", k->ptype);
|
||||
kfail(k, why);
|
||||
}
|
||||
}
|
||||
int this_seq = k->pseq;
|
||||
ack(k, this_seq, reply, rn);
|
||||
last_seq = this_seq;
|
||||
memcpy(k->ack, reply, (size_t)rn);
|
||||
k->ack_len = rn;
|
||||
k->seq = (k->seq + 1) & 63;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,476 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* xfer_xy.c - XMODEM (128-byte blocks, a checksum or CRC; 1K blocks) and
|
||||
* YMODEM (1K blocks, CRC, a batch of files each named and
|
||||
* sized in block 0), both ways. A bad block is asked for
|
||||
* again (NAK) up to MAX_ERRORS times; CAN CAN from the other
|
||||
* side ends it.
|
||||
*
|
||||
* YMODEM-G: the receiver asks with G, and the sender sends
|
||||
* the blocks with no wait for an ACK (for a line that
|
||||
* corrects its errors, as a modem's V.42). An error ends
|
||||
* the transfer, as no block can be sent again. The EOT of
|
||||
* each file is still acknowledged. A sender asked with C
|
||||
* does plain YMODEM.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "xfer.h"
|
||||
|
||||
#define SOH 0x01
|
||||
#define STX 0x02
|
||||
#define EOT 0x04
|
||||
#define ACK 0x06
|
||||
#define NAK 0x15
|
||||
#define CAN 0x18
|
||||
#define SUB 0x1a
|
||||
#define C 'C'
|
||||
#define G 'G'
|
||||
#define MAX_ERRORS 10
|
||||
#define TIMEOUT 10000
|
||||
|
||||
/* a block as it came */
|
||||
struct block {
|
||||
int number; /* -1: none (a timeout, a bad one, EOT) */
|
||||
int size;
|
||||
unsigned char data[1024];
|
||||
};
|
||||
|
||||
/* the receiver's state */
|
||||
struct rx {
|
||||
int crc; /* CRC, else the checksum */
|
||||
int eot; /* the sender's EOT came */
|
||||
int bad; /* a damaged block came */
|
||||
int g; /* YMODEM-G: no ACKs, no repeats */
|
||||
};
|
||||
|
||||
/* YMODEM-G's end on an error: no block can come again */
|
||||
static void g_fail(dmc_xfer *x)
|
||||
{
|
||||
xfer_write(x, "\x18\x18\x18\x18\x18", 5);
|
||||
xfer_fail(x, "an error on the line (YMODEM-G cannot send a block again)");
|
||||
}
|
||||
|
||||
static int sum8(const unsigned char *p, int n)
|
||||
{
|
||||
int s = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
s += p[i];
|
||||
return s & 0xff;
|
||||
}
|
||||
|
||||
/* a block within wait ms, its check checked; number -1 for a timeout, a
|
||||
bad block (the rest of it let go by) or EOT (r->eot) */
|
||||
static void read_block(dmc_xfer *x, struct rx *r, struct block *b, int use_crc,
|
||||
int wait)
|
||||
{
|
||||
r->eot = r->bad = 0;
|
||||
b->number = -1;
|
||||
/* (what isn't a block's start let go by: messages, line noise) */
|
||||
long long deadline = xfer_now() + wait;
|
||||
int size = 0;
|
||||
while (!size) {
|
||||
long long left = deadline - xfer_now();
|
||||
if (left <= 0)
|
||||
return;
|
||||
int c = xfer_read(x, (int)left);
|
||||
switch (c) {
|
||||
case -1: return;
|
||||
case SOH: size = 128; break;
|
||||
case STX: size = 1024; break;
|
||||
case EOT: r->eot = 1; return;
|
||||
case CAN:
|
||||
if (xfer_read(x, 1000) == CAN)
|
||||
xfer_cancelled(x, "cancelled by the sender");
|
||||
break;
|
||||
}
|
||||
}
|
||||
int n = xfer_read(x, 1000), nn = xfer_read(x, 1000);
|
||||
if (n < 0 || nn < 0)
|
||||
return;
|
||||
for (int i = 0; i < size; i++) {
|
||||
int c = xfer_read(x, 1000);
|
||||
if (c < 0)
|
||||
return;
|
||||
b->data[i] = (unsigned char)c;
|
||||
}
|
||||
int ok;
|
||||
if (use_crc) {
|
||||
int hi = xfer_read(x, 1000), lo = xfer_read(x, 1000);
|
||||
ok = hi >= 0 && lo >= 0 &&
|
||||
xfer_crc16(0, b->data, size) == (unsigned)((hi << 8) | lo);
|
||||
} else {
|
||||
int s = xfer_read(x, 1000);
|
||||
ok = s >= 0 && sum8(b->data, size) == s;
|
||||
}
|
||||
if (!ok || (n ^ nn) != 0xff) {
|
||||
while (xfer_read(x, 200) >= 0)
|
||||
;
|
||||
r->bad = 1;
|
||||
return;
|
||||
}
|
||||
b->number = n;
|
||||
b->size = size;
|
||||
}
|
||||
|
||||
/* the first block, asked for with C (CRC) and, for XMODEM, NAK (a
|
||||
checksum) when C isn't answered */
|
||||
static void start_and_read(dmc_xfer *x, struct rx *r, struct block *b, int ymodem,
|
||||
int tries)
|
||||
{
|
||||
int crc = 1;
|
||||
r->bad = 0;
|
||||
for (int attempt = 0; attempt < tries; attempt++) {
|
||||
if (attempt == 4 && !ymodem)
|
||||
crc = 0;
|
||||
/* (a damaged block 0 asked for again with NAK: a C could be
|
||||
taken for its ACK and a request for the data) */
|
||||
xfer_put(x, r->g ? G : crc && !(ymodem && r->bad) ? C : NAK);
|
||||
read_block(x, r, b, crc, 3000);
|
||||
if (b->number >= 0) {
|
||||
r->crc = crc;
|
||||
return;
|
||||
}
|
||||
}
|
||||
b->number = -1;
|
||||
}
|
||||
|
||||
/* a file's blocks into the received file, up to size bytes if known;
|
||||
held (XMODEM): the last block kept back, for its padding to come off */
|
||||
static void receive_file(dmc_xfer *x, struct rx *r, long long size, int ymodem,
|
||||
unsigned char *held, int *held_n)
|
||||
{
|
||||
int expect = 1, errors = 0, eots = 0, have = 0;
|
||||
long long written = 0;
|
||||
struct block bb, *b = &bb;
|
||||
if (held)
|
||||
*held_n = -1;
|
||||
if (!ymodem) {
|
||||
start_and_read(x, r, b, 0, 10);
|
||||
have = b->number >= 0;
|
||||
}
|
||||
for (;;) {
|
||||
if (!have) {
|
||||
read_block(x, r, b, r->crc, TIMEOUT);
|
||||
if (b->number < 0) {
|
||||
if (r->eot) {
|
||||
/* YMODEM: the first EOT answered NAK, the
|
||||
second ACK (to be sure it was one);
|
||||
YMODEM-G: ACK */
|
||||
eots++;
|
||||
if (ymodem && eots == 1 && !r->g) {
|
||||
xfer_put(x, NAK);
|
||||
continue;
|
||||
}
|
||||
xfer_put(x, ACK);
|
||||
break;
|
||||
}
|
||||
if (r->g)
|
||||
g_fail(x);
|
||||
if (++errors > MAX_ERRORS)
|
||||
xfer_fail(x, "too many errors on the line");
|
||||
xfer_error(x);
|
||||
xfer_put(x, NAK);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
have = 0;
|
||||
if (b->number == (expect & 0xff)) {
|
||||
int len = b->size;
|
||||
if (size >= 0 && written + len > size)
|
||||
len = size - written > 0 ? (int)(size - written) : 0;
|
||||
if (held) {
|
||||
if (*held_n >= 0)
|
||||
xfer_dst_write(x, held, *held_n);
|
||||
memcpy(held, b->data, (size_t)len);
|
||||
*held_n = len;
|
||||
} else {
|
||||
xfer_dst_write(x, b->data, len);
|
||||
}
|
||||
written += len;
|
||||
expect++;
|
||||
errors = 0;
|
||||
if (!r->g)
|
||||
xfer_put(x, ACK);
|
||||
xfer_bytes(x, written);
|
||||
} else if (b->number == ((expect - 1) & 0xff)) {
|
||||
/* a repeat of one already had (our ACK lost) */
|
||||
xfer_put(x, ACK);
|
||||
} else {
|
||||
xfer_write(x, "\x18\x18\x18", 3);
|
||||
xfer_fail(x, "blocks out of order");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void xy_receive_x(dmc_xfer *x)
|
||||
{
|
||||
struct rx r = { 1, 0, 0, 0 };
|
||||
unsigned char held[1024];
|
||||
int held_n = -1;
|
||||
xfer_create(x, x->xname);
|
||||
xfer_file_start(x, x->dst_name, -1);
|
||||
receive_file(x, &r, -1, 0, held, &held_n);
|
||||
/* the last block, its padding off */
|
||||
if (held_n > 0) {
|
||||
while (held_n > 0 && held[held_n - 1] == SUB)
|
||||
held_n--;
|
||||
xfer_dst_write(x, held, held_n);
|
||||
}
|
||||
xfer_done(x, 0);
|
||||
xfer_file_done(x);
|
||||
}
|
||||
|
||||
void xy_receive_y(dmc_xfer *x, int g)
|
||||
{
|
||||
struct rx r = { 1, 0, 0, g };
|
||||
struct block bb0, *b0 = &bb0;
|
||||
int got = 0;
|
||||
for (;;) {
|
||||
/* block 0: the file's name and size, or an empty one (the end) */
|
||||
start_and_read(x, &r, b0, 1, g && got ? 2 : 10);
|
||||
/* (lrzsz's sb in YMODEM-G ends with no empty block 0) */
|
||||
if (b0->number < 0 && g && got)
|
||||
return;
|
||||
if (b0->number < 0)
|
||||
xfer_fail(x, "the sender didn't start");
|
||||
if (b0->number != 0)
|
||||
xfer_fail(x, "no YMODEM header block");
|
||||
if (b0->data[0] == 0) {
|
||||
xfer_put(x, ACK);
|
||||
return;
|
||||
}
|
||||
char name[XFER_NAME], meta[1100];
|
||||
int nul = 0;
|
||||
while (nul < b0->size && b0->data[nul])
|
||||
nul++;
|
||||
snprintf(name, sizeof(name), "%.*s", nul, (const char *)b0->data);
|
||||
int m = 0;
|
||||
for (int i = nul + 1; i < b0->size && b0->data[i] && m < (int)sizeof(meta) - 1; i++)
|
||||
meta[m++] = (char)b0->data[i];
|
||||
meta[m] = '\0';
|
||||
long long size = -1, mtime = 0;
|
||||
char *end;
|
||||
long long v = strtoll(meta, &end, 10);
|
||||
if (end != meta)
|
||||
size = v;
|
||||
if (*end == ' ')
|
||||
mtime = strtoll(end + 1, NULL, 8);
|
||||
/* (YMODEM-G: block 0 not acknowledged, G asks for the data) */
|
||||
if (!g)
|
||||
xfer_put(x, ACK);
|
||||
xfer_create(x, name);
|
||||
xfer_file_start(x, x->dst_name, size);
|
||||
xfer_put(x, g ? G : C);
|
||||
receive_file(x, &r, size, 1, NULL, NULL);
|
||||
xfer_done(x, mtime);
|
||||
xfer_file_done(x);
|
||||
got++;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* sending */
|
||||
|
||||
/* C (CRC: 1) or NAK (a checksum: 0) from the receiver, G (2) when g;
|
||||
-1 for none */
|
||||
static int wait_for_start(dmc_xfer *x, int g)
|
||||
{
|
||||
long long end = xfer_now() + 60000;
|
||||
while (xfer_now() < end) {
|
||||
switch (xfer_read(x, TIMEOUT)) {
|
||||
case G:
|
||||
if (g)
|
||||
return 2;
|
||||
break;
|
||||
case C: return 1;
|
||||
case NAK: return 0;
|
||||
case CAN:
|
||||
if (xfer_read(x, 1000) == CAN)
|
||||
xfer_cancelled(x, "cancelled by the receiver");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* the receiver's answer: ACK, or NAK (also for a timeout); C an ACK for
|
||||
block 0 when allow_c (receivers that ask for the data at once) */
|
||||
static int answer(dmc_xfer *x, int allow_c, int wait)
|
||||
{
|
||||
long long deadline = xfer_now() + wait;
|
||||
for (;;) {
|
||||
long long left = deadline - xfer_now();
|
||||
if (left <= 0)
|
||||
return NAK;
|
||||
switch (xfer_read(x, (int)left)) {
|
||||
case -1: return NAK;
|
||||
case ACK: return ACK;
|
||||
case NAK: return NAK;
|
||||
case C:
|
||||
if (allow_c)
|
||||
return ACK;
|
||||
break;
|
||||
case CAN:
|
||||
if (xfer_read(x, 1000) == CAN)
|
||||
xfer_cancelled(x, "cancelled by the receiver");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* block n's bytes into out: SOH or STX, its number and complement, data,
|
||||
its CRC-16 or checksum: their length */
|
||||
static int block_bytes(unsigned char *out, int n, const unsigned char *data,
|
||||
int size, int use_crc)
|
||||
{
|
||||
int k = 0;
|
||||
out[k++] = size == 1024 ? STX : SOH;
|
||||
out[k++] = (unsigned char)(n & 0xff);
|
||||
out[k++] = (unsigned char)(0xff - (n & 0xff));
|
||||
memcpy(out + k, data, (size_t)size);
|
||||
k += size;
|
||||
if (use_crc) {
|
||||
unsigned c = xfer_crc16(0, data, size);
|
||||
out[k++] = (unsigned char)(c >> 8);
|
||||
out[k++] = (unsigned char)(c & 0xff);
|
||||
} else {
|
||||
out[k++] = (unsigned char)sum8(data, size);
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
/* a block sent until it's acknowledged */
|
||||
static void send_block(dmc_xfer *x, int n, const unsigned char *data, int size,
|
||||
int use_crc)
|
||||
{
|
||||
unsigned char bytes[1024 + 5];
|
||||
int len = block_bytes(bytes, n, data, size, use_crc);
|
||||
int errors = 0;
|
||||
for (;;) {
|
||||
xfer_write(x, bytes, len);
|
||||
if (answer(x, n == 0, TIMEOUT) == ACK)
|
||||
return;
|
||||
if (++errors > MAX_ERRORS)
|
||||
xfer_fail(x, "too many errors on the line");
|
||||
xfer_error(x);
|
||||
}
|
||||
}
|
||||
|
||||
/* YMODEM-G: the receiver's CAN CAN, if it sent them (no wait) */
|
||||
static void g_check(dmc_xfer *x)
|
||||
{
|
||||
while (xfer_available(x)) {
|
||||
int c = xfer_read(x, 0);
|
||||
if (c == CAN && xfer_read(x, 1000) == CAN)
|
||||
xfer_cancelled(x, "cancelled by the receiver");
|
||||
}
|
||||
}
|
||||
|
||||
/* the open file's data in blocks of block_size (a short last one in 128,
|
||||
padded with SUB), then EOT until acknowledged; g: the blocks with no
|
||||
wait for an ACK (YMODEM-G) */
|
||||
static void send_data(dmc_xfer *x, int block_size, int use_crc, int g)
|
||||
{
|
||||
unsigned char buf[1024];
|
||||
int n = 1;
|
||||
long long sent = 0;
|
||||
for (;;) {
|
||||
int len = 0;
|
||||
while (len < block_size) {
|
||||
int r = xfer_src_read(x, buf + len, block_size - len);
|
||||
if (r <= 0)
|
||||
break;
|
||||
len += r;
|
||||
}
|
||||
if (!len)
|
||||
break;
|
||||
int size = len <= 128 ? 128 : block_size;
|
||||
memset(buf + len, SUB, (size_t)(size - len));
|
||||
if (g) {
|
||||
unsigned char bytes[1024 + 5];
|
||||
xfer_write(x, bytes, block_bytes(bytes, n, buf, size, 1));
|
||||
g_check(x);
|
||||
} else {
|
||||
send_block(x, n, buf, size, use_crc);
|
||||
}
|
||||
sent += len;
|
||||
n++;
|
||||
xfer_bytes(x, sent);
|
||||
if (len < block_size)
|
||||
break;
|
||||
}
|
||||
/* the end: EOT until it's answered ACK (YMODEM answers the first NAK) */
|
||||
for (int tries = 0;; tries++) {
|
||||
xfer_put(x, EOT);
|
||||
if (answer(x, 0, TIMEOUT) == ACK)
|
||||
return;
|
||||
if (tries >= MAX_ERRORS)
|
||||
xfer_fail(x, "the end not acknowledged");
|
||||
}
|
||||
}
|
||||
|
||||
void xy_send_x(dmc_xfer *x, int one_k)
|
||||
{
|
||||
const struct xfer_file *f = &x->files[0];
|
||||
int use_crc = wait_for_start(x, 0);
|
||||
if (use_crc < 0)
|
||||
xfer_fail(x, "the receiver didn't start");
|
||||
xfer_file_start(x, f->name, f->size);
|
||||
xfer_open(x, f);
|
||||
send_data(x, one_k ? 1024 : 128, use_crc, 0);
|
||||
xfer_close(x);
|
||||
xfer_file_done(x);
|
||||
}
|
||||
|
||||
void xy_send_y(dmc_xfer *x, int g)
|
||||
{
|
||||
unsigned char block0[1024];
|
||||
for (int i = 0; i < x->nfiles; i++) {
|
||||
const struct xfer_file *f = &x->files[i];
|
||||
/* (asked with C: plain YMODEM, also for YMODEM-G) */
|
||||
int start = wait_for_start(x, g);
|
||||
if (start < 1)
|
||||
xfer_fail(x, "the receiver didn't start");
|
||||
int streaming = start == 2;
|
||||
memset(block0, 0, sizeof(block0));
|
||||
int nl = (int)strlen(f->name);
|
||||
if (nl > 900)
|
||||
nl = 900;
|
||||
memcpy(block0, f->name, (size_t)nl);
|
||||
char meta[64];
|
||||
int ml = snprintf(meta, sizeof(meta), "%lld %llo 0",
|
||||
f->size >= 0 ? f->size : 0LL,
|
||||
(unsigned long long)(f->mtime > 0 ? f->mtime : 0));
|
||||
memcpy(block0 + nl + 1, meta, (size_t)ml);
|
||||
int size0 = nl + 1 + ml > 128 ? 1024 : 128;
|
||||
if (streaming) {
|
||||
unsigned char bytes[1024 + 5];
|
||||
xfer_write(x, bytes, block_bytes(bytes, 0, block0, size0, 1));
|
||||
} else {
|
||||
send_block(x, 0, block0, size0, 1);
|
||||
}
|
||||
xfer_file_start(x, f->name, f->size);
|
||||
if (wait_for_start(x, g) != (streaming ? 2 : 1))
|
||||
xfer_fail(x, "the receiver didn't ask for the data");
|
||||
xfer_open(x, f);
|
||||
send_data(x, 1024, 1, streaming);
|
||||
xfer_close(x);
|
||||
xfer_file_done(x);
|
||||
}
|
||||
/* the batch over: an empty block 0 */
|
||||
if (wait_for_start(x, g) < 1)
|
||||
xfer_fail(x, "the receiver didn't ask for the end");
|
||||
/* (rb exits as it takes it: its ACK can be lost with it) */
|
||||
unsigned char empty[128], bytes[133];
|
||||
memset(empty, 0, sizeof(empty));
|
||||
int len = block_bytes(bytes, 0, empty, 128, 1);
|
||||
for (int attempt = 0; attempt < 3; attempt++) {
|
||||
xfer_write(x, bytes, len);
|
||||
if (answer(x, 1, 2000) == ACK)
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* zstd_codec.h - Zstandard (RFC 8878), whole frames, in plain C: the
|
||||
* encoder makes one frame of a chunk (single segment, its
|
||||
* content size and checksum in it, no dictionary), as
|
||||
* ZMODEM with zstd sends a file (docs/zmodem-zstd.md of
|
||||
* D-Modem); the decoder takes any frame up to its limit.
|
||||
* A port of D-Modem's link/zstd_codec.cpp (and of the
|
||||
* app's Zstd.kt): a hash chain over the chunk with a lazy
|
||||
* match, the repeat offsets, literals raw, RLE or Huffman
|
||||
* coded, each sequence table the predefined or a new one.
|
||||
* Named so that it does not hide the system's <zstd.h>.
|
||||
*/
|
||||
#ifndef DMC_ZSTD_CODEC_H
|
||||
#define DMC_ZSTD_CODEC_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define ZC_MAGIC 0xFD2FB528u
|
||||
|
||||
/* data[0..n) as one frame (malloc'd, its length in *out_n); NULL when
|
||||
memory ran out */
|
||||
unsigned char *zc_encode(const unsigned char *data, int n, int *out_n);
|
||||
|
||||
/* the content of the frame frame[0..n) (all of it, no more), malloc'd,
|
||||
its length in *out_n (a frame of no content gives a block of 1 byte
|
||||
that is not part of it); NULL if it is not a frame, has a dictionary,
|
||||
a window or content over max, bad data or a bad checksum (why, if
|
||||
not NULL, says which) */
|
||||
unsigned char *zc_decode(const unsigned char *frame, int n, int max, int *out_n,
|
||||
const char **why);
|
||||
|
||||
/* the length of the frame at the start of b[0..n) from its headers (RFC
|
||||
8878 3.1.1.1 and 3.1.1.2): 0 while not all of it is there, -1 if it is
|
||||
not a frame that ZMODEM with zstd takes (a frame of no more than max
|
||||
bytes of content, without a dictionary, with its content size and
|
||||
checksum) */
|
||||
int zc_frame_len(const unsigned char *b, int n, int max);
|
||||
|
||||
/* XXH64 (seed 0), Zstandard's content checksum */
|
||||
uint64_t zc_xxh64(const unsigned char *b, size_t n);
|
||||
|
||||
#endif
|
||||
@@ -2,3 +2,13 @@ add_executable(dmodemctl_test dmodemctl_test.c)
|
||||
target_link_libraries(dmodemctl_test PRIVATE dmodemctl)
|
||||
target_include_directories(dmodemctl_test PRIVATE ../src)
|
||||
add_test(NAME dmodemctl COMMAND dmodemctl_test ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
# the file transfers: both ways in memory, and against lrzsz and C-Kermit
|
||||
# when they are installed (a pseudo-terminal: POSIX only)
|
||||
if(UNIX)
|
||||
add_executable(xfer_test xfer_test.c)
|
||||
target_link_libraries(xfer_test PRIVATE dmodemctl)
|
||||
target_include_directories(xfer_test PRIVATE ../src)
|
||||
add_test(NAME xfer COMMAND xfer_test)
|
||||
set_tests_properties(xfer PROPERTIES TIMEOUT 600)
|
||||
endif()
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* dmodemctl_test.c - libdmodemctl's tests: a session against a scripted
|
||||
* modem, AT!M and AT#UD as dmodemd prints them, and the terminal screen
|
||||
@@ -140,6 +140,32 @@ static void test_command(void)
|
||||
dmc_session_free(s);
|
||||
}
|
||||
|
||||
/* a response in pages (ATI11 on an Agere modem, a MultiTech MT9234):
|
||||
a space for each "Press any key", the prompt kept out of the response */
|
||||
static void test_pages(void)
|
||||
{
|
||||
dmc_session *s = dmc_session_new();
|
||||
dmc_event ev;
|
||||
dmc_open(s, 0);
|
||||
int tag = dmc_command(s, "ATI11", DMC_QUIET, 0);
|
||||
CHECK_STR(out(s), "ATI11\r");
|
||||
feed(s, "ATI11\r\r\nLast Connection V.92\r\n"
|
||||
"Press any key to continue; ESC to quit");
|
||||
CHECK_STR(out(s), " ");
|
||||
CHECK(!next_of(s, DMC_EV_RESPONSE, &ev));
|
||||
feed(s, "\r\nNear Echo Level NA\r\n\r\nOK\r\n");
|
||||
CHECK_STR(out(s), "");
|
||||
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
|
||||
CHECK(ev.tag == tag && ev.ok);
|
||||
CHECK_STR(ev.text, "Last Connection V.92\n\nNear Echo Level NA");
|
||||
/* text typed in the terminal is the user's own: no key sent */
|
||||
dmc_type(s, "ATI11\r", 6);
|
||||
out(s);
|
||||
feed(s, "ATI11\r\r\nPress any key to continue");
|
||||
CHECK_STR(out(s), "");
|
||||
dmc_session_free(s);
|
||||
}
|
||||
|
||||
/* a call: dial, connect, online data, hang up through the escape */
|
||||
static void test_call(void)
|
||||
{
|
||||
@@ -447,14 +473,234 @@ static void test_vt(void)
|
||||
CHECK(n == 3 && !memcmp(r, "\x1bOQ", 3));
|
||||
n = dmc_vt_key(t, DMC_KEY_UP, r, sizeof(r));
|
||||
CHECK(n == 3 && !memcmp(r, "\x1b[A", 3));
|
||||
/* resized */
|
||||
/* resized: the scrollback's lines come back over the screen */
|
||||
n = dmc_vt_history(t);
|
||||
dmc_vt_resize(t, 80, 24);
|
||||
CHECK(dmc_vt_cols(t) == 80 && dmc_vt_rows(t) == 24);
|
||||
CHECK_STR(dmc_vt_text(t, 3), "01234567890123456789");
|
||||
CHECK(dmc_vt_history(t) == 0);
|
||||
CHECK_STR(dmc_vt_text(t, 3 + n), "01234567890123456789");
|
||||
CHECK(dmc_vt_palette(4) == 0x0000AA);
|
||||
dmc_vt_free(t);
|
||||
}
|
||||
|
||||
/* feed a string */
|
||||
static void vfeed(dmc_vt *t, const char *s)
|
||||
{
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
}
|
||||
|
||||
static const char *vreply(dmc_vt *t)
|
||||
{
|
||||
static char r[256];
|
||||
int n = dmc_vt_reply(t, r, sizeof(r) - 1);
|
||||
r[n] = '\0';
|
||||
return r;
|
||||
}
|
||||
|
||||
/* a row's text with the screen's blanks */
|
||||
static int has_image(const dmc_vt *t)
|
||||
{
|
||||
int y, x;
|
||||
dmc_cell c;
|
||||
for (y = 0; y < dmc_vt_rows(t); y++)
|
||||
for (x = 0; x < dmc_vt_cols(t); x++)
|
||||
if (dmc_vt_cell(t, y, x, &c) && (c.flags & DMC_CELL_IMAGE))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* every profile given every octet and noise, every cell read: nothing
|
||||
breaks (run under ASan and UBSan in CI) */
|
||||
static void test_vt_noise(void)
|
||||
{
|
||||
int i, k, y, x, n;
|
||||
unsigned seed = 12345;
|
||||
char b[4096], r[64];
|
||||
dmc_cell c;
|
||||
for (i = 0; i < dmc_vt_profile_count(); i++) {
|
||||
dmc_vt *t = dmc_vt_new(80, 25, 50);
|
||||
const char *id = dmc_vt_profile_id(i);
|
||||
CHECK(dmc_vt_set_profile(t, id));
|
||||
CHECK_STR(dmc_vt_get_profile(t), id);
|
||||
dmc_vt_set_cell_pixels(t, 8, 16);
|
||||
for (k = 0; k < 256; k++)
|
||||
b[k] = (char)k;
|
||||
dmc_vt_feed(t, b, 256);
|
||||
for (k = 0; k < 40; k++) {
|
||||
for (n = 0; n < (int)sizeof(b); n++) {
|
||||
seed = seed * 1103515245u + 12345u;
|
||||
b[n] = (char)(seed >> 16);
|
||||
/* plenty of ESC, CSI's and digits */
|
||||
if ((seed >> 8) % 7 == 0)
|
||||
b[n] = "\x1b[;0123456789!|\r\n"[(seed >> 4) % 18];
|
||||
}
|
||||
dmc_vt_feed(t, b, n);
|
||||
if (k == 20)
|
||||
dmc_vt_resize(t, 40 + k, 20);
|
||||
}
|
||||
for (y = -dmc_vt_history(t); y < dmc_vt_rows(t); y++)
|
||||
for (x = 0; x < dmc_vt_cols(t); x++) {
|
||||
dmc_vt_cell(t, y, x, &c);
|
||||
dmc_vt_cell_text(t, y, x);
|
||||
}
|
||||
for (k = DMC_KEY_UP; k <= DMC_KEY_KP_ENTER; k++)
|
||||
dmc_vt_key_mods(t, k, k % 8, r, sizeof(r));
|
||||
for (k = 0; k < 0x180; k++)
|
||||
dmc_vt_char_bytes(t, (uint32_t)k, k % 8, r, sizeof(r));
|
||||
dmc_vt_tap(t, 0.5, 0.5, r, sizeof(r));
|
||||
while (dmc_vt_reply(t, r, sizeof(r)) > 0)
|
||||
;
|
||||
dmc_vt_reset(t);
|
||||
dmc_vt_free(t);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_vt_profiles(void)
|
||||
{
|
||||
dmc_vt *t = dmc_vt_new(80, 25, 100);
|
||||
dmc_cell c;
|
||||
char r[64];
|
||||
int n, w, h;
|
||||
|
||||
CHECK(dmc_vt_profile_count() == 29);
|
||||
CHECK_STR(dmc_vt_profile_id(0), "xterm");
|
||||
CHECK_STR(dmc_vt_profile_term("rip"), "rip");
|
||||
CHECK_STR(dmc_vt_profile_telnet("rip"), "ANSI");
|
||||
CHECK_STR(dmc_vt_profile_telnet("vt220"), "DEC-VT220");
|
||||
CHECK_STR(dmc_vt_profile_term("nonsense"), "xterm-256color");
|
||||
CHECK(!dmc_vt_set_profile(t, "nonsense"));
|
||||
|
||||
/* xterm and the VTs' answers */
|
||||
CHECK_STR(dmc_vt_get_profile(t), "xterm");
|
||||
vfeed(t, "\x1b[c");
|
||||
CHECK_STR(vreply(t), "\x1b[?62;4;22c");
|
||||
dmc_vt_set_profile(t, "vt100");
|
||||
vfeed(t, "\x1b[c");
|
||||
CHECK_STR(vreply(t), "\x1b[?1;2c");
|
||||
dmc_vt_set_profile(t, "vt420");
|
||||
vfeed(t, "\x1b[>c");
|
||||
CHECK_STR(vreply(t), "\x1b[>41;10;0c");
|
||||
/* a VT420's rectangles: DECFRA */
|
||||
vfeed(t, "\x1b[2J\x1b[42;2;2;3;4$x");
|
||||
CHECK_STR(dmc_vt_text(t, 1), " ***");
|
||||
CHECK_STR(dmc_vt_text(t, 2), " ***");
|
||||
|
||||
/* VT52 */
|
||||
dmc_vt_set_profile(t, "vt52");
|
||||
vfeed(t, "\x1bH\x1bJ\x1bY\"%X\x1bZ");
|
||||
CHECK(dmc_vt_char(t, 2, 5) == 'X');
|
||||
CHECK_STR(vreply(t), "\x1b/K");
|
||||
|
||||
/* ANSI-BBS: code page 437, the VGA's font, ESC [2J homing */
|
||||
dmc_vt_set_profile(t, "ansi-bbs");
|
||||
CHECK(dmc_vt_glyph_cell(t, &w, &h) && w == 8 && h == 16);
|
||||
vfeed(t, "\x1b[5;5H\x1b[2J\xdb\xb0");
|
||||
CHECK(dmc_vt_char(t, 0, 0) == 0x2588 && dmc_vt_char(t, 0, 1) == 0x2591);
|
||||
CHECK(dmc_vt_glyph(t, 0x2588, &w, &h) && h == 16);
|
||||
CHECK(dmc_vt_glyph_row(t, 0x2588, 3) == 0xff);
|
||||
/* SyncTERM's Topaz (42) at the VGA's height, then back */
|
||||
n = (int)dmc_vt_glyph_row(t, 'A', 4);
|
||||
vfeed(t, "\x1b[0;42 D");
|
||||
CHECK(dmc_vt_glyph(t, 'A', &w, &h) && h == 16);
|
||||
CHECK((int)dmc_vt_glyph_row(t, 'A', 4) != n);
|
||||
vfeed(t, "\x1b[0;0 D");
|
||||
CHECK((int)dmc_vt_glyph_row(t, 'A', 4) == n);
|
||||
/* the line speed */
|
||||
vfeed(t, "\x1b[0;6*r");
|
||||
CHECK(dmc_vt_emulated_bps(t) > 0);
|
||||
|
||||
/* Amiga: Topaz, 8x8, its colours */
|
||||
dmc_vt_set_profile(t, "amiga");
|
||||
CHECK(dmc_vt_glyph_cell(t, &w, &h) && w == 8 && h == 8);
|
||||
vfeed(t, "\x1b[H\x1b[2J\x1b[31mR");
|
||||
CHECK(dmc_vt_cell(t, 0, 0, &c) && c.fg == 0xcc2222);
|
||||
|
||||
/* Avatar: ^V ^A attribute */
|
||||
dmc_vt_set_profile(t, "avatar");
|
||||
vfeed(t, "\x0c\x16\x01\x1fX");
|
||||
CHECK(dmc_vt_char(t, 0, 0) == 'X');
|
||||
CHECK(dmc_vt_cell(t, 0, 0, &c) && c.fg == 0xffffff && c.bg == dmc_vt_palette(4));
|
||||
|
||||
/* RIPscrip */
|
||||
dmc_vt_set_profile(t, "rip");
|
||||
dmc_vt_set_cell_pixels(t, 8, 8);
|
||||
CHECK(dmc_vt_requested_rows(t) == 43);
|
||||
vfeed(t, "\x1b[!");
|
||||
CHECK_STR(vreply(t), "RIPSCRIP015400");
|
||||
vfeed(t, "\x1b[2JHi!x\r\n!x\r\n");
|
||||
CHECK_STR(dmc_vt_text(t, 0), "Hi!x");
|
||||
CHECK_STR(dmc_vt_text(t, 1), "!x");
|
||||
CHECK(!has_image(t));
|
||||
vfeed(t, "!|c0F|L00000A0A\r\n");
|
||||
CHECK(has_image(t));
|
||||
CHECK_STR(dmc_vt_text(t, 2), "");
|
||||
vfeed(t, "!|1M0000000Z0Z00000000HI^M\r\n");
|
||||
n = dmc_vt_tap(t, 0.01, 0.01, r, sizeof(r));
|
||||
CHECK(n == 3 && !memcmp(r, "HI\r", 3));
|
||||
CHECK(dmc_vt_tap(t, 0.99, 0.99, r, sizeof(r)) == 0);
|
||||
|
||||
/* a Commodore 64 */
|
||||
dmc_vt_set_profile(t, "c64");
|
||||
CHECK(dmc_vt_requested_cols(t) == 40 && dmc_vt_requested_rows(t) == 25);
|
||||
CHECK(dmc_vt_glyph_cell(t, &w, &h) && w == 8 && h == 8);
|
||||
vfeed(t, "\x93" "A\x1c" "B\x0e" "C");
|
||||
CHECK(dmc_vt_char(t, 0, 0) == 'A');
|
||||
CHECK(dmc_vt_cell(t, 0, 1, &c) && c.fg == 0x68372b);
|
||||
CHECK(dmc_vt_char(t, 0, 2) == 'c');
|
||||
n = dmc_vt_char_bytes(t, 'a', 0, r, sizeof(r));
|
||||
CHECK(n == 1 && r[0] == 0x41);
|
||||
n = dmc_vt_key(t, DMC_KEY_UP, r, sizeof(r));
|
||||
CHECK(n == 1 && (unsigned char)r[0] == 0x91);
|
||||
|
||||
/* an Atari 8-bit */
|
||||
dmc_vt_set_profile(t, "atari");
|
||||
vfeed(t, "\x7d" "AB\x9b" "C");
|
||||
CHECK_STR(dmc_vt_text(t, 0), "AB");
|
||||
CHECK_STR(dmc_vt_text(t, 1), "C");
|
||||
|
||||
/* an Atari ST: its pens */
|
||||
dmc_vt_set_profile(t, "atari-st-low");
|
||||
CHECK(dmc_vt_requested_cols(t) == 40);
|
||||
vfeed(t, "\x1b" "E\x1b" "b\x01X");
|
||||
CHECK(dmc_vt_cell(t, 0, 0, &c) && c.ch == 'X' && c.fg == 0xff0000);
|
||||
vfeed(t, "\x1bZ");
|
||||
CHECK_STR(vreply(t), "\x1b/K");
|
||||
|
||||
/* Viewdata */
|
||||
dmc_vt_set_profile(t, "viewdata");
|
||||
CHECK(dmc_vt_requested_cols(t) == 40 && dmc_vt_requested_rows(t) == 24);
|
||||
vfeed(t, "\x0c" "A#");
|
||||
CHECK(dmc_vt_char(t, 0, 0) == 'A' && dmc_vt_char(t, 0, 1) == 0xa3);
|
||||
|
||||
/* the Minitel: its first row is the status row */
|
||||
dmc_vt_set_profile(t, "minitel");
|
||||
vfeed(t, "\x0c" "M");
|
||||
CHECK(dmc_vt_char(t, 1, 0) == 'M');
|
||||
|
||||
/* sixel on xterm */
|
||||
dmc_vt_set_profile(t, "xterm");
|
||||
dmc_vt_set_cell_pixels(t, 8, 16);
|
||||
vfeed(t, "\x1bPq#0;2;100;0;0#0~~~~~~~~\x1b\\");
|
||||
CHECK(has_image(t));
|
||||
|
||||
/* ReGIS on a VT340 */
|
||||
dmc_vt_set_profile(t, "vt340");
|
||||
vfeed(t, "\x1b[2J");
|
||||
CHECK(!has_image(t));
|
||||
vfeed(t, "\x1bP0pS(E)P[100,100]V[200,200]\x1b\\");
|
||||
CHECK(has_image(t));
|
||||
|
||||
/* a Tektronix 4014 */
|
||||
dmc_vt_set_profile(t, "tek4014");
|
||||
vfeed(t, "\x1d\x20\x60\x20\x40\x3f\x7f\x3f\x5f");
|
||||
CHECK(has_image(t));
|
||||
|
||||
/* a reset keeps the profile */
|
||||
dmc_vt_reset(t);
|
||||
CHECK_STR(dmc_vt_get_profile(t), "tek4014");
|
||||
dmc_vt_free(t);
|
||||
}
|
||||
|
||||
static void test_daemon(void)
|
||||
{
|
||||
char b[256];
|
||||
@@ -473,18 +719,632 @@ static void test_daemon(void)
|
||||
CHECK(!dmc_daemon_error("13:21:22.774 sip: SIP on 127.0.0.1:5060"));
|
||||
}
|
||||
|
||||
/* the session's bytes to the modem, binary: how many into b */
|
||||
static int outb(dmc_session *s, unsigned char *b, int max)
|
||||
{
|
||||
return dmc_output(s, (char *)b, max);
|
||||
}
|
||||
|
||||
static void feedb(dmc_session *s, const unsigned char *b, int n)
|
||||
{
|
||||
dmc_feed(s, (const char *)b, n);
|
||||
}
|
||||
|
||||
/* the setup of a voice call: voice mode, mu-law, then cmd and its
|
||||
result; the stream started */
|
||||
static void voice_setup(dmc_session *s, const char *cmd, int state)
|
||||
{
|
||||
CHECK_STR(out(s), "AT+FCLASS=8\r");
|
||||
feed(s, "AT+FCLASS=8\r\r\nOK\r\n");
|
||||
CHECK_STR(out(s), "AT+VSM=4\r");
|
||||
feed(s, "AT+VSM=4\r\r\nOK\r\n");
|
||||
char want[64];
|
||||
snprintf(want, sizeof(want), "%s\r", cmd);
|
||||
CHECK_STR(out(s), want);
|
||||
snprintf(want, sizeof(want), "%s\r\r\nOK\r\n", cmd);
|
||||
feed(s, want);
|
||||
CHECK(dmc_state(s) == state);
|
||||
CHECK_STR(out(s), "AT+VTR\r");
|
||||
term(s);
|
||||
feed(s, "AT+VTR\r\r\nCONNECT\r\n");
|
||||
/* (none of it on the screen) */
|
||||
CHECK_STR(term(s), "");
|
||||
}
|
||||
|
||||
/* a voice call dialled: the samples both ways, DTMF, hanging up */
|
||||
static void test_voice(void)
|
||||
{
|
||||
dmc_session *s = dmc_session_new();
|
||||
dmc_event ev;
|
||||
static unsigned char b[65536];
|
||||
int16_t pcm[256];
|
||||
dmc_open(s, 0);
|
||||
int tag = dmc_voice_dial(s, "555-1000");
|
||||
CHECK(tag > 0);
|
||||
CHECK(dmc_voice_dial(s, "1") == -1);
|
||||
CHECK(dmc_command(s, "ATI3", 0, 0) == -1);
|
||||
CHECK_STR(out(s), "AT+FCLASS=8\r");
|
||||
feed(s, "AT+FCLASS=8\r\r\nOK\r\n");
|
||||
CHECK_STR(out(s), "AT+VSM=4\r");
|
||||
feed(s, "AT+VSM=4\r\r\nOK\r\n");
|
||||
CHECK_STR(out(s), "ATD555-1000\r");
|
||||
CHECK(dmc_state(s) == DMC_DIALING);
|
||||
feed(s, "ATD555-1000\r\r\nOK\r\n");
|
||||
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
|
||||
CHECK(ev.tag == tag && ev.ok);
|
||||
CHECK(dmc_state(s) == DMC_VOICE);
|
||||
CHECK_STR(dmc_state_name(DMC_VOICE), "Voice call");
|
||||
CHECK_STR(out(s), "AT+VTR\r");
|
||||
term(s);
|
||||
/* the stream: samples (a DLE among them doubled), a digit */
|
||||
unsigned char in[] = { 'C', 'O', 'N', 'N', 'E', 'C', 'T', '\r', '\n',
|
||||
0xff, 0x80, 0x10, 0x10, 0x00, 0x10, '5',
|
||||
0x10, '~', 0x7f };
|
||||
feedb(s, in, sizeof(in));
|
||||
CHECK_STR(term(s), "");
|
||||
int n = dmc_voice_read(s, pcm, 256);
|
||||
CHECK(n == 5);
|
||||
CHECK(pcm[0] == 0);
|
||||
CHECK(pcm[1] == 32124);
|
||||
CHECK(pcm[3] == -32124);
|
||||
CHECK(pcm[4] == 0);
|
||||
CHECK(next_of(s, DMC_EV_VOICE, &ev));
|
||||
CHECK_STR(ev.text, "5");
|
||||
CHECK_STR(ev.result, "digit");
|
||||
CHECK(!next_of(s, DMC_EV_VOICE, &ev));
|
||||
/* ours: silence, 60 ms ahead, then at the line's pace */
|
||||
CHECK(dmc_poll(s, 0) == 20);
|
||||
n = outb(s, b, sizeof(b));
|
||||
CHECK(n == 480);
|
||||
CHECK(b[0] == 0xff && b[479] == 0xff);
|
||||
for (int i = 0; i < 160; i++)
|
||||
pcm[i] = 8000;
|
||||
dmc_voice_write(s, pcm, 160);
|
||||
dmc_poll(s, 20);
|
||||
n = outb(s, b, sizeof(b));
|
||||
CHECK(n == 160);
|
||||
CHECK(b[0] != 0xff && b[0] == b[159]);
|
||||
/* a sample that is a DLE: doubled */
|
||||
pcm[0] = -15996;
|
||||
dmc_voice_write(s, pcm, 1);
|
||||
dmc_poll(s, 21);
|
||||
n = outb(s, b, sizeof(b));
|
||||
CHECK(n == 9 && b[0] == 0x10 && b[1] == 0x10);
|
||||
/* a DTMF digit: its tone in our samples */
|
||||
CHECK(dmc_voice_dtmf(s, '!') == -1);
|
||||
CHECK(dmc_voice_dtmf(s, '1') == 0);
|
||||
dmc_poll(s, 41);
|
||||
n = outb(s, b, sizeof(b));
|
||||
int tone = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
if (b[i] != 0xff)
|
||||
tone++;
|
||||
CHECK(n == 160 && tone > 100);
|
||||
/* hung up here: <DLE>^, the modem's <DLE><ETX> and OK, ATH */
|
||||
dmc_hangup(s);
|
||||
n = outb(s, b, sizeof(b));
|
||||
CHECK(n == 2 && b[0] == 0x10 && b[1] == '^');
|
||||
CHECK(dmc_state(s) == DMC_HANGING_UP);
|
||||
unsigned char end[] = { 0xff, 0x10, 0x03, '\r', '\n', 'O', 'K', '\r',
|
||||
'\n' };
|
||||
feedb(s, end, sizeof(end));
|
||||
CHECK_STR(out(s), "ATH\r");
|
||||
feed(s, "ATH\r\r\nOK\r\n");
|
||||
CHECK(dmc_state(s) == DMC_IDLE);
|
||||
CHECK(dmc_poll(s, 100) == -1);
|
||||
|
||||
/* the far end hangs up: <DLE>l, then the stream ended as before */
|
||||
tag = dmc_voice_dial(s, "1");
|
||||
CHECK_STR(out(s), "AT+FCLASS=8\r");
|
||||
feed(s, "AT+FCLASS=8\r\r\nOK\r\nAT+VSM=4\r\r\nOK\r\n");
|
||||
CHECK_STR(out(s), "AT+VSM=4\rATD1\r");
|
||||
feed(s, "ATD1\r\r\nOK\r\nAT+VTR\r\r\nCONNECT\r\n");
|
||||
CHECK(dmc_state(s) == DMC_VOICE);
|
||||
unsigned char lci[] = { 0x10, 'l' };
|
||||
feedb(s, lci, 2);
|
||||
CHECK(next_of(s, DMC_EV_VOICE, &ev));
|
||||
CHECK_STR(ev.result, "hangup");
|
||||
n = outb(s, b, sizeof(b));
|
||||
CHECK(n >= 2 && b[n - 2] == 0x10 && b[n - 1] == '^');
|
||||
feedb(s, end + 1, sizeof(end) - 1);
|
||||
CHECK_STR(out(s), "ATH\r");
|
||||
feed(s, "ATH\r\r\nOK\r\n");
|
||||
CHECK(dmc_state(s) == DMC_IDLE);
|
||||
|
||||
/* busy: the call given up, the modem on hook in data mode */
|
||||
tag = dmc_voice_dial(s, "2");
|
||||
out(s);
|
||||
feed(s, "AT+FCLASS=8\r\r\nOK\r\nAT+VSM=4\r\r\nOK\r\n");
|
||||
out(s);
|
||||
feed(s, "ATD2\r\r\nBUSY\r\n");
|
||||
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
|
||||
CHECK(ev.tag == tag && !ev.ok);
|
||||
CHECK_STR(ev.result, "BUSY");
|
||||
CHECK(dmc_state(s) == DMC_IDLE);
|
||||
CHECK_STR(out(s), "ATH\r");
|
||||
feed(s, "ATH\r\r\nOK\r\n");
|
||||
CHECK(dmc_state(s) == DMC_IDLE);
|
||||
|
||||
/* a modem with no voice mode: the call refused */
|
||||
tag = dmc_voice_dial(s, "3");
|
||||
out(s);
|
||||
feed(s, "AT+FCLASS=8\r\r\nERROR\r\n");
|
||||
CHECK(next_of(s, DMC_EV_RESPONSE, &ev));
|
||||
CHECK(ev.tag == tag && !ev.ok);
|
||||
CHECK_STR(out(s), "ATH\r");
|
||||
feed(s, "ATH\r\r\nOK\r\n");
|
||||
CHECK(dmc_command(s, "ATI3", 0, 0) > 0);
|
||||
CHECK_STR(out(s), "ATI3\r");
|
||||
feed(s, "ATI3\r\r\nOK\r\n");
|
||||
|
||||
/* answered: RING, AT+VLS=1 */
|
||||
CHECK(dmc_voice_answer(s) == -1);
|
||||
feed(s, "RING\r\n");
|
||||
CHECK(dmc_ring_count(s) == 1);
|
||||
tag = dmc_voice_answer(s);
|
||||
CHECK(tag > 0);
|
||||
voice_setup(s, "AT+VLS=1", DMC_VOICE);
|
||||
dmc_close(s);
|
||||
CHECK(dmc_state(s) == DMC_CLOSED);
|
||||
dmc_session_free(s);
|
||||
}
|
||||
|
||||
/* the answering machine: answers after two RINGs, the greeting, the
|
||||
beep, the caller's message until they are quiet */
|
||||
static void test_voicemail(void)
|
||||
{
|
||||
dmc_session *s = dmc_session_new();
|
||||
dmc_event ev;
|
||||
static unsigned char b[65536];
|
||||
static int16_t greeting[800];
|
||||
for (int i = 0; i < 800; i++)
|
||||
greeting[i] = (int16_t)(i & 1 ? 6000 : -6000);
|
||||
dmc_voicemail_set(s, greeting, 800, 10, 1);
|
||||
dmc_voicemail_auto(s, 2);
|
||||
dmc_open(s, 0);
|
||||
feed(s, "RING\r\n\r\nNMBR = 5551234\r\n");
|
||||
CHECK_STR(out(s), "");
|
||||
feed(s, "RING\r\n");
|
||||
voice_setup(s, "AT+VLS=1", DMC_VOICEMAIL);
|
||||
CHECK_STR(dmc_state_name(DMC_VOICEMAIL), "Taking a message");
|
||||
/* the greeting, then the beep: 0.1 s and 0.5 s */
|
||||
dmc_poll(s, 0);
|
||||
dmc_poll(s, 1000);
|
||||
int n = outb(s, b, sizeof(b));
|
||||
CHECK(n == 8480);
|
||||
CHECK(b[0] == b[2] && b[0] != 0xff && b[1] != b[0]);
|
||||
CHECK(b[902] != 0xff);
|
||||
CHECK(b[4900] == 0xff);
|
||||
/* the caller speaks for 2 s, then is quiet for 1 s */
|
||||
static unsigned char talk[24000];
|
||||
for (int i = 0; i < 16000; i++)
|
||||
talk[i] = (unsigned char)(i & 1 ? 0x90 : 0x10 + 0x80);
|
||||
memset(talk + 16000, 0xff, 8000);
|
||||
feedb(s, talk, 24000);
|
||||
CHECK(next_of(s, DMC_EV_MESSAGE, &ev));
|
||||
CHECK(ev.ok);
|
||||
CHECK_STR(ev.text, "5551234");
|
||||
const int16_t *msg;
|
||||
n = dmc_voice_message(s, &msg);
|
||||
CHECK(n == 16000 + 4000);
|
||||
CHECK(msg[0] != 0 && msg[n - 1] == 0);
|
||||
n = outb(s, b, sizeof(b));
|
||||
CHECK(n >= 2 && b[n - 2] == 0x10 && b[n - 1] == '^');
|
||||
unsigned char end[] = { 0x10, 0x03, '\r', '\n', 'O', 'K', '\r', '\n' };
|
||||
feedb(s, end, sizeof(end));
|
||||
CHECK_STR(out(s), "ATH\r");
|
||||
feed(s, "ATH\r\r\nOK\r\n");
|
||||
CHECK(dmc_state(s) == DMC_IDLE);
|
||||
|
||||
/* a caller who hangs up in the greeting: no message */
|
||||
dmc_voicemail_auto(s, 0);
|
||||
feed(s, "RING\r\n");
|
||||
CHECK_STR(out(s), "");
|
||||
CHECK(dmc_voicemail_answer(s) > 0);
|
||||
voice_setup(s, "AT+VLS=1", DMC_VOICEMAIL);
|
||||
unsigned char lci[] = { 0x10, 'l' };
|
||||
feedb(s, lci, 2);
|
||||
feedb(s, end, sizeof(end));
|
||||
out(s);
|
||||
feed(s, "ATH\r\r\nOK\r\n");
|
||||
CHECK(next_of(s, DMC_EV_MESSAGE, &ev));
|
||||
CHECK(!ev.ok);
|
||||
CHECK(dmc_state(s) == DMC_IDLE);
|
||||
|
||||
/* picked up while it plays the greeting: a voice call */
|
||||
feed(s, "RING\r\n");
|
||||
CHECK(dmc_voicemail_answer(s) > 0);
|
||||
voice_setup(s, "AT+VLS=1", DMC_VOICEMAIL);
|
||||
CHECK(dmc_voice_pickup(s) == 0);
|
||||
CHECK(dmc_state(s) == DMC_VOICE);
|
||||
CHECK(dmc_voice_pickup(s) == -1);
|
||||
dmc_hangup(s);
|
||||
feedb(s, end, sizeof(end));
|
||||
out(s);
|
||||
feed(s, "ATH\r\r\nOK\r\n");
|
||||
CHECK(dmc_state(s) == DMC_IDLE);
|
||||
CHECK(!next_of(s, DMC_EV_MESSAGE, &ev));
|
||||
dmc_session_free(s);
|
||||
}
|
||||
|
||||
/* WAV files: ours read back; 16 kHz stereo made 8 kHz mono */
|
||||
static void test_wav(void)
|
||||
{
|
||||
char h[44 + 8];
|
||||
int16_t *pcm;
|
||||
CHECK(dmc_wav_header(h, 4) == 44);
|
||||
CHECK(!memcmp(h, "RIFF", 4) && !memcmp(h + 36, "data", 4));
|
||||
CHECK((unsigned char)h[40] == 8);
|
||||
memcpy(h + 44, "\x10\x00\xf0\xff\x00\x40\x00\x80", 8);
|
||||
CHECK(dmc_wav_decode(h, sizeof(h), &pcm) == 4);
|
||||
CHECK(pcm[0] == 16 && pcm[1] == -16 && pcm[2] == 16384 &&
|
||||
pcm[3] == -32768);
|
||||
dmc_free(pcm);
|
||||
/* 16 kHz, two channels, 8 frames: 4 samples */
|
||||
char w[44 + 32];
|
||||
dmc_wav_header(w, 16);
|
||||
w[22] = 2; /* channels */
|
||||
w[24] = (char)0x80; w[25] = 0x3e; /* 16000 */
|
||||
memset(w + 44, 0, 32);
|
||||
w[44] = 0x00; w[45] = 0x10; /* frame 0: 4096 and 0 */
|
||||
CHECK(dmc_wav_decode(w, sizeof(w), &pcm) == 4);
|
||||
CHECK(pcm[0] == 2048 && pcm[1] == 0);
|
||||
dmc_free(pcm);
|
||||
CHECK(dmc_wav_decode("RIFF", 4, &pcm) == -1 && !pcm);
|
||||
}
|
||||
|
||||
/* (ffmpeg -f lavfi -i sine=f=500:r=8000:d=0.07, -c:a adpcm_ima_wav or adpcm_ms -block_size 64) */
|
||||
static const unsigned char ima_wav[414] = {
|
||||
0x52, 0x49, 0x46, 0x46, 0x96, 0x01, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20,
|
||||
0x14, 0x00, 0x00, 0x00, 0x11, 0x00, 0x01, 0x00, 0x40, 0x1f, 0x00, 0x00, 0x80, 0x3e, 0x00, 0x00,
|
||||
0x40, 0x00, 0x04, 0x00, 0x02, 0x00, 0x79, 0x00, 0x66, 0x61, 0x63, 0x74, 0x04, 0x00, 0x00, 0x00,
|
||||
0x30, 0x02, 0x00, 0x00, 0x4c, 0x49, 0x53, 0x54, 0x1a, 0x00, 0x00, 0x00, 0x49, 0x4e, 0x46, 0x4f,
|
||||
0x49, 0x53, 0x46, 0x54, 0x0e, 0x00, 0x00, 0x00, 0x4c, 0x61, 0x76, 0x66, 0x36, 0x30, 0x2e, 0x31,
|
||||
0x36, 0x2e, 0x31, 0x30, 0x30, 0x00, 0x64, 0x61, 0x74, 0x61, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x77, 0x77, 0x77, 0xf2, 0xad, 0x99, 0x21, 0x53, 0x33, 0x12, 0xb9, 0xcd, 0xbb, 0x8a,
|
||||
0x30, 0x45, 0x33, 0x12, 0xb9, 0xcd, 0xbb, 0x9a, 0x31, 0x45, 0x33, 0x12, 0xb9, 0xcd, 0xbb, 0x9a,
|
||||
0x31, 0x45, 0x33, 0x12, 0xb9, 0xcd, 0xbb, 0x9a, 0x31, 0x45, 0x33, 0x12, 0xb9, 0xcd, 0xbb, 0x9a,
|
||||
0x31, 0x45, 0x33, 0x12, 0xb9, 0xcd, 0xbb, 0x9a, 0x31, 0x45, 0x33, 0x12, 0xb9, 0xcd, 0xe1, 0xf9,
|
||||
0x39, 0x00, 0x9b, 0x19, 0x52, 0x43, 0x23, 0x80, 0xdb, 0xcb, 0xab, 0x18, 0x52, 0x43, 0x23, 0x90,
|
||||
0xda, 0xcb, 0xab, 0x18, 0x52, 0x43, 0x23, 0x90, 0xda, 0xcb, 0xab, 0x18, 0x52, 0x43, 0x23, 0x90,
|
||||
0xda, 0xcb, 0xab, 0x18, 0x52, 0x43, 0x23, 0x90, 0xda, 0xcb, 0xab, 0x18, 0x52, 0x43, 0x23, 0x90,
|
||||
0xda, 0xcb, 0xab, 0x18, 0x52, 0x43, 0x23, 0x90, 0xda, 0xcb, 0xab, 0x18, 0x52, 0x43, 0x50, 0x0b,
|
||||
0x39, 0x00, 0x02, 0xa9, 0xcc, 0xbb, 0x8a, 0x30, 0x45, 0x33, 0x12, 0xb9, 0xcd, 0xbb, 0x8a, 0x30,
|
||||
0x45, 0x33, 0x02, 0xb8, 0xcd, 0xbb, 0x8a, 0x30, 0x45, 0x33, 0x02, 0xb8, 0xcd, 0xbb, 0x8a, 0x30,
|
||||
0x45, 0x33, 0x02, 0xb8, 0xcd, 0xbb, 0x8a, 0x30, 0x45, 0x33, 0x02, 0xb8, 0xcd, 0xbb, 0x8a, 0x30,
|
||||
0x45, 0x33, 0x02, 0xb8, 0xcd, 0xbb, 0x8a, 0x30, 0x45, 0x33, 0x02, 0xb8, 0xcd, 0xbb, 0x39, 0xf1,
|
||||
0x37, 0x00, 0x08, 0x43, 0x44, 0x22, 0x80, 0xcb, 0xbc, 0xab, 0x08, 0x53, 0x34, 0x23, 0x91, 0xdb,
|
||||
0xbc, 0xab, 0x08, 0x53, 0x34, 0x23, 0x91, 0xdb, 0xbc, 0xab, 0x08, 0x53, 0x34, 0x23, 0x91, 0xdb,
|
||||
0xbc, 0xab, 0x08, 0x53, 0x34, 0x23, 0x91, 0xdb, 0xbc, 0xab, 0x08, 0x53, 0x34, 0x23, 0x91, 0xdb,
|
||||
0xbc, 0xab, 0x19, 0x53, 0x34, 0x23, 0x91, 0xdb, 0xbc, 0xab, 0x19, 0x53, 0x34, 0x23, 0xff, 0x0f,
|
||||
0x36, 0x00, 0xb8, 0xbd, 0xbc, 0x8a, 0x21, 0x35, 0x34, 0x02, 0xa9, 0xbd, 0xbc, 0x8a, 0x21, 0x35,
|
||||
0x34, 0x02, 0xa9, 0xbd, 0xbc, 0x8a, 0x21, 0x35, 0x34, 0x02, 0xa9, 0xbd, 0xbc, 0x8a, 0x21, 0x35,
|
||||
0x34, 0x02, 0xa9, 0xbd, 0xbc, 0x8a, 0x21, 0x35, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
|
||||
0x80, 0x80, 0x80, 0x80, 0x00, 0x88, 0x00, 0x88, 0x00, 0x88, 0x00, 0x88, 0x00, 0x08,
|
||||
};
|
||||
|
||||
static const unsigned char ms_wav[444] = {
|
||||
0x52, 0x49, 0x46, 0x46, 0xb4, 0x01, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20,
|
||||
0x32, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x40, 0x1f, 0x00, 0x00, 0x80, 0x3e, 0x00, 0x00,
|
||||
0x40, 0x00, 0x04, 0x00, 0x20, 0x00, 0x74, 0x00, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
|
||||
0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x40, 0x00, 0xf0, 0x00, 0x00, 0x00, 0xcc, 0x01,
|
||||
0x30, 0xff, 0x88, 0x01, 0x18, 0xff, 0x66, 0x61, 0x63, 0x74, 0x04, 0x00, 0x00, 0x00, 0x30, 0x02,
|
||||
0x00, 0x00, 0x4c, 0x49, 0x53, 0x54, 0x1a, 0x00, 0x00, 0x00, 0x49, 0x4e, 0x46, 0x4f, 0x49, 0x53,
|
||||
0x46, 0x54, 0x0e, 0x00, 0x00, 0x00, 0x4c, 0x61, 0x76, 0x66, 0x36, 0x30, 0x2e, 0x31, 0x36, 0x2e,
|
||||
0x31, 0x30, 0x30, 0x00, 0x64, 0x61, 0x74, 0x61, 0x40, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1f,
|
||||
0x06, 0x00, 0x00, 0x77, 0x76, 0xec, 0xdc, 0xdf, 0xf1, 0x25, 0x34, 0x32, 0x00, 0xdb, 0xdc, 0xde,
|
||||
0x00, 0x35, 0x34, 0x32, 0x00, 0xdb, 0xdc, 0xde, 0x00, 0x35, 0x34, 0x32, 0x00, 0xdb, 0xdc, 0xde,
|
||||
0xf1, 0x35, 0x34, 0x32, 0x1f, 0xdb, 0xdc, 0xde, 0xf1, 0x35, 0x34, 0x32, 0x1f, 0xdb, 0xdc, 0xde,
|
||||
0xf1, 0x35, 0x34, 0x32, 0x1f, 0xdb, 0xdc, 0xde, 0xf1, 0x35, 0x34, 0x32, 0x00, 0x87, 0x01, 0xc7,
|
||||
0x0e, 0xff, 0x0f, 0xec, 0xcd, 0xde, 0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35,
|
||||
0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35,
|
||||
0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35,
|
||||
0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0x00, 0x99, 0x01, 0xe1,
|
||||
0xf9, 0x00, 0x00, 0xdd, 0xf1, 0x44, 0x44, 0x22, 0x1f, 0xdc, 0xcd, 0xce, 0x00, 0x34, 0x44, 0x22,
|
||||
0x1f, 0xdc, 0xcd, 0xce, 0x01, 0x24, 0x44, 0x22, 0x1f, 0xdc, 0xcd, 0xce, 0x01, 0x24, 0x44, 0x22,
|
||||
0x1f, 0xdc, 0xcd, 0xce, 0x01, 0x24, 0x44, 0x22, 0x1f, 0xdc, 0xcd, 0xce, 0x01, 0x24, 0x44, 0x22,
|
||||
0x1f, 0xdc, 0xcd, 0xce, 0x01, 0x24, 0x44, 0x22, 0x1f, 0xdc, 0xcd, 0xce, 0x00, 0xb7, 0x01, 0x39,
|
||||
0xf1, 0x01, 0xf0, 0x23, 0x53, 0x22, 0x1f, 0xeb, 0xdc, 0xde, 0x00, 0x35, 0x34, 0x32, 0x0f, 0xeb,
|
||||
0xdc, 0xde, 0x00, 0x35, 0x34, 0x32, 0x00, 0xdb, 0xdc, 0xde, 0x00, 0x35, 0x34, 0x32, 0x00, 0xdb,
|
||||
0xdc, 0xde, 0xf1, 0x35, 0x34, 0x32, 0x1f, 0xdb, 0xdc, 0xde, 0xf1, 0x35, 0x34, 0x32, 0x1f, 0xdb,
|
||||
0xdc, 0xde, 0xf1, 0x35, 0x34, 0x32, 0x1f, 0xdb, 0xdc, 0xde, 0xf1, 0x35, 0x00, 0xc4, 0x01, 0x1f,
|
||||
0x06, 0x00, 0x00, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde,
|
||||
0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde,
|
||||
0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde, 0xf1, 0x35, 0x43, 0x32, 0x1f, 0xdb, 0xcd, 0xde,
|
||||
0xf1, 0x35, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x1f, 0x1f,
|
||||
};
|
||||
|
||||
/* ffmpeg's samples [1, 5, 9, 100, 300, 577] of it */
|
||||
static const int ima_at[] = { 1, 5, 9, 100, 300, 577 };
|
||||
static const int ima_want[] = { 13, 536, -1554, 4238, -3936, 8 };
|
||||
static const int ima_n = 605;
|
||||
/* ffmpeg's samples [1, 5, 9, 100, 300, 577] of it */
|
||||
static const int ms_at[] = { 1, 5, 9, 100, 300, 577 };
|
||||
static const int ms_want[] = { 1567, 3890, -1630, 4191, -4014, -40 };
|
||||
static const int ms_n = 580;
|
||||
|
||||
static void be16(char *p, unsigned v) { p[0] = (char)(v >> 8); p[1] = (char)v; }
|
||||
static void be32(char *p, unsigned long v) { be16(p, (unsigned)(v >> 16)); be16(p + 2, (unsigned)(v & 0xffff)); }
|
||||
|
||||
/* sound files other than 16-bit WAV: AIFF, .au, 24-bit and float WAV,
|
||||
IMA and Microsoft ADPCM (against ffmpeg's decoding) */
|
||||
static void test_formats(void)
|
||||
{
|
||||
int16_t *pcm;
|
||||
int i, n;
|
||||
/* AIFF, 16-bit big-endian, 8000 Hz (80-bit: 0x400B FA00...), mono */
|
||||
char a[12 + 26 + 16 + 8];
|
||||
memset(a, 0, sizeof(a));
|
||||
memcpy(a, "FORM", 4); be32(a + 4, sizeof(a) - 8); memcpy(a + 8, "AIFF", 4);
|
||||
memcpy(a + 12, "COMM", 4); be32(a + 16, 18);
|
||||
be16(a + 20, 1); be32(a + 22, 4); be16(a + 26, 16);
|
||||
a[28] = 0x40; a[29] = 0x0b; a[30] = (char)0xfa;
|
||||
memcpy(a + 38, "SSND", 4); be32(a + 42, 8 + 8);
|
||||
be16(a + 54, 0x1000); be16(a + 56, 0xf000); be16(a + 58, 0x7fff); be16(a + 60, 0x8000);
|
||||
CHECK(dmc_audio_decode(a, sizeof(a), &pcm) == 4);
|
||||
CHECK(pcm[0] == 4096 && pcm[1] == -4096 && pcm[2] == 32767 && pcm[3] == -32768);
|
||||
dmc_free(pcm);
|
||||
/* .au, mu-law, 8000 Hz, mono */
|
||||
char u[24 + 3];
|
||||
memcpy(u, ".snd", 4); be32(u + 4, 24); be32(u + 8, 3); be32(u + 12, 1);
|
||||
be32(u + 16, 8000); be32(u + 20, 1);
|
||||
u[24] = (char)0xff; u[25] = 0x00; u[26] = (char)0x80;
|
||||
CHECK(dmc_audio_decode(u, sizeof(u), &pcm) == 3);
|
||||
CHECK(pcm[0] == 0 && pcm[1] == -32124 && pcm[2] == 32124);
|
||||
dmc_free(pcm);
|
||||
/* WAV, 24-bit, then 32-bit float */
|
||||
char w[44 + 6];
|
||||
dmc_wav_header(w, 3);
|
||||
w[32] = 3; w[34] = 24; w[40] = 6;
|
||||
memcpy(w + 44, "\x00\x00\x40\x00\x00\xc0", 6);
|
||||
CHECK(dmc_audio_decode(w, sizeof(w), &pcm) == 2);
|
||||
CHECK(pcm[0] == 16384 && pcm[1] == -16384);
|
||||
dmc_free(pcm);
|
||||
char f[44 + 8];
|
||||
float h = 0.5f, m = -1.0f;
|
||||
dmc_wav_header(f, 4);
|
||||
f[20] = 3; f[32] = 4; f[34] = 32; f[40] = 8;
|
||||
memcpy(f + 44, &h, 4); memcpy(f + 48, &m, 4);
|
||||
CHECK(dmc_audio_decode(f, sizeof(f), &pcm) == 2);
|
||||
CHECK(pcm[0] == 16383 && pcm[1] == -32767);
|
||||
dmc_free(pcm);
|
||||
/* ADPCM: Microsoft's as ffmpeg decodes it, IMA within a few steps
|
||||
(ffmpeg's arithmetic is finer than the IMA's own) */
|
||||
n = dmc_audio_decode((const char *)ms_wav, sizeof(ms_wav), &pcm);
|
||||
CHECK(n == ms_n);
|
||||
for (i = 0; n == ms_n && i < (int)(sizeof(ms_at) / sizeof(ms_at[0])); i++)
|
||||
CHECK(pcm[ms_at[i]] == ms_want[i]);
|
||||
dmc_free(pcm);
|
||||
n = dmc_audio_decode((const char *)ima_wav, sizeof(ima_wav), &pcm);
|
||||
CHECK(n == ima_n);
|
||||
for (i = 0; n == ima_n && i < (int)(sizeof(ima_at) / sizeof(ima_at[0])); i++)
|
||||
CHECK(abs(pcm[ima_at[i]] - ima_want[i]) <= 8);
|
||||
dmc_free(pcm);
|
||||
CHECK(dmc_audio_decode("FORM\0\0\0\4AIFF", 12, &pcm) == -1 && !pcm);
|
||||
CHECK(dmc_audio_decode("ID3\4\0\0\0\0\0\0\0\0", 12, &pcm) == -1);
|
||||
}
|
||||
|
||||
/* what the codec has for the socket, as a string of escaped bytes */
|
||||
static int out_is(dmc_net *n, const char *want, int wn)
|
||||
{
|
||||
char b[512];
|
||||
int k = dmc_net_output(n, b, sizeof(b));
|
||||
return k == wn && !memcmp(b, want, (size_t)wn);
|
||||
}
|
||||
|
||||
static int screen_is(dmc_net *n, const char *want, int wn)
|
||||
{
|
||||
char b[512];
|
||||
int k = dmc_net_screen(n, b, sizeof(b));
|
||||
return k == wn && !memcmp(b, want, (size_t)wn);
|
||||
}
|
||||
|
||||
static void test_net(void)
|
||||
{
|
||||
CHECK(dmc_net_default_port(DMC_NET_TELNET) == 23);
|
||||
CHECK(dmc_net_default_port(DMC_NET_RLOGIN) == 513);
|
||||
CHECK(dmc_net_default_port(DMC_NET_SSH) == 22);
|
||||
CHECK_STR(dmc_net_name(DMC_NET_RLOGIN), "rlogin");
|
||||
|
||||
/* Telnet: the first offers, then the server's */
|
||||
dmc_net *n = dmc_net_new(DMC_NET_TELNET, "", "ANSI", 80, 24);
|
||||
dmc_net_connected(n);
|
||||
CHECK(out_is(n, "\xff\xfb\x18\xff\xfb\x1f\xff\xfd\x03", 9));
|
||||
/* DO TTYPE, DO NAWS (our size), WILL ECHO, WILL SGA (asked:
|
||||
not answered again), DO LINEMODE (refused), text, IAC IAC */
|
||||
const char *in = "\xff\xfd\x18\xff\xfd\x1f\xff\xfb\x01\xff\xfb\x03"
|
||||
"\xff\xfd\x22Hi\xff\xff!";
|
||||
dmc_net_feed(n, in, 20);
|
||||
CHECK(out_is(n, "\xff\xfa\x1f\x00\x50\x00\x18\xff\xf0"
|
||||
"\xff\xfd\x01\xff\xfc\x22", 15));
|
||||
CHECK(screen_is(n, "Hi\xff!", 4));
|
||||
/* TERMINAL-TYPE SEND, split across two reads */
|
||||
dmc_net_feed(n, "\xff\xfa\x18", 3);
|
||||
dmc_net_feed(n, "\x01\xff\xf0", 3);
|
||||
CHECK(out_is(n, "\xff\xfa\x18\x00" "ANSI\xff\xf0", 10));
|
||||
/* CR NUL is a CR */
|
||||
dmc_net_feed(n, "a\r\0b", 4);
|
||||
CHECK(screen_is(n, "a\rb", 3));
|
||||
/* typed: a lone CR as CR NUL, IAC doubled */
|
||||
dmc_net_type(n, "ls\r\xff", 4);
|
||||
CHECK(out_is(n, "ls\r\0\xff\xff", 6));
|
||||
dmc_net_type(n, "\r\n", 2);
|
||||
CHECK(out_is(n, "\r\n", 2));
|
||||
/* resized: NAWS, an 0xff in it doubled */
|
||||
dmc_net_resize(n, 255, 50);
|
||||
CHECK(out_is(n, "\xff\xfa\x1f\x00\xff\xff\x00\x32\xff\xf0", 10));
|
||||
dmc_net_resize(n, 255, 50);
|
||||
CHECK(out_is(n, "", 0));
|
||||
/* WONT ECHO: DONT; and again: nothing */
|
||||
dmc_net_feed(n, "\xff\xfc\x01\xff\xfc\x01", 6);
|
||||
CHECK(out_is(n, "\xff\xfe\x01", 3));
|
||||
/* a break: IAC BRK */
|
||||
CHECK(dmc_net_break(n) == 1);
|
||||
CHECK(out_is(n, "\xff\xf3", 2));
|
||||
dmc_net_free(n);
|
||||
|
||||
/* rlogin */
|
||||
n = dmc_net_new(DMC_NET_RLOGIN, "alice", "xterm", 80, 24);
|
||||
dmc_net_connected(n);
|
||||
CHECK(out_is(n, "\0alice\0alice\0xterm/38400\0", 25));
|
||||
/* the server's NUL, then its text */
|
||||
dmc_net_feed(n, "\0Password:", 10);
|
||||
CHECK(screen_is(n, "Password:", 9));
|
||||
dmc_net_feed(n, "\0", 1);
|
||||
CHECK(screen_is(n, "\0", 1));
|
||||
/* no size until it is asked for */
|
||||
dmc_net_resize(n, 100, 30);
|
||||
CHECK(out_is(n, "", 0));
|
||||
dmc_net_urgent(n, 0x80);
|
||||
CHECK(out_is(n, "\xff\xffss\x00\x1e\x00\x64\x03\x20\x01\xe0", 12));
|
||||
dmc_net_resize(n, 80, 24);
|
||||
CHECK(out_is(n, "\xff\xffss\x00\x18\x00\x50\x02\x80\x01\x80", 12));
|
||||
/* typed as it is */
|
||||
dmc_net_type(n, "\r\xff", 2);
|
||||
CHECK(out_is(n, "\r\xff", 2));
|
||||
/* flush */
|
||||
dmc_net_feed(n, "old", 3);
|
||||
dmc_net_urgent(n, 0x02);
|
||||
CHECK(screen_is(n, "", 0));
|
||||
/* rlogin has no break */
|
||||
CHECK(dmc_net_break(n) == 0);
|
||||
CHECK(out_is(n, "", 0));
|
||||
dmc_net_free(n);
|
||||
}
|
||||
|
||||
/* the connection path: each line's steps, idle as set up, in a call as
|
||||
it is */
|
||||
static void test_path(void)
|
||||
{
|
||||
dmc_path *p = dmc_path_new();
|
||||
dmc_path_set_line(p, DMC_LINE_SIP, "", 0, "alice", "sip.example.com", 1);
|
||||
CHECK(dmc_path_steps(p) == 4);
|
||||
CHECK(dmc_path_kind(p, 0) == DMC_KIND_TERMINAL);
|
||||
CHECK(dmc_path_kind(p, 1) == DMC_KIND_SOFTMODEM);
|
||||
CHECK(dmc_path_kind(p, 3) == DMC_KIND_PHONE_NETWORK);
|
||||
CHECK_STR(dmc_path_role(p, 2), "Line");
|
||||
CHECK_STR(dmc_path_detail(p, 2), "Registers as alice@sip.example.com");
|
||||
CHECK(!dmc_path_needs_setup(p));
|
||||
CHECK_STR(dmc_path_headline(p), "Ready: start the modem to dial");
|
||||
|
||||
/* no account yet */
|
||||
dmc_path_set_line(p, DMC_LINE_SIP, "", 0, "", "", 0);
|
||||
CHECK(dmc_path_state(p, 2) == DMC_STEP_SETUP);
|
||||
CHECK(dmc_path_link(p, 1) == DMC_STEP_SETUP);
|
||||
CHECK(dmc_path_link(p, 0) == DMC_STEP_READY);
|
||||
CHECK_STR(dmc_path_headline(p), "Not ready: SIP (no account set up)");
|
||||
|
||||
/* anonymous */
|
||||
dmc_path_set_line(p, DMC_LINE_SIP, "", 1, "", "", 0);
|
||||
CHECK(dmc_path_kind(p, 3) == DMC_KIND_SIP_ADDRESS);
|
||||
CHECK(!dmc_path_needs_setup(p));
|
||||
|
||||
/* a modem that already runs: reached by its port */
|
||||
dmc_path_set_line(p, DMC_LINE_COM, "COM2", 0, "", "", 0);
|
||||
CHECK(dmc_path_kind(p, 1) == DMC_KIND_SERIAL);
|
||||
CHECK_STR(dmc_path_role(p, 1), "Port");
|
||||
CHECK_STR(dmc_path_detail(p, 1), "COM2");
|
||||
dmc_path_set_line(p, DMC_LINE_DEVICE, "", 0, "", "", 0);
|
||||
CHECK(dmc_path_state(p, 1) == DMC_STEP_SETUP);
|
||||
|
||||
/* starting, dialling, online */
|
||||
dmc_path_set_line(p, DMC_LINE_USB, "auto", 0, "", "", 0);
|
||||
dmc_path_set_live(p, 1, DMC_CLOSED, "");
|
||||
CHECK(dmc_path_state(p, 1) == DMC_STEP_WORKING);
|
||||
CHECK(dmc_path_state(p, 2) == DMC_STEP_READY);
|
||||
CHECK_STR(dmc_path_headline(p), "Starting the modem");
|
||||
dmc_path_set_live(p, 1, DMC_DIALING, "");
|
||||
CHECK(dmc_path_link(p, 2) == DMC_STEP_WORKING);
|
||||
dmc_path_set_live(p, 1, DMC_ONLINE, "CONNECT 48000/26400");
|
||||
for (int i = 0; i < 4; i++)
|
||||
CHECK(dmc_path_state(p, i) == DMC_STEP_ACTIVE);
|
||||
CHECK_STR(dmc_path_headline(p), "Online: CONNECT 48000/26400");
|
||||
CHECK(dmc_path_state(p, 9) == DMC_STEP_OFF);
|
||||
dmc_path_free(p);
|
||||
}
|
||||
|
||||
/* ASCII Setup's receive items: an LF after a CR, 7 bits, no wrap */
|
||||
static void test_vt_ascii(void)
|
||||
{
|
||||
dmc_vt *t = dmc_vt_new(10, 5, 100);
|
||||
const char *s;
|
||||
dmc_vt_set_ascii(t, DMC_VT_ASCII_ADD_LF);
|
||||
CHECK(dmc_vt_get_ascii(t) == DMC_VT_ASCII_ADD_LF);
|
||||
/* a CR alone gets its LF; a CR LF (its LF in the next chunk too)
|
||||
gets no second one */
|
||||
s = "one\rtwo\r";
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
s = "\nthree";
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
CHECK_STR(dmc_vt_text(t, 0), "one");
|
||||
CHECK_STR(dmc_vt_text(t, 1), "two");
|
||||
CHECK_STR(dmc_vt_text(t, 2), "three");
|
||||
CHECK(dmc_vt_cursor_row(t) == 2);
|
||||
/* 7 bits: 0xC1 is 'A' */
|
||||
dmc_vt_reset(t);
|
||||
dmc_vt_set_ascii(t, DMC_VT_ASCII_7BIT);
|
||||
s = "\xc1\xe2";
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
CHECK_STR(dmc_vt_text(t, 0), "Ab");
|
||||
/* no wrap: the last column written again */
|
||||
dmc_vt_reset(t);
|
||||
dmc_vt_set_ascii(t, DMC_VT_ASCII_NO_WRAP);
|
||||
s = "0123456789XY";
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
CHECK_STR(dmc_vt_text(t, 0), "012345678Y");
|
||||
CHECK_STR(dmc_vt_text(t, 1), "");
|
||||
/* the scrollback made smaller: the newest lines kept */
|
||||
dmc_vt_reset(t);
|
||||
dmc_vt_set_ascii(t, 0);
|
||||
s = "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8";
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
CHECK(dmc_vt_history(t) == 3);
|
||||
dmc_vt_set_scrollback(t, 2);
|
||||
CHECK(dmc_vt_history(t) == 2);
|
||||
CHECK_STR(dmc_vt_text(t, -2), "2");
|
||||
CHECK_STR(dmc_vt_text(t, -1), "3");
|
||||
dmc_vt_set_scrollback(t, 50);
|
||||
CHECK_STR(dmc_vt_text(t, -1), "3");
|
||||
s = "\r\n9";
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
CHECK(dmc_vt_history(t) == 3);
|
||||
CHECK_STR(dmc_vt_text(t, -1), "4");
|
||||
dmc_vt_set_scrollback(t, 0);
|
||||
CHECK(dmc_vt_history(t) == 0);
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
CHECK(dmc_vt_history(t) == 0);
|
||||
/* and with wrap, as before */
|
||||
dmc_vt_set_scrollback(t, 100);
|
||||
s = "0123456789XY";
|
||||
dmc_vt_reset(t);
|
||||
dmc_vt_set_ascii(t, 0);
|
||||
dmc_vt_feed(t, s, (int)strlen(s));
|
||||
CHECK_STR(dmc_vt_text(t, 1), "XY");
|
||||
dmc_vt_free(t);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *dir = argc > 1 ? argv[1] : ".";
|
||||
CHECK(dmc_version() == DMC_VERSION);
|
||||
test_command();
|
||||
test_pages();
|
||||
test_call();
|
||||
test_unsolicited();
|
||||
test_typed();
|
||||
test_model(dir);
|
||||
test_diag(dir);
|
||||
test_vt();
|
||||
test_vt_profiles();
|
||||
test_vt_noise();
|
||||
test_vt_ascii();
|
||||
test_daemon();
|
||||
test_voice();
|
||||
test_voicemail();
|
||||
test_wav();
|
||||
test_formats();
|
||||
test_net();
|
||||
test_path();
|
||||
printf("%d checks, %d failed\n", checks, failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,942 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* xfer_test.c - the file transfers both ways, one transfer's line the
|
||||
* other's: each protocol, each file as it was sent (and
|
||||
* its time), on a clean line and on one that damages
|
||||
* bytes; a cancel; what starts a transfer. With lrzsz's
|
||||
* sz and rz and C-Kermit (when they are installed, or
|
||||
* DMODEM_XFER_INTEROP=1 makes them needed), the same
|
||||
* against them on a pseudo-terminal.
|
||||
*/
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _XOPEN_SOURCE 700
|
||||
#define _DARWIN_C_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <utime.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "xfer.h"
|
||||
#include "zstd_codec.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(c, ...) do { \
|
||||
if (!(c)) { \
|
||||
failures++; \
|
||||
fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fputc('\n', stderr); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define MTIME 1700000000LL
|
||||
|
||||
static char tmp[512];
|
||||
|
||||
static void write_file(const char *path, const unsigned char *d, size_t n)
|
||||
{
|
||||
FILE *f = fopen(path, "wb");
|
||||
fwrite(d, 1, n, f);
|
||||
fclose(f);
|
||||
struct utimbuf u = { (time_t)MTIME, (time_t)MTIME };
|
||||
utime(path, &u);
|
||||
}
|
||||
|
||||
static unsigned char *read_file(const char *path, size_t *n)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
*n = 0;
|
||||
return NULL;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long l = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
unsigned char *d = (unsigned char *)malloc((size_t)l + 1);
|
||||
*n = fread(d, 1, (size_t)l, f);
|
||||
fclose(f);
|
||||
return d;
|
||||
}
|
||||
|
||||
static long long mtime_of(const char *path)
|
||||
{
|
||||
struct stat sb;
|
||||
return stat(path, &sb) == 0 ? (long long)sb.st_mtime : -1;
|
||||
}
|
||||
|
||||
static void empty_dir(const char *dir)
|
||||
{
|
||||
char cmd[1024];
|
||||
snprintf(cmd, sizeof(cmd), "rm -rf '%s' && mkdir -p '%s'", dir, dir);
|
||||
if (system(cmd) != 0)
|
||||
abort();
|
||||
}
|
||||
|
||||
/* the two transfers' line, until both end: every noise-th byte from a
|
||||
damaged (0 none) */
|
||||
static long long line_bytes; /* what a sent in the last link2 */
|
||||
|
||||
static void link2(dmc_xfer *a, dmc_xfer *b, int noise)
|
||||
{
|
||||
char buf[4096];
|
||||
line_bytes = 0;
|
||||
long long count = 0, end = xfer_now() + 180000;
|
||||
for (;;) {
|
||||
dmc_xfer_status sa, sb;
|
||||
dmc_xfer_get_status(a, &sa);
|
||||
dmc_xfer_get_status(b, &sb);
|
||||
if (!sa.running && !sb.running)
|
||||
break;
|
||||
if (xfer_now() > end) {
|
||||
CHECK(0, "the transfer took too long");
|
||||
dmc_xfer_cancel(a);
|
||||
dmc_xfer_cancel(b);
|
||||
}
|
||||
int moved = 0, n;
|
||||
while ((n = dmc_xfer_output(a, buf, sizeof(buf))) > 0) {
|
||||
for (int i = 0; noise && i < n; i++)
|
||||
if (++count % noise == 0)
|
||||
buf[i] ^= 0x55;
|
||||
dmc_xfer_feed(b, buf, n);
|
||||
line_bytes += n;
|
||||
moved = 1;
|
||||
}
|
||||
while ((n = dmc_xfer_output(b, buf, sizeof(buf))) > 0) {
|
||||
dmc_xfer_feed(a, buf, n);
|
||||
moved = 1;
|
||||
}
|
||||
if (!moved)
|
||||
xfer_sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned char big[40000], small[] = "hello, modem\n";
|
||||
|
||||
static void check_same(const char *path, const unsigned char *d, size_t n,
|
||||
int prefix, const char *what)
|
||||
{
|
||||
size_t got;
|
||||
unsigned char *g = read_file(path, &got);
|
||||
CHECK(g != NULL, "%s: %s not there", what, path);
|
||||
if (!g)
|
||||
return;
|
||||
if (prefix)
|
||||
CHECK(got >= n && !memcmp(g, d, n), "%s: %s differs (%zu, %zu)",
|
||||
what, path, got, n);
|
||||
else
|
||||
CHECK(got == n && !memcmp(g, d, n), "%s: %s differs (%zu, %zu)",
|
||||
what, path, got, n);
|
||||
free(g);
|
||||
}
|
||||
|
||||
/* the protocol both ways, the files as they were */
|
||||
static void both_ways(int p, int noise)
|
||||
{
|
||||
char src[600], dst[600], a[700], b[700];
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
snprintf(a, sizeof(a), "%s/data.bin", src);
|
||||
snprintf(b, sizeof(b), "%s/hello.txt", src);
|
||||
write_file(a, big, sizeof(big));
|
||||
write_file(b, small, sizeof(small) - 1);
|
||||
const char *paths[2] = { a, b };
|
||||
dmc_xfer *r = dmc_xfer_receive(p, dst, "data.bin");
|
||||
dmc_xfer *s = dmc_xfer_send(p, paths, 2);
|
||||
link2(s, r, noise);
|
||||
dmc_xfer_status ss, rs;
|
||||
dmc_xfer_get_status(s, &ss);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
const char *what = dmc_xfer_label(p);
|
||||
CHECK(!ss.result[0], "%s (noise %d): the sender: %s", what, noise, ss.result);
|
||||
CHECK(!rs.result[0], "%s (noise %d): the receiver: %s", what, noise, rs.result);
|
||||
int batch = dmc_xfer_batch(p);
|
||||
int xmodem = p == DMC_XFER_XMODEM || p == DMC_XFER_XMODEM_1K;
|
||||
char got[700];
|
||||
snprintf(got, sizeof(got), "%s/data.bin", dst);
|
||||
check_same(got, big, sizeof(big), xmodem, what);
|
||||
if (batch) {
|
||||
snprintf(got, sizeof(got), "%s/hello.txt", dst);
|
||||
check_same(got, small, sizeof(small) - 1, 0, what);
|
||||
CHECK(rs.files == 2, "%s: %d files", what, rs.files);
|
||||
CHECK(dmc_xfer_received(r, 1) && !strcmp(dmc_xfer_received(r, 1), "hello.txt"),
|
||||
"%s: the second name", what);
|
||||
CHECK(mtime_of(got) == MTIME, "%s: the time %lld", what, mtime_of(got));
|
||||
}
|
||||
if (noise)
|
||||
CHECK(rs.errors + ss.errors > 0, "%s: no errors on a noisy line", what);
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
/* YMODEM-G: an error ends it on both sides (no block again); a sender
|
||||
asked with C does plain YMODEM */
|
||||
static void ymodem_g(void)
|
||||
{
|
||||
char src[600], dst[600], a[700], got[700];
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
snprintf(a, sizeof(a), "%s/data.bin", src);
|
||||
write_file(a, big, sizeof(big));
|
||||
const char *paths[1] = { a };
|
||||
dmc_xfer *r = dmc_xfer_receive(DMC_XFER_YMODEM_G, dst, NULL);
|
||||
dmc_xfer *s = dmc_xfer_send(DMC_XFER_YMODEM_G, paths, 1);
|
||||
link2(s, r, 3001);
|
||||
dmc_xfer_status ss, rs;
|
||||
dmc_xfer_get_status(s, &ss);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(strstr(rs.result, "YMODEM-G") != NULL, "YMODEM-G noisy: the receiver: '%s'",
|
||||
rs.result);
|
||||
CHECK(ss.result[0], "YMODEM-G noisy: the sender went on");
|
||||
snprintf(got, sizeof(got), "%s/data.bin", dst);
|
||||
FILE *f = fopen(got, "rb");
|
||||
CHECK(!f, "YMODEM-G noisy: the part of the file was kept");
|
||||
if (f)
|
||||
fclose(f);
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
|
||||
empty_dir(dst);
|
||||
r = dmc_xfer_receive(DMC_XFER_YMODEM, dst, NULL);
|
||||
s = dmc_xfer_send(DMC_XFER_YMODEM_G, paths, 1);
|
||||
link2(s, r, 0);
|
||||
dmc_xfer_get_status(s, &ss);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(!ss.result[0] && !rs.result[0], "YMODEM-G to YMODEM: '%s' '%s'",
|
||||
ss.result, rs.result);
|
||||
check_same(got, big, sizeof(big), 0, "YMODEM-G to YMODEM");
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
/* link2 with the receiver's skip once it has some bytes of the first file */
|
||||
static void link2_skip(dmc_xfer *a, dmc_xfer *b)
|
||||
{
|
||||
char buf[4096];
|
||||
int skipped = 0;
|
||||
long long end = xfer_now() + 120000;
|
||||
for (;;) {
|
||||
dmc_xfer_status sa, sb;
|
||||
dmc_xfer_get_status(a, &sa);
|
||||
dmc_xfer_get_status(b, &sb);
|
||||
if (!sa.running && !sb.running)
|
||||
break;
|
||||
if (xfer_now() > end) {
|
||||
CHECK(0, "the skip: it took too long");
|
||||
dmc_xfer_cancel(a);
|
||||
dmc_xfer_cancel(b);
|
||||
}
|
||||
if (!skipped && sb.bytes > 8000 && !strcmp(sb.file, "data.bin")) {
|
||||
dmc_xfer_skip(b);
|
||||
skipped = 1;
|
||||
}
|
||||
int moved = 0, n;
|
||||
/* (a slow line: 4 KB a turn) */
|
||||
if ((n = dmc_xfer_output(a, buf, sizeof(buf))) > 0) {
|
||||
dmc_xfer_feed(b, buf, n);
|
||||
moved = 1;
|
||||
}
|
||||
while ((n = dmc_xfer_output(b, buf, sizeof(buf))) > 0) {
|
||||
dmc_xfer_feed(a, buf, n);
|
||||
moved = 1;
|
||||
}
|
||||
xfer_sleep(moved ? 1 : 2);
|
||||
}
|
||||
}
|
||||
|
||||
/* Skip File: the first file dropped, the second received */
|
||||
static void skip_file(int p)
|
||||
{
|
||||
char src[600], dst[600], a[700], b[700], got[700];
|
||||
const char *what = dmc_xfer_label(p);
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
snprintf(a, sizeof(a), "%s/data.bin", src);
|
||||
snprintf(b, sizeof(b), "%s/hello.txt", src);
|
||||
write_file(a, big, sizeof(big));
|
||||
write_file(b, small, sizeof(small) - 1);
|
||||
const char *paths[2] = { a, b };
|
||||
dmc_xfer *r = dmc_xfer_receive(p, dst, NULL);
|
||||
dmc_xfer *s = dmc_xfer_send(p, paths, 2);
|
||||
link2_skip(s, r);
|
||||
dmc_xfer_status ss, rs;
|
||||
dmc_xfer_get_status(s, &ss);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(!ss.result[0] && !rs.result[0], "%s skip: '%s' '%s'", what, ss.result,
|
||||
rs.result);
|
||||
snprintf(got, sizeof(got), "%s/data.bin", dst);
|
||||
FILE *f = fopen(got, "rb");
|
||||
CHECK(!f, "%s skip: the skipped file is there", what);
|
||||
if (f)
|
||||
fclose(f);
|
||||
snprintf(got, sizeof(got), "%s/hello.txt", dst);
|
||||
check_same(got, small, sizeof(small) - 1, 0, what);
|
||||
CHECK(rs.files == 1, "%s skip: %d files", what, rs.files);
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
/* ZMODEM crash recovery: a part kept when the transfer stops, and
|
||||
continued the next time */
|
||||
static void resume(void)
|
||||
{
|
||||
char src[600], dst[600], a[700], got[700];
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
snprintf(a, sizeof(a), "%s/data.bin", src);
|
||||
write_file(a, big, sizeof(big));
|
||||
snprintf(got, sizeof(got), "%s/data.bin", dst);
|
||||
const char *paths[1] = { a };
|
||||
dmc_xfer_options o;
|
||||
memset(&o, 0, sizeof(o));
|
||||
o.resume = 1;
|
||||
|
||||
/* a part there: the receiver's option continues it */
|
||||
write_file(got, big, 15000);
|
||||
dmc_xfer *r = dmc_xfer_receive_opt(DMC_XFER_ZMODEM, dst, NULL, &o);
|
||||
dmc_xfer *s = dmc_xfer_send(DMC_XFER_ZMODEM, paths, 1);
|
||||
long long t0 = xfer_now();
|
||||
link2(s, r, 0);
|
||||
(void)t0;
|
||||
dmc_xfer_status ss, rs;
|
||||
dmc_xfer_get_status(s, &ss);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(!ss.result[0] && !rs.result[0], "resume: '%s' '%s'", ss.result, rs.result);
|
||||
check_same(got, big, sizeof(big), 0, "resume (the receiver's option)");
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
|
||||
/* the same with the sender's ZCRECOV */
|
||||
write_file(got, big, 20000);
|
||||
r = dmc_xfer_receive(DMC_XFER_ZMODEM, dst, NULL);
|
||||
s = dmc_xfer_send_opt(DMC_XFER_ZMODEM, paths, 1, &o);
|
||||
link2(s, r, 0);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(!rs.result[0], "resume (ZCRECOV): '%s'", rs.result);
|
||||
check_same(got, big, sizeof(big), 0, "resume (the sender's ZCRECOV)");
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
|
||||
/* there whole: skipped, and not made again as "data (2).bin" */
|
||||
r = dmc_xfer_receive_opt(DMC_XFER_ZMODEM, dst, NULL, &o);
|
||||
s = dmc_xfer_send(DMC_XFER_ZMODEM, paths, 1);
|
||||
link2(s, r, 0);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(!rs.result[0], "resume (whole): '%s'", rs.result);
|
||||
char two[700];
|
||||
snprintf(two, sizeof(two), "%s/data (2).bin", dst);
|
||||
FILE *f = fopen(two, "rb");
|
||||
CHECK(!f, "resume (whole): a second copy was made");
|
||||
if (f)
|
||||
fclose(f);
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
|
||||
/* stopped in the middle: the part kept */
|
||||
remove(got);
|
||||
r = dmc_xfer_receive_opt(DMC_XFER_ZMODEM, dst, NULL, &o);
|
||||
s = dmc_xfer_send(DMC_XFER_ZMODEM, paths, 1);
|
||||
char buf[4096];
|
||||
for (int turns = 0; turns < 20000; turns++) {
|
||||
dmc_xfer_status st;
|
||||
dmc_xfer_get_status(r, &st);
|
||||
if (st.bytes > 10000)
|
||||
break;
|
||||
int n;
|
||||
if ((n = dmc_xfer_output(s, buf, 1024)) > 0)
|
||||
dmc_xfer_feed(r, buf, n);
|
||||
while ((n = dmc_xfer_output(r, buf, sizeof(buf))) > 0)
|
||||
dmc_xfer_feed(s, buf, n);
|
||||
xfer_sleep(1);
|
||||
}
|
||||
dmc_xfer_cancel(r);
|
||||
dmc_xfer_cancel(s);
|
||||
link2(s, r, 0);
|
||||
size_t n = 0;
|
||||
unsigned char *part = read_file(got, &n);
|
||||
CHECK(part && n > 0 && n < sizeof(big) && !memcmp(part, big, n),
|
||||
"resume: the part was not kept (%zu bytes)", n);
|
||||
free(part);
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* ZMODEM with zstd */
|
||||
|
||||
static int have(const char *prog);
|
||||
|
||||
/* n bytes of text that compresses, as a BBS's text files do */
|
||||
static unsigned char *text(size_t n, unsigned seed)
|
||||
{
|
||||
static const char *const words[] = {
|
||||
"modem", "carrier", "the", "BBS", "file", "area", "sysop",
|
||||
"message", "ZMODEM", "of", "and", "a", "upload", "download",
|
||||
"baud", "line", "\r\n"
|
||||
};
|
||||
unsigned char *d = (unsigned char *)malloc(n + 16);
|
||||
size_t at = 0;
|
||||
unsigned r = seed * 2654435761u + 1;
|
||||
while (at < n) {
|
||||
r = r * 1103515245u + 12345u;
|
||||
const char *w = words[(r >> 16) % (sizeof(words) / sizeof(words[0]))];
|
||||
size_t l = strlen(w);
|
||||
for (size_t i = 0; i < l && at < n; i++)
|
||||
d[at++] = (unsigned char)w[i];
|
||||
if (at < n)
|
||||
d[at++] = ' ';
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/* the codec: the specification's frame, there and back, against the
|
||||
reference zstd (where it is installed) */
|
||||
static void zstd_codec(void)
|
||||
{
|
||||
static const unsigned char vec[55] = {
|
||||
0x28, 0xb5, 0x2f, 0xfd, 0x24, 0x66, 0x55, 0x01, 0x00, 0x24, 0x02, 0x44, 0x2d, 0x4d,
|
||||
0x6f, 0x64, 0x65, 0x6d, 0x20, 0x5a, 0x4d, 0x4f, 0x44, 0x45, 0x4d, 0x20, 0x7a, 0x73,
|
||||
0x74, 0x64, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x76, 0x65, 0x63, 0x74, 0x6f, 0x72,
|
||||
0x2e, 0x0d, 0x0a, 0x01, 0x00, 0x8a, 0x82, 0xaa, 0x27, 0x32, 0x21, 0x69, 0x9d
|
||||
};
|
||||
const char *line = "D-Modem ZMODEM zstd test vector.\r\n";
|
||||
char want[103];
|
||||
snprintf(want, sizeof(want), "%s%s%s", line, line, line);
|
||||
int n;
|
||||
const char *why;
|
||||
unsigned char *d = zc_decode(vec, 55, 1024, &n, &why);
|
||||
CHECK(d && n == 102 && !memcmp(d, want, 102), "zstd: the vector: %s", why ? why : "differs");
|
||||
free(d);
|
||||
CHECK(zc_frame_len(vec, 55, 1024) == 55 && zc_frame_len(vec, 54, 1024) == 0,
|
||||
"zstd: the vector's length");
|
||||
unsigned char bad[55];
|
||||
memcpy(bad, vec, 55);
|
||||
bad[20] ^= 1;
|
||||
d = zc_decode(bad, 55, 1024, &n, &why);
|
||||
CHECK(!d, "zstd: a byte wrong, taken");
|
||||
free(d);
|
||||
d = zc_decode(vec, 55, 100, &n, &why);
|
||||
CHECK(!d, "zstd: over the limit, taken");
|
||||
free(d);
|
||||
CHECK(zc_xxh64(NULL, 0) == 0xEF46DB3751D8E999ULL &&
|
||||
zc_xxh64((const unsigned char *)"a", 1) == 0xD24EC4F1A98C6E5BULL &&
|
||||
zc_xxh64((const unsigned char *)"Nobody inspects the spammish repetition", 39) ==
|
||||
0xFBCEA83C8A378BF1ULL, "zstd: XXH64");
|
||||
|
||||
int ref = have("zstd");
|
||||
size_t sizes[] = { 0, 1, 255, 256, 65535, 65791, 65792, 200000 };
|
||||
for (size_t i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
|
||||
for (int kind = 0; kind < 3; kind++) {
|
||||
size_t sz = sizes[i];
|
||||
unsigned char *t = kind == 0 ? text(sz, (unsigned)i) : (unsigned char *)malloc(sz + 1);
|
||||
for (size_t k = 0; kind > 0 && k < sz; k++)
|
||||
t[k] = kind == 1 ? big[k % sizeof(big)] : (unsigned char)(k / 1000);
|
||||
int fn = 0, back = 0;
|
||||
unsigned char *f = zc_encode(t, (int)sz, &fn);
|
||||
d = f ? zc_decode(f, fn, 256 * 1024, &back, &why) : NULL;
|
||||
CHECK(d && (size_t)back == sz && !memcmp(d, t, sz),
|
||||
"zstd: %zu bytes (%d) there and back: %s", sz, kind, why ? why : "differs");
|
||||
CHECK(f && zc_frame_len(f, fn, 256 * 1024) == fn, "zstd: %zu bytes: its length", sz);
|
||||
if (kind == 0 && sz == 65535)
|
||||
CHECK(fn * 3 < (int)sz, "zstd: 64 KB of text in %d bytes", fn);
|
||||
/* the reference zstd: ours decoded, and its frame decoded */
|
||||
if (sz > 0 && ref && f) {
|
||||
char a[700], b[700], cmd[1500];
|
||||
snprintf(a, sizeof(a), "%s/z.raw", tmp);
|
||||
snprintf(b, sizeof(b), "%s/z.zst", tmp);
|
||||
write_file(b, f, (size_t)fn);
|
||||
snprintf(cmd, sizeof(cmd), "zstd -q -d -f -o '%s' '%s'", a, b);
|
||||
CHECK(system(cmd) == 0, "zstd -d: our frame of %zu bytes", sz);
|
||||
check_same(a, t, sz, 0, "zstd -d");
|
||||
write_file(a, t, sz);
|
||||
snprintf(cmd, sizeof(cmd), "zstd -q -19 --check -f -o '%s' '%s'", b, a);
|
||||
CHECK(system(cmd) == 0, "zstd -19: %zu bytes", sz);
|
||||
size_t rn;
|
||||
unsigned char *r = read_file(b, &rn);
|
||||
unsigned char *rd = r ? zc_decode(r, (int)rn, 1 << 23, &back, &why) : NULL;
|
||||
CHECK(rd && (size_t)back == sz && !memcmp(rd, t, sz),
|
||||
"zstd: the reference's frame of %zu bytes: %s", sz, why ? why : "differs");
|
||||
free(r);
|
||||
free(rd);
|
||||
}
|
||||
free(d);
|
||||
free(f);
|
||||
free(t);
|
||||
}
|
||||
}
|
||||
if (ref)
|
||||
printf("zstd: against the reference zstd\n");
|
||||
}
|
||||
|
||||
/* one ZMODEM transfer of the files (paths) to dst with the two sides'
|
||||
options: both ends' results checked */
|
||||
static void zstd_link(const char *const *paths, int n, const char *dst,
|
||||
const dmc_xfer_options *so, const dmc_xfer_options *ro,
|
||||
int noise, const char *what)
|
||||
{
|
||||
dmc_xfer *r = dmc_xfer_receive_opt(DMC_XFER_ZMODEM, dst, NULL, ro);
|
||||
dmc_xfer *s = dmc_xfer_send_opt(DMC_XFER_ZMODEM, paths, n, so);
|
||||
link2(s, r, noise);
|
||||
dmc_xfer_status ss, rs;
|
||||
dmc_xfer_get_status(s, &ss);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(!ss.result[0], "%s: the sender: %s", what, ss.result);
|
||||
CHECK(!rs.result[0], "%s: the receiver: %s", what, rs.result);
|
||||
if (noise)
|
||||
CHECK(rs.errors + ss.errors > 0, "%s: no errors on a noisy line", what);
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
/* ZMODEM with zstd: smaller on the line, whole after errors, resumed,
|
||||
plain when one side doesn't take it or the file is compressed */
|
||||
static void zstd_transfers(void)
|
||||
{
|
||||
char src[600], dst[600], p[5][700], got[700];
|
||||
const char *paths[5];
|
||||
size_t sizes[5] = { 0, 1, 65536, 65537, 300001 };
|
||||
unsigned char *d[5];
|
||||
long long total = 0;
|
||||
dmc_xfer_options on, off;
|
||||
memset(&on, 0, sizeof(on));
|
||||
memset(&off, 0, sizeof(off));
|
||||
off.no_zstd = 1;
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
d[i] = text(sizes[i], (unsigned)i + 1);
|
||||
snprintf(p[i], sizeof(p[i]), "%s/f%d.txt", src, i);
|
||||
write_file(p[i], d[i], sizes[i]);
|
||||
paths[i] = p[i];
|
||||
total += (long long)sizes[i];
|
||||
}
|
||||
zstd_link(paths, 5, dst, &on, &on, 0, "zstd");
|
||||
for (int i = 0; i < 5; i++) {
|
||||
snprintf(got, sizeof(got), "%s/f%d.txt", dst, i);
|
||||
check_same(got, d[i], sizes[i], 0, "zstd");
|
||||
}
|
||||
CHECK(line_bytes * 2 < total, "zstd: %lld bytes on the line for %lld", line_bytes, total);
|
||||
printf("ZMODEM with zstd: %lld bytes on the line for %lld\n", line_bytes, total);
|
||||
|
||||
/* on a noisy line */
|
||||
empty_dir(dst);
|
||||
zstd_link(paths + 4, 1, dst, &on, &on, 3001, "zstd (noisy)");
|
||||
snprintf(got, sizeof(got), "%s/f4.txt", dst);
|
||||
check_same(got, d[4], sizes[4], 0, "zstd (noisy)");
|
||||
|
||||
/* zstd stopped on either side, or a compressed file: plain */
|
||||
for (int k = 0; k < 3; k++) {
|
||||
char zip[700];
|
||||
const char *one[1] = { paths[4] };
|
||||
empty_dir(dst);
|
||||
if (k == 2) {
|
||||
snprintf(zip, sizeof(zip), "%s/f4.zip", src);
|
||||
write_file(zip, d[4], sizes[4]);
|
||||
one[0] = zip;
|
||||
}
|
||||
zstd_link(one, 1, dst, k == 0 ? &off : &on, k == 1 ? &off : &on, 0, "zstd off");
|
||||
snprintf(got, sizeof(got), "%s/f4.%s", dst, k == 2 ? "zip" : "txt");
|
||||
check_same(got, d[4], sizes[4], 0, "zstd off");
|
||||
CHECK(line_bytes >= (long long)sizes[4],
|
||||
"zstd off (%d): %lld bytes on the line: compressed", k, line_bytes);
|
||||
}
|
||||
|
||||
/* crash recovery with zstd: from the end of the part (fewer bytes on
|
||||
the line than for the whole file) */
|
||||
dmc_xfer_options res = on;
|
||||
res.resume = 1;
|
||||
empty_dir(dst);
|
||||
zstd_link(paths + 4, 1, dst, &on, &on, 0, "zstd (whole)");
|
||||
long long whole = line_bytes;
|
||||
empty_dir(dst);
|
||||
snprintf(got, sizeof(got), "%s/f4.txt", dst);
|
||||
write_file(got, d[4], 123457);
|
||||
zstd_link(paths + 4, 1, dst, &on, &res, 0, "zstd (resume)");
|
||||
check_same(got, d[4], sizes[4], 0, "zstd (resume)");
|
||||
CHECK(line_bytes * 4 < whole * 3,
|
||||
"zstd (resume): %lld bytes on the line, %lld for all: not resumed", line_bytes, whole);
|
||||
for (int i = 0; i < 5; i++)
|
||||
free(d[i]);
|
||||
}
|
||||
|
||||
/* plain text with the waits of ASCII Setup */
|
||||
static void text_delays(void)
|
||||
{
|
||||
char src[600], dst[600], a[700], got[700];
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
static const unsigned char text[] = "one\r\ntwo\r\nthree\rfour\n";
|
||||
snprintf(a, sizeof(a), "%s/t.txt", src);
|
||||
write_file(a, text, sizeof(text) - 1);
|
||||
const char *paths[1] = { a };
|
||||
dmc_xfer_options o;
|
||||
memset(&o, 0, sizeof(o));
|
||||
o.line_delay_ms = 100;
|
||||
o.char_delay_ms = 5;
|
||||
xfer_text_quiet_ms = 600;
|
||||
dmc_xfer *r = dmc_xfer_receive(DMC_XFER_TEXT, dst, "t.txt");
|
||||
long long t0 = xfer_now();
|
||||
dmc_xfer *s = dmc_xfer_send_opt(DMC_XFER_TEXT, paths, 1, &o);
|
||||
for (;;) {
|
||||
dmc_xfer_status st;
|
||||
dmc_xfer_get_status(s, &st);
|
||||
char buf[256];
|
||||
int n;
|
||||
while ((n = dmc_xfer_output(s, buf, sizeof(buf))) > 0)
|
||||
dmc_xfer_feed(r, buf, n);
|
||||
if (!st.running)
|
||||
break;
|
||||
xfer_sleep(1);
|
||||
}
|
||||
long long took = xfer_now() - t0;
|
||||
/* 4 line ends and 21 characters */
|
||||
CHECK(took >= 4 * 100 + 21 * 5, "text delays: %lld ms", took);
|
||||
link2(s, r, 0);
|
||||
snprintf(got, sizeof(got), "%s/t.txt", dst);
|
||||
check_same(got, text, sizeof(text) - 1, 0, "text delays");
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
static void hex_both_ways(int p)
|
||||
{
|
||||
char src[600], dst[600], a[700], got[700];
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
/* (70000 bytes: past 64 KB, an extended address, S2 records) */
|
||||
static unsigned char img[70000];
|
||||
for (size_t i = 0; i < sizeof(img); i++)
|
||||
img[i] = (unsigned char)(i * 7 + 3);
|
||||
for (int as_records = 0; as_records < 2; as_records++) {
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
snprintf(a, sizeof(a), "%s/image.bin", src);
|
||||
write_file(a, img, sizeof(img));
|
||||
if (as_records) {
|
||||
/* the records already: sent as they are */
|
||||
char d2[600], rec[700];
|
||||
snprintf(d2, sizeof(d2), "%s/rec", tmp);
|
||||
empty_dir(d2);
|
||||
const char *pa[1] = { a };
|
||||
dmc_xfer *r = dmc_xfer_receive(DMC_XFER_TEXT, d2, "image.hex");
|
||||
dmc_xfer *s = dmc_xfer_send(p, pa, 1);
|
||||
xfer_text_quiet_ms = 300;
|
||||
link2(s, r, 0);
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
snprintf(rec, sizeof(rec), "%s/image.hex", d2);
|
||||
snprintf(a, sizeof(a), "%s", rec);
|
||||
}
|
||||
const char *paths[1] = { a };
|
||||
dmc_xfer *r = dmc_xfer_receive(p, dst, "image.bin");
|
||||
dmc_xfer *s = dmc_xfer_send(p, paths, 1);
|
||||
link2(s, r, 0);
|
||||
dmc_xfer_status rs;
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(!rs.result[0], "%s: %s", dmc_xfer_label(p), rs.result);
|
||||
snprintf(got, sizeof(got), "%s/image.bin", dst);
|
||||
check_same(got, img, sizeof(img), 0, dmc_xfer_label(p));
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
}
|
||||
|
||||
static void text_both_ways(void)
|
||||
{
|
||||
char src[600], dst[600], a[700], got[700];
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
snprintf(a, sizeof(a), "%s/note.txt", src);
|
||||
write_file(a, big, 5000);
|
||||
xfer_text_quiet_ms = 300;
|
||||
const char *paths[1] = { a };
|
||||
dmc_xfer *r = dmc_xfer_receive(DMC_XFER_TEXT, dst, "note.txt");
|
||||
dmc_xfer *s = dmc_xfer_send(DMC_XFER_TEXT, paths, 1);
|
||||
link2(s, r, 0);
|
||||
snprintf(got, sizeof(got), "%s/note.txt", dst);
|
||||
check_same(got, big, 5000, 0, "plain text");
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
|
||||
/* a cancel keeps what came */
|
||||
empty_dir(dst);
|
||||
xfer_text_quiet_ms = 60000;
|
||||
r = dmc_xfer_receive(DMC_XFER_TEXT, dst, "kept.txt");
|
||||
dmc_xfer_feed(r, "some text", 9);
|
||||
xfer_sleep(200);
|
||||
dmc_xfer_cancel(r);
|
||||
dmc_xfer_status rs;
|
||||
do {
|
||||
xfer_sleep(10);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
} while (rs.running);
|
||||
CHECK(!rs.result[0], "plain text's cancel: %s", rs.result);
|
||||
snprintf(got, sizeof(got), "%s/kept.txt", dst);
|
||||
check_same(got, (const unsigned char *)"some text", 9, 0, "plain text's cancel");
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
static void cancel(void)
|
||||
{
|
||||
char dst[600];
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(dst);
|
||||
dmc_xfer *r = dmc_xfer_receive(DMC_XFER_ZMODEM, dst, NULL);
|
||||
xfer_sleep(100);
|
||||
dmc_xfer_cancel(r);
|
||||
char buf[256];
|
||||
int n, cans = 0;
|
||||
dmc_xfer_status rs;
|
||||
do {
|
||||
xfer_sleep(10);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
while ((n = dmc_xfer_output(r, buf, sizeof(buf))) > 0)
|
||||
for (int i = 0; i < n; i++)
|
||||
cans += buf[i] == 0x18;
|
||||
} while (rs.running);
|
||||
CHECK(!strcmp(rs.result, "cancelled"), "cancel: %s", rs.result);
|
||||
CHECK(cans >= 5, "cancel: %d CANs sent", cans);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
static void detect(void)
|
||||
{
|
||||
int at = -1;
|
||||
const char sz[] = "rz waiting\r\n**\x18" "B00000000000000\r\x8a\x11";
|
||||
CHECK(dmc_xfer_detect(sz, (int)sizeof(sz) - 1, &at) ==
|
||||
DMC_XFER_DETECT_ZMODEM_SEND && at == 12, "sz %d", at);
|
||||
const char rz[] = "**\x18" "B0100000023be50\r\x8a\x11";
|
||||
CHECK(dmc_xfer_detect(rz, (int)sizeof(rz) - 1, &at) ==
|
||||
DMC_XFER_DETECT_ZMODEM_RECEIVE && at == 0, "rz");
|
||||
const char k[] = "x\x01" "1 S~/ @-#Y3~^>J)0___F\r";
|
||||
CHECK(dmc_xfer_detect(k, (int)sizeof(k) - 1, &at) ==
|
||||
DMC_XFER_DETECT_KERMIT_SEND && at == 1, "kermit");
|
||||
CHECK(dmc_xfer_detect("hello", 5, &at) == DMC_XFER_DETECT_NONE, "none");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* lrzsz and C-Kermit, on a pseudo-terminal */
|
||||
|
||||
static int have(const char *prog)
|
||||
{
|
||||
char cmd[256];
|
||||
snprintf(cmd, sizeof(cmd), "command -v %s >/dev/null 2>&1", prog);
|
||||
return system(cmd) == 0;
|
||||
}
|
||||
|
||||
/* cmd run in dir on a pseudo-terminal (raw), the transfer on its other
|
||||
side: the command's exit status */
|
||||
static int against(dmc_xfer *x, const char *dir, const char *cmd)
|
||||
{
|
||||
int m = posix_openpt(O_RDWR | O_NOCTTY);
|
||||
if (m < 0 || grantpt(m) < 0 || unlockpt(m) < 0)
|
||||
return -1;
|
||||
const char *sname = ptsname(m);
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
setsid();
|
||||
int s = open(sname, O_RDWR);
|
||||
char raw[700];
|
||||
snprintf(raw, sizeof(raw), "stty raw -echo -ixon -ixoff <&%d; cd '%s' && exec %s",
|
||||
s, dir, cmd);
|
||||
dup2(s, 0);
|
||||
dup2(s, 1);
|
||||
int null = open("/dev/null", O_WRONLY);
|
||||
dup2(null, 2);
|
||||
execl("/bin/sh", "sh", "-c", raw, (char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
fcntl(m, F_SETFL, O_NONBLOCK);
|
||||
char buf[4096];
|
||||
long long end = xfer_now() + 120000;
|
||||
int status = -1, done = 0;
|
||||
for (;;) {
|
||||
int moved = 0;
|
||||
ssize_t n = read(m, buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
dmc_xfer_feed(x, buf, (int)n);
|
||||
moved = 1;
|
||||
}
|
||||
int k;
|
||||
while ((k = dmc_xfer_output(x, buf, sizeof(buf))) > 0) {
|
||||
int off = 0;
|
||||
while (off < k) {
|
||||
ssize_t w = write(m, buf + off, (size_t)(k - off));
|
||||
if (w > 0)
|
||||
off += (int)w;
|
||||
else if (errno == EAGAIN)
|
||||
xfer_sleep(1);
|
||||
else
|
||||
break;
|
||||
}
|
||||
moved = 1;
|
||||
}
|
||||
if (!done && waitpid(pid, &status, WNOHANG) == pid)
|
||||
done = 1;
|
||||
dmc_xfer_status st;
|
||||
dmc_xfer_get_status(x, &st);
|
||||
if (!st.running && done)
|
||||
break;
|
||||
if (xfer_now() > end) {
|
||||
kill(pid, 9);
|
||||
dmc_xfer_cancel(x);
|
||||
if (!done)
|
||||
waitpid(pid, &status, 0);
|
||||
done = 1;
|
||||
}
|
||||
if (!moved)
|
||||
xfer_sleep(2);
|
||||
}
|
||||
close(m);
|
||||
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
|
||||
}
|
||||
|
||||
static void interop_one(int p, const char *send_cmd, const char *recv_cmd, int prefix)
|
||||
{
|
||||
char src[600], dst[600], a[700], b[700], got[700], cmd[1400];
|
||||
const char *what = dmc_xfer_label(p);
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
int batch = dmc_xfer_batch(p);
|
||||
|
||||
/* theirs sends, ours receives */
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
snprintf(a, sizeof(a), "%s/data.bin", src);
|
||||
snprintf(b, sizeof(b), "%s/hello.txt", src);
|
||||
write_file(a, big, sizeof(big));
|
||||
write_file(b, small, sizeof(small) - 1);
|
||||
snprintf(cmd, sizeof(cmd), "%s data.bin%s", send_cmd, batch ? " hello.txt" : "");
|
||||
dmc_xfer *r = dmc_xfer_receive(p, dst, "data.bin");
|
||||
int rc = against(r, src, cmd);
|
||||
dmc_xfer_status st;
|
||||
dmc_xfer_get_status(r, &st);
|
||||
CHECK(!st.result[0], "%s from %s: %s", what, send_cmd, st.result);
|
||||
CHECK(rc == 0, "%s: %s exited %d", what, send_cmd, rc);
|
||||
snprintf(got, sizeof(got), "%s/data.bin", dst);
|
||||
check_same(got, big, sizeof(big), prefix, what);
|
||||
if (batch) {
|
||||
snprintf(got, sizeof(got), "%s/hello.txt", dst);
|
||||
check_same(got, small, sizeof(small) - 1, 0, what);
|
||||
CHECK(mtime_of(got) == MTIME, "%s from %s: the time %lld", what,
|
||||
send_cmd, mtime_of(got));
|
||||
}
|
||||
dmc_xfer_free(r);
|
||||
|
||||
/* ours sends, theirs receives */
|
||||
empty_dir(dst);
|
||||
const char *paths[2] = { a, b };
|
||||
dmc_xfer *s = dmc_xfer_send(p, paths, 2);
|
||||
rc = against(s, dst, recv_cmd);
|
||||
dmc_xfer_get_status(s, &st);
|
||||
CHECK(!st.result[0], "%s to %s: %s", what, recv_cmd, st.result);
|
||||
CHECK(rc == 0, "%s: %s exited %d", what, recv_cmd, rc);
|
||||
snprintf(got, sizeof(got), "%s/data.bin", dst);
|
||||
check_same(got, big, sizeof(big), prefix, what);
|
||||
if (batch) {
|
||||
snprintf(got, sizeof(got), "%s/hello.txt", dst);
|
||||
check_same(got, small, sizeof(small) - 1, 0, what);
|
||||
CHECK(mtime_of(got) == MTIME, "%s to %s: the time %lld", what,
|
||||
recv_cmd, mtime_of(got));
|
||||
}
|
||||
dmc_xfer_free(s);
|
||||
}
|
||||
|
||||
static void interop(void)
|
||||
{
|
||||
const char *want = getenv("DMODEM_XFER_INTEROP");
|
||||
int need = want && !strcmp(want, "1");
|
||||
if (have("sz") && have("rz")) {
|
||||
interop_one(DMC_XFER_ZMODEM, "sz -b", "rz -b", 0);
|
||||
interop_one(DMC_XFER_YMODEM, "sz --ymodem -b", "rz --ymodem -b", 0);
|
||||
/* (sb streams when asked with G; rb asks with C: plain YMODEM) */
|
||||
interop_one(DMC_XFER_YMODEM_G, "sz --ymodem -b", "rz --ymodem -b", 0);
|
||||
interop_one(DMC_XFER_XMODEM, "sz --xmodem -b", "rz --xmodem -b data.bin", 1);
|
||||
interop_one(DMC_XFER_XMODEM_1K, "sz --xmodem -k -b", "rz --xmodem -b data.bin", 1);
|
||||
printf("interop: lrzsz\n");
|
||||
} else {
|
||||
CHECK(!need, "sz and rz are not installed");
|
||||
}
|
||||
if (have("kermit")) {
|
||||
interop_one(DMC_XFER_KERMIT, "kermit -Y -i -s", "kermit -Y -i -r", 0);
|
||||
printf("interop: C-Kermit\n");
|
||||
} else {
|
||||
CHECK(!need, "kermit is not installed");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
snprintf(tmp, sizeof(tmp), "%s/dmodem-xfer-XXXXXX",
|
||||
getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
|
||||
if (!mkdtemp(tmp)) {
|
||||
perror("mkdtemp");
|
||||
return 1;
|
||||
}
|
||||
srand(7);
|
||||
for (size_t i = 0; i < sizeof(big); i++)
|
||||
big[i] = (unsigned char)rand();
|
||||
int protocols[] = { DMC_XFER_ZMODEM, DMC_XFER_YMODEM, DMC_XFER_XMODEM,
|
||||
DMC_XFER_XMODEM_1K, DMC_XFER_KERMIT };
|
||||
both_ways(DMC_XFER_YMODEM_G, 0);
|
||||
ymodem_g();
|
||||
printf("YMODEM-G\n");
|
||||
for (size_t i = 0; i < sizeof(protocols) / sizeof(protocols[0]); i++) {
|
||||
both_ways(protocols[i], 0);
|
||||
printf("%s: clean line\n", dmc_xfer_label(protocols[i]));
|
||||
/* (Kermit's long packets: most would be damaged at 1 in 3001) */
|
||||
both_ways(protocols[i], protocols[i] == DMC_XFER_KERMIT ? 7001 : 3001);
|
||||
printf("%s: noisy line\n", dmc_xfer_label(protocols[i]));
|
||||
}
|
||||
hex_both_ways(DMC_XFER_INTEL_HEX);
|
||||
hex_both_ways(DMC_XFER_SREC);
|
||||
printf("hex records\n");
|
||||
text_both_ways();
|
||||
text_delays();
|
||||
printf("plain text\n");
|
||||
skip_file(DMC_XFER_ZMODEM);
|
||||
skip_file(DMC_XFER_KERMIT);
|
||||
printf("skip file\n");
|
||||
resume();
|
||||
printf("ZMODEM crash recovery\n");
|
||||
zstd_codec();
|
||||
printf("zstd\n");
|
||||
zstd_transfers();
|
||||
cancel();
|
||||
detect();
|
||||
interop();
|
||||
char cmd[600];
|
||||
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", tmp);
|
||||
if (system(cmd) != 0)
|
||||
failures++;
|
||||
if (failures)
|
||||
fprintf(stderr, "%d failures\n", failures);
|
||||
else
|
||||
printf("all passed\n");
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
# Documentation style: ASD-STE100 Simplified Technical English
|
||||
|
||||
All technical documentation in the D-Modem project uses ASD-STE100
|
||||
Simplified Technical English (STE). This guide gives the rules that we use.
|
||||
It applies to READMEs, the `docs/` folders, wikis, specifications, design
|
||||
documents, protocol write-ups, CONTRIBUTING files (except legal text) and
|
||||
technical notes in the project files.
|
||||
|
||||
The official source is the ASD-STE100 specification (Issue 9) and its
|
||||
dictionary. If this guide and the specification are different, use the
|
||||
specification. You can get the specification free of charge from
|
||||
<https://www.asd-ste100.org>.
|
||||
|
||||
## 1. What STE does not change
|
||||
|
||||
Do not change these items. Copy them exactly:
|
||||
|
||||
- Code, commands, file names, paths, URLs, identifiers and configuration keys.
|
||||
- AT commands, S-registers, result codes and protocol message names.
|
||||
- Numbers, units, addresses, offsets and register values.
|
||||
- Titles of standards (for example, ITU-T V.92) and quotations from them.
|
||||
- Product names, company names and the names of people.
|
||||
- Legal text: licenses, the contributor agreement, copyright and trademark
|
||||
notices. Legal text keeps its legal wording.
|
||||
- Text that we copied from a third party (for example, `ORIGINAL-readme.txt`
|
||||
files, firmware release notes, test fixtures).
|
||||
|
||||
## 2. Words
|
||||
|
||||
1. Use approved words from the STE dictionary, with their approved meaning
|
||||
only. For example, "close" means "to move to the closed position". It
|
||||
does not mean "near".
|
||||
2. Use technical names (Category: technical names). For example: modem,
|
||||
handshake, carrier, codec, constellation, packet, socket, thread,
|
||||
buffer, firmware, sample rate, call, line, PBX, trunk, server, client.
|
||||
3. Use technical verbs (Category: technical verbs) for processes that STE
|
||||
does not cover. This project uses these technical verbs: build, compile,
|
||||
link, configure, install, uninstall, boot, flash, download, upload, dial,
|
||||
log in, log out, debug, decode, encode, modulate, demodulate, train,
|
||||
retrain, negotiate, sign, encrypt, decrypt, compress, decompress, parse,
|
||||
render, cache, merge, commit, push, clone, emulate, probe.
|
||||
4. Use one word for one thing. Do not change between synonyms. For example,
|
||||
if you write "softmodem", do not write "software modem" in the next
|
||||
paragraph.
|
||||
5. Use these verb forms only: infinitive, imperative, simple present, simple
|
||||
past, simple future, and the past participle as an adjective.
|
||||
6. Do not use "-ing" forms of verbs, except in technical names (for example,
|
||||
"ring signal", "echo canceller"). Write "the modem starts to train", not
|
||||
"the modem is training".
|
||||
7. Do not use phrasal verbs. Write "start", not "start up". Write "find",
|
||||
not "find out".
|
||||
8. Do not make noun clusters of more than three words. Use prepositions to
|
||||
break up longer clusters. Write "the receive buffer of the V.34 data
|
||||
pump", not "the V.34 data pump receive buffer".
|
||||
9. Use articles ("the", "a") and demonstrative adjectives ("this", "these")
|
||||
before nouns where possible.
|
||||
10. Do not use slang, idioms, contractions ("can't", "it's") or Latin
|
||||
abbreviations ("e.g.", "i.e.", "etc."). Write "for example", "that is",
|
||||
"and other items".
|
||||
|
||||
### Common replacements
|
||||
|
||||
| Do not write | Write |
|
||||
|---|---|
|
||||
| utilize, make use of | use |
|
||||
| ensure, make certain | make sure |
|
||||
| require, need to | is necessary, must |
|
||||
| should | must (a rule) or write what occurs (a fact) |
|
||||
| allow, enable (a person) | let, make it possible |
|
||||
| approximately, roughly | about |
|
||||
| perform, carry out | do |
|
||||
| obtain, fetch, grab | get |
|
||||
| prior to | before |
|
||||
| in order to | to |
|
||||
| via | through |
|
||||
| currently | now |
|
||||
| additional | more |
|
||||
| e.g., i.e., etc. | for example, that is, and other items |
|
||||
| turn on / turn off (a setting) | enable / disable (technical verbs for software settings) |
|
||||
| set up (verb) | configure, install |
|
||||
| figure out, find out | find, calculate |
|
||||
| it's, can't, don't | it is, cannot, do not |
|
||||
|
||||
## 3. Sentences
|
||||
|
||||
1. Write one topic in one sentence.
|
||||
2. Procedural sentences (instructions): 20 words or fewer.
|
||||
3. Descriptive sentences: 25 words or fewer.
|
||||
4. Use the active voice. In descriptive text, you can use the passive voice
|
||||
only when you do not know who or what does the action.
|
||||
5. Do not use semicolons to join sentences. Write two sentences.
|
||||
6. Use a colon before a list. Use a vertical list when there are more than
|
||||
two items.
|
||||
7. Use connecting words ("then", "but", "if", "because") to show how two
|
||||
sentences are related.
|
||||
|
||||
## 4. Procedures
|
||||
|
||||
1. Write instructions in the imperative ("Install the package").
|
||||
2. Write one instruction in one sentence. You can write two actions in one
|
||||
sentence only when the reader does them at the same time.
|
||||
3. Write each step as a numbered item.
|
||||
4. If a condition applies, write the condition first: "If the build fails,
|
||||
delete the `build/` folder."
|
||||
5. Put a warning or a caution before the step that it applies to. Do not put
|
||||
it at the end of the procedure.
|
||||
6. Write the result of a step in a separate sentence after the step, if the
|
||||
reader must know it.
|
||||
|
||||
## 5. Descriptive writing
|
||||
|
||||
1. Give the most important information first.
|
||||
2. Write one topic in one paragraph.
|
||||
3. Do not write more than six sentences in a paragraph.
|
||||
4. Use headings to divide long text into short sections.
|
||||
5. Use tables for data that has rows and columns.
|
||||
|
||||
## 6. Warnings and cautions
|
||||
|
||||
Use a warning for a risk to persons. Use a caution for a risk to equipment,
|
||||
data or the telephone line. Start with a short title, then the instruction,
|
||||
then the risk:
|
||||
|
||||
> **CAUTION: DO NOT CONNECT THE BOARD TO A LIVE LINE WITHOUT THE ISOLATION
|
||||
> TRANSFORMER.** The line voltage can cause damage to the codec.
|
||||
|
||||
## 7. Examples
|
||||
|
||||
| Before | After (STE) |
|
||||
|---|---|
|
||||
| You'll need to make sure the PBX is set up before you dial out. | Configure the PBX before you dial. |
|
||||
| The handshake is performed by the answering modem, which then starts sending ANSam. | The answering modem starts the handshake. Then it sends ANSam. |
|
||||
| Turn it off if it's causing trouble. | If this feature causes problems, disable it. |
|
||||
| `crypto/tls` can't take such a hello, and it can't be rewritten for it. | `crypto/tls` cannot accept this type of hello. Also, you cannot change `crypto/tls` to accept it. |
|
||||
|
||||
## 8. Check before you commit
|
||||
|
||||
- Are all sentences short (20 words for instructions, 25 for descriptions)?
|
||||
- Is each instruction in the imperative, with one action?
|
||||
- Is there a word with "-ing", a phrasal verb or a contraction?
|
||||
- Are code, commands and identifiers exactly the same as before?
|
||||
- Did you keep legal text and third-party text unchanged?
|
||||
@@ -0,0 +1,124 @@
|
||||
# Building
|
||||
|
||||
The app is one Win32 app (`windows/`). It builds for Windows, and for
|
||||
Linux and macOS over the portable Win32 layer of clang-retro MFC
|
||||
(`winport/`). The submodule `external/D-Modem` gives the built-in modem.
|
||||
Refer to [The built-in modem](embedded-modem.md).
|
||||
|
||||
## Windows
|
||||
|
||||
We build the app for Windows with clang-retro. Get its toolchain
|
||||
package (`clang-dos-linux-x86_64.tar.xz` or `clang-dos-windows-x86_64.zip`,
|
||||
from the [nightly release](https://github.com/clang-retro/dos-sdk/releases/tag/nightly)).
|
||||
Then set `CLANG_DOS` to the directory of the package. Each build needs the
|
||||
submodule (`git submodule update --init`) for the built-in modem:
|
||||
|
||||
# Windows 95, 98, ME, NT 4.0 and later (no C runtime DLL), with the built-in modem
|
||||
cmake -S . -B build/win9x -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCLANG_DOS=/opt/clang-dos -DCMAKE_TOOLCHAIN_FILE=cmake/clang-retro.cmake
|
||||
cmake --build build/win9x
|
||||
|
||||
# Windows 98 SE, ME, 2000 and later, with the built-in modem
|
||||
cmake -S . -B build/win98 -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCLANG_DOS=/opt/clang-dos -DCMAKE_TOOLCHAIN_FILE=cmake/clang-retro.cmake \
|
||||
-DCLANG_RETRO_CONFIG=win98
|
||||
cmake --build build/win98
|
||||
|
||||
# Windows 7 and later, with the built-in modem
|
||||
cmake -S . -B build/win7 -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCLANG_DOS=/opt/clang-dos -DCMAKE_TOOLCHAIN_FILE=cmake/clang-retro.cmake \
|
||||
-DCLANG_RETRO_CONFIG=win7
|
||||
cmake --build build/win7
|
||||
|
||||
The app is in `windows/dmodem.exe` of the build directory. MSVC
|
||||
(`cmake -S . -B build -A Win32`) and MinGW also build the same code.
|
||||
|
||||
## Linux and macOS
|
||||
|
||||
Get [clang-retro MFC](https://github.com/clang-retro/mfc),
|
||||
clang and `llvm-rc` (on macOS, `brew install llvm`). For the built-in
|
||||
modem, get the D-Modem submodule (`git submodule update --init`). On
|
||||
Linux, also install `libusb-1.0-0-dev` and `libasound2-dev`. Then:
|
||||
|
||||
cmake -S . -B build -DDMODEM_GUI_WINPORT=ON -DCLANG_RETRO_MFC=/path/to/mfc \
|
||||
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
|
||||
cmake --build build --target dmodem-win32 && ./build/winport/dmodem-win32
|
||||
|
||||
On Linux, `cmake --install build` installs the app, its desktop entry,
|
||||
its icon and the firmware of the MultiTech MT9234MU (`firmware/`).
|
||||
|
||||
On macOS, the app is for the architecture of the Mac. To build the app
|
||||
for an Intel Mac on a Mac with Apple silicon, add
|
||||
`-DCMAKE_OSX_ARCHITECTURES=x86_64` to the first command. Set one
|
||||
architecture only. The app cannot be a universal binary.
|
||||
|
||||
## Haiku
|
||||
|
||||
You build the app for Haiku (x86_64) on Linux, with clang and a sysroot
|
||||
of Haiku's packages. Refer to [The app on Haiku](haiku.md#build).
|
||||
|
||||
## Mac OS X 10.4 and later (PowerPC and Intel)
|
||||
|
||||
The app for Mac OS X 10.4 (Tiger) and later builds on Linux. It uses the
|
||||
Mac OS X 10.0-10.5 port of clang-retro MFC and the toolchain file
|
||||
`cmake/macosx-legacy.cmake`. You need these items:
|
||||
|
||||
- clang of [clang-retro/llvm](https://github.com/clang-retro/llvm) (its
|
||||
`llvm-prebuilt` release). The clang of the clang-dos package can be too
|
||||
old: it must have emulated TLS for Mac OS X 10.4.
|
||||
- The `macosx10.4` sysroot of [clang-retro/dos-sdk](https://github.com/clang-retro/dos-sdk):
|
||||
`sh packaging/get-toolchain.sh -s macosx10.4 DIR`. It has libc++ and
|
||||
the builtins, and the script also gets Apple's `MacOSX10.4u.sdk`.
|
||||
Apple's license applies to the SDK.
|
||||
- Apple's `ld64` and `ar` for PowerPC: `PREFIX=DIR sh scripts/build-cctools-ppc.sh`
|
||||
in dos-sdk. This `ld64` also links for i386.
|
||||
|
||||
Build the app one time for each processor (`powerpc` and `i386`):
|
||||
|
||||
export MACOSX_LEGACY_LLVM=/path/to/llvm MACOSX_LEGACY_SYSROOT=/path/to/macosx10.4 \
|
||||
MACOSX_LEGACY_CCTOOLS=/path/to/cctools-ppc
|
||||
cmake -S . -B build/powerpc -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_TOOLCHAIN_FILE=$PWD/cmake/macosx-legacy.cmake \
|
||||
-DMACOSX_LEGACY_ARCH=powerpc -DCLANG_RETRO_MFC=/path/to/mfc -DDMODEM_GUI_WINPORT=ON
|
||||
cmake --build build/powerpc --target dmodem-win32
|
||||
|
||||
Then join the two programs into one fat program:
|
||||
|
||||
$MACOSX_LEGACY_CCTOOLS/bin/powerpc-apple-darwin8-lipo -create \
|
||||
build/powerpc/winport/dmodem-win32 build/i386/winport/dmodem-win32 -output dmodem-win32
|
||||
|
||||
`cmake/macosx-legacy/` has the fixes for headers of the 10.4 SDK
|
||||
(`time.h`, `signal.h` and `net/if.h`). The built-in modem needs D-Modem
|
||||
with the changes for 10.4 to 10.6 (`docs/macos-details.md` in D-Modem).
|
||||
|
||||
## The library only
|
||||
|
||||
With no `DMODEM_GUI_WINPORT`, a build on Linux or macOS makes only
|
||||
libdmodemctl, libdmodemssh and their tests. You do not need clang-retro
|
||||
MFC for this build:
|
||||
|
||||
cmake -S . -B build && cmake --build build && ctest --test-dir build
|
||||
|
||||
## Notes
|
||||
|
||||
The first CMake build downloads libssh and mbedTLS for SSH. This always
|
||||
occurs on Windows and macOS. Use `-DDMODEM_GUI_SSH=OFF` for a build with
|
||||
no SSH. On Linux, it occurs if the libssh of the system
|
||||
is older than 0.11.
|
||||
|
||||
The Settings page needs a dmodemd with AT!M (the main branch of D-Modem
|
||||
since D-Modem/D-Modem#103). The other pages operate with all versions of
|
||||
D-Modem 1.0.
|
||||
|
||||
## Updates from D-Modem
|
||||
|
||||
The workflow `.github/workflows/d-modem-bump.yml` keeps the submodule
|
||||
`external/D-Modem` on the main branch of D-Modem. It starts when the main
|
||||
branch of D-Modem changes, one time each day, or when you start it
|
||||
manually. It opens a pull request that moves the submodule. Build
|
||||
(`.github/workflows/build.yml`) builds and tests the app in that pull
|
||||
request. The workflow merges the pull request when all checks pass. Then
|
||||
Build runs on `main`, and the next nightly release has the new app. If a
|
||||
check fails, the pull request stays open for a person. The workflow needs
|
||||
a `DMODEM_BUMP_TOKEN` secret. The header of the workflow gives the
|
||||
necessary permissions.
|
||||
@@ -1,81 +1,597 @@
|
||||
# Design
|
||||
|
||||
## One library, three native shells
|
||||
## One library, one Win32 app
|
||||
|
||||
The apps share everything that is not drawing. `core/` is libdmodemctl, a
|
||||
small C library with no I/O and no threads ("sans-I/O"): each app opens the
|
||||
modem in its own way, hands the bytes it reads to `dmc_feed()`, writes what
|
||||
`dmc_output()` gives it, and calls `dmc_poll()` from a 50 ms timer. That
|
||||
keeps it easy to call from C (GTK), C++ (Win32) and Swift, and easy to test
|
||||
(`core/test`, run by CI with ASan and UBSan).
|
||||
The app is one Win32 app (`windows/`). The same source code builds for
|
||||
Windows, and for Linux and macOS over the portable Win32 layer of
|
||||
clang-retro MFC (refer to "The Windows app on Linux and macOS", below).
|
||||
|
||||
All the code that does not draw is in `core/`. `core/` is libdmodemctl, a
|
||||
small C library with no I/O and no threads ("sans-I/O"). The app opens the
|
||||
modem. Then it gives the bytes that it reads to `dmc_feed()`, writes the
|
||||
data that `dmc_output()` gives it, and calls `dmc_poll()` from a 50 ms
|
||||
timer. Thus the library is easy to call from C and C++. It is also easy to
|
||||
test (`core/test`, which CI runs with ASan and UBSan).
|
||||
|
||||
| Part | What it does |
|
||||
| --- | --- |
|
||||
| `dmc_session` | The AT conversation: a queue of commands and their responses, the call's state from the result codes (RING, CONNECT, NO CARRIER), caller ID, the escape (`+++` with its guard times) and hanging up, and the byte stream for the terminal |
|
||||
| `dmc_model` | The modem's settings from AT!M, with each setting's check and the command that sets it |
|
||||
| `dmc_diag` | The last call's statistics from AT#UD, as labelled rows (the V.58 code tables decoded) |
|
||||
| `dmc_vt` | A small VT100/ANSI screen, for Windows and macOS, whose toolkits have no terminal widget |
|
||||
| `dmc_daemon_*` | Reading what dmodemd prints when it starts: the device it made, or its error |
|
||||
| `dmc_session` | The AT conversation: a queue of commands and their responses, the state of the call from the result codes (RING, CONNECT, NO CARRIER), caller ID, the escape (`+++` with its guard times) and the hang-up, and the byte stream for the terminal |
|
||||
| `dmc_model` | The settings of the modem from AT!M, with the check of each setting and the command that sets it |
|
||||
| `dmc_path` | The connection path at the top of the Settings page: the terminal, the modem, the line and what the line reaches. For each step, its role, name, state and setting, from the line settings and the state of the session |
|
||||
| `dmc_diag` | The statistics of the last call from AT#UD, as labelled rows (with the V.58 code tables decoded) |
|
||||
| `dmc_vt` | The terminal emulator of the D-Modem app (`shared/.../terminal` of D-Modem/app), ported to C: each type of terminal that it emulates (see below). The app draws it from its cells |
|
||||
| `dmc_net` | Telnet (option negotiation, terminal type, window size) and rlogin (the handshake, and the window size on the urgent request of the server), for the Remote page |
|
||||
| `dmc_daemon_*` | It reads the text that dmodemd prints when it starts: the device that it made, or its error |
|
||||
|
||||
## Where the settings come from
|
||||
|
||||
dmodemd's core has one settings model (`Config::model`), which already
|
||||
drives its configurators (AT!CF, AT!CT, AT!C). AT!M prints that model as
|
||||
tab-separated lines: each setting's key, kind (pick, number or text),
|
||||
value, default, range, AT form, group, label and help, then each choice.
|
||||
The Settings page builds its controls from it, so a setting added to the
|
||||
modem shows in every app with no change here. A change is checked by the
|
||||
library, sent as its AT command, and kept once the modem says OK.
|
||||
The core of dmodemd has one settings model (`Config::model`). This model
|
||||
also controls its configurators (AT!CF, AT!CT, AT!C). AT!M prints that model
|
||||
as tab-separated lines. Each line has the key of a setting, its kind (pick,
|
||||
number or text), value, default, range, AT form, group, label and help, and
|
||||
then each choice. The Settings page builds its controls from this output.
|
||||
Thus, if a setting is added to the modem, it shows in the app without a
|
||||
change here. The library checks a change and sends it as its AT command. The
|
||||
app keeps the change when the modem says OK.
|
||||
|
||||
The modem keeps no stored profile, so the page shows AT!R's init string
|
||||
and its **Keep** button saves it in the app's line settings, to be sent
|
||||
each time the modem opens.
|
||||
The modem keeps no stored profile. Thus the page shows the init string of
|
||||
AT!R. Its **Keep** button saves the string in the line settings of the app.
|
||||
The app sends the string each time that the modem opens.
|
||||
|
||||
## The same screens everywhere
|
||||
## The pages
|
||||
|
||||
Each app has the same five pages, with the same words:
|
||||
The app has seven pages:
|
||||
|
||||
| Page | Contents |
|
||||
| --- | --- |
|
||||
| Modem | The state ("Idle", "Dialling", "Online") and its detail (the CONNECT line, the caller), Dial, Answer, Hang Up, the line and the device, Start or Stop |
|
||||
| Terminal | The AT console; the buttons **Full-screen setup** (AT!CF) and **Command-line setup** (AT!CT) |
|
||||
| Settings | Groups of settings as native controls, changed ones marked; Defaults (ATZ), the init string and Keep |
|
||||
| Call Stats | AT#UD's rows and ATI11's report, refreshed after each call |
|
||||
| Log | What dmodemd printed, and the command line it was started with |
|
||||
| Phone | The state of a voice call, Call and Answer, the keypad (DTMF), Mute and Listen. The answering machine (on or off, rings, the greeting) and its messages |
|
||||
| Terminal | The AT console. The buttons **Full-screen setup** (AT!CF) and **Command-line setup** (AT!CT). The type of terminal |
|
||||
| Remote | SSH, Telnet or rlogin. The server, its port (empty: the port of the protocol) and the user. The login method of SSH (keys and the agent, the agent only, a key file, the import of a PuTTY key). Connect. A separate terminal, with its type. The terminal sends its size to the server when the size changes |
|
||||
| Settings | The connection path. Groups of settings as native controls, with the changed settings marked. Defaults (ATZ), the init string and Keep |
|
||||
| Call Stats | The rows of AT#UD and the report of ATI11, refreshed after each call |
|
||||
| Log | The text that dmodemd printed, and the command line that started it |
|
||||
|
||||
The terminal sends Backspace as BS (0x08), the modem's S5: it does not take
|
||||
DEL. Colours are the brand's: background `#0a0f1c`, text `#e8f0ff`.
|
||||
The terminal sends Backspace as BS (0x08), the S5 of the modem, because the
|
||||
modem does not accept DEL. The colours are the colours of the brand:
|
||||
background `#0a0f1c`, text `#e8f0ff`.
|
||||
|
||||
## Starting dmodemd
|
||||
## The connection path
|
||||
|
||||
The top of the Settings page shows how a call is made, as the "Connection
|
||||
Overview" of PCOMM draws its link: four steps joined by one line. Each step
|
||||
has its role above its icon and its setting below.
|
||||
|
||||
| Line | Program | Modem (or Port) | Line (or Modem) | Reaches (or Line) |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| SIP account | Terminal | D-Modem | SIP: the account, registered when the modem takes calls | The phone network, through the server |
|
||||
| SIP, anonymous | Terminal | D-Modem | SIP: nothing registered | SIP addresses, dialled directly |
|
||||
| USB modem | Terminal | D-Modem | The USB modem | Its phone line |
|
||||
| Codec (Linux) | Terminal | D-Modem | The ALSA device | Its phone line |
|
||||
| A modem that already runs | Terminal | Its device, TCP address or COM port | The modem | Its phone line |
|
||||
|
||||
When idle, each step shows the line settings. A step that is ready is in the
|
||||
accent colour. A step that you must still set (no SIP account, no device) is
|
||||
red and its line is dashed, and the text below says what to set. When the
|
||||
modem starts, dials and is online, the steps light green as the call goes,
|
||||
and the text below shows the CONNECT line. `dmc_path` of libdmodemctl has the
|
||||
words. The app draws them in `pathview.cpp` (GDI, with the Wingdings of
|
||||
Windows 95). The D-Modem app has the same picture at the top of its
|
||||
Settings, with its own lines (Wi-Fi and Bluetooth to another phone, the
|
||||
mobile calls of the phone, external modems).
|
||||
|
||||
## The terminals
|
||||
|
||||
The two terminals (the console of the modem and the terminal of the Remote
|
||||
page) use `dmc_vt` of libdmodemctl. This is the emulator of the app in C,
|
||||
with the 29 types of terminal (profiles) of the app. You select the type
|
||||
next to each terminal, and the app keeps it with the settings of the page:
|
||||
|
||||
| Kind | What it is |
|
||||
| --- | --- |
|
||||
| xterm (the default) | A VT220 with colours (8, 16, 256, 24-bit), sixel, the mouse, bracketed paste, the alternate screen, titles, OSC 8 links and Tek mode |
|
||||
| DEC VT52 to VT525 | The answers and sequences of each model: the sequences of the VT52, the levels of the VT100 to VT320, the sixel, ReGIS and Tek mode of the VT340, the rectangles, margins and pages of the VT420, the colours of the VT525. Soft fonts (DECDLD), user-defined keys, the printer port, the status line |
|
||||
| ANSI-BBS, Avatar, RIPscrip, Amiga ANSI | The BBS terminal of a PC in code page 437 and the font of the VGA (the SyncTERM font switch, CSI SP D, with the fonts of the machines), ANSI music, iCE colours, doorway mode. The codes of Avatar. The graphics and mouse buttons of RIPscrip on 80x43. The Topaz and palette of the Amiga |
|
||||
| Commodore 64, 128 (40 and 80 col.), Plus/4, VIC-20, PET (40 and 80) | PETSCII, and the screen, colours and ROM font of each machine |
|
||||
| Atari 8-bit (and international), Atari ST (80 and 40 col.) | ATASCII. The TOS VT52 of the ST with its pens |
|
||||
| Viewdata, Minitel | The 40x24 of Prestel with its mosaics. The videotex of the Minitel and its Téléinformatique (a VT100) |
|
||||
| Tektronix 4014 | The storage tube, as a separate type |
|
||||
|
||||
The library is sans-I/O, as the remaining parts are. Bytes go in
|
||||
(`dmc_vt_feed`) and answers come out (`dmc_vt_reply`). The library changes
|
||||
each typed key or character into the bytes that the type of terminal sends
|
||||
(`dmc_vt_key_mods`, `dmc_vt_char_bytes`: the PETSCII of a Commodore, the
|
||||
ATASCII of an Atari). A view draws each cell (`dmc_vt_cell`) with these
|
||||
items:
|
||||
|
||||
- Its character and resolved colours, and its attributes.
|
||||
- The bitmap of the font of a machine (`dmc_vt_glyph_row`). These are the
|
||||
SyncTERM copies of the ROMs, generated into `core/src/vt_data.c` by
|
||||
`tools/gen_vt_data.py`.
|
||||
- The tile of a picture (`dmc_vt_cell_image`: sixel, ReGIS, RIPscrip, the
|
||||
Tektronix).
|
||||
|
||||
If the screen of a machine has its own size (40x25 for a C64), the library
|
||||
requests that size (`dmc_vt_requested_cols`). The view stretches its cells
|
||||
to fill the window, by whole pixels where they fit.
|
||||
|
||||
The view is `termview.cpp`. It draws with GDI, one row at a time, into a
|
||||
DIB section (text by `ExtTextOutW`, glyphs and pictures by pixel). The
|
||||
view copies the section to the window (or stretches it, for the double
|
||||
width and height of DEC).
|
||||
|
||||
### Copy, capture and print
|
||||
|
||||
The terminal control (`termview.cpp`) keeps a selection. The
|
||||
selection uses the rows of `dmc_vt_cell` (a row below 0 is in the
|
||||
scrollback). The selection moves up with its text when new lines push the
|
||||
text into the scrollback.
|
||||
|
||||
- Drag with the left mouse button to select. Double-click to select a word.
|
||||
When the remote side uses the mouse, hold Shift.
|
||||
- Copy (Ctrl+Insert, or the Edit menu, or the menu of the right mouse
|
||||
button) puts the text on the clipboard. The text comes from
|
||||
`dmc_vt_text_range`. The app removes the blanks at the end of each line.
|
||||
The clipboard gets Unicode text and the text of the ANSI code page.
|
||||
- Paste to Host (Shift+Insert) types the clipboard text.
|
||||
|
||||
`ascii.cpp` has the ASCII Setup dialog (Modem menu). `dmodem.ini` keeps
|
||||
the settings in the `[ascii]` section: `send_lf`, `echo`, `line_delay`,
|
||||
`char_delay`, `add_lf`, `seven_bit` and `wrap`. The app applies them to
|
||||
the two terminals:
|
||||
|
||||
- `send_lf` sets Enter to CR LF (`dmc_vt_set_enter`).
|
||||
- `echo` shows the characters that you type on the terminal. The app does
|
||||
not show the escape sequences of the keys (for example, the arrow keys).
|
||||
- `add_lf`, `seven_bit` and `wrap` go to `dmc_vt_set_ascii`. With
|
||||
`DMC_VT_ASCII_ADD_LF`, the terminal adds an LF after each CR that it
|
||||
receives. When an LF follows the CR, the terminal does not use that LF,
|
||||
thus a CR LF does not give two lines. `DMC_VT_ASCII_7BIT` removes bit 8
|
||||
of each byte. `DMC_VT_ASCII_NO_WRAP` stops the wrap at the right margin:
|
||||
the terminal writes each new character in the last column.
|
||||
|
||||
`termset.cpp` has the Terminal Settings dialog (Modem menu). It applies
|
||||
to the terminal of the page that shows: the Remote page's, else the
|
||||
modem's. `dmodem.ini` keeps the settings in `[terminal]` (the modem) and
|
||||
`[remote_terminal]` (Remote). `termview_set_options` applies them:
|
||||
|
||||
- `backspace`: 0 sends Ctrl+H, 1 sends Del, 2 sends Ctrl+H, Space,
|
||||
Ctrl+H. The default of the modem is 0 (the configurator of the modem
|
||||
uses Ctrl+H). The default of Remote is 1.
|
||||
- `windows_keys`: Ctrl+C copies, Ctrl+V pastes, Ctrl+A selects all. The
|
||||
arrow keys, Page Up and Page Down scroll the backscroll. The function
|
||||
keys go to the window (F10 opens the menu). The terminal does not send
|
||||
these keys.
|
||||
- `backscroll`: the lines that the terminal keeps
|
||||
(`dmc_vt_set_scrollback`). When the number is smaller, the terminal
|
||||
keeps the newest lines.
|
||||
- `font` and `font_size`: a fixed-pitch font. When the name is empty, the
|
||||
terminal uses the font of the app.
|
||||
- `cursor` (0 block, 1 underline) and `cursor_blink`. A DECSCUSR from the
|
||||
remote side has priority.
|
||||
- `telnet_id` (Remote only): the TERMINAL-TYPE that Telnet sends. When it
|
||||
is empty, Telnet sends the name of the terminal type.
|
||||
- `exit_on_disconnect` (modem only): the app closes when a call ends.
|
||||
|
||||
The status bar shows the connect timer during a call ("Online 0:01:05").
|
||||
Send Break (Call menu, or Ctrl+Break in a terminal) sends a break. On a
|
||||
COM port, the app holds the line at space for 250 ms (`SetCommBreak`). On
|
||||
Telnet, the app sends IAC BRK (`dmc_net_break`). TCP to dmodemd, rlogin
|
||||
and SSH cannot send a break, and the status bar tells you.
|
||||
|
||||
`calls.cpp` has the Call menu, as HyperTerminal has it:
|
||||
|
||||
- Call opens the Call dialog. A session is a name and a number (or a SIP
|
||||
address). `dmodem.ini` keeps the sessions in the `[sessions]` section
|
||||
(`count`, `name1`, `number1` ...). Save adds or changes a session, and
|
||||
Delete removes it. Call dials the number (ATD) and shows the Terminal
|
||||
page.
|
||||
- Wait for a Call sends ATS0=1: the modem answers at the first ring. The
|
||||
app sends ATS0=1 again each time the modem opens. When you clear the
|
||||
check mark, the app sends ATS0=0.
|
||||
- Disconnect hangs up the call (`dmc_hangup`), or closes the Remote
|
||||
connection when the Remote page shows.
|
||||
|
||||
The Line Settings dialog has the settings of a COM port, as the Configure
|
||||
dialog of HyperTerminal: bits per second (110 to 921,600), data bits (5 to
|
||||
8), parity (none, odd, even, mark, space), stop bits (1, 1.5, 2) and flow
|
||||
control (none, Xon/Xoff, hardware). `dmodem.ini` keeps them as `com-bps`,
|
||||
`com-data-bits`, `com-parity`, `com-stop-bits` and `com-flow`. The default
|
||||
is 115,200 bps, 8 data bits, no parity, 1 stop bit and hardware flow
|
||||
control.
|
||||
|
||||
`capture.cpp` has the captures. Each capture is of one line (the modem or
|
||||
the Remote login). The capture does not get the bytes of a file transfer.
|
||||
|
||||
- Capture Text writes the bytes from the line to a file, as they come
|
||||
(with the escape sequences). The app adds the bytes to the end of a file
|
||||
that exists, as HyperTerminal does. `dmodem.ini` keeps the folder in the
|
||||
`[capture]` section.
|
||||
- Capture to Printer keeps the text from the line. The app removes the
|
||||
escape sequences and the control characters (but not tabs, line ends and
|
||||
form feeds). When you stop the capture, the app prints the text on the
|
||||
default printer.
|
||||
- Print (Modem menu) prints the screen or the selection, with the print
|
||||
dialog.
|
||||
- When the remote side prints (`CSI 5 i`, `dmc_vt_printed`), the app prints
|
||||
the text on the default printer after the remote side stops for 3
|
||||
seconds. During Capture to Printer, this text goes into the capture.
|
||||
|
||||
The app prints with GDI: Courier New at 10 points, with margins of half an
|
||||
inch. The app wraps long lines, and a form feed starts a new page.
|
||||
|
||||
## File transfers
|
||||
|
||||
The file transfer protocols are in libdmodemctl (`core/src/xfer*.c`). They
|
||||
are a C port of the protocols of the D-Modem app (`ZModem.kt`,
|
||||
`XYModem.kt`, `Kermit.kt`, `HexRecords.kt` and `TextTransfer.kt`). The
|
||||
protocols are:
|
||||
|
||||
- ZMODEM: CRC-16 and CRC-32 headers, ZDLE escapes, streaming data (ZCRCG)
|
||||
with a ZCRCQ each 16 KB, and recovery from errors with ZRPOS.
|
||||
- ZMODEM crash recovery (`dmc_xfer_options.resume`, "ZMODEM with Crash
|
||||
Recovery" in the dialogs). The receiver continues a part of a file that
|
||||
is in the folder: it opens the file to add to it, and it sends ZRPOS with
|
||||
the length of the part. The receiver also does this when the sender sets
|
||||
ZF0 to ZCRECOV (`sz -r`). If the file is there whole, the receiver sends
|
||||
ZSKIP. When a transfer stops, the receiver keeps the part that it
|
||||
received. The sender with this option sets ZF0 to ZCRECOV.
|
||||
- ZMODEM with zstd (the specification is `docs/zmodem-zstd.md` in the
|
||||
D-Modem repository). The D-Modem BBS, GUI and app use this mode. The
|
||||
receiver sets `CANZSTD` (0x40) in ZF1 of its ZRINIT. The sender sets
|
||||
`ZTZSTD` (8) in ZF2 of the ZFILE for each file that it compresses. Then
|
||||
the sender sends the file as independent zstd frames, one frame for each
|
||||
chunk of 64 KB. After a ZRPOS, the chunk is half as large (minimum 1 KB).
|
||||
After an ACK, the chunk is two times as large (maximum 64 KB). The
|
||||
receiver writes only complete frames. All positions are file offsets,
|
||||
thus ZRPOS and crash recovery operate as without zstd. The sender does
|
||||
not compress a file of 0 bytes, a file with a compressed extension (for
|
||||
example `.zip`, `.gz` or `.jpg`), or a file whose first 64 KB do not
|
||||
become 5% smaller. When the other side does not use this mode, the
|
||||
transfer is normal ZMODEM. To stop zstd, set `dmc_xfer_options.no_zstd`
|
||||
(in the app: Transfer, ZMODEM Compression (zstd)).
|
||||
`dmc_xfer_zstd` tells if the file now goes with zstd. The zstd encoder
|
||||
and decoder are in `core/src/zstd_codec.c`. They are a C port of the
|
||||
codec of the D-Modem app (`Zstd.kt`), and they do not use libzstd.
|
||||
- YMODEM, XMODEM and XMODEM-1K.
|
||||
- YMODEM-G: the receiver asks with G. The sender sends the blocks and does
|
||||
not wait for an ACK. Use it only on a line that corrects its errors (V.42).
|
||||
An error stops the transfer, because a block cannot come again. The
|
||||
sender does plain YMODEM when the receiver asks with C (lrzsz's `rb`).
|
||||
lrzsz's `sb` sends no empty block 0 at the end in YMODEM-G, thus the
|
||||
receiver ends the batch when no block 0 comes after a file.
|
||||
- Kermit: long packets, block checks 1 to 3, attribute packets,
|
||||
repeat counts and 8-bit prefixes.
|
||||
- Intel HEX and Motorola S-records: the app sends a file of records as it
|
||||
is, and it encodes other files as records. When it receives, it decodes
|
||||
the records to a binary image.
|
||||
- Plain text: the app sends the bytes of the file as they are. With
|
||||
`line_delay_ms` and `char_delay_ms`, the app waits after each line end
|
||||
and after each character (as the ASCII Setup of HyperTerminal). When it
|
||||
receives, it keeps the text until the line is quiet. A cancel keeps the
|
||||
text that came.
|
||||
|
||||
`dmc_xfer_skip` skips the file that the app receives now. ZMODEM sends
|
||||
ZSKIP. Kermit puts X in the ACK of a data packet, and the sender ends the
|
||||
file with Z and D. The progress dialog has a Skip file button for these
|
||||
two protocols.
|
||||
|
||||
Each transfer runs on its thread (CreateThread on Windows, pthreads on
|
||||
other systems). The thread and the app use two queues with a lock:
|
||||
|
||||
1. The app gives the bytes from the line to `dmc_xfer_feed`.
|
||||
2. The app gets the bytes for the line from `dmc_xfer_output`. The app
|
||||
gets only as much as the line can send now. The queue holds 32 KB
|
||||
maximum, thus the thread waits when the line is slow.
|
||||
3. The notify function of `dmc_xfer_set_notify` tells the app when there
|
||||
are bytes to send. The app posts `WM_APP_XFER` to the window.
|
||||
4. `dmc_xfer_get_status` gives the file, the bytes, the size, the retries
|
||||
and the result.
|
||||
|
||||
The protocol code reads files only through `xfer_src_read`, and writes
|
||||
files only through `xfer_dst_write`. The positions in the protocols are
|
||||
file offsets. When a transfer fails or you cancel it, the app deletes the
|
||||
part of the file that it received (but not for plain text).
|
||||
`dmc_xfer_cancel` sends the cancel sequence of the protocol: 10 CAN and
|
||||
10 BS for ZMODEM, an E packet for Kermit.
|
||||
|
||||
`dmc_xfer_detect` finds the start of a transfer in the received data: the
|
||||
ZRQINIT of sz, the ZRINIT of rz, and the Send-Init of kermit -s. When
|
||||
Automatic Receive is on, the app starts a receive, or it opens the Send
|
||||
File dialog for rz.
|
||||
|
||||
The app (`windows/src/xfer.cpp`) has these parts:
|
||||
|
||||
- The Transfer menu: Send File, Receive File, Automatic Receive and
|
||||
Cancel Transfer. The Terminal page also has Send and Receive buttons.
|
||||
- The Send File and Receive File dialogs, and a progress dialog as in
|
||||
HyperTerminal. The progress dialog shows the file, the protocol, the
|
||||
retries, the throughput, the time and the bytes.
|
||||
- The transfer uses the line of the page that you see: the modem on the
|
||||
Terminal page, or the login on the Remote page (`struct XferLine`).
|
||||
- `dmodem.ini` keeps the folder, the protocols and Automatic Receive in
|
||||
the `[transfer]` section. The default folder is `Received` in the data
|
||||
folder of the app.
|
||||
|
||||
`core/test/xfer_test.c` tests each protocol on a clean line and on a line
|
||||
with noise. It also does a test with `sz` and `rz` (lrzsz) and with
|
||||
C-Kermit on a pty, when they are installed. Set `DMODEM_XFER_INTEROP=1` to
|
||||
make these tests mandatory.
|
||||
|
||||
## How the app starts dmodemd
|
||||
|
||||
The app starts dmodemd with `CreateProcess`. The DTE of dmodemd is a TCP
|
||||
socket (`--tcp 127.0.0.1:port`). The app keeps its settings in
|
||||
`dmodem.ini`: `%APPDATA%\D-Modem\dmodem.ini` on Windows,
|
||||
`~/.config/D-Modem/dmodem.ini` on Linux and
|
||||
`~/Library/Application Support/D-Modem/dmodem.ini` on macOS.
|
||||
|
||||
The app gives the SIP account to dmodemd in its environment (`SIP_LOGIN`,
|
||||
`SIP_T38`, `SIP_V152`, `SIP_V150`, `SIP_SRTP`). It gives the country, the
|
||||
USB modem and the debug level as flags.
|
||||
|
||||
The app can also use a modem that already runs: a TCP address or a COM
|
||||
port. On Linux and macOS, it can also be a device path (`/dev/ttyUSB0`,
|
||||
`/dev/cu.usbmodem1`), or a COM port name that the layer changes to a
|
||||
device (refer to "The Windows app on Linux and macOS", below).
|
||||
|
||||
The app does not start dmodemd by default. It runs the modem in a thread
|
||||
(`libdmodem_embed`), and its DTE is a socket. Refer to
|
||||
[The built-in modem](embedded-modem.md).
|
||||
|
||||
## Hardware modems: the MultiTech MT9234MU
|
||||
|
||||
The COM port line can also open a hardware modem. The app sends AT commands
|
||||
to it and read its result codes, as they do with dmodemd. The Settings page
|
||||
needs `AT!M`, thus it operates only with dmodemd.
|
||||
|
||||
The MultiTech MultiMobile MT9234MU (USB 06E0:F114) and the MT9234ZBA-USB
|
||||
(06E0:F115, 06E0:0319) are V.92 data and fax modems. They have a TI TUSB3410
|
||||
USB UART. The TUSB3410 has no firmware of its own. The host downloads the
|
||||
firmware into the chip each time you connect the modem.
|
||||
|
||||
| | Linux | macOS | Windows |
|
||||
| --- | --- | --- | --- |
|
||||
| Started with | GSubprocess | Process | CreateProcess |
|
||||
| Its DTE | a pty (it prints "Use `/dev/pts/N' as modem device") | a pty | `--tcp 127.0.0.1:port` |
|
||||
| Settings kept in | `~/.config/dmodem-gui/settings.ini` (mode 0600) | user defaults, the SIP password in the keychain | `%APPDATA%\D-Modem\dmodem.ini` |
|
||||
| Driver | `ti_usb_3410_5052` (in the kernel) | we do not know a driver for the MT9234MU. The CDC-ACM models (MT9234MU-CDC) use the driver of the system | the driver of MultiTech |
|
||||
| Firmware | `mts_mt9234mu.fw` in `/lib/firmware` | not applicable | the driver of MultiTech has it |
|
||||
| The port | `/dev/ttyUSB0` | `/dev/cu.usbmodem*` (CDC-ACM models) | a COM port |
|
||||
|
||||
The SIP account goes to dmodemd in its environment (`SIP_LOGIN`,
|
||||
`SIP_T38`, `SIP_V152`, `SIP_V150`, `SIP_SRTP`); the country, USB modem and
|
||||
debug level as flags. On Linux and macOS the pty is made in the user's
|
||||
own group (`-g`), so that dmodemd needs no root.
|
||||
Linux does not include `mts_mt9234mu.fw` after June 2026. MultiTech gave no
|
||||
license that lets other persons distribute it, thus linux-firmware removed
|
||||
it. This repository includes the file, with a notice
|
||||
(`firmware/README.md`). The AGPL-3.0 license does not apply to it.
|
||||
`cmake --install` puts it in `share/dmodem-gui/firmware`, and
|
||||
`-DDMODEM_GUI_INSTALL_FIRMWARE=ON` also puts it in `/lib/firmware`. Else,
|
||||
copy it to `/lib/firmware` (for the MT9234ZBA-USB, as `mts_mt9234zba.fw`),
|
||||
then disconnect the modem and connect it again.
|
||||
|
||||
Each app can instead use a modem that already runs: a device path on
|
||||
Linux and macOS; a TCP address or a COM port on Windows.
|
||||
### Look for a modem
|
||||
|
||||
The Line Settings dialog finds the modem (`windows/src/ports.cpp`). When
|
||||
the dialog opens, it searches the ports. If it finds a modem on a port
|
||||
that is not selected, it shows the modem below the settings of the serial
|
||||
port. Click **Look for a modem** to search again. If the app finds a
|
||||
modem, it selects "A modem on a serial port", the port of the modem and
|
||||
hardware flow control (RTS/CTS). The modem uses `&K3` from the factory.
|
||||
The Port box lists all the ports that the app found, with the modems
|
||||
first. When you select a port, the dialog shows what is on it.
|
||||
|
||||
| | Where the app looks | What is a modem |
|
||||
| --- | --- | --- |
|
||||
| Windows | The modems that Windows knows: the Modem class in the registry (`AttachedTo`, and `FriendlyName` or `DriverDesc`), for Windows 95, 98 and ME and for NT. The MultiTech modems in `Enum\USB` (NT). The ports of `HARDWARE\DEVICEMAP\SERIALCOMM`. If that key has no ports, the app tries `COM1` to `COM16` | An installed modem, or a MultiTech MT9234MU or MT9234ZBA-USB |
|
||||
| Linux | `/sys/class/tty`: each `ttyUSB` and `ttyACM`, with the maker and the product of its USB device | A MultiTech MT9234MU or MT9234ZBA-USB, or a USB device whose name has "modem" in it |
|
||||
| macOS | Each `/dev/cu.*` device, but not the Bluetooth devices | A `cu.usbmodem` device (CDC-ACM, for example the MT9234MU-CDC) |
|
||||
|
||||
On Linux, if a MultiTech modem is connected but has no port, the dialog
|
||||
tells you why. With no firmware (one USB configuration, the boot ROM of
|
||||
the TUSB3410), it shows the command that copies the file. If the
|
||||
firmware is there, it tells you to load the `ti_usb_3410_5052` driver.
|
||||
|
||||
Set the country of the line with `AT%T19,0,<code>` and `AT&F&W` in the
|
||||
"Commands when the modem opens" field one time, or in the Terminal page.
|
||||
`AT%T19,0,34` is the default (North America and Europe).
|
||||
|
||||
`ATI11` on these modems (and on other Agere modems) shows its report on two
|
||||
pages. After each page, the modem waits for a key ("Press any key"). The
|
||||
library sends a space when a command of the app gets this prompt, thus the
|
||||
Call Stats page gets the full report. It does this for 8 pages at most. In
|
||||
the Terminal page, you push the key.
|
||||
|
||||
We did not test this yet with a real MT9234MU.
|
||||
|
||||
## Remote logins
|
||||
|
||||
The Remote page logs in to a server, next to the modem, in a separate
|
||||
terminal. Telnet and rlogin are a TCP connection that the app makes with
|
||||
Winsock. The app uses `dmc_net` of libdmodemctl for the protocol (sans-I/O,
|
||||
as the session). SSH is libdmodemssh (`ssh/`), the client of D-Modem on
|
||||
libssh 0.11. The Android and iOS apps use the same engine. Thus SSH is the
|
||||
same on all platforms, and you do not need to install anything:
|
||||
|
||||
- One thread for each connection calls `dmc_ssh_poll()` and gives each event
|
||||
to the thread of the UI. The UI sends typed text, the window size and the
|
||||
answers directly to the connection (it locks).
|
||||
- The app checks the key of the host against `known_hosts` of OpenSSH. If
|
||||
the key is known, the login continues. If the key is new, the app shows it
|
||||
(its SHA256 fingerprint) and adds it if the user accepts it. If the key
|
||||
changed, the app refuses it with a warning.
|
||||
- The login uses the order of OpenSSH: first the agent and the keys of
|
||||
`~/.ssh`, then keyboard-interactive or a password (three tries), in the
|
||||
dialogs of the app. The app reads `~/.ssh/config` (host aliases, users,
|
||||
ports).
|
||||
- Alternatively, you can select with Login... next to the server one of
|
||||
these: the keys of
|
||||
the agent only, or a key file. The key file can be in the format of
|
||||
OpenSSH, PEM or the `.ppk` of PuTTY (versions 2 and 3). `ssh/src/ppk.c`
|
||||
reads `.ppk` files, with Argon2, the same with each libssh build. The app
|
||||
asks for the passphrase in its dialog. You can also import a `.ppk`. The
|
||||
app writes it to `.ssh` in the OpenSSH format with the same passphrase,
|
||||
and selects it.
|
||||
- On Linux and macOS, the agent is the socket of an ssh-agent
|
||||
(`SSH_AUTH_SOCK`, or a socket that you give, as `IdentityAgent`). On
|
||||
Windows, it is the Pageant of PuTTY (the app asks it by WM_COPYDATA, as the
|
||||
tools of PuTTY do). If Pageant is not available, it is the agent of OpenSSH
|
||||
(`\\.\pipe\openssh-ssh-agent`), or a named pipe that you give.
|
||||
`ssh/src/agent.c` connects libssh to those two agents with a pair of
|
||||
loopback sockets and a thread.
|
||||
- Ed25519, ECDSA and RSA keys. A shell on a pty with TERM `xterm-256color`.
|
||||
The app sends the size of the pty when it changes. The engine also has
|
||||
commands, direct channels (`-L`, `-D`), remote listens (`-R`), agent
|
||||
channels (`-A`), and it can make keys or encrypt them again. The mobile
|
||||
apps use these functions.
|
||||
|
||||
If the system has libssh 0.11 or later (Linux), libssh and mbedTLS come from
|
||||
the system. If not, the build makes them from their release tarballs, and
|
||||
checks them by hash (`ssh/cmake/BundledLibssh.cmake`). This always occurs on
|
||||
Windows and macOS. A build with no libssh (`-DDMODEM_GUI_SSH=OFF`) runs the
|
||||
`ssh.exe` of Windows (Windows 10 1809 and later) in a pseudo console, if it
|
||||
finds it.
|
||||
|
||||
The Windows 95 build also has libssh. libssh and mbedTLS use some functions
|
||||
that Windows 95 does not have. Thus the build changes them as follows
|
||||
(`ssh/cmake/PatchMbedtls.cmake` and `PatchLibssh.cmake`, only for targets
|
||||
before Windows Vista):
|
||||
|
||||
| Function | Windows that have it | In the Windows 95 build |
|
||||
| --- | --- | --- |
|
||||
| `BCryptGenRandom` (mbedTLS, the random numbers) | Vista and later | `CryptGenRandom` of CryptoAPI, found at run time. CryptoAPI is in Windows 95 OSR2, 98, NT 4.0 and later, and Internet Explorer 3 and later installs it. If CryptoAPI is not there, SSH cannot connect. The other pages operate |
|
||||
| `SHGetSpecialFolderPathA` (libssh, the home folder) | 98, 2000, and 95 and NT 4.0 with the shell of Internet Explorer 4 | found at run time, else `USERPROFILE` |
|
||||
| `if_nametoindex` (libssh, IPv6 link-local addresses) | Vista and later | found at run time, else no interface names |
|
||||
| `WSAStringToAddressA` (libssh, the address check) | 98, and 95 with Winsock 2 | found at run time, else IPv4 with `inet_addr` of Winsock 1.1 |
|
||||
| `gai_strerrorA` of mingw-w64 (it calls `FormatMessageW`) | NT | `ssh/src/gai_strerror.c`, with `FormatMessageA` |
|
||||
|
||||
Thus the app imports only DLLs that each Windows 95 has, as before. libssh
|
||||
uses Winsock 1.1 (`wsock32`) for its sockets. `getaddrinfo` is the
|
||||
`WspiapiGetAddrInfo` of mingw-w64, which uses `gethostbyname` if Winsock 2
|
||||
is not there.
|
||||
|
||||
| | How the app does it |
|
||||
| --- | --- |
|
||||
| Telnet and rlogin | Winsock 1.1, `WSAAsyncSelect` (FD_OOB for rlogin), on all Windows versions from 95 |
|
||||
| SSH | libdmodemssh on a thread, posted events (`WM_APP_SSH`), the `.ssh` of the profile |
|
||||
| Kept in | `dmodem.ini`, `[remote]` |
|
||||
|
||||
The TERM of SSH and the terminal of rlogin come from the type of terminal
|
||||
(`xterm-256color`, `vt220`, `ansi`, `petscii` ...). The TERMINAL-TYPE of
|
||||
Telnet is a separate value
|
||||
(`XTERM-256COLOR`, `DEC-VT220`, `ANSI` ...: `dmc_vt_profile_term`,
|
||||
`dmc_vt_profile_telnet`).
|
||||
|
||||
Telnet tells the server the terminal type and the window size (NAWS). It
|
||||
sends a typed CR as CR NUL. rlogin sends the user as the local name and as
|
||||
the remote name. Some servers accept only a client port below 1024 (classic
|
||||
BSD `rlogind`). These servers do not accept an unprivileged app, but BBSes
|
||||
and most rlogin services accept it. Backspace sends DEL, as a host expects
|
||||
(the console of the modem sends BS).
|
||||
|
||||
## Voice calls and the answering machine
|
||||
|
||||
Voice uses the V.253 voice mode of dmodemd: `AT+FCLASS=8`, `AT+VSM=4` (G.711
|
||||
μ-law at 8 kHz), then `ATD` or `AT+VLS=1`, and `AT+VTR` for the full-duplex
|
||||
stream. libdmodemctl does all of this work. It does the DLE framing and its
|
||||
events (hangup, busy, dial tone, digits, caller ID), μ-law, the pacing of
|
||||
the stream by the clock that the app gives to `dmc_poll`, DTMF, and the
|
||||
answering machine. The app only moves 8 kHz 16-bit samples between the
|
||||
library and the sound card (`dmc_voice_read`, `dmc_voice_write`):
|
||||
|
||||
| | How the app does it |
|
||||
| --- | --- |
|
||||
| Sound | `waveOut` and `waveIn` with window callbacks (Windows 95 has them). On Linux and macOS, the layer supplies them |
|
||||
| Settings | `dmodem.ini`, `[voice]` |
|
||||
| Messages | `Voicemail` in the settings folder (for example `%APPDATA%\D-Modem\Voicemail`), or next to the exe |
|
||||
| Greeting files beyond the library's | the ACM codecs: MP3 and compressed WAVs (GSM 6.10 and the other installed codecs) |
|
||||
|
||||
The answering machine is disabled until you enable it. When it is enabled,
|
||||
it answers after the selected number of rings (4 by default). It plays the
|
||||
greeting (or no greeting) and a beep. Then it records until one of these
|
||||
conditions occurs: the caller hangs up, 7 s of silence, or 2 minutes. Each
|
||||
message is a WAV file. Its name is its time and the number of the caller.
|
||||
The app deletes a message that is shorter than one second.
|
||||
|
||||
You can record a greeting from the microphone, or select a sound file. The
|
||||
app converts the file to 8 kHz mono. The library itself reads WAV (8 to
|
||||
32-bit PCM, floats, A-law, μ-law, IMA and Microsoft ADPCM), AIFF and AIFF-C,
|
||||
and Sun/NeXT `.au` (`dmc_audio_decode`). The app gives all other files to
|
||||
the ACM codecs, as the table shows. Pick Up changes a message
|
||||
into a call while the caller leaves it. Listen plays the message while the
|
||||
caller leaves it. Messages stay on this computer.
|
||||
|
||||
## Windows 95 and later
|
||||
|
||||
The Windows app is plain Win32 in C++98, so that one binary runs from
|
||||
The Windows app is plain Win32 in C++98. Thus one binary runs from
|
||||
Windows 95 to Windows 11:
|
||||
|
||||
- ANSI APIs, with UTF-8 from the library converted by hand (no `CP_UTF8`
|
||||
on 9x); the terminal draws with `ExtTextOutW`, which 9x has.
|
||||
- ANSI APIs. The app converts the UTF-8 from the library with its own code
|
||||
(9x has no `CP_UTF8`). The terminal draws with `ExtTextOutW`, which 9x has.
|
||||
- The Windows 95 common controls (tabs, list views, the status bar),
|
||||
Winsock 1.1 (`wsock32`, `WSAAsyncSelect`) and the 4.0 `OPENFILENAME`.
|
||||
- No `SetHandleInformation` (NT only): the pipe from dmodemd is made
|
||||
non-inheritable with `DuplicateHandle`.
|
||||
- Built by Open Watcom 2.0 (`windows/build-watcom.sh`: `runtime
|
||||
windows=4.0`, the static C library), as CI does; MinGW and MSVC build the
|
||||
same code with CMake.
|
||||
- No `SetHandleInformation` (NT only). The app uses `DuplicateHandle` to
|
||||
make the pipe from dmodemd non-inheritable.
|
||||
- clang-retro builds the app (`cmake/clang-retro.cmake`, a CMake toolchain
|
||||
file), as CI does. Its toolchain package comes from the
|
||||
`clang-retro/dos-sdk/.github/actions/setup-toolchain` action, or from
|
||||
`packaging/get-toolchain.sh` in that repository. `CLANG_DOS` is the
|
||||
directory of the package. `CLANG_RETRO_CONFIG` is the `--config` file:
|
||||
- `win32-picolibc` (the default): Windows 95, 98, ME, NT 4.0 and later.
|
||||
picolibc is the C library, and it is linked into the app. Thus the app
|
||||
needs no `msvcrt.dll`, which some installations of Windows 95 do not
|
||||
have. This build has libssh too (see "Remote logins", above).
|
||||
- `win7`: Windows 7 and later. This is the minimum Windows version of
|
||||
dmodemd too.
|
||||
- CI examines the imports of the Windows 95 build with `check-imports.py`
|
||||
of clang-retro, for Windows 9x and for NT 4.0. The build fails if the app
|
||||
uses a function that these versions do not have. CI also runs the tests
|
||||
of the two builds in Wine.
|
||||
- picolibc has no `_snprintf`, `_stricmp` or `_strnicmp`. `app.h` gives
|
||||
these names to `snprintf`, `strcasecmp` and `strncasecmp`.
|
||||
- MinGW and MSVC also build the same code with CMake.
|
||||
|
||||
dmodemd itself needs Windows 7 or later. On Windows 9x and NT 4 the app
|
||||
defaults to a COM port, and can reach a dmodemd on another machine over TCP.
|
||||
The app has the modem built in on each Windows (refer to
|
||||
[The built-in modem](embedded-modem.md)). It can also use a COM port, or
|
||||
connect to a dmodemd on another machine through TCP.
|
||||
|
||||
## The Windows app on Linux and macOS
|
||||
|
||||
The Windows app also builds for Linux and macOS. clang-retro MFC has a
|
||||
portable Win32 layer (`port/`, `libwinport.a`). The layer supplies the
|
||||
Win32 API on X11, Wayland and Cocoa: windows and controls, Winsock,
|
||||
files, COM ports, processes and pipes, and sound (`waveOut`, `waveIn`
|
||||
and ACM). The app links with this layer and not with Windows. Its source
|
||||
code is the same as on Windows.
|
||||
|
||||
- Use `-DDMODEM_GUI_WINPORT=ON` and set `CLANG_RETRO_MFC` to a checkout of
|
||||
clang-retro MFC. Use clang. `winport/CMakeLists.txt` builds
|
||||
`libwinport.a` with the Makefile of MFC. It converts `app.rc` to C++
|
||||
with `port/tools/res2cpp.py` (this needs `llvm-rc`). The app is
|
||||
`winport/dmodem-win32` in the build directory. `windows/sources.cmake`
|
||||
is the list of sources for the two builds.
|
||||
- `DMODEM_WINPORT` is defined for this build. The app uses it in these
|
||||
locations only:
|
||||
- The settings file is in the `CSIDL_APPDATA` folder of the layer
|
||||
(`~/.config/D-Modem` on Linux, `~/Library/Application Support/D-Modem`
|
||||
on macOS). On Windows, the app uses `APPDATA`, because Windows 95 has
|
||||
no `SHGetFolderPath`.
|
||||
- The line is dmodemd by default. The layer tells the app that Windows
|
||||
is version 5, and on Windows the app then uses a COM port by default.
|
||||
- dmodemd is `dmodemd` next to the app, or else on the `PATH`. The app
|
||||
starts dmodemd only when the built-in modem is off (`builtin=0`).
|
||||
- With the D-Modem submodule, the app has the built-in modem
|
||||
(`DMODEM_GUI_EMBED`), as the Windows app on Windows has it. The layer's
|
||||
`SOCKET` is a POSIX file descriptor, so `WSAAsyncSelect` works with the
|
||||
socket that `libdmodem_embed` gives. The data file of the line is
|
||||
`dmodem0.data` in the settings folder.
|
||||
- SSH uses `~/.ssh` and `/` in paths, because libssh uses POSIX paths
|
||||
on these systems.
|
||||
- The layer changes a COM port name to a device. On Linux, `COM1` is
|
||||
`/dev/ttyS0`. On macOS, `COM1` is the first `/dev/cu.*` device (in
|
||||
alphabetical order, with no Bluetooth devices). To use another device,
|
||||
set the environment variable `WINPORT_COMn`. For example, with
|
||||
`WINPORT_COM5=/dev/ttyUSB0`, `COM5` is `/dev/ttyUSB0`.
|
||||
- CI builds the app on Linux, on macOS for arm64 and x86_64, and on Linux for Mac OS X 10.4 (PowerPC and i386, `cmake/macosx-legacy.cmake`). On Linux, `winport/test/smoke.sh`
|
||||
starts the app in Xvfb three times: with a TCP line, with a dmodemd
|
||||
that the app starts (`winport/test/dmodemd`, a test dmodemd), and with
|
||||
the built-in modem on a SIP line with no account. For the first two,
|
||||
the test makes sure that the app stays up and sends AT commands to a
|
||||
test modem. For the third, the test makes sure that the app stays up
|
||||
and that the built-in modem writes its data file. On Linux and macOS,
|
||||
CI also makes sure that the built-in modem is in the app.
|
||||
@@ -0,0 +1,82 @@
|
||||
# Download
|
||||
|
||||
The [nightly release](https://github.com/D-Modem/gui/releases/tag/nightly)
|
||||
has the app from the newest build of `main`. A workflow
|
||||
(`.github/workflows/nightly.yml`) replaces the release one time each day.
|
||||
When `main` has no new commits, the release does not change.
|
||||
|
||||
!!! warning "These builds are not stable releases"
|
||||
The files are not signed. Thus Windows SmartScreen and macOS Gatekeeper
|
||||
can show a warning before the app starts.
|
||||
|
||||
## Select a file
|
||||
|
||||
| System | File | What it is |
|
||||
| --- | --- | --- |
|
||||
| Windows 95, 98, ME, NT 4.0 and later | [`dmodem-win9x.exe`](https://github.com/D-Modem/gui/releases/download/nightly/dmodem-win9x.exe) | The app with the built-in modem. It needs no C runtime DLL. Windows 95 needs Winsock 2. |
|
||||
| Windows 98 SE, ME, 2000 and later | [`dmodem-win98.exe`](https://github.com/D-Modem/gui/releases/download/nightly/dmodem-win98.exe) | The app with the built-in modem, on `msvcrt.dll`. |
|
||||
| Windows 7 and later | [`dmodem-win7.exe`](https://github.com/D-Modem/gui/releases/download/nightly/dmodem-win7.exe) | The app with the built-in modem. |
|
||||
| Linux, x86_64 (X11) | [`dmodem-win32-linux-x86_64.tar.gz`](https://github.com/D-Modem/gui/releases/download/nightly/dmodem-win32-linux-x86_64.tar.gz) | The app on Linux, with the built-in modem. |
|
||||
| macOS, arm64 (Apple silicon) | [`dmodem-win32-macos-arm64.tar.gz`](https://github.com/D-Modem/gui/releases/download/nightly/dmodem-win32-macos-arm64.tar.gz) | The app on macOS, with the built-in modem. |
|
||||
| macOS, x86_64 (Intel Mac) | [`dmodem-win32-macos-x86_64.tar.gz`](https://github.com/D-Modem/gui/releases/download/nightly/dmodem-win32-macos-x86_64.tar.gz) | The app on macOS, with the built-in modem. |
|
||||
| Mac OS X 10.4 and later, PowerPC and Intel | [`dmodem-win32-macosx10.4.tar.gz`](https://github.com/D-Modem/gui/releases/download/nightly/dmodem-win32-macosx10.4.tar.gz) | The app with the built-in modem. One program for PowerPC (G3, G4, G5) and Intel (i386). It is not tested on a Mac yet. |
|
||||
| Haiku R1/beta6 and later, x86_64 | [`dmodem-win32-haiku-x86_64.tar.gz`](https://github.com/D-Modem/gui/releases/download/nightly/dmodem-win32-haiku-x86_64.tar.gz) | The app on Haiku, with the built-in modem. It needs libusb. |
|
||||
| All | [`SHA256SUMS`](https://github.com/D-Modem/gui/releases/download/nightly/SHA256SUMS) | The SHA-256 of each file. |
|
||||
|
||||
If a file is not in the release, the newest build did not make it. The
|
||||
notes of the release list the files that are not there.
|
||||
|
||||
## Check a file
|
||||
|
||||
1. Download the file and `SHA256SUMS` into the same directory.
|
||||
2. Open a terminal in this directory.
|
||||
3. Type `sha256sum --check --ignore-missing SHA256SUMS`.
|
||||
|
||||
The result for the file must be `OK`.
|
||||
|
||||
## Install on Windows
|
||||
|
||||
1. Download the `.exe` file for your version of Windows.
|
||||
2. Double-click the file. The app does not need an installer.
|
||||
3. If Windows SmartScreen shows a warning, click **More info**, then
|
||||
click **Run anyway**.
|
||||
|
||||
## Install on macOS
|
||||
|
||||
1. Download `dmodem-win32-macos-arm64.tar.gz` for a Mac with Apple
|
||||
silicon, or `dmodem-win32-macos-x86_64.tar.gz` for an Intel Mac.
|
||||
2. Extract the file. In a terminal, type
|
||||
`tar -xzf dmodem-win32-macos-arm64.tar.gz` (or the name of the x86_64
|
||||
file).
|
||||
3. Remove the quarantine attribute. Type
|
||||
`xattr -d com.apple.quarantine dmodem-win32`. The app is not notarized.
|
||||
4. Start the app. Type `./dmodem-win32`.
|
||||
|
||||
## Install on Mac OS X 10.4 to 10.6
|
||||
|
||||
1. Download `dmodem-win32-macosx10.4.tar.gz`. The file is for PowerPC
|
||||
Macs and for Intel Macs.
|
||||
2. Extract the file. In Terminal, type
|
||||
`tar -xzf dmodem-win32-macosx10.4.tar.gz`.
|
||||
3. Start the app. Type `./dmodem-win32`.
|
||||
|
||||
## Install on Linux
|
||||
|
||||
1. Install the libraries. On Ubuntu 24.04, type
|
||||
`sudo apt install libx11-6 libusb-1.0-0 libasound2t64`.
|
||||
2. Download `dmodem-win32-linux-x86_64.tar.gz`.
|
||||
3. Extract the file. Type `tar -xzf dmodem-win32-linux-x86_64.tar.gz`.
|
||||
4. Start the app. Type `./dmodem-win32`.
|
||||
|
||||
## Install on Haiku
|
||||
|
||||
1. Install libusb. In a Terminal, type `pkgman install libusb`.
|
||||
2. Download `dmodem-win32-haiku-x86_64.tar.gz`.
|
||||
3. Extract the file. Type `tar -xzf dmodem-win32-haiku-x86_64.tar.gz`.
|
||||
4. Start the app. Type `./dmodem-win32`.
|
||||
|
||||
For more information, refer to [The app on Haiku](haiku.md).
|
||||
|
||||
## Build the app
|
||||
|
||||
To build the app from the source code, refer to [Building](building.md).
|
||||
@@ -0,0 +1,102 @@
|
||||
# The built-in modem
|
||||
|
||||
The app has the D-Modem modem in it, on Windows (Windows 95 and later),
|
||||
on Linux and on macOS. Thus, the app does not need a separate `dmodemd`.
|
||||
|
||||
## What it does
|
||||
|
||||
The app uses `libdmodem_embed`, the library of `dmodemd` in the D-Modem
|
||||
repository ([docs/embed.md](https://github.com/D-Modem/D-Modem/blob/main/docs/embed.md)).
|
||||
|
||||
1. When you click **Start**, the app starts the modem in a thread of the
|
||||
app.
|
||||
2. The library gives the app a socket. This socket is the DTE of the modem.
|
||||
The app uses the socket as it uses the pty of `dmodemd`.
|
||||
3. When you click **Stop**, the app hangs up the call and stops the thread.
|
||||
|
||||
The built-in modem has these lines: a SIP account (or no account), a USB
|
||||
modem, and a softmodem codec (ALSA, Linux only). For "A modem that already runs", the
|
||||
app opens the device and starts no modem.
|
||||
|
||||
## Differences from dmodemd
|
||||
|
||||
- There is no pty and no `/dev/ttyDM0` link. Other programs cannot use the
|
||||
built-in modem. To share the modem with other programs, start `dmodemd`.
|
||||
- The app does not need root or a pty (Linux and macOS), or a firewall
|
||||
exception for a TCP port (Windows).
|
||||
- A USB modem needs a udev rule that lets the user open the device.
|
||||
- A softmodem codec needs the user in the `audio` group.
|
||||
- The modem keeps the measurements of the line in
|
||||
`~/.config/D-Modem/dmodem0.data` (Linux),
|
||||
`~/Library/Application Support/D-Modem/dmodem0.data` (macOS), or
|
||||
`%APPDATA%\D-Modem\dmodem0.data` (Windows).
|
||||
- The **Log** page shows the log of the modem.
|
||||
|
||||
## How to use dmodemd
|
||||
|
||||
The Line Settings dialog does not have this setting yet. Close the app,
|
||||
and add this line to the `[line]` section of the settings file:
|
||||
|
||||
builtin=0
|
||||
|
||||
The settings file is `%APPDATA%\D-Modem\dmodem.ini` (Windows),
|
||||
`~/.config/D-Modem/dmodem.ini` (Linux) or
|
||||
`~/Library/Application Support/D-Modem/dmodem.ini` (macOS).
|
||||
|
||||
The app then starts `dmodemd` as before.
|
||||
|
||||
## How to build it
|
||||
|
||||
The D-Modem source is a git submodule in `external/D-Modem`. To get it:
|
||||
|
||||
git submodule update --init
|
||||
|
||||
If the submodule is there, CMake builds the built-in modem
|
||||
(`DMODEM_GUI_EMBED`, on by default) for the app on Windows with the
|
||||
clang-retro configurations `win32-picolibc` (the Windows 95 build),
|
||||
`win98`, `winme`, `win7` and later, and for the app on Linux and macOS
|
||||
(`DMODEM_GUI_WINPORT`). To build the app with no built-in
|
||||
modem, use `-DDMODEM_GUI_EMBED=OFF`. For a USB line and an ALSA line, also
|
||||
install `libusb-1.0-0-dev` and `libasound2-dev`.
|
||||
|
||||
The first build downloads libre and Mbed TLS for the SIP line of the modem.
|
||||
|
||||
## Licence
|
||||
|
||||
D-Modem and the GUI are both under the AGPL-3.0-only, or a commercial
|
||||
licence from the D-Modem Authors. Thus the two combine under each licence.
|
||||
The built-in modem links libre (BSD-3-Clause), Mbed TLS (Apache-2.0), and
|
||||
on Linux the libusb and ALSA libraries of the system (LGPL-2.1). On
|
||||
Windows and macOS, the build links libusb (LGPL-2.1) statically, as
|
||||
`dmodemd` does.
|
||||
|
||||
## Status
|
||||
|
||||
| System | Status |
|
||||
| --- | --- |
|
||||
| Windows 98 SE and later | Done (`win98` build). It builds, and its imports agree with Windows 98 SE. It is not tested on Windows or Wine yet. |
|
||||
| Windows 7 and later | Done (`win7` build). It builds. It is not tested on Windows or Wine yet. |
|
||||
| Windows 95, NT 4.0 and later | Done (`win32-picolibc` build). It builds, and its imports agree with Windows 95 and NT 4.0. It is not tested on Windows or Wine yet. Refer to "Windows 95" below. |
|
||||
| Linux | Done. CI builds it, checks that the library is in the app, and starts the built-in modem in Xvfb (`winport/test/smoke.sh`). |
|
||||
| macOS | Done (arm64 and x86_64). CI builds it for each architecture, and checks that the library is in the app. It is not tested on a Mac yet. |
|
||||
| Mac OS X 10.4 and later | Done (PowerPC and i386, one fat program). CI builds it on Linux, and checks that the library is in each part of the program. It is not tested on a Mac yet. |
|
||||
|
||||
### Windows 95
|
||||
|
||||
The Windows 95 build of the app uses the clang-retro configuration
|
||||
`win32-picolibc`. Its C library is picolibc and its C++ library is libc++.
|
||||
Both are linked into the app, so the app does not need `msvcrt.dll`. The
|
||||
modem (C++17) builds with this configuration. D-Modem changes some names
|
||||
of libre and of its own Windows code for picolibc (refer to
|
||||
"picolibc" in D-Modem's `docs/windows-details.md`).
|
||||
|
||||
- On Windows 95, the app needs Winsock 2 (`ws2_32.dll`). Install the
|
||||
Windows Socket 2 Update of Microsoft. The SSH page needs it too.
|
||||
- The modem does not need `iphlpapi.dll`, which Windows 95 does not have.
|
||||
libre finds `GetAdaptersInfo()` at run time.
|
||||
- The `win98` build does not operate on Windows NT 4.0: its libraries use
|
||||
`_vsnprintf_s` of msvcrt.dll, which NT 4.0 does not have. Use the
|
||||
`win32-picolibc` build on NT 4.0.
|
||||
|
||||
On Windows, the modem gives the app a socket on the loopback interface
|
||||
(`127.0.0.1`), because Windows does not have `socketpair()`.
|
||||
@@ -0,0 +1,17 @@
|
||||
# Features
|
||||
|
||||
This page gives the details of the pages and menus of the app. The
|
||||
[README](../README.md) gives a summary.
|
||||
|
||||
- **Remote**: log in to a server by SSH, Telnet or rlogin, in a separate terminal. SSH is built in, on libssh: it uses the keys, agent, config and known hosts of OpenSSH. It can use or import the `.ppk` keys of PuTTY. It uses Pageant on Windows, and an ssh-agent on Linux and macOS. On Windows 95, SSH needs CryptoAPI (Windows 95 OSR2, or Internet Explorer 3 or later)
|
||||
- **Terminal types**: the two terminals use the emulator of the D-Modem app, ported to C:
|
||||
xterm, the VT52 to VT525 of DEC, ANSI-BBS (with Avatar, RIPscrip and Amiga ANSI),
|
||||
the PETSCII of the Commodores, the ATASCII of the Atari 8-bit and the ST, Viewdata,
|
||||
the Minitel and the Tektronix 4014. They have sixel, ReGIS and Tektronix
|
||||
graphics, and the font of each machine (the SyncTERM copies of their ROMs,
|
||||
LGPL-2.0-or-later: `core/licenses/syncterm-fonts-LGPL-2.0.txt`)
|
||||
- **File transfers**: send and receive files on the modem line or on a Telnet, rlogin or SSH login. The protocols are the same as the D-Modem app: ZMODEM (with Automatic Receive when the remote side starts sz, rz or kermit -s, with Crash Recovery, and with zstd compression to the D-Modem BBS and app), YMODEM, YMODEM-G, XMODEM, XMODEM-1K, Kermit, Intel HEX, Motorola S-records and plain text. Use the Transfer menu, or the Send and Receive buttons of the Terminal page
|
||||
- **Copy and capture**: select text with the mouse (Shift and the mouse when the remote side uses the mouse), then copy it. Paste to Host, Select All, Capture Text (to a file), Capture to Printer, and Print (the screen or the selection). Text that the remote side sends to its printer port goes to the printer. Use the Edit and Transfer menus, or click the right mouse button on the terminal
|
||||
- **ASCII Setup** (Modem menu): the same items as HyperTerminal. For the data that you send: Enter sends CR LF, the local echo, and the line delay and character delay of a text file. For the data that you receive: an LF after each CR, 7-bit ASCII, and the wrap of long lines. The settings apply to the modem terminal and to the Remote terminal
|
||||
- **Terminal Settings** (Modem menu): the same items as HyperTerminal, for the modem terminal and for the Remote terminal. They are what Backspace sends (Ctrl+H, Del, or Ctrl+H, Space, Ctrl+H), the function, arrow and Ctrl keys as terminal keys or Windows keys, the backscroll buffer lines, the font, the cursor, the Telnet terminal ID, and Exit program upon disconnecting. The status bar shows the connect timer during a call
|
||||
- **Call menu**: Call (sessions with a name and a number, as the connection files of HyperTerminal), Wait for a Call (ATS0=1), Disconnect, and Send Break (Ctrl+Break: a COM port or a Telnet connection). Line Settings has the COM port settings: bits per second, data bits, parity, stop bits and flow control. **Look for a modem** finds the modem and selects its port (refer to "Look for a modem" in [Design](design.md))
|
||||
@@ -0,0 +1,98 @@
|
||||
# The app on Haiku
|
||||
|
||||
The Windows app also runs on Haiku (x86_64). The app is the same Win32
|
||||
source code. It runs over the portable Win32 layer of
|
||||
[clang-retro MFC](https://github.com/clang-retro/mfc), which shows the
|
||||
windows with Haiku's Interface Kit. The built-in modem is in the app, as
|
||||
on the other systems.
|
||||
|
||||
<img src="images/haiku-modem.png" width="49%" alt="The Modem page of the app on Haiku"> <img src="images/haiku-line-settings.png" width="49%" alt="The Line Settings dialog of the app on Haiku">
|
||||
|
||||
## Requirements
|
||||
|
||||
- Haiku R1/beta6 or later, x86_64. The app was tested on R1/beta6.
|
||||
- libusb. In a Terminal, type `pkgman install libusb`.
|
||||
|
||||
## Install
|
||||
|
||||
1. Download `dmodem-win32-haiku-x86_64.tar.gz` from the
|
||||
[nightly release](https://github.com/D-Modem/gui/releases/tag/nightly).
|
||||
2. Open a Terminal in the download folder.
|
||||
3. Extract the file. Type `tar -xzf dmodem-win32-haiku-x86_64.tar.gz`.
|
||||
4. Start the app. Type `./dmodem-win32`.
|
||||
|
||||
The app keeps its settings in `~/config/settings/libmfc`.
|
||||
|
||||
## Build
|
||||
|
||||
You build the app on Linux. The build uses the system's clang and a
|
||||
sysroot that has Haiku's own packages.
|
||||
|
||||
1. Install the tools. On Ubuntu 24.04, type
|
||||
`sudo apt install cmake ninja-build clang lld llvm libzstd1 python3`.
|
||||
2. Get [clang-retro MFC](https://github.com/clang-retro/mfc) and the
|
||||
D-Modem submodule (`git submodule update --init`).
|
||||
3. Make the sysroot. The script downloads Haiku's packages and examines
|
||||
them against the SHA-256 of their repositories:
|
||||
|
||||
python3 external/D-Modem/platforms/haiku/haiku_sysroot.py haiku-sysroot
|
||||
|
||||
4. Build the app:
|
||||
|
||||
cmake -S . -B build/haiku -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_TOOLCHAIN_FILE=$PWD/cmake/haiku.cmake \
|
||||
-DHAIKU_SYSROOT=$PWD/haiku-sysroot \
|
||||
-DDMODEM_GUI_WINPORT=ON -DCLANG_RETRO_MFC=/path/to/mfc
|
||||
cmake --build build/haiku --target dmodem-win32
|
||||
|
||||
The app is `build/haiku/winport/dmodem-win32`. Copy it to the Haiku
|
||||
computer.
|
||||
|
||||
`cmake/haiku.cmake` includes the toolchain file of D-Modem
|
||||
(`external/D-Modem/platforms/haiku/haiku.cmake`). The CI job
|
||||
`winport-haiku` (`.github/workflows/build.yml`) does these steps. It does
|
||||
not start the app.
|
||||
|
||||
## Serial ports and modems
|
||||
|
||||
Haiku keeps the serial ports in `/dev/ports`:
|
||||
|
||||
- `pc_serial0`, `pc_serial1` and on are the serial ports of the computer.
|
||||
- `usb0`, `usb1` and on are the USB serial adapters and the CDC-ACM USB
|
||||
modems. Haiku's `usb_serial` driver makes these ports.
|
||||
|
||||
In **Line settings**, the **Port** list shows the ports of `/dev/ports`,
|
||||
for example `/dev/ports/usb0`. You can also type `COM1` (the first port
|
||||
by name), `COM2` (the second port), and so on.
|
||||
|
||||
**Look for a modem** does these steps:
|
||||
|
||||
1. It finds the ports in `/dev/ports`.
|
||||
2. It finds the USB devices with Haiku's USB Kit. It gives each `usbN`
|
||||
port the name of its device (the maker and the product).
|
||||
3. If a CDC-ACM device tells that it takes AT commands, the app selects
|
||||
its port as the modem.
|
||||
|
||||
Haiku has no driver for the MultiTech MT9234MU. If you connect this
|
||||
modem, **Look for a modem** tells you that Haiku cannot use it. Use a
|
||||
modem with a serial port, or a CDC-ACM USB modem.
|
||||
|
||||
## Keys
|
||||
|
||||
The keys are those of a PC keyboard:
|
||||
|
||||
| Key on the keyboard | Key in the app |
|
||||
| --- | --- |
|
||||
| Control | Control. For example, Control+C copies. |
|
||||
| Alt (Haiku's Command key, by default) | Alt. For example, Alt+M opens the **Modem** menu. |
|
||||
| Windows (Haiku's Option key) | Windows |
|
||||
|
||||
## Limits
|
||||
|
||||
- The app has no sound on Haiku. The **Phone** page cannot play or
|
||||
record the call.
|
||||
- The app cannot print. Only "Save as PDF" is available.
|
||||
- The Deskbar shows a generic icon for the app.
|
||||
- The USB modems were not tested on Haiku yet. QEMU's USB serial adapter
|
||||
does not operate with Haiku, so the tests used the serial port of the
|
||||
computer only.
|
||||
|
After Width: | Height: | Size: 96 KiB |
@@ -0,0 +1,15 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 500 500">
|
||||
<!-- D-Modem's mark (D-Modem/brand, avatar/org-avatar.svg) on a desktop tile -->
|
||||
<defs>
|
||||
<linearGradient id="books" gradientUnits="userSpaceOnUse" x1="0" y1="81" x2="0" y2="419"><stop offset="0.0000" stop-color="#ff4d5e"/><stop offset="0.1667" stop-color="#ff4d5e"/><stop offset="0.1667" stop-color="#ff8a3d"/><stop offset="0.3333" stop-color="#ff8a3d"/><stop offset="0.3333" stop-color="#ffd23d"/><stop offset="0.5000" stop-color="#ffd23d"/><stop offset="0.5000" stop-color="#3ddc84"/><stop offset="0.6667" stop-color="#3ddc84"/><stop offset="0.6667" stop-color="#3d8bff"/><stop offset="0.8333" stop-color="#3d8bff"/><stop offset="0.8333" stop-color="#a66bff"/><stop offset="1.0000" stop-color="#a66bff"/></linearGradient>
|
||||
<filter id="glow" x="-30%" y="-30%" width="160%" height="160%">
|
||||
<feGaussianBlur stdDeviation="6" result="b"/>
|
||||
<feMerge><feMergeNode in="b"/><feMergeNode in="SourceGraphic"/></feMerge>
|
||||
</filter>
|
||||
</defs>
|
||||
<rect x="10" y="10" width="480" height="480" rx="108" fill="#0a0f1c"/>
|
||||
<g transform="translate(-15 0)">
|
||||
<path d="M140 100 H240 A150 150 0 0 1 240 400 H140 Z" fill="none" stroke="url(#books)" stroke-width="38" stroke-linejoin="round" filter="url(#glow)"/>
|
||||
<path d="M178.0,250.0 L178.8,246.3 L179.5,242.7 L180.3,239.1 L181.1,235.5 L181.8,232.0 L182.6,228.6 L183.4,225.2 L184.1,222.0 L184.9,218.9 L185.7,215.9 L186.4,213.1 L187.2,210.4 L188.0,207.9 L188.7,205.6 L189.5,203.4 L190.3,201.5 L191.0,199.8 L191.8,198.3 L192.6,197.0 L193.3,195.9 L194.1,195.1 L194.9,194.5 L195.6,194.1 L196.4,194.0 L197.2,194.1 L197.9,194.5 L198.7,195.1 L199.5,195.9 L200.2,197.0 L201.0,198.3 L201.8,199.8 L202.5,201.5 L203.3,203.4 L204.1,205.6 L204.8,207.9 L205.6,210.4 L206.4,213.1 L207.1,215.9 L207.9,218.9 L208.7,222.0 L209.4,225.2 L210.2,228.6 L211.0,232.0 L211.7,235.5 L212.5,239.1 L213.3,242.7 L214.0,246.3 L214.8,250.0 L215.6,253.7 L216.3,257.3 L217.1,260.9 L217.9,264.5 L218.6,268.0 L219.4,271.4 L220.2,274.8 L220.9,278.0 L221.7,281.1 L222.5,284.1 L223.2,286.9 L224.0,289.6 L224.8,292.1 L225.5,294.4 L226.3,296.6 L227.1,298.5 L227.8,300.2 L228.6,301.7 L229.4,303.0 L230.1,304.1 L230.9,304.9 L231.7,305.5 L232.4,305.9 L233.2,306.0 L234.0,305.9 L234.7,305.5 L235.5,304.9 L236.3,304.1 L237.0,303.0 L237.8,301.7 L238.6,300.2 L239.3,298.5 L240.1,296.6 L240.9,294.4 L241.6,292.1 L242.4,289.6 L243.2,286.9 L243.9,284.1 L244.7,281.1 L245.5,278.0 L246.2,274.8 L247.0,271.4 L247.8,268.0 L248.5,264.5 L249.3,260.9 L250.1,257.3 L250.8,253.7 L251.6,250.0 L252.4,246.3 L253.1,242.7 L253.9,239.1 L254.7,235.5 L255.4,232.0 L256.2,228.6 L257.0,225.2 L257.7,222.0 L258.5,218.9 L259.3,215.9 L260.0,213.1 L260.8,210.4 L261.6,207.9 L262.3,205.6 L263.1,203.4 L263.9,201.5 L264.6,199.8 L265.4,198.3 L266.2,197.0 L266.9,195.9 L267.7,195.1 L268.5,194.5 L269.2,194.1 L270.0,194.0 H288.4 V306.0 H325.2 V194.0 H362.0" fill="none" stroke="#e8f0ff" stroke-width="16" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.9 KiB |
|
After Width: | Height: | Size: 50 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 52 KiB |
|
Before Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 8.6 KiB |
@@ -0,0 +1,124 @@
|
||||
---
|
||||
title: D-Modem desktop app
|
||||
hide:
|
||||
- navigation
|
||||
- toc
|
||||
---
|
||||
|
||||
<div class="suite-hero" markdown>
|
||||
<img src="images/dmodem.svg" alt="">
|
||||
<div markdown>
|
||||
|
||||
# D-Modem desktop app
|
||||
|
||||
**D-Modem on the desktop: a V.92 modem in a window, from Windows 95 to
|
||||
Windows 11, on macOS and on Linux.**
|
||||
|
||||
The app has the modem built in. You do not need modem hardware or a
|
||||
separate `dmodemd`. Dial a BBS, a fax machine or an ISP from a SIP call,
|
||||
a USB modem or a hardware modem.
|
||||
|
||||
[Download the nightly build](download.md){ .md-button .md-button--primary }
|
||||
[Source on GitHub](https://github.com/D-Modem/gui){ .md-button }
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<img src="images/windows-settings.png" width="49%" alt="The Settings page of the app"> <img src="images/windows-setup-screen.png" width="49%" alt="The Terminal of the app with the setup screen of the modem">
|
||||
|
||||
## What the app does
|
||||
|
||||
<div class="grid cards" markdown>
|
||||
|
||||
- **Modem**
|
||||
|
||||
---
|
||||
|
||||
The state of the call. Dial, answer and hang up. Select the line that
|
||||
the modem uses: a SIP account, a USB modem, or a modem that already
|
||||
runs.
|
||||
|
||||
- **Phone**
|
||||
|
||||
---
|
||||
|
||||
Voice calls through the sound card, with a keypad. An answering machine
|
||||
can take messages on this computer. It is disabled by default.
|
||||
|
||||
- **Terminal**
|
||||
|
||||
---
|
||||
|
||||
The AT console and the setup screens of the modem (`AT!CF`, `AT!CT`).
|
||||
The emulator has xterm, DEC VT52 to VT525, ANSI-BBS, PETSCII, ATASCII,
|
||||
Viewdata, Minitel and Tektronix 4014.
|
||||
|
||||
- **Remote**
|
||||
|
||||
---
|
||||
|
||||
Log in to a server by SSH, Telnet or rlogin. SSH is built in. It uses
|
||||
the keys and the agent of OpenSSH and PuTTY.
|
||||
|
||||
- **Settings**
|
||||
|
||||
---
|
||||
|
||||
Each setting of the modem as a native control, from `AT!M`. You do not
|
||||
have to type AT commands.
|
||||
|
||||
- **Call Stats**
|
||||
|
||||
---
|
||||
|
||||
The rate, the modulation, the error control and the line figures of
|
||||
the last call (`AT#UD`, `ATI11`).
|
||||
|
||||
</div>
|
||||
|
||||
[All the features](features.md){ .md-button }
|
||||
|
||||
<img src="images/windows-modem.png" width="49%" alt="The Modem page of the app"> <img src="images/windows-phone.png" width="49%" alt="The Phone page of the app as it takes a message">
|
||||
|
||||
## One Win32 app, one library
|
||||
|
||||
The app is plain Win32, built with
|
||||
[clang-retro](https://clang-retro.github.io/). The same Win32 code also
|
||||
runs on Linux and macOS, over the portable Win32 layer of
|
||||
[clang-retro MFC](https://github.com/clang-retro/mfc). The app is a thin
|
||||
shell over one small C library, libdmodemctl. Thus it operates in the
|
||||
same way on each system.
|
||||
|
||||
| | Windows | macOS | Linux |
|
||||
| --- | --- | --- | --- |
|
||||
| Toolkit | Win32, C++ | Win32, over the Cocoa layer of clang-retro MFC | Win32, over the X11 and Wayland layer of clang-retro MFC |
|
||||
| Runs on | Windows 95 and later | arm64 (Apple silicon) and x86_64 (Intel Mac). Also Mac OS X 10.4 and later on PowerPC and i386 | x86_64 |
|
||||
| Built-in modem | Yes | Yes | Yes |
|
||||
|
||||
Refer to [Design](design.md).
|
||||
|
||||
## Start here
|
||||
|
||||
- **Use the app:** [download the nightly build](download.md) for your
|
||||
system.
|
||||
- **Build the app:** refer to [Building](building.md).
|
||||
- **Use the modem without dmodemd:** refer to
|
||||
[The built-in modem](embedded-modem.md).
|
||||
- **Learn how the app is built:** refer to [Design](design.md) and to
|
||||
the API of [libdmodemctl](https://github.com/D-Modem/gui/blob/main/core/include/dmodemctl.h).
|
||||
|
||||
## Part of the D-Modem suite
|
||||
|
||||
The desktop app is one part of the [D-Modem suite](https://d-modem.github.io/).
|
||||
The suite has the softmodem ([D-Modem](https://d-modem.github.io/D-Modem/)),
|
||||
the [app for Android and iOS](https://d-modem.github.io/app/), the
|
||||
[pico-serial boards](https://d-modem.github.io/hardware/), and the
|
||||
servers that answer callers ([login](https://d-modem.github.io/login/) and
|
||||
[bbs](https://d-modem.github.io/bbs/)).
|
||||
|
||||
## Licence
|
||||
|
||||
AGPL-3.0-only ([LICENSE](../LICENSE)), or a commercial licence from the
|
||||
D-Modem Authors ([COMMERCIAL-LICENSE.md](../COMMERCIAL-LICENSE.md)).
|
||||
Contributions come with a contributor agreement
|
||||
([CONTRIBUTING.md](../CONTRIBUTING.md)).
|
||||
@@ -0,0 +1,32 @@
|
||||
# The firmware of the MultiTech modems
|
||||
|
||||
| File | For | Size (octets) | SHA-1 | License | Source |
|
||||
|---|---|---|---|---|---|
|
||||
| mts_mt9234mu.fw | MultiTech MT9234MU and MT9234ZBA-USB (TI TUSB3410) | 13769 | `5537e93217c509129d5a870b9fe089cd58647070` | unknown | linux-firmware, before June 2026 (`WHENCE`: "Multi-Tech USB fax modems", "Licence: Unknown") |
|
||||
|
||||
The TUSB3410 of these modems has no firmware of its own. The
|
||||
`ti_usb_3410_5052` driver of Linux downloads this file into the chip each
|
||||
time you connect the modem. The driver looks for the file in
|
||||
`/lib/firmware`. The MT9234ZBA-USB uses the name `mts_mt9234zba.fw` for the
|
||||
same code.
|
||||
|
||||
To install it:
|
||||
|
||||
sudo cp firmware/mts_mt9234mu.fw /lib/firmware/
|
||||
sudo cp firmware/mts_mt9234mu.fw /lib/firmware/mts_mt9234zba.fw
|
||||
|
||||
Then disconnect the modem and connect it again. `cmake --install` puts the
|
||||
file in `share/dmodem-gui/firmware`. With `-DDMODEM_GUI_INSTALL_FIRMWARE=ON`,
|
||||
it also puts the file in `/lib/firmware`.
|
||||
|
||||
Multi-Tech Systems, Inc. gave this file to the Linux kernel in 2010. The
|
||||
file is TI's TUSB3410 UART firmware, compiled again by MultiTech with some
|
||||
changes. The file has no copyright text and no license text. MultiTech did
|
||||
not publish a license for it. In June 2026, linux-firmware removed it with
|
||||
the other files that have an unknown license.
|
||||
|
||||
We include the file so that the modem operates without a search for it. The
|
||||
AGPL-3.0 license of D-Modem and the commercial license do not apply to this
|
||||
file. Multi-Tech Systems, Inc. and Texas Instruments keep their rights in it.
|
||||
If you are the holder of these rights and you do not agree, tell us and we
|
||||
will remove the file.
|
||||
@@ -1,20 +0,0 @@
|
||||
# The Linux (and BSD) app: GTK 4, libadwaita and VTE over libdmodemctl.
|
||||
add_executable(dmodem-gui
|
||||
src/main.c
|
||||
src/window.c
|
||||
src/modem.c
|
||||
src/config.c
|
||||
src/settings.c
|
||||
src/stats.c)
|
||||
target_link_libraries(dmodem-gui PRIVATE dmodemctl PkgConfig::GTK)
|
||||
target_compile_options(dmodem-gui PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
install(TARGETS dmodem-gui RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
install(FILES data/io.github.d_modem.DModem.desktop
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
|
||||
install(FILES data/io.github.d_modem.DModem.metainfo.xml
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo)
|
||||
install(FILES ${PROJECT_SOURCE_DIR}/assets/dmodem.svg
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps
|
||||
RENAME io.github.d_modem.DModem.svg)
|
||||
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* app.h - the Linux app (GTK 4, libadwaita, VTE): what its parts share
|
||||
*/
|
||||
#ifndef DMODEM_GUI_APP_H
|
||||
#define DMODEM_GUI_APP_H
|
||||
|
||||
#include <adwaita.h>
|
||||
#include <vte/vte.h>
|
||||
|
||||
#include "dmodemctl.h"
|
||||
|
||||
#define APP_ID "io.github.d_modem.DModem"
|
||||
|
||||
/* the line settings, kept in ~/.config/dmodem-gui/settings.ini */
|
||||
typedef struct Config {
|
||||
/* "sip", "usb", "alsa", or "device" (a modem that already runs) */
|
||||
char *line;
|
||||
char *sip_user, *sip_password, *sip_server;
|
||||
gboolean listen, t38, v152, v150;
|
||||
char *srtp; /* "", "no" or "require" */
|
||||
char *country; /* dmodemd -c's, "" for its default */
|
||||
char *usb, *alsa; /* --usb's (auto), -a's (hw:1) */
|
||||
char *device; /* the modem's device, "device" line */
|
||||
char *dmodemd; /* its path, "" to look for it */
|
||||
char *init; /* commands sent when the modem opens */
|
||||
int debug; /* dmodemd -d */
|
||||
} Config;
|
||||
|
||||
typedef struct App App;
|
||||
|
||||
/* a command's response */
|
||||
typedef void (*ResponseFn)(App *a, const dmc_event *ev, gpointer data);
|
||||
|
||||
struct App {
|
||||
AdwApplication *gapp;
|
||||
GtkWindow *window;
|
||||
AdwToastOverlay *toasts;
|
||||
Config cfg;
|
||||
|
||||
/* the modem */
|
||||
dmc_session *s;
|
||||
GSubprocess *proc;
|
||||
GCancellable *cancel;
|
||||
char *device; /* the DTE's path */
|
||||
int fd;
|
||||
guint fd_watch, tick;
|
||||
GByteArray *pending; /* written when the DTE takes it */
|
||||
GHashTable *waiting; /* tag -> struct Waiting */
|
||||
int last_state;
|
||||
|
||||
/* the pages' widgets */
|
||||
AdwViewStack *stack;
|
||||
GtkWidget *status_icon, *status_title, *status_detail;
|
||||
GtkWidget *number, *dial_btn, *answer_btn, *hangup_btn;
|
||||
GtkWidget *run_row, *run_btn, *device_row, *copy_btn;
|
||||
VteTerminal *term;
|
||||
GtkWidget *term_buttons;
|
||||
AdwPreferencesPage *settings_page;
|
||||
GPtrArray *settings_groups;
|
||||
GPtrArray *setting_rows; /* by the setting's index */
|
||||
int settings_gen; /* a new model: older answers ignored */
|
||||
GtkWidget *settings_status, *init_row;
|
||||
dmc_model *model;
|
||||
gboolean settings_quiet; /* (rows set by the app, not the user) */
|
||||
AdwPreferencesGroup *stats_group;
|
||||
GPtrArray *stats_rows;
|
||||
GtkWidget *stats_report, *stats_empty;
|
||||
GtkTextBuffer *log;
|
||||
};
|
||||
|
||||
/* config.c */
|
||||
void config_load(Config *c);
|
||||
void config_save(const Config *c);
|
||||
void config_free(Config *c);
|
||||
/* "SIP: alice@sip.example.com" and the like */
|
||||
char *config_summary(const Config *c);
|
||||
/* the line settings dialog */
|
||||
void prefs_show(App *a);
|
||||
|
||||
/* modem.c */
|
||||
void modem_start(App *a);
|
||||
void modem_stop(App *a);
|
||||
gboolean modem_running(App *a);
|
||||
/* bytes typed in the terminal */
|
||||
void modem_type(App *a, const char *data, gsize n);
|
||||
/* a command; fn (may be NULL) gets its response */
|
||||
int modem_command(App *a, const char *cmd, int flags, ResponseFn fn,
|
||||
gpointer data);
|
||||
/* the session's output, terminal bytes and events handled now */
|
||||
void modem_pump(App *a);
|
||||
void app_log(App *a, const char *line);
|
||||
|
||||
/* window.c */
|
||||
void window_build(App *a);
|
||||
void window_state(App *a);
|
||||
void window_toast(App *a, const char *text);
|
||||
/* the modem's events (the session's DMC_EV_*) */
|
||||
void window_event(App *a, const dmc_event *ev);
|
||||
void window_opened(App *a);
|
||||
void window_closed(App *a);
|
||||
|
||||
/* settings.c */
|
||||
GtkWidget *settings_page_new(App *a);
|
||||
void settings_load(App *a);
|
||||
void settings_clear(App *a);
|
||||
void settings_sensitive(App *a, gboolean on);
|
||||
|
||||
/* stats.c */
|
||||
GtkWidget *stats_page_new(App *a);
|
||||
void stats_refresh(App *a);
|
||||
|
||||
#endif
|
||||
@@ -1,316 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* config.c - the line settings: kept, and the dialog that changes them
|
||||
*
|
||||
* They are a key file only the user may read (the SIP password is in
|
||||
* it), ~/.config/dmodem-gui/settings.ini.
|
||||
*/
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#include "app.h"
|
||||
|
||||
static char *path(void)
|
||||
{
|
||||
return g_build_filename(g_get_user_config_dir(), "dmodem-gui",
|
||||
"settings.ini", NULL);
|
||||
}
|
||||
|
||||
static char *get_s(GKeyFile *k, const char *key, const char *def)
|
||||
{
|
||||
char *v = g_key_file_get_string(k, "line", key, NULL);
|
||||
return v ? v : g_strdup(def);
|
||||
}
|
||||
|
||||
static gboolean get_b(GKeyFile *k, const char *key, gboolean def)
|
||||
{
|
||||
GError *e = NULL;
|
||||
gboolean v = g_key_file_get_boolean(k, "line", key, &e);
|
||||
if (e) {
|
||||
g_error_free(e);
|
||||
return def;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
void config_load(Config *c)
|
||||
{
|
||||
GKeyFile *k = g_key_file_new();
|
||||
char *p = path();
|
||||
g_key_file_load_from_file(k, p, G_KEY_FILE_NONE, NULL);
|
||||
g_free(p);
|
||||
c->line = get_s(k, "line", "sip");
|
||||
c->sip_user = get_s(k, "sip-user", "");
|
||||
c->sip_password = get_s(k, "sip-password", "");
|
||||
c->sip_server = get_s(k, "sip-server", "");
|
||||
c->listen = get_b(k, "listen", FALSE);
|
||||
c->t38 = get_b(k, "t38", TRUE);
|
||||
c->v152 = get_b(k, "v152", TRUE);
|
||||
c->v150 = get_b(k, "v150", FALSE);
|
||||
c->srtp = get_s(k, "srtp", "");
|
||||
c->country = get_s(k, "country", "");
|
||||
c->usb = get_s(k, "usb", "auto");
|
||||
c->alsa = get_s(k, "alsa", "hw:1");
|
||||
c->device = get_s(k, "device", "/dev/ttyDM0");
|
||||
c->dmodemd = get_s(k, "dmodemd", "");
|
||||
c->init = get_s(k, "init", "");
|
||||
c->debug = g_key_file_get_integer(k, "line", "debug", NULL);
|
||||
g_key_file_unref(k);
|
||||
}
|
||||
|
||||
void config_save(const Config *c)
|
||||
{
|
||||
GKeyFile *k = g_key_file_new();
|
||||
g_key_file_set_string(k, "line", "line", c->line);
|
||||
g_key_file_set_string(k, "line", "sip-user", c->sip_user);
|
||||
g_key_file_set_string(k, "line", "sip-password", c->sip_password);
|
||||
g_key_file_set_string(k, "line", "sip-server", c->sip_server);
|
||||
g_key_file_set_boolean(k, "line", "listen", c->listen);
|
||||
g_key_file_set_boolean(k, "line", "t38", c->t38);
|
||||
g_key_file_set_boolean(k, "line", "v152", c->v152);
|
||||
g_key_file_set_boolean(k, "line", "v150", c->v150);
|
||||
g_key_file_set_string(k, "line", "srtp", c->srtp);
|
||||
g_key_file_set_string(k, "line", "country", c->country);
|
||||
g_key_file_set_string(k, "line", "usb", c->usb);
|
||||
g_key_file_set_string(k, "line", "alsa", c->alsa);
|
||||
g_key_file_set_string(k, "line", "device", c->device);
|
||||
g_key_file_set_string(k, "line", "dmodemd", c->dmodemd);
|
||||
g_key_file_set_string(k, "line", "init", c->init);
|
||||
g_key_file_set_integer(k, "line", "debug", c->debug);
|
||||
char *p = path();
|
||||
char *dir = g_path_get_dirname(p);
|
||||
g_mkdir_with_parents(dir, 0700);
|
||||
GError *e = NULL;
|
||||
if (!g_key_file_save_to_file(k, p, &e)) {
|
||||
g_warning("%s: %s", p, e->message);
|
||||
g_error_free(e);
|
||||
}
|
||||
g_chmod(p, 0600);
|
||||
g_free(dir);
|
||||
g_free(p);
|
||||
g_key_file_unref(k);
|
||||
}
|
||||
|
||||
void config_free(Config *c)
|
||||
{
|
||||
g_free(c->line);
|
||||
g_free(c->sip_user);
|
||||
g_free(c->sip_password);
|
||||
g_free(c->sip_server);
|
||||
g_free(c->srtp);
|
||||
g_free(c->country);
|
||||
g_free(c->usb);
|
||||
g_free(c->alsa);
|
||||
g_free(c->device);
|
||||
g_free(c->dmodemd);
|
||||
g_free(c->init);
|
||||
}
|
||||
|
||||
char *config_summary(const Config *c)
|
||||
{
|
||||
if (!g_strcmp0(c->line, "usb"))
|
||||
return g_strdup_printf("USB modem (%s)", c->usb);
|
||||
if (!g_strcmp0(c->line, "alsa"))
|
||||
return g_strdup_printf("Softmodem codec %s", c->alsa);
|
||||
if (!g_strcmp0(c->line, "device"))
|
||||
return g_strdup_printf("The modem at %s", c->device);
|
||||
if (!*c->sip_user || !*c->sip_server)
|
||||
return g_strdup("SIP account not set up");
|
||||
return g_strdup_printf("SIP: %s@%s%s", c->sip_user, c->sip_server,
|
||||
c->listen ? ", taking calls" : "");
|
||||
}
|
||||
|
||||
/*
|
||||
* the dialog
|
||||
*/
|
||||
|
||||
static const char *const lines[] = { "sip", "usb", "alsa", "device", NULL };
|
||||
static const char *const line_labels[] = {
|
||||
"SIP account", "USB modem", "Softmodem codec (ALSA)",
|
||||
"A modem that already runs", NULL
|
||||
};
|
||||
static const char *const srtps[] = { "", "no", "require", NULL };
|
||||
static const char *const srtp_labels[] = {
|
||||
"When the other end offers it", "Never", "Always (refuse plain RTP)",
|
||||
NULL
|
||||
};
|
||||
|
||||
typedef struct Prefs {
|
||||
App *a;
|
||||
AdwComboRow *line, *srtp;
|
||||
AdwEntryRow *user, *server, *country, *usb, *alsa, *device, *dmodemd,
|
||||
*init;
|
||||
AdwPasswordEntryRow *password;
|
||||
AdwSwitchRow *listen, *t38, *v152, *v150;
|
||||
AdwSpinRow *debug;
|
||||
AdwPreferencesGroup *sip, *usb_g, *alsa_g, *device_g;
|
||||
} Prefs;
|
||||
|
||||
static int index_of(const char *const *v, const char *s)
|
||||
{
|
||||
for (int i = 0; v[i]; i++)
|
||||
if (!g_strcmp0(v[i], s))
|
||||
return i;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void show_groups(Prefs *p)
|
||||
{
|
||||
const char *l = lines[adw_combo_row_get_selected(p->line)];
|
||||
gtk_widget_set_visible(GTK_WIDGET(p->sip), !g_strcmp0(l, "sip"));
|
||||
gtk_widget_set_visible(GTK_WIDGET(p->usb_g), !g_strcmp0(l, "usb"));
|
||||
gtk_widget_set_visible(GTK_WIDGET(p->alsa_g), !g_strcmp0(l, "alsa"));
|
||||
gtk_widget_set_visible(GTK_WIDGET(p->device_g),
|
||||
!g_strcmp0(l, "device"));
|
||||
}
|
||||
|
||||
static void line_changed(GObject *o, GParamSpec *ps, gpointer data)
|
||||
{
|
||||
(void)o;
|
||||
(void)ps;
|
||||
show_groups(data);
|
||||
}
|
||||
|
||||
static AdwEntryRow *entry(AdwPreferencesGroup *g, const char *title,
|
||||
const char *value)
|
||||
{
|
||||
GtkWidget *r = adw_entry_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(r), title);
|
||||
gtk_editable_set_text(GTK_EDITABLE(r), value);
|
||||
adw_preferences_group_add(g, r);
|
||||
return ADW_ENTRY_ROW(r);
|
||||
}
|
||||
|
||||
static AdwSwitchRow *toggle(AdwPreferencesGroup *g, const char *title,
|
||||
const char *sub, gboolean on)
|
||||
{
|
||||
GtkWidget *r = adw_switch_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(r), title);
|
||||
if (sub)
|
||||
adw_action_row_set_subtitle(ADW_ACTION_ROW(r), sub);
|
||||
adw_switch_row_set_active(ADW_SWITCH_ROW(r), on);
|
||||
adw_preferences_group_add(g, r);
|
||||
return ADW_SWITCH_ROW(r);
|
||||
}
|
||||
|
||||
static AdwComboRow *combo(AdwPreferencesGroup *g, const char *title,
|
||||
const char *const *labels, int sel)
|
||||
{
|
||||
GtkWidget *r = adw_combo_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(r), title);
|
||||
GtkStringList *m = gtk_string_list_new((const char *const *)labels);
|
||||
adw_combo_row_set_model(ADW_COMBO_ROW(r), G_LIST_MODEL(m));
|
||||
g_object_unref(m);
|
||||
adw_combo_row_set_selected(ADW_COMBO_ROW(r), (guint)sel);
|
||||
adw_preferences_group_add(g, r);
|
||||
return ADW_COMBO_ROW(r);
|
||||
}
|
||||
|
||||
static AdwPreferencesGroup *group(AdwPreferencesPage *page, const char *title,
|
||||
const char *desc)
|
||||
{
|
||||
GtkWidget *g = adw_preferences_group_new();
|
||||
adw_preferences_group_set_title(ADW_PREFERENCES_GROUP(g), title);
|
||||
if (desc)
|
||||
adw_preferences_group_set_description(ADW_PREFERENCES_GROUP(g),
|
||||
desc);
|
||||
adw_preferences_page_add(page, ADW_PREFERENCES_GROUP(g));
|
||||
return ADW_PREFERENCES_GROUP(g);
|
||||
}
|
||||
|
||||
static char *text_of(AdwEntryRow *r)
|
||||
{
|
||||
return g_strstrip(g_strdup(gtk_editable_get_text(GTK_EDITABLE(r))));
|
||||
}
|
||||
|
||||
static void closed(AdwDialog *d, gpointer data)
|
||||
{
|
||||
(void)d;
|
||||
Prefs *p = data;
|
||||
Config *c = &p->a->cfg;
|
||||
config_free(c);
|
||||
c->line = g_strdup(lines[adw_combo_row_get_selected(p->line)]);
|
||||
c->sip_user = text_of(p->user);
|
||||
c->sip_password = g_strdup(gtk_editable_get_text(GTK_EDITABLE(p->password)));
|
||||
c->sip_server = text_of(p->server);
|
||||
c->listen = adw_switch_row_get_active(p->listen);
|
||||
c->t38 = adw_switch_row_get_active(p->t38);
|
||||
c->v152 = adw_switch_row_get_active(p->v152);
|
||||
c->v150 = adw_switch_row_get_active(p->v150);
|
||||
c->srtp = g_strdup(srtps[adw_combo_row_get_selected(p->srtp)]);
|
||||
c->country = text_of(p->country);
|
||||
c->usb = text_of(p->usb);
|
||||
c->alsa = text_of(p->alsa);
|
||||
c->device = text_of(p->device);
|
||||
c->dmodemd = text_of(p->dmodemd);
|
||||
c->init = text_of(p->init);
|
||||
c->debug = (int)adw_spin_row_get_value(p->debug);
|
||||
config_save(c);
|
||||
window_state(p->a);
|
||||
g_free(p);
|
||||
}
|
||||
|
||||
void prefs_show(App *a)
|
||||
{
|
||||
Config *c = &a->cfg;
|
||||
Prefs *p = g_new0(Prefs, 1);
|
||||
p->a = a;
|
||||
AdwDialog *d = ADW_DIALOG(adw_preferences_dialog_new());
|
||||
adw_dialog_set_title(d, "Line");
|
||||
AdwPreferencesPage *page = ADW_PREFERENCES_PAGE(adw_preferences_page_new());
|
||||
adw_preferences_page_set_title(page, "Line");
|
||||
adw_preferences_page_set_icon_name(page, "network-wired-symbolic");
|
||||
|
||||
AdwPreferencesGroup *g = group(page, "The modem's line",
|
||||
"Where dmodemd's calls go. Changes take effect the next time "
|
||||
"the modem starts.");
|
||||
p->line = combo(g, "Line", line_labels, index_of(lines, c->line));
|
||||
p->country = entry(g, "Country (dmodemd -c, empty for the USA)",
|
||||
c->country);
|
||||
|
||||
p->sip = group(page, "SIP account", "Calls go to sip:<number>@server.");
|
||||
p->user = entry(p->sip, "User name", c->sip_user);
|
||||
GtkWidget *pw = adw_password_entry_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(pw), "Password");
|
||||
gtk_editable_set_text(GTK_EDITABLE(pw), c->sip_password);
|
||||
adw_preferences_group_add(p->sip, pw);
|
||||
p->password = ADW_PASSWORD_ENTRY_ROW(pw);
|
||||
p->server = entry(p->sip, "Server (host or host:port)", c->sip_server);
|
||||
p->listen = toggle(p->sip, "Take calls", "Register, so calls to the "
|
||||
"account ring the modem", c->listen);
|
||||
p->t38 = toggle(p->sip, "Fax over T.38", NULL, c->t38);
|
||||
p->v152 = toggle(p->sip, "Mark calls as modem data (V.152)", NULL,
|
||||
c->v152);
|
||||
p->v150 = toggle(p->sip, "Modem relay (V.150.1)", NULL, c->v150);
|
||||
p->srtp = combo(p->sip, "Encrypted media (SRTP)", srtp_labels,
|
||||
index_of(srtps, c->srtp));
|
||||
|
||||
p->usb_g = group(page, "USB modem", "A SmartUSB56, a pico-serial "
|
||||
"Line or WB, an Agere or an Apple USB modem.");
|
||||
p->usb = entry(p->usb_g, "Device (auto, or bus:address)", c->usb);
|
||||
p->alsa_g = group(page, "Softmodem codec", "An AC'97 or HD-audio "
|
||||
"modem codec through ALSA (aplay -l lists it).");
|
||||
p->alsa = entry(p->alsa_g, "ALSA device", c->alsa);
|
||||
p->device_g = group(page, "A modem that already runs",
|
||||
"dmodemd started as a service, or any modem's "
|
||||
"serial port.");
|
||||
p->device = entry(p->device_g, "Device", c->device);
|
||||
|
||||
AdwPreferencesGroup *adv = group(page, "Advanced", NULL);
|
||||
p->init = entry(adv, "Commands when the modem opens", c->init);
|
||||
p->dmodemd = entry(adv, "dmodemd (empty to look for it)", c->dmodemd);
|
||||
GtkWidget *dbg = adw_spin_row_new_with_range(0, 9, 1);
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(dbg),
|
||||
"Debug messages (dmodemd -d)");
|
||||
adw_spin_row_set_value(ADW_SPIN_ROW(dbg), c->debug);
|
||||
adw_preferences_group_add(adv, dbg);
|
||||
p->debug = ADW_SPIN_ROW(dbg);
|
||||
|
||||
show_groups(p);
|
||||
g_signal_connect(p->line, "notify::selected", G_CALLBACK(line_changed),
|
||||
p);
|
||||
adw_preferences_dialog_add(ADW_PREFERENCES_DIALOG(d), page);
|
||||
g_signal_connect(d, "closed", G_CALLBACK(closed), p);
|
||||
adw_dialog_present(d, GTK_WIDGET(a->window));
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* main.c - D-Modem for Linux and the BSDs: GTK 4 and libadwaita over
|
||||
* libdmodemctl
|
||||
*/
|
||||
#include "app.h"
|
||||
|
||||
void modem_init(App *a);
|
||||
|
||||
static void activate(GApplication *g, gpointer data)
|
||||
{
|
||||
App *a = data;
|
||||
if (a->window) {
|
||||
gtk_window_present(a->window);
|
||||
return;
|
||||
}
|
||||
window_build(a);
|
||||
gtk_window_present(a->window);
|
||||
/* a modem that runs already, or one this app starts */
|
||||
if (!g_strcmp0(a->cfg.line, "device") ||
|
||||
(!g_strcmp0(a->cfg.line, "sip") && *a->cfg.sip_user))
|
||||
modem_start(a);
|
||||
(void)g;
|
||||
}
|
||||
|
||||
static void shutdown(GApplication *g, gpointer data)
|
||||
{
|
||||
App *a = data;
|
||||
(void)g;
|
||||
if (modem_running(a))
|
||||
modem_stop(a);
|
||||
g_cancellable_cancel(a->cancel);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
App a = { 0 };
|
||||
config_load(&a.cfg);
|
||||
modem_init(&a);
|
||||
a.gapp = adw_application_new(APP_ID, G_APPLICATION_DEFAULT_FLAGS);
|
||||
g_signal_connect(a.gapp, "activate", G_CALLBACK(activate), &a);
|
||||
g_signal_connect(a.gapp, "shutdown", G_CALLBACK(shutdown), &a);
|
||||
int r = g_application_run(G_APPLICATION(a.gapp), argc, argv);
|
||||
g_object_unref(a.gapp);
|
||||
return r;
|
||||
}
|
||||
@@ -1,429 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* modem.c - dmodemd started and stopped, its pty opened, and the bytes
|
||||
* between the pty, libdmodemctl's session and the terminal
|
||||
*
|
||||
* dmodemd runs as the user: it makes its pty and says where it is
|
||||
* ("Use `/dev/pts/3' as modem device"), and the app opens that. With
|
||||
* the "device" line nothing is started and the given device is opened.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <grp.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <glib-unix.h>
|
||||
|
||||
#include "app.h"
|
||||
|
||||
typedef struct Waiting {
|
||||
ResponseFn fn;
|
||||
gpointer data;
|
||||
} Waiting;
|
||||
|
||||
#define LOG_LINES 5000
|
||||
|
||||
void app_log(App *a, const char *line)
|
||||
{
|
||||
GtkTextIter end;
|
||||
gtk_text_buffer_get_end_iter(a->log, &end);
|
||||
gtk_text_buffer_insert(a->log, &end, line, -1);
|
||||
gtk_text_buffer_insert(a->log, &end, "\n", 1);
|
||||
int n = gtk_text_buffer_get_line_count(a->log);
|
||||
if (n > LOG_LINES) {
|
||||
GtkTextIter s, e;
|
||||
gtk_text_buffer_get_start_iter(a->log, &s);
|
||||
gtk_text_buffer_get_iter_at_line(a->log, &e, n - LOG_LINES);
|
||||
gtk_text_buffer_delete(a->log, &s, &e);
|
||||
}
|
||||
}
|
||||
|
||||
static int64_t now_ms(void)
|
||||
{
|
||||
return g_get_monotonic_time() / 1000;
|
||||
}
|
||||
|
||||
static void flush(App *a)
|
||||
{
|
||||
while (a->fd >= 0 && a->pending->len) {
|
||||
ssize_t n = write(a->fd, a->pending->data, a->pending->len);
|
||||
if (n > 0) {
|
||||
g_byte_array_remove_range(a->pending, 0, (guint)n);
|
||||
continue;
|
||||
}
|
||||
if (n < 0 && errno == EINTR)
|
||||
continue;
|
||||
break; /* EAGAIN: the next tick */
|
||||
}
|
||||
}
|
||||
|
||||
void modem_pump(App *a)
|
||||
{
|
||||
char buf[4096];
|
||||
int n;
|
||||
dmc_poll(a->s, now_ms());
|
||||
while ((n = dmc_output(a->s, buf, sizeof(buf))) > 0)
|
||||
g_byte_array_append(a->pending, (guint8 *)buf, (guint)n);
|
||||
flush(a);
|
||||
while ((n = dmc_terminal(a->s, buf, sizeof(buf))) > 0)
|
||||
vte_terminal_feed(a->term, buf, n);
|
||||
dmc_event ev;
|
||||
while (dmc_next_event(a->s, &ev)) {
|
||||
if (ev.type == DMC_EV_RESPONSE) {
|
||||
Waiting *w = g_hash_table_lookup(a->waiting,
|
||||
GINT_TO_POINTER(ev.tag));
|
||||
if (w) {
|
||||
Waiting copy = *w;
|
||||
g_hash_table_remove(a->waiting,
|
||||
GINT_TO_POINTER(ev.tag));
|
||||
if (copy.fn)
|
||||
copy.fn(a, &ev, copy.data);
|
||||
}
|
||||
} else {
|
||||
window_event(a, &ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean tick(gpointer data)
|
||||
{
|
||||
modem_pump(data);
|
||||
return G_SOURCE_CONTINUE;
|
||||
}
|
||||
|
||||
static void close_dte(App *a)
|
||||
{
|
||||
if (a->fd_watch) {
|
||||
g_source_remove(a->fd_watch);
|
||||
a->fd_watch = 0;
|
||||
}
|
||||
if (a->fd >= 0) {
|
||||
close(a->fd);
|
||||
a->fd = -1;
|
||||
}
|
||||
g_byte_array_set_size(a->pending, 0);
|
||||
g_hash_table_remove_all(a->waiting);
|
||||
if (dmc_state(a->s) != DMC_CLOSED) {
|
||||
dmc_close(a->s);
|
||||
modem_pump(a);
|
||||
window_closed(a);
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean readable(gint fd, GIOCondition cond, gpointer data)
|
||||
{
|
||||
App *a = data;
|
||||
char buf[4096];
|
||||
(void)cond;
|
||||
for (;;) {
|
||||
ssize_t n = read(fd, buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
dmc_feed(a->s, buf, (int)n);
|
||||
continue;
|
||||
}
|
||||
if (n < 0 && errno == EINTR)
|
||||
continue;
|
||||
if (n < 0 && errno == EAGAIN)
|
||||
break;
|
||||
/* EOF, or EIO when dmodemd closed its end */
|
||||
a->fd_watch = 0;
|
||||
app_log(a, "The modem's device closed.");
|
||||
close_dte(a);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
modem_pump(a);
|
||||
return G_SOURCE_CONTINUE;
|
||||
}
|
||||
|
||||
static gboolean open_dte(App *a, const char *path)
|
||||
{
|
||||
int fd = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
char *m = g_strdup_printf("Can't open %s: %s", path,
|
||||
g_strerror(errno));
|
||||
app_log(a, m);
|
||||
window_toast(a, m);
|
||||
g_free(m);
|
||||
return FALSE;
|
||||
}
|
||||
struct termios t;
|
||||
if (!tcgetattr(fd, &t)) {
|
||||
cfmakeraw(&t);
|
||||
cfsetispeed(&t, B115200);
|
||||
cfsetospeed(&t, B115200);
|
||||
t.c_cflag |= CLOCAL | CREAD;
|
||||
tcsetattr(fd, TCSANOW, &t);
|
||||
}
|
||||
a->fd = fd;
|
||||
g_free(a->device);
|
||||
a->device = g_strdup(path);
|
||||
a->fd_watch = g_unix_fd_add(fd, G_IO_IN | G_IO_HUP | G_IO_ERR,
|
||||
readable, a);
|
||||
dmc_open(a->s, now_ms());
|
||||
window_opened(a);
|
||||
/* the user's own commands first */
|
||||
if (*a->cfg.init)
|
||||
modem_command(a, a->cfg.init, 0, NULL, NULL);
|
||||
modem_pump(a);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* dmodemd
|
||||
*/
|
||||
|
||||
static char *find_dmodemd(const Config *c)
|
||||
{
|
||||
if (*c->dmodemd)
|
||||
return g_strdup(c->dmodemd);
|
||||
char *p = g_find_program_in_path("dmodemd");
|
||||
if (p)
|
||||
return p;
|
||||
static const char *const places[] = {
|
||||
"/usr/local/sbin/dmodemd", "/usr/sbin/dmodemd",
|
||||
"/usr/local/bin/dmodemd", "/app/bin/dmodemd", NULL
|
||||
};
|
||||
for (int i = 0; places[i]; i++)
|
||||
if (g_file_test(places[i], G_FILE_TEST_IS_EXECUTABLE))
|
||||
return g_strdup(places[i]);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
typedef struct Reader {
|
||||
App *a;
|
||||
GDataInputStream *in;
|
||||
} Reader;
|
||||
|
||||
static void read_line(Reader *r);
|
||||
|
||||
static void line_read(GObject *src, GAsyncResult *res, gpointer data)
|
||||
{
|
||||
Reader *r = data;
|
||||
gsize len;
|
||||
GError *e = NULL;
|
||||
char *line = g_data_input_stream_read_line_finish_utf8(
|
||||
G_DATA_INPUT_STREAM(src), res, &len, &e);
|
||||
if (!line) {
|
||||
/* the end of dmodemd's output (or cancelled: the app may be
|
||||
gone, so nothing of it is touched) */
|
||||
if (e)
|
||||
g_error_free(e);
|
||||
g_object_unref(r->in);
|
||||
g_free(r);
|
||||
return;
|
||||
}
|
||||
App *a = r->a;
|
||||
app_log(a, line);
|
||||
char dev[512];
|
||||
if (dmc_daemon_ready(line, dev, sizeof(dev)) && a->fd < 0)
|
||||
open_dte(a, dev);
|
||||
else if (dmc_daemon_error(line) && a->fd < 0 &&
|
||||
!strstr(line, "group") && !strstr(line, "symbolic link"))
|
||||
window_toast(a, line + 7);
|
||||
g_free(line);
|
||||
read_line(r);
|
||||
}
|
||||
|
||||
static void read_line(Reader *r)
|
||||
{
|
||||
g_data_input_stream_read_line_async(r->in, G_PRIORITY_DEFAULT,
|
||||
r->a->cancel, line_read, r);
|
||||
}
|
||||
|
||||
static void watch(App *a, GInputStream *s)
|
||||
{
|
||||
Reader *r = g_new0(Reader, 1);
|
||||
r->a = a;
|
||||
r->in = g_data_input_stream_new(s);
|
||||
read_line(r);
|
||||
}
|
||||
|
||||
static void exited(GObject *src, GAsyncResult *res, gpointer data)
|
||||
{
|
||||
GSubprocess *p = G_SUBPROCESS(src);
|
||||
GError *e = NULL;
|
||||
if (!g_subprocess_wait_finish(p, res, &e)) {
|
||||
/* cancelled: the app is closing */
|
||||
g_error_free(e);
|
||||
g_object_unref(p);
|
||||
return;
|
||||
}
|
||||
App *a = data;
|
||||
char *m;
|
||||
if (g_subprocess_get_if_signaled(p))
|
||||
m = g_strdup_printf("dmodemd stopped (signal %d).",
|
||||
g_subprocess_get_term_sig(p));
|
||||
else
|
||||
m = g_strdup_printf("dmodemd stopped (exit status %d).",
|
||||
g_subprocess_get_exit_status(p));
|
||||
app_log(a, m);
|
||||
if (a->proc == p) {
|
||||
g_clear_object(&a->proc);
|
||||
close_dte(a);
|
||||
if (!g_subprocess_get_if_signaled(p) ||
|
||||
g_subprocess_get_term_sig(p) != SIGTERM)
|
||||
window_toast(a, m);
|
||||
}
|
||||
g_free(m);
|
||||
g_object_unref(p);
|
||||
window_state(a);
|
||||
}
|
||||
|
||||
static void setenv_if(GSubprocessLauncher *l, const char *name,
|
||||
gboolean set, const char *value)
|
||||
{
|
||||
if (set)
|
||||
g_subprocess_launcher_setenv(l, name, value, TRUE);
|
||||
}
|
||||
|
||||
void modem_start(App *a)
|
||||
{
|
||||
Config *c = &a->cfg;
|
||||
if (modem_running(a))
|
||||
return;
|
||||
if (!g_strcmp0(c->line, "device")) {
|
||||
open_dte(a, c->device);
|
||||
window_state(a);
|
||||
return;
|
||||
}
|
||||
char *exe = find_dmodemd(c);
|
||||
if (!exe) {
|
||||
window_toast(a, "dmodemd was not found: install D-Modem, or "
|
||||
"set its path in the line settings");
|
||||
return;
|
||||
}
|
||||
if (!g_strcmp0(c->line, "sip") &&
|
||||
(!*c->sip_user || !*c->sip_server)) {
|
||||
window_toast(a, "Set up the SIP account first");
|
||||
prefs_show(a);
|
||||
g_free(exe);
|
||||
return;
|
||||
}
|
||||
GPtrArray *argv = g_ptr_array_new_with_free_func(g_free);
|
||||
g_ptr_array_add(argv, exe);
|
||||
if (*c->country) {
|
||||
g_ptr_array_add(argv, g_strdup("-c"));
|
||||
g_ptr_array_add(argv, g_strdup(c->country));
|
||||
}
|
||||
/* (the pty in the user's own group: the default, dialout, would
|
||||
need root) */
|
||||
struct group *gr = getgrgid(getgid());
|
||||
if (gr) {
|
||||
g_ptr_array_add(argv, g_strdup("-g"));
|
||||
g_ptr_array_add(argv, g_strdup(gr->gr_name));
|
||||
}
|
||||
if (c->debug > 0)
|
||||
g_ptr_array_add(argv, g_strdup_printf("-d%d", c->debug));
|
||||
if (!g_strcmp0(c->line, "usb")) {
|
||||
g_ptr_array_add(argv, g_strdup_printf("--usb=%s", c->usb));
|
||||
} else if (!g_strcmp0(c->line, "alsa")) {
|
||||
g_ptr_array_add(argv, g_strdup("-a"));
|
||||
g_ptr_array_add(argv, g_strdup(c->alsa));
|
||||
} else if (c->listen) {
|
||||
g_ptr_array_add(argv, g_strdup("-L"));
|
||||
}
|
||||
g_ptr_array_add(argv, NULL);
|
||||
|
||||
GSubprocessLauncher *l = g_subprocess_launcher_new(
|
||||
G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_PIPE);
|
||||
if (!g_strcmp0(c->line, "sip")) {
|
||||
char *login = g_strdup_printf("%s:%s@%s", c->sip_user,
|
||||
c->sip_password, c->sip_server);
|
||||
g_subprocess_launcher_setenv(l, "SIP_LOGIN", login, TRUE);
|
||||
g_free(login);
|
||||
}
|
||||
setenv_if(l, "SIP_T38", !c->t38, "no");
|
||||
setenv_if(l, "SIP_V152", !c->v152, "no");
|
||||
setenv_if(l, "SIP_V150", c->v150, "yes");
|
||||
setenv_if(l, "SIP_SRTP", *c->srtp, c->srtp);
|
||||
/* its log files where the user may write them */
|
||||
char *dir = g_build_filename(g_get_user_cache_dir(), "dmodem-gui", NULL);
|
||||
g_mkdir_with_parents(dir, 0700);
|
||||
g_subprocess_launcher_set_cwd(l, dir);
|
||||
g_free(dir);
|
||||
|
||||
GError *e = NULL;
|
||||
a->proc = g_subprocess_launcher_spawnv(l, (const char *const *)argv->pdata,
|
||||
&e);
|
||||
g_object_unref(l);
|
||||
char *shown = g_strjoinv(" ", (char **)argv->pdata);
|
||||
g_ptr_array_unref(argv);
|
||||
if (!a->proc) {
|
||||
char *m = g_strdup_printf("Can't start dmodemd: %s", e->message);
|
||||
window_toast(a, m);
|
||||
app_log(a, m);
|
||||
g_free(m);
|
||||
g_error_free(e);
|
||||
g_free(shown);
|
||||
window_state(a);
|
||||
return;
|
||||
}
|
||||
char *m = g_strdup_printf("$ %s", shown);
|
||||
app_log(a, m);
|
||||
g_free(m);
|
||||
g_free(shown);
|
||||
watch(a, g_subprocess_get_stderr_pipe(a->proc));
|
||||
watch(a, g_subprocess_get_stdout_pipe(a->proc));
|
||||
g_subprocess_wait_async(g_object_ref(a->proc), a->cancel, exited, a);
|
||||
window_state(a);
|
||||
}
|
||||
|
||||
void modem_stop(App *a)
|
||||
{
|
||||
if (a->proc) {
|
||||
/* hang up first, so the call ends cleanly */
|
||||
if (a->fd >= 0)
|
||||
dmc_hangup(a->s);
|
||||
modem_pump(a);
|
||||
close_dte(a);
|
||||
g_subprocess_send_signal(a->proc, SIGTERM);
|
||||
} else {
|
||||
close_dte(a);
|
||||
}
|
||||
window_state(a);
|
||||
}
|
||||
|
||||
gboolean modem_running(App *a)
|
||||
{
|
||||
return a->proc != NULL || a->fd >= 0;
|
||||
}
|
||||
|
||||
void modem_type(App *a, const char *data, gsize n)
|
||||
{
|
||||
if (a->fd < 0)
|
||||
return;
|
||||
dmc_type(a->s, data, (int)n);
|
||||
modem_pump(a);
|
||||
}
|
||||
|
||||
int modem_command(App *a, const char *cmd, int flags, ResponseFn fn,
|
||||
gpointer data)
|
||||
{
|
||||
int tag = dmc_command(a->s, cmd, flags, 0);
|
||||
if (tag > 0) {
|
||||
Waiting *w = g_new0(Waiting, 1);
|
||||
w->fn = fn;
|
||||
w->data = data;
|
||||
g_hash_table_insert(a->waiting, GINT_TO_POINTER(tag), w);
|
||||
modem_pump(a);
|
||||
}
|
||||
return tag;
|
||||
}
|
||||
|
||||
/* the ticks the session's timers need */
|
||||
void modem_init(App *a);
|
||||
void modem_init(App *a)
|
||||
{
|
||||
a->s = dmc_session_new();
|
||||
a->fd = -1;
|
||||
a->pending = g_byte_array_new();
|
||||
a->waiting = g_hash_table_new_full(g_direct_hash, g_direct_equal,
|
||||
NULL, g_free);
|
||||
a->cancel = g_cancellable_new();
|
||||
a->tick = g_timeout_add(50, tick, a);
|
||||
}
|
||||
@@ -1,355 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* settings.c - the Settings page: the modem's own settings (AT!M), one
|
||||
* row each, grouped as the configurators group them
|
||||
*
|
||||
* A change runs the setting's command at once, as the configurators do;
|
||||
* a value the modem refuses is put back. AT!R's init string, the
|
||||
* commands that set what differs from the defaults, can be kept as the
|
||||
* commands the app sends when the modem opens.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "app.h"
|
||||
|
||||
typedef struct Row {
|
||||
App *a;
|
||||
int i; /* the setting's index in the model */
|
||||
GtkWidget *w;
|
||||
} Row;
|
||||
|
||||
static void set_row(App *a, Row *r, const char *value);
|
||||
static void mark(App *a, Row *r);
|
||||
|
||||
/* a change sent to the modem */
|
||||
typedef struct Change {
|
||||
int gen, i;
|
||||
char *value;
|
||||
} Change;
|
||||
|
||||
static void applied(App *a, const dmc_event *ev, gpointer data)
|
||||
{
|
||||
Change *c = data;
|
||||
if (a->model && c->gen == a->settings_gen) {
|
||||
Row *r = g_ptr_array_index(a->setting_rows, (guint)c->i);
|
||||
if (ev->ok) {
|
||||
dmc_setting_set(a->model, c->i, c->value);
|
||||
mark(a, r);
|
||||
} else {
|
||||
char *m = g_strdup_printf("The modem refused %s",
|
||||
dmc_setting_label(a->model, c->i));
|
||||
window_toast(a, m);
|
||||
g_free(m);
|
||||
set_row(a, r, dmc_setting_value(a->model, c->i));
|
||||
}
|
||||
/* the country sets other settings' defaults too */
|
||||
if (ev->ok && !strcmp(dmc_setting_key(a->model, c->i),
|
||||
"country"))
|
||||
settings_load(a);
|
||||
}
|
||||
g_free(c->value);
|
||||
g_free(c);
|
||||
}
|
||||
|
||||
static void apply(Row *r, const char *value)
|
||||
{
|
||||
App *a = r->a;
|
||||
if (a->settings_quiet || !a->model)
|
||||
return;
|
||||
if (!strcmp(value, dmc_setting_value(a->model, r->i)))
|
||||
return;
|
||||
const char *why = dmc_setting_check(a->model, r->i, value);
|
||||
if (*why) {
|
||||
window_toast(a, why);
|
||||
set_row(a, r, dmc_setting_value(a->model, r->i));
|
||||
return;
|
||||
}
|
||||
Change *c = g_new0(Change, 1);
|
||||
c->gen = a->settings_gen;
|
||||
c->i = r->i;
|
||||
c->value = g_strdup(value);
|
||||
const char *cmd = dmc_setting_command(a->model, r->i, value);
|
||||
if (modem_command(a, cmd, 0, applied, c) < 0) {
|
||||
window_toast(a, "The modem is busy");
|
||||
g_free(c->value);
|
||||
g_free(c);
|
||||
set_row(a, r, dmc_setting_value(a->model, r->i));
|
||||
}
|
||||
}
|
||||
|
||||
static void picked(GObject *o, GParamSpec *ps, gpointer data)
|
||||
{
|
||||
(void)ps;
|
||||
Row *r = data;
|
||||
guint sel = adw_combo_row_get_selected(ADW_COMBO_ROW(o));
|
||||
if (sel == GTK_INVALID_LIST_POSITION || !r->a->model)
|
||||
return;
|
||||
apply(r, dmc_choice_value(r->a->model, r->i, (int)sel));
|
||||
}
|
||||
|
||||
static void spun(GObject *o, GParamSpec *ps, gpointer data)
|
||||
{
|
||||
(void)ps;
|
||||
Row *r = data;
|
||||
char v[32];
|
||||
g_snprintf(v, sizeof(v), "%.0f", adw_spin_row_get_value(ADW_SPIN_ROW(o)));
|
||||
apply(r, v);
|
||||
}
|
||||
|
||||
static void entered(AdwEntryRow *e, gpointer data)
|
||||
{
|
||||
apply(data, gtk_editable_get_text(GTK_EDITABLE(e)));
|
||||
}
|
||||
|
||||
static void set_row(App *a, Row *r, const char *value)
|
||||
{
|
||||
a->settings_quiet = TRUE;
|
||||
switch (dmc_setting_kind(a->model, r->i)) {
|
||||
case DMC_PICK: {
|
||||
int c = dmc_setting_choice_of(a->model, r->i, value);
|
||||
adw_combo_row_set_selected(ADW_COMBO_ROW(r->w),
|
||||
c < 0 ? GTK_INVALID_LIST_POSITION :
|
||||
(guint)c);
|
||||
break;
|
||||
}
|
||||
case DMC_NUMBER:
|
||||
adw_spin_row_set_value(ADW_SPIN_ROW(r->w), atof(value));
|
||||
break;
|
||||
default:
|
||||
gtk_editable_set_text(GTK_EDITABLE(r->w), value);
|
||||
break;
|
||||
}
|
||||
a->settings_quiet = FALSE;
|
||||
}
|
||||
|
||||
/* a row shows when its value is not the default */
|
||||
static void mark(App *a, Row *r)
|
||||
{
|
||||
gboolean changed = strcmp(dmc_setting_value(a->model, r->i),
|
||||
dmc_setting_default(a->model, r->i)) != 0;
|
||||
if (changed)
|
||||
gtk_widget_add_css_class(r->w, "accent");
|
||||
else
|
||||
gtk_widget_remove_css_class(r->w, "accent");
|
||||
}
|
||||
|
||||
static GtkWidget *row_for(App *a, int i)
|
||||
{
|
||||
dmc_model *m = a->model;
|
||||
Row *r = g_new0(Row, 1);
|
||||
r->a = a;
|
||||
r->i = i;
|
||||
switch (dmc_setting_kind(m, i)) {
|
||||
case DMC_PICK: {
|
||||
GtkStringList *l = gtk_string_list_new(NULL);
|
||||
for (int c = 0; c < dmc_setting_choices(m, i); c++)
|
||||
gtk_string_list_append(l, dmc_choice_label(m, i, c));
|
||||
r->w = adw_combo_row_new();
|
||||
adw_combo_row_set_model(ADW_COMBO_ROW(r->w), G_LIST_MODEL(l));
|
||||
g_object_unref(l);
|
||||
if (dmc_setting_choices(m, i) > 12)
|
||||
adw_combo_row_set_enable_search(ADW_COMBO_ROW(r->w), TRUE);
|
||||
set_row(a, r, dmc_setting_value(m, i));
|
||||
g_signal_connect(r->w, "notify::selected", G_CALLBACK(picked), r);
|
||||
break;
|
||||
}
|
||||
case DMC_NUMBER:
|
||||
r->w = adw_spin_row_new_with_range((double)dmc_setting_lo(m, i),
|
||||
(double)dmc_setting_hi(m, i), 1);
|
||||
set_row(a, r, dmc_setting_value(m, i));
|
||||
g_signal_connect(r->w, "notify::value", G_CALLBACK(spun), r);
|
||||
break;
|
||||
default:
|
||||
r->w = adw_entry_row_new();
|
||||
adw_entry_row_set_show_apply_button(ADW_ENTRY_ROW(r->w), TRUE);
|
||||
set_row(a, r, dmc_setting_value(m, i));
|
||||
g_signal_connect(r->w, "apply", G_CALLBACK(entered), r);
|
||||
break;
|
||||
}
|
||||
adw_preferences_row_set_use_markup(ADW_PREFERENCES_ROW(r->w), FALSE);
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(r->w),
|
||||
dmc_setting_label(m, i));
|
||||
if (ADW_IS_ACTION_ROW(r->w))
|
||||
adw_action_row_set_subtitle(ADW_ACTION_ROW(r->w),
|
||||
dmc_setting_help(m, i));
|
||||
else
|
||||
gtk_widget_set_tooltip_text(r->w, dmc_setting_help(m, i));
|
||||
g_object_set_data_full(G_OBJECT(r->w), "row", r, g_free);
|
||||
g_ptr_array_add(a->setting_rows, r);
|
||||
mark(a, r);
|
||||
return r->w;
|
||||
}
|
||||
|
||||
void settings_clear(App *a)
|
||||
{
|
||||
for (guint g = 0; a->settings_groups && g < a->settings_groups->len; g++)
|
||||
adw_preferences_page_remove(a->settings_page,
|
||||
g_ptr_array_index(a->settings_groups, g));
|
||||
if (a->settings_groups)
|
||||
g_ptr_array_set_size(a->settings_groups, 0);
|
||||
if (a->setting_rows)
|
||||
g_ptr_array_set_size(a->setting_rows, 0);
|
||||
a->settings_gen++;
|
||||
dmc_model_free(a->model);
|
||||
a->model = NULL;
|
||||
gtk_label_set_text(GTK_LABEL(a->settings_status),
|
||||
"Start the modem to see its settings.");
|
||||
gtk_widget_set_visible(a->settings_status, TRUE);
|
||||
}
|
||||
|
||||
static void got_init(App *a, const dmc_event *ev, gpointer data)
|
||||
{
|
||||
(void)data;
|
||||
if (!ev->ok)
|
||||
return;
|
||||
/* AT!R's lines, as one line of commands */
|
||||
GString *s = g_string_new(NULL);
|
||||
char **lines = g_strsplit(ev->text, "\n", -1);
|
||||
for (int i = 0; lines[i]; i++) {
|
||||
char *l = g_strstrip(lines[i]);
|
||||
if (!*l || !g_ascii_strcasecmp(l, "AT"))
|
||||
continue;
|
||||
if (s->len) {
|
||||
/* (the next line's commands after this one's) */
|
||||
g_string_append(s, l + 2);
|
||||
} else {
|
||||
g_string_append(s, l);
|
||||
}
|
||||
}
|
||||
g_strfreev(lines);
|
||||
adw_action_row_set_subtitle(ADW_ACTION_ROW(a->init_row),
|
||||
s->len ? s->str : "AT (all at their defaults)");
|
||||
g_object_set_data_full(G_OBJECT(a->init_row), "init",
|
||||
g_string_free(s, FALSE), g_free);
|
||||
}
|
||||
|
||||
static void got_model(App *a, const dmc_event *ev, gpointer data)
|
||||
{
|
||||
(void)data;
|
||||
if (!ev->ok) {
|
||||
gtk_label_set_text(GTK_LABEL(a->settings_status),
|
||||
"This modem does not list its settings "
|
||||
"(AT!M): update D-Modem, or use the "
|
||||
"Terminal's setup screen.");
|
||||
return;
|
||||
}
|
||||
dmc_model *m = dmc_model_parse(ev->text);
|
||||
if (!m) {
|
||||
gtk_label_set_text(GTK_LABEL(a->settings_status),
|
||||
"The modem's settings list was not readable.");
|
||||
return;
|
||||
}
|
||||
settings_clear(a);
|
||||
a->model = m;
|
||||
gtk_widget_set_visible(a->settings_status, FALSE);
|
||||
for (int g = 0; g < dmc_model_groups(m); g++) {
|
||||
const char *name = dmc_model_group(m, g);
|
||||
GtkWidget *grp = adw_preferences_group_new();
|
||||
adw_preferences_group_set_title(ADW_PREFERENCES_GROUP(grp), name);
|
||||
for (int i = 0; i < dmc_model_count(m); i++)
|
||||
if (!strcmp(dmc_setting_group(m, i), name))
|
||||
adw_preferences_group_add(ADW_PREFERENCES_GROUP(grp),
|
||||
row_for(a, i));
|
||||
adw_preferences_page_add(a->settings_page,
|
||||
ADW_PREFERENCES_GROUP(grp));
|
||||
g_ptr_array_add(a->settings_groups, grp);
|
||||
}
|
||||
modem_command(a, "AT!R", DMC_QUIET, got_init, NULL);
|
||||
settings_sensitive(a, dmc_state(a->s) == DMC_IDLE);
|
||||
}
|
||||
|
||||
void settings_load(App *a)
|
||||
{
|
||||
if (dmc_state(a->s) == DMC_CLOSED)
|
||||
return;
|
||||
modem_command(a, "AT!M", DMC_QUIET, got_model, NULL);
|
||||
}
|
||||
|
||||
void settings_sensitive(App *a, gboolean on)
|
||||
{
|
||||
for (guint g = 0; a->settings_groups && g < a->settings_groups->len; g++)
|
||||
gtk_widget_set_sensitive(g_ptr_array_index(a->settings_groups, g),
|
||||
on);
|
||||
}
|
||||
|
||||
static void reload(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
settings_load(data);
|
||||
}
|
||||
|
||||
static void reset_done(App *a, const dmc_event *ev, gpointer data)
|
||||
{
|
||||
(void)data;
|
||||
if (!ev->ok)
|
||||
window_toast(a, "The modem refused ATZ");
|
||||
settings_load(a);
|
||||
}
|
||||
|
||||
static void reset(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
App *a = data;
|
||||
modem_command(a, "ATZ", 0, reset_done, NULL);
|
||||
}
|
||||
|
||||
static void keep_init(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
App *a = data;
|
||||
const char *init = g_object_get_data(G_OBJECT(a->init_row), "init");
|
||||
g_free(a->cfg.init);
|
||||
a->cfg.init = g_strdup(init ? init : "");
|
||||
config_save(&a->cfg);
|
||||
window_toast(a, *a->cfg.init ? "Sent each time the modem opens" :
|
||||
"Nothing to send: every setting is at its default");
|
||||
}
|
||||
|
||||
GtkWidget *settings_page_new(App *a)
|
||||
{
|
||||
a->settings_groups = g_ptr_array_new();
|
||||
a->setting_rows = g_ptr_array_new();
|
||||
a->settings_page = ADW_PREFERENCES_PAGE(adw_preferences_page_new());
|
||||
|
||||
GtkWidget *top = adw_preferences_group_new();
|
||||
adw_preferences_group_set_title(ADW_PREFERENCES_GROUP(top), "The modem's settings");
|
||||
adw_preferences_group_set_description(ADW_PREFERENCES_GROUP(top),
|
||||
"Changes take effect at once. The modem keeps no stored "
|
||||
"profile, so keep the init string to have them each time.");
|
||||
GtkWidget *bar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
|
||||
GtkWidget *b = gtk_button_new_from_icon_name("view-refresh-symbolic");
|
||||
gtk_widget_set_tooltip_text(b, "Read the settings again");
|
||||
gtk_widget_add_css_class(b, "flat");
|
||||
g_signal_connect(b, "clicked", G_CALLBACK(reload), a);
|
||||
gtk_box_append(GTK_BOX(bar), b);
|
||||
b = gtk_button_new_with_label("Defaults");
|
||||
gtk_widget_set_tooltip_text(b, "Every setting back to its default (ATZ)");
|
||||
gtk_widget_add_css_class(b, "flat");
|
||||
g_signal_connect(b, "clicked", G_CALLBACK(reset), a);
|
||||
gtk_box_append(GTK_BOX(bar), b);
|
||||
adw_preferences_group_set_header_suffix(ADW_PREFERENCES_GROUP(top), bar);
|
||||
|
||||
a->init_row = adw_action_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(a->init_row),
|
||||
"Init string");
|
||||
adw_action_row_set_subtitle(ADW_ACTION_ROW(a->init_row), "");
|
||||
adw_action_row_set_subtitle_selectable(ADW_ACTION_ROW(a->init_row), TRUE);
|
||||
gtk_widget_add_css_class(a->init_row, "property");
|
||||
b = gtk_button_new_with_label("Keep");
|
||||
gtk_widget_set_tooltip_text(b, "Send it each time the modem opens");
|
||||
gtk_widget_set_valign(b, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(b, "clicked", G_CALLBACK(keep_init), a);
|
||||
adw_action_row_add_suffix(ADW_ACTION_ROW(a->init_row), b);
|
||||
adw_preferences_group_add(ADW_PREFERENCES_GROUP(top), a->init_row);
|
||||
a->settings_status = gtk_label_new("");
|
||||
gtk_label_set_wrap(GTK_LABEL(a->settings_status), TRUE);
|
||||
gtk_widget_add_css_class(a->settings_status, "dim-label");
|
||||
gtk_widget_set_margin_top(a->settings_status, 12);
|
||||
adw_preferences_group_add(ADW_PREFERENCES_GROUP(top), a->settings_status);
|
||||
adw_preferences_page_add(a->settings_page, ADW_PREFERENCES_GROUP(top));
|
||||
settings_clear(a);
|
||||
return GTK_WIDGET(a->settings_page);
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* stats.c - the Call Stats page: the last call's statistics (AT#UD, as
|
||||
* labelled rows) and the modem's own report of it (ATI11)
|
||||
*/
|
||||
#include "app.h"
|
||||
|
||||
static void got_report(App *a, const dmc_event *ev, gpointer data)
|
||||
{
|
||||
(void)data;
|
||||
gtk_label_set_text(GTK_LABEL(a->stats_report),
|
||||
ev->ok ? ev->text : "");
|
||||
}
|
||||
|
||||
static void got_diag(App *a, const dmc_event *ev, gpointer data)
|
||||
{
|
||||
(void)data;
|
||||
for (guint i = 0; i < a->stats_rows->len; i++)
|
||||
adw_preferences_group_remove(a->stats_group,
|
||||
g_ptr_array_index(a->stats_rows, i));
|
||||
g_ptr_array_set_size(a->stats_rows, 0);
|
||||
if (!ev->ok) {
|
||||
gtk_widget_set_visible(a->stats_empty, TRUE);
|
||||
return;
|
||||
}
|
||||
dmc_diag *d = dmc_diag_parse(ev->text);
|
||||
gboolean call = dmc_diag_has_call(d);
|
||||
gtk_widget_set_visible(a->stats_empty, !call);
|
||||
for (int i = 0; call && i < dmc_diag_count(d); i++) {
|
||||
if (dmc_diag_key(d, i) == 0)
|
||||
continue; /* the report's version */
|
||||
GtkWidget *r = adw_action_row_new();
|
||||
adw_preferences_row_set_use_markup(ADW_PREFERENCES_ROW(r), FALSE);
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(r),
|
||||
dmc_diag_label(d, i));
|
||||
GtkWidget *v = gtk_label_new(dmc_diag_value(d, i));
|
||||
gtk_label_set_selectable(GTK_LABEL(v), TRUE);
|
||||
gtk_widget_add_css_class(v, "dim-label");
|
||||
adw_action_row_add_suffix(ADW_ACTION_ROW(r), v);
|
||||
adw_preferences_group_add(a->stats_group, r);
|
||||
g_ptr_array_add(a->stats_rows, r);
|
||||
}
|
||||
dmc_diag_free(d);
|
||||
}
|
||||
|
||||
void stats_refresh(App *a)
|
||||
{
|
||||
int st = dmc_state(a->s);
|
||||
if (st == DMC_CLOSED || st == DMC_ONLINE)
|
||||
return;
|
||||
modem_command(a, "AT#UD", DMC_QUIET, got_diag, NULL);
|
||||
modem_command(a, "ATI11", DMC_QUIET, got_report, NULL);
|
||||
}
|
||||
|
||||
static void refresh(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
stats_refresh(data);
|
||||
}
|
||||
|
||||
GtkWidget *stats_page_new(App *a)
|
||||
{
|
||||
a->stats_rows = g_ptr_array_new();
|
||||
GtkWidget *page = adw_preferences_page_new();
|
||||
GtkWidget *g = adw_preferences_group_new();
|
||||
a->stats_group = ADW_PREFERENCES_GROUP(g);
|
||||
adw_preferences_group_set_title(a->stats_group, "Last call");
|
||||
adw_preferences_group_set_description(a->stats_group,
|
||||
"As the modem reports it (AT#UD), after each call; during "
|
||||
"one, escape to command mode to see it live.");
|
||||
GtkWidget *b = gtk_button_new_from_icon_name("view-refresh-symbolic");
|
||||
gtk_widget_set_tooltip_text(b, "Read them again");
|
||||
gtk_widget_add_css_class(b, "flat");
|
||||
g_signal_connect(b, "clicked", G_CALLBACK(refresh), a);
|
||||
adw_preferences_group_set_header_suffix(a->stats_group, b);
|
||||
a->stats_empty = gtk_label_new("No call yet.");
|
||||
gtk_widget_add_css_class(a->stats_empty, "dim-label");
|
||||
adw_preferences_group_add(a->stats_group, a->stats_empty);
|
||||
adw_preferences_page_add(ADW_PREFERENCES_PAGE(page), a->stats_group);
|
||||
|
||||
GtkWidget *rg = adw_preferences_group_new();
|
||||
adw_preferences_group_set_title(ADW_PREFERENCES_GROUP(rg),
|
||||
"The modem's report (ATI11)");
|
||||
a->stats_report = gtk_label_new("");
|
||||
gtk_label_set_selectable(GTK_LABEL(a->stats_report), TRUE);
|
||||
gtk_label_set_xalign(GTK_LABEL(a->stats_report), 0);
|
||||
gtk_widget_add_css_class(a->stats_report, "monospace");
|
||||
adw_preferences_group_add(ADW_PREFERENCES_GROUP(rg), a->stats_report);
|
||||
adw_preferences_page_add(ADW_PREFERENCES_PAGE(page),
|
||||
ADW_PREFERENCES_GROUP(rg));
|
||||
return page;
|
||||
}
|
||||
@@ -1,485 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* window.c - the main window: the Modem, Terminal, Settings, Call and
|
||||
* Log pages, the same pages as the Windows and macOS apps
|
||||
* (docs/design.md)
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "app.h"
|
||||
|
||||
void window_toast(App *a, const char *text)
|
||||
{
|
||||
AdwToast *t = adw_toast_new(text);
|
||||
adw_toast_set_timeout(t, 4);
|
||||
adw_toast_overlay_add_toast(a->toasts, t);
|
||||
}
|
||||
|
||||
static const char *state_icon(int state)
|
||||
{
|
||||
switch (state) {
|
||||
case DMC_RINGING:
|
||||
return "call-incoming-symbolic";
|
||||
case DMC_DIALING:
|
||||
case DMC_ANSWERING:
|
||||
return "call-outgoing-symbolic";
|
||||
case DMC_ONLINE:
|
||||
case DMC_ONLINE_COMMAND:
|
||||
return "network-transmit-receive-symbolic";
|
||||
case DMC_CONFIGURATOR:
|
||||
return "preferences-system-symbolic";
|
||||
default:
|
||||
return "call-start-symbolic";
|
||||
}
|
||||
}
|
||||
|
||||
/* the Modem page and the buttons as the state is now */
|
||||
void window_state(App *a)
|
||||
{
|
||||
int st = dmc_state(a->s);
|
||||
gboolean running = modem_running(a);
|
||||
gtk_image_set_from_icon_name(GTK_IMAGE(a->status_icon),
|
||||
running ? state_icon(st) :
|
||||
"network-offline-symbolic");
|
||||
const char *title = running && st == DMC_CLOSED ? "Starting" :
|
||||
dmc_state_name(st);
|
||||
gtk_label_set_text(GTK_LABEL(a->status_title), title);
|
||||
char *detail = NULL;
|
||||
if (st == DMC_ONLINE || st == DMC_ONLINE_COMMAND ||
|
||||
st == DMC_HANGING_UP)
|
||||
detail = g_strdup(dmc_connect_text(a->s));
|
||||
else if (st == DMC_RINGING && *dmc_caller_number(a->s))
|
||||
detail = g_strdup_printf("%s %s", dmc_caller_number(a->s),
|
||||
dmc_caller_name(a->s));
|
||||
else
|
||||
detail = config_summary(&a->cfg);
|
||||
gtk_label_set_text(GTK_LABEL(a->status_detail), detail);
|
||||
g_free(detail);
|
||||
|
||||
gboolean open = st != DMC_CLOSED;
|
||||
gtk_widget_set_sensitive(a->dial_btn, open && st == DMC_IDLE);
|
||||
gtk_widget_set_sensitive(a->number, open);
|
||||
gtk_widget_set_sensitive(a->answer_btn, st == DMC_RINGING);
|
||||
gtk_widget_set_sensitive(a->hangup_btn,
|
||||
st == DMC_DIALING || st == DMC_ANSWERING ||
|
||||
st == DMC_ONLINE || st == DMC_ONLINE_COMMAND ||
|
||||
st == DMC_RINGING);
|
||||
gtk_button_set_label(GTK_BUTTON(a->run_btn), running ? "Stop" : "Start");
|
||||
if (running)
|
||||
gtk_widget_remove_css_class(a->run_btn, "suggested-action");
|
||||
else
|
||||
gtk_widget_add_css_class(a->run_btn, "suggested-action");
|
||||
char *sum = config_summary(&a->cfg);
|
||||
adw_action_row_set_subtitle(ADW_ACTION_ROW(a->run_row), sum);
|
||||
g_free(sum);
|
||||
adw_action_row_set_subtitle(ADW_ACTION_ROW(a->device_row),
|
||||
a->fd >= 0 && a->device ? a->device :
|
||||
"Not running");
|
||||
gtk_widget_set_sensitive(a->copy_btn, a->fd >= 0);
|
||||
gtk_widget_set_sensitive(a->term_buttons, st == DMC_IDLE);
|
||||
settings_sensitive(a, st == DMC_IDLE);
|
||||
}
|
||||
|
||||
void window_opened(App *a)
|
||||
{
|
||||
window_state(a);
|
||||
settings_load(a);
|
||||
stats_refresh(a);
|
||||
}
|
||||
|
||||
void window_closed(App *a)
|
||||
{
|
||||
settings_clear(a);
|
||||
window_state(a);
|
||||
}
|
||||
|
||||
void window_event(App *a, const dmc_event *ev)
|
||||
{
|
||||
switch (ev->type) {
|
||||
case DMC_EV_STATE: {
|
||||
int was = a->last_state;
|
||||
a->last_state = ev->state;
|
||||
window_state(a);
|
||||
/* a call over: its statistics */
|
||||
if (ev->state == DMC_IDLE &&
|
||||
(was == DMC_ONLINE || was == DMC_ONLINE_COMMAND ||
|
||||
was == DMC_HANGING_UP || was == DMC_DIALING ||
|
||||
was == DMC_ANSWERING))
|
||||
stats_refresh(a);
|
||||
/* a configurator over: the settings it changed */
|
||||
if (ev->state == DMC_IDLE && was == DMC_CONFIGURATOR)
|
||||
settings_load(a);
|
||||
break;
|
||||
}
|
||||
case DMC_EV_RESULT:
|
||||
if (!strcmp(ev->result, "RING")) {
|
||||
if (!gtk_window_is_active(a->window)) {
|
||||
GNotification *n = g_notification_new("Incoming call");
|
||||
const char *who = dmc_caller_number(a->s);
|
||||
g_notification_set_body(n, *who ? who :
|
||||
"The modem is ringing");
|
||||
g_application_send_notification(
|
||||
G_APPLICATION(a->gapp), "ring", n);
|
||||
g_object_unref(n);
|
||||
}
|
||||
} else if (ev->result[0]) {
|
||||
window_toast(a, ev->result);
|
||||
}
|
||||
break;
|
||||
case DMC_EV_CALLER:
|
||||
window_state(a);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* the Modem page
|
||||
*/
|
||||
|
||||
static void dialled(App *a, const dmc_event *ev, gpointer data)
|
||||
{
|
||||
(void)data;
|
||||
if (!ev->ok)
|
||||
window_toast(a, ev->result);
|
||||
}
|
||||
|
||||
static void dial(GtkWidget *w, gpointer data)
|
||||
{
|
||||
App *a = data;
|
||||
(void)w;
|
||||
const char *n = gtk_editable_get_text(GTK_EDITABLE(a->number));
|
||||
if (!*n) {
|
||||
window_toast(a, "Enter a number to dial");
|
||||
return;
|
||||
}
|
||||
char *cmd = g_strdup_printf("ATD%s", n);
|
||||
modem_command(a, cmd, 0, dialled, NULL);
|
||||
g_free(cmd);
|
||||
}
|
||||
|
||||
static void answer(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
App *a = data;
|
||||
modem_command(a, "ATA", 0, dialled, NULL);
|
||||
}
|
||||
|
||||
static void hangup(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
App *a = data;
|
||||
dmc_hangup(a->s);
|
||||
modem_pump(a);
|
||||
}
|
||||
|
||||
static void run(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
App *a = data;
|
||||
if (modem_running(a))
|
||||
modem_stop(a);
|
||||
else
|
||||
modem_start(a);
|
||||
}
|
||||
|
||||
static void copy_device(GtkWidget *w, gpointer data)
|
||||
{
|
||||
App *a = data;
|
||||
if (!a->device)
|
||||
return;
|
||||
gdk_clipboard_set_text(gtk_widget_get_clipboard(w), a->device);
|
||||
window_toast(a, "Copied");
|
||||
}
|
||||
|
||||
static void line_settings(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
prefs_show(data);
|
||||
}
|
||||
|
||||
static GtkWidget *modem_page(App *a)
|
||||
{
|
||||
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 24);
|
||||
gtk_widget_set_margin_top(box, 24);
|
||||
gtk_widget_set_margin_bottom(box, 24);
|
||||
gtk_widget_set_margin_start(box, 12);
|
||||
gtk_widget_set_margin_end(box, 12);
|
||||
|
||||
/* the state, large */
|
||||
a->status_icon = gtk_image_new_from_icon_name("network-offline-symbolic");
|
||||
gtk_image_set_pixel_size(GTK_IMAGE(a->status_icon), 96);
|
||||
gtk_widget_add_css_class(a->status_icon, "dim-label");
|
||||
gtk_box_append(GTK_BOX(box), a->status_icon);
|
||||
a->status_title = gtk_label_new("Not connected");
|
||||
gtk_widget_add_css_class(a->status_title, "title-1");
|
||||
gtk_box_append(GTK_BOX(box), a->status_title);
|
||||
a->status_detail = gtk_label_new("");
|
||||
gtk_label_set_wrap(GTK_LABEL(a->status_detail), TRUE);
|
||||
gtk_widget_add_css_class(a->status_detail, "dim-label");
|
||||
gtk_box_append(GTK_BOX(box), a->status_detail);
|
||||
|
||||
/* the call */
|
||||
GtkWidget *call = adw_preferences_group_new();
|
||||
adw_preferences_group_set_title(ADW_PREFERENCES_GROUP(call), "Call");
|
||||
a->number = adw_entry_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(a->number),
|
||||
"Number to dial");
|
||||
gtk_editable_set_text(GTK_EDITABLE(a->number), "");
|
||||
a->dial_btn = gtk_button_new_with_label("Dial");
|
||||
gtk_widget_set_valign(a->dial_btn, GTK_ALIGN_CENTER);
|
||||
gtk_widget_add_css_class(a->dial_btn, "suggested-action");
|
||||
adw_entry_row_add_suffix(ADW_ENTRY_ROW(a->number), a->dial_btn);
|
||||
g_signal_connect(a->dial_btn, "clicked", G_CALLBACK(dial), a);
|
||||
g_signal_connect(a->number, "entry-activated", G_CALLBACK(dial), a);
|
||||
adw_preferences_group_add(ADW_PREFERENCES_GROUP(call), a->number);
|
||||
|
||||
GtkWidget *calls = adw_action_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(calls), "Answer or hang up");
|
||||
a->answer_btn = gtk_button_new_with_label("Answer");
|
||||
gtk_widget_set_valign(a->answer_btn, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(a->answer_btn, "clicked", G_CALLBACK(answer), a);
|
||||
a->hangup_btn = gtk_button_new_with_label("Hang Up");
|
||||
gtk_widget_set_valign(a->hangup_btn, GTK_ALIGN_CENTER);
|
||||
gtk_widget_add_css_class(a->hangup_btn, "destructive-action");
|
||||
g_signal_connect(a->hangup_btn, "clicked", G_CALLBACK(hangup), a);
|
||||
adw_action_row_add_suffix(ADW_ACTION_ROW(calls), a->answer_btn);
|
||||
adw_action_row_add_suffix(ADW_ACTION_ROW(calls), a->hangup_btn);
|
||||
adw_preferences_group_add(ADW_PREFERENCES_GROUP(call), calls);
|
||||
gtk_box_append(GTK_BOX(box), call);
|
||||
|
||||
/* the modem */
|
||||
GtkWidget *mg = adw_preferences_group_new();
|
||||
adw_preferences_group_set_title(ADW_PREFERENCES_GROUP(mg), "Modem");
|
||||
GtkWidget *line_btn = gtk_button_new_from_icon_name("emblem-system-symbolic");
|
||||
gtk_widget_set_tooltip_text(line_btn, "Line settings");
|
||||
gtk_widget_add_css_class(line_btn, "flat");
|
||||
g_signal_connect(line_btn, "clicked", G_CALLBACK(line_settings), a);
|
||||
adw_preferences_group_set_header_suffix(ADW_PREFERENCES_GROUP(mg),
|
||||
line_btn);
|
||||
a->run_row = adw_action_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(a->run_row), "dmodemd");
|
||||
a->run_btn = gtk_button_new_with_label("Start");
|
||||
gtk_widget_set_valign(a->run_btn, GTK_ALIGN_CENTER);
|
||||
g_signal_connect(a->run_btn, "clicked", G_CALLBACK(run), a);
|
||||
adw_action_row_add_suffix(ADW_ACTION_ROW(a->run_row), a->run_btn);
|
||||
adw_preferences_group_add(ADW_PREFERENCES_GROUP(mg), a->run_row);
|
||||
a->device_row = adw_action_row_new();
|
||||
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(a->device_row),
|
||||
"Device");
|
||||
a->copy_btn = gtk_button_new_from_icon_name("edit-copy-symbolic");
|
||||
gtk_widget_set_tooltip_text(a->copy_btn, "Copy the device's path");
|
||||
gtk_widget_set_valign(a->copy_btn, GTK_ALIGN_CENTER);
|
||||
gtk_widget_add_css_class(a->copy_btn, "flat");
|
||||
g_signal_connect(a->copy_btn, "clicked", G_CALLBACK(copy_device), a);
|
||||
adw_action_row_add_suffix(ADW_ACTION_ROW(a->device_row), a->copy_btn);
|
||||
adw_preferences_group_add(ADW_PREFERENCES_GROUP(mg), a->device_row);
|
||||
gtk_box_append(GTK_BOX(box), mg);
|
||||
|
||||
GtkWidget *clamp = adw_clamp_new();
|
||||
adw_clamp_set_maximum_size(ADW_CLAMP(clamp), 560);
|
||||
adw_clamp_set_child(ADW_CLAMP(clamp), box);
|
||||
GtkWidget *scroll = gtk_scrolled_window_new();
|
||||
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
|
||||
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
|
||||
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scroll), clamp);
|
||||
return scroll;
|
||||
}
|
||||
|
||||
/*
|
||||
* the Terminal page
|
||||
*/
|
||||
|
||||
static void committed(VteTerminal *t, char *text, guint size, gpointer data)
|
||||
{
|
||||
(void)t;
|
||||
modem_type(data, text, size);
|
||||
}
|
||||
|
||||
static void typed_command(App *a, const char *cmd)
|
||||
{
|
||||
if (dmc_state(a->s) != DMC_IDLE)
|
||||
return;
|
||||
modem_type(a, cmd, strlen(cmd));
|
||||
gtk_widget_grab_focus(GTK_WIDGET(a->term));
|
||||
}
|
||||
|
||||
static void setup_screen(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
typed_command(data, "AT!CF\r");
|
||||
}
|
||||
|
||||
static void setup_cli(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
typed_command(data, "AT!CT\r");
|
||||
}
|
||||
|
||||
static void clear_term(GtkWidget *w, gpointer data)
|
||||
{
|
||||
(void)w;
|
||||
App *a = data;
|
||||
vte_terminal_reset(a->term, TRUE, TRUE);
|
||||
}
|
||||
|
||||
static GtkWidget *terminal_page(App *a)
|
||||
{
|
||||
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||
GtkWidget *bar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
|
||||
gtk_widget_set_margin_start(bar, 6);
|
||||
gtk_widget_set_margin_end(bar, 6);
|
||||
gtk_widget_set_margin_top(bar, 6);
|
||||
gtk_widget_set_margin_bottom(bar, 6);
|
||||
a->term_buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
|
||||
GtkWidget *b = gtk_button_new_with_label("Full-Screen Setup");
|
||||
gtk_widget_set_tooltip_text(b, "The modem's own setup screen (AT!CF)");
|
||||
g_signal_connect(b, "clicked", G_CALLBACK(setup_screen), a);
|
||||
gtk_box_append(GTK_BOX(a->term_buttons), b);
|
||||
b = gtk_button_new_with_label("Command-Line Setup");
|
||||
gtk_widget_set_tooltip_text(b, "The modem's IOS-style command line "
|
||||
"(AT!CT)");
|
||||
g_signal_connect(b, "clicked", G_CALLBACK(setup_cli), a);
|
||||
gtk_box_append(GTK_BOX(a->term_buttons), b);
|
||||
gtk_box_append(GTK_BOX(bar), a->term_buttons);
|
||||
GtkWidget *spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
||||
gtk_widget_set_hexpand(spacer, TRUE);
|
||||
gtk_box_append(GTK_BOX(bar), spacer);
|
||||
b = gtk_button_new_from_icon_name("edit-clear-all-symbolic");
|
||||
gtk_widget_set_tooltip_text(b, "Clear");
|
||||
gtk_widget_add_css_class(b, "flat");
|
||||
g_signal_connect(b, "clicked", G_CALLBACK(clear_term), a);
|
||||
gtk_box_append(GTK_BOX(bar), b);
|
||||
gtk_box_append(GTK_BOX(box), bar);
|
||||
|
||||
a->term = VTE_TERMINAL(vte_terminal_new());
|
||||
vte_terminal_set_scrollback_lines(a->term, 5000);
|
||||
vte_terminal_set_size(a->term, 80, 25);
|
||||
/* (the modem takes S5, a backspace, not DEL) */
|
||||
vte_terminal_set_backspace_binding(a->term, VTE_ERASE_ASCII_BACKSPACE);
|
||||
PangoFontDescription *f = pango_font_description_from_string("Monospace 11");
|
||||
vte_terminal_set_font(a->term, f);
|
||||
pango_font_description_free(f);
|
||||
/* the brand's colours */
|
||||
GdkRGBA fg, bg;
|
||||
gdk_rgba_parse(&fg, "#e8f0ff");
|
||||
gdk_rgba_parse(&bg, "#0a0f1c");
|
||||
vte_terminal_set_colors(a->term, &fg, &bg, NULL, 0);
|
||||
gtk_widget_set_vexpand(GTK_WIDGET(a->term), TRUE);
|
||||
gtk_widget_set_hexpand(GTK_WIDGET(a->term), TRUE);
|
||||
g_signal_connect(a->term, "commit", G_CALLBACK(committed), a);
|
||||
GtkWidget *scroll = gtk_scrolled_window_new();
|
||||
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scroll),
|
||||
GTK_WIDGET(a->term));
|
||||
gtk_widget_set_vexpand(scroll, TRUE);
|
||||
gtk_box_append(GTK_BOX(box), scroll);
|
||||
return box;
|
||||
}
|
||||
|
||||
/*
|
||||
* the Log page
|
||||
*/
|
||||
|
||||
static GtkWidget *log_page(App *a)
|
||||
{
|
||||
GtkWidget *view = gtk_text_view_new();
|
||||
gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
|
||||
gtk_text_view_set_monospace(GTK_TEXT_VIEW(view), TRUE);
|
||||
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
|
||||
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), 12);
|
||||
gtk_text_view_set_top_margin(GTK_TEXT_VIEW(view), 12);
|
||||
a->log = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
|
||||
GtkWidget *scroll = gtk_scrolled_window_new();
|
||||
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scroll), view);
|
||||
return scroll;
|
||||
}
|
||||
|
||||
/*
|
||||
* the window
|
||||
*/
|
||||
|
||||
static void about(GSimpleAction *act, GVariant *v, gpointer data)
|
||||
{
|
||||
(void)act;
|
||||
(void)v;
|
||||
App *a = data;
|
||||
AdwDialog *d = adw_about_dialog_new();
|
||||
adw_about_dialog_set_application_name(ADW_ABOUT_DIALOG(d), "D-Modem");
|
||||
adw_about_dialog_set_application_icon(ADW_ABOUT_DIALOG(d), APP_ID);
|
||||
adw_about_dialog_set_developer_name(ADW_ABOUT_DIALOG(d),
|
||||
"The D-Modem Authors");
|
||||
adw_about_dialog_set_version(ADW_ABOUT_DIALOG(d), "1.0.0");
|
||||
adw_about_dialog_set_comments(ADW_ABOUT_DIALOG(d),
|
||||
"The front panel of D-Modem, the open V.92 softmodem.");
|
||||
adw_about_dialog_set_website(ADW_ABOUT_DIALOG(d),
|
||||
"https://github.com/D-Modem/gui");
|
||||
adw_about_dialog_set_license_type(ADW_ABOUT_DIALOG(d),
|
||||
GTK_LICENSE_BSD_3);
|
||||
adw_dialog_present(d, GTK_WIDGET(a->window));
|
||||
}
|
||||
|
||||
static void line_action(GSimpleAction *act, GVariant *v, gpointer data)
|
||||
{
|
||||
(void)act;
|
||||
(void)v;
|
||||
prefs_show(data);
|
||||
}
|
||||
|
||||
void window_build(App *a)
|
||||
{
|
||||
AdwApplicationWindow *w = ADW_APPLICATION_WINDOW(
|
||||
adw_application_window_new(GTK_APPLICATION(a->gapp)));
|
||||
a->window = GTK_WINDOW(w);
|
||||
gtk_window_set_title(a->window, "D-Modem");
|
||||
gtk_window_set_default_size(a->window, 860, 680);
|
||||
|
||||
const GActionEntry actions[] = {
|
||||
{ "about", about, NULL, NULL, NULL, { 0 } },
|
||||
{ "line", line_action, NULL, NULL, NULL, { 0 } },
|
||||
};
|
||||
g_action_map_add_action_entries(G_ACTION_MAP(w), actions,
|
||||
G_N_ELEMENTS(actions), a);
|
||||
|
||||
a->stack = ADW_VIEW_STACK(adw_view_stack_new());
|
||||
adw_view_stack_add_titled_with_icon(a->stack, modem_page(a), "modem",
|
||||
"Modem", "call-start-symbolic");
|
||||
adw_view_stack_add_titled_with_icon(a->stack, terminal_page(a),
|
||||
"terminal", "Terminal",
|
||||
"utilities-terminal-symbolic");
|
||||
adw_view_stack_add_titled_with_icon(a->stack, settings_page_new(a),
|
||||
"settings", "Settings",
|
||||
"preferences-system-symbolic");
|
||||
adw_view_stack_add_titled_with_icon(a->stack, stats_page_new(a),
|
||||
"call", "Call Stats",
|
||||
"utilities-system-monitor-symbolic");
|
||||
adw_view_stack_add_titled_with_icon(a->stack, log_page(a), "log",
|
||||
"Log", "text-x-generic-symbolic");
|
||||
|
||||
GtkWidget *header = adw_header_bar_new();
|
||||
GtkWidget *sw = adw_view_switcher_new();
|
||||
adw_view_switcher_set_stack(ADW_VIEW_SWITCHER(sw), a->stack);
|
||||
adw_view_switcher_set_policy(ADW_VIEW_SWITCHER(sw),
|
||||
ADW_VIEW_SWITCHER_POLICY_WIDE);
|
||||
adw_header_bar_set_title_widget(ADW_HEADER_BAR(header), sw);
|
||||
GMenu *menu = g_menu_new();
|
||||
g_menu_append(menu, "Line Settings", "win.line");
|
||||
g_menu_append(menu, "About D-Modem", "win.about");
|
||||
GtkWidget *mb = gtk_menu_button_new();
|
||||
gtk_menu_button_set_icon_name(GTK_MENU_BUTTON(mb), "open-menu-symbolic");
|
||||
gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(mb), G_MENU_MODEL(menu));
|
||||
g_object_unref(menu);
|
||||
adw_header_bar_pack_end(ADW_HEADER_BAR(header), mb);
|
||||
|
||||
GtkWidget *tv = adw_toolbar_view_new();
|
||||
adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(tv), header);
|
||||
a->toasts = ADW_TOAST_OVERLAY(adw_toast_overlay_new());
|
||||
adw_toast_overlay_set_child(a->toasts, GTK_WIDGET(a->stack));
|
||||
adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(tv), GTK_WIDGET(a->toasts));
|
||||
adw_application_window_set_content(w, tv);
|
||||
/* (a page to open on: screenshots and the docs) */
|
||||
const char *page = g_getenv("DMODEM_GUI_PAGE");
|
||||
if (page)
|
||||
adw_view_stack_set_visible_child_name(a->stack, page);
|
||||
window_state(a);
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
{
|
||||
"images": [
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "16x16",
|
||||
"scale": "1x",
|
||||
"filename": "icon_16x16.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "16x16",
|
||||
"scale": "2x",
|
||||
"filename": "icon_16x16@2x.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "32x32",
|
||||
"scale": "1x",
|
||||
"filename": "icon_32x32.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "32x32",
|
||||
"scale": "2x",
|
||||
"filename": "icon_32x32@2x.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "128x128",
|
||||
"scale": "1x",
|
||||
"filename": "icon_128x128.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "128x128",
|
||||
"scale": "2x",
|
||||
"filename": "icon_128x128@2x.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "256x256",
|
||||
"scale": "1x",
|
||||
"filename": "icon_256x256.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "256x256",
|
||||
"scale": "2x",
|
||||
"filename": "icon_256x256@2x.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "512x512",
|
||||
"scale": "1x",
|
||||
"filename": "icon_512x512.png"
|
||||
},
|
||||
{
|
||||
"idiom": "mac",
|
||||
"size": "512x512",
|
||||
"scale": "2x",
|
||||
"filename": "icon_512x512@2x.png"
|
||||
}
|
||||
],
|
||||
"info": {
|
||||
"version": 1,
|
||||
"author": "xcode"
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 6.4 KiB |
|
Before Width: | Height: | Size: 538 B |
|
Before Width: | Height: | Size: 978 B |
|
Before Width: | Height: | Size: 6.4 KiB |
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 978 B |
|
Before Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 29 KiB |
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright (c) 2026, D-Modem
|
||||
*
|
||||
* Bridging.h - libdmodemctl, for Swift
|
||||
*/
|
||||
#include "dmodemctl.h"
|
||||
@@ -1,306 +0,0 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// Copyright (c) 2026, D-Modem
|
||||
//
|
||||
// ContentView.swift - the window: its pages (Modem, Terminal, Settings,
|
||||
// Call Stats, Log) in a sidebar
|
||||
|
||||
import SwiftUI
|
||||
|
||||
enum Page: String, CaseIterable, Identifiable {
|
||||
case modem = "Modem", terminal = "Terminal", settings = "Settings"
|
||||
case stats = "Call Stats", log = "Log"
|
||||
var id: String { rawValue }
|
||||
var icon: String {
|
||||
switch self {
|
||||
case .modem: return "phone.connection"
|
||||
case .terminal: return "terminal"
|
||||
case .settings: return "slider.horizontal.3"
|
||||
case .stats: return "chart.bar"
|
||||
case .log: return "doc.plaintext"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentView: View {
|
||||
@EnvironmentObject var modem: Modem
|
||||
@State private var page: Page? = Page(rawValue: ProcessInfo.processInfo.environment["DMODEM_GUI_PAGE"] ?? "") ?? .modem
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List(Page.allCases, selection: $page) { p in
|
||||
Label(p.rawValue, systemImage: p.icon).tag(p)
|
||||
}
|
||||
.navigationSplitViewColumnWidth(170)
|
||||
} detail: {
|
||||
Group {
|
||||
switch page ?? .modem {
|
||||
case .modem: ModemPage()
|
||||
case .terminal: TerminalPane()
|
||||
case .settings: SettingsPage()
|
||||
case .stats: StatsPage()
|
||||
case .log: LogPage()
|
||||
}
|
||||
}
|
||||
.navigationTitle(page?.rawValue ?? "D-Modem")
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(modem.running ? "Stop" : "Start") {
|
||||
modem.running ? modem.stop() : modem.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
.overlay(alignment: .bottom) {
|
||||
if let t = modem.toast {
|
||||
Text(t)
|
||||
.padding(.horizontal, 14).padding(.vertical, 8)
|
||||
.background(.regularMaterial, in: Capsule())
|
||||
.padding(16)
|
||||
.transition(.opacity)
|
||||
.task(id: t) {
|
||||
try? await Task.sleep(nanoseconds: 4_000_000_000)
|
||||
if modem.toast == t { modem.toast = nil }
|
||||
}
|
||||
}
|
||||
}
|
||||
.animation(.default, value: modem.toast)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Modem
|
||||
|
||||
struct ModemPage: View {
|
||||
@EnvironmentObject var modem: Modem
|
||||
@State private var number = ""
|
||||
|
||||
private var icon: String {
|
||||
guard modem.running else { return "phone.down" }
|
||||
switch modem.state {
|
||||
case Int(DMC_RINGING): return "phone.arrow.down.left"
|
||||
case Int(DMC_DIALING), Int(DMC_ANSWERING): return "phone.arrow.up.right"
|
||||
case Int(DMC_ONLINE), Int(DMC_ONLINE_COMMAND): return "arrow.up.arrow.down"
|
||||
case Int(DMC_CONFIGURATOR): return "gearshape"
|
||||
default: return "phone"
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section {
|
||||
HStack(spacing: 16) {
|
||||
Image(systemName: icon)
|
||||
.font(.system(size: 36))
|
||||
.frame(width: 56)
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(modem.stateName).font(.title.bold())
|
||||
Text(modem.detail).foregroundStyle(.secondary).textSelection(.enabled)
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 8)
|
||||
}
|
||||
Section("Call") {
|
||||
HStack {
|
||||
TextField("Number", text: $number)
|
||||
.onSubmit { if modem.isIdle { modem.dial(number) } }
|
||||
Button("Dial") { modem.dial(number) }
|
||||
.disabled(!modem.isIdle)
|
||||
}
|
||||
.disabled(!modem.isOpen)
|
||||
HStack {
|
||||
Button("Answer") { modem.answer() }
|
||||
.disabled(modem.state != Int(DMC_RINGING))
|
||||
Button("Hang Up", role: .destructive) { modem.hangUp() }
|
||||
.disabled(!modem.canHangUp)
|
||||
}
|
||||
}
|
||||
Section("Line") {
|
||||
LabeledContent("Line", value: modem.config.summary)
|
||||
LabeledContent("Modem device") {
|
||||
HStack {
|
||||
Text(modem.device.isEmpty ? "Not running" : modem.device)
|
||||
.textSelection(.enabled)
|
||||
if !modem.device.isEmpty {
|
||||
Button {
|
||||
NSPasteboard.general.clearContents()
|
||||
NSPasteboard.general.setString(modem.device, forType: .string)
|
||||
} label: { Image(systemName: "doc.on.doc") }
|
||||
.buttonStyle(.borderless)
|
||||
.help("Copy, for another program to use the modem")
|
||||
}
|
||||
}
|
||||
}
|
||||
HStack {
|
||||
if #available(macOS 14, *) {
|
||||
SettingsLink { Text("Line Settings…") }
|
||||
} else {
|
||||
Button("Line Settings…") {
|
||||
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
Button(modem.running ? "Stop" : "Start") {
|
||||
modem.running ? modem.stop() : modem.start()
|
||||
}
|
||||
.keyboardShortcut(.defaultAction)
|
||||
}
|
||||
}
|
||||
}
|
||||
.formStyle(.grouped)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Settings
|
||||
|
||||
struct SettingsPage: View {
|
||||
@EnvironmentObject var modem: Modem
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section {
|
||||
LabeledContent("Init string") {
|
||||
HStack {
|
||||
Text(modem.initString.isEmpty ? "AT (all at their defaults)" : modem.initString)
|
||||
.font(.body.monospaced())
|
||||
.textSelection(.enabled)
|
||||
Button("Keep") { modem.keepInit() }
|
||||
.help("Send it each time the modem opens")
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
HStack {
|
||||
Text("The modem's settings")
|
||||
Spacer()
|
||||
Button { modem.loadSettings() } label: { Image(systemName: "arrow.clockwise") }
|
||||
.buttonStyle(.borderless)
|
||||
.help("Read the settings again")
|
||||
Button("Defaults") { modem.resetSettings() }
|
||||
.buttonStyle(.borderless)
|
||||
.help("Every setting back to its default (ATZ)")
|
||||
}
|
||||
} footer: {
|
||||
Text("Changes take effect at once. The modem keeps no stored profile, so keep the init string to have them each time.")
|
||||
}
|
||||
.disabled(!modem.isIdle)
|
||||
|
||||
if !modem.settingsNote.isEmpty {
|
||||
Text(modem.settingsNote).foregroundStyle(.secondary)
|
||||
}
|
||||
ForEach(modem.groups, id: \.self) { g in
|
||||
Section(g) {
|
||||
ForEach(modem.settings.filter { $0.group == g }) { s in
|
||||
SettingRow(s: s)
|
||||
}
|
||||
}
|
||||
.disabled(!modem.isIdle)
|
||||
}
|
||||
}
|
||||
.formStyle(.grouped)
|
||||
}
|
||||
}
|
||||
|
||||
struct SettingRow: View {
|
||||
@EnvironmentObject var modem: Modem
|
||||
let s: Setting
|
||||
@State private var text = ""
|
||||
|
||||
private var title: some View {
|
||||
Text(s.label).fontWeight(s.changed ? .semibold : .regular)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
switch s.kind {
|
||||
case Int(DMC_PICK):
|
||||
Picker(selection: Binding(get: { s.value }, set: { modem.apply(s.index, $0) })) {
|
||||
ForEach(s.choices, id: \.value) { Text($0.label).tag($0.value) }
|
||||
if !s.choices.contains(where: { $0.value == s.value }) {
|
||||
Text(s.value).tag(s.value)
|
||||
}
|
||||
} label: { title }
|
||||
case Int(DMC_NUMBER):
|
||||
LabeledContent {
|
||||
HStack {
|
||||
TextField("", text: $text)
|
||||
.frame(width: 80)
|
||||
.multilineTextAlignment(.trailing)
|
||||
.onSubmit { modem.apply(s.index, text) }
|
||||
Stepper("", value: Binding(
|
||||
get: { Int(s.value) ?? s.lo },
|
||||
set: { modem.apply(s.index, String($0)) }),
|
||||
in: s.lo...max(s.lo, s.hi))
|
||||
.labelsHidden()
|
||||
}
|
||||
} label: { title }
|
||||
default:
|
||||
LabeledContent {
|
||||
TextField("", text: $text)
|
||||
.onSubmit { modem.apply(s.index, text) }
|
||||
} label: { title }
|
||||
}
|
||||
}
|
||||
.help(s.help)
|
||||
.onAppear { text = s.value }
|
||||
.onChange(of: s.value) { text = $0 }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Call Stats
|
||||
|
||||
struct StatsPage: View {
|
||||
@EnvironmentObject var modem: Modem
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
HStack {
|
||||
Text("The last call, as the modem reports it (AT#UD), after each call.")
|
||||
.foregroundStyle(.secondary)
|
||||
Spacer()
|
||||
Button { modem.refreshStats() } label: { Image(systemName: "arrow.clockwise") }
|
||||
.disabled(!modem.isIdle)
|
||||
.help("Ask the modem again")
|
||||
}
|
||||
.padding(12)
|
||||
if !modem.diagNote.isEmpty {
|
||||
Text(modem.diagNote).foregroundStyle(.secondary).padding(.horizontal, 12)
|
||||
}
|
||||
Table(modem.diag) {
|
||||
TableColumn("Statistic", value: \.label)
|
||||
TableColumn("Value") { r in Text(r.value).textSelection(.enabled) }
|
||||
}
|
||||
.frame(minHeight: 180)
|
||||
Text("The modem's report (ATI11)").font(.headline).padding(12)
|
||||
ScrollView {
|
||||
Text(modem.report.isEmpty ? " " : modem.report)
|
||||
.font(.body.monospaced())
|
||||
.textSelection(.enabled)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(12)
|
||||
}
|
||||
.frame(minHeight: 120)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Log
|
||||
|
||||
struct LogPage: View {
|
||||
@EnvironmentObject var modem: Modem
|
||||
|
||||
var body: some View {
|
||||
ScrollViewReader { proxy in
|
||||
ScrollView {
|
||||
LazyVStack(alignment: .leading, spacing: 1) {
|
||||
ForEach(Array(modem.log.enumerated()), id: \.offset) { i, line in
|
||||
Text(line).font(.callout.monospaced()).id(i)
|
||||
}
|
||||
}
|
||||
.textSelection(.enabled)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(12)
|
||||
}
|
||||
.onChange(of: modem.log.count) { n in
|
||||
if n > 0 { proxy.scrollTo(n - 1, anchor: .bottom) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||