2012-10-23 22:55:44 +02:00
|
|
|
#include "remote.hh"
|
|
|
|
|
|
|
|
#include "display_buffer.hh"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class RemoteUIMsg
|
|
|
|
{
|
|
|
|
PrintStatus,
|
|
|
|
MenuShow,
|
|
|
|
MenuSelect,
|
|
|
|
MenuHide,
|
|
|
|
Draw
|
|
|
|
};
|
|
|
|
|
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) {}
|
2012-10-26 13:45:32 +02:00
|
|
|
~Message()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<char> m_stream;
|
|
|
|
int m_socket;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void write(Message& msg, const T& val)
|
|
|
|
{
|
|
|
|
msg.write((const char*)&val, sizeof(val));
|
|
|
|
};
|
|
|
|
|
|
|
|
void write(Message& msg, const String& str)
|
|
|
|
{
|
|
|
|
write(msg, str.length());
|
|
|
|
msg.write(str.c_str(), (int)str.length());
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void write(Message& msg, const memoryview<T>& view)
|
|
|
|
{
|
|
|
|
write<uint32_t>(msg, view.size());
|
|
|
|
for (auto& val : view)
|
|
|
|
write(msg, val);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void write(Message& msg, const std::vector<T>& vec)
|
|
|
|
{
|
|
|
|
write(msg, memoryview<T>(vec));
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(Message& msg, const DisplayAtom& atom)
|
|
|
|
{
|
|
|
|
write(msg, atom.fg_color);
|
|
|
|
write(msg, atom.bg_color);
|
|
|
|
write(msg, atom.attribute);
|
|
|
|
write(msg, atom.content.content());
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(Message& msg, const DisplayLine& line)
|
|
|
|
{
|
|
|
|
write(msg, line.atoms());
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(Message& msg, const DisplayBuffer& display_buffer)
|
|
|
|
{
|
|
|
|
write(msg, display_buffer.lines());
|
|
|
|
}
|
|
|
|
|
2012-10-26 13:45:32 +02:00
|
|
|
void read(int socket, char* buffer, size_t size)
|
|
|
|
{
|
|
|
|
int res = ::read(socket, buffer, size);
|
|
|
|
if (res == 0)
|
|
|
|
throw peer_disconnected{};
|
|
|
|
if (res == -1)
|
|
|
|
throw socket_error{};
|
|
|
|
assert(res == size);
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
template<typename T>
|
|
|
|
T read(int socket)
|
|
|
|
{
|
|
|
|
char value[sizeof(T)];
|
2012-10-26 13:45:32 +02:00
|
|
|
read(socket, value, sizeof(T));
|
2012-10-23 22:55:44 +02:00
|
|
|
return *(T*)(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
String read<String>(int socket)
|
|
|
|
{
|
|
|
|
ByteCount length = read<ByteCount>(socket);
|
|
|
|
char buffer[2048];
|
|
|
|
assert(length < 2048);
|
2012-10-26 13:45:32 +02:00
|
|
|
read(socket, buffer, (int)length);
|
2012-10-23 22:55:44 +02:00
|
|
|
return String(buffer, buffer+(int)length);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::vector<T> read_vector(int socket)
|
|
|
|
{
|
|
|
|
uint32_t size = read<uint32_t>(socket);
|
|
|
|
std::vector<T> res;
|
|
|
|
res.reserve(size);
|
|
|
|
while (size--)
|
|
|
|
res.push_back(read<T>(socket));
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
DisplayAtom read<DisplayAtom>(int socket)
|
|
|
|
{
|
|
|
|
Color fg_color = read<Color>(socket);
|
|
|
|
Color bg_color = read<Color>(socket);
|
|
|
|
Attribute attribute = read<Attribute>(socket);
|
|
|
|
DisplayAtom atom(AtomContent(read<String>(socket)));
|
|
|
|
atom.fg_color = fg_color;
|
|
|
|
atom.bg_color = bg_color;
|
|
|
|
atom.attribute = attribute;
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
template<>
|
|
|
|
DisplayLine read<DisplayLine>(int socket)
|
|
|
|
{
|
|
|
|
return DisplayLine(0, read_vector<DisplayAtom>(socket));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
DisplayBuffer read<DisplayBuffer>(int socket)
|
|
|
|
{
|
|
|
|
DisplayBuffer db;
|
|
|
|
db.lines() = read_vector<DisplayLine>(socket);
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::print_status(const String& status, CharCount cursor_pos)
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
|
|
|
write(msg, RemoteUIMsg::PrintStatus);
|
|
|
|
write(msg, status);
|
|
|
|
write(msg, cursor_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_show(const memoryview<String>& choices,
|
|
|
|
const DisplayCoord& anchor, MenuStyle style)
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
|
|
|
write(msg, RemoteUIMsg::MenuShow);
|
|
|
|
write(msg, choices);
|
|
|
|
write(msg, anchor);
|
|
|
|
write(msg, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_select(int selected)
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
|
|
|
write(msg, RemoteUIMsg::MenuSelect);
|
|
|
|
write(msg, selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_hide()
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
|
|
|
write(msg, RemoteUIMsg::MenuHide);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::draw(const DisplayBuffer& display_buffer,
|
|
|
|
const String& status_line)
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
|
|
|
write(msg, RemoteUIMsg::Draw);
|
|
|
|
write(msg, display_buffer);
|
|
|
|
write(msg, status_line);
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:51:01 +02:00
|
|
|
static const Key::Modifiers resize_modifier = (Key::Modifiers)0x80;
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
Key RemoteUI::get_key()
|
|
|
|
{
|
|
|
|
Key key = read<Key>(m_socket);
|
2012-10-25 12:51:01 +02:00
|
|
|
if (key.modifiers == resize_modifier)
|
|
|
|
{
|
|
|
|
m_dimensions = { (int)(key.key >> 16), (int)(key.key & 0xFFFF) };
|
|
|
|
return Key::Invalid;
|
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
DisplayCoord RemoteUI::dimensions()
|
|
|
|
{
|
|
|
|
return m_dimensions;
|
|
|
|
}
|
|
|
|
|
2012-10-25 12:51:01 +02:00
|
|
|
RemoteClient::RemoteClient(int socket, UserInterface* ui)
|
|
|
|
: m_socket(socket), m_ui(ui)
|
|
|
|
{
|
|
|
|
DisplayCoord size = ui->dimensions();
|
|
|
|
Key key{ resize_modifier, (Codepoint)(((int)size.line << 16) | (int)size.column) };
|
|
|
|
Message msg(socket);
|
|
|
|
write(msg, key);
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
void RemoteClient::process_next_message()
|
|
|
|
{
|
|
|
|
RemoteUIMsg msg = read<RemoteUIMsg>(m_socket);
|
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case RemoteUIMsg::PrintStatus:
|
|
|
|
{
|
|
|
|
auto status = read<String>(m_socket);
|
|
|
|
auto cursor_pos = read<CharCount>(m_socket);
|
|
|
|
m_ui->print_status(status, cursor_pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RemoteUIMsg::MenuShow:
|
|
|
|
{
|
|
|
|
auto choices = read_vector<String>(m_socket);
|
|
|
|
auto anchor = read<DisplayCoord>(m_socket);
|
|
|
|
auto style = read<MenuStyle>(m_socket);
|
|
|
|
m_ui->menu_show(choices, anchor, style);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RemoteUIMsg::MenuSelect:
|
|
|
|
m_ui->menu_select(read<int>(m_socket));
|
|
|
|
break;
|
|
|
|
case RemoteUIMsg::MenuHide:
|
|
|
|
m_ui->menu_hide();
|
|
|
|
break;
|
|
|
|
case RemoteUIMsg::Draw:
|
|
|
|
{
|
|
|
|
DisplayBuffer display_buffer = read<DisplayBuffer>(m_socket);
|
|
|
|
String status_line = read<String>(m_socket);
|
|
|
|
m_ui->draw(display_buffer, status_line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteClient::write_next_key()
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
|
|
|
write(msg, m_ui->get_key());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|