Skip to content

Commit

Permalink
Add get_config_json
Browse files Browse the repository at this point in the history
Adds a non-complete implementation of a new IPC message,
get_config_json, which returns the current configuration as JSON.
  • Loading branch information
j-jzk committed May 19, 2021
1 parent fcae64f commit c95b1e0
Show file tree
Hide file tree
Showing 7 changed files with 567 additions and 91 deletions.
2 changes: 2 additions & 0 deletions i3-msg/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ int main(int argc, char *argv[]) {
message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
} else if (strcasecmp(optarg, "get_config") == 0) {
message_type = I3_IPC_MESSAGE_TYPE_GET_CONFIG;
} else if (strcasecmp(optarg, "get_config_json") == 0) {
message_type = I3_IPC_MESSAGE_TYPE_GET_CONFIG_JSON;
} else if (strcasecmp(optarg, "send_tick") == 0) {
message_type = I3_IPC_MESSAGE_TYPE_SEND_TICK;
} else if (strcasecmp(optarg, "subscribe") == 0) {
Expand Down
10 changes: 10 additions & 0 deletions include/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,13 @@ bool load_keymap(void);
* The list is terminated by a 0.
*/
int *bindings_get_buttons_to_grab(void);

/**
* Dumps an i3_event_state_mask_t into JSON (as an array)
*/
void dump_event_state_mask(yajl_gen gen, i3_event_state_mask_t mask);

/**
* Dumps a binding into JSON
*/
void dump_binding(yajl_gen gen, Binding *bind);
8 changes: 8 additions & 0 deletions include/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ struct Config {

/* The number of currently parsed barconfigs */
int number_barconfigs;

/** A yajl_gen for generating the structured configuration */
yajl_gen json_gen;

/** The loaded configuration as JSON*/
unsigned char *json;
/** Length of the JSON */
size_t json_len;
};

/**
Expand Down
4 changes: 4 additions & 0 deletions include/i3/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ typedef struct i3_ipc_header {
/** Request the current binding state. */
#define I3_IPC_MESSAGE_TYPE_GET_BINDING_STATE 12

/** Request the loaded configuration as JSON */
#define I3_IPC_MESSAGE_TYPE_GET_CONFIG_JSON 13

/*
* Messages from i3 to clients
*
Expand All @@ -86,6 +89,7 @@ typedef struct i3_ipc_header {
#define I3_IPC_REPLY_TYPE_TICK 10
#define I3_IPC_REPLY_TYPE_SYNC 11
#define I3_IPC_REPLY_TYPE_GET_BINDING_STATE 12
#define I3_IPC_REPLY_TYPE_GET_CONFIG_JSON 13

/*
* Events from i3 to clients. Events have the first bit set high.
Expand Down
91 changes: 91 additions & 0 deletions src/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* bindings.c: Functions for configuring, finding and, running bindings.
*/
#include "all.h"
#include "yajl_utils.h"

#include <math.h>

Expand Down Expand Up @@ -1027,3 +1028,93 @@ int *bindings_get_buttons_to_grab(void) {

return buttons;
}

void dump_event_state_mask(yajl_gen gen, i3_event_state_mask_t mask) {
y(array_open);
for (int i = 0; i < 20; i++) {
if (mask & (1 << i)) {
switch (1 << i) {
case XCB_KEY_BUT_MASK_SHIFT:
ystr("shift");
break;
case XCB_KEY_BUT_MASK_LOCK:
ystr("lock");
break;
case XCB_KEY_BUT_MASK_CONTROL:
ystr("ctrl");
break;
case XCB_KEY_BUT_MASK_MOD_1:
ystr("Mod1");
break;
case XCB_KEY_BUT_MASK_MOD_2:
ystr("Mod2");
break;
case XCB_KEY_BUT_MASK_MOD_3:
ystr("Mod3");
break;
case XCB_KEY_BUT_MASK_MOD_4:
ystr("Mod4");
break;
case XCB_KEY_BUT_MASK_MOD_5:
ystr("Mod5");
break;
case XCB_KEY_BUT_MASK_BUTTON_1:
ystr("Button1");
break;
case XCB_KEY_BUT_MASK_BUTTON_2:
ystr("Button2");
break;
case XCB_KEY_BUT_MASK_BUTTON_3:
ystr("Button3");
break;
case XCB_KEY_BUT_MASK_BUTTON_4:
ystr("Button4");
break;
case XCB_KEY_BUT_MASK_BUTTON_5:
ystr("Button5");
break;
case (I3_XKB_GROUP_MASK_1 << 16):
ystr("Group1");
break;
case (I3_XKB_GROUP_MASK_2 << 16):
ystr("Group2");
break;
case (I3_XKB_GROUP_MASK_3 << 16):
ystr("Group3");
break;
case (I3_XKB_GROUP_MASK_4 << 16):
ystr("Group4");
break;
}
}
}
y(array_close);
}

void dump_binding(yajl_gen gen, Binding *bind) {
y(map_open);
ystr("input_code");
y(integer, bind->keycode);

ystr("input_type");
ystr((const char *)(bind->input_type == B_KEYBOARD ? "keyboard" : "mouse"));

ystr("symbol");
if (bind->symbol == NULL)
y(null);
else
ystr(bind->symbol);

ystr("command");
ystr(bind->command);

// This key is only provided for compatibility, new programs should use
// event_state_mask instead.
ystr("mods");
dump_event_state_mask(gen, bind->event_state_mask);

ystr("event_state_mask");
dump_event_state_mask(gen, bind->event_state_mask);

y(map_close);
}

0 comments on commit c95b1e0

Please sign in to comment.