summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2011-07-27 20:13:15 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2011-08-02 15:09:32 +1000
commita0f848fe7206a470e11a0bc87ad739f5aef76137 (patch)
tree152606d02884e8168292fb4712d19711ab00e913
parent162042061be88338b61fc2e788db45e537b00fd3 (diff)
XOrg: Add a parent class for the xorgconf module
The future HAL module will need some of the same functions, might as well just write them once. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r--XOrg/ConfigGenerator.pm108
-rwxr-xr-xXOrg/xorgconf.pm50
2 files changed, 119 insertions, 39 deletions
diff --git a/XOrg/ConfigGenerator.pm b/XOrg/ConfigGenerator.pm
new file mode 100644
index 0000000..4959f2e
--- /dev/null
+++ b/XOrg/ConfigGenerator.pm
@@ -0,0 +1,108 @@
+#!/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::ConfigGenerator - simple parent class for configuration generators
+
+=head1 DESCRIPTION
+
+This module provides a few functions common to configuration generators.
+
+=cut
+
+package XOrg::ConfigGenerator;
+
+use warnings;
+use strict;
+use Carp;
+use Data::Dumper;
+
+=head2 B<$config-E<gt>print>
+
+Return a textual representation of this object.
+=cut
+sub print {
+ return "Override me";
+}
+
+=head2 B<$config-E<gt>save([$filename)>
+
+Save configuration to $filename;
+=cut
+sub save {
+ my $self = shift;
+ my $filename = shift;
+
+ if (defined $self->DEFAULT_CONFIG_FILE) {
+ $filename = $self->DEFAULT_CONFIG_FILE if not defined $filename;
+ }
+
+ open FILE, "> $filename";
+ print FILE $self->print;
+ close FILE;
+}
+
+
+=head2 B<$config-E<gt>add_to_placeholder($placeholder, $replacement)>
+
+Replace $placehoder with $replacement, leaving one placeholder instance for
+the next replacement.
+
+Returns the replaced text.
+=cut
+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 replace {
+ my $self = shift;
+ my $str = shift;
+ my $replace = shift;
+
+ for (keys %{$replace}) {
+ $str =~ s/→$_←/$replace->{$_}/;
+ }
+ return $str;
+}
+
+
+=head2 B<$config-E<gt>purify($string)
+
+Removes any placeholders from the string and returns it.
+=cut
+sub purify {
+ my $self = shift;
+ my $str = shift;
+ $str =~ s/→\w+←//g;
+ return $str;
+}
+
+1;
+
+# vim: set noexpandtab shiftwidth=8 tabstop=8:
diff --git a/XOrg/xorgconf.pm b/XOrg/xorgconf.pm
index 8cf4c7b..987eecc 100755
--- a/XOrg/xorgconf.pm
+++ b/XOrg/xorgconf.pm
@@ -71,10 +71,14 @@ use warnings;
use strict;
use Carp;
use Data::Dumper;
+use XOrg::ConfigGenerator;
+
my $default_config_file = "/etc/X11/xorg.conf";
sub DEFAULT_CONFIG_FILE { return $default_config_file};
+our @ISA = qw(XOrg::ConfigGenerator);
+
my $conf_header = '
# This configuration was generated by XOrg::xorgconf.pm.
';
@@ -126,7 +130,7 @@ sub add_serverlayout {
$identifier = "<default server layout>";
}
- $layout =~ s/→identifier←/$identifier/;
+ $layout = $self->replace($layout, {identifier => $identifier});
for (keys %options) {
$layout = $self->_section_add_option($layout, $_, $options{$_});
@@ -156,8 +160,8 @@ sub add_input_device {
}
my $section = $input_device_template;
- $section =~ s/→identifier←/$identifier/;
- $section =~ s/→driver←/$driver/;
+ $section = $self->replace($section, {identifier => $identifier,
+ driver => $driver});
for (keys %options) {
$section = $self->_section_add_option($section, $_, $options{$_});
}
@@ -166,7 +170,7 @@ sub add_input_device {
if (not defined $self->_sections->{serverlayout}) {
$self->add_serverlayout;
}
- $self->_sections->{serverlayout} = $self->_add_to_placeholder($self->_sections->{serverlayout},
+ $self->_sections->{serverlayout} = $self->add_to_placeholder($self->_sections->{serverlayout},
"input_device",
"InputDevice \"$identifier\"");
}
@@ -179,8 +183,8 @@ sub print {
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};
+ $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}};
@@ -192,45 +196,13 @@ sub print {
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;
+ $str = $self->add_to_placeholder($str, "option", "Option \"$optname\" \"$optval\"");
return $str;
}