RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    SupaCEC - A utility to switch TV inputs when a controller is connected

    Scheduled Pinned Locked Moved Ideas and Development
    cecpackagespackagehdmi cechdmi
    29 Posts 8 Posters 6.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      superterran
      last edited by

      I couldn't find anything enabling this, so I wrote something that gets it working. If you want to play around with it, check it out at:

      https://github.com/superterran/SupaCEC

      This is a simple bash script, along with an init.d control script and an installer in the form of a RetroPie-Setup script, that will allows for the system to automatically switch inputs when it detects a input device added to the system. When detected, it will trigger the active source to pause and will steal the active source for itself. The script is very simple, and should be considered a stub for a more robust solution.

      1 Reply Last reply Reply Quote 1
      • R
        robertybob
        last edited by

        Could you ELI5 what this is for? I may have a use for it but want to be sure before installing anything on to my Pi :)

        1 Reply Last reply Reply Quote 0
        • HexH
          Hex
          last edited by

          @superterran What is the use for this? Of what I know retropie already switches on my TV and sets source to itself while booting up

          Sent from 20,000 leagues under the sea.

          Powersaver Emulation station : https://github.com/hex007/EmulationStation
          ES dev script : https://github.com/hex007/es-dev/blob/master/es-tests.sh

          SanoS 1 Reply Last reply Reply Quote 0
          • SanoS
            Sano @Hex
            last edited by

            @Hex
            Precisely, only when booting up (or launching kodi).
            Given the power consumption of the RPi, I suppose many people just let it powered on 24/7, and I'm one of them :)
            So I have to switch the amp input everytime I want to play, so this script could be really handy for people like me. I'll take a look.
            Thx @superterran

            1 Reply Last reply Reply Quote 0
            • SanoS
              Sano
              last edited by

              Ok, took a look, some interesting hints on CEC management.
              But the daemon mode with an infinite loop is bothering me.
              I'm currently making some tests to generate an udev rule that execute the right cec commands when an new input device is detected.
              It should be cleaner :)

              1 Reply Last reply Reply Quote 1
              • S
                superterran
                last edited by

                @Sano Very nice, I didn't really like my approach either but didn't want to go down the road of installing inotify-tools and this way seemed good enough for the purposes of a proof of concept.

                @robertybob so what this does is scan for new input devices like joysticks and keyboards to be detected by the system, and it will trigger a CEC command that will direct your TV to switch inputs to the RetroPie. The usecase here is if your sitting on your couch, your TV is off or on another input, you can interact with the controller and the TV will either change inputs or turn on the TV. If you were watching a movie on your Chromecast, it will pause the movie first.

                1 Reply Last reply Reply Quote 0
                • G
                  godhunter74
                  last edited by

                  Hi very Nice job
                  It will be interesting to add some feature like :

                  • turn off tv when ES is sleeping instead off screensaver
                  • Turn on TV when ES Wake up
                  • display name of rom playing in cec name osd
                    Maybe some others tools il you have more idea
                  1 Reply Last reply Reply Quote 1
                  • S
                    superterran
                    last edited by

                    I'm interested to see if @Sano made any progress, I might explore his route too as I know the loop is no good. In terms of @godhunter74's feedback I really like the notion of firing this when ES is waking up, I'll explore that a bit - maybe that's a better idea than listening? Personally, I'd like to add a controller hotkey to modify the TV volume, this would be huge in my household and would improve my whole entertainment center.

                    1 Reply Last reply Reply Quote 1
                    • SanoS
                      Sano
                      last edited by Sano

                      Ah yes sorry, didn't had much time the few past days.
                      The udev rule was quite simple :
                      ACTION=="add", SUBSYSTEM=="input", RUN+="echo 'as' | cec-client -s -d 1"

                      Unfortunately, the RUN primitive of udev rules seems not to apply since systemd.
                      I wasted an entire evening to understand that by testing :)
                      Then I found this thread :
                      https://retropie.org.uk/forum/topic/3335/anyone-familiar-with-udev-rules

                      It should be possible to circumvent this via the use of a systemd daemon script.

                      Maybe @mediamogul can help us on this one, as he faced the same issu ?

                      mediamogulM 1 Reply Last reply Reply Quote 0
                      • P
                        psyke83 Global Moderator
                        last edited by

                        @Sano said in SupaCEC - A utility to switch TV inputs when a controller is connected:

                        ACTION=="add", SUBSYSTEM=="input", RUN+="echo 'as' | cec-client -s -d 1"

                        No, I think the problem is that udev doesn't parse pipe or redirects as it doesn't run a shell but passes arguments literally. If you use "/bin/sh -c 'commands'" it should process pipes and redirections correctly.

                        1 Reply Last reply Reply Quote 0
                        • SanoS
                          Sano
                          last edited by

                          Thanks, but I also tried to call a simple shell script with the command line and, it was not working either...
                          So seeing @mediamogul post I thought udev/systemd was indeed the problem...

                          1 Reply Last reply Reply Quote 0
                          • P
                            psyke83 Global Moderator
                            last edited by

                            Yes, it's not working, but I did try: "/usr/bin/touch ~/test", and it worked, so the RUN argument is not fully deprecated.

                            There are existing udev scripts in retropie that run commands directly, and I even added my own to control disk spindown via smartctl, so it does work... I'm betting the problem is the pipe.

                            SanoS 1 Reply Last reply Reply Quote 0
                            • SanoS
                              Sano @psyke83
                              last edited by

                              @psyke83
                              Roger that, but why my call to a (working) sh script didn't work ?
                              And why does a simple touch works ? Strange AF...

                              Plus there are a lot of this kind of docs highlighting the need of a systemd script in order to launch anything... https://fixmynix.com/run-shell-scripts-from-udev-rules/

                              1 Reply Last reply Reply Quote 0
                              • P
                                psyke83 Global Moderator
                                last edited by

                                I got it to work like so:

                                ACTION=="add", SUBSYSTEM=="input", RUN+="sh -c /home/pi/waketv.sh"

                                waketv.sh:
                                #!/bin/sh
                                /bin/sh -c 'echo 'as' | cec-client -s -d 1'

                                I'll see if there's a way to make the pipe work without relying on a script.

                                SanoS 1 Reply Last reply Reply Quote 2
                                • SanoS
                                  Sano @psyke83
                                  last edited by

                                  @psyke83
                                  Congrats !
                                  That said, even with your syntax I can't make it work on my setup :(
                                  Maybe something is messed up on my RPi...

                                  P 1 Reply Last reply Reply Quote 0
                                  • mediamogulM
                                    mediamogul Global Moderator @Sano
                                    last edited by mediamogul

                                    @Sano

                                    Maybe @mediamogul can help us on this one, as he faced the same issu ?

                                    Sorry, I'm just now seeing this. Unfortunately, I was never able to get further than what was posted in that original thread. I'm glad it's being revisited and that there might be a working option.

                                    RetroPie v4.5 • RPi3 Model B • 5.1V 2.5A PSU • 16GB SanDisk microSD • 512GB External Drive

                                    1 Reply Last reply Reply Quote 1
                                    • P
                                      psyke83 Global Moderator @Sano
                                      last edited by

                                      @Sano

                                      This should work:

                                      ACTION=="add", SUBSYSTEM=="input", RUN+="/bin/bash -c 'echo 'as' | cec-client -s -d 1'"

                                      I actually tested this (as someone else is using the tv):
                                      ACTION=="add", SUBSYSTEM=="input", RUN+="/bin/bash -c 'echo test | tee /dev/kmsg'"

                                      For some reason, "test" is echoed three times to the kernel log & I'm not certain precisely why...

                                      SanoS 1 Reply Last reply Reply Quote 1
                                      • SanoS
                                        Sano @psyke83
                                        last edited by Sano

                                        @psyke83 said in SupaCEC - A utility to switch TV inputs when a controller is connected:

                                        This should work:

                                        ACTION=="add", SUBSYSTEM=="input", RUN+="/bin/bash -c 'echo 'as' | cec-client -s -d 1'"

                                        Sorry, still no luck :(
                                        But thanks a lot for trying !

                                        For some reason, "test" is echoed three times to the kernel log & I'm not certain precisely why...

                                        That I know :)
                                        just run an sudo udevadm monitor -e before pluging anything
                                        It's just the kernel and udev adding the new device, link, driver,... (depends on the device)
                                        Easy to filter in the rules, if only the RUN primitive could work as intented...

                                        @mediamogul thanks for reading and answering anyway. Hope we'll find a clean solution. @psyke83 seems to be the man.

                                        P 1 Reply Last reply Reply Quote 1
                                        • P
                                          psyke83 Global Moderator @Sano
                                          last edited by psyke83

                                          @Sano said in SupaCEC - A utility to switch TV inputs when a controller is connected:

                                          ACTION=="add", SUBSYSTEM=="input", RUN+="/bin/bash -c 'echo 'as' | cec-client -s -d 1'"

                                          OK, I commandeered the TV to test and can confirm it didn't work ;). However, the pipe is working via the tee command, so the problem might be due to the cec-client being called three times in succession. Can you try refining the rule and testing? Also test "tee >/dev/kmsg" just to make sure there's no difference in configuration between our systems.

                                          Edit: also, try specifying the path of cec-client in the command just in case the PATH is not initialized properly.

                                          1 Reply Last reply Reply Quote 0
                                          • P
                                            psyke83 Global Moderator
                                            last edited by psyke83

                                            OK, this definitely works! ;)

                                            ACTION=="add", SUBSYSTEM=="input", RUN+="/bin/bash -c 'echo as | /usr/bin/cec-client -s -d 1'"

                                            The problem was either the nested apostrophes or missing PATH. I don't want to annoy the person watching the tv, so you'll need to test, but the above line worked for me. I assume it executed the command three times, however, so refining is a good idea.

                                            This filter works for my DS3 controller causing it to execute once:
                                            ACTION=="add", SUBSYSTEM=="input", KERNEL=="js[0-9]*", RUN+="/bin/bash -c 'echo as | /usr/bin/cec-client -s -d 1'"

                                            1 Reply Last reply Reply Quote 2
                                            • First post
                                              Last post

                                            Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

                                            Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.