add libi3/ipc_connect, use it in i3-config-wizard, i3-input, i3bar

This commit is contained in:
Michael Stapelberg 2011-10-23 17:18:14 +01:00
parent 4539546f48
commit 4f57d49318
9 changed files with 46 additions and 99 deletions

View File

@ -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 <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <err.h>
/*
* 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;
}

View File

@ -1,6 +0,0 @@
#ifndef _IPC_H
#define _IPC_H
int connect_ipc(char *socket_path);
#endif

View File

@ -51,7 +51,6 @@
while (0) while (0)
#include "xcb.h" #include "xcb.h"
#include "ipc.h"
#include "libi3.h" #include "libi3.h"
enum { STEP_WELCOME, STEP_GENERATE } current_step = STEP_WELCOME; enum { STEP_WELCOME, STEP_GENERATE } current_step = STEP_WELCOME;
@ -371,7 +370,7 @@ static void finish() {
fclose(ks_config); fclose(ks_config);
/* tell i3 to reload the config file */ /* 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"); ipc_send_message(sockfd, strlen("reload"), 0, (uint8_t*)"reload");
close(sockfd); close(sockfd);

View File

@ -16,7 +16,6 @@ extern xcb_window_t root;
char *convert_ucs_to_utf8(char *input); char *convert_ucs_to_utf8(char *input);
char *convert_utf8_to_ucs2(char *input, int *real_strlen); 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_colorpixel(xcb_connection_t *conn, char *hex);
uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode); 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); xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height);

View File

@ -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 <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <err.h>
/*
* 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;
}

View File

@ -341,7 +341,7 @@ int main(int argc, char *argv[]) {
if (socket_path == NULL) if (socket_path == NULL)
socket_path = "/tmp/i3-ipc.sock"; socket_path = "/tmp/i3-ipc.sock";
sockfd = connect_ipc(socket_path); sockfd = ipc_connect(socket_path);
if (prompt != NULL) if (prompt != NULL)
prompt = convert_utf8_to_ucs2(prompt, &prompt_len); prompt = convert_utf8_to_ucs2(prompt, &prompt_len);

View File

@ -271,21 +271,7 @@ int i3_send_msg(uint32_t type, const char *payload) {
*/ */
int init_connection(const char *socket_path) { int init_connection(const char *socket_path) {
sock_path = socket_path; sock_path = socket_path;
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); int sockfd = ipc_connect(socket_path);
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);
}
i3_connection = smalloc(sizeof(ev_io)); i3_connection = smalloc(sizeof(ev_io));
ev_io_init(i3_connection, &got_data, sockfd, EV_READ); ev_io_init(i3_connection, &got_data, sockfd, EV_READ);
ev_io_start(main_loop, i3_connection); ev_io_start(main_loop, i3_connection);

View File

@ -53,6 +53,13 @@ char *sstrdup(const char *str);
*/ */
int sasprintf(char **strp, const char *fmt, ...); 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 * Formats a message (payload) of the given size and type and sends it to i3 via
* the given socket file descriptor. * the given socket file descriptor.

36
libi3/ipc_connect.c Normal file
View File

@ -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 <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <err.h>
#include <stdlib.h>
/*
* 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;
}