mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-22 20:25:39 +02:00
module/i3: break out get_socket_address()
This commit is contained in:
parent
6e44d8e04e
commit
37d15096f9
4 changed files with 102 additions and 81 deletions
|
@ -37,8 +37,10 @@ find_file(I3_IPC_H i3/ipc.h)
|
||||||
if (NOT I3_IPC_H)
|
if (NOT I3_IPC_H)
|
||||||
message(FATAL_ERROR "cannot find header file: i3/ipc.h")
|
message(FATAL_ERROR "cannot find header file: i3/ipc.h")
|
||||||
endif ()
|
endif ()
|
||||||
|
add_library(i3-common STATIC EXCLUDE_FROM_ALL i3-common.c i3-common.h)
|
||||||
|
|
||||||
add_library(i3 ${lib_type} i3.c)
|
add_library(i3 ${lib_type} i3.c)
|
||||||
target_link_libraries(i3 module-sdk dynlist PkgConfig::json)
|
target_link_libraries(i3 module-sdk i3-common dynlist PkgConfig::json)
|
||||||
if (ENABLE_X11)
|
if (ENABLE_X11)
|
||||||
target_link_libraries(i3 xcb-stuff)
|
target_link_libraries(i3 xcb-stuff)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
88
modules/i3-common.c
Normal file
88
modules/i3-common.c
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
#include "i3-common.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#if defined(ENABLE_X11)
|
||||||
|
#include <xcb/xcb.h>
|
||||||
|
#include <xcb/xcb_aux.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define LOG_MODULE "i3:common"
|
||||||
|
#include "../log.h"
|
||||||
|
|
||||||
|
#if defined(ENABLE_X11)
|
||||||
|
#include "../xcb.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ENABLE_X11)
|
||||||
|
static bool
|
||||||
|
get_socket_address_x11(struct sockaddr_un *addr)
|
||||||
|
{
|
||||||
|
int default_screen;
|
||||||
|
xcb_connection_t *conn = xcb_connect(NULL, &default_screen);
|
||||||
|
if (xcb_connection_has_error(conn) > 0) {
|
||||||
|
LOG_ERR("failed to connect to X");
|
||||||
|
xcb_disconnect(conn);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
xcb_screen_t *screen = xcb_aux_get_screen(conn, default_screen);
|
||||||
|
|
||||||
|
xcb_atom_t atom = get_atom(conn, "I3_SOCKET_PATH");
|
||||||
|
assert(atom != XCB_ATOM_NONE);
|
||||||
|
|
||||||
|
xcb_get_property_cookie_t cookie
|
||||||
|
= xcb_get_property_unchecked(
|
||||||
|
conn, false, screen->root, atom,
|
||||||
|
XCB_GET_PROPERTY_TYPE_ANY, 0, sizeof(addr->sun_path));
|
||||||
|
|
||||||
|
xcb_generic_error_t *err;
|
||||||
|
xcb_get_property_reply_t *reply =
|
||||||
|
xcb_get_property_reply(conn, cookie, &err);
|
||||||
|
|
||||||
|
if (err != NULL) {
|
||||||
|
LOG_ERR("failed to get i3 socket path: %s", xcb_error(err));
|
||||||
|
free(err);
|
||||||
|
free(reply);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int len = xcb_get_property_value_length(reply);
|
||||||
|
assert(len < sizeof(addr->sun_path));
|
||||||
|
|
||||||
|
if (len == 0) {
|
||||||
|
LOG_ERR("failed to get i3 socket path: empty reply");
|
||||||
|
free(reply);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(addr->sun_path, xcb_get_property_value(reply), len);
|
||||||
|
addr->sun_path[len] = '\0';
|
||||||
|
|
||||||
|
free(reply);
|
||||||
|
xcb_disconnect(conn);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool
|
||||||
|
i3_get_socket_address(struct sockaddr_un *addr)
|
||||||
|
{
|
||||||
|
*addr = (struct sockaddr_un){.sun_family = AF_UNIX};
|
||||||
|
|
||||||
|
const char *sway_sock = getenv("SWAYSOCK");
|
||||||
|
if (sway_sock == NULL) {
|
||||||
|
sway_sock = getenv("I3SOCK");
|
||||||
|
if (sway_sock == NULL) {
|
||||||
|
#if defined(ENABLE_X11)
|
||||||
|
return get_socket_address_x11(addr);
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(addr->sun_path, sway_sock, sizeof(addr->sun_path) - 1);
|
||||||
|
return true;
|
||||||
|
}
|
9
modules/i3-common.h
Normal file
9
modules/i3-common.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
|
||||||
|
bool i3_get_socket_address(struct sockaddr_un *addr);
|
82
modules/i3.c
82
modules/i3.c
|
@ -13,11 +13,6 @@
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
#if defined(ENABLE_X11)
|
|
||||||
#include <xcb/xcb.h>
|
|
||||||
#include <xcb/xcb_aux.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <i3/ipc.h>
|
#include <i3/ipc.h>
|
||||||
|
|
||||||
#include <json-c/json_tokener.h>
|
#include <json-c/json_tokener.h>
|
||||||
|
@ -33,9 +28,7 @@
|
||||||
#include "../particles/dynlist.h"
|
#include "../particles/dynlist.h"
|
||||||
#include "../plugin.h"
|
#include "../plugin.h"
|
||||||
|
|
||||||
#if defined(ENABLE_X11)
|
#include "i3-common.h"
|
||||||
#include "../xcb.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct ws_content {
|
struct ws_content {
|
||||||
char *name;
|
char *name;
|
||||||
|
@ -479,77 +472,6 @@ handle_window_event(struct private *m, const struct json_object *json)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(ENABLE_X11)
|
|
||||||
static bool
|
|
||||||
get_socket_address_x11(struct sockaddr_un *addr)
|
|
||||||
{
|
|
||||||
int default_screen;
|
|
||||||
xcb_connection_t *conn = xcb_connect(NULL, &default_screen);
|
|
||||||
if (xcb_connection_has_error(conn) > 0) {
|
|
||||||
LOG_ERR("failed to connect to X");
|
|
||||||
xcb_disconnect(conn);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
xcb_screen_t *screen = xcb_aux_get_screen(conn, default_screen);
|
|
||||||
|
|
||||||
xcb_atom_t atom = get_atom(conn, "I3_SOCKET_PATH");
|
|
||||||
assert(atom != XCB_ATOM_NONE);
|
|
||||||
|
|
||||||
xcb_get_property_cookie_t cookie
|
|
||||||
= xcb_get_property_unchecked(
|
|
||||||
conn, false, screen->root, atom,
|
|
||||||
XCB_GET_PROPERTY_TYPE_ANY, 0, sizeof(addr->sun_path));
|
|
||||||
|
|
||||||
xcb_generic_error_t *err;
|
|
||||||
xcb_get_property_reply_t *reply =
|
|
||||||
xcb_get_property_reply(conn, cookie, &err);
|
|
||||||
|
|
||||||
if (err != NULL) {
|
|
||||||
LOG_ERR("failed to get i3 socket path: %s", xcb_error(err));
|
|
||||||
free(err);
|
|
||||||
free(reply);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int len = xcb_get_property_value_length(reply);
|
|
||||||
assert(len < sizeof(addr->sun_path));
|
|
||||||
|
|
||||||
if (len == 0) {
|
|
||||||
LOG_ERR("failed to get i3 socket path: empty reply");
|
|
||||||
free(reply);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(addr->sun_path, xcb_get_property_value(reply), len);
|
|
||||||
addr->sun_path[len] = '\0';
|
|
||||||
|
|
||||||
free(reply);
|
|
||||||
xcb_disconnect(conn);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static bool
|
|
||||||
get_socket_address(struct sockaddr_un *addr)
|
|
||||||
{
|
|
||||||
*addr = (struct sockaddr_un){.sun_family = AF_UNIX};
|
|
||||||
|
|
||||||
const char *sway_sock = getenv("SWAYSOCK");
|
|
||||||
if (sway_sock == NULL) {
|
|
||||||
sway_sock = getenv("I3SOCK");
|
|
||||||
if (sway_sock == NULL) {
|
|
||||||
#if defined(ENABLE_X11)
|
|
||||||
return get_socket_address_x11(addr);
|
|
||||||
#else
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(addr->sun_path, sway_sock, sizeof(addr->sun_path) - 1);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
run(struct module *mod)
|
run(struct module *mod)
|
||||||
|
@ -557,7 +479,7 @@ run(struct module *mod)
|
||||||
struct private *m = mod->private;
|
struct private *m = mod->private;
|
||||||
|
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr;
|
||||||
if (!get_socket_address(&addr))
|
if (!i3_get_socket_address(&addr))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue