Detecting hardware with Ruby

Jamis Buck said:

Here’s a rather off-the-wall question: how would I go about detecting
the presence of external hardware in Ruby? Specifically, sometimes my
laptop is attached to an external monitor, and sometimes it isn’t. Just
before X starts, I’d like to have a Ruby script that runs and checks to
see if the monitor is present, and if it isn’t, chooses a different
server layout from the XF86Config file.

Or is there already a solution to this that I haven’t found?

I solved this problem by using init scripts. I have two init scripts
called setup_home and setup_work. These scripts copy alternate versions
of configuration files depending on whether I’m at work (attached to
external monitor, keyboard, etc.) or at “home” (unattached laptop). I
have one of these two scripts start depending on the runlevel. I can then
choose a run level at boot time, which is very easy with GRUB, and these
scripts will choose the right settings for me.

Granted, this is not quite the same as “just before X starts”, but you can
at least choose at boot time which environment you want to have set up.

I have a more detailed description of this at:

http://forum.libranet.com/viewtopic.php?t=3908&highlight=

If you want to follow the hardware detection route, you can probably write
a simple wrapper around Kudzu.

···


Jason Voegele
“There is an essential core at the center of each man and woman that
remains unaltered no matter how life’s externals may be transformed
or recombined. But it’s smaller than we think.”
– Gene Wolfe, The Book of the Long Sun

Jason Voegele wrote:

I solved this problem by using init scripts. I have two init scripts
called setup_home and setup_work. These scripts copy alternate versions
of configuration files depending on whether I’m at work (attached to
external monitor, keyboard, etc.) or at “home” (unattached laptop). I
have one of these two scripts start depending on the runlevel. I can then
choose a run level at boot time, which is very easy with GRUB, and these
scripts will choose the right settings for me.

Ah, much nicer. I’ve got a custom runlevel already for booting without
X–don’t know why it didn’t occur to me to do this for booting with a
different X server layout. Thanks, Jason! This will do nicely.

I may still try to experiment with the hardware detection route… just
for fun… :wink:

···


Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

ruby -h | ruby -e
‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a <<
r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

Jamis Buck wrote:

Jason Voegele wrote:

I solved this problem by using init scripts. I have two init scripts
called setup_home and setup_work. These scripts copy alternate versions
of configuration files depending on whether I’m at work (attached to
external monitor, keyboard, etc.) or at “home” (unattached laptop). I
have one of these two scripts start depending on the runlevel. I can then
choose a run level at boot time, which is very easy with GRUB, and these
scripts will choose the right settings for me.

Ah, much nicer. I’ve got a custom runlevel already for booting without
X–don’t know why it didn’t occur to me to do this for booting with a
different X server layout. Thanks, Jason! This will do nicely.

I may still try to experiment with the hardware detection route… just
for fun… :wink:

You could always start X in -probeonly mode (or something), then parse
the output of /var/log/XFree86.0.log for the DDC detection stuff.

You could do this with xinit. Pass it a script as its startup program
file which will parse the log file and output the results somewhere.
When the script exits, xinit will bring down the X server cleanly. Then
run another script after xinit finishes to check the output of the
previous script. voila! I think…

···