summaryrefslogtreecommitdiff
path: root/XOrg/xorgconf.pm
blob: 8cf4c7b37a24a8720a4459ba1d15ce87495443e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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: