feat(workspace): catch ipc errors

This commit is contained in:
Alexis 2018-08-11 02:09:39 +02:00
parent 424ebb3c9b
commit 14053d61fc
3 changed files with 33 additions and 20 deletions

View File

@ -6,7 +6,7 @@ project(
) )
cpp_args = [] cpp_args = []
cpp_link_args = ['-g'] cpp_link_args = []
if false # libc++ if false # libc++
cpp_args += ['-stdlib=libc++'] cpp_args += ['-stdlib=libc++']

View File

@ -95,7 +95,6 @@ std::string ipc_single_command(int socketfd, uint32_t type, const char *payload,
} }
struct ipc_response resp = ipc_recv_response(socketfd); struct ipc_response resp = ipc_recv_response(socketfd);
std::string response = resp.payload;
*len = resp.size; *len = resp.size;
return response; return resp.payload;
} }

View File

@ -5,12 +5,17 @@ waybar::modules::Workspaces::Workspaces(Bar &bar)
: _bar(bar) : _bar(bar)
{ {
_box.get_style_context()->add_class("workspaces"); _box.get_style_context()->add_class("workspaces");
std::string socketPath = get_socketpath(); try {
_ipcSocketfd = ipc_open_socket(socketPath); std::string socketPath = get_socketpath();
_ipcEventSocketfd = ipc_open_socket(socketPath); _ipcSocketfd = ipc_open_socket(socketPath);
const char *subscribe = "[ \"workspace\", \"mode\" ]"; _ipcEventSocketfd = ipc_open_socket(socketPath);
uint32_t len = strlen(subscribe); const char *subscribe = "[ \"workspace\", \"mode\" ]";
ipc_single_command(_ipcEventSocketfd, IPC_SUBSCRIBE, subscribe, &len); uint32_t len = strlen(subscribe);
ipc_single_command(_ipcEventSocketfd, IPC_SUBSCRIBE, subscribe, &len);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return;
}
_thread = [this] { _thread = [this] {
Glib::signal_idle().connect_once([this] { Glib::signal_idle().connect_once([this] {
update(); update();
@ -60,9 +65,13 @@ void waybar::modules::Workspaces::_addWorkspace(Json::Value node)
_box.pack_start(button, false, false, 0); _box.pack_start(button, false, false, 0);
button.set_relief(Gtk::RELIEF_NONE); button.set_relief(Gtk::RELIEF_NONE);
button.signal_clicked().connect([this, pair] { button.signal_clicked().connect([this, pair] {
auto value = fmt::format("workspace \"{}\"", pair.first->first); try {
uint32_t size = value.size(); auto value = fmt::format("workspace \"{}\"", pair.first->first);
ipc_single_command(_ipcSocketfd, IPC_COMMAND, value.c_str(), &size); uint32_t size = value.size();
ipc_single_command(_ipcSocketfd, IPC_COMMAND, value.c_str(), &size);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}); });
_box.reorder_child(button, node["num"].asInt() - 1); _box.reorder_child(button, node["num"].asInt() - 1);
if (node["focused"].asBool()) { if (node["focused"].asBool()) {
@ -77,14 +86,19 @@ Json::Value waybar::modules::Workspaces::_getWorkspaces()
Json::Value root; Json::Value root;
Json::CharReaderBuilder builder; Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader(); Json::CharReader* reader = builder.newCharReader();
std::string err; try {
std::string str = ipc_single_command(_ipcSocketfd, IPC_GET_WORKSPACES, std::string str = ipc_single_command(_ipcSocketfd, IPC_GET_WORKSPACES,
nullptr, &len); nullptr, &len);
bool res = reader->parse(str.c_str(), str.c_str() + str.size(), &root, &err); std::string err;
delete reader; bool res =
if (!res) { reader->parse(str.c_str(), str.c_str() + str.size(), &root, &err);
std::cerr << err << std::endl; delete reader;
return nullptr; if (!res) {
std::cerr << err << std::endl;
return nullptr;
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
} }
return root; return root;
} }