forked from tagtime/TagTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagtimed.pl
executable file
·140 lines (105 loc) · 3.92 KB
/
tagtimed.pl
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
#!/usr/bin/env perl
# TagTime daemon: this figures out from scratch when to beep and does so,
# continuously, even when the previous ping is still unanswered.
# After each ping it also runs launch.pl (with the 'quiet' arg since
# this is already doing the beeping) which launches popups or an editor
# for any overdue pings.
# Might be nice to watch ~/.tagtimerc (aka settings.pl) for changes a la
# watching.pl so that we don't have to restart this daemon when settings
# change. (does it suffice to just re-require it?)
=head1 NAME
tagtimed - Stochastic tagging of your time!
=head1 SYNPOSIS
$ tagtimed.pl # Start tagtime
$ tagtimed.pl -l # Start tagtime if not already running.
$ tagtimed.pl -s # Start tagtime if not already running, be silent otherwise.
=head1 DESCRIPTION
See L<http://messymatters.com/tagtime/>
You can add the following to your C<.bashrc> or equivalent to make sure TagTime starts
when you open a shell:
tagtimed.pl -l -s &
=head1 BUGS
The C<-l> switch must be used on all processes for locks to work correctly.
The C<-l> switch will exit Tagtime if it's already running with another user using the C<-l>
switch.
=cut
{
use strict;
use warnings;
use Fcntl qw(:flock);
use Getopt::Std qw(getopts);
# Lock our process if required. For more details, see:
# http://perltraining.com.au/tips/2005-08-08.html
# Note: without the '-l' switch, we won't even try to create a lock.
my %opts = ( l => 0, s => 0); # [l]ock and [s]ilent
getopts('ls',\%opts);
# Lock process if called with -l switch
if ($opts{l} and not flock(DATA,LOCK_EX|LOCK_NB)) {
# Oops, we're already locked...
exit(0) if $opts{s}; # Silent exit on already locked.
die "Already running\n"; # Noisy exit otherwise.
}
}
$launchTime = time();
eval {
require "$ENV{HOME}/.tagtimerc";
};
if ($@) {
die "$0: $ENV{HOME}/.tagtimerc can't be loaded ($!). Do you need to run install.py?\n"
}
require "${path}util.pl";
# TN, 2013/18/12: name the process for ps/top etc.
$0 = "tagtimed";
my $lstping = prevping($launchTime);
my $nxtping = nextping($lstping);
if($cygwin) { unlock(); } # on cygwin may have stray lock files around.
$cmd = "${path}launch.pl"; # Catch up on any old pings.
system($cmd) == 0 or print "SYSERR: $cmd\n";
print "TagTime is watching you! Last ping would've been ",
ss(time()-$lstping), " ago.\n";
my $start = time();
my $i = 1;
while(1) {
# sleep till next ping but check again in at most a few seconds in
# case computer was off (should be event-based and check upon wake).
sleep(clip($nxtping-time(), 0, 2));
$now = time();
if($nxtping <= $now) {
if($catchup || $nxtping > $now-$retrothresh) {
if(!defined($playsound)) { print STDERR "\a"; }
else { system("$playsound") == 0 or print "SYSERR: $playsound\n"; }
}
# invokes popup for this ping plus additional popups if there were more
# pings while answering this one:
$cmd = "${path}launch.pl quiet &";
system($cmd) == 0 or print "SYSERR: $cmd\n";
print annotime(padl($i," ",4).": PING! gap ".
ss($nxtping-$lstping)." avg ".
ss((0.0+time()-$start)/$i). " tot ".
ss(0.0+time()-$start), $nxtping, 72), "\n";
$lstping = $nxtping;
$nxtping = nextping($nxtping);
$i++;
}
}
# Invoke popup for this ping plus additional popups if there were more pings
# while answering this one.
sub pingery {
# TODO: move everything from launch.pl to here
return 0;
}
#SCHDEL (scheduled for deletion):
#check if X11 is already running, and if not, start it
#$X11= '/Applications/Utilities/X11.app/Contents/MacOS/X11 &';
#if (-e $X11) {
# $filename='/tmp/.X0-lock';
# my $xorg=`ps -A|grep -c 'X11.app'`;
# print $xorg;
# #unless (-e $filename || $xorg>1) {
# unless ($xorg>2) {
# `$X11`
# }
#}
__DATA__
This section exists to make it trivial to implement the -l (lock)
feature of tagtimed.