From db7b43acd5f30186b9a46a0032bca03c40366d7d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 23 Mar 2018 07:36:18 +1100 Subject: [PATCH] Restore client name after converting to client When Kakoune forked the sever to background, the newly converted to client process (the original client/server process) was not preserving its previous client name. --- src/client_manager.cc | 5 +++-- src/client_manager.hh | 2 +- src/main.cc | 18 ++++++++++-------- src/remote.cc | 6 ++++-- src/remote.hh | 2 +- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/client_manager.cc b/src/client_manager.cc index 9cb5f8e5b..ad9a50096 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -39,7 +39,7 @@ String ClientManager::generate_name() const } Client* ClientManager::create_client(std::unique_ptr&& ui, int pid, - EnvVarMap env_vars, StringView init_cmds, + String name, EnvVarMap env_vars, StringView init_cmds, Optional init_coord, Client::OnExitCallback on_exit) { @@ -47,7 +47,8 @@ Client* ClientManager::create_client(std::unique_ptr&& ui, int pi WindowAndSelections ws = get_free_window(buffer); Client* client = new Client{std::move(ui), std::move(ws.window), std::move(ws.selections), pid, - std::move(env_vars), generate_name(), + std::move(env_vars), + name.empty() ? generate_name() : std::move(name), std::move(on_exit)}; m_clients.emplace_back(client); diff --git a/src/client_manager.hh b/src/client_manager.hh index f6fb30e35..f56fefd4c 100644 --- a/src/client_manager.hh +++ b/src/client_manager.hh @@ -20,7 +20,7 @@ public: ~ClientManager(); Client* create_client(std::unique_ptr&& ui, int pid, - EnvVarMap env_vars, StringView init_cmds, + String name, EnvVarMap env_vars, StringView init_cmds, Optional init_coord, Client::OnExitCallback on_exit); diff --git a/src/main.cc b/src/main.cc index 2dc794be7..f1ae9f815 100644 --- a/src/main.cc +++ b/src/main.cc @@ -511,14 +511,14 @@ std::unique_ptr create_local_ui(UIType ui_type) return std::make_unique(); } -int run_client(StringView session, StringView client_init, +int run_client(StringView session, StringView name, StringView client_init, Optional init_coord, UIType ui_type, bool suspend) { try { EventManager event_manager; - RemoteClient client{session, make_ui(ui_type), getpid(), get_env_vars(), + RemoteClient client{session, name, make_ui(ui_type), getpid(), get_env_vars(), client_init, std::move(init_coord)}; if (suspend) raise(SIGTSTP); @@ -536,6 +536,7 @@ int run_client(StringView session, StringView client_init, struct convert_to_client_mode { String session; + String client_name; String buffer_name; String selections; }; @@ -659,7 +660,7 @@ int run_server(StringView session, StringView server_init, if (not (flags & ServerFlags::Daemon)) { local_client = client_manager.create_client( - create_local_ui(ui_type), getpid(), get_env_vars(), client_init, std::move(init_coord), + create_local_ui(ui_type), getpid(), {}, get_env_vars(), client_init, std::move(init_coord), [](int status) { local_client_exit = status; }); if (startup_error) @@ -693,8 +694,9 @@ int run_server(StringView session, StringView server_init, } else if (convert_to_client_pending) { - String buffer_name = local_client->context().buffer().name(); - String selections = selection_list_to_string(local_client->context().selections()); + const String client_name = local_client->context().name(); + const String buffer_name = local_client->context().buffer().name(); + const String selections = selection_list_to_string(local_client->context().selections()); ClientManager::instance().remove_client(*local_client, true, 0); client_manager.clear_client_trash(); @@ -704,7 +706,7 @@ int run_server(StringView session, StringView server_init, { String session = server.session(); server.close_session(false); - throw convert_to_client_mode{ std::move(session), std::move(buffer_name), std::move(selections) }; + throw convert_to_client_mode{ std::move(session), std::move(client_name), std::move(buffer_name), std::move(selections) }; } } } @@ -985,7 +987,7 @@ int main(int argc, char* argv[]) for (auto name : files) new_files += format("edit '{}';", escape(real_path(name), "'", '\\')); - return run_client(*server_session, new_files + client_init, init_coord, ui_type, false); + return run_client(*server_session, {}, new_files + client_init, init_coord, ui_type, false); } else { @@ -1000,7 +1002,7 @@ int main(int argc, char* argv[]) } catch (convert_to_client_mode& convert) { - return run_client(convert.session, + return run_client(convert.session, convert.client_name, format("try %^buffer '{}'; select '{}'^; echo converted to client only mode", escape(convert.buffer_name, "'^", '\\'), convert.selections), {}, ui_type, true); } diff --git a/src/remote.cc b/src/remote.cc index 12313d835..521663f67 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -566,7 +566,7 @@ bool check_session(StringView session) return connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) != -1; } -RemoteClient::RemoteClient(StringView session, std::unique_ptr&& ui, +RemoteClient::RemoteClient(StringView session, StringView name, std::unique_ptr&& ui, int pid, const EnvVarMap& env_vars, StringView init_command, Optional init_coord) : m_ui(std::move(ui)) @@ -576,6 +576,7 @@ RemoteClient::RemoteClient(StringView session, std::unique_ptr&& { MsgWriter msg{m_send_buffer, MessageType::Connect}; msg.write(pid); + msg.write(name); msg.write(init_command); msg.write(init_coord); msg.write(m_ui->dimensions()); @@ -723,13 +724,14 @@ private: case MessageType::Connect: { auto pid = m_reader.read(); + auto name = m_reader.read(); auto init_cmds = m_reader.read(); auto init_coord = m_reader.read_optional(); auto dimensions = m_reader.read(); auto env_vars = m_reader.read_hash_map(); auto* ui = new RemoteUI{sock, dimensions}; if (auto* client = ClientManager::instance().create_client( - std::unique_ptr(ui), pid, + std::unique_ptr(ui), pid, std::move(name), std::move(env_vars), init_cmds, init_coord, [ui](int status) { ui->exit(status); })) ui->set_client(client); diff --git a/src/remote.hh b/src/remote.hh index ce84cb42d..a8056fca5 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -30,7 +30,7 @@ using RemoteBuffer = Vector; class RemoteClient { public: - RemoteClient(StringView session, std::unique_ptr&& ui, + RemoteClient(StringView session, StringView name, std::unique_ptr&& ui, int pid, const EnvVarMap& env_vars, StringView init_command, Optional init_coord);