1 | # Copyright (C) 2010-2014 Torsten Schoenfeld <kaffeetisch@gmx.de>
|
2 | #
|
3 | # This library is free software; you can redistribute it and/or modify it under
|
4 | # the terms of the GNU Library General Public License as published by the Free
|
5 | # Software Foundation; either version 2.1 of the License, or (at your option)
|
6 | # any later version.
|
7 | #
|
8 | # This library is distributed in the hope that it will be useful, but WITHOUT
|
9 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
10 | # FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for
|
11 | # more details.
|
12 | #
|
13 | # See the LICENSE file in the top-level directory of this distribution for the
|
14 | # full license terms.
|
15 |
|
16 | package Glib::Object::Introspection;
|
17 |
|
18 | use strict;
|
19 | use warnings;
|
20 | use Glib;
|
21 |
|
22 | our $VERSION = '0.032';
|
23 |
|
24 | use Carp;
|
25 | $Carp::Internal{(__PACKAGE__)}++;
|
26 |
|
27 | require XSLoader;
|
28 | XSLoader::load(__PACKAGE__, $VERSION);
|
29 |
|
30 | my @OBJECT_PACKAGES_WITH_VFUNCS;
|
31 | my %SEEN;
|
32 | our %_FORBIDDEN_SUB_NAMES = map { $_ => 1 } qw/AUTOLOAD CLONE DESTROY BEGIN
|
33 | UNITCHECK CHECK INIT END/;
|
34 | our %_BASENAME_TO_PACKAGE;
|
35 | our %_REBLESSERS;
|
36 |
|
37 | sub _create_invoker_sub {
|
38 | my ($basename, $namespace, $name,
|
39 | $shift_package_name, $flatten_array_ref_return,
|
40 | $handle_sentinel_boolean) = @_;
|
41 | if ($flatten_array_ref_return && $handle_sentinel_boolean) {
|
42 | croak sprintf
|
43 | "Cannot handle the options flatten_array_ref and handle_sentinel_boolean " .
|
44 | "at the same time for %s%s::%s",
|
45 | $_BASENAME_TO_PACKAGE{$basename},
|
46 | defined $namespace ? "::$namespace" : '',
|
47 | $name;
|
48 | }
|
49 | if ($flatten_array_ref_return) {
|
50 | return sub {
|
51 | shift if $shift_package_name;
|
52 | my $ref = __PACKAGE__->invoke($basename, $namespace, $name, @_);
|
53 | return if not defined $ref;
|
54 | return wantarray ? @$ref : $ref->[$#$ref];
|
55 | };
|
56 | } elsif ($handle_sentinel_boolean) {
|
57 | return sub {
|
58 | shift if $shift_package_name;
|
59 | my ($bool, @stuff) = __PACKAGE__->invoke($basename, $namespace, $name, @_);
|
60 | return $bool
|
61 | ? @stuff[0..$#stuff] # slice to correctly behave in scalar context
|
62 | : ();
|
63 | };
|
64 | } else {
|
65 | return sub {
|
66 | shift if $shift_package_name;
|
67 | return __PACKAGE__->invoke($basename, $namespace, $name, @_);
|
68 | };
|
69 | }
|
70 | }
|
71 |
|
72 | sub setup {
|
73 | my ($class, %params) = @_;
|
74 | my $basename = $params{basename};
|
75 | my $version = $params{version};
|
76 | my $package = $params{package};
|
77 | my $search_path = $params{search_path} || undef;
|
78 | my $name_corrections = $params{name_corrections} || {};
|
79 |
|
80 | # Avoid repeating setting up a library as this can lead to issues, e.g., due
|
81 | # to types being registered more than once with perl-Glib. In particular,
|
82 | # the lazy-loading mechanism of Glib::Object is not prepared to handle
|
83 | # repeated type registrations.
|
84 | if ($SEEN{$basename}{$version}{$package}++) {
|
85 | return;
|
86 | }
|
87 |
|
88 | $_BASENAME_TO_PACKAGE{$basename} = $package;
|
89 |
|
90 | my %shift_package_name_for = exists $params{class_static_methods}
|
91 | ? map { $_ => 1 } @{$params{class_static_methods}}
|
92 | : ();
|
93 | my %flatten_array_ref_return_for = exists $params{flatten_array_ref_return_for}
|
94 | ? map { $_ => 1 } @{$params{flatten_array_ref_return_for}}
|
95 | : ();
|
96 | my %handle_sentinel_boolean_for = exists $params{handle_sentinel_boolean_for}
|
97 | ? map { $_ => 1 } @{$params{handle_sentinel_boolean_for}}
|
98 | : ();
|
99 | my @use_generic_signal_marshaller_for = exists $params{use_generic_signal_marshaller_for}
|
100 | ? @{$params{use_generic_signal_marshaller_for}}
|
101 | : ();
|
102 |
|
103 | if (exists $params{reblessers}) {
|
104 | $_REBLESSERS{$_} = $params{reblessers}->{$_}
|
105 | for keys %{$params{reblessers}}
|
106 | }
|
107 |
|
108 | __PACKAGE__->_load_library($basename, $version, $search_path);
|
109 |
|
110 | my ($functions, $constants, $fields, $interfaces, $objects_with_vfuncs) =
|
111 | __PACKAGE__->_register_types($basename, $package);
|
112 |
|
113 | no strict qw(refs);
|
114 | no warnings qw(redefine);
|
115 |
|
116 | foreach my $namespace (keys %{$functions}) {
|
117 | my $is_namespaced = $namespace ne "";
|
118 | NAME:
|
119 | foreach my $name (@{$functions->{$namespace}}) {
|
120 | my $auto_name = $is_namespaced
|
121 | ? $package . '::' . $namespace . '::' . $name
|
122 | : $package . '::' . $name;
|
123 | my $corrected_name = exists $name_corrections->{$auto_name}
|
124 | ? $name_corrections->{$auto_name}
|
125 | : $auto_name;
|
126 | if (defined &{$corrected_name}) {
|
127 | next NAME;
|
128 | }
|
129 | *{$corrected_name} = _create_invoker_sub (
|
130 | $basename, $is_namespaced ? $namespace : undef, $name,
|
131 | $shift_package_name_for{$corrected_name},
|
132 | $flatten_array_ref_return_for{$corrected_name},
|
133 | $handle_sentinel_boolean_for{$corrected_name});
|
134 | }
|
135 | }
|
136 |
|
137 | foreach my $name (@{$constants}) {
|
138 | my $auto_name = $package . '::' . $name;
|
139 | my $corrected_name = exists $name_corrections->{$auto_name}
|
140 | ? $name_corrections->{$auto_name}
|
141 | : $auto_name;
|
142 | # Install a sub which, on the first invocation, calls _fetch_constant and
|
143 | # then overrides itself with a constant sub returning that value.
|
144 | *{$corrected_name} = sub {
|
145 | my $value = __PACKAGE__->_fetch_constant($basename, $name);
|
146 | {
|
147 | *{$corrected_name} = sub { $value };
|
148 | }
|
149 | return $value;
|
150 | };
|
151 | }
|
152 |
|
153 | foreach my $namespace (keys %{$fields}) {
|
154 | foreach my $field_name (@{$fields->{$namespace}}) {
|
155 | my $auto_name = $package . '::' . $namespace . '::' . $field_name;
|
156 | my $corrected_name = exists $name_corrections->{$auto_name}
|
157 | ? $name_corrections->{$auto_name}
|
158 | : $auto_name;
|
159 | *{$corrected_name} = sub {
|
160 | my ($invocant, $new_value) = @_;
|
161 | my $old_value = __PACKAGE__->_get_field($basename, $namespace,
|
162 | $field_name, $invocant);
|
163 | # If a new value is provided, even if it is undef, update the field.
|
164 | if (scalar @_ > 1) {
|
165 | __PACKAGE__->_set_field($basename, $namespace,
|
166 | $field_name, $invocant, $new_value);
|
167 | }
|
168 | return $old_value;
|
169 | };
|
170 | }
|
171 | }
|
172 |
|
173 | foreach my $name (@{$interfaces}) {
|
174 | my $adder_name = $package . '::' . $name . '::_ADD_INTERFACE';
|
175 | *{$adder_name} = sub {
|
176 | my ($class, $target_package) = @_;
|
177 | __PACKAGE__->_add_interface($basename, $name, $target_package);
|
178 | };
|
179 | }
|
180 |
|
181 | foreach my $object_name (@{$objects_with_vfuncs}) {
|
182 | my $object_package = $package . '::' . $object_name;
|
183 | my $installer_name = $object_package . '::_INSTALL_OVERRIDES';
|
184 | *{$installer_name} = sub {
|
185 | my ($target_package) = @_;
|
186 | # Delay hooking up the vfuncs until INIT so that we can see whether the
|
187 | # package defines the relevant subs or not. FIXME: Shouldn't we only do
|
188 | # the delay dance if ${^GLOBAL_PHASE} eq 'START'?
|
189 | push @OBJECT_PACKAGES_WITH_VFUNCS,
|
190 | [$basename, $object_name, $target_package];
|
191 | };
|
192 | }
|
193 |
|
194 | foreach my $packaged_signal (@use_generic_signal_marshaller_for) {
|
195 | __PACKAGE__->_use_generic_signal_marshaller_for (@$packaged_signal);
|
196 | }
|
197 |
|
198 | return;
|
199 | }
|
200 |
|
201 | INIT {
|
202 | no strict qw(refs);
|
203 |
|
204 | # Hook up the implemented vfuncs first.
|
205 | foreach my $target (@OBJECT_PACKAGES_WITH_VFUNCS) {
|
206 | my ($basename, $object_name, $target_package) = @{$target};
|
207 | __PACKAGE__->_install_overrides($basename, $object_name, $target_package);
|
208 | }
|
209 |
|
210 | # And then, for each vfunc in our ancestry that has an implementation, add a
|
211 | # wrapper sub to our immediate parent. We delay this step until after all
|
212 | # Perl overrides are in place because otherwise, the override code would see
|
213 | # the fallback vfuncs (via gv_fetchmethod) we are about to set up, and it
|
214 | # would mistake them for an actual implementation. This would then lead it
|
215 | # to put Perl callbacks into the vfunc slots regardless of whether the Perl
|
216 | # class in question actually provides implementations.
|
217 | my %implementer_packages_seen;
|
218 | foreach my $target (@OBJECT_PACKAGES_WITH_VFUNCS) {
|
219 | my ($basename, $object_name, $target_package) = @{$target};
|
220 | my @non_perl_parent_packages =
|
221 | __PACKAGE__->_find_non_perl_parents($basename, $object_name,
|
222 | $target_package);
|
223 |
|
224 | # For each non-Perl parent, look at all the vfuncs it and its parents
|
225 | # provide. For each vfunc which has an implementation in the parent
|
226 | # (i.e. the corresponding struct pointer is not NULL), install a fallback
|
227 | # sub which invokes the vfunc implementation. This assumes that
|
228 | # @non_perl_parent_packages contains the parents in "ancestorial" order,
|
229 | # i.e. the first entry must be the immediate parent.
|
230 | IMPLEMENTER: for (my $i = 0; $i < @non_perl_parent_packages; $i++) {
|
231 | my $implementer_package = $non_perl_parent_packages[$i];
|
232 | next IMPLEMENTER if $implementer_packages_seen{$implementer_package}++;
|
233 | for (my $j = $i; $j < @non_perl_parent_packages; $j++) {
|
234 | my $provider_package = $non_perl_parent_packages[$j];
|
235 | my @vfuncs = __PACKAGE__->_find_vfuncs_with_implementation(
|
236 | $provider_package, $implementer_package);
|
237 | VFUNC: foreach my $vfunc_name (@vfuncs) {
|
238 | my $perl_vfunc_name = uc $vfunc_name;
|
239 | if (exists $_FORBIDDEN_SUB_NAMES{$perl_vfunc_name}) {
|
240 | $perl_vfunc_name .= '_VFUNC';
|
241 | }
|
242 | my $full_perl_vfunc_name =
|
243 | $implementer_package . '::' . $perl_vfunc_name;
|
244 | next VFUNC if defined &{$full_perl_vfunc_name};
|
245 | *{$full_perl_vfunc_name} = sub {
|
246 | __PACKAGE__->_invoke_fallback_vfunc($provider_package,
|
247 | $vfunc_name,
|
248 | $implementer_package,
|
249 | @_);
|
250 | }
|
251 | }
|
252 | }
|
253 | }
|
254 | }
|
255 |
|
256 | @OBJECT_PACKAGES_WITH_VFUNCS = ();
|
257 | }
|
258 |
|
259 | # Monkey-patch Glib with a generic constructor for boxed types. Glib cannot
|
260 | # provide this on its own because it does not know how big the struct of a
|
261 | # boxed type is. FIXME: This sort of violates encapsulation.
|
262 | {
|
263 | if (! defined &{Glib::Boxed::new}) {
|
264 | *{Glib::Boxed::new} = sub {
|
265 | my ($class, @rest) = @_;
|
266 | my $boxed = Glib::Object::Introspection->_construct_boxed ($class);
|
267 | my $fields = 1 == @rest ? $rest[0] : { @rest };
|
268 | foreach my $field (keys %$fields) {
|
269 | if ($boxed->can ($field)) {
|
270 | $boxed->$field ($fields->{$field});
|
271 | }
|
272 | }
|
273 | return $boxed;
|
274 | }
|
275 | }
|
276 | }
|
277 |
|
278 | package Glib::Object::Introspection::_FuncWrapper;
|
279 |
|
280 | use overload
|
281 | '&{}' => sub {
|
282 | my ($func) = @_;
|
283 | return sub { Glib::Object::Introspection::_FuncWrapper::_invoke($func, @_) }
|
284 | },
|
285 | fallback => 1;
|
286 |
|
287 | package Glib::Object::Introspection;
|
288 |
|
289 | 1;
|
290 | __END__
|
291 |
|
292 | =encoding utf8
|
293 |
|
294 | =head1 NAME
|
295 |
|
296 | Glib::Object::Introspection - Dynamically create Perl language bindings
|
297 |
|
298 | =head1 SYNOPSIS
|
299 |
|
300 | use Glib::Object::Introspection;
|
301 | Glib::Object::Introspection->setup(
|
302 | basename => 'Gtk',
|
303 | version => '3.0',
|
304 | package => 'Gtk3');
|
305 | # now GtkWindow, to mention just one example, is available as
|
306 | # Gtk3::Window, and you can call gtk_window_new as Gtk3::Window->new
|
307 |
|
308 | =head1 ABSTRACT
|
309 |
|
310 | Glib::Object::Introspection uses the gobject-introspection and libffi projects
|
311 | to dynamically create Perl bindings for a wide variety of libraries. Examples
|
312 | include gtk+, webkit, libsoup and many more.
|
313 |
|
314 | =head1 DESCRIPTION FOR LIBRARY USERS
|
315 |
|
316 | To allow Glib::Object::Introspection to create bindings for a library, the
|
317 | library must have installed a typelib file, for example
|
318 | C<$prefix/lib/girepository-1.0/Gtk-3.0.typelib>. In your code you then simply
|
319 | call C<< Glib::Object::Introspection->setup >> with the following key-value
|
320 | pairs to set everything up:
|
321 |
|
322 | =over
|
323 |
|
324 | =item basename => $basename
|
325 |
|
326 | The basename of the library that should be wrapped. If your typelib is called
|
327 | C<Gtk-3.0.typelib>, then the basename is 'Gtk'.
|
328 |
|
329 | =item version => $version
|
330 |
|
331 | The particular version of the library that should be wrapped, in string form.
|
332 | For C<Gtk-3.0.typelib>, it is '3.0'.
|
333 |
|
334 | =item package => $package
|
335 |
|
336 | The name of the Perl package where every class and method of the library should
|
337 | be rooted. If a library with basename 'Gtk' contains an class 'GtkWindow',
|
338 | and you pick as the package 'Gtk3', then that class will be available as
|
339 | 'Gtk3::Window'.
|
340 |
|
341 | =back
|
342 |
|
343 | The Perl wrappers created by C<Glib::Object::Introspection> follow the
|
344 | conventions of the L<Glib> module and old hand-written bindings like L<Gtk2>.
|
345 | You can use the included tool C<perli11ndoc> to view the documentation of all
|
346 | installed libraries organized and displayed in accordance with these
|
347 | conventions. The guiding principles underlying the conventions are described
|
348 | in the following.
|
349 |
|
350 | =head2 Namespaces and Objects
|
351 |
|
352 | The namespaces of the C libraries are mapped to Perl packages according to the
|
353 | C<package> option specified, for example:
|
354 |
|
355 | gtk_ => Gtk3
|
356 | gdk_ => Gtk3::Gdk
|
357 | gdk_pixbuf_ => Gtk3::Gdk::Pixbuf
|
358 | pango_ => Pango
|
359 |
|
360 | Classes, interfaces and boxed and fundamental types get their own namespaces,
|
361 | in a way, as the concept of the GType is completely replaced in the Perl
|
362 | bindings by the Perl package name.
|
363 |
|
364 | GtkButton => Gtk3::Button
|
365 | GdkPixbuf => Gtk3::Gdk::Pixbuf
|
366 | GtkScrolledWindow => Gtk3::ScrolledWindow
|
367 | PangoFontDescription => Pango::FontDescription
|
368 |
|
369 | With this package mapping and Perl's built-in method lookup, the bindings can
|
370 | do object casting for you. This gives us a rather comfortably object-oriented
|
371 | syntax, using normal Perl object semantics:
|
372 |
|
373 | in C:
|
374 | GtkWidget * b;
|
375 | b = gtk_check_button_new_with_mnemonic ("_Something");
|
376 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (b), TRUE);
|
377 | gtk_widget_show (b);
|
378 |
|
379 | in Perl:
|
380 | my $b = Gtk3::CheckButton->new_with_mnemonic ('_Something');
|
381 | $b->set_active (1);
|
382 | $b->show;
|
383 |
|
384 | You see from this that cast macros are not necessary and that you don't need to
|
385 | type namespace prefixes quite so often, so your code is a lot shorter.
|
386 |
|
387 | =head2 Flags and Enums
|
388 |
|
389 | Flags and enum values are handled as strings, because it's much more readable
|
390 | than numbers, and because it's automagical thanks to the GType system. Values
|
391 | are referred to by their nicknames; basically, strip the common prefix,
|
392 | lower-case it, and optionally convert '_' to '-':
|
393 |
|
394 | GTK_WINDOW_TOPLEVEL => 'toplevel'
|
395 | GTK_BUTTONS_OK_CANCEL => 'ok-cancel' (or 'ok_cancel')
|
396 |
|
397 | Flags are a special case. You can't (sensibly) bitwise-or these
|
398 | string-constants, so you provide a reference to an array of them instead.
|
399 | Anonymous arrays are useful here, and an empty anonymous array is a simple
|
400 | way to say 'no flags'.
|
401 |
|
402 | FOO_BAR_BAZ | FOO_BAR_QUU | FOO_BAR_QUUX => [qw/baz quu qux/]
|
403 | 0 => []
|
404 |
|
405 | In some cases you need to see if a bit is set in a bitfield; methods
|
406 | returning flags therefore return an overloaded object. See L<Glib> for
|
407 | more details on which operations are allowed on these flag objects, but
|
408 | here is a quick example:
|
409 |
|
410 | in C:
|
411 | /* event->state is a bitfield */
|
412 | if (event->state & GDK_CONTROL_MASK) g_printerr ("control was down\n");
|
413 |
|
414 | in Perl:
|
415 | # $event->state is a special object
|
416 | warn "control was down\n" if $event->state & "control-mask";
|
417 |
|
418 | But this also works:
|
419 |
|
420 | warn "control was down\n" if $event->state * "control-mask";
|
421 | warn "control was down\n" if $event->state >= "control-mask";
|
422 | warn "control and shift were down\n"
|
423 | if $event->state >= ["control-mask", "shift-mask"];
|
424 |
|
425 | =head2 Memory Handling
|
426 |
|
427 | The functions for ref'ing and unref'ing objects and free'ing boxed structures
|
428 | are not even mapped to Perl, because it's all handled automagically by the
|
429 | bindings. Objects will be kept alive so long as you have a Perl scalar
|
430 | pointing to it or the object is referenced in another way, e.g. from a
|
431 | container.
|
432 |
|
433 | The only thing you have to be careful about is the lifespan of non
|
434 | reference counted structures, which means most things derived from
|
435 | C<Glib::Boxed>. If it comes from a signal callback it might be good
|
436 | only until you return, or if it's the insides of another object then
|
437 | it might be good only while that object lives. If in doubt you can
|
438 | C<copy>. Structs from C<copy> or C<new> are yours and live as long as
|
439 | referred to from Perl.
|
440 |
|
441 | =head2 Callbacks
|
442 |
|
443 | Use normal Perl callback/closure tricks with callbacks. The most common use
|
444 | you'll have for callbacks is with the L<Glib> C<signal_connect> method:
|
445 |
|
446 | $widget->signal_connect (event => \&event_handler, $user_data);
|
447 | $button->signal_connect (clicked => sub { warn "hi!\n" });
|
448 |
|
449 | $user_data is optional, and with Perl closures you don't often need it
|
450 | (see L<perlsub/Persistent variables with closures>).
|
451 |
|
452 | The userdata is held in a scalar, initialized from what you give in
|
453 | C<signal_connect> etc. It's passed to the callback in usual Perl
|
454 | "call by reference" style which means the callback can modify its last
|
455 | argument, ie. $_[-1], to modify the held userdata. This is a little
|
456 | subtle, but you can use it for some "state" associated with the
|
457 | connection.
|
458 |
|
459 | $widget->signal_connect (activate => \&my_func, 1);
|
460 | sub my_func {
|
461 | print "activation count: $_[-1]\n";
|
462 | $_[-1] ++;
|
463 | }
|
464 |
|
465 | Because the held userdata is a new scalar there's no change to the
|
466 | variable (etc.) you originally passed to C<signal_connect>.
|
467 |
|
468 | If you have a parent object in the userdata (or closure) you have to be careful
|
469 | about circular references preventing parent and child being destroyed. See
|
470 | L<perlobj/Two-Phased Garbage Collection> about this generally. Toplevel
|
471 | widgets like C<Gtk3::Window> always need an explicit C<< $widget->destroy >> so
|
472 | their C<destroy> signal is a good place to break circular references. But for
|
473 | other widgets it's usually friendliest to avoid circularities in the first
|
474 | place, either by using weak references in the userdata, or possibly locating a
|
475 | parent dynamically with C<< $widget->get_ancestor >>.
|
476 |
|
477 | =head2 Miscellaneous
|
478 |
|
479 | In C you can only return one value from a function, and it is a common practice
|
480 | to modify pointers passed in to simulate returning multiple values. In Perl,
|
481 | you can return lists; any functions which modify arguments are changed to
|
482 | return them instead.
|
483 |
|
484 | Arguments and return values that have the types GList or GSList or which are C
|
485 | arrays of values will be converted to and from references to normal Perl
|
486 | arrays. The same holds for GHashTable and references to normal Perl hashes.
|
487 |
|
488 | You don't need to specify string lengths. You can always use C<substr> to pass
|
489 | different parts of a string.
|
490 |
|
491 | Anything that uses GError in C will C<croak> on failure, setting $@ to a
|
492 | magical exception object, which is overloaded to print as the
|
493 | returned error message. The ideology here is that GError is to be used
|
494 | for runtime exceptions, and C<croak> is how you do that in Perl. You can
|
495 | catch a croak very easily by wrapping the function in an eval:
|
496 |
|
497 | eval {
|
498 | my $pixbuf = Gtk3::Gdk::Pixbuf->new_from_file ($filename);
|
499 | $image->set_from_pixbuf ($pixbuf);
|
500 | };
|
501 | if ($@) {
|
502 | print "$@\n"; # prints the possibly-localized error message
|
503 | if (Glib::Error::matches ($@, 'Gtk3::Gdk::Pixbuf::Error',
|
504 | 'unknown-format')) {
|
505 | change_format_and_try_again ();
|
506 | } elsif (Glib::Error::matches ($@, 'Glib::File::Error', 'noent')) {
|
507 | change_source_dir_and_try_again ();
|
508 | } else {
|
509 | # don't know how to handle this
|
510 | die $@;
|
511 | }
|
512 | }
|
513 |
|
514 | This has the added advantage of letting you bunch things together as you would
|
515 | with a try/throw/catch block in C++ -- you get cleaner code. By using
|
516 | Glib::Error exception objects, you don't have to rely on string matching
|
517 | on a possibly localized error message; you can match errors by explicit and
|
518 | predictable conditions. See L<Glib::Error> for more information.
|
519 |
|
520 | =head1 DESCRIPTION FOR LIBRARY BINDING AUTHORS
|
521 |
|
522 | =head2 C<< Glib::Object::Introspection->setup >>
|
523 |
|
524 | C<< Glib::Object::Introspection->setup >> takes a few optional arguments that
|
525 | augment the generated API:
|
526 |
|
527 | =over
|
528 |
|
529 | =item search_path => $search_path
|
530 |
|
531 | A path that should be used when looking for typelibs. If you use typelibs from
|
532 | system directories, or if your environment contains a properly set
|
533 | C<GI_TYPELIB_PATH> variable, then this should not be necessary.
|
534 |
|
535 | =item name_corrections => { auto_name => new_name, ... }
|
536 |
|
537 | A hash ref that is used to rename functions and methods. Use this if you don't
|
538 | like the automatically generated mapping for a function or method. For
|
539 | example, if C<g_file_hash> is automatically represented as
|
540 | C<Glib::IO::file_hash> but you want C<Glib::IO::File::hash> then pass
|
541 |
|
542 | name_corrections => {
|
543 | 'Glib::IO::file_hash' => 'Glib::IO::File::hash'
|
544 | }
|
545 |
|
546 | =item class_static_methods => [ function1, ... ]
|
547 |
|
548 | An array ref of function names that you want to be treated as class-static
|
549 | methods. That is, if you want be able to call
|
550 | C<Gtk3::Window::list_toplevels> as C<< Gtk3::Window->list_toplevels >>, then
|
551 | pass
|
552 |
|
553 | class_static_methods => [
|
554 | 'Gtk3::Window::list_toplevels'
|
555 | ]
|
556 |
|
557 | The function names refer to those after name corrections.
|
558 |
|
559 | =item flatten_array_ref_return_for => [ function1, ... ]
|
560 |
|
561 | An array ref of function names that return an array ref that you want to be
|
562 | flattened so that they return plain lists. For example
|
563 |
|
564 | flatten_array_ref_return_for => [
|
565 | 'Gtk3::Window::list_toplevels'
|
566 | ]
|
567 |
|
568 | The function names refer to those after name corrections. Functions occuring
|
569 | in C<flatten_array_ref_return_for> may also occur in C<class_static_methods>.
|
570 |
|
571 | =item handle_sentinel_boolean_for => [ function1, ... ]
|
572 |
|
573 | An array ref of function names that return multiple values, the first of which
|
574 | is to be interpreted as indicating whether the rest of the returned values are
|
575 | valid. This frequently occurs with functions that have out arguments; the
|
576 | boolean then indicates whether the out arguments have been written. With
|
577 | C<handle_sentinel_boolean_for>, the first return value is taken to be the
|
578 | sentinel boolean. If it is true, the rest of the original return values will
|
579 | be returned, and otherwise an empty list will be returned.
|
580 |
|
581 | handle_sentinel_boolean_for => [
|
582 | 'Gtk3::TreeSelection::get_selected'
|
583 | ]
|
584 |
|
585 | The function names refer to those after name corrections. Functions occuring
|
586 | in C<handle_sentinel_boolean_for> may also occur in C<class_static_methods>.
|
587 |
|
588 | =item use_generic_signal_marshaller_for => [ [package1, signal1, [arg_converter1]], ... ]
|
589 |
|
590 | Use an introspection-based generic signal marshaller for the signal C<signal1>
|
591 | of type C<package1>. If given, use the code reference C<arg_converter1> to
|
592 | convert the arguments that are passed to the signal handler. In contrast to
|
593 | L<Glib>'s normal signal marshaller, the generic signal marshaller supports,
|
594 | among other things, pointer arrays and out arguments.
|
595 |
|
596 | =item reblessers => { package => \&reblesser, ... }
|
597 |
|
598 | Tells G:O:I to invoke I<reblesser> whenever a Perl object is created for an
|
599 | object of type I<package>. Currently, this only applies to boxed unions. The
|
600 | reblesser gets passed the pre-created Perl object and needs to return the
|
601 | modified Perl object. For example:
|
602 |
|
603 | sub Gtk3::Gdk::Event::_rebless {
|
604 | my ($event) = @_;
|
605 | return bless $event, lookup_real_package_for ($event);
|
606 | }
|
607 |
|
608 | =back
|
609 |
|
610 | =head2 C<< Glib::Object::Introspection->invoke >>
|
611 |
|
612 | To invoke specific functions manually, you can use the low-level C<<
|
613 | Glib::Object::Introspection->invoke >>.
|
614 |
|
615 | Glib::Object::Introspection->invoke(
|
616 | $basename, $namespace, $function, @args)
|
617 |
|
618 | =over
|
619 |
|
620 | =item * $basename is the basename of a library, like 'Gtk'.
|
621 |
|
622 | =item * $namespace refers to a namespace inside that library, like 'Window'. Use
|
623 | undef here if you want to call a library-global function.
|
624 |
|
625 | =item * $function is the name of the function you want to invoke. It can also
|
626 | refer to the name of a constant.
|
627 |
|
628 | =item * @args are the arguments that should be passed to the function. For a
|
629 | method, this should include the invocant. For a constructor, this should
|
630 | include the package name.
|
631 |
|
632 | =back
|
633 |
|
634 | C<< Glib::Object::Introspection->invoke >> returns whatever the function being
|
635 | invoked returns.
|
636 |
|
637 | =head2 Overrides
|
638 |
|
639 | To override the behavior of a specific function or method, create an
|
640 | appropriately named sub in the correct package and have it call C<<
|
641 | Glib::Object::Introspection->invoke >>. Say you want to override
|
642 | C<Gtk3::Window::list_toplevels>, then do this:
|
643 |
|
644 | sub Gtk3::Window::list_toplevels {
|
645 | # ...do something...
|
646 | my $ref = Glib::Object::Introspection->invoke (
|
647 | 'Gtk', 'Window', 'list_toplevels',
|
648 | @_);
|
649 | # ...do something...
|
650 | return wantarray ? @$ref : $ref->[$#$ref];
|
651 | }
|
652 |
|
653 | The sub's name and package must be those after name corrections.
|
654 |
|
655 | =head2 Converting a Perl variable to a GValue
|
656 |
|
657 | If you need to marshal into a GValue, then Glib::Object::Introspection cannot
|
658 | do this automatically because the type information is missing. If you do have
|
659 | this information in your module, however, you can use
|
660 | Glib::Object::Introspection::GValueWrapper to do the conversion. In the
|
661 | wrapper for a function that expects a GValue, do this:
|
662 |
|
663 | ...
|
664 | my $type = ...; # somehow get the package name that
|
665 | # corresponds to the correct GType
|
666 | my $real_value =
|
667 | Glib::Object::Introspection::GValueWrapper->new ($type, $value);
|
668 | # now use Glib::Object::Introspection->invoke and
|
669 | # substitute $real_value where you'd use $value
|
670 | ...
|
671 |
|
672 | =head2 Handling extendable enumerations
|
673 |
|
674 | If you need to handle extendable enumerations for which more than the
|
675 | pre-defined values might be valid, then use C<<
|
676 | Glib::Object::Introspection->convert_enum_to_sv >> and C<<
|
677 | Glib::Object::Introspection->convert_sv_to_enum >>. They will raise an
|
678 | exception on unknown values; catching it then allows you to implement fallback
|
679 | behavior.
|
680 |
|
681 | Glib::Object::Introspection->convert_enum_to_sv (package, enum_value)
|
682 | Glib::Object::Introspection->convert_sv_to_enum (package, sv)
|
683 |
|
684 | =head1 SEE ALSO
|
685 |
|
686 | =over
|
687 |
|
688 | =item perl-Glib: L<Glib>
|
689 |
|
690 | =item gobject-introspection: L<http://live.gnome.org/GObjectIntrospection>
|
691 |
|
692 | =item libffi: L<http://sourceware.org/libffi/>
|
693 |
|
694 | =back
|
695 |
|
696 | =head1 AUTHORS
|
697 |
|
698 | =over
|
699 |
|
700 | =item Emmanuele Bassi <ebassi at linux intel com>
|
701 |
|
702 | =item muppet <scott asofyet org>
|
703 |
|
704 | =item Torsten Schönfeld <kaffeetisch at gmx de>
|
705 |
|
706 | =back
|
707 |
|
708 | =head1 LICENSE
|
709 |
|
710 | This library is free software; you can redistribute it and/or modify it under
|
711 | the terms of the Lesser General Public License (LGPL). For more information,
|
712 | see http://www.fsf.org/licenses/lgpl.txt
|
713 |
|
714 | =cut
|