Touchscreen Calibration Fix Raspberry Pi XBMC Kodi

Touchscreen Calibration fix XBMC Raspberry Pi Hello!

I've spent a lot of time lately to get my Adafruit touchscreen to work with XBMC Kodi. Whatever I tried, the mouse pointer calibration was off.
When my finger was in the upper-left corner, the pointer was right, but as soon as I moved my finger away from the corner the pointer the pointer moves faster over the screen and disappears.
Luckily I've now found a solution for this problem, it involves modifying the XBMC source code and compiling XBMC on your Raspberry Pi so it takes a while.
On my Raspberry Pi 2 it takes about 6 hours. Luckily the 'make' takes 4 hours so you don't need to be watching your Pi for a straight 6 hours. Just check once in a while for errors.
Okay, so let's get started! If you have any questions, please ask me in the comments below.
  1. Write Raspbian to SD-card
    Start with a fresh instal of Raspbian Wheezy.
    Download img from here: https://www.raspberrypi.org/downloads/ and write it to your SD-card.

  2. Download neccesary files
    Download XBMC Helix 14.0 Source code from here:
    http://mirrors.kodi.tv/releases/source/
    Extract and put it in home/pi/ directory.

    Download libcec 2.2.0 from here:
    https://github.com/Pulse-Eight/libcec/releases/tag/libcec-2.2.0
    Put it in home/pi directory

    Download patches for source files from here: https://goo.gl/yw7RR9
    Extract and put folder in /home/pi/

    Download calibration plugin zip file from here: https://goo.gl/KGEX8f
    Do not extract but place the zip file in /home/pi

  3. Set minimum amount of video memory and create a swap partition:
    Turn on your raspberry pi and use:
    
    sudo raspi-config
        
    Here you should expand_rootfs, disable overscan, configure_keyboard, change_pass, change_timezone and enable ssh. Select memory_split and enter 16 then restart.
    Log in via SSH (not neccesary but a lot easier)

    Now, to create a swap partition, use the following:
    
    dd if=/dev/zero of=/home/pi/swapfile1 bs=1024 count=204800
    sudo mkswap /home/pi/swapfile1
    sudo chown root:root /home/pi/swapfile1
    sudo chmod 0600 /home/pi/swapfile1
    sudo swapon /home/pi/swapfile1
        
    The swap file is needed as extra memory for the compiler. This will prevent you from getting errors like:
    gcc: internal compiler error: Killed (program cc1)
  4. Patch source files
    In order to calibrate our touchscreen later we need to patch the source files of XBMC:
    
    cd pathches/
    patch -p1 ../xbmc-14.0-Helix/xbmc/input/linux/LinuxInputDevices.cpp < LinuxInputDevices_cpp.patch
    patch -p1 ../xbmc-14.0-Helix/xbmc/input/MouseStat.cpp < MouseStat_cpp.patch
    patch -p1 ../xbmc-14.0-Helix/xbmc/input/MouseStat.h < MouseStat_h.patch
    patch -p1 ../xbmc-14.0-Helix/xbmc/interfaces/legacy/ModuleXbmcgui.cpp < ModuleXbmcgui_cpp.patch
    patch -p1 ../xbmc-14.0-Helix/xbmc/interfaces/legacy/ModuleXbmcgui.h < ModuleXbmcgui_h.patch
    patch -p1 ../xbmc-14.0-Helix/xbmc/visualizations/OpenGLSpectrum/opengl_spectrum.cpp < opengl_spectrum_cpp.patch
    patch -p1 ../xbmc-14.0-Helix/xbmc/rendering/gles/RenderSystemGLES.cpp < RenderSystemGLES_cpp.patch
        
  5. Update the system and install some dependencies

    
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install autotools-dev libshairport-dev comerr-dev dpkg-dev libalsaplayer-dev libapt-pkg-dev:armhf libasound2-dev libass-dev:armhf libatk1.0-dev libavahi-client-dev libavahi-common-dev libavcodec-dev libavformat-dev libavutil-dev libbison-dev:armhf libbluray-dev:armhf libboost1.49-dev \
      libbz2-dev:armhf libc-dev-bin libc6-dev:armhf libcaca-dev libcairo2-dev libcdio-dev libclalsadrv-dev libcrypto++-dev libcups2-dev libcurl3-gnutls-dev \
      libdbus-1-dev libdbus-glib-1-dev libdirectfb-dev libdrm-dev libegl1-mesa-dev libelf-dev libenca-dev libept-dev libevent-dev libexpat1-dev libflac-dev:armhf \
      libfontconfig1-dev libfreetype6-dev libfribidi-dev libgconf2-dev libgcrypt11-dev libgdk-pixbuf2.0-dev libgl1-mesa-dev libgles2-mesa-dev \
      libglew-dev:armhf libglewmx-dev:armhf libglib2.0-dev libglu1-mesa-dev libgnome-keyring-dev libgnutls-dev libgpg-error-dev libgtk2.0-dev libhal-dev \
      libhunspell-dev:armhf libice-dev:armhf libicu-dev libidn11-dev libiso9660-dev libjasper-dev libjbig-dev:armhf libjconv-dev libjpeg8-dev:armhf libkrb5-dev \
      libldap2-dev:armhf libltdl-dev:armhf liblzo2-dev libmad0-dev libmicrohttpd-dev libmodplug-dev libmp3lame-dev:armhf libmpeg2-4-dev libmysqlclient-dev \
      libncurses5-dev libnspr4-dev libnss3-dev libogg-dev:armhf libopenal-dev:armhf libp11-kit-dev libpam0g-dev:armhf libpango1.0-dev libpcre++-dev libpcre3-dev \
      libpixman-1-dev libpng12-dev libprotobuf-dev libpthread-stubs0-dev:armhf libpulse-dev:armhf librtmp-dev libsamplerate0-dev:armhf \
      libsdl-image1.2-dev:armhf libsdl1.2-dev libslang2-dev:armhf libsm-dev:armhf libsmbclient-dev:armhf libspeex-dev:armhf \
      libsqlite3-dev libssh-dev libssh2-1-dev libssl-dev libstdc++6-4.6-dev libtagcoll2-dev libtasn1-3-dev libtiff4-dev libtinfo-dev:armhf libtinyxml-dev \
      libts-dev:armhf libudev-dev libv8-dev libva-dev:armhf libvdpau-dev:armhf libvorbis-dev:armhf libvpx-dev:armhf libwebp-dev:armhf libwibble-dev \
      libx11-dev:armhf libx11-xcb-dev libxapian-dev libxau-dev:armhf libxcb-glx0-dev:armhf libxcb-render0-dev:armhf libxcb-shm0-dev:armhf \
      libxcb1-dev:armhf libxcomposite-dev libxcursor-dev:armhf libxdamage-dev libxdmcp-dev:armhf libxext-dev:armhf libxfixes-dev libxft-dev libxi-dev \
      libxinerama-dev:armhf libxml2-dev:armhf libxmu-dev:armhf libxrandr-dev libxrender-dev:armhf libxslt1-dev libxss-dev:armhf libxt-dev:armhf \
      libxtst-dev:armhf libxxf86vm-dev libyajl-dev libzip-dev linux-libc-dev:armhf lzma-dev mesa-common-dev python-dev python2.7-dev x11proto-composite-dev \
      x11proto-core-dev x11proto-damage-dev x11proto-dri2-dev x11proto-fixes-dev x11proto-gl-dev x11proto-input-dev x11proto-kb-dev x11proto-randr-dev \
      x11proto-record-dev x11proto-render-dev x11proto-scrnsaver-dev x11proto-xext-dev x11proto-xf86vidmode-dev x11proto-xinerama-dev xtrans-dev \
      libnfs-dev libplist-dev avahi-daemon zlib1g-dev:armhf swig java-package libafpclient-dev liblockdev1-dev autoconf automake libtool gcc udev openjdk-6-jre \
      cmake g++ libudev-dev build-essential autoconf ccache gawk gperf mesa-utils zip unzip curl
        
    This will take some time depending on your internet speed
  6. Copy libraries headers and create some symlinks for libraries:
    
    sudo cp -R /opt/vc/include/* /usr/include
    sudo cp /opt/vc/include/interface/vcos/pthreads/* /usr/include/interface/vcos
    sudo ln -fs /opt/vc/lib/libEGL.so /usr/lib/libEGL.so
    sudo ln -fs /opt/vc/lib/libEGL.so /usr/lib/arm-linux-gnueabihf/libEGL.so
    sudo ln -fs /opt/vc/lib/libEGL.so /usr/lib/arm-linux-gnueabihf/libEGL.so.1
    sudo ln -fs /opt/vc/lib/libEGL_static.a /usr/lib/libEGL_static.a
    sudo ln -fs /opt/vc/lib/libEGL_static.a /usr/lib/arm-linux-gnueabihf/libEGL_static.a
    sudo ln -fs /opt/vc/lib/libGLESv2.so /usr/lib/libGLESv2.so
    sudo ln -fs /opt/vc/lib/libGLESv2.so /usr/lib/arm-linux-gnueabihf/libGLESv2.so
    sudo ln -fs /opt/vc/lib/libGLESv2.so /usr/lib/arm-linux-gnueabihf/libGLESv2.so.2
    sudo ln -fs /opt/vc/lib/libGLESv2_static.a /usr/lib/libGLESv2_static.a
    sudo ln -fs /opt/vc/lib/libGLESv2_static.a /usr/lib/arm-linux-gnueabihf/libGLESv2_static.a
    sudo ln -fs /opt/vc/lib/libbcm_host.so /usr/lib/libbcm_host.so
    sudo ln -fs /opt/vc/lib/libbcm_host.so /usr/lib/arm-linux-gnueabihf/libbcm_host.so
    sudo ln -fs /opt/vc/lib/libvchiq_arm.a /usr/lib/libvchiq_arm.a
    sudo ln -fs /opt/vc/lib/libvchiq_arm.a /usr/lib/arm-linux-gnueabihf/libvchiq_arm.a
    sudo ln -fs /opt/vc/lib/libvchiq_arm.so /usr/lib/libvchiq_arm.so
    sudo ln -fs /opt/vc/lib/libvchiq_arm.so /usr/lib/arm-linux-gnueabihf/libvchiq_arm.so
    sudo ln -fs /opt/vc/lib/libvcos.a /usr/lib/libvcos.a
    sudo ln -fs /opt/vc/lib/libvcos.a /usr/lib/arm-linux-gnueabihf/libvcos.a
    sudo ln -fs /opt/vc/lib/libvcos.so /usr/lib/libvcos.so
    sudo ln -fs /opt/vc/lib/libvcos.so /usr/lib/arm-linux-gnueabihf/libvcos.so
    sudo ln -fs /opt/vc/lib/libmmal.so /usr/lib/libmmal.so
    sudo ln -fs /opt/vc/lib/libmmal_core.so /usr/lib/libmmal_core.so
    sudo ln -fs /opt/vc/lib/libmmal_util.so /usr/lib/libmmal_util.so
        
    There is a problem when compiling, with the file /usr/include/interface/vmcs_host/vcgencmd.h which includes the wrong vchost_config.h, so I have created a command to put the right inclusion:
    
    sudo sed -i 's/#include "vchost_config.h"/#include "linux\/vchost_config.h"/' /usr/include/interface/vmcs_host/vcgencmd.h 
        
  7. Install taglib and libcec
    
    cd <path_to_xbmc>
    make -C lib/taglib
    sudo make -C lib/taglib install
    
    cd <path to libcec>
    chmod a+x -R .
    ./bootstrap
    ./configure --prefix=/usr/local
    make
    sudo make install
        
  8. Configure and compile XBMC
    
    cd <path_to_xbmc_dir>
    sudo chmod a+x -R .
    export TARGET_SUBARCH="armv6zk"
    export TARGET_CPU="arm1176jzf-s"
    export TARGET_FLOAT="hard"
    export TARGET_FPU="vfp"
    export TARGET_FPU_FLAGS="-mfloat-abi=$TARGET_FLOAT -mfpu=$TARGET_FPU"
    export TARGET_EXTRA_FLAGS="-Wno-psabi -Wa,-mno-warn-deprecated"
    export TARGET_COPT="-Wall -pipe -fomit-frame-pointer -O3 -fexcess-precision=fast -ffast-math  -fgnu89-inline"
    export TARGET_LOPT="-s -Wl,--as-needed"
    export CFLAGS="-march=$TARGET_SUBARCH -mcpu=$TARGET_CPU $TARGET_FPU_FLAGS -mabi=aapcs-linux $TARGET_COPT $TARGET_EXTRA_FLAGS"
    export CXXFLAGS="$CFLAGS"
    export LDFLAGS="-march=$TARGET_SUBARCH -mtune=$TARGET_CPU $TARGET_LOPT"
        
    Fix some errors:
    
    sed -i 's/USE_BUILDROOT=1/USE_BUILDROOT=0/' tools/rbp/setup-sdk.sh
    sed -i 's/TOOLCHAIN=\/usr\/local\/bcm-gcc/TOOLCHAIN=\/usr/' tools/rbp/setup-sdk.sh
        
    Run:
    
    sudo sh tools/rbp/setup-sdk.sh
        
    Fix other errors:
    
    sed -i 's/cd $(SOURCE); $(CONFIGURE)/#cd $(SOURCE); $(CONFIGURE)/' tools/rbp/depends/xbmc/Makefile
        
    Fix more errors:
    
    cd tools/depends/native/JsonSchemaBuilder/src
    bash autogen.sh
    ./configure
    make
    sudo make install
        
    Run:
    
    cd <path_to_xbmc_dir>
    make -C tools/rbp/depends/xbmc/ 2>&1 | tee log_1_rbp_depends.log
        
    Configure:
         
    ./configure --prefix=/usr/local --build=arm-linux-gnueabihf \
        --host=arm-linux-gnueabihf --localstatedir=/var/lib \
        --with-platform=raspberry-pi --disable-gl --enable-gles \
        --disable-x11 --disable-sdl --enable-ccache --enable-optimizations \
        --disable-external-libraries --disable-goom --disable-hal \
        --disable-pulse --disable-vaapi --disable-vdpau --disable-xrandr \
        --enable-airplay --disable-alsa --enable-avahi --enable-libbluray \
        --enable-dvdcss --disable-debug --disable-joystick --disable-mid \
        --enable-nfs --disable-profiling --disable-projectm --enable-rsxs \
        --enable-rtmp --disable-vaapi --disable-vdadecoder \
        --disable-external-ffmpeg --enable-optical-drive \
        --enable-player=omxplayer 2>&1 | tee log_2_configure.log
        
        
    After configuration completes, please run he following command:
    
    sed -i 's/ifeq (1,1)/ifeq (0,1)/' tools/TexturePacker/Makefile
        
    Compile(this will take about 9 hours depending on your RPi model):
    
    make
        
  9. Install XBMC 14 in Raspbian
    
    sudo make install
        
    After this step you have to run raspi-config again and to set video memory to 128 and then restart. Now you should be able to run XBMC using:

    /usr/local/lib/kodi/kodi.bin
  10. Calibrate touchscreen
    Now you should be able to launch XBMC and using a keyboard navigate to 'Programs'.
    Select 'install add-ons from zip file' and then select the file 'plugin.program.touchCalibration.zip' from the /home/pi folder.
    When it's installed select the plugin and follow the steps on the screen.

    When you are finished with the calibration your touchscreen should be calibrated but in my case the pointer was still a little bit off. I fixed this with a little trial and error:
    Open the file touchscreen_axes_calib:
    
    nano touchscreen_axes_calib
        
    In this file you will find seven variables but we will only use these four: calib_x_d, calib_x_fact, calib_y_d and calib_y_fact.
    If your mouse pointer is correct in the left-upper corner you can set calib_x_d and calib_y_d to zero.
    If your mouse pointer moves to fast in the x direction (horizontal direction) you should decrease the value of calib_x_fact. The same goes for the y direction (vertical direction), then you should decrease the value of calib_y_fact.
    Now restart your Pi and check if the calibration is better. Repeat these steps untill the calibration is perfect.

    You can download my touchscreen_axes_calib file for reference from here: https://goo.gl/Mf9TH4
So this is it. After these steps I finally had a well calibrated touchscreen. If you have any questions please ask them and I will do my very best to help you!
Many thanks to this blogger from where I got the idea: http://www.engineering-diy.blogspot.ro/

31 comments:

  1. Would you be able to share the image?

    ReplyDelete
    Replies
    1. Hello,
      Sorry for my late answer, unfortunately I don't have an image for this solution.
      If you have any questions please ask.

      Delete
  2. Replies
    1. Hey Jared,
      Thanks for your reply. I don't have a guide for the newest version of Kodi. I suggest you try to use this guide for the newest version as I think it will still work.
      Good luck!

      Delete

  3. hi,
    why in the configuration of xbmc have disabled ALSA and PULSE ? I use that a hifiberry - digi I enable them ?

    ReplyDelete
    Replies
    1. Hey Mauro, thanks for your reply. You can enable those options, I disabled them only to save space.
      Good luck!

      Delete
  4. I'm trying to build a portable touchscreen raspberry pi with OpenELEC, and I've run into nothing but dead ends. I would really like to know if this method (or an updated version of it) will solve the problem. Thanks in advance!

    ReplyDelete
    Replies
    1. Hello,
      Thanks for your reply, this wil work if you want to calibrate your touchscreen with the kodi sourcecode found on http://mirrors.kodi.tv/releases/source/.
      I don't know exactly what your problem is so I can not say for sure if it will help you, maybe you can explain what the problem you are experiencing is?

      Delete
    2. My problem is exactly the one described at the beginning of this tutorial. I have a 5" Adafruit touchscreen, and a small portion of the upper left corner of the screen seems to represent the entirety of the actual screen. The touch area is very disproportionate to the actual screen size. I'm very very new to anything Linux, and still trying to figure out how to do basically anything beyond installation, so the more "Barney-style" you can make it for me, the better.

      Delete
    3. Then I think this is the guide to fix your problem, if you just follow everything step by step you can't do anything wrong. If you, however, get stuck somewhere please let me know. I will be glad to help you!

      Delete
    4. Then I think this is the guide to fix your problem, if you just follow everything step by step you can't do anything wrong. If you, however, get stuck somewhere please let me know. I will be glad to help you!

      Delete
    5. This comment has been removed by the author.

      Delete
  5. READERS!! Finally after trying Different Methods and Different Tutorials You Must Have been tired by now like me. HERE IS THE SOLUTION and it will definitely work.

    FOR XBIAN: http://markamc.traki-iski.co.uk/raspberry-pixbian-egalax-touchscreen/

    FOR OSMC: http://markamc.traki-iski.co.uk/raspberry-pi-2-osmc-egalax-touchscreen/

    FOR RASPBMC: http://markamc.traki-iski.co.uk/raspberry-pi-raspbmc-egalax-touchscreen/

    CREDIT GOES TO:

    UPDATE: uimapper – calibration works if different resolution selected in KODI, after re-plug/restart.
    Thanks to ‘NIce Guy’ for figuring I’d messed up the tarball!! (now fixed…hopefully

    ReplyDelete
  6. I am using the new official 7" touchscreen built for the Pi that uses the SPI port. I have tried some of the wheezy builds, but the new touchscreen is apparently only supported in jessie. My issue is that I am using KODI 15.2-2 and when I launch the touch screen calibrator, I get the target in the upper left with the message "Touch the top left point and then press ENTER". However, when I touch the target point and hit ENTER on my keyboard, nothing happens. My main issue with all of this is that I cannot get KODI to work on a single touch. I am working with the CarPC touch screen skin and I am having some of the same trouble I have been seeing in a lot of other forums where people say a touch only highlights an item. I need to either tap multiple times on an item, or touch once to highlight and then press the OK button on my keyboard. This is my main hang-up before installing this in my car.

    Any help is appreciated

    ReplyDelete
    Replies
    1. When I did it, I had to turn my screen upside down due to display issues with it. After following this tutorial, I ended up having to just do trial and error and manually adjust the numbers. Very tedious start, stop, adjust... Start, stop, adjust. Touching the points on the screen just messed up the numbers for me.

      Delete
  7. I have downloaded a sample file and put it in /home/pi/touchscreen_axes_calib I am not sure if that is the correct place to have it. I would think it should be somewhere under either the /hom/pi/.kodi/ or /usr/share/kodi folders, but I am not sure. Reading a forum post from one site showed the file for the eGalax touch screen in /usr/share/eGalaxCalibration/touchscreen_axes_calib. I don't see any difference in the operation within kodi with or without the file in the /home/pi/folder. Is there another folder it should be in when using the official 7" screen?

    ReplyDelete
  8. when i go to step 4 i get this message (-bash: cd: pathches/: No such file or directory) what em i doing wrong

    ReplyDelete
    Replies
    1. Have you done this:
      Download patches for source files from here: https://goo.gl/yw7RR9
      Extract and put folder in /home/pi/

      Delete
    2. Have you done this:
      Download patches for source files from here: https://goo.gl/yw7RR9
      Extract and put folder in /home/pi/

      Delete
    3. yes i think my something was corrupted ill reinstall raspbian and retry thank you though

      Delete
  9. Having problems with Hunk #1 failing at 274 while applying patch. What am I doing wrong?

    ReplyDelete
    Replies
    1. i'm having problems with patching to but when i try running the command patch -p1 ../xbmc-14.0-Helix/xbmc/input/linux/LinuxInputDevices.cpp it just runs on for 11 hours before i just closed the terminal

      Delete
  10. This comment has been removed by the author.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Hello
    In Point No.5 (Update the system and install some dependencies) I am getting error message while installing below packages, is it ok if i skip these packages?
    sudo apt-get install ibdbus-1-dev libgnutls-dev libsmbclient-dev:armhf libtasn1-3-dev libtiff4-dev openjdk-6-jre

    Regards,
    Neeraj

    ReplyDelete
  13. when I run the third command in step 5, I get this:

    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Note, selecting 'libcurl4-gnutls-dev' instead of 'libcurl3-gnutls-dev'
    Package openjdk-6-jre is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
    icedtea-netx-common icedtea-netx

    Package libgnutls-dev is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    Package libtasn1-3-dev is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
    libtasn1-doc

    Package libtiff4-dev is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
    libtiff5-dev

    E: Package 'libgnutls-dev' has no installation candidate
    E: Package 'libtasn1-3-dev' has no installation candidate
    E: Package 'libtiff4-dev' has no installation candidate
    E: Package 'openjdk-6-jre' has no installation candidate

    Any tips?

    I'm running raspbian jessie because that's all I could find anymore.

    I tried it with 16.0 Jarvis and had issues with the first patch. Now I'm doing it with 14.0 Helix

    ReplyDelete
  14. hello and from me this is nc and very good guide but i have one problem i try to use this lcd
    http://www.waveshare.com/wiki/5inch_HDMI_LCD#Getting_Started
    i dowload the plugin for touch but nothing can you help to find how to calibrate this touch?? thanks againe :D

    ReplyDelete
  15. None of the patching appears to work.

    I have the patches in exactly the places you've stated here, and xbmc extracted to /home/pi (I've also tried this with xbmc-17.0-Krypton, updating the patch lines as instructed, but none of the patches work. Below is the behavior I get every time:

    pi@raspberrypi:~/patches $ patch ./xbmc-14.0-Helix/xbmc/input/linux/LinuxInputDevices.cpp < LinuxInputDevices_cpp.patch
    patching file ./xbmc-14.0-Helix/xbmc/input/linux/LinuxInputDevices.cpp
    Hunk #1 FAILED at 274.
    Hunk #2 FAILED at 297.
    Hunk #3 FAILED at 455.
    Hunk #4 FAILED at 602.
    patch: **** Can't reopen file ./xbmc-14.0-Helix/xbmc/input/linux/LinuxInputDevices.cpp : No such file or directory

    Trying to copy and paste your lines in this doc (from cd patches, btw, typo up there) I just get:

    pi@raspberrypi:~/patches $ patch ../xbmc-14.0-Helix/xbmc/input/linux/LinuxInputDevices.cpp < LinuxInputDevices_cpp.patch
    Invalid file name ../xbmc-14.0-Helix/xbmc/input/linux/LinuxInputDevices.cpp -- skipping patch

    Any advice here?

    ReplyDelete