2012-10-23 22:55:44 +02:00
|
|
|
#include "remote.hh"
|
|
|
|
|
|
|
|
#include "display_buffer.hh"
|
2012-11-19 19:06:32 +01:00
|
|
|
#include "debug.hh"
|
2012-12-18 21:20:36 +01:00
|
|
|
#include "client_manager.hh"
|
|
|
|
#include "event_manager.hh"
|
2012-10-23 22:55:44 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class RemoteUIMsg
|
|
|
|
{
|
|
|
|
PrintStatus,
|
|
|
|
MenuShow,
|
|
|
|
MenuSelect,
|
|
|
|
MenuHide,
|
2012-12-14 19:04:34 +01:00
|
|
|
InfoShow,
|
|
|
|
InfoHide,
|
2012-10-23 22:55:44 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
template<typename T>
|
|
|
|
void write(const T& val)
|
|
|
|
{
|
|
|
|
write((const char*)&val, sizeof(val));
|
|
|
|
};
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
void write(const String& str)
|
|
|
|
{
|
|
|
|
write(str.length());
|
|
|
|
write(str.c_str(), (int)str.length());
|
|
|
|
};
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
template<typename T>
|
|
|
|
void write(const memoryview<T>& view)
|
|
|
|
{
|
|
|
|
write<uint32_t>(view.size());
|
|
|
|
for (auto& val : view)
|
|
|
|
write(val);
|
|
|
|
};
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
template<typename T>
|
|
|
|
void write(const std::vector<T>& vec)
|
|
|
|
{
|
|
|
|
write(memoryview<T>(vec));
|
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
void write(const DisplayAtom& atom)
|
|
|
|
{
|
|
|
|
write(atom.fg_color);
|
|
|
|
write(atom.bg_color);
|
|
|
|
write(atom.attribute);
|
|
|
|
write(atom.content.content());
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
write(display_buffer.lines());
|
|
|
|
}
|
2012-10-23 22:55:44 +02:00
|
|
|
|
2012-12-10 18:55:11 +01:00
|
|
|
private:
|
|
|
|
std::vector<char> m_stream;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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);
|
2012-10-30 14:00:44 +01:00
|
|
|
if (length == 0)
|
|
|
|
return String{};
|
2012-10-23 22:55:44 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-18 21:20:36 +01:00
|
|
|
class RemoteUI : public UserInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RemoteUI(int socket);
|
|
|
|
~RemoteUI();
|
|
|
|
|
|
|
|
void print_status(const String& status, CharCount cursor_pos) override;
|
|
|
|
|
|
|
|
void menu_show(const memoryview<String>& choices,
|
|
|
|
const DisplayCoord& anchor, MenuStyle style) override;
|
|
|
|
void menu_select(int selected) override;
|
|
|
|
void menu_hide() override;
|
|
|
|
|
|
|
|
void info_show(const String& content,
|
|
|
|
const DisplayCoord& anchor, MenuStyle style) override;
|
|
|
|
void info_hide() override;
|
|
|
|
|
|
|
|
void draw(const DisplayBuffer& display_buffer,
|
|
|
|
const String& mode_line) override;
|
|
|
|
|
|
|
|
bool is_key_available() override;
|
|
|
|
Key get_key() override;
|
|
|
|
DisplayCoord dimensions() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_socket;
|
|
|
|
DisplayCoord m_dimensions;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-11-19 19:06:32 +01:00
|
|
|
RemoteUI::RemoteUI(int socket)
|
|
|
|
: m_socket(socket)
|
|
|
|
{
|
|
|
|
write_debug("remote client connected: " + int_to_str(m_socket));
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteUI::~RemoteUI()
|
|
|
|
{
|
|
|
|
write_debug("remote client disconnected: " + int_to_str(m_socket));
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
void RemoteUI::print_status(const String& status, CharCount cursor_pos)
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(RemoteUIMsg::PrintStatus);
|
|
|
|
msg.write(status);
|
|
|
|
msg.write(cursor_pos);
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_show(const memoryview<String>& choices,
|
|
|
|
const DisplayCoord& anchor, MenuStyle style)
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(RemoteUIMsg::MenuShow);
|
|
|
|
msg.write(choices);
|
|
|
|
msg.write(anchor);
|
|
|
|
msg.write(style);
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::menu_select(int selected)
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
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()
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(RemoteUIMsg::MenuHide);
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
2012-12-14 19:04:34 +01:00
|
|
|
void RemoteUI::info_show(const String& content,
|
|
|
|
const DisplayCoord& anchor, MenuStyle style)
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
|
|
|
msg.write(RemoteUIMsg::InfoShow);
|
|
|
|
msg.write(content);
|
|
|
|
msg.write(anchor);
|
|
|
|
msg.write(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteUI::info_hide()
|
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
|
|
|
msg.write(RemoteUIMsg::InfoHide);
|
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
void RemoteUI::draw(const DisplayBuffer& display_buffer,
|
2012-10-29 19:01:57 +01:00
|
|
|
const String& mode_line)
|
2012-10-23 22:55:44 +02:00
|
|
|
{
|
|
|
|
Message msg(m_socket);
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(RemoteUIMsg::Draw);
|
|
|
|
msg.write(display_buffer);
|
|
|
|
msg.write(mode_line);
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(m_socket, &rfds);
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
int res = select(m_socket+1, &rfds, NULL, NULL, &tv);
|
|
|
|
return res == 1;
|
|
|
|
}
|
|
|
|
|
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-12-18 21:20:36 +01:00
|
|
|
RemoteClient::RemoteClient(int socket, UserInterface* ui,
|
|
|
|
const String& init_command)
|
2012-10-26 16:14:51 +02:00
|
|
|
: m_socket(socket), m_ui(ui), m_dimensions(ui->dimensions())
|
2012-10-25 12:51:01 +02:00
|
|
|
{
|
2012-12-10 18:46:57 +01:00
|
|
|
Message msg(socket);
|
2012-12-18 21:20:36 +01:00
|
|
|
msg.write(init_command);
|
|
|
|
Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) | (int)m_dimensions.column) };
|
2012-12-10 18:55:11 +01:00
|
|
|
msg.write(key);
|
2012-10-25 12:51:01 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
void RemoteClient::process_next_message()
|
|
|
|
{
|
|
|
|
RemoteUIMsg msg = read<RemoteUIMsg>(m_socket);
|
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case RemoteUIMsg::PrintStatus:
|
|
|
|
{
|
2013-01-04 18:39:13 +01:00
|
|
|
auto status = read<String>(m_socket);
|
|
|
|
auto cursor_pos = read<CharCount>(m_socket);
|
|
|
|
m_ui->print_status(status, cursor_pos);
|
|
|
|
break;
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
case RemoteUIMsg::MenuShow:
|
|
|
|
{
|
2013-01-04 18:39:13 +01:00
|
|
|
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;
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
case RemoteUIMsg::MenuSelect:
|
2013-01-04 18:39:13 +01:00
|
|
|
m_ui->menu_select(read<int>(m_socket));
|
|
|
|
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-01-04 18:39:13 +01:00
|
|
|
auto choices = read<String>(m_socket);
|
|
|
|
auto anchor = read<DisplayCoord>(m_socket);
|
|
|
|
auto style = read<MenuStyle>(m_socket);
|
|
|
|
m_ui->info_show(choices, anchor, style);
|
|
|
|
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-01-04 18:39:13 +01:00
|
|
|
DisplayBuffer display_buffer = read<DisplayBuffer>(m_socket);
|
|
|
|
String mode_line = read<String>(m_socket);
|
|
|
|
m_ui->draw(display_buffer, mode_line);
|
|
|
|
break;
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteClient::write_next_key()
|
|
|
|
{
|
2012-12-10 18:46:57 +01:00
|
|
|
Message msg(m_socket);
|
|
|
|
// 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
|
|
|
|
2012-10-26 16:14:51 +02:00
|
|
|
DisplayCoord dimensions = m_ui->dimensions();
|
|
|
|
if (dimensions != m_dimensions)
|
|
|
|
{
|
|
|
|
m_dimensions = dimensions;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
void handle_remote(FDWatcher& watcher)
|
2012-12-18 21:20:36 +01:00
|
|
|
{
|
2013-01-10 18:54:40 +01:00
|
|
|
int socket = watcher.fd();
|
2012-12-18 21:20:36 +01:00
|
|
|
String init_command = read<String>(socket);
|
|
|
|
|
|
|
|
RemoteUI* ui = new RemoteUI{socket};
|
2013-01-10 18:54:40 +01:00
|
|
|
delete &watcher;
|
2012-12-18 21:20:36 +01:00
|
|
|
ClientManager::instance().create_client(
|
2012-12-28 13:51:14 +01:00
|
|
|
std::unique_ptr<UserInterface>{ui}, socket, init_command);
|
2012-12-18 21:20:36 +01:00
|
|
|
}
|
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
}
|