UPDATE: See A simple libCEC4 example for the Raspberry Pi for more up to date information.
After getting the libcec test client (cec-client) to run my thoughts turned to incorporating CEC control into my own code. The source code for cec-client is 1145 lines long, according to sloccount, which takes a bit of time to get your head around. By tossing away almost all of cec-client’s features I have managed to create a 75 sloc example which is much easier to understand. The source code for this really short example can be found on github .
The basic idea of the program is
- Initialise libcec
- Give libcec a callback that will handle the CEC keypresses
- Go to sleep until ctrl-c finishes the program
Any keys pressed on your remote control then wake up the callback function which I creatively called on_keypress.