i3
bindings.c File Reference
#include "all.h"
#include <math.h>
#include <xkbcommon/xkbcommon-x11.h>
#include <xkbcommon/xkbcommon.h>
Include dependency graph for bindings.c:

Go to the source code of this file.

Data Structures

struct  resolve
 

Macros

#define GRAB_KEY(modifier)
 
#define ADD_TRANSLATED_KEY(code, mods)
 

Functions

static struct Modemode_from_name (const char *name, bool pango_markup)
 
Bindingconfigure_binding (const char *bindtype, const char *modifiers, const char *input_code, const char *release, const char *border, const char *whole_window, const char *exclude_titlebar, const char *command, const char *modename, bool pango_markup)
 Adds a binding from config parameters given as strings and returns a pointer to the binding structure.
 
static bool binding_in_current_group (const Binding *bind)
 
static void grab_keycode_for_binding (xcb_connection_t *conn, Binding *bind, uint32_t keycode)
 
void grab_all_keys (xcb_connection_t *conn)
 Grab the bound keys (tell X to send us keypress events for those keycodes)
 
void regrab_all_buttons (xcb_connection_t *conn)
 Release the button grabs on all managed windows and regrab them, reevaluating which buttons need to be grabbed.
 
static Bindingget_binding (i3_event_state_mask_t state_filtered, bool is_release, uint16_t input_code, input_type_t input_type)
 
Bindingget_binding_from_xcb_event (xcb_generic_event_t *event)
 Returns a pointer to the Binding that matches the given xcb event or NULL if no such binding exists.
 
static void add_keycode_if_matches (struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
 
void translate_keysyms (void)
 Translates keysymbols to keycodes for all bindings which use keysyms.
 
void switch_mode (const char *new_mode)
 Switches the key bindings to the given mode, if the mode exists.
 
static int reorder_binding_cmp (const void *a, const void *b)
 
static void reorder_bindings_of_mode (struct Mode *mode)
 
void reorder_bindings (void)
 Reorders bindings by event_state_mask descendingly so that get_binding() correctly matches more specific bindings before more generic bindings.
 
static bool binding_same_key (Binding *a, Binding *b)
 
void check_for_duplicate_bindings (struct context *context)
 Checks for duplicate key bindings (the same keycode or keysym is configured more than once).
 
static Bindingbinding_copy (Binding *bind)
 
void binding_free (Binding *bind)
 Frees the binding.
 
CommandResultrun_binding (Binding *bind, Con *con)
 Runs the given binding and handles parse errors.
 
static int fill_rmlvo_from_root (struct xkb_rule_names *xkb_names)
 
bool load_keymap (void)
 Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
 
int * bindings_get_buttons_to_grab (void)
 Returns a list of buttons that should be grabbed on a window.
 

Variables

static struct xkb_context * xkb_context
 
static struct xkb_keymap * xkb_keymap
 
pid_t command_error_nagbar_pid = -1
 
const char * DEFAULT_BINDING_MODE = "default"
 The name of the default mode.
 

Macro Definition Documentation

◆ ADD_TRANSLATED_KEY

#define ADD_TRANSLATED_KEY ( code,
mods )
Value:
do { \
struct Binding_Keycode *binding_keycode = smalloc(sizeof(struct Binding_Keycode)); \
binding_keycode->modifiers = (mods); \
binding_keycode->keycode = (code); \
TAILQ_INSERT_TAIL(&(bind->keycodes_head), binding_keycode, keycodes); \
} while (0)
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
Stores a resolved keycode (from a keysym), including the modifier mask.
Definition data.h:290
i3_event_state_mask_t modifiers
Definition data.h:292
xcb_keycode_t keycode
Definition data.h:291

Definition at line 378 of file bindings.c.

Referenced by add_keycode_if_matches(), and translate_keysyms().

◆ GRAB_KEY

#define GRAB_KEY ( modifier)
Value:
do { \
xcb_grab_key(conn, 0, root, modifier, keycode, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC); \
} while (0)
xcb_connection_t * conn
XCB connection and root screen.
Definition main.c:54
xcb_window_t root
Definition main.c:67

Referenced by grab_keycode_for_binding().

Function Documentation

◆ add_keycode_if_matches()

static void add_keycode_if_matches ( struct xkb_keymap * keymap,
xkb_keycode_t key,
void * data )
static

◆ binding_copy()

static Binding * binding_copy ( Binding * bind)
static

Definition at line 813 of file bindings.c.

References Binding::command, smalloc(), sstrdup(), Binding::symbol, TAILQ_FOREACH, TAILQ_INIT, and TAILQ_INSERT_TAIL.

Referenced by run_binding().

Here is the call graph for this function:

◆ binding_free()

void binding_free ( Binding * bind)

Frees the binding.

If bind is null, it simply returns.

Definition at line 836 of file bindings.c.

References Binding::command, FREE, Binding::symbol, TAILQ_EMPTY, TAILQ_FIRST, and TAILQ_REMOVE.

Referenced by free_configuration(), and run_binding().

◆ binding_in_current_group()

static bool binding_in_current_group ( const Binding * bind)
static

◆ binding_same_key()

static bool binding_same_key ( Binding * a,
Binding * b )
static

◆ bindings_get_buttons_to_grab()

int * bindings_get_buttons_to_grab ( void )

Returns a list of buttons that should be grabbed on a window.

This list will always contain 1–3, all higher buttons will only be returned if there is a whole-window binding for it on some window in the current config. The list is terminated by a 0.

Definition at line 1036 of file bindings.c.

References B_MOUSE, bindings, ELOG, Binding::input_type, parse_long(), scalloc(), Binding::symbol, TAILQ_FOREACH, and Binding::whole_window.

Referenced by manage_window(), and regrab_all_buttons().

Here is the call graph for this function:

◆ check_for_duplicate_bindings()

void check_for_duplicate_bindings ( struct context * context)

Checks for duplicate key bindings (the same keycode or keysym is configured more than once).

If a duplicate binding is found, a message is printed to stderr and the has_errors variable is set to true, which will start i3-nagbar.

Definition at line 784 of file bindings.c.

References binding_same_key(), bindings, Binding::command, ELOG, Binding::event_state_mask, context::has_errors, Binding::keycode, Binding::symbol, and TAILQ_FOREACH.

Referenced by parse_file().

Here is the call graph for this function:

◆ configure_binding()

Binding * configure_binding ( const char * bindtype,
const char * modifiers,
const char * input_code,
const char * release,
const char * border,
const char * whole_window,
const char * exclude_titlebar,
const char * command,
const char * mode,
bool pango_markup )

Adds a binding from config parameters given as strings and returns a pointer to the binding structure.

Returns NULL if the input code could not be parsed.

Definition at line 59 of file bindings.c.

References B_KEYBOARD, B_MOUSE, bindings, Mode::bindings, Binding::border, Binding::command, DLOG, ELOG, event_state_from_str(), Binding::event_state_mask, Binding::exclude_titlebar, FREE, I3_XKB_GROUP_MASK_1, I3_XKB_GROUP_MASK_2, I3_XKB_GROUP_MASK_3, I3_XKB_GROUP_MASK_4, Binding::input_type, Binding::keycode, mode_from_name(), Mode::pango_markup, parse_long(), Binding::release, scalloc(), sstrdup(), Binding::symbol, TAILQ_INIT, TAILQ_INSERT_TAIL, and Binding::whole_window.

Referenced by CFGFUN(), and CFGFUN().

Here is the call graph for this function:

◆ fill_rmlvo_from_root()

static int fill_rmlvo_from_root ( struct xkb_rule_names * xkb_names)
static

Definition at line 909 of file bindings.c.

References conn, DLOG, root, and sasprintf().

Referenced by load_keymap().

Here is the call graph for this function:

◆ get_binding()

static Binding * get_binding ( i3_event_state_mask_t state_filtered,
bool is_release,
uint16_t input_code,
input_type_t input_type )
static

◆ get_binding_from_xcb_event()

Binding * get_binding_from_xcb_event ( xcb_generic_event_t * event)

Returns a pointer to the Binding that matches the given xcb event or NULL if no such binding exists.

Definition at line 315 of file bindings.c.

References B_KEYBOARD, B_MOUSE, DLOG, get_binding(), I3_XKB_GROUP_MASK_1, I3_XKB_GROUP_MASK_2, I3_XKB_GROUP_MASK_3, and I3_XKB_GROUP_MASK_4.

Referenced by handle_button_press(), handle_key_press(), and route_click().

Here is the call graph for this function:

◆ grab_all_keys()

void grab_all_keys ( xcb_connection_t * conn)

Grab the bound keys (tell X to send us keypress events for those keycodes)

Definition at line 155 of file bindings.c.

References B_KEYBOARD, binding_in_current_group(), bindings, conn, DLOG, grab_keycode_for_binding(), Binding::input_type, Binding::keycode, Binding_Keycode::keycode, Binding_Keycode::modifiers, root, and TAILQ_FOREACH.

Referenced by handle_event(), handle_mapping_notify(), load_configuration(), main(), and switch_mode().

Here is the call graph for this function:

◆ grab_keycode_for_binding()

static void grab_keycode_for_binding ( xcb_connection_t * conn,
Binding * bind,
uint32_t keycode )
static

Definition at line 133 of file bindings.c.

References DLOG, Binding::event_state_mask, GRAB_KEY, and xcb_numlock_mask.

Referenced by grab_all_keys().

◆ load_keymap()

bool load_keymap ( void )

Loads the XKB keymap from the X11 server and feeds it to xkbcommon.

Definition at line 983 of file bindings.c.

References conn, ELOG, fill_rmlvo_from_root(), LOG, xkb_context, xkb_keymap, and xkb_supported.

Referenced by handle_event(), and main().

Here is the call graph for this function:

◆ mode_from_name()

static struct Mode * mode_from_name ( const char * name,
bool pango_markup )
static

Definition at line 32 of file bindings.c.

References Mode::bindings, modes, Mode::name, Mode::pango_markup, scalloc(), SLIST_FOREACH, SLIST_INSERT_HEAD, sstrdup(), and TAILQ_INIT.

Referenced by configure_binding().

Here is the call graph for this function:

◆ regrab_all_buttons()

void regrab_all_buttons ( xcb_connection_t * conn)

Release the button grabs on all managed windows and regrab them, reevaluating which buttons need to be grabbed.

Definition at line 187 of file bindings.c.

References all_cons, bindings_get_buttons_to_grab(), conn, FREE, Window::id, TAILQ_FOREACH, Con::window, and xcb_grab_buttons().

Referenced by load_configuration(), and switch_mode().

Here is the call graph for this function:

◆ reorder_binding_cmp()

static int reorder_binding_cmp ( const void * a,
const void * b )
static

Definition at line 678 of file bindings.c.

References Binding::event_state_mask.

Referenced by reorder_bindings_of_mode().

◆ reorder_bindings()

void reorder_bindings ( void )

Reorders bindings by event_state_mask descendingly so that get_binding() correctly matches more specific bindings before more generic bindings.

Take the following binding configuration as an example:

bindsym n nop lower-case n pressed bindsym Shift+n nop upper-case n pressed

Without reordering, the first binding’s event_state_mask of 0x0 would match the actual event_stat_mask of 0x1 and hence trigger instead of the second keybinding.

Definition at line 732 of file bindings.c.

References bindings, Mode::bindings, current_mode, modes, reorder_bindings_of_mode(), and SLIST_FOREACH.

Referenced by load_configuration().

Here is the call graph for this function:

◆ reorder_bindings_of_mode()

static void reorder_bindings_of_mode ( struct Mode * mode)
static

Definition at line 690 of file bindings.c.

References bindings, Mode::bindings, reorder_binding_cmp(), scalloc(), TAILQ_EMPTY, TAILQ_FOREACH, TAILQ_INIT, TAILQ_INSERT_TAIL, and TAILQ_REMOVE.

Referenced by reorder_bindings().

Here is the call graph for this function:

◆ run_binding()

CommandResult * run_binding ( Binding * bind,
Con * con )

Runs the given binding and handles parse errors.

If con is passed, it will execute the command binding with that container selected by criteria. Returns a CommandResult for running the binding's command. Caller should render tree if needs_tree_render is true. Free with command_result_free().

Definition at line 859 of file bindings.c.

References binding_copy(), binding_free(), Binding::command, command_error_nagbar_pid, config, current_binding_mode, errorfilename, Config::font, FREE, ipc_send_binding_event(), CommandResult::needs_tree_render, parse_command(), CommandResult::parse_error, Font::pattern, sasprintf(), sstrdup(), start_nagbar(), and tree_render().

Referenced by handle_button_press(), handle_key_press(), and route_click().

Here is the call graph for this function:

◆ switch_mode()

void switch_mode ( const char * new_mode)

Switches the key bindings to the given mode, if the mode exists.

Definition at line 639 of file bindings.c.

References bindings, Mode::bindings, conn, current_binding_mode, DLOG, ELOG, FREE, grab_all_keys(), ipc_send_event(), modes, Mode::name, Mode::pango_markup, regrab_all_buttons(), Binding::release, sasprintf(), SLIST_FOREACH, TAILQ_FOREACH, translate_keysyms(), and ungrab_all_keys().

Referenced by cmd_mode(), and free_configuration().

Here is the call graph for this function:

◆ translate_keysyms()

Variable Documentation

◆ command_error_nagbar_pid

pid_t command_error_nagbar_pid = -1

Definition at line 19 of file bindings.c.

Referenced by cmd_reload(), i3_exit(), i3_restart(), and run_binding().

◆ DEFAULT_BINDING_MODE

const char* DEFAULT_BINDING_MODE = "default"

The name of the default mode.

Definition at line 25 of file bindings.c.

Referenced by CFGFUN(), and CFGFUN().

◆ xkb_context

struct xkb_context* xkb_context
static

Definition at line 16 of file bindings.c.

Referenced by load_keymap().

◆ xkb_keymap

struct xkb_keymap* xkb_keymap
static

Definition at line 17 of file bindings.c.

Referenced by load_keymap(), and translate_keysyms().