/* * SPDX-License-Identifier: AGPL-3.0-only * Copyright (C) 2026 D-Modem Authors * * config.cpp - the line settings, kept in dmodem.ini (in * %APPDATA%\D-Modem, or beside the app where there is no * APPDATA, as on Windows 9x), and the Line Settings dialog */ #include #include #include #include "app.h" #include #include "resource.h" #ifdef DMODEM_WINPORT #include #endif #ifndef OPENFILENAME_SIZE_VERSION_400 #define OPENFILENAME_SIZE_VERSION_400 76 #endif void app_data_dir(char *out, int n) { char dir[MAX_PATH]; #ifdef DMODEM_WINPORT /* (Linux and macOS, winport/: the layer's CSIDL_APPDATA, ~/.config or ~/Library/Application Support; Windows 95 has no SHGetFolderPath, so Windows uses APPDATA) */ if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, dir)) && strlen(dir) < sizeof(dir) - 10) { strcat(dir, "/D-Modem"); CreateDirectory(dir, NULL); _snprintf(out, n, "%s", dir); out[n - 1] = '\0'; return; } #endif DWORD k = GetEnvironmentVariable("APPDATA", dir, sizeof(dir)); if (k > 0 && k < sizeof(dir) - 10) { strcat(dir, "\\D-Modem"); CreateDirectory(dir, NULL); _snprintf(out, n, "%s", dir); } else { GetModuleFileName(NULL, out, n); char *slash = strrchr(out, '\\'); if (slash) *slash = '\0'; } out[n - 1] = '\0'; } static void ini_path(char *out, int n) { char dir[MAX_PATH]; app_data_dir(dir, sizeof(dir)); _snprintf(out, n, "%s\\dmodem.ini", dir); out[n - 1] = '\0'; } static void get(const char *ini, const char *key, const char *def, char *out, int n) { GetPrivateProfileString("line", key, def, out, n, ini); } static int get_i(const char *ini, const char *key, int def) { return (int)GetPrivateProfileInt("line", key, def, ini); } static void put(const char *ini, const char *key, const char *v) { WritePrivateProfileString("line", key, v, ini); } static void put_i(const char *ini, const char *key, int v) { char b[16]; _snprintf(b, sizeof(b), "%d", v); b[sizeof(b) - 1] = '\0'; put(ini, key, b); } void config_load(Config *c) { char ini[MAX_PATH]; ini_path(ini, sizeof(ini)); memset(c, 0, sizeof(*c)); /* (Windows 9x and NT 4 can't run dmodemd: a COM port by default) */ OSVERSIONINFO v; memset(&v, 0, sizeof(v)); v.dwOSVersionInfoSize = sizeof(v); GetVersionEx(&v); bool old = v.dwPlatformId != VER_PLATFORM_WIN32_NT || v.dwMajorVersion < 6; #ifdef DMODEM_WINPORT /* (Linux and macOS, winport/: dmodemd runs there, whatever Windows version the layer reports) */ (void)old; c->line = get_i(ini, "line", LINE_DMODEMD); #else #ifdef DMODEM_GUI_EMBED /* (the modem built in runs on Windows 98 SE and later) */ (void)old; c->line = get_i(ini, "line", LINE_DMODEMD); #else c->line = get_i(ini, "line", old ? LINE_COM : LINE_DMODEMD); #endif #endif c->builtin = get_i(ini, "builtin", 1); get(ini, "sip-user", "", c->sip_user, sizeof(c->sip_user)); get(ini, "sip-password", "", c->sip_password, sizeof(c->sip_password)); get(ini, "sip-server", "", c->sip_server, sizeof(c->sip_server)); c->sip_anonymous = get_i(ini, "sip-anonymous", 0); c->listen = get_i(ini, "listen", 0); c->t38 = get_i(ini, "t38", 1); c->v152 = get_i(ini, "v152", 1); c->v150 = get_i(ini, "v150", 0); get(ini, "country", "", c->country, sizeof(c->country)); get(ini, "usb", "", c->usb, sizeof(c->usb)); get(ini, "dmodemd", "", c->dmodemd, sizeof(c->dmodemd)); get(ini, "tcp", "127.0.0.1:2323", c->tcp, sizeof(c->tcp)); get(ini, "com", "COM1", c->com, sizeof(c->com)); /* (115,200 bps, 8N1, RTS/CTS: as it was before these settings) */ c->com_bps = get_i(ini, "com-bps", 115200); if (c->com_bps < 110) c->com_bps = 115200; c->com_data = get_i(ini, "com-data-bits", 8); if (c->com_data < 5 || c->com_data > 8) c->com_data = 8; c->com_parity = get_i(ini, "com-parity", 0); if (c->com_parity < 0 || c->com_parity > 4) c->com_parity = 0; c->com_stop = get_i(ini, "com-stop-bits", 0); if (c->com_stop < 0 || c->com_stop > 2) c->com_stop = 0; c->com_flow = get_i(ini, "com-flow", 2); if (c->com_flow < 0 || c->com_flow > 2) c->com_flow = 2; get(ini, "init", "", c->init, sizeof(c->init)); /* the answering machine: off until the user turns it on */ c->voicemail = (int)GetPrivateProfileInt("voice", "voicemail", 0, ini); c->vm_rings = (int)GetPrivateProfileInt("voice", "rings", 4, ini); if (c->vm_rings < 1 || c->vm_rings > 15) c->vm_rings = 4; c->vm_max = (int)GetPrivateProfileInt("voice", "max-seconds", 120, ini); if (c->vm_max < 10) c->vm_max = 120; } void config_save(const Config *c) { char ini[MAX_PATH]; ini_path(ini, sizeof(ini)); put_i(ini, "line", c->line); put(ini, "sip-user", c->sip_user); put(ini, "sip-password", c->sip_password); put(ini, "sip-server", c->sip_server); put_i(ini, "sip-anonymous", c->sip_anonymous); put_i(ini, "listen", c->listen); put_i(ini, "t38", c->t38); put_i(ini, "v152", c->v152); put_i(ini, "v150", c->v150); put(ini, "country", c->country); put(ini, "usb", c->usb); put(ini, "dmodemd", c->dmodemd); put_i(ini, "builtin", c->builtin); put(ini, "tcp", c->tcp); put(ini, "com", c->com); put_i(ini, "com-bps", c->com_bps); put_i(ini, "com-data-bits", c->com_data); put_i(ini, "com-parity", c->com_parity); put_i(ini, "com-stop-bits", c->com_stop); put_i(ini, "com-flow", c->com_flow); put(ini, "init", c->init); char b[16]; _snprintf(b, sizeof(b), "%d", c->voicemail); b[sizeof(b) - 1] = '\0'; WritePrivateProfileString("voice", "voicemail", b, ini); _snprintf(b, sizeof(b), "%d", c->vm_rings); b[sizeof(b) - 1] = '\0'; WritePrivateProfileString("voice", "rings", b, ini); _snprintf(b, sizeof(b), "%d", c->vm_max); b[sizeof(b) - 1] = '\0'; WritePrivateProfileString("voice", "max-seconds", b, ini); } void config_summary(const Config *c, char *out, int n) { switch (c->line) { case LINE_TCP: _snprintf(out, n, "dmodemd at %s", c->tcp); break; case LINE_COM: /* (as HyperTerminal's status bar: 115200 8-N-1) */ _snprintf(out, n, "The modem on %s (%d %d-%c-%s)", c->com, c->com_bps, c->com_data, "NOEMS"[c->com_parity], c->com_stop == 1 ? "1.5" : c->com_stop == 2 ? "2" : "1"); break; default: { /* (what runs the line: the modem built in, or dmodemd) */ const char *who = "dmodemd"; #ifdef DMODEM_GUI_EMBED if (c->builtin) who = "Built-in modem"; #endif if (c->usb[0]) _snprintf(out, n, "%s: USB modem (%s)", who, c->usb); else if (c->sip_anonymous) _snprintf(out, n, "%s: SIP, anonymous (no account)%s", who, c->listen ? ", taking calls" : ""); else if (!c->sip_user[0] || !c->sip_server[0]) _snprintf(out, n, "%s: SIP account not set up", who); else _snprintf(out, n, "%s: SIP %s@%s%s", who, c->sip_user, c->sip_server, c->listen ? ", taking calls" : ""); break; } } out[n - 1] = '\0'; } bool config_sip_ready(const Config *c) { return c->sip_anonymous || (c->sip_user[0] && c->sip_server[0]); } /* * the dialog */ static Config *editing; /* the choices of the COM port's settings, as HyperTerminal lists them */ static const int bps_list[] = { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0 }; static const char *const parity_names[] = { "None", "Odd", "Even", "Mark", "Space", NULL }; static const char *const stop_names[] = { "1", "1.5", "2", NULL }; static const char *const flow_names[] = { "None", "Xon / Xoff", "Hardware", NULL }; /* a list of names into a combo box, [sel] chosen */ static void fill(HWND d, int id, const char *const *names, int sel) { HWND c = GetDlgItem(d, id); for (int i = 0; names[i]; i++) SendMessage(c, CB_ADDSTRING, 0, (LPARAM)names[i]); SendMessage(c, CB_SETCURSEL, (WPARAM)sel, 0); } static int chosen(HWND d, int id, int def) { int i = (int)SendDlgItemMessage(d, id, CB_GETCURSEL, 0, 0); return i < 0 ? def : i; } static void enable(HWND d) { bool here = IsDlgButtonChecked(d, IDC_L_DMODEMD) == BST_CHECKED; bool tcp = IsDlgButtonChecked(d, IDC_L_TCP) == BST_CHECKED; bool com = IsDlgButtonChecked(d, IDC_L_COM) == BST_CHECKED; bool anon = IsDlgButtonChecked(d, IDC_L_ANONYMOUS) == BST_CHECKED; static const int account_ids[] = { IDC_L_USER, IDC_L_PASSWORD, IDC_L_SERVER, 0 }; static const int dmodemd_ids[] = { IDC_L_ANONYMOUS, IDC_L_LISTEN, IDC_L_T38, IDC_L_V152, IDC_L_V150, IDC_L_COUNTRY, IDC_L_USB, IDC_L_PATH, IDC_L_BROWSE, 0 }; for (int i = 0; dmodemd_ids[i]; i++) EnableWindow(GetDlgItem(d, dmodemd_ids[i]), here); /* (no account: anonymous, its fields kept but not used) */ for (int i = 0; account_ids[i]; i++) EnableWindow(GetDlgItem(d, account_ids[i]), here && !anon); EnableWindow(GetDlgItem(d, IDC_L_ADDRESS), tcp); EnableWindow(GetDlgItem(d, IDC_L_PORT), com); static const int com_ids[] = { IDC_L_BPS, IDC_L_DATA, IDC_L_PARITY, IDC_L_STOP, IDC_L_FLOW, 0 }; for (int i = 0; com_ids[i]; i++) EnableWindow(GetDlgItem(d, com_ids[i]), com); } static void check(HWND d, int id, int on) { CheckDlgButton(d, id, on ? BST_CHECKED : BST_UNCHECKED); } static int checked(HWND d, int id) { return IsDlgButtonChecked(d, id) == BST_CHECKED; } static void browse(HWND d) { char file[MAX_PATH] = "dmodemd.exe"; OPENFILENAME o; memset(&o, 0, sizeof(o)); /* (the size Windows 95 and NT 4 know) */ o.lStructSize = OPENFILENAME_SIZE_VERSION_400; o.hwndOwner = d; o.lpstrFilter = "dmodemd.exe\0dmodemd.exe\0Programs\0*.exe\0"; o.lpstrFile = file; o.nMaxFile = sizeof(file); o.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; if (GetOpenFileName(&o)) SetDlgItemText(d, IDC_L_PATH, file); } static BOOL CALLBACK line_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) { (void)lp; Config *c = editing; switch (m) { case WM_INITDIALOG: { check(d, IDC_L_DMODEMD, c->line == LINE_DMODEMD); check(d, IDC_L_TCP, c->line == LINE_TCP); check(d, IDC_L_COM, c->line == LINE_COM); SetDlgItemText(d, IDC_L_USER, c->sip_user); SetDlgItemText(d, IDC_L_PASSWORD, c->sip_password); SetDlgItemText(d, IDC_L_SERVER, c->sip_server); check(d, IDC_L_ANONYMOUS, c->sip_anonymous); check(d, IDC_L_LISTEN, c->listen); check(d, IDC_L_T38, c->t38); check(d, IDC_L_V152, c->v152); check(d, IDC_L_V150, c->v150); SetDlgItemText(d, IDC_L_COUNTRY, c->country); SetDlgItemText(d, IDC_L_USB, c->usb); SetDlgItemText(d, IDC_L_PATH, c->dmodemd); SetDlgItemText(d, IDC_L_ADDRESS, c->tcp); HWND port = GetDlgItem(d, IDC_L_PORT); for (int i = 1; i <= 16; i++) { char n[8]; _snprintf(n, sizeof(n), "COM%d", i); n[sizeof(n) - 1] = '\0'; SendMessage(port, CB_ADDSTRING, 0, (LPARAM)n); } SetWindowText(port, c->com); HWND bps = GetDlgItem(d, IDC_L_BPS); for (int i = 0; bps_list[i]; i++) { char n[16]; _snprintf(n, sizeof(n), "%d", bps_list[i]); n[sizeof(n) - 1] = '\0'; SendMessage(bps, CB_ADDSTRING, 0, (LPARAM)n); } SetDlgItemInt(d, IDC_L_BPS, (UINT)c->com_bps, FALSE); static const char *const data_names[] = { "5", "6", "7", "8", NULL }; fill(d, IDC_L_DATA, data_names, c->com_data - 5); fill(d, IDC_L_PARITY, parity_names, c->com_parity); fill(d, IDC_L_STOP, stop_names, c->com_stop); fill(d, IDC_L_FLOW, flow_names, c->com_flow); SetDlgItemText(d, IDC_L_INIT, c->init); enable(d); return TRUE; } case WM_COMMAND: switch (LOWORD(wp)) { case IDC_L_DMODEMD: case IDC_L_TCP: case IDC_L_COM: case IDC_L_ANONYMOUS: enable(d); return TRUE; case IDC_L_BROWSE: browse(d); return TRUE; case IDOK: c->line = checked(d, IDC_L_TCP) ? LINE_TCP : checked(d, IDC_L_COM) ? LINE_COM : LINE_DMODEMD; GetDlgItemText(d, IDC_L_USER, c->sip_user, sizeof(c->sip_user)); GetDlgItemText(d, IDC_L_PASSWORD, c->sip_password, sizeof(c->sip_password)); GetDlgItemText(d, IDC_L_SERVER, c->sip_server, sizeof(c->sip_server)); c->sip_anonymous = checked(d, IDC_L_ANONYMOUS); c->listen = checked(d, IDC_L_LISTEN); c->t38 = checked(d, IDC_L_T38); c->v152 = checked(d, IDC_L_V152); c->v150 = checked(d, IDC_L_V150); GetDlgItemText(d, IDC_L_COUNTRY, c->country, sizeof(c->country)); GetDlgItemText(d, IDC_L_USB, c->usb, sizeof(c->usb)); GetDlgItemText(d, IDC_L_PATH, c->dmodemd, sizeof(c->dmodemd)); GetDlgItemText(d, IDC_L_ADDRESS, c->tcp, sizeof(c->tcp)); GetDlgItemText(d, IDC_L_PORT, c->com, sizeof(c->com)); { char b[16]; GetDlgItemText(d, IDC_L_BPS, b, sizeof(b)); int v = atoi(b); c->com_bps = v >= 110 ? v : 115200; } c->com_data = 5 + chosen(d, IDC_L_DATA, 3); c->com_parity = chosen(d, IDC_L_PARITY, 0); c->com_stop = chosen(d, IDC_L_STOP, 0); c->com_flow = chosen(d, IDC_L_FLOW, 2); GetDlgItemText(d, IDC_L_INIT, c->init, sizeof(c->init)); EndDialog(d, IDOK); return TRUE; case IDCANCEL: EndDialog(d, IDCANCEL); return TRUE; } break; } return FALSE; } bool line_dialog(HWND owner, Config *c) { Config copy = *c; editing = © if (DialogBox(app.inst, MAKEINTRESOURCE(IDD_LINE), owner, (DLGPROC)line_proc) != IDOK) return false; *c = copy; return true; }