Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
trigger script by gamepad button
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
Zubziro
Apprentice
Apprentice


Joined: 21 Sep 2005
Posts: 248
Location: Sweden

PostPosted: Sat Apr 02, 2016 5:13 pm    Post subject: trigger script by gamepad button Reply with quote

Hi

I'm in need to trigger scripts by different keys on usb gamepad device.
Key mapper will probably not work because screen is locked by screenlocker.

Any advice on how to catch button press event and trigger custom script?

Thanks!
Back to top
View user's profile Send private message
Zubziro
Apprentice
Apprentice


Joined: 21 Sep 2005
Posts: 248
Location: Sweden

PostPosted: Thu Apr 07, 2016 7:35 pm    Post subject: Reply with quote

Hi again...

No replays... so I was forced to solve it by myself:

Code:
emerge joystick


my script 'jcap':
Code:

#!/bin/bash

function getDesktopState {
total=$(wmctrl -d | wc -l)
current=$(wmctrl -d | sed -n 's/^\([0-9]\+\) *\*.*/\1/p')

if [ -z "$total" ] || [ -z "$current" ]; then
  echo 1>&2 "$0: Could not obtain workspace information!"
  exit 2
fi
}

function desktopLeft {
getDesktopState
target=$(($current-1))
if [ "$target" = -1 ]; then
  target=$(($total-1));
fi
wmctrl -s $target
}


function desktopRight {
getDesktopState
target=$(($current+1))
if [ "$target" = "$total" ]; then
  target=0;
fi
wmctrl -s $target
}

function main {
while read data; do
  if [[ $data =~ ^Event\:.type.2,.*number.0,.value.-32767 ]] ; then desktopLeft; fi
  if [[ $data =~ ^Event\:.type.2,.*number.0,.value.32767 ]] ; then desktopRight; fi
done
}

main


Now execution:
Code:
jstest --event /dev/input/js0 | ./jcap


You will probably need to modify it to suit your gamepad, run jstest --event /dev/input/js0 and press buttons of interest then modify regex in main function.
This script gives me possibility to rotate virtual desktops on my surveillance server while screenlock (alock) is ON

/bye
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Thu Apr 07, 2016 11:30 pm    Post subject: Reply with quote

Zubziro wrote:
No replays... so I was forced to solve it by myself:

Zubziro ... that's just our way of providing you a solution ... and it works! ;)

Zubziro wrote:
jcap:
total=$(wmctrl -d | wc -l)
current=$(wmctrl -d | sed -n 's/^\([0-9]\+\) *\*.*/\1/p')

perhaps like this ...

Code:
read -r current total <<< "$(wmctrl -d | awk -v ORS=" " '{if ($2 == "*") print $1};END {print $NF}')"

Also, just for anyone who might want to reuse the script, wmctrl is x11-misc/wmctrl.

Zubziro wrote:
This script gives me possibility to rotate virtual desktops on my surveillance server while screenlock (alock) is ON

... that's me in the ski mask waving bye ... khay
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum