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

    shell scripting topic

    Scheduled Pinned Locked Moved Ideas and Development
    shellshell scriptprogramming
    191 Posts 10 Posters 76.5k 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.
    • hiulitH
      hiulit
      last edited by

      For the fun facts splashscreens project I want to know which theme is currently active in EmulationStation and its font.

      Here's what I'm doing:

      function get_current_theme() {
          grep "name=\"ThemeSet\"" "$home/.emulationstation/es_settings.cfg" | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p"
      }
      
      function get_theme_font() {
          xmlstarlet sel -t -v "/theme/view[contains(@name,'detailed')]/textlist/fontPath" "$ES_DIR/themes/$current_theme/$current_theme.xml" 2> /dev/null
      }
      

      It's working but I don't know if it's the right way to do it.

      Acutally, get_theme_font() is taken from @meleu 's generate-launching-imagesso I guess it should be ok :P

      My little contributions to the RetroPie project:

      • Shell-Script-Boilerplate
      • Fun-Facts-Splashscreens
      • Limit-Last-Played-Games
      meleuM 1 Reply Last reply Reply Quote 0
      • meleuM
        meleu @hiulit
        last edited by meleu

        @hiulit I think they are just fine. Maybe get_current_theme() can be tweaked a little to be an one-liner sed. But as this is a small script, it's fine to keep it as is.

        Another little thing I noticed is regarding that Regular Expression in the sed line. You don't need to explicitly match "start of the line followed by anything" (this is what ^.* means).

        Also, I think we can tweak get_theme_font() in order to let it get the font for an arbitrary theme (defined by the user).

        • Useful topics
        • joystick-selection tool
        • rpie-art tool
        • achievements I made
        1 Reply Last reply Reply Quote 1
        • cyperghostC
          cyperghost @hiulit
          last edited by cyperghost

          @hiulit said in shell scripting topic:

          I found that if I specify a font when using convert it decreases the image creation time, like a lot!
          It also depends on the sentence's length. But when specifying a font I get pretty good results with a 3 line sentence :O

          That was my first suggestion. But convert gots 2 caveeats

          1. It needs ghostscript (I'm unsure if caption needs it)
          2. You have to set correct position
          3. (It needs the font you call)

          ... but it's faster than just use captions because it's independent of the images you merge but it's your turn to set coordinates ;)

          @meleu It's always a pleasure to see you in action ;)

          1 Reply Last reply Reply Quote 1
          • meleuM
            meleu
            last edited by

            @hiulit said in Fun Facts Splashscreens:

            @meleu When I encountered the problem with Pixel theme and xmlstarlet I tried a different approach for finding the font.

            function get_theme_font() {
                get_current_theme
                if [[ -n "$(find "$ES_DIR/themes/$current_theme/art" -type f -name '*.ttf')" ]]; then
                    font="$(find "$ES_DIR/themes/$current_theme/art" -type f -name '*.ttf')"
                else
                    font=$DEFAULT_FONT
                fi
            }
            

            I did the job, but I wasn't happy about repeating the find function , but I don't know any better :P

            Do you think that could be a workaround for finding theme fonts?

            I know that some themes can have multiple fonts but I think it's ok to use the first one found.

            3 notes about the code above:

            1. you should use that font="$(find...)" before the if and then if [[ -n "$font" ]] (optmization)

            2. maybe using find can be a good approach but only after the current method fail. Also that find command needs more tweaking... Look the files at ComicBook/art directory for example. There are more than one .ttf file. Then we can make the find get only the first match. You did a good catching here! ;)

            3. that line with font=$DEFAULT_FONT... Man, memorize one thing for you shell script coder life: unless you have a really good reason, ALWAYS DOUBLE QUOTE YOUR VARIABLES. If you want an extensive (and a bit boring) explanation for that, you can read it here

            I'm on my lunch time now. I'll try to eat something and then send another PR.

            Cheers!

            • Useful topics
            • joystick-selection tool
            • rpie-art tool
            • achievements I made
            cyperghostC 1 Reply Last reply Reply Quote 0
            • cyperghostC
              cyperghost @meleu
              last edited by

              @meleu @hiulit Maybe annother point of view but maybe if the theme name is already extracted why not just search for *.ttf-files and use results as match and then do use just a simple grep alongside the xml to get a match ;)

              meleuM 1 Reply Last reply Reply Quote 0
              • meleuM
                meleu @cyperghost
                last edited by

                @cyperghost as far as I understood @hiulit wants specifically the font used in detailed view. At least that's what that xmlstarlet command is getting.

                @hiulit submitted another PR: https://github.com/hiulit/es-fun-facts-splashscreens/pull/2

                • Useful topics
                • joystick-selection tool
                • rpie-art tool
                • achievements I made
                cyperghostC hiulitH 2 Replies Last reply Reply Quote 0
                • cyperghostC
                  cyperghost @meleu
                  last edited by

                  @meleu Ah okay approved. But I think a generic solution is hard to find - so it's nice to have the fallback option to use Carbon-fonts :)

                  1 Reply Last reply Reply Quote 0
                  • hiulitH
                    hiulit @meleu
                    last edited by hiulit

                    Well, actually, I used the same line @meleu is using in his generate-launching-images.sh:P See:

                    https://github.com/meleu/generate-launching-images/blob/c55e61ee0066438068cf5b72c62430337bb1e3d5/generate-launching-images.sh#L416

                    xml_path="/theme/view[contains(@name,'detailed')]/textlist/fontPath"
                    

                    I don't know if we could have a more "standard" search function for fonts, because some themes have different fonts and it would be difficult to know which one is the "primary" font. I'm not into EmulationStation theming so I don't know if there's a predominant font that we could use.

                    Also, @cyperghost , if I understood you correctly, you though about searching for the font based on the theme's name? (e.g Pixel theme -> pixel.ttf)
                    Because, for what I see, there isn't a "standardized" naming convention on font naming for themes.
                    The default to Carbon's font it's nice, yeah! ;)

                    My little contributions to the RetroPie project:

                    • Shell-Script-Boilerplate
                    • Fun-Facts-Splashscreens
                    • Limit-Last-Played-Games
                    cyperghostC 1 Reply Last reply Reply Quote 0
                    • hiulitH
                      hiulit
                      last edited by

                      I would like to try to run Fun Facts Splashscreens on system's shutdown (or startup) but I don't have any clue about where to start looking. Any hint?

                      I've been looking at this thread https://retropie.org.uk/forum/topic/4836/emulationstation-shutdown-script/ and I've been able to add a line in /opt/retropie/supplementary/emulationstation/emulationstation.sh to launch es-fun-facts-splashscreens.sh and it works, but I don't think this is right place to do it nor the way to do it.

                      The place isn't right because, I think, the script should run either on system's startup or shutdown, as @Zigurana mentioned, not when EmulationStation starts, restarts or shutdowns. It doesn't make sense, because the splashscreens shows before EmulationStation starts.

                      And the way to do it isn't right because I don't like that, for this script to be launched, you have to edit some "core" files.

                      Is there any way to launch a script either on system's startup or shutdown?

                      My little contributions to the RetroPie project:

                      • Shell-Script-Boilerplate
                      • Fun-Facts-Splashscreens
                      • Limit-Last-Played-Games
                      meleuM 1 Reply Last reply Reply Quote 0
                      • meleuM
                        meleu @hiulit
                        last edited by

                        @hiulit said in shell scripting topic:

                        /opt/retropie/supplementary/emulationstation/emulationstation.sh

                        This file will be overwritten after the next ES update. Then, you're right, it's not the best script to tweak...

                        Is there any way to launch a script either on system's startup or shutdown?

                        At shutdown it's a bit tricky. I did this for my trick to gracefully finish ES and save metadata in every system shutdown

                        At start it's pretty simple: /etc/rc.local. And in my opinion your script can safely be placed here but you need to remove that command to show the image in the end.

                        It's a good place because the script can be setted to run in background (add & in the end of line that is calling your script). I think the current splashscreen to be displayed will be the previous generated one, but your script would be generating a new splash for the next boot. ;-)

                        • Useful topics
                        • joystick-selection tool
                        • rpie-art tool
                        • achievements I made
                        hiulitH 1 Reply Last reply Reply Quote 2
                        • hiulitH
                          hiulit @meleu
                          last edited by

                          @meleu I've added the script to etc/rc.local but it seems that there's something wrong with the $home variable. I get this error:

                          sed: cant' read /root/.emulationstation/es_settings.cfg: No such file or directory
                          

                          Instead of getting /home/pi as $home, it gets root, so the script doesn't work.

                          My little contributions to the RetroPie project:

                          • Shell-Script-Boilerplate
                          • Fun-Facts-Splashscreens
                          • Limit-Last-Played-Games
                          hiulitH 1 Reply Last reply Reply Quote 0
                          • hiulitH
                            hiulit @hiulit
                            last edited by hiulit

                            @hiulit If I launch the script adding -H -u pi it works, but I don't think it's the best way to do it...

                            My little contributions to the RetroPie project:

                            • Shell-Script-Boilerplate
                            • Fun-Facts-Splashscreens
                            • Limit-Last-Played-Games
                            meleuM 1 Reply Last reply Reply Quote 0
                            • meleuM
                              meleu @hiulit
                              last edited by

                              @hiulit said in shell scripting topic:

                              @hiulit If a launch the script adding -H -u pi it works, but I don't think it's the best way to do it...

                              IMHO it's perfectly acceptable and also a really good way to do it. ;-)

                              Of course if we evolve this method, this config (adding the script call in rc.local) won't be made manually, and then we can get the username to use.

                              ;)

                              • Useful topics
                              • joystick-selection tool
                              • rpie-art tool
                              • achievements I made
                              hiulitH 1 Reply Last reply Reply Quote 0
                              • hiulitH
                                hiulit @meleu
                                last edited by hiulit

                                @meleu But then, does it make sense to keep using this?

                                user="$SUDO_USER"
                                [[ -z "$user" ]] && user="$(id -un)"
                                home="$(eval echo ~$user)"
                                

                                Because that's what's getting root as $home when the script is launched in /etc/rc.local.

                                We could then just do home="/home/pi", don't you think?
                                Because adding the user -H -u pi like this removes the dynamism of the function (getting the user dynamically), right?

                                My little contributions to the RetroPie project:

                                • Shell-Script-Boilerplate
                                • Fun-Facts-Splashscreens
                                • Limit-Last-Played-Games
                                meleuM 1 Reply Last reply Reply Quote 0
                                • meleuM
                                  meleu @hiulit
                                  last edited by

                                  @hiulit said in shell scripting topic:

                                  @meleu But then, does it make sense to keep using this?

                                  user="$SUDO_USER"
                                  [[ -z "$user" ]] && user="$(id -un)"
                                  home="$(eval echo ~$user)"
                                  

                                  Yes. Keeping this in the script makes sense because the user can use it at any moment, right?

                                  Because that's what's getting root as $home when the script is launched in /etc/rc.local.

                                  Does it happen even when using sudo -H -u pi ...???

                                  We could then just do home="/home/pi", don't you think?
                                  Because adding the user -H -u pi like this removes the dynamism of the function (getting the user dynamically), right?

                                  Sorry if I misunderstood you, but you want to avoid to remove dynamism by hardcoding home=/home/pi? Sounds weird for me.

                                  I would consider what we have until now like a proof-of-concept script (kudos to you for working on this). Let's wait what the big boss will say about my proposal and take next steps based on his decision.

                                  If he refuse, we can continue this project extra-officially. I'm just trying to say that what approach to use to launch the script at startup depends on that decision.

                                  • Useful topics
                                  • joystick-selection tool
                                  • rpie-art tool
                                  • achievements I made
                                  hiulitH 1 Reply Last reply Reply Quote 0
                                  • cyperghostC
                                    cyperghost @hiulit
                                    last edited by

                                    @hiulit said in shell scripting topic:

                                    Also, @cyperghost , if I understood you correctly, you though about searching for the font based on the theme's name? (e.g Pixel theme -> pixel.ttf)
                                    Because, for what I see, there isn't a "standardized" naming convention on font naming for themes.
                                    The default to Carbon's font it's nice, yeah! ;)

                                    No, we misunderstood ;)
                                    If the theme CARBON looks like this
                                    ./art/font-1.ttf and ./art/font-2.ttf
                                    you can try to use ls *.ttf -R to find all fonts that are in the theme folder.
                                    Then do a grep search with all fonts and give the most usefull hit the path to your $DEFAULT_FONT

                                    Is it clear now? But maybe @meleu gots a better solution. I know it's bad to use lscommand for this action ;)

                                    hiulitH 1 Reply Last reply Reply Quote 0
                                    • hiulitH
                                      hiulit @meleu
                                      last edited by

                                      @meleu Ok, you're right! I didn't explain myself clearly, hehe! It sounds weird not wanting to hardcode sudo -H -u pi but wanting to hardcode home="/home/pi" instead :P Nonsense!

                                      Now, I'm at home and I'm testing the script in an actual Raspberry Pi with RetroPie and I have a couple of things to comment:

                                      • I've added sudo ~/es-fun-facts-splashscreens/es-fun-facts-splashscreens.sh & and it seems to work when I launch /etc/rc.local from the terminal, but not when the system boots up.

                                      • I've tried adding -H -u pi and it doesn't seem to work.

                                      This code worked perfectly! :D I didn't had imagemagick installed on this Raspberri Pi and the warning pop up, nice!

                                      if ! which convert > /dev/null; then
                                          echo "ERROR: The imagemagick package is not installed!"
                                          echo "Please, install it with 'sudo apt-get install imagemagick'."
                                          exit 1
                                      fi
                                      

                                      But I didn't had feh installed and the warning didn't pop up :(

                                      if [[ -n "$DISPLAY" ]] && ! which feh  > /dev/null; then
                                          echo "ERROR: The feh package is not installed!"
                                          echo "Please, install it with 'sudo apt-get install feh'."
                                          exit 1
                                      fi
                                      

                                      Maybe instead of && it should be || ?
                                      Or maybe remove [[ -n "$DISPLAY" ]] and just leave if ! which feh > /dev/null ?

                                      Anyways, the script works perfectly by itself and when launching /etc/rc.local from the terminal, but not when it's supposed to be launched in /etc.rc.local at startup.

                                      My little contributions to the RetroPie project:

                                      • Shell-Script-Boilerplate
                                      • Fun-Facts-Splashscreens
                                      • Limit-Last-Played-Games
                                      1 Reply Last reply Reply Quote 0
                                      • hiulitH
                                        hiulit @cyperghost
                                        last edited by

                                        @cyperghost Ok, but how would we know which is "the best hit"? Based on what?

                                        My little contributions to the RetroPie project:

                                        • Shell-Script-Boilerplate
                                        • Fun-Facts-Splashscreens
                                        • Limit-Last-Played-Games
                                        cyperghostC 1 Reply Last reply Reply Quote 0
                                        • cyperghostC
                                          cyperghost @hiulit
                                          last edited by cyperghost

                                          @hiulit Well the xml in the root of the theme or keywords like "mainfont=font-1.ttf".... That are just suggestions.

                                          Or you write an identifier.txt so you say: theme, font

                                          # Example file
                                          Carbon, system.ttf
                                          IO, mainfont.ttf
                                          
                                          hiulitH 1 Reply Last reply Reply Quote 1
                                          • hiulitH
                                            hiulit @cyperghost
                                            last edited by

                                            @cyperghost don't get me wrong! I really appreciate the feedback :)

                                            I think we can try to find a generic method to get fonts if we squeeze our minds together

                                            My little contributions to the RetroPie project:

                                            • Shell-Script-Boilerplate
                                            • Fun-Facts-Splashscreens
                                            • Limit-Last-Played-Games
                                            1 Reply Last reply Reply Quote 0
                                            • 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.