| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | wl-draw.pl - Perl Wayland demo | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | B<wl-draw.pl> | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This is an example application written using Perl bindings for Wayland protocol | ||
| 14 | demonstrating the use and capabilities of the package. It creates a surface and | ||
| 15 | continually updates its contents with random ARGB color noise. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use WL::Connection; | ||
| 20 | use File::Temp qw/tempfile/; | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | |||
| 25 | my $width = 100; | ||
| 26 | my $height = 100; | ||
| 27 | my $depth = 4; | ||
| 28 | |||
| 29 | # Connect to the server, compositor | ||
| 30 | my $conn = new WL::Connection; | ||
| 31 | |||
| 32 | # Obtain the instance of WL::wl_display singleton | ||
| 33 | my $display = $conn->get_display (); | ||
| 34 | |||
| 35 | # Respond to the events | ||
| 36 | $display->{'WL::wl_display::delete_id'} = sub { | ||
| 37 | }; | ||
| 38 | $display->{'WL::wl_display::error'} = sub { | ||
| 39 | my $self = shift; | ||
| 40 | my $object = shift; | ||
| 41 | my $code = shift; | ||
| 42 | my $message = shift; | ||
| 43 | |||
| 44 | warn $message; | ||
| 45 | }; | ||
| 46 | |||
| 47 | # We'll need these objects for drawing and displaying, we'll remember them | ||
| 48 | # as soon as they're announced via a global event | ||
| 49 | my ($shm, $shell, $compositor); | ||
| 50 | |||
| 51 | # We'll also need to remember which pixel formats are supported. | ||
| 52 | my %formats; | ||
| 53 | |||
| 54 | # Obtain the global object registry | ||
| 55 | my $registry = $display->get_registry (); | ||
| 56 | $registry->{'WL::wl_registry::global'} = sub { | ||
| 57 | my $self = shift; | ||
| 58 | my $id = shift; | ||
| 59 | my $class = shift; | ||
| 60 | my $version = shift; | ||
| 61 | |||
| 62 | if ($class eq 'wl_shm') { | ||
| 63 | $shm = $registry->bind ($id, $class, $version); | ||
| 64 | $shm->{'WL::wl_shm::format'} = sub { | ||
| 65 | my $self = shift; | ||
| 66 | my $format = shift; | ||
| 67 | |||
| 68 | $formats{$format} = 1; | ||
| 69 | }; | ||
| 70 | $conn->round_trip ($display); | ||
| 71 | } | ||
| 72 | elsif ($class eq 'wl_shell') { | ||
| 73 | $shell = $registry->bind ($id, $class, $version); | ||
| 74 | $conn->round_trip ($display); | ||
| 75 | } | ||
| 76 | elsif ($class eq 'wl_compositor') { | ||
| 77 | $compositor = $registry->bind ($id, $class, $version); | ||
| 78 | $conn->round_trip ($display); | ||
| 79 | } | ||
| 80 | }; | ||
| 81 | # Ensure everything up to here was processed and therefore we have all the | ||
| 82 | # required objects and information we bound and requested above | ||
| 83 | $conn->round_trip ($display); | ||
| 84 | |||
| 85 | # A sanity check | ||
| 86 | die 'ARGB8888 not supported' unless $formats{WL::wl_shm::FORMAT_ARGB8888}; | ||
| 87 | |||
| 88 | # Nothing new specific to Perl bindings here... | ||
| 89 | my $surface = $compositor->create_surface (); | ||
| 90 | my $shell_surface = $shell->get_shell_surface ($surface); | ||
| 91 | $shell_surface->{'WL::wl_shell_surface::ping'} = sub { | ||
| 92 | my $self = shift; | ||
| 93 | my $serial = shift; | ||
| 94 | $self->pong ($serial); | ||
| 95 | }; | ||
| 96 | $shell_surface->set_title ("Hello from Perl!"); | ||
| 97 | $shell_surface->set_toplevel (); | ||
| 98 | |||
| 99 | # Obtain a file handle for contents that we'll render into that we'd send to | ||
| 100 | # the server and allocate the desired space | ||
| 101 | my $file = tempfile (); | ||
| 102 | unlink $file; | ||
| 103 | truncate $file, ($width * $height * $depth); | ||
| 104 | |||
| 105 | # Now share our buffer with the server | ||
| 106 | my $pool = $shm->create_pool ($file, $width * $height * $depth); | ||
| 107 | my $buffer = $pool->create_buffer (0, $width, $height, $width * $depth, | ||
| 108 | WL::wl_shm::FORMAT_ARGB8888); | ||
| 109 | $buffer->{'WL::wl_buffer::release'} = sub { | ||
| 110 | }; | ||
| 111 | |||
| 112 | do { | ||
| 113 | # Render the noise | ||
| 114 | seek $file, 0, 0; | ||
| 115 | print $file $_ foreach (map {chr(rand(0x100))} | ||
| 116 | 0..($width * $height * $depth)); | ||
| 117 | flush $file; | ||
| 118 | |||
| 119 | # And let the compositor know | ||
| 120 | $surface->attach ($buffer, 0, 0); | ||
| 121 | $surface->damage (0, 0, $width, $height); | ||
| 122 | $surface->commit (); | ||
| 123 | |||
| 124 | # This is here so that we don't render more quickly that the server | ||
| 125 | # is able to consume | ||
| 126 | $conn->round_trip ($display); | ||
| 127 | } while (1); | ||
| 128 | |||
| 129 | =head1 SEE ALSO | ||
| 130 | |||
| 131 | =over | ||
| 132 | |||
| 133 | =item * | ||
| 134 | |||
| 135 | L<http://wayland.freedesktop.org/> -- Wayland project web site | ||
| 136 | |||
| 137 | =item * | ||
| 138 | |||
| 139 | L<WL::Connection> -- Estabilish a Wayland connection | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | =head1 COPYRIGHT | ||
| 144 | |||
| 145 | Copyright 2013 Lubomir Rintel | ||
| 146 | |||
| 147 | This program is free software; you can redistribute it and/or modify it | ||
| 148 | under the same terms as Perl itself. | ||
| 149 | |||
| 150 | =head1 AUTHORS | ||
| 151 | |||
| 152 | Lubomir Rintel C<lkundrak@v3.sk> | ||
| 153 | |||
| 154 | =cut |