end_keys_setup() {  # called by rc after sourcing this file
    # activate default mode:
    eval $($READ /action/mode_$DEFAULTMODE)
}

bind() {
    if [ $# -eq 3 ]; then
        $CREATE /keys/mode/"$1"/"$2" "$3"
    else
        echo "error: $0: bind takes exactly three arguments" >&2
        echo "usage: bind <mode> <keycombo> <command>" >&2
    fi
}

#---------------------------------------------------------------------

# set location of the actions:
$WRITE /keys/lookup/actions "/action $WMIFS_SOCKET"

# set box style for shortcut sequences:
$WRITE /keys/box/style/text-align "$SELECTED_TEXT_ALIGN"
$WRITE /keys/box/style/text-font "$SELECTED_TEXT_FONT"
$WRITE /keys/box/style/text-size "$SELECTED_TEXT_SIZE"
$WRITE /keys/box/style/text-color "$SELECTED_FG_COLOR"
$WRITE /keys/box/style/bg-color "$SELECTED_BG_COLOR"
$WRITE /keys/box/style/border-color "$SELECTED_BORDER_COLOR"

$REMOVE /keys/mode 2>/dev/null

# set key bindings of the bare mode:
bind bare $MODKEY+Escape "@{mode_$DEFAULTMODE}"

# set key bindings of the move mode:
bind move Escape "@{mode_$DEFAULTMODE}"
bind move $MODKEY+ctrl+r "@{mode_resize}"
bind move $NORTHKEY "@{move_frame_north}"
bind move $SOUTHKEY "@{move_frame_south}"
bind move $WESTKEY "@{move_frame_west}"
bind move $EASTKEY "@{move_frame_east}"
bind move shift+$NORTHKEY "@{snap_frame_north}"
bind move shift+$SOUTHKEY "@{snap_frame_south}"
bind move shift+$WESTKEY "@{snap_frame_west}"
bind move shift+$EASTKEY "@{snap_frame_east}"

# set key bindings of the resize mode:
bind resize Escape "@{mode_$DEFAULTMODE}"
bind resize $MODKEY+ctrl+m "@{mode_move}"
bind resize $NORTHKEY "@{grow_frame_north}"
bind resize $SOUTHKEY "@{grow_frame_south}"
bind resize $WESTKEY "@{grow_frame_west}"
bind resize $EASTKEY "@{grow_frame_east}"
bind resize shift+$NORTHKEY "@{shrink_frame_north}"
bind resize shift+$SOUTHKEY "@{shrink_frame_south}"
bind resize shift+$WESTKEY "@{shrink_frame_west}"
bind resize shift+$EASTKEY "@{shrink_frame_east}"

# set key bindings of the advanced mode:
# ctrl prefix is always WM related
bind advanced $MODKEY+ctrl+n "@{mode_normal}"
bind advanced $MODKEY+ctrl+b "@{mode_bare}"
bind advanced $MODKEY+ctrl+m "@{mode_move}"
bind advanced $MODKEY+ctrl+r "@{mode_resize}"
bind advanced $MODKEY+ctrl+$WESTKEY "@{select_prev_page}"
bind advanced $MODKEY+ctrl+$EASTKEY "@{select_next_page}"
bind advanced $MODKEY+ctrl+y "@{new_page}"
bind advanced $MODKEY+ctrl+a "@{input_actions}"
bind advanced $MODKEY+ctrl+p "@{input_programs}"
bind advanced $MODKEY+ctrl+c "@{close_client}"
bind advanced $MODKEY+ctrl+q/y "@{quit}"
# shortcuts without ctrl are frame related
bind advanced $MODKEY+t "@{term}"
bind advanced $MODKEY+Tab "@{select_next_frame}"
bind advanced $MODKEY+shift+Tab "@{select_prev_frame}"
bind advanced $MODKEY+$EASTKEY "@{select_next_frame}"
bind advanced $MODKEY+$WESTKEY "@{select_prev_frame}"
bind advanced $MODKEY+$NORTHKEY "@{select_parent_frame}"
bind advanced $MODKEY+$SOUTHKEY "@{select_child_frame}"
bind advanced $MODKEY+d "@{detach_client}"
bind advanced $MODKEY+a "@{attach_client}"
bind advanced $MODKEY+m "@{max_frame}"
bind advanced $MODKEY+Return "@{swap}"
bind advanced $MODKEY+z "@{toggle_zoom}"
bind advanced $MODKEY+shift+h "@{apply_hsplit_layout}"
bind advanced $MODKEY+shift+m "@{apply_multi_layout}"
bind advanced $MODKEY+shift+g "@{apply_grid_layout}"
bind advanced $MODKEY+shift+s "@{apply_single_layout}"
bind advanced $MODKEY+shift+t "@{apply_tile_layout}"
bind advanced $MODKEY+shift+v "@{apply_vsplit_layout}"

# set key bindings of the normal mode:
# ctrl prefix is always WM related
bind normal $MODKEY+ctrl+v "@{mode_advanced}"
bind normal $MODKEY+ctrl+b "@{mode_bare}"
bind normal $MODKEY+ctrl+m "@{mode_move}"
bind normal $MODKEY+ctrl+r "@{mode_resize}"
bind normal $MODKEY+ctrl+a "@{input_actions}"
bind normal $MODKEY+ctrl+p "@{input_programs}"
bind normal $MODKEY+ctrl+$WESTKEY "@{select_prev_page}"
bind normal $MODKEY+ctrl+$EASTKEY "@{select_next_page}"
# shortcuts without ctrl are frame related
bind normal $MODKEY+t "@{term}"
bind normal $MODKEY+Tab "@{select_next_frame}"
bind normal $MODKEY+shift+Tab "@{select_prev_frame}"
bind normal $MODKEY+$EASTKEY "@{select_next_frame}"
bind normal $MODKEY+$WESTKEY "@{select_prev_frame}"
bind normal $MODKEY+$NORTHKEY "@{select_parent_frame}"
bind normal $MODKEY+$SOUTHKEY "@{select_child_frame}"
