i3bar: don’t reconnect, but exit(0) on EOF
Since i3 starts i3bar instances as necessary, EOF is considered a signal to either shutdown because i3 exited or because it is restarting.
This commit is contained in:
parent
230b238870
commit
c5caa9682c
|
@ -24,55 +24,11 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
ev_io *i3_connection;
|
ev_io *i3_connection;
|
||||||
ev_timer *reconn = NULL;
|
|
||||||
|
|
||||||
const char *sock_path;
|
const char *sock_path;
|
||||||
|
|
||||||
typedef void(*handler_t)(char*);
|
typedef void(*handler_t)(char*);
|
||||||
|
|
||||||
/*
|
|
||||||
* Retry to connect.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void retry_connection(struct ev_loop *loop, ev_timer *w, int events) {
|
|
||||||
static int retries = 8;
|
|
||||||
if (init_connection(sock_path) == 0) {
|
|
||||||
if (retries == 0) {
|
|
||||||
ELOG("Retried 8 times - connection failed!\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
retries--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
retries = 8;
|
|
||||||
ev_timer_stop(loop, w);
|
|
||||||
subscribe_events();
|
|
||||||
|
|
||||||
/* We get the current outputs and workspaces, to
|
|
||||||
* reconfigure all bars with the current configuration */
|
|
||||||
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
|
|
||||||
if (!config.disable_ws) {
|
|
||||||
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Schedule a reconnect
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void reconnect() {
|
|
||||||
if (reconn == NULL) {
|
|
||||||
if ((reconn = malloc(sizeof(ev_timer))) == NULL) {
|
|
||||||
ELOG("malloc() failed: %s\n", strerror(errno));
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ev_timer_stop(main_loop, reconn);
|
|
||||||
}
|
|
||||||
ev_timer_init(reconn, retry_connection, 0.25, 0.25);
|
|
||||||
ev_timer_start(main_loop, reconn);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Called, when we get a reply to a command from i3.
|
* Called, when we get a reply to a command from i3.
|
||||||
* Since i3 does not give us much feedback on commands, we do not much
|
* Since i3 does not give us much feedback on commands, we do not much
|
||||||
|
@ -210,12 +166,10 @@ void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
/* EOF received. We try to recover a few times, because most likely
|
/* EOF received. Since i3 will restart i3bar instances as appropriate,
|
||||||
* i3 just restarted */
|
* we exit here. */
|
||||||
ELOG("EOF received, try to recover...\n");
|
DLOG("EOF received, exiting...\n");
|
||||||
destroy_connection();
|
exit(EXIT_SUCCESS);
|
||||||
reconnect();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
rec += n;
|
rec += n;
|
||||||
}
|
}
|
||||||
|
@ -239,12 +193,10 @@ void got_data(struct ev_loop *loop, ev_io *watcher, int events) {
|
||||||
* of the message */
|
* of the message */
|
||||||
char *buffer = malloc(size + 1);
|
char *buffer = malloc(size + 1);
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
/* EOF received. We try to recover a few times, because most likely
|
/* EOF received. Since i3 will restart i3bar instances as appropriate,
|
||||||
* i3 just restarted */
|
* we exit here. */
|
||||||
ELOG("EOF received, try to recover...\n");
|
DLOG("EOF received, exiting...\n");
|
||||||
destroy_connection();
|
exit(EXIT_SUCCESS);
|
||||||
reconnect();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
rec = 0;
|
rec = 0;
|
||||||
|
|
||||||
|
@ -346,8 +298,7 @@ int init_connection(const char *socket_path) {
|
||||||
strcpy(addr.sun_path, sock_path);
|
strcpy(addr.sun_path, sock_path);
|
||||||
if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
|
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));
|
ELOG("Could not connect to i3! %s: %s\n", sock_path, strerror(errno));
|
||||||
reconnect();
|
exit(EXIT_FAILURE);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i3_connection = malloc(sizeof(ev_io));
|
i3_connection = malloc(sizeof(ev_io));
|
||||||
|
|
Loading…
Reference in New Issue