Moblin Applets: Touchscreen Calibration

Snapshot

Overview

The touchscreen calibration utility is used to create an alignment mapping between the touchscreen's and the underlying LCD screen's coordinate system, which are usually very different. Touchscreen resolutions are usually in the shape of a box, for instance 1024x1024 or 4096x4096. Whereas the LCD resolutions are usually rectangular as 4:3 standard or 16:9 widescreen, for instance 1024x768 or 1024x600. We need to ensure that when a person touches the screen, the point that is registered by the touchscreen driver is mapped to the corresponding LCD screen point under their finger. This is accomplished through the use of a config file which stores the coordinates of the upper left-most and lower right-most points in the touchscreen that can be displayed on the LCD.

Files Needed

  • /usr/lib/moblin-applets/libhcpcommon.so
    • all control panel applets need this
  • /usr/lib/hildon-control-panel/touchscreen.so
    • The applet library
  • /usr/share/applications/hildon-control-panel/touchscreen.desktop
  • /usr/bin/moblin-touchscreen
    • The GUI user space executable
  • /usr/bin/touchdump
    • A simple debug utility for dumping out raw touchscreen driver data to stdout
  • /usr/share/icons/gnome/48x48/apps/touchscreen.png
    • The touchscreen desktop shortcut icon
  • /etc/init.d/moblin-system-daemon
    • Needed to carry out the touchscreen driver access and xorg file edits as root

Applet Design



System Daemon

This applet needs to read touchscreen data from the touchscreen driver and edit the xorg.conf file with a new mapping; both of these files require root access to read or write. In order to eliminate the need to have the user login as root, the handling of these two files has been separated into a root user daemon called "moblin-system-daemon" and is accessible via any user process that registers to use the org.moblin.SystemDaemon D-Bus service.

Moblin-system-daemon is started automatically as a service during bootup. The moblin-applets package sets this up by installing a startup script at /etc/init.d/moblin-system-daemon with update-rc.d. The service exposes two D-Bus calls for touchscreen calibration: CaptureTouch and ApplyCalibration. CaptureTouch is used to capture the coordinates of the next touchscreen touch and send the data back to the caller (asynchronously via the use of a callback). This function can be called as many times as are needed to establish a new mapping. The caller uses this data to generate five values:

  • Minimum X: The X value of the touchscreen point which maps to the left edge of the display.
  • Minimum Y: The Y value of the touchscreen point which maps to the top edge of the display.
  • Maximum X: The X value of the touchscreen point which maps to the right edge of the display.
  • Maximum Y: The Y value of the touchscreen point which maps to the bottom edge of the display.
  • Rotation: The rotation of the touchscreen relative to the display. 0 for normal, 1 for clockwise, and -1 for counter-clockwise.

Once the caller has established these values it then calls ApplyCalibration with them in order to update the xorg.conf file. The daemon edits /etc/X11/xorg.conf by default, but first it checks /etc/event.d/session to see if startx has been used and if the -config option was used. If that option was used the daemon uses the config file it points to. If the xorg.conf file does not have a touchscreen calibration section in it, it adds one as it applies the calibration as follows:

Section "InputDevice"
  Identifier "touchscreen"
  Driver "evtouch"
  Option "Device" "/dev/input/touchscreen"
  Option "SendCoreEvents"
  Option "ReportingMode" "Raw"
  Option "Emulate3Buttons"
  Option "Emulate3Timeout" "50"
  Option "MinX" "0"
  Option "MinY" "0"
  Option "MaxX" "4096"
  Option "MaxY" "4096"
  Option "TapTimer" "2"
  Option "LongTouchTimer" "4"
EndSection

Calibration GUI

The calibration GUI is contained within a separate executable called moblin-touchscreen. This is because the GUI displays some animation which requires the use of the SIGALARM and various other executable resources that may conflict with the inner workings of hildon-desktop. Hildon-desktop therefore spawns this process via g_spawn_async and monitors it so as to prohibit multiple instances, but that's the limit of hildon-desktop's responsibility.

The GUI is comprised of a black screen populated by four points, positioned at X coordinates of 1/16 and 15/16 of the screen width, and Y coordinates of 1/16 and 15/16 of the screen height. Moblin-touchscreen queries the screen resolution from the xserver but uses fractions so that it doesn't matter what that resolution is. It then calls CaptureTouch four times, once for each point, so that it can understand what touchscreen coordinates the user sees these four points at. It then does a little math to convert these four points into the four corners of the visible touchscreen (the points are drawn further out because it would be difficult for a user to touch the actual screen corners). The MinX value is taken as the average of the leftmost two points, the MinY as the average of the topmost two points, the MaxX from the rightmost points, and the MaxY from the bottommost points.

Code Reuse

This applet is based the CarPC touchscreen calibrator.