summaryrefslogtreecommitdiffstats
path: root/clipbored
blob: 5a29413a168fd4afaa35d0e816eaff5dd940cb61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/perl
use strict;
use Getopt::Long;
use Pod::Usage;
use File::Path 'make_path';

my $xsel_log = "$ENV{XDG_DATA_HOME}/clipbored/clips";
my $pidfile  = '/tmp/clipbored.pid';

if(!-f $xsel_log) {
  make_path("$ENV{XDG_DATA_HOME}/clipbored");
  open(my $fh, '>', $xsel_log) or die($!);
  close($fh);
}

our($opt_no_daemon) = undef;
GetOptions(
 'no-daemon' => \$opt_no_daemon,
 'kill'      => \&killkid,
 'clear'     => sub {
   if(-e $xsel_log) {
     open (my $fh, '>', $xsel_log) or die("Could not open $xsel_log: $!");
     close($fh);
   }
   print "$xsel_log cleared\n";
   exit(0);
   },
 'last:i'    => \&lastlog,
 'help'      => sub {pod2usage(-verbose => 1) and exit(0)},
 'man'       => sub {pod2usage(-verbose => 3) and exit(0)},
);

if(-f $pidfile) {
  print "clipbored is already running\n";
  exit(1);
}

sync_cb();

sub lastlog {
  shift;
  my $wayback = shift // 25;
  if($wayback < 1) {
    $wayback = 25;
  }
  open(my $fh, '<', $xsel_log) or die("Could not open $xsel_log: $!");
  my @records = <$fh>;
  close($fh);

  if(scalar(@records) < $wayback) {
    $wayback = scalar(@records);
  }

  my $i = 0;
  for(@records[0 .. $wayback-1]) {
    printf("\e[1m%2d\e[0m %s", $i, $_);
    $i++;
  }
  exit(0);
}


sub sync_cb {
  daemonize() unless(defined($opt_no_daemon));
  while(1) {
    chomp(my $current_selection = `/usr/bin/xclip -o`);
    if(defined($current_selection)) {
      open(my $r_xsel, '<', $xsel_log) or die("Cant open $xsel_log: $!");
      chomp(my @selections = <$r_xsel>);
      close($r_xsel);
      $current_selection =~ s/\n/ /g; # newline hassle

      if($current_selection ~~ @selections) {

      }
      else {
        open(my $a_xsel, '>>', $xsel_log) or die("Cant open $xsel_log: $!");
        print $a_xsel $current_selection, "\n";
        close($a_xsel);
        print $current_selection, "\n" if(defined($opt_no_daemon));
      }
    }
    else {
    }
    sleep 2;
  }
}

sub killkid {
  open(my $fh, '<', $pidfile) or print "clipbored is not running\n" and exit(1);
  my $target = <$fh>;
  close($fh);

  if(kill(9, $target)) {
    print "clipbored with PID $target terminated\n";
  }
  else {
    print "Could not kill $target: $!";
  }
  exit(0);
}

sub daemonize {
  use POSIX 'setsid';
  my $PID = fork();
  exit(0) if($PID); #parent
  exit(1) if(!defined($PID)); # out of resources

  setsid();
  $PID = fork();
  exit(1) if(!defined($PID));

  if($PID) { # parent
    waitpid($PID, 0);
    unlink($pidfile); # remove the lock when child have died
    exit(0);
  }
  elsif($PID == 0) { # child
    open(my $fh, '>', $pidfile) or die("Cant open $pidfile: $!");
    print $fh $$;
    close($fh);
    open(STDOUT, '>', '/dev/null');
    open(STDERR, '>', '/dev/null');
    open(STDIN,  '<', '/dev/null');
  }
}


=pod

=head1 NAME

  clipbored - continuously collects all selections in Xorg's clipboard buffers

=head1 SYNOPSIS

  clipbored [OPTIONS]

=head1 DESCRIPTION

B<clipbored> is a daemon that continuously grabs all non-duplicate selections
in the X.org clipboard buffers and writes them to a plaintext history file for
later use.

There are several scripts distributed with clipbored that'll use the history
file for different purposes.

=head2 Scripts

  dmenurl   - launch dmenu with all previously yanked URLs for you to select
              from.

  dmenuclip - launch dmenu listing all previously clipboarded content

  fmenuclip - do the same thing but vertically

=head1 OPTIONS

  -l,   --last        show the n latest additions
  -c,   --clear       clear all history
  -n,   --no-daemon   do not detach from the shell
  -k,   --kill        kill a running clipbored session
  -h,   --help        show this help
  -m,   --man         display the manual

=head1 ENVIRONMENT

The history file will be placed in $XDG_DATA_HOME/clipbored/clips

=head1 AUTHOR

Written by Magnus Woldrich.

=head1 REPORTING BUGS

Report bugs to [email protected]

clipbored home page: <http://github.com/trapd00r/clipbored/>

=head1 COPYRIGHT

(C) Copyright 2010 Magnus Woldrich.

License GPLv2: GNU GPL version 2

=cut