From dd12715f5fa54a05b0a74acb57ec460e0c4e7010 Mon Sep 17 00:00:00 2001 From: Ryan Reaves Date: Mon, 29 Jul 2019 15:47:00 -0500 Subject: [PATCH] - Added more prompts and detections to install script, added arguments to simple test script for keymap testing. Don't assume individual keymap commands can be applied to multiple keyboards at once if the device command for setxkbmap or xkbcomp is used. --- install.py | 110 ++++++++++++++++++++++++++-------- system-config/xactive.sh | 124 +++++++++++++++++++++++++++++++++++++++ test.py | 62 +++++++++++++------- 3 files changed, 250 insertions(+), 46 deletions(-) create mode 100755 system-config/xactive.sh diff --git a/install.py b/install.py index f58f1c0..66c07b2 100755 --- a/install.py +++ b/install.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -import os, platform, sysconfig, sys, subprocess +import os, platform, sysconfig, sys, subprocess, time yellow = "\033[1;33m" green = "\033[0;32m" @@ -18,6 +18,54 @@ syme = '\u0250' syma = '\u0251' # bang = '\u1F589'.decode('unicode-escape') +internalid = 0 +usbid = 0 + +def keyboard_detect(): + global internalid + global usbid + + internal_kbname = "" + usb_kbname = "" + print() + print("Looking for keyboards...") + 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') + if result != "": + 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') + 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') + if result != "": + usb_kbname = result.strip() + + # Loop the following to ensure the id is picked up after 5-10 tries + usbid = "" + usbcount=0 + 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') + if usbid == "": + usbcount += 1 + # print('usbid not found '+ str(usbcount)) + if usbcount == 5: + usbid = "none found" + time.sleep(1) + print("\nUSB Keyboard\n" + "Name: " + usb_kbname + "\nID: " + usbid) + + + +def os_detect(): + print() + print("Checking for OS and system information...") + print() + print("OS Type") + # print(" Platform: " + platform_name) + if platform_name == 'Linux': + result = subprocess.check_output("lsb_release -a | grep -v LSB ", shell=True).decode('ascii') + print(result) def hwinfo(): print("Requires user password to display hardware information...") @@ -36,36 +84,46 @@ print(italic + " - F!x the d" + syma + "mn k" + syme + "yb" + circleo sys.stdout.write(reset) print() input("Press Enter to begin...") -print() -print("Checking for OS and system information...") -print() -print("OS Type") -# print(" Platform: " + platform_name) -if platform_name == 'Linux': - result = subprocess.check_output("lsb_release -a | grep -v LSB ", shell=True).decode('ascii') - print(result) -print() -print("Looking for keyboards...") -print() -result = subprocess.check_output('xinput list | grep -iv "Virtual\|USB" | grep -i "keyboard.*keyboard";exit 0', shell=True).decode('utf-8') -if result != "": - print("Built in keyboard") - print(result) -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 != "": - print("USB keyboard") - print(result) +system_type = input("\nWhat type of system are you using?\n\ + 1) Windows\\Linux Laptop\n\ + 2) Chromebook Laptop\n\ + 3) Macbook\n\ + 4) Windows\Linux Desktop\n\ + 5) Chrome Desktop (w/ Chrome keyboard)\n\ + 6) Mac Desktop\n") -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') +swap_behavior = 1 +# Chromebook +if system_type == "2" or system_type == "5": + if not 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": + swap_behavior = 0 +# Windows +if system_type == "1" or system_type == "4": + 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\ +Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y": + swap_behavior = 0 +# Mac +if system_type == "3" or system_type == "6": + 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\ +Note: For a more mac like experience & less issues with terminal based interactions y is recommended.\n").lower().strip()[:1] == "y": + swap_behavior = 0 -# Loop the following to ensure the id is picked up after 5-10 tries -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 == "": - usbid = "none found" +if int(system_type) < 4: + laptop_kb = input("\nWhat is your keyboard configuration?\n\ + 1) Both\n\ + 2) Built-in\n\ + 3) USB External\n") +else: + laptop_kb = 3 +keyboard_detect() + +# terminal_kb + +# os_detect() -print("Internal Keyboard ID: " + internalid + "USB Keyboard ID: " + usbid) # Print out/confirm the keyboard type or types detected, Windows, Mac, or Chromebook. # Print out the keymap method/arrangement that will be used for the keyboard(s) diff --git a/system-config/xactive.sh b/system-config/xactive.sh new file mode 100755 index 0000000..29a263f --- /dev/null +++ b/system-config/xactive.sh @@ -0,0 +1,124 @@ +#!/bin/bash +class_name='konsole' + +internalkb=$1 +internalid=$2 +usbkb=$3 +usbid=$4 +swapbehavior=$5 + +# echo $1 $2 $3 $4 + +internalgui="setxkbmap -device $internalid -option " +internalterm="$internalgui" +usbgui="setxkbmap -device $usbid -option " +usbterm="$usbgui" + +if [[ $internalkb == "windows" ]]; then + internalgui+="altwin:ctrl_alt_win" + internalterm+="altwin:swap_alt_win" + internalgrep_term="-q ctrl_alt_win" + internalgrep_gui="-v ctrl_alt_win\ 1>/dev/null" +elif [[ $internalkb == "chromebook" ]]; then + internalgui="xkbcomp -w0 -I$HOME/.xkb -i $internalid ~/.xkb/keymap/kbd.gui $DISPLAY" + internalterm+="altwin:swap_lalt_lwin" + # internalgrep_term="-vq swap_lalt_lwin" + internalgrep_term="-q swap_lalt_lwin" + internalgrep_gui="-v swap_lalt_lwin 1>/dev/null" +elif [[ $internalkb == "mac" ]]; then + internalgui+="ctrl:swap_lwin_lctl" + #mac term is blank + internalterm="" + internalgrep_term="-q swap_lwin_lctl" + internalgrep_gui="-v swap_lwin_lctl 1>/dev/null" +fi + +if [[ $usbkb == "windows" ]]; then + usbgui+="altwin:ctrl_alt_win" + 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 +grep_id='0[xX][a-zA-Z0-9]\{7\}' + +#Storing timestamp and will use timediff to prevent xprop duplicates +timestp=$(date +%s) + +xprop -spy -root _NET_ACTIVE_WINDOW | grep --line-buffered -o $grep_id | +while read -r id; do + class="`xprop -id $id WM_CLASS | grep $class_name`" + newtime=$(date +%s) + timediff=$((newtime-timestp)) + if [ $timediff -gt 0 ]; then + if [ -n "$class" ]; then + # Set keymap for terminal, Alt is Super, Ctrl is Ctrl, Super is Alt + if [[ $internalid -gt 0 ]]; then + echo "internal gui to term" + echo "$internalgrep_gui" + eval "setxkbmap -query | grep $internalgrep_gui" + if [ $? -eq 0 ]; then + echo "* inside internal gui to term" + echo "* $internalterm" + eval setxkbmap -device $internalid -option + eval $internalterm + fi + fi + if [[ $usbid -gt 0 ]]; then + echo "usb gui to term" + echo "$usbgrep_gui" + eval "setxkbmap -query | grep $usbgrep_gui" + if [ $? -eq 0 ]; then + echo "* inside usb gui to term" + echo "* $usbterm" + eval setxkbmap -device $usbid -option + eval $usbterm + fi + fi + else + # Set keymap for gui, Alt is Ctrl,Super is Alt, Ctrl is Super + if [[ $internalid -gt 0 ]]; then + echo "internal term to gui" + echo "$internalgrep_term" + eval "setxkbmap -query | grep $internalgrep_term" + if [ $? -eq 0 ]; then + echo "** inside internal term to gui" + echo "** $internalgui" + eval "setxkbmap -device $internalid -option" + eval "$internalgui" + fi + fi + if [[ $usbid -gt 0 ]]; then + 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 + timestp=$(date +%s) + fi +done \ No newline at end of file diff --git a/test.py b/test.py index 1120a13..f04cdec 100755 --- a/test.py +++ b/test.py @@ -2,7 +2,13 @@ # pip3 install --no-deps pynput from pynput.keyboard import Key, Listener -import sys +import sys, subprocess + +uitype=sys.argv[1] +apply_rules=sys.argv[2] +windows=sys.argv[3] +chromebook=sys.argv[4] +mac=sys.argv[5] class color: PURPLE = '\033[95m' @@ -266,28 +272,44 @@ def mac_keys_terminal(): on_release=is_ctrl_terminal) as listener: listener.join() -# reset setxkbmap -option +if uitype == 'gui': + if chromebook == '1': + if apply_rules == '1': + subprocess.check_output('setxkbmap -option;xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY', shell=True).decode('utf-8') + print() + print("Testing chromebook - GUI apps - Kinto keymapping...") + chromebook_keys_gui() -# xkbcomp -w0 -I$HOME/.xkb ~/.xkb/keymap/kbd.gui $DISPLAY -# print("Testing chromebook - GUI apps - Kinto keymapping...") -# chromebook_keys_gui() + if windows == '1': + if apply_rules == '1': + subprocess.check_output('setxkbmap -option;setxkbmap -option altwin:ctrl_alt_win', shell=True).decode('utf-8') + print() + print ("Testing windows keyboard - GUI apps - Kinto keymapping...") + windows_keys_gui() -# setxkbmap -option altwin:swap_lalt_lwin -# print("Testing chromebook - terminal - Kinto keymapping...") -# chromebook_keys_terminal() + if mac == '1': + if apply_rules == '1': + subprocess.check_output('setxkbmap -option;setxkbmap -option ctrl:swap_lwin_lctl', shell=True).decode('utf-8') + print ("Testing mac keyboard - GUI apps - Kinto keymapping...") + mac_keys_gui() -# setxkbmap -option altwin:ctrl_alt_win -# print ("Testing windows keyboard - GUI apps - Kinto keymapping...") -# windows_keys_gui() +if uitype == 'term': + if chromebook == '1': + if apply_rules == '1': + subprocess.check_output('setxkbmap -option;setxkbmap -option altwin:swap_lalt_lwin', shell=True).decode('utf-8') + print("Testing chromebook - terminal - Kinto keymapping...") + chromebook_keys_terminal() -# setxkbmap -option altwin:swap_alt_win -# print ("Testing windows keyboard - terminal - Kinto keymapping...") -# windows_keys_terminal() + if windows == '1': + if apply_rules == '1': + subprocess.check_output('setxkbmap -option;setxkbmap -option altwin:swap_alt_win', shell=True).decode('utf-8') + print ("Testing windows keyboard - terminal - Kinto keymapping...") + windows_keys_terminal() -# setxkbmap -option ctrl:swap_lwin_lctl -# print ("Testing mac keyboard - GUI apps - Kinto keymapping...") -# mac_keys_gui() + if mac == '1': + if apply_rules == '1': + subprocess.check_output('setxkbmap -option', shell=True).decode('utf-8') + print ("Testing mac keyboard - terminal - Kinto keymapping...") + mac_keys_terminal() -# setxkbmap -option -# print ("Testing windows keyboard - terminal - Kinto keymapping...") -# mac_keys_terminal() \ No newline at end of file +subprocess.run('setxkbmap -option', shell=True) \ No newline at end of file