Discussion:
DISPLAY env in acpi/actions scripts
Add Reply
f6k
2020-09-30 09:56:54 UTC
Reply
Permalink
hello,

i've made myself a really simple acpi/actions scripts to handle the
volume from Fn keys and display volume level thanks to osd_cat. but i
can't have osd_cat display anything after the boot. i need to restart
acpid to make it display on my screen. here's the script for
volumedown:

------8<-------
amixer -q set Master 2-
if [ ! -z "$DISPLAY" ]; then
VOLUME=$(amixer get Master| grep Mono:| sed -r 's/.*\[(.*)%\].*/\1/')
pkill osd_cat &>/dev/null
DISPLAY=:0 osd_cat -f "-xos4-terminus-bold-r-normal--24-*-*-*-*-*-*" -b percentage -d 2 -P $VOLUME -A center -p bottom -l 2 -o 5 -c green&
fi
------8<-------

again, the problem i have it that, after the boot, osd_cat don't
display anything -- but the volume is goind down. i have to
/etc/rc.d/rc.acpid restart to make osd_cat display something. i was
thinking that the problem was from my if statement since during the
boot there's no $DISPLAY and that maybe the script stays like that in
some cache. so i used something more complicated found on thinkwiki[1]
which basically define $DISPLAY based on what's available in
/tmp/.X11-unix/X*

1: http://www.thinkwiki.org/wiki/ACPI_action_script_optimized_for_R51

------8<-------
export DISPLAY="auto"

amixer -q set Master 2-

if [ "${DISPLAY}" == "auto" ]; then
X="$(ls /tmp/.X11-unix/X* | head -n1)";
[ "${X}" != "" ] && export DISPLAY=":${X/\/tmp\/.X11-unix\/X/}"
fi
if [ "${DISPLAY}" != "auto" ]; then
pkill osd_cat &>/dev/null
echo "$DISPLAY" > /tmp/display.txt
DISPLAY="$DISPLAY" osd_cat -f "-xos4-terminus-bold-r-normal--24-*-*-*-*-*-*" -b percentage -d 2 -P $VOLUME -A center -p bottom -l 2 -o 5 -c green&
else
logger "OSD cat: $@"
fi
------8<-------

but again, it doesn't work, osd_cat doesn't display nothing after the
boot and i have to /etc/rc.d/rc.acpid restart.

please note the test echo inside the second if statement. it gives me
the expected result, meaning ":0" (without restarting acpid after the
boot). i'm out of ideas and i can't see by myself what i'm doing
wrong. can anyone help me please? thanks.

-f6k
--
~{,_,"> indignus LabRat - ftp://shl.huld.re
Henrik Carlqvist
2020-10-01 06:05:54 UTC
Reply
Permalink
Post by f6k
i've made myself a really simple acpi/actions scripts to handle the
volume from Fn keys and display volume level thanks to osd_cat. but i
can't have osd_cat display anything after the boot. i need to restart
acpid to make it display on my screen.
If osd_cat is some kind of daemon being an X program it will have to get
started _after_ X is started. Failing to do so it will probably abort
with an error message like:

Error: Can't open display: :0

So if you want this to start at boot you will also make sure that X is
started at boot (and not only after login) and that osd_cat is started
after X.

You also might want to create some kind of "keep alive" function for
osd_cat to make it respawn if X is killed and restarted. This might
happen if a user logs out and the same or another user logs in again
using KDM/XDM or something like that. That "keep alive" function might
also be your solution to make sure that osd_cat is started after X.

Another thing to consider is that one or several different users might
have multiple X sessions with different DISPALY numbers on different TTYs.

Also, another tricky thing might be the permissions to your X server.
That might be handled with xhost, xauth or some other mechanism. Any user
on any machine having permission to connect to your DISPLAY will be able
to put Windows on your screen and make screenshots of your screen.

regards Henrik
Henrik Carlqvist
2020-10-01 06:07:32 UTC
Reply
Permalink
Usually the "right" way for this is to not start X applications in your
boot scripts but in the startup scripts of your window manager.

regards Henrik
f6k
2020-10-01 13:47:54 UTC
Reply
Permalink
hello,
Post by Henrik Carlqvist
Post by f6k
i've made myself a really simple acpi/actions scripts to handle the
volume from Fn keys and display volume level thanks to osd_cat. but i
can't have osd_cat display anything after the boot. i need to restart
acpid to make it display on my screen.
If osd_cat is some kind of daemon being an X program it will have to get
started _after_ X is started. Failing to do so it will probably abort
Error: Can't open display: :0
So if you want this to start at boot you will also make sure that X is
started at boot (and not only after login) and that osd_cat is started
after X.
actually osd_cat isn't a daemon at all; it's more like cat(1) but
display whatever text you want on your X11 desktop, on top of
everything (somehow like the green OSD TV when you up/down the volume).
Post by Henrik Carlqvist
Another thing to consider is that one or several different users might
have multiple X sessions with different DISPALY numbers on different TTYs.
Also, another tricky thing might be the permissions to your X server.
That might be handled with xhost, xauth or some other mechanism. Any user
on any machine having permission to connect to your DISPLAY will be able
to put Windows on your screen and make screenshots of your screen.
and that was it! thank you very much, you made me understand what i
was missing... xauth was the link i needed. since i already started
several times X with a valid user, i just had to correctly export
XAUTHORITY env at the top of the script so it doesn't need X and acpid
restart to have the correct path.

btw to answer why i'm using acpid instead of my WM to handle that, is
because i want to be able to use my Fn-volume keys from both X and TTY
(and have a direct visual feedback when i'm under X, while it will log
the action when i'm on a console).

thank you very much Henrik for your lights and insights :)

-f6k
--
~{,_,"> indignus LabRat - ftp://shl.huld.re
Bit Twister
2020-10-01 13:08:21 UTC
Reply
Permalink
Post by f6k
hello,
i've made myself a really simple acpi/actions scripts to handle the
volume from Fn keys and display volume level thanks to osd_cat. but i
can't have osd_cat display anything after the boot. i need to restart
acpid to make it display on my screen.
Then I would guess there would be error messages. I can suggest using
"script" to log what is going on. Even better have bash show you what
is going on. Example command line test shot:

script -c "bash -x /where/ever/your_script" > /some/where/debug.log

You may have to put that in a wrapper script in order to have it run
as desired from some autostart directory.
Post by f6k
------8<-------
amixer -q set Master 2-
if [ ! -z "$DISPLAY" ]; then
VOLUME=$(amixer get Master| grep Mono:| sed -r 's/.*\[(.*)%\].*/\1/')
pkill osd_cat &>/dev/null
DISPLAY=:0 osd_cat -f "-xos4-terminus-bold-r-normal--24-*-*-*-*-*-*" -b percentage -d 2 -P $VOLUME -A center -p bottom -l 2 -o 5 -c green&
fi
------8<-------
again, the problem i have it that, after the boot, osd_cat don't
display anything -- but the volume is goind down. i have to
/etc/rc.d/rc.acpid restart to make osd_cat display something. i was
thinking that the problem was from my if statement since during the
boot there's no $DISPLAY and that maybe the script stays like that in
some cache. so i used something more complicated found on thinkwiki[1]
which basically define $DISPLAY based on what's available in
/tmp/.X11-unix/X*
1: http://www.thinkwiki.org/wiki/ACPI_action_script_optimized_for_R51
------8<-------
export DISPLAY="auto"
amixer -q set Master 2-
if [ "${DISPLAY}" == "auto" ]; then
As a general rule, that is not the best method for $DISPLAY testing. Suggest
if [ -z ${SOME_VAR:-""} ] ; then

I find it handy to use "set -u" for syntax and whatnot problem detection. Example
#!/bin/bash
#*********************************
#* script header here
#*********************************

set -u # Show usage of unset variables as an error to help catch typos.


You might also want/need to add the user to the xhosts file for DISPLAY
access.

I needed to have xmessage pop up messages from root cron jobs but I
was getting xauth error messages. Solution was to allow root on localhost.
xhost +si:localuser:root
Bit Twister
2020-10-01 13:15:31 UTC
Reply
Permalink
Post by Bit Twister
Post by f6k
if [ "${DISPLAY}" == "auto" ]; then
As a general rule, that is not the best method for $DISPLAY testing. Suggest
if [ -z ${SOME_VAR:-""} ] ; then
Sorry, I cut that from my help file and forgot to go back and change SOME_VAR
should have been
if [ -z ${DISPLAY:-""} ] ; then
f6k
2020-10-01 14:14:19 UTC
Reply
Permalink
Post by Bit Twister
Post by Bit Twister
As a general rule, that is not the best method for $DISPLAY testing. Suggest
if [ -z ${SOME_VAR:-""} ] ; then
Sorry, I cut that from my help file and forgot to go back and change
SOME_VAR should have been
if [ -z ${DISPLAY:-""} ] ; then
no worries, thank you :)

-f6k
--
~{,_,"> indignus LabRat - ftp://shl.huld.re
f6k
2020-10-01 14:22:39 UTC
Reply
Permalink
Post by Bit Twister
As a general rule, that is not the best method for $DISPLAY testing. Suggest
if [ -z ${SOME_VAR:-""} ] ; then
since we're here, can i ask you some details regarding that please? i
mean why is that a best method. as said earlier, i've notice that
method of declaration in SlackBuilds, even i noted things like e.g.
TAG=${TAG:-_SBo}. what's the ":-" for?

thank you for your answer :)

-f6k
--
~{,_,"> indignus LabRat - ftp://shl.huld.re
Bit Twister
2020-10-01 15:02:58 UTC
Reply
Permalink
Post by f6k
Post by Bit Twister
As a general rule, that is not the best method for $DISPLAY testing. Suggest
if [ -z ${SOME_VAR:-""} ] ; then
since we're here, can i ask you some details regarding that please? i
mean why is that a best method. as said earlier, i've notice that
method of declaration in SlackBuilds, even i noted things like e.g.
TAG=${TAG:-_SBo}. what's the ":-" for?
In a nutshell, if variable is not set/null, set it to this value.

You may want to bookmark this url,
https://tldp.org/LDP/abs/html/index.html

If you look in the index, you might like
B-4. Parameter Substitution and Expansion
which points to
https://tldp.org/LDP/abs/html/refcards.html#AEN22728
f6k
2020-10-01 15:29:56 UTC
Reply
Permalink
Post by Bit Twister
Post by f6k
Post by Bit Twister
As a general rule, that is not the best method for $DISPLAY testing. Suggest
if [ -z ${SOME_VAR:-""} ] ; then
since we're here, can i ask you some details regarding that please? i
mean why is that a best method. as said earlier, i've notice that
method of declaration in SlackBuilds, even i noted things like e.g.
TAG=${TAG:-_SBo}. what's the ":-" for?
In a nutshell, if variable is not set/null, set it to this value.
You may want to bookmark this url,
https://tldp.org/LDP/abs/html/index.html
If you look in the index, you might like
B-4. Parameter Substitution and Expansion
which points to
https://tldp.org/LDP/abs/html/refcards.html#AEN22728
thank you very much! very appreciated :)

-f6k
--
~{,_,"> indignus LabRat - ftp://shl.huld.re
f6k
2020-10-01 14:12:43 UTC
Reply
Permalink
hello,
Post by Bit Twister
Post by f6k
hello,
i've made myself a really simple acpi/actions scripts to handle the
volume from Fn keys and display volume level thanks to osd_cat. but i
can't have osd_cat display anything after the boot. i need to restart
acpid to make it display on my screen.
Then I would guess there would be error messages. I can suggest using
"script" to log what is going on. Even better have bash show you what
script -c "bash -x /where/ever/your_script" > /some/where/debug.log
You may have to put that in a wrapper script in order to have it run
as desired from some autostart directory.
actually i never tried to do that. since i wanted to test the DISPLAY
env, i just added echo $DISPLAY > foo.txt in various sections of the
script (yeah, i guess it's a bad way to run tests...). i'll keep that
one for future use, thank you!
Post by Bit Twister
Post by f6k
if [ "${DISPLAY}" == "auto" ]; then
As a general rule, that is not the best method for $DISPLAY testing. Suggest
if [ -z ${SOME_VAR:-""} ] ; then
i've seen that formulation few times (well for instance in
SlackBuilds); never thought to use them myself in my own script. will
update my script and others! thank you for that too!
Post by Bit Twister
I find it handy to use "set -u" for syntax and whatnot problem
detection. Example
#!/bin/bash
#*********************************
#* script header here
#*********************************
set -u # Show usage of unset variables as an error to help catch typos.
You might also want/need to add the user to the xhosts file for DISPLAY
access.
I needed to have xmessage pop up messages from root cron jobs but I
was getting xauth error messages. Solution was to allow root on localhost.
xhost +si:localuser:root
that's interesting. i've found my way by exporting XAUTHORITY in the
script to my own ~/.Xauthority file. but probably this solution is
better?

-f6k
--
~{,_,"> indignus LabRat - ftp://shl.huld.re
Joe Rosevear
2020-10-01 22:08:18 UTC
Reply
Permalink
Post by f6k
hello,
i've made myself a really simple acpi/actions scripts to handle the
volume from Fn keys and display volume level thanks to osd_cat. but i
can't have osd_cat display anything after the boot. i need to restart
acpid to make it display on my screen. here's the script for
------8<-------
amixer -q set Master 2-
if [ ! -z "$DISPLAY" ]; then
VOLUME=$(amixer get Master| grep Mono:| sed -r 's/.*\[(.*)%\].*/\1/')
pkill osd_cat &>/dev/null
DISPLAY=:0 osd_cat -f "-xos4-terminus-bold-r-normal--24-*-*-*-*-*-*" -b percentage -d 2 -P $VOLUME -A center -p bottom -l 2 -o 5 -c green&
fi
------8<-------
[snip]
Hello, f6k.

Are you open to suggestions?

If so, then perhaps try a different approach altogether. In
particular, there is a tool that I use called xfce4-volumed-pulse which
is included with Slackware 14.1.

I invoke it from my .fvwm2rc file like this:

Exec exec /usr/bin/xfce4-volumed-pulse

It creates a process which automagically displays a volume graphic for
about five seconds whenever you change the volume.

I coupled that with these lines also in .fvwm2rc:

vvv
# Mute
# Key F8 A N Exec :
Key XF86AudioMute A N Exec :

# Volume down
Key F9 A N Exec /usr/local/bin/changevol down
Key XF86AudioLowerVolume A N Exec /usr/local/bin/changevol down

# Volume up
Key F10 A N Exec /usr/local/bin/changevol up
Key XF86AudioRaiseVolume A N Exec /usr/local/bin/changevol up
^^^

Note that I have commented-out the code for the F8 key, because I did
not want to allow the volume to be muted--perhaps muting works if you
un-comment that line.

changevol (in the above) is a script that I wrote. I won't put it all
here, but the gist of it is in this line:

pacmd set-sink-volume $default `up $cur_vol`

and this line

pacmd set-sink-volume $default `down $cur_vol`

Note that up and down are functions defined in script changevol.

There you go--some food for thought. Have fun!

-Joe
Joe Rosevear
2020-10-01 23:03:26 UTC
Reply
Permalink
Post by Joe Rosevear
Post by f6k
hello,
i've made myself a really simple acpi/actions scripts to handle the
volume from Fn keys and display volume level thanks to osd_cat. but i
can't have osd_cat display anything after the boot. i need to restart
acpid to make it display on my screen. here's the script for
------8<-------
amixer -q set Master 2-
if [ ! -z "$DISPLAY" ]; then
VOLUME=$(amixer get Master| grep Mono:| sed -r 's/.*\[(.*)%\].*/\1/')
pkill osd_cat &>/dev/null
DISPLAY=:0 osd_cat -f "-xos4-terminus-bold-r-normal--24-*-*-*-*-*-*" -b percentage -d 2 -P $VOLUME -A center -p bottom -l 2 -o 5 -c green&
fi
------8<-------
[snip]
Hello, f6k.
Are you open to suggestions?
If so, then perhaps try a different approach altogether. In
particular, there is a tool that I use called xfce4-volumed-pulse which
is included with Slackware 14.1.
Exec exec /usr/bin/xfce4-volumed-pulse
It creates a process which automagically displays a volume graphic for
about five seconds whenever you change the volume.
vvv
# Mute
# Volume down
Key F9 A N Exec /usr/local/bin/changevol down
Key XF86AudioLowerVolume A N Exec /usr/local/bin/changevol down
# Volume up
Key F10 A N Exec /usr/local/bin/changevol up
Key XF86AudioRaiseVolume A N Exec /usr/local/bin/changevol up
^^^
Note that I have commented-out the code for the F8 key, because I did
not want to allow the volume to be muted--perhaps muting works if you
un-comment that line.
changevol (in the above) is a script that I wrote. I won't put it all
pacmd set-sink-volume $default `up $cur_vol`
and this line
pacmd set-sink-volume $default `down $cur_vol`
Note that up and down are functions defined in script changevol.
There you go--some food for thought. Have fun!
-Joe
I think you need these lines also:

xmodmap -e 'keycode 122=F9'
xmodmap -e 'keycode 123=F10'

Put them in a file (I used /home/joe/keycodes) and invoke it. I
invoked it from .fvwm2rc (before the first bit) like this:

Exec exec /home/joe/keycodes

I don't remember exactly why thy are needed, but I think keys "F9" and
"F10" won't be found in the first bit otherwise.

-Joe
f6k
2020-10-02 14:56:09 UTC
Reply
Permalink
Post by Joe Rosevear
xmodmap -e 'keycode 122=F9'
xmodmap -e 'keycode 123=F10'
Put them in a file (I used /home/joe/keycodes) and invoke it. I
Exec exec /home/joe/keycodes
I don't remember exactly why thy are needed, but I think keys "F9" and
"F10" won't be found in the first bit otherwise.
noted, thank you again joe! :)

-f6k
--
~{,_,"> indignus LabRat - ftp://shl.huld.re
f6k
2020-10-02 14:55:36 UTC
Reply
Permalink
hello joe.
Post by Joe Rosevear
Are you open to suggestions?
yes, always!
Post by Joe Rosevear
If so, then perhaps try a different approach altogether. In
particular, there is a tool that I use called xfce4-volumed-pulse which
is included with Slackware 14.1.
Exec exec /usr/bin/xfce4-volumed-pulse
interesting i never thinked to use some DWM tools for that, thank you
for the idea.
Post by Joe Rosevear
It creates a process which automagically displays a volume graphic for
about five seconds whenever you change the volume.
yes this is basically what i expect from my configuration. so it's
nice!
Post by Joe Rosevear
[snip]
changevol (in the above) is a script that I wrote. I won't put it all
pacmd set-sink-volume $default `up $cur_vol`
and this line
pacmd set-sink-volume $default `down $cur_vol`
yes, i should do that. as you noted i use two script that does the
same exact thing where the only difference in them is a "+" and a "-".
that's quite stupid. i need to change that, that is to say merge them
and search for down or up in $1, like you seems doing.

the only downside i see in your solution is that it works for X11 but
not for TTY. my purpose using acpi script is that my keystroke
configurations will be effective on TTY _and_ X11. but these days on
my new fancy computer i spend most of my time under X11; so it would be
worth it delegate all my configuration to my WM.

i'll give it a try, thank you!

-f6k
--
~{,_,"> indignus LabRat - ftp://shl.huld.re
Loading...