1package Clipboard::Xclip;
2$Clipboard::Xclip::VERSION = '0.28';
3use strict;
4use warnings;
5
6use File::Spec ();
7
8sub copy {
9    my $self = shift;
10    my ($input) = @_;
11    return $self->copy_to_selection($self->favorite_selection, $input);
12}
13
14sub copy_to_all_selections {
15    my $self = shift;
16    my ($input) = @_;
17    foreach my $sel ($self->all_selections) {
18        $self->copy_to_selection($sel, $input);
19    }
20    return;
21}
22
23sub copy_to_selection {
24    my $self = shift;
25    my ($selection, $input) = @_;
26    my $cmd = '|xclip -i -selection '. $selection;
27    my $r = open my $exe, $cmd or die "Couldn't run `$cmd`: $!\n";
28    binmode $exe, ':encoding(UTF-8)';
29    print {$exe} $input;
30    close $exe or die "Error closing `$cmd`: $!";
31
32    return;
33}
34sub paste {
35    my $self = shift;
36    for ($self->all_selections) {
37        my $data = $self->paste_from_selection($_);
38        return $data if length $data;
39    }
40    return undef;
41}
42sub paste_from_selection {
43    my $self = shift;
44    my ($selection) = @_;
45    my $cmd = "xclip -o -selection $selection|";
46    open my $exe, $cmd or die "Couldn't run `$cmd`: $!\n";
47    my $result = join '', <$exe>;
48    close $exe or die "Error closing `$cmd`: $!";
49    return $result;
50}
51# This ordering isn't officially verified, but so far seems to work the best:
52sub all_selections { qw(primary buffer clipboard secondary) }
53sub favorite_selection { my $self = shift; ($self->all_selections)[0] }
54
55sub xclip_available {
56    # close STDERR
57    open my $olderr, '>&', \*STDERR;
58    close STDERR;
59    open STDERR, '>', File::Spec->devnull;
60
61    my $open_retval = open my $just_checking, 'xclip -o|';
62
63    # restore STDERR
64    close STDERR;
65    open STDERR, '>&', $olderr;
66    close $olderr;
67
68    return $open_retval;
69}
70
71{
72  xclip_available() or warn <<'EPIGRAPH';
73
74Can't find the 'xclip' script.  Clipboard.pm's X support depends on it.
75
76Here's the project homepage: http://sourceforge.net/projects/xclip/
77
78EPIGRAPH
79}
80
811;
82
83__END__
84
85=pod
86
87=encoding UTF-8
88
89=head1 VERSION
90
91version 0.28
92
93=for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
94
95=head1 SUPPORT
96
97=head2 Websites
98
99The following websites have more information about this module, and may be of help to you. As always,
100in addition to those websites please use your favorite search engine to discover more resources.
101
102=over 4
103
104=item *
105
106MetaCPAN
107
108A modern, open-source CPAN search engine, useful to view POD in HTML format.
109
110L<https://metacpan.org/release/Clipboard>
111
112=item *
113
114RT: CPAN's Bug Tracker
115
116The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
117
118L<https://rt.cpan.org/Public/Dist/Display.html?Name=Clipboard>
119
120=item *
121
122CPANTS
123
124The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
125
126L<http://cpants.cpanauthors.org/dist/Clipboard>
127
128=item *
129
130CPAN Testers
131
132The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
133
134L<http://www.cpantesters.org/distro/C/Clipboard>
135
136=item *
137
138CPAN Testers Matrix
139
140The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
141
142L<http://matrix.cpantesters.org/?dist=Clipboard>
143
144=item *
145
146CPAN Testers Dependencies
147
148The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
149
150L<http://deps.cpantesters.org/?module=Clipboard>
151
152=back
153
154=head2 Bugs / Feature Requests
155
156Please report any bugs or feature requests by email to C<bug-clipboard at rt.cpan.org>, or through
157the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Clipboard>. You will be automatically notified of any
158progress on the request by the system.
159
160=head2 Source Code
161
162The code is open to the world, and available for you to hack on. Please feel free to browse it and play
163with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
164from your repository :)
165
166L<https://github.com/shlomif/Clipboard>
167
168  git clone git://github.com/shlomif/Clipboard.git
169
170=head1 AUTHOR
171
172Shlomi Fish <shlomif@cpan.org>
173
174=head1 BUGS
175
176Please report any bugs or feature requests on the bugtracker website
177L<https://github.com/shlomif/Clipboard/issues>
178
179When submitting a bug or request, please include a test-file or a
180patch to an existing test-file that illustrates the bug or desired
181feature.
182
183=head1 COPYRIGHT AND LICENSE
184
185This software is copyright (c) 2021 by Ryan King <rking@panoptic.com>.
186
187This is free software; you can redistribute it and/or modify it under
188the same terms as the Perl 5 programming language system itself.
189
190=cut
191