lib/WL/Base.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 57 CC 5 SCAR 15.9
CC
5
Cov
0%
new
6.0
AUTOLOAD
6.0
nv2fixed
6.0
CRAP 4.9
1 package WL::Base;
2  
3 =head1 NAME
4  
5 WL::Base - Base class for Wayland objects
6  
7 =head1 SYNOPSIS
8  
9 # Obtain an object instance
10 my $display = $conn->get_display ();
11  
12 # Attach callbacks for events
13 $display->{'WL::wl_display::error'} = sub {
14 my ($self, $object, $code, $message) = @_;
15 die $message;
16 };
17  
18 # Issue a request
19 my $registry = $display->get_registry ();
20  
21 # Rebless to attach event handlers
22 bless $registry, 'MyRegistry';
23  
24 package MyRegistry;
25 use base qw/WL::wl_registry/;
26  
27 # Implementation of an event handler
28 sub global
29 {
30 my ($self, $id, $class, $version) = @_;
31 warn "Object $id is of class $class version $version";
32 }
33  
34 =head1 DESCRIPTION
35  
36 B<WL::Base> is a base class for Wayland protocol objects. It provides support
37 routines for common Wayland object actions and helper routines. It should not
38 be used directly.
39  
40 Please consider this an alpha quality code, whose API can change at any time,
41 until we reach version 1.0.
42  
43 =cut
44  
45 use strict;
46 use warnings;
47  
48 =head1 METHODS
49  
50 =over 4
51  
52 =item B<new> CONNECTION [ID]
53  
54 Create a new object instance. This should not be used directly, as
55 L<WL::Connection> creates objects whenever needed.
56  
57 First argument is the L<WL::Connection> instance while second, optional is the
58 object number. It only makes sense for remote objects, local objects get their
59 number allocated automatically.
60  
61 =cut
62  
63 sub new
64 {
65 my $class = shift;
66 my $conn = shift;
67 my $id = shift;
68  
69 my $self = {};
70  
71 if ($id) {
72 # Remote object
73 $self->{id} = $id;
74 } else {
75 # New local object, allocate id
76 push @{$conn->{objs}}, $self;
77 $self->{id} = scalar @{$conn->{objs}} - 1;
78 }
79 $self->{conn} = $conn;
80  
81 return bless $self, $class;
82 }
83  
84  
85 =item B<call> OPCODE PAYLOAD [FILE]
86  
87 Send a request. The payload is already serialized request body without opcode
88 and size part.
89  
90 Optional file is an open file handle that would get passed with the request as
91 anciliary data.
92  
93 =cut
94  
95 sub call
96 {
97 my $self = shift;
98 my $opcode = shift;
99 my $payload = shift;
100 my $file = shift;
101  
102 my $conn = $self->{conn};
103 my $id = $self->{id};
104 $conn->send_request ($id, $opcode, $payload, $file);
105 }
106  
107 =item B<AUTOLOAD> [...]
108  
109 If an attempt was made to call a method that does not exist (this happend upon
110 event receipt), autoloader dispatches to a function reference if a property
111 named like the event exists.
112  
113 This makes it possible to attach event handlers without subclassing (it is
114 still possible to implement event handlers in subclasses though).
115  
116 =cut
117  
118 our $AUTOLOAD;
119 sub AUTOLOAD
120 {
121 my $self = shift;
122 die "No $AUTOLOAD" unless exists $self->{$AUTOLOAD};
123 $self->{$AUTOLOAD}($self, @_);
124 }
125  
126 =back
127  
128 =head1 FUNCTIONS
129  
130 =over 4
131  
132 =item B<nv2fixed> NUMBER
133  
134 This is a helper routine that serializes a Perl numeric value to a Wayland 24.8
135 fixed format number.
136  
137 =cut
138  
139 sub nv2fixed
140 {
141 my $nv = shift;
142  
143 my $sign = $nv < 0;
144 my $whole = abs(int($nv)) & 0x7fffff;
145 my $decimal = abs($nv-int($nv)) * 1000;
146 $decimal /= 10 if $decimal > 0xff;
147  
148 return $sign << 31 | $whole << 8 | $decimal
149 }
150  
151 =item B<fixed2nv> FIXED
152  
153 This is a helper routine that deserializes a Wayland 24.8 fixed format number
154 to a Perl numeric value.
155  
156 =cut
157  
158 sub fixed2nv
159 {
160 my $fixed = shift;
161  
162 sprintf ("%s%d.%d", $fixed & 0x80000000 ? '-' : '',
163 $fixed >> 8 & 0x7fffff, $fixed & 0xff);
164 }
165  
166 =back
167  
168 =head1 SEE ALSO
169  
170 =over
171  
172 =item *
173  
174 L<http://wayland.freedesktop.org/> -- Wayland project web site
175  
176 =item *
177  
178 L<WL::Connection> -- Estabilish a Wayland connection
179  
180 =back
181  
182 =head1 COPYRIGHT
183  
184 Copyright 2013 Lubomir Rintel
185  
186 This program is free software; you can redistribute it and/or modify it
187 under the same terms as Perl itself.
188  
189 =head1 AUTHORS
190  
191 Lubomir Rintel C<lkundrak@v3.sk>
192  
193 =cut
194  
195 1;