Skip to content

Commit

Permalink
Profile script
Browse files Browse the repository at this point in the history
  • Loading branch information
josepuga committed Nov 19, 2023
1 parent f3cbf84 commit 09fbbd7
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 11.0.82, 2023-11-17T19:58:40. -->
<!-- Written by QtCreator 11.0.82, 2023-11-17T22:01:41. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
5 changes: 3 additions & 2 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

QMice cannot (yet) read data from system mouses. Instead it works with profiles, which are .conf files created by users. They contain the hardware data of a specific mouse. In this way, the Kernel can access it and "remap" it.

- **If your mouse is not on the list** you can do this process manually with console commands as explained on the next page . <https://askubuntu.com/questions/1145057/remapping-mouse-buttons-to-keyboard-keysXXX>.
- **If your mouse is not on the list**

If with this page you manage to remap your mouse, you can create a profile so that other users can benefit from it. You have a tutorial on how to do it [here.](./tutorial-create-profile-EN.md)
- you can do this process manually with console commands as explained on the next page . <https://askubuntu.com/questions/1145057/remapping-mouse-buttons-to-keyboard-keysXXX>.
- Or you can create a profile and remap your mouse with the app. You have a tutorial on how to do it [here.](./tutorial-create-profile-EN.md)

- **If your mouse is on the list..**

Expand Down
Binary file added doc/images/create-profile-script.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed doc/images/default-buttons-section.png
Binary file not shown.
Binary file removed doc/images/dummy.png
Binary file not shown.
Binary file removed doc/images/evtest-testing.png
Binary file not shown.
Binary file removed doc/images/id-section.png
Binary file not shown.
Binary file removed doc/images/info-section.png
Binary file not shown.
Binary file removed doc/images/input-devices.png
Binary file not shown.
Binary file added doc/images/profile-generated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 23 additions & 25 deletions doc/tutorial-create-profile-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,47 @@

In this tutorial, we are going to create a profile for the Kensington Expert Mouse as sample.

## The 'easy' part
Wen finish, copy the profile to your **$HOME/.config/qmice-remap/profiles** directory.

Open with your favorite editor the **template.profile** file. <http://github.com/josepuga/qmice-remap/assets/template.profile>
## Getting the device number

Open a system console.
Linux assign an unique id to every device. Not always is the same, it could change after reboot. We need to know wich device number has our mouse. Open a system console:

```bash
cat /proc/bus/input/devices > /tmp/devices.logs
sudo evtest
```

Open /tmp/devices.log and search until you see your device. In this case:

![Input Devices](./images/input-devices.png)
![evtest](./images/evtest.png)

The only important data is the yellow one.
The program shows a list with all your devices. Take note of your mouse id (in our example is 11). We can exit pressing CTRL+C

Copy Bus, Vendor, Product and Version to **template.profile**.
## The 'easy' part

![id Section](./images/id-section.png)
A shell script will do almost all the job. Download it from <http://github.com/josepuga/qmice-remap/scripts/create-profile.sh>

On info section:
The script needs sudo privileges and will check all requisites before start. **Change de number 11 for your device id**.

- The device name.
- (optional). Your name as uploader.
- (optional). Any comment about the device. I.e. Button 5 only works in Windows. (I'm jocking, this model only has 4 buttons). The comment should be in ONLY ONE LINE.
```bash
sudo ./create-profile.sh 11
```

![info Section](./images/info-section.png)
![Create profile script](./images/create-profile-script.png)

## The 'hard' part
Click your mouse buttons. Don't worry if you repeat some of them. Is more important **to not press 2 or more together**. At the end check if the number buttons recorded is the same of your mouse buttons.

Now we need to backup the default buttons configuration before any mapping. The utility **evtest** must be installed on your system. Another command in the system console:
Open with your favorite editor the file generated on /tmp

```bash
sudo evtest
```
![Profile generate](./images/profile-generated.png)

![evtest](./images/evtest.png)
There are few things to do:

Evtest is asking what event to handle. **Is the event number in devices.logs before**. In this case 11. Type the number and now you will enter in an interactive mode. Click all your buttons one by one, and anotatte any event in the **default buttons** section in **template.profile**. (In order from smallest value to biggest **THIS IS VERY IMPORTANT**).
- (optional). **uploader=** Type your nick or name.
- (optional). **comment=** Any necessary comment about the device. The comment should be in only one line. Other lines will be ignored.
- **image=** This parameter is mandatory as I explain in the next section.

![evtest testing](./images/evtest-testing.png)
---

![default buttons Section](./images/default-buttons-section.png)
> **_NOTE:_** There is no magic way to know wich one is your button number 1, 2, 3, ... The numbers inside **[default buttons]** could be 90007, 90023 or whatever. Leave the list sorted, and set the button 1 the fist line, and so on. Its easy to match with your real mouse because every BTN_XXX tag is different (In the sample are 2 equals because has been remaped before)
## The 'funny' part

Expand All @@ -68,7 +66,7 @@ Upload the picture and encode it.Copy the result text, and paste on gui section,

## Final: Copying and publishing the file

Rename the template to other name and copy it to your **$HOME/.config/pugamice-remap/**. The name is up to you. The extension must be **.profile**. However, to avoid name conflicts. I suggest use the formula bus-vendor-product-version: 0003-047d-1020-0111.profile
Copy it to **$HOME/.config/qmice-remap/profiles**. The extension must be **.profile**. To avoid name conflicts. I suggest use the name bus-vendor-product-version: 0003-047d-1020-0111.profile

At this time I dont have any repository, and I don't think it's necessary for every user to have a GitHub account to send the file like a post.

Expand Down
2 changes: 1 addition & 1 deletion profiles/0003-046d-c408-0110.profile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Profile file for pugamice-remap.
# Profile file for qmice-remap.
#
# Read tutorial on github.

Expand Down
157 changes: 157 additions & 0 deletions scripts/create-profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/bin/bash
# Script to create a qmice-remap profile file from a mouse.
# https://github.com/josepuga/qmice-remap
# By José Puga 2023. Under MIT License.

# Does evtest is installed?
#TODO: python3 too
evtest_command=$(whereis evtest | awk '{print $2;}')
if [ -z "$evtest_command" ]; then
echo "evtest must be installed."
exit 1
fi


# Check parameter
if [ -z "$1" ]; then
echo "Usage $0 DeviceNumber"
exit 1
fi
event_path="/dev/input/event$1"

# If not a Character device...
if [ ! -c "$event_path" ]; then
echo "Bad Device Number: $1"
exit 1
fi

if [ "$(id -u)" -ne 0 ]; then
echo Run with sudo
exit 1
fi

# Trap temp file for delete it on any exit
tmp_file=$(mktemp)
trap 'rm "$tmp_file"' EXIT

# Run evtest in background and check for errors.
evtest "$event_path" | tee "$tmp_file" &> /dev/null &
if [ ! "$?" -eq 0 ]; then
echo Error running evtest

exit 1
fi
evtest_pid=$!

echo evtest is lissening your mouse. Click all your buttons ONE BY ONE.
for n in {8..1}; do
echo "$n" seconds left...
sleep 1
done
kill -9 "$evtest_pid"

# Parse device name
# $1: File to parse
# Return: string with the name
GetName() {
grep "Input device name" "$1" | \
awk -F: '{print $2}' | tr -d '"'
}

# Parse buttons
# Every button info is in 2 lines. The first one, has the KEYBOARD_
# The second one, the button tag (BTN_XXX)
# Sample output...
# Event: time 1700309638.527939, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90004
# Event: time 1700309638.271909, type 1 (EV_KEY), code 273 (BTN_RIGHT), value 1
# $1: File to parse
# Return: One line by button, ie: 90004=BTN_RIGHT
GetButtons() {
is_keycode=1
IFS=$'\n'
lines=$(grep -e ^Event "$1" | grep -v 'SYN_REPORT\|EV_REL')
key=
value=
raw_key_value=
for line in $lines; do
if [ "$is_keycode" -eq 1 ]; then
is_keycode=0
key=$(echo "$line" | awk '{print $NF}')
else
is_keycode=1
value=$(echo "$line" | awk '{print $(NF-2)}' | tr -d '(),')
raw_key_value="$raw_key_value$key=$value\n"
fi
done
IFS=$' '
echo -e "$raw_key_value" | sort -u

}

# Parse bus, vendor, product and version. Extract from line like this:
# Input device ID: bus 0x3 vendor 0x47d product 0x1020 version 0x111
# $1: File to parse
# Return: string with the 4 numbers
GetInfo () {
# Extract those numbers and rip off the "0x"
raw_info=$(grep "Input device ID" "$1" | \
awk '{print $5, $7, $9, $11}' | \
sed 's/0x//g' | tr " " "\n")
# Now we need to pad with 4 len every data with '0'. IE. 47d => 047d
# I don't know how to do that in Bash with strings, (`printf "%04d" $param`
# only works with numbers). Using python instead.
result=( $(echo $raw_info) )
for i in "${!result[@]}"; do
result[i]=$(python3 -c "print('${result[i]}'.rjust(4, '0'))")
done
echo "${result[@]}"
}



name=$(GetName "$tmp_file")
info=( $(GetInfo "$tmp_file") )

# Creating the name of the profile file, now we get the info.
profile_file="/tmp/${info[0]}-${info[1]}-${info[2]}-${info[3]}.profile"
cat <<EOF > "$profile_file"
# Mouse profile file for qmice-remap.
#
# Read tutorial on https://github.com/josepuga/qmice-remap/blob/main/doc/tutorial-create-profile-EN.md
[info]
name=$name
uploader=
comment=formula
[id]
bustype=${info[0]}
vendor=${info[1]}
product=${info[2]}
version=${info[3]}
EOF

buttons=$(GetButtons "$tmp_file" | grep -v ^$) #TODO: Check this missing return
printf "%s\n" "[default buttons]" "$buttons" >> "$profile_file"
cat <<EOF >> "$profile_file"
[gui]
image=
EOF

# The real owner needs the profile
[ "$SUDO_USER" ] && chown "$SUDO_UID:$SUDO_GID" "$profile_file"

buttons_count=$(echo "$buttons" | wc -w)
echo ''
echo "$buttons_count" buttons recorded.
echo Your profile has been saved on "$profile_file"

exit






0 comments on commit 09fbbd7

Please sign in to comment.