refactor(bspwm): Use defined socket path for ipc connections

This commit is contained in:
Michael Carlberg 2016-10-11 09:23:26 +02:00
parent 4055778468
commit f05d5678d4

View File

@ -11,7 +11,7 @@ LEMONBUDDY_NS
namespace bspwm_util { namespace bspwm_util {
struct payload; struct payload;
using subscriber_t = unique_ptr<socket_util::unix_connection>; using connection_t = unique_ptr<socket_util::unix_connection>;
using payload_t = unique_ptr<payload>; using payload_t = unique_ptr<payload>;
/** /**
@ -30,23 +30,22 @@ namespace bspwm_util {
* 3. Value of the macro BSPWM_SOCKET_PATH * 3. Value of the macro BSPWM_SOCKET_PATH
*/ */
string get_socket_path() { string get_socket_path() {
string env_path{read_env("BSPWM_SOCKET")}; string env_path;
if (!env_path.empty())
if ((env_path = read_env("BSPWM_SOCKET")).empty() == false)
return env_path; return env_path;
struct sockaddr_un sa; struct sockaddr_un sa;
char* tpl_path = nullptr;
char* host = nullptr; char* host = nullptr;
int dsp = 0; int dsp = 0;
int scr = 0; int scr = 0;
if (xcb_parse_display(nullptr, &host, &dsp, &scr) != 0) if (xcb_parse_display(nullptr, &host, &dsp, &scr) == 0)
std::snprintf(tpl_path, sizeof(sa.sun_path), "/tmp/bspwm%s_%i_%i-socket", host, dsp, scr); return BSPWM_SOCKET_PATH;
if (tpl_path != nullptr) snprintf(sa.sun_path, sizeof(sa.sun_path), "/tmp/bspwm%s_%i_%i-socket", host, dsp, scr);
return tpl_path;
return BSPWM_SOCKET_PATH; return sa.sun_path;
} }
/** /**
@ -68,6 +67,19 @@ namespace bspwm_util {
return pl; return pl;
} }
/**
* Create an ipc socket connection
*
* Example usage:
* @code cpp
* auto ipc = bspwm_util::make_connection();
* ipc->send(bspwm_util::make_payload("desktop -f eDP-1:^1"));
* @endcode
*/
connection_t make_connection() {
return socket_util::make_unix_connection(get_socket_path());
}
/** /**
* Create a connection and subscribe to events * Create a connection and subscribe to events
* on the bspwm socket * on the bspwm socket
@ -83,8 +95,8 @@ namespace bspwm_util {
* } * }
* @endcode * @endcode
*/ */
subscriber_t make_subscriber() { connection_t make_subscriber() {
auto conn = socket_util::make_unix_connection(BSPWM_SOCKET_PATH); auto conn = make_connection();
auto payload = make_payload("subscribe report"); auto payload = make_payload("subscribe report");
if (conn->send(payload->data, payload->len, 0) == 0) if (conn->send(payload->data, payload->len, 0) == 0)
throw system_error("Failed to initialize subscriber"); throw system_error("Failed to initialize subscriber");