| 2101 | } |
| 2102 | |
| 2103 | static void main_daemon_ctl_open_browser(int port) { |
| 2104 | char url[64]; |
| 2105 | (void)snprintf(url, sizeof(url), "http://127.0.0.1:%d", port); |
| 2106 | #ifdef CBM_ENABLE_TEST_SEAMS |
| 2107 | char marker_path[MAIN_PATH_CAP]; |
| 2108 | if (cbm_safe_getenv("CBM_TEST_DAEMON_OPEN_MARKER", marker_path, sizeof(marker_path), NULL)) { |
| 2109 | FILE *marker = cbm_fopen(marker_path, "wb"); |
| 2110 | bool opened = marker && fwrite(url, 1, strlen(url), marker) == strlen(url); |
| 2111 | if (marker && fclose(marker) != 0) { |
| 2112 | opened = false; |
| 2113 | } |
| 2114 | if (!opened) { |
| 2115 | (void)fprintf(stderr, "hint: could not record the test browser request\n"); |
| 2116 | } |
| 2117 | return; |
| 2118 | } |
| 2119 | #endif |
| 2120 | #if defined(_WIN32) |
| 2121 | /* ShellExecuteW resolves the http protocol association directly — no |
| 2122 | * command shell interprets the argument. Values > 32 signal success. */ |
| 2123 | wchar_t *wide_url = cbm_utf8_to_wide(url); |
| 2124 | bool opened = |
| 2125 | wide_url && (INT_PTR)ShellExecuteW(NULL, L"open", wide_url, NULL, NULL, SW_SHOWNORMAL) > 32; |
| 2126 | free(wide_url); |
| 2127 | #elif defined(__APPLE__) |
| 2128 | const char *open_argv[] = {"open", url, NULL}; |
| 2129 | bool opened = cbm_exec_no_shell(open_argv) == 0; |
| 2130 | #else |
| 2131 | const char *open_argv[] = {"xdg-open", url, NULL}; |
| 2132 | bool opened = cbm_exec_no_shell(open_argv) == 0; |
| 2133 | #endif |
| 2134 | if (!opened) { |
| 2135 | (void)fprintf(stderr, "hint: could not open a browser automatically; visit %s\n", url); |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | static int main_daemon_ctl_finish_ui_open(cbm_daemon_runtime_client_t **client_io, int port, |
| 2140 | bool open_browser) { |
no test coverage detected