Touch Screen and Tablet on x220 Tablet and FreeBSD

My main laptop is a Lenovo x220 Tablet with an an awesome swivel screen. The screen on the laptop is a touch screen and wacom tablet which uses a pen that hides in the side of the laptop.

I had quite a bit of trouble getting this all setup. Wacom touch and pen devices are supported by webcamd in FreeBSD. I set up webcamd as documented elsewhere on the internet and while I could see webcamd grabbing the input devices the touch screen or pen didn't work at all under X.

Eventually I figured out the problem was xorg not detecting the hid device nodes. To solve this I had to manually create an xorg.conf and the following sections.

Section "ServerLayout"
    Identifier     "X.org Configured"
    Screen      0  "Screen0" 0 0
    Screen      1  "Screen1" RightOf "Screen0"
    InputDevice    "Mouse0" "CorePointer"
    InputDevice    "Keyboard0" "CoreKeyboard"
    InputDevice    "stylus" "SendCoreEvents"
    InputDevice    "touch" "SendCoreEvents" 
EndSection

...

Section "InputDevice"
    Driver        "wacom"
    Identifier    "stylus"
    Option        "Device"       "/dev/input/event0"
    Option        "Type"         "stylus"
    Option        "USB"          "on"                 # USB ONLY
    Option        "Mode"         "Absolute"           # other option: "Absolute"
    Option        "Vendor"       "WACOM"
    Option        "tilt"         "off"  # add this if your tablet supports tilt
    Option        "Threshold"    "5"   # the official linuxwacom howto advises this line
EndSection

Section "InputDevice"
    Driver        "wacom"
    Identifier    "touch"
    Option        "Device"       "/dev/input/event1"
    Option        "Type"         "touch"
    Option        "USB"          "on"                  # USB ONLY
    Option        "Mode"         "Absolute"            # other option: "Absolute"
    Option        "Vendor"       "WACOM"
    Option        "tilt"         "off"  # add this if your tablet supports tilt
    Option        "Threshold"    "5"   # the official linuxwacom howto advises this line
EndSection

With the now xorg.conf dropped into /etc I could now restart the server and boom, touch screen and tablet working quite well.

When I sniveled the screen I wanted to to be able to rotate my display and input devices to the correct orientation. I wrote a little shell script that can either advance the screen rotation by 90 degrees or set it back to the default orientation.

#!/bin/sh

output=LVDS1
rotation="normal";

stylus="stylus"
touch="touch"

if [ "normal" == "$1" ]; then
    rotation="left";
else
    rotation=`xrandr --query --verbose | grep $output | awk '{print $5}'`
fi

case $rotation in
    normal)
        xrandr --output $output --rotation right
        xsetwacom --set "$stylus" Rotate cw
        xsetwacom --set "$touch"  Rotate cw
    ;;
    right)
        xrandr --output $output --rotation inverted
        xsetwacom --set "$stylus" Rotate half
        xsetwacom --set "$touch"  Rotate half
    ;;
    inverted)
        xrandr --output $output --rotation left
        xsetwacom --set "$stylus" Rotate ccw
        xsetwacom --set "$touch"  Rotate ccw
    ;;
    left)
        xrandr --output $output --rotation normal
        xsetwacom --set "$stylus" Rotate none
        xsetwacom --set "$touch"  Rotate none
    ;;
esac

I bound the script in my .i3/config to the two screen rotation buttons on the fron of the bezzel. I found the keycodes by using xev.

bindcode 198 exec rotate normal
bindcode 204 exec rotate

Overall the touch screen and tablet work quite well. When webcamd starts it doesn't always detect both the touch screen and tablet and sometimes in places them at different event points. If I could figure out a way to make these predictable or if a later xorg detects the input devices correctly then this setup would be perfect.