2012-10-23 22:55:44 +02:00
|
|
|
#include "remote.hh"
|
|
|
|
|
2013-03-12 18:53:18 +01:00
|
|
|
#include "buffer_manager.hh"
|
2015-06-06 12:54:48 +02:00
|
|
|
#include "buffer_utils.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "client_manager.hh"
|
2013-03-12 18:53:18 +01:00
|
|
|
#include "command_manager.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "display_buffer.hh"
|
|
|
|
#include "event_manager.hh"
|
2012-10-23 22:55:44 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2013-03-13 19:59:39 +01:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class RemoteUIMsg
|
|
|
|
{
|
|
|
|
MenuShow,
|
|
|
|
MenuSelect,
|
|
|
|
MenuHide,
|
2012-12-14 19:04:34 +01:00
|
|
|
InfoShow,
|
|
|
|
InfoHide,
|
2014-04-15 20:19:44 +02:00
|
|
|
Draw,
|
2014-11-11 00:29:16 +01:00
|
|
|
Refresh,
|
|
|
|
SetOptions
|
2012-10-23 22:55:44 +02:00
|
|
|
};
|
|
|
|
|
2012-10-26 13:45:32 +02:00
|
|
|
struct socket_error{};
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
class Message
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Message(int sock) : m_socket(sock) {}
|
2014-10-03 14:39:13 +02:00
|
|
|
~Message() noexcept(false)
|
2012-10-26 13:45:32 +02:00
|
|
|
{
|
|
|
|
if (m_stream.size() == 0)
|
|
|
|
return;
|
|
|
|
int res = ::write(m_socket, m_stream.data(), m_stream.size());
|
|
|
|
if (res == 0)
|
|
|
|
throw peer_disconnected{};
|
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
|
|
|
void write(const char* val, size_t size)
|
|
|
|
{
|
|
|
|
m_stream.insert(m_stream.end(), val, val + size);
|
|
|
|
}
|
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
template<typename T>
|
|
|
|
void write(const T& val)
|
|
|
|
{
|
|
|
|
write((const char*)&val, sizeof(val));
|
2013-04-03 19:20:38 +02:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2014-04-30 20:39:52 +02:00
|
|
|
void write(StringView str)
|
2012-12-10 18:55:11 +01:00
|
|
|
{
|
|
|
|
write(str.length());
|
2014-04-30 20:39:52 +02:00
|
|
|
write(str.data(), (int)str.length());
|
2012-12-10 18:55:11 +01:00
|
|
|
};
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2014-04-30 20:39:52 +02:00
|
|
|
void write(const String& str)
|
|
|
|
{
|
|
|
|
write(StringView{str});
|
|
|
|
}
|
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
template<typename T>
|
2015-03-09 14:48:41 +01:00
|
|
|
void write(ConstArrayView<T> view)
|
2012-12-10 18:55:11 +01:00
|
|
|
{
|
|
|
|
write<uint32_t>(view.size());
|
|
|
|
for (auto& val : view)
|
|
|
|
write(val);
|
2013-04-03 19:20:38 +02:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2015-01-14 20:16:32 +01:00
|
|
|
template<typename T, MemoryDomain domain>
|
|
|
|
void write(const Vector<T, domain>& vec)
|
2012-12-10 18:55:11 +01:00
|
|
|
{
|
2015-03-09 14:48:41 +01:00
|
|
|
write(ConstArrayView<T>(vec));
|
2012-12-10 18:55:11 +01:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2015-01-14 20:16:32 +01:00
|
|
|
template<typename Key, typename Val, MemoryDomain domain>
|
|
|
|
void write(const UnorderedMap<Key, Val, domain>& map)
|
2014-04-07 22:25:44 +02:00
|
|
|
{
|
|
|
|
write<uint32_t>(map.size());
|
|
|
|
for (auto& val : map)
|
|
|
|
{
|
|
|
|
write(val.first);
|
|
|
|
write(val.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 00:26:43 +02:00
|
|
|
void write(Color color)
|
2013-05-07 18:52:23 +02:00
|
|
|
{
|
|
|
|
write(color.color);
|
2015-04-25 11:47:39 +02:00
|
|
|
if (color.color == Color::RGB)
|
2013-05-07 18:52:23 +02:00
|
|
|
{
|
|
|
|
write(color.r);
|
|
|
|
write(color.g);
|
|
|
|
write(color.b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
void write(Face face)
|
2013-04-04 13:53:47 +02:00
|
|
|
{
|
2014-07-11 01:27:04 +02:00
|
|
|
write(face.fg);
|
|
|
|
write(face.bg);
|
|
|
|
write(face.attributes);
|
2013-04-04 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
void write(const DisplayAtom& atom)
|
|
|
|
{
|
2013-07-24 14:55:57 +02:00
|
|
|
write(atom.content());
|
2014-07-11 01:27:04 +02:00
|
|
|
write(atom.face);
|
2012-12-10 18:55:11 +01:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
void write(const DisplayLine& line)
|
|
|
|
{
|
|
|
|
write(line.atoms());
|
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
void write(const DisplayBuffer& display_buffer)
|
|
|
|
{
|
2015-06-04 19:51:00 +02:00
|
|
|
write(display_buffer.default_face());
|
2012-12-10 18:55:11 +01:00
|
|
|
write(display_buffer.lines());
|
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
private:
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<char> m_stream;
|
2012-12-10 18:55:11 +01:00
|
|
|
int m_socket;
|
|
|
|
};
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-10-26 13:45:32 +02:00
|
|
|
void read(int socket, char* buffer, size_t size)
|
|
|
|
{
|
2012-11-19 19:07:32 +01:00
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
int res = ::read(socket, buffer, size);
|
|
|
|
if (res == 0)
|
|
|
|
throw peer_disconnected{};
|
|
|
|
if (res < 0)
|
|
|
|
throw socket_error{};
|
|
|
|
|
|
|
|
buffer += res;
|
|
|
|
size -= res;
|
|
|
|
}
|
2012-10-26 13:45:32 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
template<typename T>
|
|
|
|
T read(int socket)
|
|
|
|
{
|
2013-07-31 00:32:51 +02:00
|
|
|
union U
|
2013-07-26 01:30:01 +02:00
|
|
|
{
|
|
|
|
T object;
|
2014-11-05 14:50:06 +01:00
|
|
|
alignas(T) char data[sizeof(T)];
|
2013-07-31 00:32:51 +02:00
|
|
|
U() {}
|
|
|
|
~U() { object.~T(); }
|
|
|
|
} u;
|
|
|
|
read(socket, u.data, sizeof(T));
|
|
|
|
return u.object;
|
2013-04-03 19:20:38 +02:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
|
|
|
template<>
|
|
|
|
String read<String>(int socket)
|
|
|
|
{
|
|
|
|
ByteCount length = read<ByteCount>(socket);
|
2014-04-23 23:21:04 +02:00
|
|
|
String res;
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
res.resize((int)length);
|
2014-08-11 20:40:35 +02:00
|
|
|
read(socket, &res[0_byte], (int)length);
|
2014-04-23 23:21:04 +02:00
|
|
|
}
|
|
|
|
return res;
|
2013-04-03 19:20:38 +02:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<T> read_vector(int socket)
|
2012-10-23 22:55:44 +02:00
|
|
|
{
|
|
|
|
uint32_t size = read<uint32_t>(socket);
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<T> res;
|
2012-10-23 22:55:44 +02:00
|
|
|
res.reserve(size);
|
|
|
|
while (size--)
|
|
|
|
res.push_back(read<T>(socket));
|
2013-05-07 18:52:23 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
Color read<Color>(int socket)
|
|
|
|
{
|
|
|
|
Color res;
|
2015-04-25 11:47:39 +02:00
|
|
|
res.color = read<Color::NamedColor>(socket);
|
|
|
|
if (res.color == Color::RGB)
|
2013-05-07 18:52:23 +02:00
|
|
|
{
|
|
|
|
res.r = read<unsigned char>(socket);
|
|
|
|
res.g = read<unsigned char>(socket);
|
|
|
|
res.b = read<unsigned char>(socket);
|
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
return res;
|
2013-04-03 19:20:38 +02:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2013-04-04 13:53:47 +02:00
|
|
|
template<>
|
2014-07-11 01:27:04 +02:00
|
|
|
Face read<Face>(int socket)
|
2013-04-04 13:53:47 +02:00
|
|
|
{
|
2014-07-11 01:27:04 +02:00
|
|
|
Face res;
|
|
|
|
res.fg = read<Color>(socket);
|
|
|
|
res.bg = read<Color>(socket);
|
|
|
|
res.attributes = read<Attribute>(socket);
|
2013-04-04 13:53:47 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
template<>
|
|
|
|
DisplayAtom read<DisplayAtom>(int socket)
|
|
|
|
{
|
2013-07-24 14:55:57 +02:00
|
|
|
DisplayAtom atom(read<String>(socket));
|
2014-07-11 01:27:04 +02:00
|
|
|
atom.face = read<Face>(socket);
|
2012-10-23 22:55:44 +02:00
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
template<>
|
|
|
|
DisplayLine read<DisplayLine>(int socket)
|
|
|
|
{
|
2013-07-23 20:11:26 +02:00
|
|
|
return DisplayLine(read_vector<DisplayAtom>(socket));
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
DisplayBuffer read<DisplayBuffer>(int socket)
|
|
|
|
{
|
|
|
|
DisplayBuffer db;
|
2015-06-04 19:51:00 +02:00
|
|
|
db.set_default_face(read<Face>(socket));
|
2012-10-23 22:55:44 +02:00
|
|
|
db.lines() = read_vector<DisplayLine>(socket);
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:16:32 +01:00
|
|
|
template<typename Key, typename Val, MemoryDomain domain>
|
|
|
|
UnorderedMap<Key, Val, domain> read_map(int socket)
|
2014-04-07 22:25:44 +02:00
|
|
|
{
|
|
|
|
uint32_t size = read<uint32_t>(socket);
|
2015-01-14 20:16:32 +01:00
|
|
|
UnorderedMap<Key, Val, domain> res;
|
2014-04-07 22:25:44 +02:00
|
|
|
while (size--)
|
|
|
|
{
|
|
|
|
auto key = read<Key>(socket);
|
|
|
|
auto val = read<Val>(socket);
|
|
|
|
res.insert({std::move(key), std::move(val)});
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-12-18 21:20:36 +01:00
|
|
|
class RemoteUI : public UserInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RemoteUI(int socket);
|
|
|
|
~RemoteUI();
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
void menu_show(ConstArrayView<String> choices,
|
2014-07-11 01:27:04 +02:00
|
|
|
CharCoord anchor, Face fg, Face bg,
|
2013-04-04 13:53:47 +02:00
|
|
|
MenuStyle style) override;
|
2012-12-18 21:20:36 +01:00
|
|
|
void menu_select(int selected) override;
|
|
|
|
void menu_hide() override;
|
|
|
|
|
2014-04-30 20:39:52 +02:00
|
|
|
void info_show(StringView title, StringView content,
|
2014-07-11 01:27:04 +02:00
|
|
|
CharCoord anchor, Face face,
|
2014-11-08 18:59:38 +01:00
|
|
|
InfoStyle style) override;
|
2012-12-18 21:20:36 +01:00
|
|
|
void info_hide() override;
|
|
|
|
|
|
|
|
void draw(const DisplayBuffer& display_buffer,
|
2013-09-16 20:15:13 +02:00
|
|
|
const DisplayLine& status_line,
|
2013-04-04 18:50:00 +02:00
|
|
|
const DisplayLine& mode_line) override;
|
2012-12-18 21:20:36 +01:00
|
|
|
|
2014-04-15 20:19:44 +02:00
|
|
|
void refresh() override;
|
|
|
|
|
2012-12-18 21:20:36 +01:00
|
|
|
bool is_key_available() override;
|
|
|
|
Key get_key() override;
|
2014-05-07 20:51:01 +02:00
|
|
|
CharCoord dimensions() override;
|
2012-12-18 21:20:36 +01:00
|
|
|
|
2013-01-11 19:17:21 +01:00
|
|
|
void set_input_callback(InputCallback callback) override;
|
|
|
|
|
2014-11-11 00:29:16 +01:00
|
|
|
void set_ui_options(const Options& options) override;
|
|
|
|
|
2012-12-18 21:20:36 +01:00
|
|
|
private:
|
2013-01-11 19:17:21 +01:00
|
|
|
FDWatcher m_socket_watcher;
|
2014-05-07 20:51:01 +02:00
|
|
|
CharCoord m_dimensions;
|
2013-01-11 19:17:21 +01:00
|
|
|
InputCallback m_input_callback;
|
2012-12-18 21:20:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-11-19 19:06:32 +01:00
|
|
|
RemoteUI::RemoteUI(int socket)
|
2014-11-25 02:00:18 +01:00
|
|
|
: m_socket_watcher(socket, [this](FDWatcher&, EventMode mode) {
|
2014-04-05 13:04:37 +02:00
|
|
|
if (m_input_callback)
|
2014-11-25 02:00:18 +01:00
|
|
|
m_input_callback(mode);
|
2014-04-05 13:04:37 +02:00
|
|
|
})
|
2012-11-19 19:06:32 +01:00
|
|
|
{
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer(format("remote client connected: {}", m_socket_watcher.fd()));
|
2012-11-19 19:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteUI::~RemoteUI()
|
|
|
|
{
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer(format("remote client disconnected: {}", m_socket_watcher.fd()));
|
2014-12-03 14:56:02 +01:00
|
|
|
m_socket_watcher.close_fd();
|
2012-11-19 19:06:32 +01:00
|
|
|
}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
void RemoteUI::menu_show(ConstArrayView<String> choices,
|
2014-07-11 01:27:04 +02:00
|
|
|
CharCoord anchor, Face fg, Face bg,
|
2013-04-04 13:53:47 +02:00
|
|
|
MenuStyle style)
|
2012-10-23 22:55:44 +02:00
|
|
|
{
|
2013-01-11 19:17:21 +01:00
|
|
|
Message msg(m_socket_watcher.fd());
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(RemoteUIMsg::MenuShow);
|
|
|
|
msg.write(choices);
|
|
|
|
msg.write(anchor);
|
2013-04-04 13:53:47 +02:00
|
|
|
msg.write(fg);
|
|
|
|
msg.write(bg);
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(style);
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_select(int selected)
|
|
|
|
{
|
2013-01-11 19:17:21 +01:00
|
|
|
Message msg(m_socket_watcher.fd());
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(RemoteUIMsg::MenuSelect);
|
|
|
|
msg.write(selected);
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_hide()
|
|
|
|
{
|
2013-01-11 19:17:21 +01:00
|
|
|
Message msg(m_socket_watcher.fd());
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(RemoteUIMsg::MenuHide);
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 20:39:52 +02:00
|
|
|
void RemoteUI::info_show(StringView title, StringView content,
|
2014-07-11 01:27:04 +02:00
|
|
|
CharCoord anchor, Face face,
|
2014-11-08 18:59:38 +01:00
|
|
|
InfoStyle style)
|
2012-12-14 19:04:34 +01:00
|
|
|
{
|
2013-01-11 19:17:21 +01:00
|
|
|
Message msg(m_socket_watcher.fd());
|
2012-12-14 19:04:34 +01:00
|
|
|
msg.write(RemoteUIMsg::InfoShow);
|
2013-10-10 23:51:16 +02:00
|
|
|
msg.write(title);
|
2012-12-14 19:04:34 +01:00
|
|
|
msg.write(content);
|
|
|
|
msg.write(anchor);
|
2014-07-11 01:27:04 +02:00
|
|
|
msg.write(face);
|
2012-12-14 19:04:34 +01:00
|
|
|
msg.write(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::info_hide()
|
|
|
|
{
|
2013-01-11 19:17:21 +01:00
|
|
|
Message msg(m_socket_watcher.fd());
|
2012-12-14 19:04:34 +01:00
|
|
|
msg.write(RemoteUIMsg::InfoHide);
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
void RemoteUI::draw(const DisplayBuffer& display_buffer,
|
2013-09-16 20:15:13 +02:00
|
|
|
const DisplayLine& status_line,
|
2013-04-04 18:50:00 +02:00
|
|
|
const DisplayLine& mode_line)
|
2012-10-23 22:55:44 +02:00
|
|
|
{
|
2013-01-11 19:17:21 +01:00
|
|
|
Message msg(m_socket_watcher.fd());
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(RemoteUIMsg::Draw);
|
|
|
|
msg.write(display_buffer);
|
2013-09-16 20:15:13 +02:00
|
|
|
msg.write(status_line);
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(mode_line);
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
2014-04-15 20:19:44 +02:00
|
|
|
void RemoteUI::refresh()
|
|
|
|
{
|
|
|
|
Message msg(m_socket_watcher.fd());
|
|
|
|
msg.write(RemoteUIMsg::Refresh);
|
|
|
|
}
|
|
|
|
|
2014-11-11 00:29:16 +01:00
|
|
|
void RemoteUI::set_ui_options(const Options& options)
|
|
|
|
{
|
|
|
|
Message msg(m_socket_watcher.fd());
|
|
|
|
msg.write(RemoteUIMsg::SetOptions);
|
|
|
|
msg.write(options);
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:51:01 +02:00
|
|
|
static const Key::Modifiers resize_modifier = (Key::Modifiers)0x80;
|
|
|
|
|
2012-10-28 09:26:54 +01:00
|
|
|
bool RemoteUI::is_key_available()
|
|
|
|
{
|
|
|
|
timeval tv;
|
|
|
|
fd_set rfds;
|
|
|
|
|
2013-01-11 19:17:21 +01:00
|
|
|
int sock = m_socket_watcher.fd();
|
2012-10-28 09:26:54 +01:00
|
|
|
FD_ZERO(&rfds);
|
2013-01-11 19:17:21 +01:00
|
|
|
FD_SET(sock, &rfds);
|
2012-10-28 09:26:54 +01:00
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
2013-07-28 16:40:02 +02:00
|
|
|
int res = select(sock+1, &rfds, nullptr, nullptr, &tv);
|
2012-10-28 09:26:54 +01:00
|
|
|
return res == 1;
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
Key RemoteUI::get_key()
|
|
|
|
{
|
2013-08-28 20:19:54 +02:00
|
|
|
try
|
2012-10-25 12:51:01 +02:00
|
|
|
{
|
2013-08-28 20:19:54 +02:00
|
|
|
Key key = read<Key>(m_socket_watcher.fd());
|
|
|
|
if (key.modifiers == resize_modifier)
|
|
|
|
{
|
|
|
|
m_dimensions = { (int)(key.key >> 16), (int)(key.key & 0xFFFF) };
|
|
|
|
return Key::Invalid;
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
catch (peer_disconnected&)
|
|
|
|
{
|
|
|
|
throw client_removed{};
|
|
|
|
}
|
|
|
|
catch (socket_error&)
|
|
|
|
{
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer("ungraceful deconnection detected");
|
2013-08-28 20:19:54 +02:00
|
|
|
throw client_removed{};
|
2012-10-25 12:51:01 +02:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
CharCoord RemoteUI::dimensions()
|
2012-10-23 22:55:44 +02:00
|
|
|
{
|
|
|
|
return m_dimensions;
|
|
|
|
}
|
|
|
|
|
2013-01-11 19:17:21 +01:00
|
|
|
void RemoteUI::set_input_callback(InputCallback callback)
|
|
|
|
{
|
|
|
|
m_input_callback = std::move(callback);
|
|
|
|
}
|
|
|
|
|
2014-11-06 14:54:18 +01:00
|
|
|
static sockaddr_un session_addr(StringView session)
|
2012-10-25 12:51:01 +02:00
|
|
|
{
|
2014-11-06 14:54:18 +01:00
|
|
|
sockaddr_un addr;
|
|
|
|
addr.sun_family = AF_UNIX;
|
2015-06-01 22:15:59 +02:00
|
|
|
strncpy(addr.sun_path, format("/tmp/kak-{}", session).c_str(),
|
2014-11-06 14:54:18 +01:00
|
|
|
sizeof(addr.sun_path) - 1);
|
|
|
|
return addr;
|
|
|
|
}
|
2014-11-05 14:57:12 +01:00
|
|
|
|
2014-11-06 14:54:18 +01:00
|
|
|
static int connect_to(StringView session)
|
|
|
|
{
|
2014-11-05 14:57:12 +01:00
|
|
|
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
fcntl(sock, F_SETFD, FD_CLOEXEC);
|
2014-11-06 14:54:18 +01:00
|
|
|
sockaddr_un addr = session_addr(session);
|
2014-11-05 14:57:12 +01:00
|
|
|
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
|
2014-11-06 14:54:18 +01:00
|
|
|
throw connection_failed(addr.sun_path);
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
|
|
|
|
const EnvVarMap& env_vars, StringView init_command)
|
|
|
|
: m_ui(std::move(ui)), m_dimensions(m_ui->dimensions())
|
|
|
|
{
|
|
|
|
int sock = connect_to(session);
|
2014-04-07 22:25:44 +02:00
|
|
|
|
2014-11-05 14:57:12 +01:00
|
|
|
{
|
|
|
|
Message msg(sock);
|
|
|
|
msg.write(init_command.data(), (int)init_command.length());
|
|
|
|
msg.write((char)0);
|
|
|
|
msg.write(env_vars);
|
|
|
|
|
|
|
|
Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) |
|
|
|
|
(int)m_dimensions.column) };
|
|
|
|
msg.write(key);
|
|
|
|
}
|
2013-01-11 19:17:21 +01:00
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
m_ui->set_input_callback([this](EventMode){ write_next_key(); });
|
2014-11-05 14:57:12 +01:00
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
m_socket_watcher.reset(new FDWatcher{sock, [this](FDWatcher&, EventMode){ process_available_messages(); }});
|
2012-10-25 12:51:01 +02:00
|
|
|
}
|
|
|
|
|
2014-04-15 20:08:47 +02:00
|
|
|
void RemoteClient::process_available_messages()
|
|
|
|
{
|
2014-11-05 14:57:12 +01:00
|
|
|
int socket = m_socket_watcher->fd();
|
2014-04-15 20:08:47 +02:00
|
|
|
timeval tv{ 0, 0 };
|
|
|
|
fd_set rfds;
|
|
|
|
|
|
|
|
do {
|
|
|
|
process_next_message();
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(socket, &rfds);
|
|
|
|
} while (select(socket+1, &rfds, nullptr, nullptr, &tv) == 1);
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
void RemoteClient::process_next_message()
|
|
|
|
{
|
2014-11-05 14:57:12 +01:00
|
|
|
int socket = m_socket_watcher->fd();
|
2013-01-11 18:44:02 +01:00
|
|
|
RemoteUIMsg msg = read<RemoteUIMsg>(socket);
|
2012-10-23 22:55:44 +02:00
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case RemoteUIMsg::MenuShow:
|
|
|
|
{
|
2013-01-11 18:44:02 +01:00
|
|
|
auto choices = read_vector<String>(socket);
|
2014-05-07 20:51:01 +02:00
|
|
|
auto anchor = read<CharCoord>(socket);
|
2014-07-11 01:27:04 +02:00
|
|
|
auto fg = read<Face>(socket);
|
|
|
|
auto bg = read<Face>(socket);
|
2013-01-11 18:44:02 +01:00
|
|
|
auto style = read<MenuStyle>(socket);
|
2013-04-04 13:53:47 +02:00
|
|
|
m_ui->menu_show(choices, anchor, fg, bg, style);
|
2013-01-04 18:39:13 +01:00
|
|
|
break;
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
case RemoteUIMsg::MenuSelect:
|
2013-01-11 18:44:02 +01:00
|
|
|
m_ui->menu_select(read<int>(socket));
|
2013-01-04 18:39:13 +01:00
|
|
|
break;
|
2012-10-23 22:55:44 +02:00
|
|
|
case RemoteUIMsg::MenuHide:
|
2013-01-04 18:39:13 +01:00
|
|
|
m_ui->menu_hide();
|
|
|
|
break;
|
2012-12-14 19:04:34 +01:00
|
|
|
case RemoteUIMsg::InfoShow:
|
|
|
|
{
|
2013-10-10 23:51:16 +02:00
|
|
|
auto title = read<String>(socket);
|
|
|
|
auto content = read<String>(socket);
|
2014-05-07 20:51:01 +02:00
|
|
|
auto anchor = read<CharCoord>(socket);
|
2014-07-11 01:27:04 +02:00
|
|
|
auto face = read<Face>(socket);
|
2014-11-08 18:59:38 +01:00
|
|
|
auto style = read<InfoStyle>(socket);
|
2014-07-11 01:27:04 +02:00
|
|
|
m_ui->info_show(title, content, anchor, face, style);
|
2013-01-04 18:39:13 +01:00
|
|
|
break;
|
2012-12-14 19:04:34 +01:00
|
|
|
}
|
|
|
|
case RemoteUIMsg::InfoHide:
|
2013-01-04 18:39:13 +01:00
|
|
|
m_ui->info_hide();
|
|
|
|
break;
|
2012-10-23 22:55:44 +02:00
|
|
|
case RemoteUIMsg::Draw:
|
|
|
|
{
|
2013-04-04 18:50:00 +02:00
|
|
|
auto display_buffer = read<DisplayBuffer>(socket);
|
2013-09-16 20:15:13 +02:00
|
|
|
auto status_line = read<DisplayLine>(socket);
|
2013-04-04 18:50:00 +02:00
|
|
|
auto mode_line = read<DisplayLine>(socket);
|
2013-09-16 20:15:13 +02:00
|
|
|
m_ui->draw(display_buffer, status_line, mode_line);
|
2013-01-04 18:39:13 +01:00
|
|
|
break;
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
2014-04-15 20:19:44 +02:00
|
|
|
case RemoteUIMsg::Refresh:
|
|
|
|
m_ui->refresh();
|
|
|
|
break;
|
2014-11-11 00:29:16 +01:00
|
|
|
case RemoteUIMsg::SetOptions:
|
2015-01-14 20:16:32 +01:00
|
|
|
m_ui->set_ui_options(read_map<String, String, MemoryDomain::Options>(socket));
|
2014-11-11 00:29:16 +01:00
|
|
|
break;
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteClient::write_next_key()
|
|
|
|
{
|
2014-11-05 14:57:12 +01:00
|
|
|
Message msg(m_socket_watcher->fd());
|
2012-12-10 18:46:57 +01:00
|
|
|
// do that before checking dimensions as get_key may
|
|
|
|
// handle a resize event.
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(m_ui->get_key());
|
2012-12-03 18:51:28 +01:00
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
CharCoord dimensions = m_ui->dimensions();
|
2012-10-26 16:14:51 +02:00
|
|
|
if (dimensions != m_dimensions)
|
|
|
|
{
|
|
|
|
m_dimensions = dimensions;
|
2014-04-05 13:04:37 +02:00
|
|
|
Key key{ resize_modifier, Codepoint(((int)dimensions.line << 16) |
|
|
|
|
(int)dimensions.column) };
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(key);
|
2012-10-26 16:14:51 +02:00
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 20:18:38 +02:00
|
|
|
void send_command(StringView session, StringView command)
|
2014-03-02 03:01:09 +01:00
|
|
|
{
|
2014-11-06 14:54:18 +01:00
|
|
|
int sock = connect_to(session);
|
|
|
|
::write(sock, command.data(), (int)command.length());
|
2014-03-02 03:01:09 +01:00
|
|
|
close(sock);
|
|
|
|
}
|
|
|
|
|
2013-03-12 18:53:18 +01:00
|
|
|
|
2013-03-13 19:59:39 +01:00
|
|
|
// A client accepter handle a connection until it closes or a nul byte is
|
|
|
|
// recieved. Everything recieved before is considered to be a command.
|
|
|
|
//
|
|
|
|
// * When a nul byte is recieved, the socket is handed to a new Client along
|
|
|
|
// with the command.
|
|
|
|
// * When the connection is closed, the command is run in an empty context.
|
2014-03-25 10:21:20 +01:00
|
|
|
class Server::Accepter
|
2012-12-18 21:20:36 +01:00
|
|
|
{
|
2013-03-13 19:59:39 +01:00
|
|
|
public:
|
2014-03-25 10:21:20 +01:00
|
|
|
Accepter(int socket)
|
2014-04-05 13:04:37 +02:00
|
|
|
: m_socket_watcher(socket,
|
2014-11-25 02:00:18 +01:00
|
|
|
[this](FDWatcher&, EventMode mode) {
|
|
|
|
if (mode == EventMode::Normal)
|
|
|
|
handle_available_input();
|
|
|
|
})
|
2014-04-05 13:04:37 +02:00
|
|
|
{}
|
2013-03-13 19:59:39 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
void handle_available_input()
|
2013-03-12 18:53:18 +01:00
|
|
|
{
|
2013-03-13 19:59:39 +01:00
|
|
|
int socket = m_socket_watcher.fd();
|
|
|
|
timeval tv{ 0, 0 };
|
|
|
|
fd_set rfds;
|
|
|
|
do
|
2013-03-12 18:53:18 +01:00
|
|
|
{
|
2013-03-13 19:59:39 +01:00
|
|
|
char c;
|
|
|
|
int res = ::read(socket, &c, 1);
|
|
|
|
if (res <= 0)
|
2013-03-12 18:53:18 +01:00
|
|
|
{
|
2013-03-13 19:59:39 +01:00
|
|
|
if (not m_buffer.empty()) try
|
|
|
|
{
|
2015-04-19 19:47:52 +02:00
|
|
|
Context context{Context::EmptyContextFlag{}};
|
2013-03-13 19:59:39 +01:00
|
|
|
CommandManager::instance().execute(m_buffer, context);
|
|
|
|
}
|
|
|
|
catch (runtime_error& e)
|
|
|
|
{
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer(format("error running command '{}': {}",
|
2015-06-01 22:15:59 +02:00
|
|
|
m_buffer, e.what()));
|
2013-03-13 19:59:39 +01:00
|
|
|
}
|
2013-04-15 18:55:41 +02:00
|
|
|
catch (client_removed&) {}
|
2013-04-02 14:27:04 +02:00
|
|
|
close(socket);
|
2014-03-25 10:21:20 +01:00
|
|
|
Server::instance().remove_accepter(this);
|
2013-03-13 19:59:39 +01:00
|
|
|
return;
|
2013-03-12 18:53:18 +01:00
|
|
|
}
|
2014-04-05 13:04:37 +02:00
|
|
|
if (c == 0) // end of initial command stream, go to interactive ui
|
2013-03-12 18:53:18 +01:00
|
|
|
{
|
2015-01-14 20:16:32 +01:00
|
|
|
EnvVarMap env_vars = read_map<String, String, MemoryDomain::EnvVars>(socket);
|
2014-04-05 13:04:37 +02:00
|
|
|
std::unique_ptr<UserInterface> ui{new RemoteUI{socket}};
|
|
|
|
ClientManager::instance().create_client(std::move(ui),
|
2014-04-07 22:25:44 +02:00
|
|
|
std::move(env_vars),
|
2014-04-05 13:04:37 +02:00
|
|
|
m_buffer);
|
2014-03-25 10:21:20 +01:00
|
|
|
Server::instance().remove_accepter(this);
|
2013-03-13 19:59:39 +01:00
|
|
|
return;
|
2013-03-12 18:53:18 +01:00
|
|
|
}
|
2013-03-13 19:59:39 +01:00
|
|
|
else
|
|
|
|
m_buffer += c;
|
2012-12-18 21:20:36 +01:00
|
|
|
|
2013-03-13 19:59:39 +01:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(socket, &rfds);
|
|
|
|
}
|
2013-07-28 16:40:02 +02:00
|
|
|
while (select(socket+1, &rfds, nullptr, nullptr, &tv) == 1);
|
2013-03-12 18:53:18 +01:00
|
|
|
}
|
2013-03-13 19:59:39 +01:00
|
|
|
|
|
|
|
String m_buffer;
|
|
|
|
FDWatcher m_socket_watcher;
|
|
|
|
};
|
|
|
|
|
2013-09-25 20:04:52 +02:00
|
|
|
Server::Server(String session_name)
|
|
|
|
: m_session{std::move(session_name)}
|
2013-03-13 19:59:39 +01:00
|
|
|
{
|
|
|
|
int listen_sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
fcntl(listen_sock, F_SETFD, FD_CLOEXEC);
|
2014-11-06 14:54:18 +01:00
|
|
|
sockaddr_un addr = session_addr(m_session);
|
2013-03-13 19:59:39 +01:00
|
|
|
|
|
|
|
if (bind(listen_sock, (sockaddr*) &addr, sizeof(sockaddr_un)) == -1)
|
2015-06-01 20:06:35 +02:00
|
|
|
throw runtime_error(format("unable to bind listen socket '{}'", addr.sun_path));
|
2013-03-13 19:59:39 +01:00
|
|
|
|
|
|
|
if (listen(listen_sock, 4) == -1)
|
2015-06-01 20:06:35 +02:00
|
|
|
throw runtime_error(format("unable to listen on socket '{}'", addr.sun_path));
|
2013-03-13 19:59:39 +01:00
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
auto accepter = [this](FDWatcher& watcher, EventMode mode) {
|
2013-03-13 19:59:39 +01:00
|
|
|
sockaddr_un client_addr;
|
|
|
|
socklen_t client_addr_len = sizeof(sockaddr_un);
|
2014-04-05 13:04:37 +02:00
|
|
|
int sock = accept(watcher.fd(), (sockaddr*) &client_addr,
|
|
|
|
&client_addr_len);
|
2013-03-13 19:59:39 +01:00
|
|
|
if (sock == -1)
|
|
|
|
throw runtime_error("accept failed");
|
|
|
|
fcntl(sock, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
2014-03-25 10:21:20 +01:00
|
|
|
m_accepters.emplace_back(new Accepter{sock});
|
2013-03-13 19:59:39 +01:00
|
|
|
};
|
|
|
|
m_listener.reset(new FDWatcher{listen_sock, accepter});
|
|
|
|
}
|
|
|
|
|
2014-01-27 20:53:17 +01:00
|
|
|
void Server::close_session()
|
2013-03-13 19:59:39 +01:00
|
|
|
{
|
2015-06-01 22:15:59 +02:00
|
|
|
unlink(format("/tmp/kak-{}", m_session).c_str());
|
2014-12-03 14:56:02 +01:00
|
|
|
m_listener->close_fd();
|
2014-01-27 20:53:17 +01:00
|
|
|
m_listener.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
|
|
|
close_session();
|
2012-12-18 21:20:36 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 10:21:20 +01:00
|
|
|
void Server::remove_accepter(Accepter* accepter)
|
|
|
|
{
|
|
|
|
auto it = find(m_accepters, accepter);
|
|
|
|
kak_assert(it != m_accepters.end());
|
|
|
|
m_accepters.erase(it);
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|