You are not logged in.

#1 2010-01-03 20:16:53

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

[Solved] Start Caffeine together with MPlayer

Hi,
I would like to automatically disable my XScreenSaver and Gnome Power Management when I start MPlayer.

I found different solution, but the only one that works for me is Caffeine (in the AUR).
This program sits in the tray and disable all screensaver / power managemet apps running when certain programs are started.
However, I don't like to have an app running all the time in my tray since I watch movies not that often: luckily Caffeine also has a -a option that activates it when it started. A good piece of software indeed!

So let's come to the question:
How can I start caffeine -a every time I use mplayer or gnome-mplayer?

Maybe through aliases in .bashrc? Any help / hint would be appreciated!

Thank you! big_smile

Last edited by rent0n (2010-01-04 16:01:07)


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#2 2010-01-03 20:30:24

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Start Caffeine together with MPlayer

alaises would be easy:

alias mplayer='caffeine -a &>/dev/null &; mplayer'

but if you expanded to a function you could do something more complicated like, also kill caffiene when the movie is over:

mplayer() {
  PID=$(pgrep caffeine)
  
  if [ -z "$PID" ]; then
    caffeine -a &>/dev/null &
    PID=$!
  fi

  /usr/bin/mplayer $*
  kill $PID
}

note: untested.

Last edited by brisbin33 (2010-01-03 20:31:49)

Offline

#3 2010-01-03 21:37:47

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Re: [Solved] Start Caffeine together with MPlayer

Thank you brisbin, you are always so helpful. smile

The alias seems not to work, I got a bash syntax error:

┌─[enrico@beetle][~]
└─[$] alias mplayer='caffeine -a &>/dev/null &; mplayer'
┌─[enrico@beetle][~]
└─[$] mplayer 
bash: syntax error near unexpected token `;'

Removing & before ; or replacing &; with && doesn't work because mplayer will start only when caffeine is closed.
Is there a way to mke the alias work as expected?

Regarding the function, how can I use it?
I mean where do I store it and how do I call mplayer?


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#4 2010-01-03 23:22:48

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Start Caffeine together with MPlayer

using the function is exactly the same as the alias, place it in bashrc and call it by name (mplayer in my example).  i never tried an alias as i had written, i suppose you can't put multiliners as an alias (i.e. two commands separated by ';').

edit: and just as with the alias, you can also do all this from CLI for testing purposes.  an example:

//blue/0/~/ tester() {
// echo 'hey look, im a function'
// }
//blue/0/~/ tester
hey look, im a function
//blue/0/~/

('//' is just my PS2 prompt)

Last edited by brisbin33 (2010-01-03 23:24:48)

Offline

#5 2010-01-03 23:52:07

damjan
Member
Registered: 2006-05-30
Posts: 452

Re: [Solved] Start Caffeine together with MPlayer

have you tried the heartbeat option in mplayer??
smth like this:

heartbeat-cmd="gnome-screensaver-command -p"

in ~/.mplayer/config

Last edited by damjan (2010-01-03 23:52:20)

Offline

#6 2010-01-04 10:57:25

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Re: [Solved] Start Caffeine together with MPlayer

@damjan: I tried that but it has a big problem: video freezes every 30 second for a couple of seconds and movies are unwatchable...Moreover it works only for xscreensaver and not for gpm. sad

@brisbin33: The functions work perfectly, thank you.
However, they are called only if I start the programs from the terminal.
Is it possible to have the functions called also when I double click on a avi file that is associated with gnome-mplayer?


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#7 2010-01-04 13:39:19

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Start Caffeine together with MPlayer

rent0n wrote:

Is it possible to have the functions called also when I double click on a avi file that is associated with gnome-mplayer?

in this case i'd make it a script. 

put just the body of the function in a file, make it executable, and place it somewhere in your $PATH.  (if you do name it 'mplayer' make sure it's ahead of /usr/bin in your path or you'll never get to it.). 

once that's setup and working, you can define that script as your default handler for avis, mpgs, or whatever.

Offline

#8 2010-01-04 15:54:22

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Re: [Solved] Start Caffeine together with MPlayer

Great, I've created mplayer-caffeine and gnome-mplayer-caffeine in /usr/bin and they handle well most files.
There is only a problem in files that contain spaces in their names: they are not played at all. sad

Have you got any idea on how to get rid of this and play correctly file with spaces?

Thanks!

EDIT: Found out: simply include $* in double quotes ("$*") works.
Awesome, thank you brisbin33! big_smile

Reporting here /usr/bin/gnome-mplayer-caffeine:

#!/bin/bash

PID=$(pgrep caffeine)
  
if [ -z "$PID" ]; then
  caffeine -a &>/dev/null &
  PID=$!
fi

/usr/bin/gnome-mplayer "$*"
kill $PID

Last edited by rent0n (2010-01-04 16:00:36)


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#9 2010-01-04 15:58:09

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Start Caffeine together with MPlayer

try changing

/usr/bin/mplayer $*

to

/usr/bin/mplayer "$@"

Offline

#10 2010-01-04 16:06:44

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Re: [Solved] Start Caffeine together with MPlayer

What's the difference between "$*" and "$@"?
They seem both to handle well filenames with spaces.


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

#11 2010-01-04 16:13:59

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Start Caffeine together with MPlayer

as was taught to me in IRC

//blue/0/~/ tester() { for arg in "$*"; do echo $arg; done; for arg in "$@"; do echo $arg; done; }
//blue/0/~/ tester "a b c"
a b c
a b c
//blue/0/~/ tester "a b" c
a b c
a b
c
//blue/0/~/ tester a b c
a b c
a
b
c
//blue/0/~/

Offline

#12 2010-01-04 16:25:49

rent0n
Member
From: Italy
Registered: 2009-10-29
Posts: 457
Website

Re: [Solved] Start Caffeine together with MPlayer

So "$*" is more similar to a scalar while "$@" acts as an array.

So the final solution is (reporting here, you never know):

$ sudo touch /usr/bin/gnome-mplayer-caffeine && sudo chmod +x gnome-mplayer-caffeine && sudo nano /usr/bin/gnome-mplayer-caffeine

Add:

#!/bin/bash

PID=$(pgrep caffeine)
  
if [ -z "$PID" ]; then
  caffeine -a &>/dev/null &
  PID=$!
fi

/usr/bin/gnome-mplayer "$@"
kill $PID

Save and close.
Now associate .avi files with gnome-mplayer-caffeine and it's done!


rent0n@deviantART | rent0n@bitbucket | rent0n@identi.ca | LRU #337812
aspire: Acer Aspire 5920 Arch Linux x86_64 | beetle: Gericom Beetle G733 Arch Linux i686

Offline

Board footer

Powered by FluxBB