Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
trying to reinvent the wheel so to speak
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
LinuxDolt
Tux's lil' helper
Tux's lil' helper


Joined: 05 May 2003
Posts: 104

PostPosted: Sat Jun 07, 2003 9:42 pm    Post subject: trying to reinvent the wheel so to speak Reply with quote

i'm trying to make a bash script that will capture F-Key presses and then perform an action depending on what F-Key was pressed... ie: you press F1 and it opens xman or varient...

so i was wondering if anyone could help me in this endeavor, i have been reading the Advanced Bash-Scripting Guide to try and figure out how to do this, but reading that is like reading Japanese, it is a slow and painful process for someone brand new to linux and especially to BASH...

and yes, the reason i say i am trying to reinvent the wheel is because i realize that the major de's, wm's, and other packages already have support for this using C(++) and either qt/gtk+/other libraries, but i really think it'd be nice to have a shot at doing a script for it, and also none of the software out there does quite what i want...
Back to top
View user's profile Send private message
Pythonhead
Developer
Developer


Joined: 16 Dec 2002
Posts: 1801
Location: Redondo Beach, Republic of Calif.

PostPosted: Sat Jun 07, 2003 10:45 pm    Post subject: Reply with quote

Bash has the 'read' command, but it only works if you press enter.
So, you really can't do it easily, unless you want to press F1 then enter.

Here is a hack:

Code:

stty raw
F = `head --bytes=3`
echo $F


I used 3 because an F-key actually returns 3 bytes.

Do 'tty sane' to get your terminal back in normal mode.

I call this a hack because it waits for 3 and only 3 characters, so if you accidently press anything but an F-key, its going to wait for 2 more characters.

There might be a very short C program out there that does what you want that you can use in your Bash script.

So, run that code above, figure out what each F-key returns, then use the CASE statement to run a program for each F-key value.

Quote:
and yes, the reason i say i am trying to reinvent the wheel is because i realize that the major de's, wm's, and other packages already have support for this using C(++) and either qt/gtk+/other libraries, but i really think it'd be nice to have a shot at doing a script for it, and also none of the software out there does quite what i want...


I hope you realize a shell script is only going to capture an F-key when the xterm its running in is focused.
Back to top
View user's profile Send private message
aja
l33t
l33t


Joined: 26 Aug 2002
Posts: 705
Location: Edmonton, Canada

PostPosted: Sun Jun 08, 2003 3:01 am    Post subject: Reply with quote

I don't know about bash, but in zsh, you can bind function keys in your .zshrc with calls like:

Code:

  bindkey '\e[OP'  where-is           # F1
  bindkey '\e[O2P' universal-argument # shift F1


I believe you can also do bindings in the file /etc/inputrc .

Code:

man readline


Should give you some examples (readline is what most shells use for their interface in Linux).
Back to top
View user's profile Send private message
LinuxDolt
Tux's lil' helper
Tux's lil' helper


Joined: 05 May 2003
Posts: 104

PostPosted: Sun Jun 08, 2003 7:48 pm    Post subject: Reply with quote

Pythonhead wrote:
Bash has the 'read' command, but it only works if you press enter.
So, you really can't do it easily, unless you want to press F1 then enter.

yah, i kinda figured that out already
Pythonhead wrote:

Here is a hack:

Code:

stty raw
F = `head --bytes=3`
echo $F


I used 3 because an F-key actually returns 3 bytes.

Do 'tty sane' to get your terminal back in normal mode.

I call this a hack because it waits for 3 and only 3 characters, so if you accidently press anything but an F-key, its going to wait for 2 more characters.

hmmm... small problem, i have mapped all my extra keys to F-Keys ranging up to F30+, once you hit a certain number on the F-Key list it no longer returns three, but four, and even futher it starts using 5... i do not believe my list has any with 5, but i do have both 3's and 4's in there...
Pythonhead wrote:

There might be a very short C program out there that does what you want that you can use in your Bash script.

i'll have to look for that...
Pythonhead wrote:

So, run that code above, figure out what each F-key returns, then use the CASE statement to run a program for each F-key value.

I hope you realize a shell script is only going to capture an F-key when the xterm its running in is focused.

ok, and yes, i do realize i have to have the terminal i am using in focus for this to work. that is a part of my plan, i want the keys to be able to act as normal F-Keys except when i want to alter their behavior, and then i just bring my F-Key redirection terminal into focus... make sense?

another problem, how can i grab current volume and muted states on the Master channel from ALSA? the guide i am using does not talk about alsa... is there some kind of thing alsa uses that i could grab stuff from?
Back to top
View user's profile Send private message
vericgar
Retired Dev
Retired Dev


Joined: 13 Dec 2002
Posts: 79
Location: Spokane, WA

PostPosted: Sun Jun 08, 2003 10:01 pm    Post subject: Reply with quote

LinuxDolt wrote:
another problem, how can i grab current volume and muted states on the Master channel from ALSA? the guide i am using does not talk about alsa... is there some kind of thing alsa uses that i could grab stuff from?


you might want to look into amixer (a part of alsa-utils, so if you have alsa setup you should have it)

a tidbit of sample output:

Code:

Simple mixer control 'Master',0
  Capabilities: pvolume pswitch cswitch cswitch-joined cswitch-exclusive
  Capture exclusive group: 0
  Playback channels: Front Left - Front Right
  Capture channels: Mono
  Limits: Playback 0 - 63
  Mono: Capture [off]
  Front Left: Playback 38 [60%] [on]
  Front Right: Playback 38 [60%] [on]


LinuxDolt wrote:

hmmm... small problem, i have mapped all my extra keys to F-Keys ranging up to F30+, once you hit a certain number on the F-Key list it no longer returns three, but four, and even futher it starts using 5... i do not believe my list has any with 5, but i do have both 3's and 4's in there...


cold get the first 3, use the switch as suggested above and if the 3 match one of the 4-byte keys, read the next byte and do a switch on that... I've never done anything with stty raw so I don't know if that would work or not, jsut an idea for you to look into.
_________________
+~+ Sometimes a good ole loving kick is all it needs +~+
Back to top
View user's profile Send private message
LinuxDolt
Tux's lil' helper
Tux's lil' helper


Joined: 05 May 2003
Posts: 104

PostPosted: Sun Jun 08, 2003 10:47 pm    Post subject: Reply with quote

ok, i have changed my way of going about this, i am now going to use xbindkeys and just have xbindkeys run scripts for each of the commands... anyone know how to pass commands to xosd from a bash script?

as for that amixer stuff, anyone have any good bash text editing commands faqs/howtos/guides i could refer to to rip out just the portion i want?
Back to top
View user's profile Send private message
Pythonhead
Developer
Developer


Joined: 16 Dec 2002
Posts: 1801
Location: Redondo Beach, Republic of Calif.

PostPosted: Sun Jun 08, 2003 11:31 pm    Post subject: Reply with quote

xosd is a C library, so you're not going to be able to access if directly from Bash. I think there is a sample program with it called osd_cat that might work. If not, you need a higher level language interface to it. Probably Python would be the easiest:

http://repose.cx/pyosd/index.html
Back to top
View user's profile Send private message
LinuxDolt
Tux's lil' helper
Tux's lil' helper


Joined: 05 May 2003
Posts: 104

PostPosted: Sun Jun 08, 2003 11:57 pm    Post subject: Reply with quote

thank you python, but you kinda answered with it exactly as i was figuring that out myself... (i got the wise idea to do ls searching for it, ie ls /usr/bin/*osd* ls /usr/sbin/*osd* ls /bin/*osd* etc on different lines, it came out with /usr/bin/osd_cat /usr/bin/xosd-config -- i brought up the man pages for them both, and osd_cat is the one i want)

now i just need to find a good guide on text handling in bash... the guide i am using kinda half-asses that topic...
Back to top
View user's profile Send private message
LinuxDolt
Tux's lil' helper
Tux's lil' helper


Joined: 05 May 2003
Posts: 104

PostPosted: Mon Jun 09, 2003 6:28 pm    Post subject: Reply with quote

the project is almost complete! i have, as i stated earlier, decided to go with making a library of scripts for use with xbindkeys and xosd. the default library will come with a default customized .xbindkeysrc file to be copied to your home dir, scripts for starting a word processor, spreadsheet, webcam, file browser (like konquorer), email app, web browser (MozillaFirebird), calendar (iCal by default), Help browser, Control Center/Panel App(let), File searcher (kfind), graphics editor (gimp), and starting any miscellanious Internet/Networking prog of your choice (kaddressbook by default). there'll also be scripts for MUTE, VolUp, and VolDown toggle/incrementers (uses ALSA by default) and for controlling your favorite (xmms by default) multimedia app with a prev, stop, play-pause, and next script.

due to the nature of this library it is EXTREMELY low-overhead, it only uses what overhead it takes to use xbindkeys, xosd, and the apps you have it scripted for. it is also highly configurable and most inordinately simple. as long as you have a minimal knowledge of bash scripting (i didn't use anything particularly fancy) and know how xbindkeys works (also nothing difficult) you can figure out how to edit my scripts to your needs -- or even make new ones using the same general practices. i explain much in the comments of the scripts and the config file...

if anyone would like to try it out, lemme know your email and i'll send you a binary xMEAK-0.1.tar.gz

remember, in order to use this, you must have x-windows, xosd, and xbindkeys... i do not believe there are any other requirements, except that you need to edit the scripts to your needs...
my email is slicersv@buckeye-express.com
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming 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