Headphone Indicator

Forum Forums General Tips and Tricks Headphone Indicator

  • This topic has 9 replies, 5 voices, and was last updated Mar 2-3:52 pm by Lead Farmer.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #100818
    Member
    Lead Farmer

      This script will show an headphone icon when the a headphone is connected through the headphone jack.

      #!/bin/bash
      #
      # Show a headphone indicator when the headphone is pluged
      #
      # can be added to startup
      
      FLAG=1
      
      while true; do
      	HEADPHONE=$(amixer -c 0 contents | awk -F "=" 'FNR == 3 {print $2}')
      	if [[ "$HEADPHONE" = "on" && $FLAG -eq 1 ]]; then
      		yad --notification --image="/usr/share/icons/Adwaita/48x48/devices/audio-headphones.png" --command="clicked=true'" &
      		sleep 1
      		YAD=$(pidof yad | awk '{print $1}')
      		FLAG=0
      	fi
      	if [[ "$HEADPHONE" = "off" && $FLAG -eq 0 ]]; then
      		kill "$YAD"
      		FLAG=1
      	fi
      	sleep 1
      done
      

      How to run it:

      1) Open the terminal and copy the script and press enter.

      2) Save the it in a file will .sh extension, and make it executable, and run it the terminal, like:

      bash Documents/headphone.sh
      Note: don’t run the script from a file manger because you will don’t have a way to exit it.

      3) Running the script from startup, for example in Control Center > Edit IceWM Setting > startup

      bash Documents/headphone.sh

      Note: The script will always run in the background and take about 1% CPU depending by your CPU, so if you are using headphone regularly and you don’t mine the CPU usage do it, otherwise I don’t recommend using it.

      Icon: In the script I use a white headphone icon that is in /usr/share/icons/Adwaita/48×48/devices/ folder, but you can replace it with other icon of your choosing, I post mine.

      #####
      I Edit the script a little.

      • This topic was modified 2 months, 1 week ago by Lead Farmer.
      • This topic was modified 2 months, 1 week ago by Lead Farmer.
      • This topic was modified 2 months, 1 week ago by Lead Farmer.
      #100832
      Member
      PPC
        Helpful
        Up
        0
        ::

        Very nice idea! I have not yet tested it because I have no way to do so here.
        You keep coming up with some ideas that allow you to do exactly what you want your system to do, and share them here!

        I started in antiX just like that.

        Suggestion: include, from the system tray icon, a way to disable the script, to save system resources – that is not always so easy to implement as it seems…

        Like you noticed, antiX already includes a headphones image (several, even). Example: /usr/share/icons/Adwaita/48×48/devices/audio-headphones.png, so there’s no need to attach the file, like you did

        PS: one of the reasons that antiX does not include similar tools that say in RAM is to save system resources- it’s exactly the same reason why antiX does not include an automated way to check for updates (and you also found a way around that, using a cronjob and a script)

        edit: the problem with yad icons on the toolbar is not particularly the CPU, but the RAM, each one uses about 6Mb, if I recall correctly – that’s a lot of RAM for 1GB or RAM or less legacy computer…

        • This reply was modified 2 months, 1 week ago by PPC.
        • This reply was modified 2 months, 1 week ago by PPC.
        #100837
        Member
        Lead Farmer
          Helpful
          Up
          0
          ::

          Very nice idea! I have not yet tested it because I have no way to do so here.
          You keep coming up with some ideas that allow you to do exactly what you want your system to do, and share them here!

          I started in antiX just like that.

          Thanks

          Suggestion: include, from the system tray icon, a way to disable the script, to save system resources – that is not always so easy to implement as it seems…

          I also thought of doing something similar, like activating the script from a hotkey, but the problem is exiting the script. so I think is best to run it from the terminal.

          PS: one of the reasons that antiX does not include similar tools that say in RAM is to save system resources- it’s exactly the same reason why antiX does not include an automated way to check for updates (and you also found a way around that, using a cronjob and a script)

          edit: the problem with yad icons on the toolbar is not particularly the CPU, but the RAM, each one uses about 6Mb, if I recall correctly – that’s a lot of RAM for 1GB or RAM or less legacy computer…

          True, if the headphone connected most of the time, but the CPU resources is more important, the script do calculation every second checking if the headphone is connected, something that could be meaningless if you don’t going to connect headphone.

          • This reply was modified 2 months, 1 week ago by Lead Farmer.
          #100841
          Member
          blur13
            Helpful
            Up
            0
            ::

            Genuinely curious about the use case for this?

            An ocular inspection will also indicate if a headphone is connected through the headphone jack. At least on a laptop its really obvious. Maybe on a desktop with audio connections on the back its less so.

            #100842
            Member
            PPC
              Helpful
              Up
              0
              ::

              Genuinely curious about the use case for this?

              1- Years ago I thought my computer’s audio had died because my wife left the headphones plugged to the back of the desktop, and I heard no audio… until I noticed the almost inaudible sound coming from the phones resting on their usual place, on the top of the computer…
              2- Practice creating scripts – if one tries hard enough, one can strike gold, and create something really useful for many people. Practice makes perfect, right? I shared many scripts here, some went nowhere, some are now standard in this OS…

              #100848
              Member
              Lead Farmer
                Helpful
                Up
                0
                ::

                Genuinely curious about the use case for this?

                An ocular inspection will also indicate if a headphone is connected through the headphone jack. At least on a laptop its really obvious. Maybe on a desktop with audio connections on the back its less so.

                Like android phones that show the same indicator when the headphone is plugged, it have some benefits.

                Antix (and I guess all other Linux distros) switch audio output to headphone when it’s connected, it’s a visual indication that audio is now output from the headphones, in case you wonder why you don’t have audio from your speakers if the headphone is connected.

                Also let say the headphone jack on your headphone don’t make a good connection (it happen to me more then once) the indicator will not show.

                • This reply was modified 2 months, 1 week ago by Lead Farmer.
                #100884
                Member
                blur13
                  Helpful
                  Up
                  0
                  ::

                  These are all good points, thank you for sharing!

                  #100898
                  Member
                  RJP
                    Helpful
                    Up
                    0
                    ::

                    Great script! Must add to the startup!

                    #100901
                    Member
                    ile
                      Helpful
                      Up
                      0
                      ::

                      made the sleep 8 and 3. happy. it works a charm in all the antiX toolbars!

                      #100932
                      Member
                      Lead Farmer
                        Helpful
                        Up
                        0
                        ::

                        Great script! Must add to the startup!

                        Glad you like it.

                        made the sleep 8 and 3. happy. it works a charm in all the antiX toolbars!

                        8 seconds sound excessive, the first sleep is to give time to yad massage to register before saving it’s PID (process identification number), if the script don’t work currently you might what to increase it 2 or 3.
                        The second sleep it’s a wait before checking if the headphone is connected again, you might what to increase it if you what to reduce the CPU load by the script, but it will cause a bigger delay when the icon shows and removed.

                        • This reply was modified 2 months, 1 week ago by Lead Farmer.
                      Viewing 10 posts - 1 through 10 (of 10 total)
                      • You must be logged in to reply to this topic.