13 #include "workspaces.h"
17 #define STDIN_CHUNK_SIZE 1024
19 void ev_prepare_cb(struct ev_loop *loop, ev_prepare *w, int revents) {
20 xcb_flush(xcb_connection);
23 void ev_check_cb(struct ev_loop *loop, ev_check *w, int revents) {
24 xcb_generic_event_t *event;
25 if ((event = xcb_poll_for_event(xcb_connection)) != NULL) {
26 handle_xcb_event(event);
31 void xcb_io_cb(struct ev_loop *loop, ev_io *w, int revents) {
34 void start_child(char *command) {
40 printf("ERROR: Couldn't fork()");
45 dup2(fd[1], STDOUT_FILENO);
47 static const char *shell = NULL;
49 if ((shell = getenv("SHELL")) == NULL)
52 execl(shell, shell, "-c", command, (char*) NULL);
57 dup2(fd[0], STDIN_FILENO);
63 void strip_dzen_formats(char *buffer) {
66 while (*src != '\0') {
68 if (!strncmp(src, "^ro", strlen("^ro"))) {
86 void child_io_cb(struct ev_loop *loop, ev_io *w, int revents) {
90 int buffer_len = STDIN_CHUNK_SIZE;
91 char *buffer = malloc(buffer_len);
92 memset(buffer, '\0', buffer_len);
94 n = read(fd, buffer + rec, buffer_len - rec);
96 if (errno == EAGAIN) {
99 printf("ERROR: read() failed!");
103 if (rec == buffer_len) {
105 buffer = malloc(buffer_len + STDIN_CHUNK_SIZE);
106 memset(buffer, '\0', buffer_len);
107 strncpy(buffer, tmp, buffer_len);
108 buffer_len += STDIN_CHUNK_SIZE;
116 if (strlen(buffer) == 0) {
120 strip_dzen_formats(buffer);
123 printf("%s", buffer);
127 int main(int argc, char **argv) {
128 main_loop = ev_default_loop(0);
131 init_connection("/home/mero/.i3/ipc.sock");
135 ev_io *xcb_io = malloc(sizeof(ev_io));
136 ev_prepare *ev_prep = malloc(sizeof(ev_prepare));
137 ev_check *ev_chk = malloc(sizeof(ev_check));
139 ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ);
140 ev_prepare_init(ev_prep, &ev_prepare_cb);
141 ev_check_init(ev_chk, &ev_check_cb);
143 ev_io_start(main_loop, xcb_io);
144 ev_prepare_start(main_loop, ev_prep);
145 ev_check_start(main_loop, ev_chk);
147 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
148 i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
150 start_child("i3status");
152 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
153 ev_io *child_io = malloc(sizeof(ev_io));
154 ev_io_init(child_io, &child_io_cb, STDIN_FILENO, EV_READ);
155 ev_io_start(main_loop, child_io);
157 ev_loop(main_loop, 0);
159 ev_prepare_stop(main_loop, ev_prep);
160 ev_check_stop(main_loop, ev_chk);
164 ev_default_destroy();
168 FREE_SLIST(outputs, i3_output);