diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2011-07-27 11:26:05 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2011-08-02 15:09:32 +1000 |
commit | a4ded86b700c3cc4c2c253b0fb970443b8ac39e4 (patch) | |
tree | c6e8754bec366045a31deef2d877376a8df99483 /XOrg | |
parent | 24325773ecdb4ef1e881e5b548d5a23151768adf (diff) |
XOrg: Add a xorg.conf generator.
Only works for selected input device features though.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'XOrg')
-rwxr-xr-x | XOrg/xorgconf.pm | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/XOrg/xorgconf.pm b/XOrg/xorgconf.pm new file mode 100755 index 0000000..8cf4c7b --- /dev/null +++ b/XOrg/xorgconf.pm @@ -0,0 +1,239 @@ +#!/usr/bin/perl + +# Copyright © 2011 by Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# Authors: Peter Hutterer <peter.hutterer@redhat.com> + + +=head1 NAME + +XOrg::xorgconf - simple xorg.conf generation module + +=head1 SYNOPSIS + + use XOrg::xorgconf; + + my $conf = XOrg::xorg.conf->new(); + + # add a server layout with an identifier and an option list + $conf->add_serverlayout("my layout", { "AutoAddDevices" => "off"}); + + # add a named input device with driver evdev + $conf->add_input_device("my keyboard", "evdev"); + + # add a named input device with an option list too + $conf->add_input_device("my mouse", "evdev", { "Emulate3Buttons" => "on"}); + + # add a named input device with no options, _not_ referenced in the + # layout + $conf->add_input_device("unref device", "evdev", {}, 0); + + # Print configuration + print $conf->print; + + # Save configuration in the default file + $conf->save; + + # find out where we just saved to + print $conf->DEFAULT_CONFIG_FILE; + + # save somewhere else + $conf->save("/tmp/xorg.conf");; + +=head1 BUGS + +This module currently only supports the necessary stuff to get input devices +up and running. Output devices are not supported. + +=head1 FILES + +The default configuration file is /etc/X11/xorg.conf + +=cut + +package XOrg::xorgconf; + +use warnings; +use strict; +use Carp; +use Data::Dumper; +my $default_config_file = "/etc/X11/xorg.conf"; + +sub DEFAULT_CONFIG_FILE { return $default_config_file}; + +my $conf_header = ' +# This configuration was generated by XOrg::xorgconf.pm. +'; + +my $layout_template = ' +Section "ServerLayout" + Identifier "→identifier←" + →input_device← + →option← +EndSection +'; + +my $input_device_template = ' +Section "InputDevice" + Identifier "→identifier←" + Driver "→driver←" + →option← +EndSection +'; + +# Instance methods +sub new { + my $class = shift; + my $self = {@_}; + bless ($self, $class); + + my %sections = ( + header => $conf_header, + ); + + $self->{sections} = \%sections; + return $self; +} + +sub _sections { + my $self = shift; + return $self->{sections}; +} + +sub add_serverlayout { + my $self = shift; + my $identifier = shift; + my $optref = shift; + my $layout = $layout_template; + + my %options = %{$optref} if defined $optref; + + if (not $identifier) { + $identifier = "<default server layout>"; + } + + $layout =~ s/→identifier←/$identifier/; + + for (keys %options) { + $layout = $self->_section_add_option($layout, $_, $options{$_}); + } + + $self->_sections->{serverlayout} = $layout; +} + + +=head2 B<$conf-E<gt>add_input_device($identifier, $driver, [\%options)], [$referenced]> + +Add an InputDevice section with the given $identifier and $driver and the given +options (if any). If $referenced is true (default), the device is added to the +ServerLayout section as well. +=cut +sub add_input_device { + my $self = shift; + my $identifier = shift; + my $driver = shift; + my $optref = shift; + my $referenced = shift; + + my %options = %{$optref} if defined $optref; + $referenced = 1 if not defined $referenced; + + if ($referenced) { + } + + my $section = $input_device_template; + $section =~ s/→identifier←/$identifier/; + $section =~ s/→driver←/$driver/; + for (keys %options) { + $section = $self->_section_add_option($section, $_, $options{$_}); + } + + if ($referenced) { + if (not defined $self->_sections->{serverlayout}) { + $self->add_serverlayout; + } + $self->_sections->{serverlayout} = $self->_add_to_placeholder($self->_sections->{serverlayout}, + "input_device", + "InputDevice \"$identifier\""); + } + + push (@{$self->_sections->{inputdevices}}, $section); +} + +sub print { + my $self = shift; + my $str = $self->_sections->{header}; + my @devices; + + $str .= $self->_purify($self->_sections->{serverlayout}) if defined $self->_sections->{serverlayout}; + $str .= $self->_purify($self->_sections->{serverflags}) if defined $self->_sections->{serverflags}; + + if ($self->_sections->{inputdevices}) { + @devices = @{$self->_sections->{inputdevices}}; + for (@devices) { + $str .= $self->purify($_); + } + } + + return $str ."\n"; +} + +sub save { + my $self = shift; + my $filename = shift; + + $filename = $self->DEFAULT_CONFIG_FILE if not defined $filename; + + open XCONF, "> $filename"; + print XCONF $self->print; + close XCONF; +} + + +# Internal methods +sub _add_to_placeholder { + my $self = shift; + my $str = shift; + my $placeholder = shift; + my $replacement = shift; + + $str =~ s/(→$placeholder←)/$replacement\n $1/; + + return $str; +} + +sub _section_add_option { + my $self = shift; + my $str = shift; + my $optname = shift; + my $optval = shift; + + $str = $self->_add_to_placeholder($str, "option", "Option \"$optname\" \"$optval\""); + return $str; +} + +# Called before printing each section, removes any leftover placeholders. +sub _purify { + my $self = shift; + my $str = shift; + $str =~ s/→\w+←//g; + return $str; +} + +1; + +# vim: set noexpandtab shiftwidth=8 tabstop=8: |