Using cec-client with a raspberry pi on debian or ubuntu does not work out of the box, as the cec-client in the debian and ubuntu repositories has not been compiled with support for the propriary raspberry libraries. In addition, these libraries are 32-bit only. This is a guide in how to get this working.
First you need to set up a 32-bit chroot environment. Install
debootstrap and then run
debootstrap --variant=buildd --arch=armhf bullseye /opt/cec-client/
You will then need to download the raspberry pi libraries. Unpack /opt/vc from the tarball into
/opt/cec-client (replace url with newer version if needed):
curl -L https://github.com/raspberrypi/firmware/archive/1.20210108.tar.gz | tar zxf -
cp -r firmware*/opt/ /opt/cec-client/
Add the raspberry pi lib to ldconfig by running
echo "/opt/vc/lib" > /opt/cec-client/etc/ld.so.conf.d/rpi.conf
You can now compile libcec and cec-utils:
apt-get install cmake libudev-dev libxrandr-dev python3-dev swig git
git clone https://github.com/Pulse-Eight/platform.git
git clone https://github.com/Pulse-Eight/libcec.git
cmake -DRPI_INCLUDE_DIR=/opt/vc/include -DRPI_LIB_DIR=/opt/vc/lib ..
In order to run cec-client inside the chroot, you will also need access to devices. You can do this by mounting this up before running chroot, but an easier and better way is to use systemd. Many of the examples you will find of how to use cec-client adds the -s flag to run a single command, but without this, cec-client runs until told to quit and listens on commands on stdin. We will use this and let systemd set up a fifo for commands for us. Create the socket and service files:
Description=CEC client socket
ExecStart=/usr/local/bin/cec-client -d 1
ExecStop=/bin/bash -c "echo q > /run/cec.fifo"
Then reload systemd, enable and start:
systemctl enable cec-client.socket
systemctl start cec-client.socket
You can now send cec-client commands by writing to /run/cec.fifo, for instance to turn on the tv with address 0.0.0.0 run
echo 'on 0.0.0.0' > /run/cec.fifo
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.