diff --git a/CMakeLists.txt b/CMakeLists.txt index 79fd4a9..b085d3d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ add_subdirectory(core/test) 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.4 vte-2.91-gtk4) + pkg_check_modules(GTK IMPORTED_TARGET gtk4>=4.10 libadwaita-1>=1.5 vte-2.91-gtk4) endif() if(GTK_FOUND) add_subdirectory(linux) diff --git a/assets/dmodem.svg b/assets/dmodem.svg new file mode 100644 index 0000000..66401ab --- /dev/null +++ b/assets/dmodem.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/docs/images/linux-modem.png b/docs/images/linux-modem.png new file mode 100644 index 0000000..863ee6a Binary files /dev/null and b/docs/images/linux-modem.png differ diff --git a/docs/images/linux-settings.png b/docs/images/linux-settings.png new file mode 100644 index 0000000..99f0c4b Binary files /dev/null and b/docs/images/linux-settings.png differ diff --git a/docs/images/linux-setup-screen.png b/docs/images/linux-setup-screen.png new file mode 100644 index 0000000..2bbcbbd Binary files /dev/null and b/docs/images/linux-setup-screen.png differ diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt new file mode 100644 index 0000000..0052053 --- /dev/null +++ b/linux/CMakeLists.txt @@ -0,0 +1,20 @@ +# 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) diff --git a/linux/data/io.github.d_modem.DModem.desktop b/linux/data/io.github.d_modem.DModem.desktop new file mode 100644 index 0000000..56d80da --- /dev/null +++ b/linux/data/io.github.d_modem.DModem.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Name=D-Modem +GenericName=Softmodem +Comment=Dial up over SIP, a USB modem or a softmodem codec +Exec=dmodem-gui +Icon=io.github.d_modem.DModem +Terminal=false +Type=Application +Categories=Network;Dialup;GTK; +Keywords=modem;dial-up;V.92;fax;SIP;AT; +StartupNotify=true diff --git a/linux/data/io.github.d_modem.DModem.metainfo.xml b/linux/data/io.github.d_modem.DModem.metainfo.xml new file mode 100644 index 0000000..dd74b0b --- /dev/null +++ b/linux/data/io.github.d_modem.DModem.metainfo.xml @@ -0,0 +1,24 @@ + + + io.github.d_modem.DModem + CC0-1.0 + BSD-3-Clause + D-Modem + The front panel of the open V.92 softmodem + +

D-Modem is an open softmodem: V.92, V.90, V.34 and the older + modulations, fax and voice, over a SIP call, a USB modem or a + softmodem codec. This app starts it, dials and answers, changes its + settings, shows each call's statistics and gives you a terminal on + it, while your own programs (pppd, minicom, efax) use its device.

+
+ io.github.d_modem.DModem.desktop + https://github.com/D-Modem/gui + + The D-Modem Authors + + + + + +
diff --git a/linux/src/app.h b/linux/src/app.h new file mode 100644 index 0000000..01530c6 --- /dev/null +++ b/linux/src/app.h @@ -0,0 +1,115 @@ +/* + * 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 +#include + +#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 diff --git a/linux/src/config.c b/linux/src/config.c new file mode 100644 index 0000000..6b4cfe8 --- /dev/null +++ b/linux/src/config.c @@ -0,0 +1,316 @@ +/* + * 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 + +#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:@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)); +} diff --git a/linux/src/main.c b/linux/src/main.c new file mode 100644 index 0000000..ca182b9 --- /dev/null +++ b/linux/src/main.c @@ -0,0 +1,48 @@ +/* + * 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; +} diff --git a/linux/src/modem.c b/linux/src/modem.c new file mode 100644 index 0000000..6d9ceee --- /dev/null +++ b/linux/src/modem.c @@ -0,0 +1,429 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include + +#include + +#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); +} diff --git a/linux/src/settings.c b/linux/src/settings.c new file mode 100644 index 0000000..fe35f46 --- /dev/null +++ b/linux/src/settings.c @@ -0,0 +1,355 @@ +/* + * 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 +#include + +#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); +} diff --git a/linux/src/stats.c b/linux/src/stats.c new file mode 100644 index 0000000..a7b8fbd --- /dev/null +++ b/linux/src/stats.c @@ -0,0 +1,94 @@ +/* + * 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; +} diff --git a/linux/src/window.c b/linux/src/window.c new file mode 100644 index 0000000..2430c4c --- /dev/null +++ b/linux/src/window.c @@ -0,0 +1,483 @@ +/* + * 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 + +#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); + 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); +}