Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Introduce GET_BINDING_STATE IPC command
Browse files Browse the repository at this point in the history
fixes #3892
  • Loading branch information
stapelberg committed Jun 6, 2020
1 parent 50160eb commit 45feaac
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 2 deletions.
4 changes: 3 additions & 1 deletion AnyEvent-I3/lib/AnyEvent/I3.pm
Expand Up @@ -101,11 +101,13 @@ use constant TYPE_GET_BINDING_MODES => 8;
use constant TYPE_GET_CONFIG => 9;
use constant TYPE_SEND_TICK => 10;
use constant TYPE_SYNC => 11;
use constant TYPE_GET_BINDING_STATE => 12;

our %EXPORT_TAGS = ( 'all' => [
qw(i3 TYPE_RUN_COMMAND TYPE_COMMAND TYPE_GET_WORKSPACES TYPE_SUBSCRIBE TYPE_GET_OUTPUTS
TYPE_GET_TREE TYPE_GET_MARKS TYPE_GET_BAR_CONFIG TYPE_GET_VERSION
TYPE_GET_BINDING_MODES TYPE_GET_CONFIG TYPE_SEND_TICK TYPE_SYNC)
TYPE_GET_BINDING_MODES TYPE_GET_CONFIG TYPE_SEND_TICK TYPE_SYNC
TYPE_GET_BINDING_STATE)
] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
Expand Down
14 changes: 14 additions & 0 deletions docs/ipc
Expand Up @@ -66,6 +66,7 @@ to do that).
| 9 | +GET_CONFIG+ | <<_config_reply,CONFIG>> | Returns the last loaded i3 config.
| 10 | +SEND_TICK+ | <<_tick_reply,TICK>> | Sends a tick event with the specified payload.
| 11 | +SYNC+ | <<_sync_reply,SYNC>> | Sends an i3 sync event with the specified random value to the specified window.
| 12 | +GET_BINDING_STATE+ | <<_binding_state_reply,BINDING_STATE>> | Request the current binding state, e.g. the currently active binding mode name.
|======================================================

So, a typical message could look like this:
Expand Down Expand Up @@ -131,6 +132,8 @@ GET_CONFIG (9)::
Reply to the GET_CONFIG message.
TICK (10)::
Reply to the SEND_TICK message.
GET_BINDING_STATE (12)::
Reply to the GET_BINDING_STATE message.

[[_command_reply]]
=== COMMAND reply
Expand Down Expand Up @@ -709,6 +712,17 @@ responded to.
{ "success": true }
-------------------

[[_binding_state_reply]]
=== GET_BINDING_STATE reply

The binding_state reply is a map which currently only contains the "name"
member, which is the name of the currently active binding mode as a string.

*Example:*
-------------------
{ "name": "default" }
-------------------

== Events

[[events]]
Expand Down
1 change: 1 addition & 0 deletions include/i3.h
Expand Up @@ -58,6 +58,7 @@ extern char **start_argv;
extern Display *xlibdpy, *xkbdpy;
extern int xkb_current_group;
extern TAILQ_HEAD(bindings_head, Binding) * bindings;
extern const char *current_binding_mode;
extern TAILQ_HEAD(autostarts_head, Autostart) autostarts;
extern TAILQ_HEAD(autostarts_always_head, Autostart) autostarts_always;
extern TAILQ_HEAD(ws_assignments_head, Workspace_Assignment) ws_assignments;
Expand Down
4 changes: 4 additions & 0 deletions include/i3/ipc.h
Expand Up @@ -66,6 +66,9 @@ typedef struct i3_ipc_header {
/** Trigger an i3 sync protocol message via IPC. */
#define I3_IPC_MESSAGE_TYPE_SYNC 11

/** Request the current binding state. */
#define I3_IPC_MESSAGE_TYPE_GET_BINDING_STATE 12

/*
* Messages from i3 to clients
*
Expand All @@ -82,6 +85,7 @@ typedef struct i3_ipc_header {
#define I3_IPC_REPLY_TYPE_CONFIG 9
#define I3_IPC_REPLY_TYPE_TICK 10
#define I3_IPC_REPLY_TYPE_SYNC 11
#define I3_IPC_REPLY_TYPE_GET_BINDING_STATE 12

/*
* Events from i3 to clients. Events have the first bit set high.
Expand Down
1 change: 1 addition & 0 deletions src/bindings.c
Expand Up @@ -628,6 +628,7 @@ void switch_mode(const char *new_mode) {

ungrab_all_keys(conn);
bindings = mode->bindings;
current_binding_mode = mode->name;
translate_keysyms();
grab_all_keys(conn);

Expand Down
1 change: 1 addition & 0 deletions src/config.c
Expand Up @@ -174,6 +174,7 @@ bool load_configuration(const char *override_configpath, config_load_t load_type
SLIST_INSERT_HEAD(&modes, default_mode, modes);

bindings = default_mode->bindings;
current_binding_mode = default_mode->name;

/* Clear the old config or initialize the data structure */
memset(&config, 0, sizeof(config));
Expand Down
21 changes: 20 additions & 1 deletion src/ipc.c
Expand Up @@ -1337,9 +1337,27 @@ IPC_HANDLER(sync) {
ipc_send_client_message(client, strlen(reply), I3_IPC_REPLY_TYPE_SYNC, (const uint8_t *)reply);
}

IPC_HANDLER(get_binding_state) {
yajl_gen gen = ygenalloc();

y(map_open);

ystr("name");
ystr(current_binding_mode);

y(map_close);

const unsigned char *payload;
ylength length;
y(get_buf, &payload, &length);

ipc_send_client_message(client, length, I3_IPC_REPLY_TYPE_GET_BINDING_STATE, payload);
y(free);
}

/* The index of each callback function corresponds to the numeric
* value of the message type (see include/i3/ipc.h) */
handler_t handlers[12] = {
handler_t handlers[13] = {
handle_run_command,
handle_get_workspaces,
handle_subscribe,
Expand All @@ -1352,6 +1370,7 @@ handler_t handlers[12] = {
handle_get_config,
handle_send_tick,
handle_sync,
handle_get_binding_state,
};

/*
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Expand Up @@ -79,6 +79,7 @@ const int default_shmlog_size = 25 * 1024 * 1024;

/* The list of key bindings */
struct bindings_head *bindings;
const char *current_binding_mode = NULL;

/* The list of exec-lines */
struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
Expand Down
41 changes: 41 additions & 0 deletions testcases/t/311-get-binding-modes.t
@@ -0,0 +1,41 @@
#!perl
# vim:ts=4:sw=4:expandtab
#
# Please read the following documents before working on tests:
# • https://build.i3wm.org/docs/testsuite.html
# (or docs/testsuite)
#
# • https://build.i3wm.org/docs/lib-i3test.html
# (alternatively: perldoc ./testcases/lib/i3test.pm)
#
# • https://build.i3wm.org/docs/ipc.html
# (or docs/ipc)
#
# • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
# (unless you are already familiar with Perl)
#
# Verifies the GET_BINDING_MODE IPC command
# Ticket: #3892
# Bug still in: 4.18-318-g50160eb1
use i3test i3_config => <<EOT;
# i3 config file (v4)
font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
mode "extra" {
bindsym Mod1+x nop foo
}
EOT

my $i3 = i3(get_socket_path());
$i3->connect->recv;
# TODO: use the symbolic name for the command/reply type instead of the
# numerical 12:
my $binding_state = $i3->message(12, "")->recv;
is($binding_state->{name}, 'default', 'at startup, binding mode is default');

cmd 'mode extra';

$binding_state = $i3->message(12, "")->recv;
is($binding_state->{name}, 'extra', 'after switching, binding mode is extra');

done_testing;

0 comments on commit 45feaac

Please sign in to comment.