From 4f57d49318d6e50ac03c427026b2653804a08527 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 23 Oct 2011 17:18:14 +0100 Subject: [PATCH] add libi3/ipc_connect, use it in i3-config-wizard, i3-input, i3bar --- i3-config-wizard/ipc.c | 37 ------------------------------------- i3-config-wizard/ipc.h | 6 ------ i3-config-wizard/main.c | 3 +-- i3-input/i3-input.h | 1 - i3-input/ipc.c | 37 ------------------------------------- i3-input/main.c | 2 +- i3bar/src/ipc.c | 16 +--------------- include/libi3.h | 7 +++++++ libi3/ipc_connect.c | 36 ++++++++++++++++++++++++++++++++++++ 9 files changed, 46 insertions(+), 99 deletions(-) delete mode 100644 i3-config-wizard/ipc.c delete mode 100644 i3-config-wizard/ipc.h delete mode 100644 i3-input/ipc.c create mode 100644 libi3/ipc_connect.c diff --git a/i3-config-wizard/ipc.c b/i3-config-wizard/ipc.c deleted file mode 100644 index 1d99e90a..00000000 --- a/i3-config-wizard/ipc.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * vim:ts=8:expandtab - * - * i3 - an improved dynamic tiling window manager - * - * © 2009 Michael Stapelberg and contributors - * - * See file LICENSE for license information. - * - */ -#include -#include -#include -#include -#include -#include -#include - -/* - * Connects to the i3 IPC socket and returns the file descriptor for the - * socket. die()s if anything goes wrong. - * - */ -int connect_ipc(char *socket_path) { - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - if (sockfd == -1) - err(EXIT_FAILURE, "Could not create socket"); - - struct sockaddr_un addr; - memset(&addr, 0, sizeof(struct sockaddr_un)); - addr.sun_family = AF_LOCAL; - strcpy(addr.sun_path, socket_path); - if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) - err(EXIT_FAILURE, "Could not connect to i3"); - - return sockfd; -} diff --git a/i3-config-wizard/ipc.h b/i3-config-wizard/ipc.h deleted file mode 100644 index a6e3e0dd..00000000 --- a/i3-config-wizard/ipc.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _IPC_H -#define _IPC_H - -int connect_ipc(char *socket_path); - -#endif diff --git a/i3-config-wizard/main.c b/i3-config-wizard/main.c index 56ebf3b2..d3f9bcbf 100644 --- a/i3-config-wizard/main.c +++ b/i3-config-wizard/main.c @@ -51,7 +51,6 @@ while (0) #include "xcb.h" -#include "ipc.h" #include "libi3.h" enum { STEP_WELCOME, STEP_GENERATE } current_step = STEP_WELCOME; @@ -371,7 +370,7 @@ static void finish() { fclose(ks_config); /* tell i3 to reload the config file */ - int sockfd = connect_ipc(socket_path); + int sockfd = ipc_connect(socket_path); ipc_send_message(sockfd, strlen("reload"), 0, (uint8_t*)"reload"); close(sockfd); diff --git a/i3-input/i3-input.h b/i3-input/i3-input.h index d0861ced..934ada03 100644 --- a/i3-input/i3-input.h +++ b/i3-input/i3-input.h @@ -16,7 +16,6 @@ extern xcb_window_t root; char *convert_ucs_to_utf8(char *input); char *convert_utf8_to_ucs2(char *input, int *real_strlen); -int connect_ipc(char *socket_path); uint32_t get_colorpixel(xcb_connection_t *conn, char *hex); uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode); xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height); diff --git a/i3-input/ipc.c b/i3-input/ipc.c deleted file mode 100644 index 8851e811..00000000 --- a/i3-input/ipc.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * vim:ts=8:expandtab - * - * i3 - an improved dynamic tiling window manager - * - * © 2009 Michael Stapelberg and contributors - * - * See file LICENSE for license information. - * - */ -#include -#include -#include -#include -#include -#include -#include - -/* - * Connects to the i3 IPC socket and returns the file descriptor for the - * socket. die()s if anything goes wrong. - * - */ -int connect_ipc(char *socket_path) { - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - if (sockfd == -1) - err(EXIT_FAILURE, "Could not create socket"); - - struct sockaddr_un addr; - memset(&addr, 0, sizeof(struct sockaddr_un)); - addr.sun_family = AF_LOCAL; - strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); - if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) - err(EXIT_FAILURE, "Could not connect to i3"); - - return sockfd; -} diff --git a/i3-input/main.c b/i3-input/main.c index 0d9d964e..ca6ae7a0 100644 --- a/i3-input/main.c +++ b/i3-input/main.c @@ -341,7 +341,7 @@ int main(int argc, char *argv[]) { if (socket_path == NULL) socket_path = "/tmp/i3-ipc.sock"; - sockfd = connect_ipc(socket_path); + sockfd = ipc_connect(socket_path); if (prompt != NULL) prompt = convert_utf8_to_ucs2(prompt, &prompt_len); diff --git a/i3bar/src/ipc.c b/i3bar/src/ipc.c index cfbc404e..009c276f 100644 --- a/i3bar/src/ipc.c +++ b/i3bar/src/ipc.c @@ -271,21 +271,7 @@ int i3_send_msg(uint32_t type, const char *payload) { */ int init_connection(const char *socket_path) { sock_path = socket_path; - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - if (sockfd == -1) { - ELOG("Could not create Socket: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - struct sockaddr_un addr; - memset(&addr, 0, sizeof(struct sockaddr_un)); - addr.sun_family = AF_LOCAL; - strcpy(addr.sun_path, sock_path); - if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) { - ELOG("Could not connect to i3! %s: %s\n", sock_path, strerror(errno)); - exit(EXIT_FAILURE); - } - + int sockfd = ipc_connect(socket_path); i3_connection = smalloc(sizeof(ev_io)); ev_io_init(i3_connection, &got_data, sockfd, EV_READ); ev_io_start(main_loop, i3_connection); diff --git a/include/libi3.h b/include/libi3.h index 9613b7f3..9e9c4012 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -53,6 +53,13 @@ char *sstrdup(const char *str); */ int sasprintf(char **strp, const char *fmt, ...); +/** + * Connects to the i3 IPC socket and returns the file descriptor for the + * socket. die()s if anything goes wrong. + * + */ +int ipc_connect(const char *socket_path); + /** * Formats a message (payload) of the given size and type and sends it to i3 via * the given socket file descriptor. diff --git a/libi3/ipc_connect.c b/libi3/ipc_connect.c new file mode 100644 index 00000000..2c41fcc2 --- /dev/null +++ b/libi3/ipc_connect.c @@ -0,0 +1,36 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * + * © 2009-2011 Michael Stapelberg and contributors + * + * See file LICENSE for license information. + * + */ +#include +#include +#include +#include +#include +#include + +/* + * Connects to the i3 IPC socket and returns the file descriptor for the + * socket. die()s if anything goes wrong. + * + */ +int ipc_connect(const char *socket_path) { + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + if (sockfd == -1) + err(EXIT_FAILURE, "Could not create socket"); + + struct sockaddr_un addr; + memset(&addr, 0, sizeof(struct sockaddr_un)); + addr.sun_family = AF_LOCAL; + strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); + if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) + err(EXIT_FAILURE, "Could not connect to i3"); + + return sockfd; +}