diff --git a/src/manager.c b/src/manager.c index 070fba5..86c3ba3 100644 --- a/src/manager.c +++ b/src/manager.c @@ -3427,6 +3427,19 @@ static int repeated(long long *last, int ms) { #include "web.c" +/* the web page's address for the screen ("" when nothing serves it): from whichever instance serves it, or the background + * server's when this plugin's own web page is off */ +static void web_link(const mgr_t *m, char *b, int n) { + pthread_mutex_lock(&web_mu); + snprintf(b, n, "%s", web_url); + pthread_mutex_unlock(&web_mu); + if (!b[0] && m->bg_run) { + char a[48]; + web_addr(a, sizeof a); + if (a[0]) snprintf(b, n, "http://%s:%d", a, WEB_PORT); + } +} + /* the trigger params (a tap), not the option params the host steps on purpose (kind, sort, ...), the keyboard keys or THEME (its own window) */ static int is_button(const char *key) { static const char *const end[] = {"_act", "_open", "_more", "_reinstall", "_remove", "_btn", "_pick1", "_pick2", "_pick3", "_pick4", @@ -3792,6 +3805,13 @@ static int mgr_get_param(void *inst, const char *key, char *b, int n) { if (cp >= 0) snprintf(u, sizeof u, "%d%% used", cp); snprintf(b, n, "%s%s", u, m->clearcache ? " \xc2\xb7 clears on APPLY" : cp >= 85 ? " \xc2\xb7 FULL" : ""); } + else if (!strcmp(key, "hdr_web_txt") || !strcmp(key, "set_web_url")) { /* the PLUGINS header and the SETTINGS web page row */ + char u[64]; + web_link(m, u, sizeof u); + if (!u[0]) snprintf(b, n, " "); + else if (key[0] == 'h') snprintf(b, n, "WEB PAGE %s", u + 7); /* the header: no "http://", browsers add it */ + else snprintf(b, n, "Open %s on your phone or computer", u); + } else if (!strcmp(key, "set_web_txt")) snprintf(b, n, "%s%s", m->web_pref ? "ON" : "OFF", m->web_pref != m->web_on ? " \xc2\xb7 restart" : ""); else if (!strcmp(key, "set_bgsrv_txt")) { if (!m->bg_avail) snprintf(b, n, "not in this build"); diff --git a/src/web.c b/src/web.c index 3e89387..6e4e5be 100644 --- a/src/web.c +++ b/src/web.c @@ -400,6 +400,15 @@ static int web_quit(mgr_t *m) { return q; } +static void web_url_refresh(void) { /* web_url from this MPC's address now: it can change (Wi-Fi joined later, DHCP) */ + char a[48], u[64] = ""; + web_addr(a, sizeof a); + if (a[0]) snprintf(u, sizeof u, "http://%s:%d", a, WEB_PORT); + pthread_mutex_lock(&web_mu); + snprintf(web_url, sizeof web_url, "%s", u); + pthread_mutex_unlock(&web_mu); +} + static void *web_thread(void *ud) { mgr_t *m = ud; int s = -1; @@ -410,9 +419,10 @@ static void *web_thread(void *ud) { int one = 1; struct sockaddr_in a = {.sin_family = AF_INET, .sin_port = htons(WEB_PORT), .sin_addr.s_addr = htonl(INADDR_ANY)}; if (s >= 0) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); - if (s < 0 || bind(s, (struct sockaddr *)&a, sizeof a) || listen(s, 4)) { /* another instance has it */ + if (s < 0 || bind(s, (struct sockaddr *)&a, sizeof a) || listen(s, 4)) { /* another instance or the background server has it */ if (s >= 0) close(s); s = -1; + web_url_refresh(); /* the page is still served, by whoever holds the port: the screen shows its address too */ for (int i = 0; i < 4 && !web_quit(m); i++) usleep(500000); continue; } @@ -420,12 +430,7 @@ static void *web_thread(void *ud) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); if (ts.tv_sec - last >= 5) { - char a[48], u[64] = ""; - web_addr(a, sizeof a); - if (a[0]) snprintf(u, sizeof u, "http://%s:%d", a, WEB_PORT); - pthread_mutex_lock(&web_mu); - snprintf(web_url, sizeof web_url, "%s", u); - pthread_mutex_unlock(&web_mu); + web_url_refresh(); last = ts.tv_sec; } struct pollfd pf = {s, POLLIN, 0}; diff --git a/tests/drive.c b/tests/drive.c index bf14f5c..efcbe67 100644 --- a/tests/drive.c +++ b/tests/drive.c @@ -226,6 +226,7 @@ int main(int argc, char **argv) { press("set_beta_btn"); press("set_web_btn"); usleep(300000); printf("after: beta=%s web=%s\n", get("set_beta_txt"), get("set_web_txt")); + printf("web address: header=|%s| settings=|%s|\n", get("hdr_web_txt"), get("set_web_url")); press("set_beta_btn"); printf("again: beta=%s\n", get("set_beta_txt")); printf("cache: |%s|\n", get("set_cache_txt")); press("set_cache_btn"); diff --git a/tests/run.sh b/tests/run.sh index 8e47152..0ba8e63 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -250,6 +250,10 @@ rm -rf /media /sdcard /nvme /synths /data /tmp/openmpc-pm /tmp/pm-mounts; mkdir sh tests/devices/two-targets.sh PM_CACHE_PCT=99 PM_NO_WEB=1 /tmp/drive_addins settings > /tmp/settings.out grep -qF "before: beta=SHOWN web=ON" /tmp/settings.out && grep -qF "after: beta=HIDDEN web=OFF" /tmp/settings.out && grep -qF "again: beta=SHOWN" /tmp/settings.out || { cat /tmp/settings.out; exit 1; } +# the web page address in the PLUGINS header and on the SETTINGS row while the page is served (users could not find it) +rm -f /tmp/openmpc-pm/.openmpcpm-settings /media/*/.openmpcpm-settings /sdcard/.openmpcpm-settings 2>/dev/null || true +/tmp/drive_addins settings > /tmp/settings_web.out +grep -qE "web address: header=\|WEB PAGE [0-9.]+:8790\| settings=\|Open http://[0-9.]+:8790 on your phone or computer\|" /tmp/settings_web.out || { grep "web address" /tmp/settings_web.out; exit 1; } grep -qF "cache queued: |99% used · clears on APPLY| apply=1" /tmp/settings.out || { cat /tmp/settings.out; exit 1; } grep -qx "beta=1" /media/az01-internal/.openmpcpm-settings && grep -qx "web=0" /media/az01-internal/.openmpcpm-settings || { cat /media/az01-internal/.openmpcpm-settings; exit 1; } # ABOUT: the page names the running version (PM_VERSION) and this MPC, and never claims an installed copy it cannot see diff --git a/vst/layout.conf b/vst/layout.conf index bdc8977..6ad4e3b 100644 --- a/vst/layout.conf +++ b/vst/layout.conf @@ -12,6 +12,8 @@ art_css=skin.css art file=images/bg.svg art file=images/net_on.svg x=930 y=104 w=100 h=24 when=net:online art file=images/net_off.svg x=930 y=104 w=100 h=24 when=net:offline +# the web page's address, always in view (users could not find it): left of the ONLINE pill, blank when nothing serves it +readout box=0 w=380 h=23 cx=725 cy=107 label="" key=hdr_web_txt tsize=21 tcolor=9da1a7 talign=right tpad=0 readout box=0 w=216 h=23 cx=1148 cy=107 label="" key=disk_txt tsize=21 tcolor=ececed talign=right tpad=0 # more than one place to install to: the readout names the drive ("CARD · 12.0 GB ▾") and a tap opens the drive menu (the engine ignores it otherwise) button cx=1148 cy=112 label="" key=disk_btn img=images/disk_hit.svg w=216 h=40 @@ -1780,6 +1782,7 @@ readout box=0 w=260 h=30 cx=930 cy=297 label="" key=set_beta_txt tsize=22 tweigh art file=images/set_row_web.svg x=24 y=346 w=1232 h=82 button cx=1160 cy=387 label="" key=set_web_btn img=images/set_btn.svg w=150 h=46 readout box=0 w=260 h=30 cx=930 cy=387 label="" key=set_web_txt tsize=22 tweight=bold tcolor=ececed talign=right tpad=0 +readout box=0 w=560 h=28 cx=500 cy=374 label="" key=set_web_url tsize=22 tweight=bold tcolor=ececed talign=left tpad=0 art file=images/set_row_bgsrv.svg x=24 y=436 w=1232 h=82 button cx=1160 cy=477 label="" key=set_bgsrv_btn img=images/set_btn.svg w=150 h=46 readout box=0 w=300 h=30 cx=890 cy=477 label="" key=set_bgsrv_txt tsize=22 tweight=bold tcolor=ececed talign=right tpad=0 diff --git a/vst/params.json b/vst/params.json index 446c63a..505d0d2 100644 --- a/vst/params.json +++ b/vst/params.json @@ -4648,6 +4648,18 @@ "name": "Setting: background server button", "type": "trigger", "momentary": true + }, + { + "key": "hdr_web_txt", + "name": "Header: web page address", + "type": "readout", + "display": "string" + }, + { + "key": "set_web_url", + "name": "Setting: web page address", + "type": "readout", + "display": "string" } ] }