Getting keyboard input with ruby while forking mplayer


#1

Hi

I’m trying to create a simple ruby script, which forks mplayer and
afterwards it stays in a while loop which blocks on gets (this is to
get keyboard input). I start this script from a terminal (linux). When
i start it, it forks mplayer and the focus is on mplayer now. If i
press some keys on my keyboard these are send to mplayer.

Is there a solution to get focus on the terminal/script again (without
user interaction) after i started mplayer? Or another solution to get
keyboard commands into my script?

For some more information i included my program. The client is the one
i’m talking about. The server sends the data via netcat to the client.
It is the beginning of something you might call a pvr (personal video
recorder).

Thanks in advance,

By the way this is my first ruby program (used to program c++/c#)

== pvr_client.rb ==
#!/usr/bin/ruby -w

require ‘soap/rpc/driver’

class UI
def initialize
@driver = SOAP::RPC::Driver.new(‘http://localhost:8888/’,
‘urn:mySoapServer’)
@driver.add_method(‘startstream’, ‘toip’);
fork do
exec(‘nc -l -p 5000 | mplayer -’)
end
@driver.startstream(‘hoi’)
end

def getInput
    while( command = gets ) do
        puts command
    end
end

end

ui = UI.new
ui.getInput

== pvr_server.rb ==
#!/usr/bin/ruby -w
require ‘soap/rpc/standaloneServer’

class MyServer < SOAP::RPC::StandaloneServer
def initialize(*args)
super
add_method(self, ‘startstream’, ‘toip’);
end

def startstream(toip)
    fork do
        puts 'start streaming'
        exec('cat piet.mpg | nc localhost 5000')
    end
end

end

server = MyServer.new(‘CoolServer’, ‘urn:mySoapServer’, ‘localhost’,
8888)
trap(‘INT’) { server.shutdown }
server.start


#2

On 19/04/07, removed_email_address@domain.invalid removed_email_address@domain.invalid wrote:

keyboard commands into my script?

In X the newly started program usually gets keyboard focus unless you
configure your windowmanager do do something special. Of course, the
steps vary wildly between different windowmanagers, and may would
probably not allow such thing at all.

However, if you started the script from another machine through ssh or
on another X server (you should be able to put in another videocard,
and start a second X server) you should get the input to your script.

Thanks

Michal


#3

On Apr 21, 12:23 pm, “Michal S.” removed_email_address@domain.invalid wrote:

Is there a solution to get focus on the terminal/script again (without
and start a second X server) you should get the input to your script.
Is there a way to get all keyboard input with my ruby script.


#4

On 21/04/07, removed_email_address@domain.invalid removed_email_address@domain.invalid wrote:

on another X server (you should be able to put in another videocard,
and start a second X server) you should get the input to your script.

Is there a way to get all keyboard input with my ruby script.

I guess there is no portable way (across different X desktops) except
keyboard grab. However, keyboard grab is quite problematic. If your
script breaks and does not release the grab nothing in X (not even the
windowmanager) can use keyboard until you kill it. You will need some
Xlib bindings or X11 protocol library like RubyX11 for that (you need
to talk to the X server). And you will probably have to extend
whatever you use to support such exotic feature.
Using input grab (especially long) is considered quite unfriendly. It
looks like firefox uses input grab for DnD and likes to lock up with
input grabbed :-S

However, mplayer has some feature that allows you to specify a window
id into which the video is rendered (it is used for mplayer mozilla
plugin). That should avoid these problems. Have it render in the
terminal window or open a window of your own that does not receive
focus (I beleive there is a flag for that, and since it is your window
you can get the input anyway).

Thanks

Michal