niri: Replace gnu extension with GDataInputStream

This commit is contained in:
Ivan Molodetskikh 2024-09-13 10:33:08 +03:00
parent 46e7ed35de
commit fef0bb995c
1 changed files with 32 additions and 13 deletions

View File

@ -8,8 +8,11 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include "giomm/datainputstream.h"
#include "giomm/dataoutputstream.h"
#include "giomm/unixinputstream.h"
#include "giomm/unixoutputstream.h"
#include <ext/stdio_filebuf.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <thread> #include <thread>
@ -63,18 +66,23 @@ void IPC::startIPC() {
spdlog::info("Niri IPC starting"); spdlog::info("Niri IPC starting");
__gnu_cxx::stdio_filebuf<char> filebuf(socketfd, std::ios::in | std::ios::out); auto unix_istream = Gio::UnixInputStream::create(socketfd, true);
std::iostream fs(&filebuf); auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false);
fs << R"("EventStream")" << std::endl; auto istream = Gio::DataInputStream::create(unix_istream);
auto ostream = Gio::DataOutputStream::create(unix_ostream);
std::string line; if (!ostream->put_string("\"EventStream\"\n") || !ostream->flush()) {
std::getline(fs, line);
if (line != R"({"Ok":"Handled"})") {
spdlog::error("Niri IPC: failed to start event stream"); spdlog::error("Niri IPC: failed to start event stream");
return; return;
} }
while (std::getline(fs, line)) { std::string line;
if (!istream->read_line(line) || line != R"({"Ok":"Handled"})") {
spdlog::error("Niri IPC: failed to start event stream");
return;
}
while (istream->read_line(line)) {
spdlog::debug("Niri IPC: received {}", line); spdlog::debug("Niri IPC: received {}", line);
try { try {
@ -231,18 +239,29 @@ Json::Value IPC::send(const Json::Value& request) {
if (socketfd == -1) if (socketfd == -1)
throw std::runtime_error("Niri is not running"); throw std::runtime_error("Niri is not running");
__gnu_cxx::stdio_filebuf<char> filebuf(socketfd, std::ios::in | std::ios::out); auto unix_istream = Gio::UnixInputStream::create(socketfd, true);
std::iostream fs(&filebuf); auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false);
auto istream = Gio::DataInputStream::create(unix_istream);
auto ostream = Gio::DataOutputStream::create(unix_ostream);
// Niri needs the request on a single line. // Niri needs the request on a single line.
Json::StreamWriterBuilder builder; Json::StreamWriterBuilder builder;
builder["indentation"] = ""; builder["indentation"] = "";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(request, &fs); std::ostringstream oss;
fs << std::endl; writer->write(request, &oss);
oss << '\n';
if (!ostream->put_string(oss.str()) || !ostream->flush())
throw std::runtime_error("error writing to niri socket");
std::string line;
if (!istream->read_line(line))
throw std::runtime_error("error reading from niri socket");
std::istringstream iss(std::move(line));
Json::Value response; Json::Value response;
fs >> response; iss >> response;
return response; return response;
} }