xW: show a connect dialog when run without args

This commit is contained in:
2023-07-27 01:27:24 +02:00
parent efb25b8aae
commit 3e37efd9cd
3 changed files with 71 additions and 25 deletions

View File

@@ -113,6 +113,9 @@ struct {
// Networking:
std::wstring host; ///< Host as given by user
std::wstring port; ///< Port/service as given by user
addrinfoW *addresses; ///< GetAddrInfo() result
addrinfoW *addresses_iterator; ///< Currently processed address
SOCKET socket; ///< Relay socket
@@ -153,6 +156,15 @@ format_error_message(int err)
return copy;
}
static std::wstring
window_get_text(HWND hWnd)
{
int length = GetWindowTextLength(hWnd);
std::wstring buffer(length, {});
GetWindowText(hWnd, buffer.data(), length + 1);
return buffer;
}
// --- Networking --------------------------------------------------------------
static bool
@@ -411,10 +423,7 @@ refresh_status()
}
// Buffer scrolling would cause a ton of flickering redraws.
int length = GetWindowTextLength(g.hwndStatus);
std::wstring buffer(length, {});
GetWindowText(g.hwndStatus, buffer.data(), length + 1);
if (buffer != status)
if (window_get_text(g.hwndStatus) != status)
SetWindowText(g.hwndStatus, status.c_str());
}
@@ -811,8 +820,6 @@ relay_process_callbacks(uint32_t command_seq,
}
}
static std::wstring input_get_contents();
static void
relay_process_message(const Relay::EventMessage &m)
{
@@ -941,7 +948,7 @@ relay_process_message(const Relay::EventMessage &m)
old->new_unimportant_messages = 0;
old->highlighted = false;
old->input = input_get_contents();
old->input = window_get_text(g.hwndInput);
SendMessage(g.hwndInput, EM_GETSEL,
(WPARAM) &old->input_start, (LPARAM) &old->input_end);
@@ -1183,15 +1190,6 @@ relay_process_socket_events(std::wstring &error)
// --- Input line --------------------------------------------------------------
static std::wstring
input_get_contents()
{
int length = GetWindowTextLength(g.hwndInput);
std::wstring buffer(length, {});
GetWindowText(g.hwndInput, buffer.data(), length + 1);
return buffer;
}
static void
input_set_contents(const std::wstring &input)
{
@@ -1209,7 +1207,7 @@ input_submit()
auto input = new Relay::CommandData_BufferInput();
input->buffer_name = b->buffer_name;
input->text = input_get_contents();
input->text = window_get_text(g.hwndInput);
// Buffer::history[Buffer::history.size()] is virtual,
// and is represented either by edit contents when it's currently
@@ -1233,7 +1231,7 @@ input_stamp()
{
DWORD start = {}, end = {};
SendMessage(g.hwndInput, EM_GETSEL, (WPARAM) &start, (LPARAM) &end);
return {start, end, input_get_contents()};
return {start, end, window_get_text(g.hwndInput)};
}
static void
@@ -1340,7 +1338,7 @@ input_proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
if (b->history_at < 1)
break;
if (b->history_at == b->history.size())
b->input = input_get_contents();
b->input = window_get_text(g.hwndInput);
input_set_contents(b->history.at(--b->history_at));
return 0;
}
@@ -1705,6 +1703,26 @@ window_proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
static INT_PTR CALLBACK
connect_proc(
HWND hDlg, UINT uMsg, WPARAM wParam, [[maybe_unused]] LPARAM lParam)
{
switch (uMsg) {
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
g.host = window_get_text(GetDlgItem(hDlg, IDC_HOST));
g.port = window_get_text(GetDlgItem(hDlg, IDC_PORT));
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
}
return FALSE;
}
static void
get_font()
{
@@ -1883,11 +1901,14 @@ wWinMain(HINSTANCE hInstance, [[maybe_unused]] HINSTANCE hPrevInstance,
int argc = 0;
LPWSTR *argv = CommandLineToArgvW(pCmdLine, &argc);
if (argc < 2) {
show_error_message(
L"You must pass the relay address and port on the command line.");
return 1;
if (argc >= 2) {
g.host = argv[0];
g.port = argv[1];
} else if (DialogBox(hInstance, MAKEINTRESOURCE(IDD_CONNECT),
g.hwndMain, connect_proc) != IDOK) {
return 0;
}
LocalFree(argv);
// We have a few suboptimal asynchronous options:
// a) WSAAsyncGetHostByName() requires us to distinguish hostnames
@@ -1898,8 +1919,7 @@ wWinMain(HINSTANCE hInstance, [[maybe_unused]] HINSTANCE hPrevInstance,
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
err = GetAddrInfo(argv[0], argv[1], &hints, &g.addresses);
LocalFree(argv);
err = GetAddrInfo(g.host.c_str(), g.port.c_str(), &hints, &g.addresses);
if (err) {
show_error_message(format_error_message(err).c_str());
return 1;