File Coverage

blib/lib/X11/Protocol/GrabServer.pm
Criterion Covered Total %
statement 14 38 36.8
branch 1 12 8.3
condition 1 7 14.2
subroutine 6 11 54.5
pod 4 4 100.0
total 26 72 36.1


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2013, 2014 Kevin Ryde
2              
3             # This file is part of X11-Protocol-Other.
4             #
5             # X11-Protocol-Other is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # X11-Protocol-Other is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with X11-Protocol-Other. If not, see .
17              
18              
19             package X11::Protocol::GrabServer;
20 1     1   357 BEGIN { require 5 }
21 1     1   2 use strict;
  1         2  
  1         17  
22 1     1   3 use Carp;
  1         0  
  1         91  
23             BEGIN {
24             # weaken() if available, which means new enough Perl to have weakening,
25             # and Scalar::Util with its XS code
26 1 50 33 1   41 eval "use Scalar::Util 'weaken'; 1"
  1     1   3  
  1         1  
  1         68  
27             or eval "\n#line ".(__LINE__+1)." \"".__FILE__."\"\n" . <<'HERE' or die;
28             sub weaken {} # otherwise noop
29             HERE
30             }
31              
32             # uncomment this to run the ### lines
33             #use Smart::Comments;
34              
35 1     1   3 use vars '$VERSION';
  1         1  
  1         315  
36             $VERSION = 30;
37              
38             sub new {
39 0     0 1   my ($class, $X) = @_;
40             ### GrabServer-object new(): "$X"
41 0 0         if (! defined $X) {
42 0           croak "No X connection given";
43             }
44 0           my $self = bless { X => $X }, $class;
45 0           weaken ($self->{'X'});
46 0           $self->grab;
47 0           return $self;
48             }
49             sub DESTROY {
50 0     0     my ($self) = @_;
51             ### GrabServer-object DESTROY()
52              
53 0           $self->ungrab;
54              
55             # ### initial error: $@
56             # local $@; # don't clobber if destroyed during a die() unwind
57             # eval { $self->ungrab }; # ignore EPIPE write if server already closed
58             # ### DESTROY error: $@
59             }
60              
61             sub grab {
62 0     0 1   my ($self) = @_;
63             ### GrabServer-object grab()
64 0 0         unless ($self->{'grabbed'}) {
65 0   0       my $X = $self->{'X'} || return;
66 0           $self->{'grabbed'} = 1;
67 0 0         if (! $X->{__PACKAGE__.'.count'}++) {
68             ### initial X->GrabServer
69 0           $X->GrabServer;
70             }
71             }
72             ### grab count now: $self->{'X'} && $self->{'X'}->{__PACKAGE__.'.count'}
73             }
74              
75             sub ungrab {
76 0     0 1   my ($self) = @_;
77             ### GrabServer-object ungrab()
78 0 0         if (delete $self->{'grabbed'}) {
79 0   0       my $X = $self->{'X'} || return;
80 0 0         if (--$X->{__PACKAGE__.'.count'} <= 0) {
81 0           delete $X->{__PACKAGE__.'.count'}; # cleanup
82             ### final X->UngrabServer
83 0           $X->UngrabServer;
84 0           $X->flush;
85             }
86             }
87             ### grab count now: $self->{'X'} && $self->{'X'}->{__PACKAGE__.'.count'}
88             }
89              
90             sub is_grabbed {
91 0     0 1   my ($self) = @_;
92 0           return $self->{'grabbed'};
93             }
94              
95             # # not sure about this ...
96             # sub call_with_grab {
97             # my $class = shift;
98             # my $X = shift;
99             # my $subr = shift;
100             # my $grab = $class->new ($X);
101             # &$subr (@_);
102             # }
103              
104             1;
105             __END__