pa_volume icon indicating copy to clipboard operation
pa_volume copied to clipboard

Add support for relative volume change

Open zagor opened this issue 5 years ago • 3 comments

This patch adds the option of specifying a relative volume change, in the form of saying +10 or -10 to change the volume from current level. This allows using pa_volume for app-specific volume buttons.

Due to quirks in getopt(), negative changes need to be specified like: pa_volume paplay -- -10

I had to butcher some asserts a bit, but I didn't want to rewrite them too much without your input.

zagor avatar Dec 08 '20 18:12 zagor

Thank you for the suggestion and the code. Having to use -- is a bit awkward. I will try and see if there is an option to getopt to tell it "no more options after the first non-option argument" (which is a method used by some tools). Another way around it is to do what amixer (https://linux.die.net/man/1/amixer) does, which is to use "5%+" and "5%-" for a relative increase or decrease ie have the "+/-" after the number (I don't think I want to require a % sign).

Addendum, yes, avoiding the need for "--" should be possible. man getopt_long says:

  By default, getopt() permutes the contents of argv as it scans, so  that  eventually  all  the
  nonoptions are at the end.  Two other scanning modes are also implemented.  If the first char‐
  acter of optstring is '+' or the environment variable POSIXLY_CORRECT is set, then option pro‐
  cessing  stops as soon as a nonoption argument is encountered.  If the first character of opt‐
  string is '-', then each nonoption argv-element is handled as if it were the  argument  of  an
  option  with  character code 1.  (This is used by programs that were written to expect options
  and other argv-elements in any order and that care about the ordering of the two.)   The  spe‐
  cial argument "--" forces an end of option-scanning regardless of the scanning mode.

so adding a "+" at the beginning of optstring will remove the need for "--"

rhaas80 avatar Dec 08 '20 19:12 rhaas80

Ahh, moving the +/- to a suffix is a great idea.

I briefly looked into persuading getopt to accept -10 but I got the impression it was only possible for required parameters.

Whichever way you prefer is fine with me.

Edit: Let me know if you want me to submit an update or if you prefer to write it yourself.

zagor avatar Dec 08 '20 19:12 zagor

Another way around it is to do what amixer (https://linux.die.net/man/1/amixer) does, which is to use "5%+" and "5%-" for a relative increase or decrease ie have the "+/-" after the number (I don't think I want to require a % sign)

Yep, second to that, as I too am thinking that having to use -- is a bit awkward.

So, shall we settle with:

pa_volume paplay 10+
pa_volume paplay 10-

to specify a relative volume change (of +10 or -10 to change the volume from current level), as I believe this is what @rhaas80 meant.

@zagor, if you OK with such notation, would you submit an update on top of this PR please?

suntong avatar Jul 19 '23 21:07 suntong