Enhanced.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 174 CC 24 SCAR 36.5
CC
24
Cov
0%
pack_mask
72.0
pack_enum
56.0
unpack_mask
30.0
CRAP 38.3
1 package X11::Protocol::Enhanced;
2 use X11::Protocol qw(pad padding padded make_num_hash);
3 use Carp;
4 use strict;
5 no warnings;
6 use vars '$VERSION';
7 $VERSION = 0.05;
8  
9 =head1 NAME
10  
11 X11::Protocol::Enhanced - bit mask enhancements for X11::Protocol
12  
13 =head1 SYNOPSIS
14  
15 use X11::Protocol;
16 use X11::Protocol::Enhanced;
17  
18 # just to define 'Rotation' type:
19 my $X = X11::Protocol->new();
20 $X->init_extension('RANDR') or die "Cannot initialize RANDR!";
21  
22 my $mask = $X->pack_mask(Rotation=>qw(Rotate_0 Reflect_X));
23 my %mask = $X->unpack_mask(Rotation=>$mask);
24 print "Bits set are: ",join(',',keys %mask),"\n";
25  
26 =head1 DESCRIPTION
27  
28 This module is used by a number of protocol extensions to enhance the
29 enumeration and bit mask handling of the L<X11::Protocol(3pm)> object.
30 It does this by adding or overriding a number of methods on the
31 L<X11::Protocol(3pm)> module when it loads.
32  
33 =cut
34  
35 =head1 METHODS
36  
37 L<X11::Protocol(3pm)> does not provide a way to pack and unpack masks using
38 symbolic constants, except for event masks.
39 B<X11::Protocol::Ext::KEYBOARD> uses so many masks that either we needed
40 to define bitmap mask constants in perl, or we needed a way to pack and
41 unpack masks. To provide this capability, the
42 B<X11::Protocol::Enhanced> module adds the following methods to the
43 C<X11::Protocol(3pm)> object:
44  
45 =over
46  
47 =item $mask = $X->B<pack_mask>(I<$typename>,\@constants)
48  
49 Where, I<$typename> is a symbolic constant type name like C<Bool> or
50 C<XkbControl>, and C<\@constants> is a list (array ref) of symbolic
51 constants defined for I<$typename> or bit numbers. B<pack_mask> returns
52 a numeric value representing the mask with the appropriate bits set.
53 When C<\@constants> is a scalar value, the value is simply returned.
54  
55 This function is similar to B<pack_event_mask> with the exception that
56 it can be used where I<$typename> is not equal to 'EventMask'.
57  
58 B<pack_mask> is called by the request functions in this module on may of
59 the arguments passed into the request. Therefore, the arguments passed
60 to the request can either be the numeric bit mask, or can be an array or
61 hash of bit values.
62  
63 =cut
64 sub pack_mask {
65 my $self = shift;
66 my($typename,$x) = @_;
67 return $x unless ref($x) eq 'ARRAY';
68 my $type = $self->{const_num}{$typename};
69 $type = $self->{ext_const_num}{$typename} unless $type;
70 if (not $type) {
71 if ($self->{const}{$typename}) {
72 $type = $self->{const_num}{$typename} =
73 {make_num_hash($self->{const}{$typename})};
74 }
75 elsif ($self->{ext_const}{$typename}) {
76 $type = $self->{ext_const_num}{$typename} =
77 {make_num_hash($self->{ext_const}{$typename})};
78 }
79 }
80 my($i, $mask);
81 $mask = 0;
82 for $i (@$x) {
83 $i = $type->{$i} if $type and exists $type->{$i};
84 if ($i =~ m{^\d+$} and $i<32) {
85 $mask |= 1<<$i;
86 }
87 }
88 return $mask;
89 }
90  
91 *X11::Protocol::pack_mask =
92 \&X11::Protocol::Enhanced::pack_mask;
93  
94 =item %mask = $X->B<unpack_mask>(I<$typename>,I<$mask>)
95  
96 Where, I<$typename> is a symbolic constant type name like C<Bool> or
97 C<XkbBoolCtrl>, and I<$mask> is a numerical representation of the bit
98 mask. The bit positions in the number are converted into a HASH where
99 the keys of the hash are the symbolic names of the bits set to 1 or the
100 bit number when no symbolic name for the bit appears in I<$typename>.
101 The value associated with the key is always 1. The HASH is returned.
102  
103 If you would prefer the ARRAY form as was passed to B<pack_mask>, this
104 trick will do:
105  
106 %mask = $X->unpack_mask($typename,$mask);
107 @mask = keys %mask;
108  
109 Masks are not automatically unpacked by the event routines, nor for
110 responses to requests. You will need to unpack returned fields
111 yourself using this function. Refer to the specification to see which
112 symbolic constant I<$typename> to use when unpacking.
113  
114 =cut
115 sub unpack_mask($$) {
116 my $self = shift;
117 my($typename,$mask) = @_;
118 my $type = $self->{const}{$typename};
119 $type = $self->{ext_const}{$typename} unless $type;
120 carp "Could not find typename '$typename'" unless $type;
121 my $i = 0;
122 my %h = ();
123 while ($mask) {
124 if ($mask & 0x1) {
125 if ($type and $type->[$i]) {
126 $h{$type->[$i]} = 1;
127 } else {
128 $h{$i} = 1;
129 }
130 }
131 $i++;
132 $mask >>= 1;
133 }
134 return %h;
135 }
136  
137 *X11::Protocol::unpack_mask =
138 \&X11::Protocol::Enhanced::unpack_mask;
139  
140 =item $mask = $X->B<interp_mask>(I<$typename>,I<$mask>)
141  
142 Like B<unpack_mask>, but only interprets the mask when the
143 C<do_interp_mask> flag is set on C<$X>.
144  
145 =cut
146 sub interp_mask($$) {
147 my $self = shift;
148 my ($typename,$mask) = @_;
149 return $self->unpack_mask(@_) if $self->{do_interp_mask};
150 return $mask;
151 }
152  
153 *X11::Protocol::interp_mask =
154 \&X11::Protocol::Enhanced::interp_mask;
155  
156 =item $num = $X->B<pack_enum>(I<$typename>,I<$nameornum>)
157  
158 =item $num = $X->B<num>(I<$typename>,I<$nameornum>)
159  
160 Where I<$typename> is a symbolic constant type name like C<Bool> or
161 C<XkbBoolCtrl>, and I<$nameornum> is a symbolic constant name under
162 I<$typename> or a simple number. The number corresponding to the
163 symbolic constant name is returned.
164  
165 B<X11::Protocol::Enhanced> also overrides the L<X11::Protocol(3pm)>
166 B<num> method with this method. (The L<X11::Protocol(3pm)> B<num>
167 method cannot handle special names for only a few values.>
168  
169 =cut
170 sub pack_enum($$) {
171 my $self = shift;
172 my ($typename,$x) = @_;
173 my $type = $self->{const_num}{$typename};
174 $type = $self->{ext_const_num}{$typename} unless $type;
175 if (not $type) {
176 if ($self->{const}{$typename}) {
177 $type = $self->{const_num}{$typename} =
178 {make_num_hash($self->{const}{$typename})};
179 }
180 elsif ($self->{ext_const}{$typename}) {
181 $type = $self->{ext_const_num}{$typename} =
182 {make_num_hash($self->{ext_const}{$typename})};
183 }
184 }
185 return $self->{const_num}{$typename}{$x}
186 if exists $self->{const_num}{$typename}{$x};
187 return $self->{ext_const_num}{$typename}{$x}
188 if exists $self->{ext_const_num}{$typename}{$x};
189 return $x;
190 }
191  
192 *X11::Protocol::pack_enum =
193 \&X11::Protocol::Enhanced::pack_enum;
194  
195 *X11::Protocol::num =
196 \&X11::Protocol::Enhanced::pack_enum;
197  
198 =item $nameornum = $X->B<unpack_enum>(I<$typename>,I<$num>)
199  
200 =item $nameornum = $X->B<do_interp>(I<$typename>,I<$num>)
201  
202 Where I<$typename> is a symbolic constnat type name like C<Bool> or
203 C<XkbControl>, and I<$num> is a simple number. The name corresponding
204 ot the symbolic constant is returned if it is defined, and the number is
205 returned otherwise.
206  
207 B<X11::Protocol::Enhanced> also overrides the L<X11::Protocol(3pm)>
208 B<do_interp> method with this method. (The L<X11::Protocol(3pm)>
209 B<do_interp> method cannot handle special names for only a few values.>
210  
211 =cut
212 sub unpack_enum {
213 my $self = shift;
214 my ($typename,$num) = @_;
215 if ($self->{do_interp}) {
216 return $num if $num < 0;
217 if ($self->{const}{$typename} and
218 defined($self->{const}{$typename}[$num])) {
219 $num = $self->{const}{$typename}[$num];
220 }
221 elsif ($self->{ext_const}{$typename} and
222 defined($self->{ext_const}{$typename}[$num])) {
223 $num = $self->{ext_const}{$typename}[$num];
224 }
225 }
226 return $num;
227 }
228  
229 *X11::Protocol::unpack_enum =
230 \&X11::Protocol::Enhanced::unpack_enum;
231  
232 *X11::Protocol::do_interp =
233 \&X11::Protocol::Enhanced::unpack_enum;
234  
235 =item $nameornum = $X->B<interp_enum>(I<$typename>,I<$num>)
236  
237 =item $nameornum = $X->B<interp>(I<$typename>,I<$num>)
238  
239 Like B<unpack_enum>, but only unpacks the enumeration when the
240 C<do_interp> flag is set on C<$X>.
241  
242 B<X11::Protocol::Enhanced> also overrides the L<X11::Protocol(3pm)>
243 B<interp> method with this method. (The L<X11::Protocol(3pm)> method
244 cannot handle special names for only a few values.)
245  
246 =cut
247 sub interp_enum {
248 my $self = shift;
249 my ($typename,$num) = @_;
250 return $self->unpack_enum(@_) if $self->{do_interp};
251 return $num;
252 }
253  
254 *X11::Protocol::interp =
255 \&X11::Protocol::Enhanced::interp_enum;
256  
257 =back
258  
259 =cut
260  
261 1;
262  
263 =head1 AUTHOR
264  
265 Brian Bidulock <bidulock@cpan.org>
266  
267 =head1 SEE ALSO
268  
269 L<X11::Protocol(3pm)>,
270 L<X11::Protocol::Ext::XKEYBOARD(3pm)>,
271 L<X11::Protocol::Ext::RANDR(3pm)>,
272 L<X11::Protocol::Ext::SYNC(3pm)>.
273  
274 =cut
275  
276 # vim: sw=4 tw=72
277  
278  
279