When you rotate your screen output using manual shortcuts, Sway rotates the display (and the pointer position on the touchpad) as requested, but it doesn’t automatically update the calibration for touchscreen input events. This is why your touch input on the screen remains “unrotated” relative to the display.
To fix this, you need to apply a matching transformation (or calibration matrix) for your touchscreen device. Here’s how you can do that:
1. Identify Your Touchscreen Device
Run the following command to list your input devices:
bash
swaymsg -t get_inputs
Look for the touchscreen device by name or identifier.
2. Calculate the Calibration Matrix
For Wayland/Sway the touchscreen’s calibration is defined by a 3×3 matrix.
For example, if you manually rotate your screen 90° to the left, the required transformation matrix is:
code
[ 0 -1 1 ]
[ 1 0 0 ]
[ 0 0 1 ]
This matrix essentially swaps and inverts the axes so that the touchscreen coordinates map correctly to the rotated screen.
Similarly, for other rotations you’ll need to use the appropriate matrix:
Normal (0°):
code
[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
90° (rotated left):
code
[ 0 -1 1 ]
[ 1 0 0 ]
[ 0 0 1 ]
270° (rotated right):
code
[ 0 1 0 ]
[ -1 0 1 ]
[ 0 0 1 ]
180° (inverted):
code
[ -1 0 1 ]
[ 0 -1 1 ]
[ 0 0 1 ]
Note: The first two numbers in the last column are there to translate the coordinates back into the [0,1] range.
3. Configure the Touchscreen in Your Sway Config
Open (or create) your Sway configuration file, typically located at ~/.config/sway/config, and add an input block for your touchscreen device. Replace <TOUCHSCREEN_NAME> with the identifier from the previous step.
For example, if you want your touchscreen to match a 90° left rotation, add:
Similarly, if you rotate the screen differently, change the matrix accordingly.
4. (Optional) Bind a Shortcut to Update Calibration
If you’re using manual shortcuts to rotate your screen, you might want to have matching shortcuts to update the touchscreen calibration on the fly. For example, you could write small scripts that run both the output rotation and update the input calibration via swaymsg. For instance:
Make the script executable and bind it in your config:
sway
bindsym $mod+Shift+l exec ~/bin/rotate90.sh
This way, both the display and touchscreen stay in sync.
Summary
Screen output rotation: Your manual keybindings rotate the display.
Touchscreen calibration: Must be updated separately using an input block (or a script) with the appropriate calibration matrix.
Identify device: Use swaymsg -t get_inputs to find your touchscreen’s identifier.
Matrix adjustments: Use the correct 3×3 matrix according to the desired rotation.
This approach should ensure that when you rotate your screen, both the display output and the touchscreen input behave as expected.
User
please write commands for the config, so that it updates on the fly when pressing shortcuts
Assistant
Below is an example of how you can update both the screen orientation and the touchscreen calibration on the fly via key bindings in your Sway config. Replace <TOUCHSCREEN_DEVICE> with your touchscreen’s identifier (you can find it using swaymsg -t get_inputs).
For example, add the following lines to your ~/.config/sway/config:
Output transformation:
Each binding sends a swaymsg output "*" transform <angle> command to rotate the display.
Touchscreen calibration:
The calibration_matrix commands update the touchscreen coordinate mapping. The provided matrices are:
0° (Normal):
code
[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
90° (Rotated left):
code
[ 0 -1 1 ]
[ 1 0 0 ]
[ 0 0 1 ]
270° (Rotated right):
code
[ 0 1 0 ]
[ -1 0 1 ]
[ 0 0 1 ]
180° (Inverted):
code
[ -1 0 1 ]
[ 0 -1 1 ]
[ 0 0 1 ]
When you press the corresponding key combinations (for example, $mod+Shift+l to rotate left), Sway first rotates the output and then applies the matching touchscreen calibration.
Feel free to adjust the key combinations or matrices if your display geometry requires slight modifications. This setup lets you update both the display and touchscreen settings on the fly without needing any additional scripts or services.