| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | wl-scanner.pl - Generate Perl bindings for Wayland protocol | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | B<wl-draw.pl> | ||
| 10 | [<protocol.xml>] | ||
| 11 | [<output_dir>/] | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This tool processes Wayland protocol specification and generates L<WL::Base> | ||
| 16 | subclasses with wrappers for requests, event processing and constants for enums. | ||
| 17 | |||
| 18 | Output is to STDOUT or specified directory. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | |||
| 25 | use XML::Smart; | ||
| 26 | |||
| 27 | our $package_name; | ||
| 28 | our $output_file; | ||
| 29 | |||
| 30 | # Output routines | ||
| 31 | sub op | ||
| 32 | { | ||
| 33 | if ($output_file) { | ||
| 34 | print $output_file @_; | ||
| 35 | } else { | ||
| 36 | print @_; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | sub opf | ||
| 41 | { | ||
| 42 | op sprintf shift, @_; | ||
| 43 | } | ||
| 44 | |||
| 45 | # Information for type marshalling/unmarshalling: | ||
| 46 | # [ <routine returning pack string and code for fetching the value from | ||
| 47 | # argument stack>, <routine returning pack string and code that obtains | ||
| 48 | # the actual value given unpacked data on an argument stack >], ... | ||
| 49 | my %typemap = ( | ||
| 50 | int => [ | ||
| 51 | sub { 'l' => 'shift' }, | ||
| 52 | sub { 'l' => 'shift' }], | ||
| 53 | uint => [ | ||
| 54 | sub { 'L' => 'shift' }, | ||
| 55 | sub { 'L' => 'shift' }], | ||
| 56 | fixed => [ | ||
| 57 | sub { 'l' => '$self->nv2fixed ($_)' }, | ||
| 58 | sub { 'L' => '$self->fixed2nv (shift)' }], | ||
| 59 | string => [ | ||
| 60 | sub { 'L/ax!4' => 'shift."\x00"' }, | ||
| 61 | sub { 'L/ax!4' => '[shift =~ /(.*)./]->[0]' }], | ||
| 62 | object => [ | ||
| 63 | sub { 'L' => 'shift->{id}' }, | ||
| 64 | sub { 'L' => 'new WL::'.(shift->{interface} || 'Base').' ($self->{conn}, shift)' }, | ||
| 65 | ], | ||
| 66 | new_id => [ | ||
| 67 | sub { my $interface = shift->{interface}; $interface | ||
| 68 | ? ('L' => '($retval = new WL::'.$interface.' ($self->{conn}))->{id}') | ||
| 69 | : ('L/ax!4 L L' => '$_[0]."\x00", delete $_[1], ($retval = ("WL::".shift)->new ($self->{conn}))->{id}') }, | ||
| 70 | sub { 'L' => 'new WL::'.shift->{interface}.' ($self->{conn})' }], | ||
| 71 | array => [ | ||
| 72 | sub { 'L/ax!4' => 'shift' }, | ||
| 73 | sub { 'L/ax!4' => 'shift' }], | ||
| 74 | fd => [ | ||
| 75 | sub { '' => '($file = shift, ())' }, | ||
| 76 | sub { '' => 'shift' }] | ||
| 77 | ); | ||
| 78 | |||
| 79 | sub process_request | ||
| 80 | { | ||
| 81 | my $request = shift; | ||
| 82 | |||
| 83 | op "sub $request->{name}\n"; | ||
| 84 | op "{\n"; | ||
| 85 | op "\tmy \$self = shift;\n"; | ||
| 86 | op "\tmy \$file;\n"; | ||
| 87 | op "\tmy \$retval;\n"; | ||
| 88 | op "\n"; | ||
| 89 | |||
| 90 | my @pack; | ||
| 91 | my @map; | ||
| 92 | foreach my $arg ($request->{arg}('@')) { | ||
| 93 | my ($pack, $map) = $typemap{$arg->{type}}[0]->($arg); | ||
| 94 | push @pack, $pack if $pack; | ||
| 95 | push @map, $map; | ||
| 96 | } | ||
| 97 | my $pack = join ' ', @pack; | ||
| 98 | my $map = join ",\n\t\t", @map; | ||
| 99 | |||
| 100 | opf "\t\$self->call (REQUEST_%s, pack ('%s',\n\t\t%s), \$file);\n", | ||
| 101 | uc ($request->{name}), $pack, $map; | ||
| 102 | op "\n"; | ||
| 103 | |||
| 104 | op "\treturn \$retval;\n"; | ||
| 105 | op "}\n"; | ||
| 106 | op "\n"; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub process_event | ||
| 110 | { | ||
| 111 | my $event = shift; | ||
| 112 | |||
| 113 | my @pack; | ||
| 114 | my @map; | ||
| 115 | foreach my $arg ($event->{arg}('@')) { | ||
| 116 | my ($pack, $map) = $typemap{$arg->{type}}[1]->($arg); | ||
| 117 | push @pack, $pack if $pack; | ||
| 118 | push @map, $map; | ||
| 119 | } | ||
| 120 | my $pack = join ' ', @pack; | ||
| 121 | my $map = join ",\n\t\t\t", @map; | ||
| 122 | |||
| 123 | op "\t\t\@_ = unpack ('$pack', shift);\n" if $pack; | ||
| 124 | op "\t\treturn \$self->$event->{name} ($map);\n"; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub process_enum | ||
| 128 | { | ||
| 129 | my $enum = shift; | ||
| 130 | |||
| 131 | foreach my $entry ($enum->{entry}('@')) { | ||
| 132 | opf "use constant %s => %s;\n", | ||
| 133 | uc ($enum->{name}.'_'.$entry->{name}), | ||
| 134 | $entry->{value}; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | sub process_interface | ||
| 139 | { | ||
| 140 | my $interface = shift; | ||
| 141 | my $opcode; | ||
| 142 | |||
| 143 | opf "package WL::$interface->{name};\n"; | ||
| 144 | op "\n"; | ||
| 145 | op "our \@ISA = qw/WL::Base/;\n"; | ||
| 146 | op "our \$VERSION = $interface->{version};\n"; | ||
| 147 | op "our \$INTERFACE = '$interface->{name}';\n"; | ||
| 148 | op "\n"; | ||
| 149 | |||
| 150 | my @requests = $interface->{request}('@'); | ||
| 151 | if (@requests) { | ||
| 152 | $opcode = 0; | ||
| 153 | op "# Requests\n"; | ||
| 154 | foreach my $request (@requests) { | ||
| 155 | opf "use constant REQUEST_%s => %d;\n", | ||
| 156 | uc ($request->{name}), $opcode++; | ||
| 157 | } | ||
| 158 | op "\n"; | ||
| 159 | foreach my $request (@requests) { | ||
| 160 | process_request ($request); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | my @events = $interface->{event}('@'); | ||
| 165 | if (@events) { | ||
| 166 | $opcode = 0; | ||
| 167 | op "# Events\n"; | ||
| 168 | foreach my $event (@events) { | ||
| 169 | opf "use constant EVENT_%s => %d;\n", | ||
| 170 | uc ($event->{name}), $opcode++; | ||
| 171 | } | ||
| 172 | |||
| 173 | op "\n"; | ||
| 174 | op "sub callback\n"; | ||
| 175 | op "{\n"; | ||
| 176 | op "\tmy \$self = shift;\n"; | ||
| 177 | op "\tmy \$opcode = shift;\n"; | ||
| 178 | op "\n"; | ||
| 179 | |||
| 180 | op "\t"; | ||
| 181 | foreach my $event (@events) { | ||
| 182 | opf "if (\$opcode == EVENT_%s) {\n", uc ($event->{name}); | ||
| 183 | process_event ($event); | ||
| 184 | op "\t} els"; | ||
| 185 | } | ||
| 186 | op "e {\n"; | ||
| 187 | op "\t\tdie 'Bad opcode';\n"; | ||
| 188 | op "\t}\n"; | ||
| 189 | |||
| 190 | op "}\n"; | ||
| 191 | op "\n"; | ||
| 192 | } | ||
| 193 | |||
| 194 | my @enums = $interface->{enum}('@'); | ||
| 195 | if (@enums) { | ||
| 196 | op "# Enums\n"; | ||
| 197 | foreach my $enum (@enums) { | ||
| 198 | process_enum ($enum); | ||
| 199 | } | ||
| 200 | op "\n"; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | sub process_protocol | ||
| 205 | { | ||
| 206 | my $protocol = shift; | ||
| 207 | |||
| 208 | op "package $package_name;\n"; | ||
| 209 | op "\n"; | ||
| 210 | op "our \$VERSION = 0.92;\n"; | ||
| 211 | op "\n"; | ||
| 212 | |||
| 213 | foreach my $interface ($protocol->{interface}('@')) { | ||
| 214 | process_interface ($interface); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | my $file = shift @ARGV; | ||
| 219 | my $dir = shift @ARGV || '.'; | ||
| 220 | open (my $source, '<', $file || 'wayland.xml') | ||
| 221 | or die "Cannot open input file: $!"; | ||
| 222 | my $spec = new XML::Smart ($source); | ||
| 223 | my $protocol_name = $spec->{protocol}{name}; | ||
| 224 | if ($protocol_name eq 'wayland') { | ||
| 225 | $package_name = 'WL'; | ||
| 226 | } else { | ||
| 227 | $package_name = 'WL::'.$spec->{protocol}{name}; | ||
| 228 | } | ||
| 229 | |||
| 230 | if ($dir) { | ||
| 231 | my $fname = "$dir/$package_name.pm"; | ||
| 232 | $fname =~ s/::/\//g; | ||
| 233 | open ($output_file, '>', $fname) | ||
| 234 | or die "$fname: $!"; | ||
| 235 | } | ||
| 236 | |||
| 237 | op "# DO NOT EDIT, PRETTY PLEASE!\n"; | ||
| 238 | op "# This file is automatically generated by wl-scanner.pl\n"; | ||
| 239 | op "#\n"; | ||
| 240 | op "\n"; | ||
| 241 | op "use strict;\n"; | ||
| 242 | op "use warnings;\n"; | ||
| 243 | op "use utf8;\n"; | ||
| 244 | op "\n"; | ||
| 245 | op "=encoding utf8\n"; | ||
| 246 | op "=cut\n"; | ||
| 247 | |||
| 248 | # Trick POD parser so that it does not consider this to be a documentation for | ||
| 249 | # the tool itself. | ||
| 250 | my $P = '='; | ||
| 251 | |||
| 252 | op <<POD; | ||
| 253 | |||
| 254 | ${P}head1 NAME | ||
| 255 | |||
| 256 | $package_name - Perl binding for $protocol_name protocol | ||
| 257 | |||
| 258 | ${P}head1 SYNOPSIS | ||
| 259 | |||
| 260 | use $package_name; | ||
| 261 | |||
| 262 | ${P}head1 DESCRIPTION | ||
| 263 | |||
| 264 | B<$package_name> is a package generated from Wayland protocol definition | ||
| 265 | using L<wl-scanner.pl>. It implements L<WL::Base> subclasses with wrappers | ||
| 266 | for requests, event processing and constants for enums. | ||
| 267 | |||
| 268 | It is not indended to be used directly. Instead, see L<WL::Connection> to see | ||
| 269 | how to obtain the object instances. | ||
| 270 | |||
| 271 | To see how to attach event callbacks and issue requests, please refer to | ||
| 272 | L<WL::Base> base class. | ||
| 273 | |||
| 274 | Until proper documentation is finished, please refer to documentation of C | ||
| 275 | bindings of the generated code (it is intended to be readable) to see what | ||
| 276 | arguments to give to requests and expect from events. | ||
| 277 | |||
| 278 | Please consider this an alpha quality code, whose API can change at any time, | ||
| 279 | until we reach version 1.0. | ||
| 280 | |||
| 281 | ${P}cut | ||
| 282 | |||
| 283 | POD | ||
| 284 | |||
| 285 | process_protocol ($spec->{protocol}); | ||
| 286 | |||
| 287 | op <<POD; | ||
| 288 | |||
| 289 | ${P}head1 BUGS | ||
| 290 | |||
| 291 | The interface documentation for the bindings is lacking. | ||
| 292 | |||
| 293 | Only client part implemented, not server. | ||
| 294 | |||
| 295 | ${P}head1 SEE ALSO | ||
| 296 | |||
| 297 | ${P}over | ||
| 298 | |||
| 299 | ${P}item * | ||
| 300 | |||
| 301 | L<http://wayland.freedesktop.org/> -- Wayland project web site | ||
| 302 | |||
| 303 | ${P}item * | ||
| 304 | |||
| 305 | L<wl-draw.pl> -- Example Wayland client | ||
| 306 | |||
| 307 | ${P}item * | ||
| 308 | |||
| 309 | L<wl-scanner.pl> -- Tool that generated this module | ||
| 310 | |||
| 311 | ${P}item * | ||
| 312 | |||
| 313 | L<WL::Base> -- Base class for Wayland objects | ||
| 314 | |||
| 315 | ${P}item * | ||
| 316 | |||
| 317 | L<WL::Connection> -- Estabilish a Wayland connection | ||
| 318 | |||
| 319 | ${P}back | ||
| 320 | |||
| 321 | ${P}head1 COPYRIGHT | ||
| 322 | |||
| 323 | Copyright 2013, 2014 Lubomir Rintel | ||
| 324 | |||
| 325 | This program is free software; you can redistribute it and/or modify it | ||
| 326 | under the same terms as Perl itself. | ||
| 327 | POD | ||
| 328 | |||
| 329 | # XML::Smart recodes this to ISO-8859 for some weird reason, | ||
| 330 | # therefore we won't use $spec->{protocol}{copyright} here. | ||
| 331 | seek $source, 0, 0; | ||
| 332 | my @copyright = map { /<copyright>/ .. /<\/copyright>/ ? $_ : () } <$source>; | ||
| 333 | shift @copyright; | ||
| 334 | pop @copyright; | ||
| 335 | map { s/\s*(.*\S?)\s*/ $1\n/ } @copyright; | ||
| 336 | |||
| 337 | if (@copyright) { | ||
| 338 | op "\nCopyright notice from the protocol definition file:\n\n"; | ||
| 339 | op @copyright; | ||
| 340 | } | ||
| 341 | |||
| 342 | op <<POD; | ||
| 343 | |||
| 344 | ${P}head1 AUTHORS | ||
| 345 | |||
| 346 | Lubomir Rintel C<lkundrak\@v3.sk> | ||
| 347 | |||
| 348 | ${P}cut | ||
| 349 | |||
| 350 | POD | ||
| 351 | |||
| 352 | =head1 SEE ALSO | ||
| 353 | |||
| 354 | =over | ||
| 355 | |||
| 356 | =item * | ||
| 357 | |||
| 358 | L<http://wayland.freedesktop.org/> -- Wayland project web site | ||
| 359 | |||
| 360 | =item * | ||
| 361 | |||
| 362 | L<WL> -- Perl Wayland protocol binding | ||
| 363 | |||
| 364 | =back | ||
| 365 | |||
| 366 | =head1 COPYRIGHT | ||
| 367 | |||
| 368 | Copyright 2013, 2014 Lubomir Rintel | ||
| 369 | |||
| 370 | This program is free software; you can redistribute it and/or modify it | ||
| 371 | under the same terms as Perl itself. | ||
| 372 | |||
| 373 | =head1 AUTHORS | ||
| 374 | |||
| 375 | Lubomir Rintel C<lkundrak@v3.sk> | ||
| 376 | |||
| 377 | =cut |