Add click events to i3bar
If the statusline generator (i.e. i3status) specifies click_events:true in the protocol header, i3bar will write a JSON array on it's stdin notifying it if the user clicks on a block. The exact protocol is documented in docs/i3bar-protocol.
This commit is contained in:
committed by
Michael Stapelberg
parent
5adb09c5fc
commit
58e68940f6
@ -21,6 +21,7 @@
|
||||
#include <yajl/yajl_common.h>
|
||||
#include <yajl/yajl_parse.h>
|
||||
#include <yajl/yajl_version.h>
|
||||
#include <yajl/yajl_gen.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
@ -35,6 +36,9 @@ ev_child *child_sig;
|
||||
yajl_callbacks callbacks;
|
||||
yajl_handle parser;
|
||||
|
||||
/* JSON generator for stdout */
|
||||
yajl_gen gen;
|
||||
|
||||
typedef struct parser_ctx {
|
||||
/* True if one of the parsed blocks was urgent */
|
||||
bool has_urgent;
|
||||
@ -85,6 +89,8 @@ static int stdin_start_array(void *context) {
|
||||
first = TAILQ_FIRST(&statusline_head);
|
||||
I3STRING_FREE(first->full_text);
|
||||
FREE(first->color);
|
||||
FREE(first->name);
|
||||
FREE(first->instance);
|
||||
TAILQ_REMOVE(&statusline_head, first, blocks);
|
||||
free(first);
|
||||
}
|
||||
@ -152,6 +158,18 @@ static int stdin_string(void *context, const unsigned char *val, unsigned int le
|
||||
ctx->block.min_width = (uint32_t)predict_text_width(text);
|
||||
i3string_free(text);
|
||||
}
|
||||
if (strcasecmp(ctx->last_map_key, "name") == 0) {
|
||||
char *copy = (char*)malloc(len+1);
|
||||
strncpy(copy, (const char *)val, len);
|
||||
copy[len] = 0;
|
||||
ctx->block.name = copy;
|
||||
}
|
||||
if (strcasecmp(ctx->last_map_key, "instance") == 0) {
|
||||
char *copy = (char*)malloc(len+1);
|
||||
strncpy(copy, (const char *)val, len);
|
||||
copy[len] = 0;
|
||||
ctx->block.instance = copy;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -336,6 +354,18 @@ void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
void child_write_output(void) {
|
||||
if (child.click_events) {
|
||||
const unsigned char *output;
|
||||
size_t size;
|
||||
yajl_gen_get_buf(gen, &output, &size);
|
||||
fwrite(output, 1, size, stdout);
|
||||
fwrite("\n", 1, 1, stdout);
|
||||
fflush(stdout);
|
||||
yajl_gen_clear(gen);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Start a child-process with the specified command and reroute stdin.
|
||||
* We actually start a $SHELL to execute the command so we don't have to care
|
||||
@ -361,10 +391,16 @@ void start_child(char *command) {
|
||||
parser = yajl_alloc(&callbacks, NULL, &parser_context);
|
||||
#endif
|
||||
|
||||
gen = yajl_gen_alloc(NULL);
|
||||
|
||||
if (command != NULL) {
|
||||
int fd[2];
|
||||
if (pipe(fd) == -1)
|
||||
err(EXIT_FAILURE, "pipe(fd)");
|
||||
int pipe_in[2]; /* pipe we read from */
|
||||
int pipe_out[2]; /* pipe we write to */
|
||||
|
||||
if (pipe(pipe_in) == -1)
|
||||
err(EXIT_FAILURE, "pipe(pipe_in)");
|
||||
if (pipe(pipe_out) == -1)
|
||||
err(EXIT_FAILURE, "pipe(pipe_out)");
|
||||
|
||||
child.pid = fork();
|
||||
switch (child.pid) {
|
||||
@ -372,10 +408,13 @@ void start_child(char *command) {
|
||||
ELOG("Couldn't fork(): %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
case 0:
|
||||
/* Child-process. Reroute stdout and start shell */
|
||||
close(fd[0]);
|
||||
/* Child-process. Reroute streams and start shell */
|
||||
|
||||
dup2(fd[1], STDOUT_FILENO);
|
||||
close(pipe_in[0]);
|
||||
close(pipe_out[1]);
|
||||
|
||||
dup2(pipe_in[1], STDOUT_FILENO);
|
||||
dup2(pipe_out[0], STDIN_FILENO);
|
||||
|
||||
static const char *shell = NULL;
|
||||
|
||||
@ -385,10 +424,13 @@ void start_child(char *command) {
|
||||
execl(shell, shell, "-c", command, (char*) NULL);
|
||||
return;
|
||||
default:
|
||||
/* Parent-process. Rerout stdin */
|
||||
close(fd[1]);
|
||||
/* Parent-process. Reroute streams */
|
||||
|
||||
dup2(fd[0], STDIN_FILENO);
|
||||
close(pipe_in[1]);
|
||||
close(pipe_out[0]);
|
||||
|
||||
dup2(pipe_in[0], STDIN_FILENO);
|
||||
dup2(pipe_out[1], STDOUT_FILENO);
|
||||
|
||||
break;
|
||||
}
|
||||
@ -409,6 +451,52 @@ void start_child(char *command) {
|
||||
atexit(kill_child_at_exit);
|
||||
}
|
||||
|
||||
void child_click_events_initialize(void) {
|
||||
if (!child.click_events_init) {
|
||||
yajl_gen_array_open(gen);
|
||||
child_write_output();
|
||||
child.click_events_init = true;
|
||||
}
|
||||
}
|
||||
|
||||
void child_click_events_key(const char *key) {
|
||||
yajl_gen_string(gen, (const unsigned char *)key, strlen(key));
|
||||
}
|
||||
|
||||
/*
|
||||
* Generates a click event, if enabled.
|
||||
*
|
||||
*/
|
||||
void send_block_clicked(int button, const char *name, const char *instance, int x, int y) {
|
||||
if (child.click_events) {
|
||||
child_click_events_initialize();
|
||||
|
||||
yajl_gen_map_open(gen);
|
||||
|
||||
if (name) {
|
||||
child_click_events_key("name");
|
||||
yajl_gen_string(gen, (const unsigned char *)name, strlen(name));
|
||||
}
|
||||
|
||||
if (instance) {
|
||||
child_click_events_key("instance");
|
||||
yajl_gen_string(gen, (const unsigned char *)instance, strlen(instance));
|
||||
}
|
||||
|
||||
child_click_events_key("button");
|
||||
yajl_gen_integer(gen, button);
|
||||
|
||||
child_click_events_key("x");
|
||||
yajl_gen_integer(gen, x);
|
||||
|
||||
child_click_events_key("y");
|
||||
yajl_gen_integer(gen, y);
|
||||
|
||||
yajl_gen_map_close(gen);
|
||||
child_write_output();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* kill()s the child-process (if any). Called when exit()ing.
|
||||
*
|
||||
|
Reference in New Issue
Block a user