diff --git a/README.md b/README.md index 099eb24..bced242 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ libdmodemctl. Thus they operate in the same way. - **File transfers** (Windows app): 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, and with Crash Recovery), 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** (Windows app): 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** (Windows app, 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** (Windows app, 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. Send Break (Modem menu, or Ctrl+Break) sends a break on a COM port or a Telnet connection - **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) diff --git a/core/include/dmodemctl.h b/core/include/dmodemctl.h index 9abc86f..21285bd 100644 --- a/core/include/dmodemctl.h +++ b/core/include/dmodemctl.h @@ -497,6 +497,8 @@ DMC_API void dmc_vt_feed(dmc_vt *t, const char *data, int n); /* 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 @@ -664,6 +666,9 @@ DMC_API void dmc_net_type(dmc_net *n, const char *data, int len); 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: diff --git a/core/src/net.c b/core/src/net.c index 9228cd3..9bfd398 100644 --- a/core/src/net.c +++ b/core/src/net.c @@ -27,7 +27,7 @@ enum { IAC = 255, DONT = 254, DO = 253, WONT = 252, WILL = 251, SB = 250, - SE = 240, + SE = 240, BRK = 243, O_BINARY = 0, O_ECHO = 1, O_SGA = 3, O_TTYPE = 24, O_NAWS = 31 }; @@ -336,6 +336,16 @@ 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) diff --git a/core/src/vt.c b/core/src/vt.c index 9dc71e4..6a7809b 100644 --- a/core/src/vt.c +++ b/core/src/vt.c @@ -1603,6 +1603,30 @@ void dmc_vt_feed(dmc_vt *t, const char *data, int n) t->changed = 1; } +void dmc_vt_set_scrollback(dmc_vt *t, int lines) +{ + struct vt_line **h; + int n, i, drop; + if (lines < 0) + lines = 0; + if (lines == t->hist_max) + return; + /* the newest lines kept, the oldest freed */ + n = t->hist_n < lines ? t->hist_n : lines; + drop = t->hist_n - n; + for (i = 0; i < drop; i++) + line_free(hist_at(t, i)); + h = xcalloc((size_t)(lines ? lines : 1), sizeof(*h)); + for (i = 0; i < n; i++) + h[i] = hist_at(t, drop + i); + free(t->hist); + t->hist = h; + t->hist_max = lines; + t->hist_n = n; + t->hist_first = 0; + t->changed = 1; +} + void dmc_vt_set_ascii(dmc_vt *t, int flags) { t->ascii = flags; diff --git a/core/test/dmodemctl_test.c b/core/test/dmodemctl_test.c index e2d0afa..6217e04 100644 --- a/core/test/dmodemctl_test.c +++ b/core/test/dmodemctl_test.c @@ -1178,6 +1178,9 @@ static void test_net(void) /* 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 */ @@ -1203,6 +1206,9 @@ static void test_net(void) 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); } @@ -1287,7 +1293,29 @@ static void test_vt_ascii(void) 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)); diff --git a/docs/design.md b/docs/design.md index de8e053..591d515 100644 --- a/docs/design.md +++ b/docs/design.md @@ -149,6 +149,35 @@ the two terminals: 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 (Modem 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. + `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. diff --git a/windows/res/app.rc b/windows/res/app.rc index c641936..91b9e4c 100644 --- a/windows/res/app.rc +++ b/windows/res/app.rc @@ -20,6 +20,8 @@ BEGIN MENUITEM "&Start or Stop", IDM_START MENUITEM "&Line Settings...", IDM_LINE MENUITEM "&ASCII Setup...", IDM_ASCII + MENUITEM "&Terminal Settings...", IDM_TERMSET + MENUITEM "Send &Break\tCtrl+Break", IDM_BREAK MENUITEM SEPARATOR MENUITEM "&Print...", IDM_FPRINT MENUITEM SEPARATOR @@ -192,6 +194,40 @@ BEGIN PUSHBUTTON "Cancel", IDCANCEL, 222, 130, 50, 14 END +/* the Modem menu's Terminal Settings, as HyperTerminal has them, for the + terminal of the page shown (the Remote page's, else the modem's) */ +IDD_TERMSET DIALOG 0, 0, 284, 232 +STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Terminal Settings" +FONT 8, "MS Shell Dlg" +BEGIN + GROUPBOX "&Backspace key sends", -1, 8, 6, 118, 50 + AUTORADIOBUTTON "Ctrl+&H", IDC_T_BSH, 16, 18, 100, 10, WS_GROUP | WS_TABSTOP + AUTORADIOBUTTON "&Del", IDC_T_BSDEL, 16, 30, 100, 10 + AUTORADIOBUTTON "Ctrl+H, &Space, Ctrl+H", IDC_T_BSHSH, 16, 42, 104, 10 + GROUPBOX "Function, arrow, and ctrl keys act as", -1, 134, 6, 142, 50 + AUTORADIOBUTTON "Te&rminal keys", IDC_T_TERMKEYS, 142, 18, 100, 10, WS_GROUP | WS_TABSTOP + AUTORADIOBUTTON "&Windows keys", IDC_T_WINKEYS, 142, 30, 100, 10 + GROUPBOX "Cursor", -1, 8, 62, 118, 50 + AUTORADIOBUTTON "Bl&ock", IDC_T_BLOCK, 16, 74, 100, 10, WS_GROUP | WS_TABSTOP + AUTORADIOBUTTON "&Underline", IDC_T_UNDERLINE, 16, 86, 100, 10 + AUTOCHECKBOX "B&link", IDC_T_BLINK, 16, 98, 100, 10, WS_GROUP | WS_TABSTOP + GROUPBOX "Font", -1, 134, 62, 142, 50 + LTEXT "&Name:", -1, 142, 76, 30, 8 + EDITTEXT IDC_T_FONT, 176, 74, 92, 12, ES_AUTOHSCROLL + LTEXT "Si&ze:", -1, 142, 94, 30, 8 + EDITTEXT IDC_T_FONTSIZE, 176, 92, 24, 12, ES_NUMBER + LTEXT "points", -1, 204, 94, 40, 8 + LTEXT "Ba&ckscroll buffer lines:", -1, 8, 122, 110, 8 + EDITTEXT IDC_T_BACKSCROLL, 120, 120, 44, 12, ES_NUMBER + LTEXT "Telnet terminal &ID:", IDC_T_TELNETLABEL, 8, 140, 110, 8 + EDITTEXT IDC_T_TELNETID, 120, 138, 80, 12, ES_AUTOHSCROLL + AUTOCHECKBOX "E&xit program upon disconnecting", IDC_T_EXIT, 8, 158, 240, 10 + LTEXT "An empty font name uses the font of the app. An empty Telnet terminal ID uses the terminal type.", -1, 8, 174, 268, 20 + DEFPUSHBUTTON "OK", IDOK, 168, 210, 50, 14 + PUSHBUTTON "Cancel", IDCANCEL, 226, 210, 50, 14 +END + /* the Modem menu's ASCII Setup, as HyperTerminal has it */ IDD_ASCII DIALOG 0, 0, 220, 176 STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU diff --git a/windows/sources.cmake b/windows/sources.cmake index fa1b763..9a1d0bc 100644 --- a/windows/sources.cmake +++ b/windows/sources.cmake @@ -14,5 +14,6 @@ set(DMODEM_WIN_SOURCES remote.cpp xfer.cpp capture.cpp - ascii.cpp) + ascii.cpp + termset.cpp) list(TRANSFORM DMODEM_WIN_SOURCES PREPEND "${CMAKE_CURRENT_LIST_DIR}/src/") diff --git a/windows/src/app.h b/windows/src/app.h index 1c705ae..4f31bc7 100644 --- a/windows/src/app.h +++ b/windows/src/app.h @@ -119,6 +119,8 @@ void link_start(); void link_stop(); bool link_running(); bool link_open(); /* the DTE is open */ +/* a break on the line (a COM port's only): false when none can be sent */ +bool link_break(); /* the session's output written, input read, events handled */ void link_pump(); /* socket messages (WM_APP_SOCKET) */ @@ -183,6 +185,20 @@ void termview_reset(HWND w); void termview_size(HWND w, int *cols, int *rows); /* what Backspace sends (DEL unless set) */ void termview_set_backspace(HWND w, char b); +/* Terminal Settings: Backspace sends Ctrl+H (0), Del (1) or Ctrl+H, + Space, Ctrl+H (2) (-1: as it is); the function, arrow and Ctrl keys as + Windows keys; the backscroll's lines; the font (none: the app's) and + its size in points; the cursor (0 block, 1 underline) and its blink */ +struct TermOptions { + int backspace; + bool windows_keys; + int scrollback; + char font[32]; + int font_size; + int cursor; + bool cursor_blink; +}; +void termview_set_options(HWND w, const TermOptions *o); /* ASCII Setup: the received data's DMC_VT_ASCII_* flags, what Enter sends (0 CR, 1 CR LF), and the local echo */ void termview_set_ascii(HWND w, int flags, int enter, bool echo); @@ -233,6 +249,14 @@ void xfer_set_auto(bool on); /* the transfer's dialog's keys (IsDialogMessage) */ bool xfer_dialog_key(MSG *m); +/* termset.cpp: Terminal Settings, for the modem's terminal or the + Remote page's ([terminal], [remote_terminal] in dmodem.ini) */ +void termset_apply(HWND view, bool remote); +void termset_dialog(HWND parent, bool remote); +/* the Telnet terminal ID set (empty: the terminal type's) */ +void termset_telnet_id(char *out, int n); +bool termset_exit_on_disconnect(); + /* ascii.cpp: ASCII Setup ([ascii] in dmodem.ini), for both terminals */ struct AsciiSetup { bool send_lf; /* Enter sends CR LF */ @@ -278,6 +302,8 @@ void remote_ssh(LPARAM l); /* the focus to its first control */ bool remote_focus(); void remote_stop(); +/* a break to the server (Telnet's): false when none can be sent */ +bool remote_break(); /* settings.cpp: the Settings page */ HWND settings_page_new(HWND parent); diff --git a/windows/src/link.cpp b/windows/src/link.cpp index d169441..04af89a 100644 --- a/windows/src/link.cpp +++ b/windows/src/link.cpp @@ -45,6 +45,16 @@ bool link_open() com != INVALID_HANDLE_VALUE; } +bool link_break() +{ + /* (a COM port's: the line held at space for 250 ms) */ + if (com == INVALID_HANDLE_VALUE || !SetCommBreak(com)) + return false; + Sleep(250); + ClearCommBreak(com); + return true; +} + bool link_running() { return proc != NULL || sock != INVALID_SOCKET || diff --git a/windows/src/main.cpp b/windows/src/main.cpp index 8450d8d..cd22ba6 100644 --- a/windows/src/main.cpp +++ b/windows/src/main.cpp @@ -109,16 +109,51 @@ void app_log(const char *line) log_append(line); } -void app_state() +/* when the call was connected (GetTickCount), 0 when not online; the + seconds the status bar shows */ +static DWORD online_at; +static long online_shown = -1; + +/* the state in the status bar, with the connect timer when online, as + HyperTerminal has it: "Online 0:01:05" */ +static void show_state() { char t[160]; int st = dmc_state(app.s); - if (link_running() && st == DMC_CLOSED) + if (link_running() && st == DMC_CLOSED) { strcpy(t, "Starting"); - else + } else if (online_at) { + long s = (long)((GetTickCount() - online_at) / 1000); + online_shown = s; + _snprintf(t, sizeof(t), "%s %ld:%02ld:%02ld", dmc_state_name(st), + s / 3600, s / 60 % 60, s % 60); + } else { _snprintf(t, sizeof(t), "%s", dmc_state_name(st)); + } t[sizeof(t) - 1] = '\0'; SendMessage(status_bar, SB_SETTEXT, 1, (LPARAM)t); +} + +/* the connect timer moved on a second */ +static void state_tick() +{ + if (online_at && (long)((GetTickCount() - online_at) / 1000) != online_shown) + show_state(); +} + +void app_state() +{ + int st = dmc_state(app.s); + bool on = st == DMC_ONLINE || st == DMC_ONLINE_COMMAND; + bool was_online = online_at != 0; + if (on && !online_at) + online_at = GetTickCount() ? GetTickCount() : 1; + else if (!on) + online_at = 0; + show_state(); + /* Terminal Settings: Exit program upon disconnecting */ + if (was_online && !online_at && termset_exit_on_disconnect()) + PostMessage(app.main, WM_CLOSE, 0, 0); modem_page_state(); voice_state(); term_state(); @@ -317,6 +352,17 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) case IDM_ASCII: ascii_dialog(w); return 0; + case IDM_TERMSET: + termset_dialog(w, line_now() == &remote_line); + return 0; + case IDM_BREAK: + if (line_now() == &remote_line ? remote_break() : link_break()) + app_message("Break sent"); + else + app_message(line_now() == &remote_line ? + "This connection cannot send a break (Telnet only)" : + "This line cannot send a break (a COM port only)"); + return 0; case IDM_START: if (link_running()) link_stop(); @@ -416,6 +462,7 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) remote_tick(); xfer_pump(); capture_tick(); + state_tick(); return 0; case WM_APP_XFER: xfer_message(wp, lp); diff --git a/windows/src/remote.cpp b/windows/src/remote.cpp index d2d92c8..380d187 100644 --- a/windows/src/remote.cpp +++ b/windows/src/remote.cpp @@ -303,6 +303,14 @@ static int line_room() return room > 0 ? room : 0; } +bool remote_break() +{ + if (!running || !net || !sock_open || !dmc_net_break(net)) + return false; + flush(); + return true; +} + static void raw_write(const char *data, int n) { if (!running) @@ -1249,10 +1257,13 @@ static bool start_socket(int proto, const char *host, int port, sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) return false; - /* (Telnet's TERMINAL-TYPE, rlogin's TERM) */ + /* (Telnet's TERMINAL-TYPE, rlogin's TERM; Terminal Settings' Telnet + terminal ID first) */ const char *type = termview_profile(view); + char id[40]; + termset_telnet_id(id, sizeof(id)); net = dmc_net_new(proto, user, proto == DMC_NET_TELNET ? - dmc_vt_profile_telnet(type) : + (id[0] ? id : dmc_vt_profile_telnet(type)) : dmc_vt_profile_term(type), cols, rows); sock_open = false; WSAAsyncSelect(sock, app.main, WM_APP_REMOTE, @@ -1403,6 +1414,7 @@ HWND remote_page_new(HWND parent) #endif view = termview_new(page, IDC_R_TERM, typed, NULL); termview_set_printer(view, capture_printed, &remote_line); + termset_apply(view, true); ascii_apply(view); status_txt = make_child("STATIC", "Not connected", SS_LEFTNOWORDWRAP, page, IDC_R_STATUS); diff --git a/windows/src/resource.h b/windows/src/resource.h index 0478d87..9486f21 100644 --- a/windows/src/resource.h +++ b/windows/src/resource.h @@ -28,6 +28,8 @@ #define IDM_CAPPRINT 215 #define IDM_PRNPAUSE 216 #define IDM_ASCII 217 +#define IDM_TERMSET 218 +#define IDM_BREAK 219 #define IDC_STATUS 300 #define IDC_TABS 301 @@ -202,4 +204,21 @@ #define IDC_A_7BIT 1246 #define IDC_A_WRAP 1247 +/* Terminal Settings */ +#define IDD_TERMSET 1250 +#define IDC_T_BSH 1251 +#define IDC_T_BSDEL 1252 +#define IDC_T_BSHSH 1253 +#define IDC_T_TERMKEYS 1254 +#define IDC_T_WINKEYS 1255 +#define IDC_T_BACKSCROLL 1256 +#define IDC_T_FONT 1257 +#define IDC_T_FONTSIZE 1258 +#define IDC_T_BLOCK 1259 +#define IDC_T_UNDERLINE 1260 +#define IDC_T_BLINK 1261 +#define IDC_T_TELNETID 1262 +#define IDC_T_TELNETLABEL 1263 +#define IDC_T_EXIT 1264 + #endif diff --git a/windows/src/term.cpp b/windows/src/term.cpp index 4fb7c18..c641365 100644 --- a/windows/src/term.cpp +++ b/windows/src/term.cpp @@ -126,6 +126,7 @@ HWND term_page_new(HWND parent) termview_set_printer(term, capture_printed, &term_line); /* the modem's configurator wants Backspace as BS */ termview_set_backspace(term, 0x08); + termset_apply(term, false); ascii_apply(term); char path[MAX_PATH], v[64]; ini(path, sizeof(path)); diff --git a/windows/src/termset.cpp b/windows/src/termset.cpp new file mode 100644 index 0000000..4cf94e1 --- /dev/null +++ b/windows/src/termset.cpp @@ -0,0 +1,200 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (C) 2026 D-Modem Authors + * + * termset.cpp - Terminal Settings, as HyperTerminal has them, for each + * terminal (the modem's, kept in [terminal] of dmodem.ini; + * the Remote page's, in [remote_terminal]): what Backspace + * sends, the function, arrow and Ctrl keys as terminal or + * Windows keys, the backscroll buffer's lines, the font, + * the cursor, the Telnet terminal ID (Remote), and the + * exit upon disconnecting (the modem). + */ +#include +#include +#include + +#include "app.h" +#include "resource.h" + +static void ini(char *out, int n) +{ + char dir[MAX_PATH]; + app_data_dir(dir, sizeof(dir)); + _snprintf(out, n, "%s\\dmodem.ini", dir); + out[n - 1] = '\0'; +} + +/* the section of a terminal: the Remote page's, or the modem's */ +static const char *section(bool remote) +{ + return remote ? "remote_terminal" : "terminal"; +} + +static int get(bool remote, const char *key, int def) +{ + char path[MAX_PATH]; + ini(path, sizeof(path)); + return (int)GetPrivateProfileInt(section(remote), key, def, path); +} + +static void get_str(bool remote, const char *key, char *out, int n) +{ + char path[MAX_PATH]; + ini(path, sizeof(path)); + GetPrivateProfileString(section(remote), key, "", out, n, path); +} + +static void put(bool remote, const char *key, int v) +{ + char path[MAX_PATH], s[16]; + ini(path, sizeof(path)); + _snprintf(s, sizeof(s), "%d", v); + s[sizeof(s) - 1] = '\0'; + WritePrivateProfileString(section(remote), key, s, path); +} + +static void put_str(bool remote, const char *key, const char *v) +{ + char path[MAX_PATH]; + ini(path, sizeof(path)); + WritePrivateProfileString(section(remote), key, v, path); +} + +struct TermSet { + bool remote; + TermOptions o; + char telnet_id[40]; + bool exit_on_disconnect; +}; + +static void load(bool remote, TermSet *s) +{ + memset(s, 0, sizeof(*s)); + s->remote = remote; + /* (the modem's configurator wants Ctrl+H; a remote shell, Del) */ + s->o.backspace = get(remote, "backspace", remote ? 1 : 0); + if (s->o.backspace < 0 || s->o.backspace > 2) + s->o.backspace = remote ? 1 : 0; + s->o.windows_keys = get(remote, "windows_keys", 0) != 0; + s->o.scrollback = get(remote, "backscroll", 2000); + if (s->o.scrollback < 0) + s->o.scrollback = 0; + if (s->o.scrollback > 100000) + s->o.scrollback = 100000; + get_str(remote, "font", s->o.font, sizeof(s->o.font)); + s->o.font_size = get(remote, "font_size", 10); + s->o.cursor = get(remote, "cursor", 0) == 1 ? 1 : 0; + s->o.cursor_blink = get(remote, "cursor_blink", 1) != 0; + if (remote) + get_str(remote, "telnet_id", s->telnet_id, sizeof(s->telnet_id)); + else + s->exit_on_disconnect = get(remote, "exit_on_disconnect", 0) != 0; +} + +static void save(const TermSet *s) +{ + bool r = s->remote; + put(r, "backspace", s->o.backspace); + put(r, "windows_keys", s->o.windows_keys); + put(r, "backscroll", s->o.scrollback); + put_str(r, "font", s->o.font); + put(r, "font_size", s->o.font_size); + put(r, "cursor", s->o.cursor); + put(r, "cursor_blink", s->o.cursor_blink); + if (r) + put_str(r, "telnet_id", s->telnet_id); + else + put(r, "exit_on_disconnect", s->exit_on_disconnect); +} + +void termset_apply(HWND view, bool remote) +{ + if (!view) + return; + TermSet s; + load(remote, &s); + termview_set_options(view, &s.o); +} + +void termset_telnet_id(char *out, int n) +{ + get_str(true, "telnet_id", out, n); +} + +bool termset_exit_on_disconnect() +{ + return get(false, "exit_on_disconnect", 0) != 0; +} + +static int num(HWND d, int id, int lo, int hi, int def) +{ + BOOL ok; + UINT v = GetDlgItemInt(d, id, &ok, FALSE); + if (!ok) + return def; + if ((int)v < lo) + return lo; + return (int)v > hi ? hi : (int)v; +} + +static INT_PTR CALLBACK termset_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) +{ + TermSet *s = (TermSet *)GetWindowLongPtr(d, DWLP_USER); + switch (m) { + case WM_INITDIALOG: + s = (TermSet *)lp; + SetWindowLongPtr(d, DWLP_USER, (LONG_PTR)s); + SetWindowText(d, s->remote ? "Terminal Settings: Remote" : + "Terminal Settings: Modem"); + CheckRadioButton(d, IDC_T_BSH, IDC_T_BSHSH, + IDC_T_BSH + s->o.backspace); + CheckRadioButton(d, IDC_T_TERMKEYS, IDC_T_WINKEYS, + s->o.windows_keys ? IDC_T_WINKEYS : IDC_T_TERMKEYS); + SetDlgItemInt(d, IDC_T_BACKSCROLL, (UINT)s->o.scrollback, FALSE); + SetDlgItemText(d, IDC_T_FONT, s->o.font); + SetDlgItemInt(d, IDC_T_FONTSIZE, (UINT)s->o.font_size, FALSE); + CheckRadioButton(d, IDC_T_BLOCK, IDC_T_UNDERLINE, + s->o.cursor ? IDC_T_UNDERLINE : IDC_T_BLOCK); + CheckDlgButton(d, IDC_T_BLINK, s->o.cursor_blink ? BST_CHECKED : BST_UNCHECKED); + SetDlgItemText(d, IDC_T_TELNETID, s->telnet_id); + EnableWindow(GetDlgItem(d, IDC_T_TELNETID), s->remote); + EnableWindow(GetDlgItem(d, IDC_T_TELNETLABEL), s->remote); + CheckDlgButton(d, IDC_T_EXIT, s->exit_on_disconnect ? BST_CHECKED : BST_UNCHECKED); + EnableWindow(GetDlgItem(d, IDC_T_EXIT), !s->remote); + return TRUE; + case WM_COMMAND: + switch (LOWORD(wp)) { + case IDOK: + for (int i = 0; i < 3; i++) + if (IsDlgButtonChecked(d, IDC_T_BSH + i) == BST_CHECKED) + s->o.backspace = i; + s->o.windows_keys = IsDlgButtonChecked(d, IDC_T_WINKEYS) == BST_CHECKED; + s->o.scrollback = num(d, IDC_T_BACKSCROLL, 0, 100000, 2000); + GetDlgItemText(d, IDC_T_FONT, s->o.font, sizeof(s->o.font)); + s->o.font_size = num(d, IDC_T_FONTSIZE, 6, 72, 10); + s->o.cursor = IsDlgButtonChecked(d, IDC_T_UNDERLINE) == BST_CHECKED; + s->o.cursor_blink = IsDlgButtonChecked(d, IDC_T_BLINK) == BST_CHECKED; + GetDlgItemText(d, IDC_T_TELNETID, s->telnet_id, sizeof(s->telnet_id)); + s->exit_on_disconnect = IsDlgButtonChecked(d, IDC_T_EXIT) == BST_CHECKED; + EndDialog(d, IDOK); + return TRUE; + case IDCANCEL: + EndDialog(d, IDCANCEL); + return TRUE; + } + break; + } + return FALSE; +} + +void termset_dialog(HWND parent, bool remote) +{ + TermSet s; + load(remote, &s); + if (DialogBoxParam(app.inst, MAKEINTRESOURCE(IDD_TERMSET), parent, + termset_proc, (LPARAM)&s) != IDOK) + return; + save(&s); + termset_apply(remote ? *remote_line.view : *term_line.view, remote); +} diff --git a/windows/src/termview.cpp b/windows/src/termview.cpp index 63df435..72dacaa 100644 --- a/windows/src/termview.cpp +++ b/windows/src/termview.cpp @@ -23,6 +23,7 @@ #include #include "app.h" +#include "resource.h" #ifndef WM_MOUSEWHEEL #define WM_MOUSEWHEEL 0x020A @@ -53,6 +54,12 @@ struct TermView { TermPrintedFn pfn; /* what the remote prints goes to it */ void *pctx; bool echo; /* ASCII Setup: what is typed also shown */ + /* Terminal Settings: Backspace as Ctrl+H, Space, Ctrl+H; the + function, arrow and Ctrl keys as Windows keys; the cursor's + shape (0 block, 1 underline) and blink */ + bool bs_three, win_keys, cur_blink; + int cur_shape; + bool eat_char; /* a key done: its WM_CHAR not sent */ HFONT fonts[4]; /* regular, bold, italic, bold italic */ /* a row being drawn */ HDC row_dc; @@ -138,11 +145,27 @@ static void play_music(dmc_vt *vt) /* ------------------------------------------------------------------ */ /* the screen's size */ -static void make_fonts(TermView *t) +/* the fonts made for the view freed (not the app's) */ +static void free_fonts(TermView *t) +{ + for (int i = 1; i < 4; i++) + if (t->fonts[i] && t->fonts[i] != t->fonts[0] && + t->fonts[i] != app.mono_font) + DeleteObject(t->fonts[i]); + if (t->fonts[0] && t->fonts[0] != app.mono_font) + DeleteObject(t->fonts[0]); + for (int i = 0; i < 4; i++) + t->fonts[i] = NULL; +} + +/* the regular font [base] (the app's when NULL), and its bold and + italic */ +static void make_fonts(TermView *t, HFONT base = NULL) { LOGFONT lf; - GetObject(app.mono_font, sizeof(lf), &lf); - t->fonts[0] = app.mono_font; + free_fonts(t); + t->fonts[0] = base ? base : app.mono_font; + GetObject(t->fonts[0], sizeof(lf), &lf); lf.lfWeight = FW_BOLD; t->fonts[1] = CreateFontIndirect(&lf); lf.lfWeight = FW_NORMAL; @@ -152,13 +175,13 @@ static void make_fonts(TermView *t) t->fonts[3] = CreateFontIndirect(&lf); for (int i = 1; i < 4; i++) if (!t->fonts[i]) - t->fonts[i] = app.mono_font; + t->fonts[i] = t->fonts[0]; } static void measure(TermView *t) { HDC dc = GetDC(t->w); - HGDIOBJ old = SelectObject(dc, app.mono_font); + HGDIOBJ old = SelectObject(dc, t->fonts[0]); TEXTMETRIC tm; GetTextMetrics(dc, &tm); t->fcw = tm.tmAveCharWidth > 0 ? tm.tmAveCharWidth : 8; @@ -520,7 +543,15 @@ static void draw_row(TermView *t, int vrow, int ncols, bool cursor_here) int l = cx * cw, r = l + ((cells[cx].flags & DMC_CELL_WIDE) ? 2 * cw : cw); int style = dmc_vt_cursor_style(vt); int th = ch / 8 > 1 ? ch / 8 : 2; - if (!t->focused) { + /* (the remote's DECSCUSR, else the settings': 1 a + blinking block, 2 a block, 3 and 4 an underline) */ + if (style == 0) + style = (t->cur_shape == 1 ? 3 : 1) + (t->cur_blink ? 0 : 1); + if (t->focused && (style & 1)) + t->has_blink = true; + if (t->focused && (style & 1) && t->blink_off) { + /* (blinking: off now) */ + } else if (!t->focused) { px_rect(t, l, 0, r, 1, cc); px_rect(t, l, ch - 1, r, ch, cc); px_rect(t, l, 0, l + 1, ch, cc); @@ -804,7 +835,7 @@ static void paint(TermView *t, HDC dc, RECT *area) int cw = t->cw, ch = t->ch, w = cols * cw; t->has_blink = false; if (row_buffer(t, dc, w, ch)) { - HGDIOBJ oldf = SelectObject(t->row_dc, app.mono_font); + HGDIOBJ oldf = SelectObject(t->row_dc, t->fonts[0]); for (int y = 0; y < rows; y++) { int vrow = y + t->scroll; int size = dmc_vt_line_size(vt, vrow); @@ -839,7 +870,7 @@ static void paint(TermView *t, HDC dc, RECT *area) for (int i = 0; i < n; i++) dx[i] = cw; RECT sr = { 0, 0, w, ch }; - SelectObject(t->row_dc, app.mono_font); + SelectObject(t->row_dc, t->fonts[0]); SetTextColor(t->row_dc, cref(dmc_vt_background(vt))); SetBkColor(t->row_dc, cref(dmc_vt_foreground(vt))); ExtTextOutW(t->row_dc, 0, 0, ETO_OPAQUE | ETO_CLIPPED, &sr, @@ -898,6 +929,11 @@ static void send_key(TermView *t, int key, int mods) { char b[64]; int n = dmc_vt_key_mods(t->vt, key, mods, b, sizeof(b)); + /* Backspace as Ctrl+H, Space, Ctrl+H (the character erased) */ + if (key == DMC_KEY_BACKSPACE && t->bs_three && !mods) { + memcpy(b, "\b \b", 3); + n = 3; + } to_bottom(t); send_text(t, b, n); echo_typed(t, b, n); @@ -1027,6 +1063,51 @@ static int vk_key(WPARAM wp, int mods, TermView *t, HWND w) return 0; } +/* the function, arrow and Ctrl keys as Windows keys: Ctrl+C copies, + Ctrl+V pastes, Ctrl+A selects all, the arrows and Page Up and Down + scroll (the function keys go to the window: the caller's); true when + the key was one of these */ +static bool windows_key(TermView *t, WPARAM wp, int mods) +{ + if (mods == DMC_MOD_CTRL) { + switch (wp) { + case 'C': + copy_selection(t); + return true; + case 'V': + paste(t); + return true; + case 'A': + select_all(t); + return true; + } + } + if (mods & (DMC_MOD_CTRL | DMC_MOD_ALT)) + return false; + switch (wp) { + case VK_UP: + SendMessage(t->w, WM_VSCROLL, SB_LINEUP, 0); + return true; + case VK_DOWN: + SendMessage(t->w, WM_VSCROLL, SB_LINEDOWN, 0); + return true; + case VK_PRIOR: + SendMessage(t->w, WM_VSCROLL, SB_PAGEUP, 0); + return true; + case VK_NEXT: + SendMessage(t->w, WM_VSCROLL, SB_PAGEDOWN, 0); + return true; + case VK_LEFT: + case VK_RIGHT: + case VK_HOME: + case VK_END: + case VK_INSERT: + case VK_DELETE: + return true; + } + return false; +} + static LRESULT CALLBACK view_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) { TermView *t = of(w); @@ -1190,6 +1271,20 @@ static LRESULT CALLBACK view_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) case WM_KEYDOWN: case WM_SYSKEYDOWN: { int mods = mods_now(); + t->eat_char = false; + /* Ctrl+Break: a break, to the page's line */ + if (wp == VK_CANCEL) { + PostMessage(app.main, WM_COMMAND, IDM_BREAK, 0); + t->eat_char = true; + return 0; + } + if (t->win_keys && windows_key(t, wp, mods)) { + t->eat_char = true; + return 0; + } + /* (the function keys to the window: F10 the menu) */ + if (t->win_keys && wp >= VK_F1 && wp <= VK_F24) + break; int key = vk_key(wp, mods, t, w); if (key < 0) return 0; @@ -1214,6 +1309,10 @@ static LRESULT CALLBACK view_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) } break; case WM_CHAR: { + if (t->eat_char) { + t->eat_char = false; + return 0; + } int mods = mods_now() & ~DMC_MOD_ALT; to_bottom(t); switch (wp) { @@ -1253,9 +1352,7 @@ static LRESULT CALLBACK view_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) DeleteObject(t->row_bmp); DeleteDC(t->row_dc); } - for (int i = 1; i < 4; i++) - if (t->fonts[i] != app.mono_font) - DeleteObject(t->fonts[i]); + free_fonts(t); dmc_vt_free(t->vt); free(t); return 0; @@ -1359,6 +1456,38 @@ void termview_set_backspace(HWND w, char b) dmc_vt_set_backspace(t->vt, (unsigned char)b); } +void termview_set_options(HWND w, const TermOptions *o) +{ + TermView *t = of(w); + if (!t) + return; + if (o->backspace >= 0) + dmc_vt_set_backspace(t->vt, o->backspace == 1 ? 0x7f : 0x08); + t->bs_three = o->backspace == 2; + t->win_keys = o->windows_keys; + t->cur_shape = o->cursor; + t->cur_blink = o->cursor_blink; + dmc_vt_set_scrollback(t->vt, o->scrollback); + if (t->scroll < -dmc_vt_history(t->vt)) + t->scroll = -dmc_vt_history(t->vt); + /* the font: the app's when none is named or it can't be made */ + HFONT f = NULL; + if (o->font[0] && o->font_size > 0) { + HDC dc = GetDC(w); + int h = -MulDiv(o->font_size, GetDeviceCaps(dc, LOGPIXELSY), 72); + ReleaseDC(w, dc); + f = CreateFont(h, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, + DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, + CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, + FIXED_PITCH | FF_MODERN, o->font); + } + make_fonts(t, f); + measure(t); + layout(t); + update_scrollbar(t); + InvalidateRect(w, NULL, TRUE); +} + void termview_set_ascii(HWND w, int flags, int enter, bool echo) { TermView *t = of(w); diff --git a/winport/include/windows.h b/winport/include/windows.h index ec3f283..3452e2e 100644 --- a/winport/include/windows.h +++ b/winport/include/windows.h @@ -75,6 +75,8 @@ typedef struct _COMMTIMEOUTS { BOOL WINAPI GetCommState(HANDLE, LPDCB); BOOL WINAPI SetCommState(HANDLE, LPDCB); BOOL WINAPI SetCommTimeouts(HANDLE, LPCOMMTIMEOUTS); +BOOL WINAPI SetCommBreak(HANDLE); +BOOL WINAPI ClearCommBreak(HANDLE); /* anonymous pipes */ BOOL WINAPI CreatePipe(PHANDLE, PHANDLE, LPSECURITY_ATTRIBUTES, DWORD); diff --git a/winport/src/stubs.cpp b/winport/src/stubs.cpp index 1b6f63c..37b8962 100644 --- a/winport/src/stubs.cpp +++ b/winport/src/stubs.cpp @@ -67,6 +67,16 @@ BOOL WINAPI SetCommTimeouts(HANDLE, LPCOMMTIMEOUTS) SetLastError(ERROR_NOT_SUPPORTED); return FALSE; } +BOOL WINAPI SetCommBreak(HANDLE) +{ + SetLastError(ERROR_NOT_SUPPORTED); + return FALSE; +} +BOOL WINAPI ClearCommBreak(HANDLE) +{ + SetLastError(ERROR_NOT_SUPPORTED); + return FALSE; +} /* anonymous pipes: none (no dmodemd started by the app) */ BOOL WINAPI CreatePipe(PHANDLE, PHANDLE, LPSECURITY_ATTRIBUTES, DWORD)