Skip to content

Instantly share code, notes, and snippets.

@Langerz82
Created February 26, 2026 11:09
Show Gist options
  • Select an option

  • Save Langerz82/4c88bc1b3c869ec404fc9f8d8d28b838 to your computer and use it in GitHub Desktop.

Select an option

Save Langerz82/4c88bc1b3c869ec404fc9f8d8d28b838 to your computer and use it in GitHub Desktop.
EE - joy_common.sh 4.9
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2020-present Shanti Gilbert (https://github.com/shantigilbert)
# Copyright (C) 2022-present Joshua L (https://github.com/Langerz82)
# 08/01/23 - Joshua L - Modified get GUID thanks to shantigilbert.
# 16/10/25 - Joshua L - Modified uses sdljoytest.
# 22/02/26 - Joshua L - Added js instance ID.
# 23/02/26 - Pablo S - Added ES Ordering fixes. (https://github.com/pmsobrado)
# Source predefined functions and variables
. /etc/profile
GCDB="${SDL_GAMECONTROLLERCONFIG_FILE}"
EMULATOR="${1}"
mkdir -p "/tmp/jc"
GAMEPAD_INFO_ALL="/tmp/jc/gamepad_info.txt"
CONTROLLERS_PRIORITY_DATA=
[[ -f "/tmp/controllerconfig.txt" ]] && CONTROLLERS_PRIORITY_DATA=$(cat "/tmp/controllerconfig.txt")
jc_wipe_config_sub_heading() {
local config_file="$1"
local sub_heading="$2"
local tmp_file="$3"
shift
shift
shift
declare -a array_ref=("$@")
[[ -f "${tmp_file}" ]] && rm "${tmp_file}"
local LN=1
local START_LN=-1
[[ ! -f "${config_file}" ]] && return
local REGEX_SUB_HEADING='^\[.+\]$'
while read -r line; do
if [[ "${line}" =~ $REGEX_SUB_HEADING ]]; then
if [[ "${line}" == "${sub_heading}" ]]; then
START_LN=${LN}
else
[[ ${START_LN} != -1 ]] && break
fi
fi
LN=$(( LN + 1 ))
local rx=
for item in "${array_ref[@]}"; do
rx="^${item}\ *\=.+$"
[[ "${line}" =~ ${rx} ]] && echo "${line}" >> ${tmp_file}
done
done < ${config_file}
if [[ ${START_LN} != -1 ]]; then
sed -i "${START_LN},$(( LN-1 ))d" "${config_file}"
fi
}
jc_get_config() {
local GAMEPAD_DATA=$(cat ${GAMEPAD_INFO_ALL} | grep -E -A5 "^Gamepad ${1}$")
[[ -z "${GAMEPAD_DATA}" ]] && echo '' && return
mapfile -t GAMEPAD_INFO < <(echo "${GAMEPAD_DATA}")
local JOY_UDEV_NAME="$( echo "${GAMEPAD_INFO[1]}" | cut -c18- )"
local JOY_SDL_NAME="$( echo "${GAMEPAD_INFO[2]}" | cut -c18- )"
local DEVICE_GUID="$( echo "${GAMEPAD_INFO[3]}" | cut -c18- )"
local JOYMAPPING="$( echo "${GAMEPAD_INFO[4]}" | cut -c18- )"
local INSTANCE_ID="$( echo "${GAMEPAD_INFO[5]}" | cut -c18- )"
local JS_INDEX="$( echo "${GAMEPAD_INFO[6]}" | cut -c18- )"
echo js${JS_INDEX} ${DEVICE_GUID} \"${JOY_UDEV_NAME}\" \"${JOYMAPPING}\" \"${JOY_SDL_NAME}\"
}
jc_get_players() {
gamepad_info -more > ${GAMEPAD_INFO_ALL}
declare -a player_cfgs=()
declare -a player_order=()
for i in {0..3}; do
local CFG=$( jc_get_config "${i}" )
local CURRENT_GUID=$(echo "${CFG}" | awk '{print $2}')
local PINDEX=
# 1. Get player for this physical index (jci)
if [[ ! -z "${CURRENT_GUID}" ]]; then
PINDEX=$(echo "${CONTROLLERS_PRIORITY_DATA}" | grep -o "\-p[1-4]guid ${CURRENT_GUID}" | head -n1 | cut -c3)
PINDEX=$(( PINDEX - 1 ))
fi
# 2. If it does not exist on the priority settings, assign default one but respecting reserved joypad slots
if [[ ! -n "${PINDEX}" ]]; then
for pi in {0..3}; do
PINDEX=$pi
[[ ! " ${player_order[@]} " =~ " ${PINDEX} " ]] && break
done
fi
player_order+=(${PINDEX})
player_cfgs+=("${CFG}")
done
for i in {0..3}; do
local pi=$(( i + 1 ))
clean_pad ${pi}
local order=${player_order[${i}]}
local cfg=${player_cfgs[${order}]}
[[ ! -z "${cfg}" ]] && eval set_pad ${pi} ${cfg}
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment