- Simplified and refactored much of the code for the actual use cases, the granular/specific approach did not pan out and in some cases less ideal.

This commit is contained in:
Ryan Reaves
2019-07-29 21:29:26 -05:00
parent a22e05cebd
commit aa895a8be8
2 changed files with 83 additions and 130 deletions

View File

@@ -22,38 +22,34 @@ internalid = 0
usbid = 0 usbid = 0
def keyboard_detect(): def keyboard_detect():
global internalid global internalid, usbid, chromeswap
global usbid
internal_kbname = "" internal_kbname = ""
usb_kbname = "" usb_kbname = ""
print() print()
print("Looking for keyboards...") print("Looking for keyboards...")
print() print()
if laptop_kb == "1" or laptop_kb == "2": result = subprocess.check_output('xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | grep -o -P "(?<=↳).*(?=id\=)";exit 0', shell=True).decode('utf-8')
result = subprocess.check_output('xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | grep -o -P "(?<=↳).*(?=id\=)";exit 0', shell=True).decode('utf-8') if result != "":
if result != "": internal_kbname = result.strip()
internal_kbname = result.strip() internalid = subprocess.check_output('xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | cut -d "=" -f 2- | awk \'{print $1}\';exit 0', shell=True).decode('utf-8')
internalid = subprocess.check_output('xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard" | cut -d "=" -f 2- | awk \'{print $1}\';exit 0', shell=True).decode('utf-8') print("Internal Keyboard\nName: " + internal_kbname + "\nID: " + internalid)
print("Internal Keyboard\nName: " + internal_kbname + "\nID: " + internalid)
if laptop_kb == "1" or laptop_kb == "3": result = subprocess.check_output('udevadm info -e | grep -o -P "(?<=by-id/usb-).*(?=-event-kbd)" | head -1;exit 0', shell=True).decode('utf-8')
result = subprocess.check_output('udevadm info -e | grep -o -P "(?<=by-id/usb-).*(?=-event-kbd)" | head -1;exit 0', shell=True).decode('utf-8') if result != "":
if result != "": usb_kbname = result.strip()
usb_kbname = result.strip()
# Loop the following to ensure the id is picked up after 5-10 tries # Loop the following to ensure the id is picked up after 5-10 tries
usbid = "" usbid = ""
usbcount=0 usbcount=0
while usbid == "": while usbid == "":
usbid = subprocess.check_output('udevadm info -e | stdbuf -oL grep -o -P "(?<=event-kbd /dev/input/by-path/pci-0000:00:).*(?=.0-usb)";exit 0', shell=True).decode('utf-8') usbid = subprocess.check_output('udevadm info -e | stdbuf -oL grep -o -P "(?<=event-kbd /dev/input/by-path/pci-0000:00:).*(?=.0-usb)";exit 0', shell=True).decode('utf-8')
if usbid == "": if usbid == "":
usbcount += 1 usbcount += 1
# print('usbid not found '+ str(usbcount)) # print('usbid not found '+ str(usbcount))
if usbcount == 5: if usbcount == 5:
usbid = "none found" usbid = "none found"
time.sleep(1) time.sleep(1)
print("\nUSB Keyboard\n" + "Name: " + usb_kbname + "\nID: " + usbid) print("\nUSB Keyboard\n" + "Name: " + usb_kbname + "\nID: " + usbid)
@@ -86,37 +82,36 @@ print()
input("Press Enter to begin...") input("Press Enter to begin...")
system_type = input("\nWhat type of system are you using?\n\ system_type = input("\nWhat type of system are you using?\n\
1) Windows\\Linux Laptop\n\ 1) Windows\n\
2) Chromebook Laptop\n\ 2) Chromebook\n\
3) Macbook\n\ 3) Mac\n")
4) Windows\Linux Desktop\n\
5) Chrome Desktop (w/ Chrome keyboard)\n\
6) Mac Desktop\n")
swap_behavior = 1 swap_behavior = 1
# Chromebook # Chromebook
if system_type == "2" or system_type == "5": if system_type == "2":
if not input("\nWould you like to swap Alt to Super/Win and Search key to Ctrl when using terminal applications? (y/n)\n\ if input("\nWould you like to swap Alt to Super/Win and Search key to Ctrl when using terminal applications? (y/n)\n\
Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y": Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y":
swap_behavior = 0 swap_behavior = 0
# Windows # Windows
if system_type == "1" or system_type == "4": if system_type == "1":
if not input("\nWould you like to swap Alt to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)\n\ if input("\nWould you like to swap Alt to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)\n\
Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y": Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y":
swap_behavior = 0 swap_behavior = 0
# Mac # Mac
if system_type == "3" or system_type == "6": if system_type == "3":
if not input("\nWould you like to swap Command back to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)\n\ if input("\nWould you like to swap Command back to Super/Win and Ctrl key back to Ctrl when using terminal applications? (y/n)\n\
Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y": Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y":
swap_behavior = 0 swap_behavior = 0
if int(system_type) < 4: print(system_type + " " + str(swap_behavior))
laptop_kb = input("\nWhat is your keyboard configuration?\n\ if int(system_type) == 2 and swap_behavior == 0:
1) Both\n\ chromeswap = input("\nIf the keyswap is applied on a chromebook with both an internal and external Apple keyboard\n\
2) Built-in\n\ you may need to press a key on the external Apple keyboard any time you switch between the terminal and gui based apps.\n\
3) USB External\n") Are you ok with that, or would you like to only apply the keyswap on one keyboard type?\n\
else: 1) Built-in\n\
laptop_kb = 3 2) Both - (Chromebook & Windows)\n\
3) Both - (Chromebook & Mac)\n\
4) USB External - (Mac)\n")
keyboard_detect() keyboard_detect()

View File

@@ -1,63 +1,37 @@
#!/bin/bash #!/bin/bash
class_name='konsole' class_name='konsole'
internalkb=$1 systemtype=$1
internalid=$2 internalid=$2
usbkb=$3 usbid=$3
usbid=$4 swapbehavior=$4
swapbehavior=$5
# echo $1 $2 $3 $4 # echo $1 $2 $3 $4
internalgui="setxkbmap -device $internalid -option " swapcmd_term="setxkbmap -option;setxkbmap -option altwin:swap_alt_win"
internalterm="$internalgui" fallbackcmd_gui=""
usbgui="setxkbmap -device $usbid -option " if [[ "$systemtype" == "windows" || "$systemtype" == "mac" ]]; then
usbterm="$usbgui" swapcmd_gui="setxkbmap -option;setxkbmap -option altwin:ctrl_alt_win"
check_gt="setxkbmap -query | grep -q 'ctrl_alt_win'"
if [[ $internalkb == "windows" ]]; then check_tg="setxkbmap -query | grep -v 'ctrl_alt_win' 1>/dev/null"
internalgui+="altwin:ctrl_alt_win" elif [[ "$swapbehavior" == "both_mac" ]]; then
internalterm+="altwin:swap_alt_win" swapcmd_gui="setxkbmap -option;setxkbmap -option ctrl:swap_lwin_lctl; xkbcomp -w0 -i $internalid -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY"
internalgrep_term="-q ctrl_alt_win" swapcmd_term="setxkbmap -option;setxkbmap -device $internalid -option 'altwin:swap_alt_win'"
internalgrep_gui="-v ctrl_alt_win\ 1>/dev/null" check_gt="setxkbmap -query | grep -v 'swap_alt_win' 1>/dev/null"
elif [[ $internalkb == "chromebook" ]]; then check_tg="setxkbmap -query | grep -q 'swap_alt_win'"
internalgui="xkbcomp -w0 -I$HOME/.xkb -i $internalid ~/.xkb/keymap/kbd.gui $DISPLAY" elif [[ "$swapbehavior" == "both_win" ]]; then
internalterm+="altwin:swap_lalt_lwin" swapcmd_gui="setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY; setxkbmap -device $usbid -option altwin:ctrl_alt_win"
# internalgrep_term="-vq swap_lalt_lwin" fallbackcmd_gui="setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY"
internalgrep_term="-q swap_lalt_lwin" check_gt="setxkbmap -query | grep -q 'ctrl_alt_win'"
internalgrep_gui="-v swap_lalt_lwin 1>/dev/null" check_tg="setxkbmap -query | grep -v 'ctrl_alt_win' 1>/dev/null"
elif [[ $internalkb == "mac" ]]; then elif [[ "$swapbehavior" == "none" ]]; then
internalgui+="ctrl:swap_lwin_lctl" swapcmd_gui="setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY"
#mac term is blank check_gt="setxkbmap -query | grep -v 'swap_alt_win' 1>/dev/null"
internalterm="" check_tg="setxkbmap -query | grep -q 'swap_alt_win'"
internalgrep_term="-q swap_lwin_lctl"
internalgrep_gui="-v swap_lwin_lctl 1>/dev/null"
fi fi
if [[ $usbkb == "windows" ]]; then # echo "$systemtype $swapbehavior"
usbgui+="altwin:ctrl_alt_win" # echo "$swapcmd_gui"
usbterm+="altwin:swap_alt_win"
usbgrep_term="-q ctrl_alt_win"
usbgrep_gui="-v ctrl_alt_win 1>/dev/null"
elif [[ $usbkb == "chromebook" ]]; then
usbgui="xkbcomp -w0 -I$HOME/.xkb -i $usbid ~/.xkb/keymap/kbd.gui $DISPLAY"
usbterm+="altwin:swap_lalt_lwin"
usbgrep_term="-q swap_lalt_lwin"
usbgrep_gui="-v swap_lalt_lwin 1>/dev/null"
elif [[ $usbkb == "mac" ]]; then
usbgui+="ctrl:swap_lwin_lctl"
#mac term is blank
usbterm=""
usbgrep_term="-q swap_lwin_lctl"
usbgrep_gui="-v swap_lwin_lctl 1>/dev/null"
fi
# echo "$internalgui"
# echo "$internalterm"
# echo "$usbgui"
# echo "$usbterm"
# exit
# regex for extracting hex id's # regex for extracting hex id's
grep_id='0[xX][a-zA-Z0-9]\{7\}' grep_id='0[xX][a-zA-Z0-9]\{7\}'
@@ -74,48 +48,32 @@ while read -r id; do
if [ -n "$class" ]; then if [ -n "$class" ]; then
# Set keymap for terminal, Alt is Super, Ctrl is Ctrl, Super is Alt # Set keymap for terminal, Alt is Super, Ctrl is Ctrl, Super is Alt
if [[ $internalid -gt 0 ]]; then if [[ $internalid -gt 0 ]]; then
echo "internal gui to term" eval "$check_gt;echo $?"
echo "$internalgrep_gui"
eval "setxkbmap -query | grep $internalgrep_gui"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "* inside internal gui to term" echo "internal gui to term"
echo "* $internalterm" eval "$swapcmd_term"
eval setxkbmap -device $internalid -option
eval $internalterm # Quick hack, will want to refactor later
fi # just resets required checks, for chromebooks that
fi # use usb windows keyboards
if [[ $usbid -gt 0 ]]; then if [[ "$swapbehavior" == "both_win" ]]; then
echo "usb gui to term" check_gt="setxkbmap -query | grep -q 'ctrl_alt_win'"
echo "$usbgrep_gui" check_tg="setxkbmap -query | grep -v 'ctrl_alt_win' 1>/dev/null"
eval "setxkbmap -query | grep $usbgrep_gui" fi
if [ $? -eq 0 ]; then
echo "* inside usb gui to term"
echo "* $usbterm"
eval setxkbmap -device $usbid -option
eval $usbterm
fi fi
fi fi
else else
# Set keymap for gui, Alt is Ctrl,Super is Alt, Ctrl is Super # Set keymap for gui, Alt is Ctrl,Super is Alt, Ctrl is Super
if [[ $internalid -gt 0 ]]; then if [[ $internalid -gt 0 ]]; then
echo "internal term to gui" eval "$check_tg;echo $?"
echo "$internalgrep_term"
eval "setxkbmap -query | grep $internalgrep_term"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "** inside internal term to gui" echo "internal term to gui"
echo "** $internalgui" eval "$swapcmd_gui;echo $?"
eval "setxkbmap -device $internalid -option" if [ $? -eq 0 ] && [[ "$swapbehavior" == "both_win" ]]; then
eval "$internalgui" eval "$fallbackcmd_gui"
fi check_gt="setxkbmap -query | grep -v 'swap_alt_win' 1>/dev/null"
fi check_tg="setxkbmap -query | grep -q 'swap_alt_win'"
if [[ $usbid -gt 0 ]]; then fi
echo "usb term to gui"
echo "$usbgrep_term"
eval "setxkbmap -query | grep $usbgrep_term"
if [ $? -eq 0 ]; then
echo "** inside usb term to gui"
eval setxkbmap -device $usbid -option
eval $usbgui
fi fi
fi fi
fi fi