Mapping ACPI events for brightness buttons on Lenovo Yoga X1 v2 - brightness

I installed Ubuntu Gnome 17.04 on my new Lenovo Yoga X1 (version 2) and the brightness buttons don't work out of the box. I've gone through the steps (below) I thought necessary to map these keys to xrandr calls, but nothing happens, even if I log the key mapping event being caught successfully. If I manually run the logged command brightness changes appropriately. What am I missing in the ACPI route?
First see what ACPI events the brightness buttons are sending
$ acpi_listen
video/brightnessdown BRTDN 00000087 00000000
video/brightnessup BRTUP 00000086 00000000
Then create the event definitions
$ cat yoga-brightness-up
event=video/brightnessup BRTUP 00000086
action=/etc/acpi/yoga-brightness.sh up
$ cat yoga-brightness-down
event=video/brightnessdown BRTDN 00000087
action=/etc/acpi/yoga-brightness.sh down
Define the action script
$ cat /etc/acpi/yoga-brightness.sh
#!/bin/sh
# Where the backlight brightness is stored
BR_DIR="/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/"
test -d "$BR_DIR" || exit 0
MIN=0
MAX=$(cat "$BR_DIR/max_brightness")
VAL=$(cat "$BR_DIR/brightness")
if [ "$1" = down ]; then
VAL=$((VAL-71))
else
VAL=$((VAL+71))
fi
if [ "$VAL" -lt $MIN ]; then
VAL=$MIN
elif [ "$VAL" -gt $MAX ]; then
VAL=$MAX
fi
PERCENT=`echo "$VAL / $MAX" | bc -l`
#export XAUTHORITY=/home/ivo/.Xauthority # CHANGE "ivo" TO YOUR USER
#export DISPLAY=:0.0
export XAUTHORITY=/home/jorvis/.Xauthority
export DISPLAY=:0
echo "xrandr --output eDP-1 --brightness $PERCENT" > /tmp/yoga-brightness.log
xrandr --output eDP-1 --brightness $PERCENT
echo $VAL > "$BR_DIR/brightness"
Restart acpid
$ sudo /etc/init.d/acpid reload
Success should write to the brightness log
$ rm /tmp/yoga-brightness.log
[ hit brightness down button three times ]
$ sudo cat /tmp/yoga-brightness.log
xrandr --output eDP-1 --brightness .76603773584905660377
Log is written correctly, as is the brightness value:
$ cat /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/brightness
759
Nothing happens on the actual display though. It DOES work
though if I manually run the command which was logged to
have run.
$ xrandr --output eDP-1 --brightness .76603773584905660377

And after reading through this post I noticed the ENV part again and double-checked it. The problem was the setting of $XAUTHORITY in the script. It was fine for it to point to my ~/.Xauthority (which didn't exist by default), but I needed to do this:
$ ln -s /run/user/1000/gdm/Xauthority ~/.Xauthority
After that the brightness buttons worked.

Related

parse erorr near '\n' in .zshrc file

When I open my MacOS terminal, it shows
Last login: Sun Jan 2 15:50:48 on ttys000
/Users/rajeshrao/.zshrc:18: parse error near `\n'
(base) rajeshrao#Rajeshs-MacBook-Air ~ %
I tried opening that .zshrc file which had this code in there ---->
export PATH="/usr/local/sbin:$PATH"
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/rajeshrao/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/rajeshrao/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/Users/rajeshrao/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/Users/rajeshrao/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home
<<<conda initialize<<<
here is the image of code
I don't know much about how shell works and am just a beginner. I didn't interfere with anything before using the terminal.
The last line has lost the initial '#' (ie. is not a comment).
Edit the file to end with this line:
# <<<conda initialize<<<

How to catch "$variable is not defined" in jq?

Let's pretend I'm running something like this:
jq -nr --arg target /tmp \
'(["echo","Hello, world"]|#sh)+">\($target)/sample.txt"' \
| sh
Everything is fine unless I forgot to pass variable $target:
$ jq -nr '(["echo","Hello, world"]|#sh)+">\($target)/sample.txt"'
jq: error: $target is not defined at <top-level>, line 1:
(["echo","Hello, world"]|#sh)+">\($target)/sample.txt"
jq: 1 compile error
How can I catch this and use default value?
I've tried:
$target?
($target)?
try $target catch null
$target? // null
But it seems to be parsing-time error, which obviously can't be caught at runtime. Have I've missed any dynamic syntax?
I've found that command-line arguments can be found in $ARGS.name, but there are two drawbacks:
This was introduced in version 1.6, but I have 1.5 on CentOS 7.
It doesn't catch locally defined variables.
Assuming you need to do something more useful with jq than write 'Hello World' over a text file. I propose the following,
Maybe we can learn some programming tips from Jesus:
"Give to Caesar what belongs to Caesar, and give to God what belongs to God"
Suppose that Caesar is bash shell and God is jq, bash is appropriate to work and test the existence of files, directories and environment variables, jq is appropriate to process information in json format.
#!/bin/bash
dest_folder=$1
#if param1 is not given, then the default is /tmp:
if [ -z $dest_folder ]; then dest_folder=/tmp ; fi
echo destination folder: $dest_folder
#check if destination folder exists
if [ ! -d $dest_folder ]
then
echo "_err_ folder not found"
exit 1
fi
jq -nr --arg target $dest_folder '(["echo","Hello, world"]|#sh)+">\($target)/sample.txt"' | sh
#if the file is succesfully created, return 0, if not return 1
if [ -e "$dest_folder/sample.txt" ]
then
echo "_suc_ file was created ok"
exit 0
else
echo "_err_ when creating file"
exit 1
fi
Now you can include this script as a step in a more complex batch, because it is congruent with linux style, returning 0 on success.

Enter a command for a user

I have written a ZSH function whose output is a command line which runs a program I need the user to be able to interact with.
At the moment I just echo the command line and instruct the user to copy-paste it so that they have the necessary access to its pipes, however is there a way I can just have the function finish by entering the command for the user as if they had copied and pasted it themselves?
I have looked into using zle but that seems to require a key binding, whereas I just want the user to be able to run: myzshfunction arg1 and the ultimate result to be their terminal attached to the program launched as a result of some processing of their arg1.
$ myzshfunction arg2*2
Run this command! foobar baz4
$ foobar baz4
...
The function looks something like this:
myzshfunction() {
if [[ $# = 0 ]]
then
echo "usage: myzshfunction 1.2.3.4"
return
fi
local creds=`curl "https://xxx/$1/latest" | jq -r 'x'`
local cred_arr=("${(#s|/|)creds}")
local pwd_pipe=$(mktemp -u)
mkfifo $pwd_pipe
exec 3<>$pwd_pipe
rm $pwd_pipe
echo $cred_arr[2] >&3
echo "Run this: sshpass -d3 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$cred_arr[1]#$1"
exec 3>&-
}
TIA
Use print -z to add text to the buffer. From the documentation:
-z Push the arguments onto the editing buffer stack, separated by spaces.
Calling foo, defined below, will result in hi being placed on the command line as if the user had typed it. For example,
% foo () { print -z hi; }
% foo
% hi
The best solution I could come up with was to use zle - the Zsh Line Editor.
This lets you update the command the user is currently editing, which to me feels a bit hacky. I would prefer a solution that lets you call a function, hit return, and then cleanly run a function with STDIO attached to your terminal as if you had run the resulting command line.
Perhaps you could emulate this by bindkey'ing the return key and doing some sort of decision/routing from there to see if the user wants to call myfunc. For now, my solution requires the Esc+i sequence is entered after typing a target for $host.
zle-myfunc() {
apikey=$(keychain-environment-variable api-key)
if [ $? -ne 0 ]; then
echo "Add your api-key to the keychain: "
BUFFER='security add-generic-password -U -a ${USER} -D "environment variable" -s "api-key" -w'
zle accept-line
return 1
fi
local host=$BUFFER
zle kill-buffer
local creds=`curl ..." | jq -r ...`
if [ -z creds ]; then
echo "Couldn't get creds, check your network"
return 1
fi
local creds_arr=("${(#s|/|)creds}")
local pwd_pipe=$(mktemp -u)
mkfifo $pwd_pipe
exec 3<>$pwd_pipe
# anonymise the pipe
rm $pwd_pipe
echo "$creds_arr[2]" >&3
BUFFER="sshpass -d3 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $creds_arr[1]#$host"
zle accept-line
# exec 3>&-
}
zle -N zle-myfunc
bindkey '\ei' zle-myfunc

is `cap` a reserved word? - zsh completion?

I'm trying to create a Capistrano mutilstage completion for ZSH:
$ cap |
production staging
$ cap production |
deploy -- Deploy a new release
deploy:bundle -- Bundle
...
Completion code:
#compdef cap
#autoload
# /Users/pablo/.oh-my-zsh/custom/plugins/capistrano_custom/_capistrano_custom
local curcontext="$curcontext" state line ret=1
local -a _configs
_arguments -C \
'1: :->cmds' \
'2:: :->args' && ret=0
_cap_tasks() {
if [[ ! -f .cap_tasks~ ]]; then
echo "\nGenerating .cap_tasks~..." > /dev/stderr
cap -v --tasks | grep '#' | cut -d " " -f 2 > .cap_tasks~
fi
cat .cap_tasks~
}
_cap_stages() {
find config/deploy -name \*.rb | cut -d/ -f3 | sed s:.rb::g
}
case $state in
cmds)
if [[ -d config/deploy ]]; then
compadd `_cap_stages`
else
compadd `_cap_tasks`
fi
ret=0
;;
args)
compadd `_cap_tasks`
ret=0
;;
esac
return ret
The problem:
#compdef cap doesn't work. If I type cap and [TAB] it doesn't execute the completion, but with other words (i.e. shipit) works fine.
Any ideas?
Solution:
cap is really a reserved word and it seems that we can't use it with #compdef cap.
I'm wondering how cap and capistrano completions worked before (maybe an old version of ZSH).
Solution dotfiles code: capistrano_custom
Solution oh-my-zsh/PR: #2471
Both solutions use shipit instead of cap.
$ shipit |
production staging
$ shipit production |
deploy -- Deploy a new release
deploy:bundle -- Bundle
...
Yes, cap is a ZSH builtin. Quoting from zsh docs:
The zsh/cap module is used for manipulating POSIX.1e (POSIX.6)
capability sets. [...]. The builtins in this module are:
cap [ capabilities ] Change the shell’s process capability sets to the
specified capabilities, otherwise display the shell’s current
capabilities.

How can I assign command output to a variable in GNU make target rule?

At a BASH prompt, I can do the following:
~/repo$ HISTORY_LOG=$(git log $(get_old_version)..$(get_new_version)); [[ ! -z ${HISTOR_LOG} ]] && ( echo "Some header"; echo "${HISTORY_LOG}" )
Where git log is demonstrably simplified version of what I actually have.
In a make file I have the following command as part of a target:
$(OUTPUT): $(INPUT)
...
echo "Some header" > $(LOG_FILE)
git log $(shell get_old_version)..$(shell get_new_version) >> $(LOG_FILE)
How can I rewrite the make target to behave like the bash command?
If I do the following line-feeds are being stripped:
$(OUTPUT): $(INPUT)
...
HISTORY_LOG="$(shell git log $(shell get_old_version)..$(shell get_new_version))" ; \
[ -z "$${HISTORY_LOG}" ] && \
true || \
(echo "Some header" ; echo "$${HISTORY_LOG}" )
when run looks like:
~/repo $ make
commit 2b4d87b0e64d129028c1a7a0b46ccde2f42c5e93 Author: Jamie <Jamie#mymail.com> Date: Mon Jun 25 18:46:27 2012 -0400 Issue #468: This sucker's been sped up.
and what I prefer would be:
~/repo $ make
commit 2b4d87b0e64d129028c1a7a0b46ccde2f42c5e93
Author: Jamie <Jamie#mymail.com>
Date: Mon Jun 25 18:46:27 2012 -0400
Issue #468: This sucker's been sped up.
I think the issue is the that make executes commands in /bin/sh and not /bin/bash. Regardless I'm looking for a portable solution if there is one.
Make's shell is eating your newlines. Just stop using it. Instead of $( shell get_old_version ), escape the $:
$$( get_old_version )

Resources