|
|
@ -1,6 +1,7 @@
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <json-c/json.h>
|
|
|
|
#include <json-c/json.h>
|
|
|
|
#include <linux/input-event-codes.h>
|
|
|
|
#include <linux/input-event-codes.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <unistd.h>
|
|
|
@ -24,24 +25,19 @@ void i3bar_block_unref(struct i3bar_block *block) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool i3bar_parse_json(struct status_line *status, const char *text) {
|
|
|
|
static void i3bar_parse_json(struct status_line *status,
|
|
|
|
|
|
|
|
struct json_object *json_array) {
|
|
|
|
struct i3bar_block *block, *tmp;
|
|
|
|
struct i3bar_block *block, *tmp;
|
|
|
|
wl_list_for_each_safe(block, tmp, &status->blocks, link) {
|
|
|
|
wl_list_for_each_safe(block, tmp, &status->blocks, link) {
|
|
|
|
wl_list_remove(&block->link);
|
|
|
|
wl_list_remove(&block->link);
|
|
|
|
i3bar_block_unref(block);
|
|
|
|
i3bar_block_unref(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
json_object *results = json_tokener_parse(text);
|
|
|
|
for (size_t i = 0; i < json_object_array_length(json_array); ++i) {
|
|
|
|
if (!results) {
|
|
|
|
|
|
|
|
status_error(status, "[failed to parse i3bar json]");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_log(WLR_DEBUG, "Got i3bar json: '%s'", text);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < json_object_array_length(results); ++i) {
|
|
|
|
|
|
|
|
json_object *full_text, *short_text, *color, *min_width, *align, *urgent;
|
|
|
|
json_object *full_text, *short_text, *color, *min_width, *align, *urgent;
|
|
|
|
json_object *name, *instance, *separator, *separator_block_width;
|
|
|
|
json_object *name, *instance, *separator, *separator_block_width;
|
|
|
|
json_object *background, *border, *border_top, *border_bottom;
|
|
|
|
json_object *background, *border, *border_top, *border_bottom;
|
|
|
|
json_object *border_left, *border_right, *markup;
|
|
|
|
json_object *border_left, *border_right, *markup;
|
|
|
|
json_object *json = json_object_array_get_idx(results, i);
|
|
|
|
json_object *json = json_object_array_get_idx(json_array, i);
|
|
|
|
if (!json) {
|
|
|
|
if (!json) {
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -110,96 +106,149 @@ static bool i3bar_parse_json(struct status_line *status, const char *text) {
|
|
|
|
json_object_get_int(border_right) : 1;
|
|
|
|
json_object_get_int(border_right) : 1;
|
|
|
|
wl_list_insert(&status->blocks, &block->link);
|
|
|
|
wl_list_insert(&status->blocks, &block->link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
json_object_put(results);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool i3bar_handle_readable(struct status_line *status) {
|
|
|
|
bool i3bar_handle_readable(struct status_line *status) {
|
|
|
|
struct i3bar_protocol_state *state = &status->i3bar_state;
|
|
|
|
while (!status->started) { // look for opening bracket
|
|
|
|
|
|
|
|
for (size_t c = 0; c < status->buffer_index; ++c) {
|
|
|
|
char *cur = &state->buffer[state->buffer_index];
|
|
|
|
if (status->buffer[c] == '[') {
|
|
|
|
ssize_t n = read(status->read_fd, cur,
|
|
|
|
status->started = true;
|
|
|
|
state->buffer_size - state->buffer_index);
|
|
|
|
status->buffer_index -= ++c;
|
|
|
|
if (n == -1) {
|
|
|
|
memmove(status->buffer, &status->buffer[c], status->buffer_index);
|
|
|
|
status_error(status, "[failed to read from status command]");
|
|
|
|
break;
|
|
|
|
return false;
|
|
|
|
} else if (!isspace(status->buffer[c])) {
|
|
|
|
}
|
|
|
|
status_error(status, "[invalid json]");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status->started) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (n == (ssize_t)(state->buffer_size - state->buffer_index)) {
|
|
|
|
errno = 0;
|
|
|
|
state->buffer_size = state->buffer_size * 2;
|
|
|
|
ssize_t read_bytes = read(status->read_fd, status->buffer, status->buffer_size);
|
|
|
|
char *new_buffer = realloc(state->buffer, state->buffer_size);
|
|
|
|
if (read_bytes > -1) {
|
|
|
|
if (!new_buffer) {
|
|
|
|
status->buffer_index = read_bytes;
|
|
|
|
free(state->buffer);
|
|
|
|
} else if (errno == EAGAIN) {
|
|
|
|
status_error(status, "[failed to allocate buffer]");
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
status_error(status, "[error reading from status command]");
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state->current_node += new_buffer - state->buffer;
|
|
|
|
|
|
|
|
cur += new_buffer - state->buffer;
|
|
|
|
|
|
|
|
state->buffer = new_buffer;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cur[n] = '\0';
|
|
|
|
struct json_object *last_object = NULL;
|
|
|
|
bool redraw = false;
|
|
|
|
struct json_object *test_object;
|
|
|
|
while (*cur) {
|
|
|
|
size_t buffer_pos = 0;
|
|
|
|
if (state->nodes[state->depth] == JSON_NODE_STRING) {
|
|
|
|
while (true) {
|
|
|
|
if (!state->escape && *cur == '"') {
|
|
|
|
// since the incoming stream is an infinite array
|
|
|
|
--state->depth;
|
|
|
|
// parsing is split into two parts
|
|
|
|
|
|
|
|
// first, attempt to parse the current object, reading more if the
|
|
|
|
|
|
|
|
// parser indicates that the current object is incomplete, and failing
|
|
|
|
|
|
|
|
// if the parser fails
|
|
|
|
|
|
|
|
// second, look for separating comma, ignoring whitespace, failing if
|
|
|
|
|
|
|
|
// any other characters are encountered
|
|
|
|
|
|
|
|
if (status->expecting_comma) {
|
|
|
|
|
|
|
|
for (; buffer_pos < status->buffer_index; ++buffer_pos) {
|
|
|
|
|
|
|
|
if (status->buffer[buffer_pos] == ',') {
|
|
|
|
|
|
|
|
status->expecting_comma = false;
|
|
|
|
|
|
|
|
++buffer_pos;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else if (!isspace(status->buffer[buffer_pos])) {
|
|
|
|
|
|
|
|
status_error(status, "[invalid i3bar json]");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_pos < status->buffer_index) {
|
|
|
|
|
|
|
|
continue; // look for new object without reading more input
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state->escape = !state->escape && *cur == '\\';
|
|
|
|
buffer_pos = status->buffer_index = 0;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
switch (*cur) {
|
|
|
|
test_object = json_tokener_parse_ex(status->tokener,
|
|
|
|
case '[':
|
|
|
|
&status->buffer[buffer_pos], status->buffer_index - buffer_pos);
|
|
|
|
++state->depth;
|
|
|
|
if (json_tokener_get_error(status->tokener) == json_tokener_success) {
|
|
|
|
if (state->depth >
|
|
|
|
if (json_object_get_type(test_object) == json_type_array) {
|
|
|
|
sizeof(state->nodes) / sizeof(state->nodes[0])) {
|
|
|
|
if (last_object) {
|
|
|
|
status_error(status, "[i3bar json too deep]");
|
|
|
|
json_object_put(last_object);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_object = test_object;
|
|
|
|
state->nodes[state->depth] = JSON_NODE_ARRAY;
|
|
|
|
} else {
|
|
|
|
if (state->depth == 1) {
|
|
|
|
json_object_put(test_object);
|
|
|
|
state->current_node = cur;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ']':
|
|
|
|
// in order to print the json for debugging purposes
|
|
|
|
if (state->nodes[state->depth] != JSON_NODE_ARRAY) {
|
|
|
|
// the last character is temporarily replaced with a null character
|
|
|
|
status_error(status, "[failed to parse i3bar json]");
|
|
|
|
// (the last character is used in case the buffer is full)
|
|
|
|
return false;
|
|
|
|
char *last_char_pos =
|
|
|
|
|
|
|
|
&status->buffer[buffer_pos + status->tokener->char_offset - 1];
|
|
|
|
|
|
|
|
char last_char = *last_char_pos;
|
|
|
|
|
|
|
|
while (isspace(last_char)) {
|
|
|
|
|
|
|
|
last_char = *--last_char_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--state->depth;
|
|
|
|
*last_char_pos = '\0';
|
|
|
|
if (state->depth == 0) {
|
|
|
|
size_t offset = strspn(&status->buffer[buffer_pos], " \f\n\r\t\v");
|
|
|
|
// cur[1] is valid since cur[0] != '\0'
|
|
|
|
wlr_log(WLR_DEBUG, "Received i3bar json: '%s%c'",
|
|
|
|
char p = cur[1];
|
|
|
|
&status->buffer[buffer_pos + offset], last_char);
|
|
|
|
cur[1] = '\0';
|
|
|
|
*last_char_pos = last_char;
|
|
|
|
redraw = i3bar_parse_json(
|
|
|
|
|
|
|
|
status, state->current_node) || redraw;
|
|
|
|
buffer_pos += status->tokener->char_offset;
|
|
|
|
cur[1] = p;
|
|
|
|
status->expecting_comma = true;
|
|
|
|
memmove(state->buffer, cur,
|
|
|
|
|
|
|
|
state->buffer_size - (cur - state->buffer));
|
|
|
|
if (buffer_pos < status->buffer_index) {
|
|
|
|
cur = state->buffer;
|
|
|
|
continue; // look for comma without reading more input
|
|
|
|
state->current_node = cur + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
buffer_pos = status->buffer_index = 0;
|
|
|
|
case '"':
|
|
|
|
} else if (json_tokener_get_error(status->tokener) == json_tokener_continue) {
|
|
|
|
++state->depth;
|
|
|
|
if (status->buffer_index < status->buffer_size) {
|
|
|
|
if (state->depth >
|
|
|
|
// move the object to the start of the buffer
|
|
|
|
sizeof(state->nodes) / sizeof(state->nodes[0])) {
|
|
|
|
status->buffer_index -= buffer_pos;
|
|
|
|
status_error(status, "[i3bar json too deep]");
|
|
|
|
memmove(status->buffer, &status->buffer[buffer_pos],
|
|
|
|
return false;
|
|
|
|
status->buffer_index);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// expand buffer
|
|
|
|
|
|
|
|
status->buffer_size *= 2;
|
|
|
|
|
|
|
|
char *new_buffer = realloc(status->buffer, status->buffer_size);
|
|
|
|
|
|
|
|
if (new_buffer) {
|
|
|
|
|
|
|
|
status->buffer = new_buffer;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
free(status->buffer);
|
|
|
|
|
|
|
|
status_error(status, "[failed to allocate buffer]");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state->nodes[state->depth] = JSON_NODE_STRING;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
status_error(status, "[failed to parse i3bar json]");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++cur;
|
|
|
|
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
ssize_t read_bytes = read(status->read_fd, &status->buffer[status->buffer_index],
|
|
|
|
|
|
|
|
status->buffer_size - status->buffer_index);
|
|
|
|
|
|
|
|
if (read_bytes > -1) {
|
|
|
|
|
|
|
|
status->buffer_index += read_bytes;
|
|
|
|
|
|
|
|
} else if (errno == EAGAIN) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
status_error(status, "[error reading from status command]");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (last_object) {
|
|
|
|
|
|
|
|
wlr_log(WLR_DEBUG, "Rendering last received json");
|
|
|
|
|
|
|
|
i3bar_parse_json(status, last_object);
|
|
|
|
|
|
|
|
json_object_put(last_object);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state->buffer_index = cur - state->buffer;
|
|
|
|
|
|
|
|
return redraw;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum hotspot_event_handling i3bar_block_send_click(struct status_line *status,
|
|
|
|
enum hotspot_event_handling i3bar_block_send_click(struct status_line *status,
|
|
|
|
struct i3bar_block *block, int x, int y, enum x11_button button) {
|
|
|
|
struct i3bar_block *block, int x, int y, enum x11_button button) {
|
|
|
|
wlr_log(WLR_DEBUG, "block %s clicked", block->name ? block->name : "(nil)");
|
|
|
|
wlr_log(WLR_DEBUG, "block %s clicked", block->name ? block->name : "(nil)");
|
|
|
|
if (!block->name || !status->i3bar_state.click_events) {
|
|
|
|
if (!block->name || !status->click_events) {
|
|
|
|
return HOTSPOT_PROCESS;
|
|
|
|
return HOTSPOT_PROCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -214,7 +263,7 @@ enum hotspot_event_handling i3bar_block_send_click(struct status_line *status,
|
|
|
|
json_object_object_add(event_json, "button", json_object_new_int(button));
|
|
|
|
json_object_object_add(event_json, "button", json_object_new_int(button));
|
|
|
|
json_object_object_add(event_json, "x", json_object_new_int(x));
|
|
|
|
json_object_object_add(event_json, "x", json_object_new_int(x));
|
|
|
|
json_object_object_add(event_json, "y", json_object_new_int(y));
|
|
|
|
json_object_object_add(event_json, "y", json_object_new_int(y));
|
|
|
|
if (dprintf(status->write_fd, "%s\n",
|
|
|
|
if (dprintf(status->write_fd, "%s,\n",
|
|
|
|
json_object_to_json_string(event_json)) < 0) {
|
|
|
|
json_object_to_json_string(event_json)) < 0) {
|
|
|
|
status_error(status, "[failed to write click event]");
|
|
|
|
status_error(status, "[failed to write click event]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|