summaryrefslogtreecommitdiff
path: root/cam/feh-cam
blob: 014b22b0193ce8d66c171237e09104f319892cb4 (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
187
188
189
190
191
192
#!/usr/bin/perl -w

use strict;
use Getopt::Long;

#############################################
############# CAM RUN-TIME OPTIONS ##########
#############################################
my $feh   = "feh";

# additional feh cmdline options
my $PRE  = " -q -G -Twebcam -1 0 -0 1 ";
my $POST = ""; 
#############################################
#############################################

# Options
my $help        = '';
my $fullscreen  = '';
my $geometry    = '';
my $list        = '';
my $verbose     = '';
my $add         = '';
my $keep        = '';
my $deftitle    = '%cCAM - %u';
my $title       = '';
my $bp          = $ENV{HOME}."/.cam_bookmarks";
my $DEBUG       = 0;

# check args
&print_usage_and_exit unless (@ARGV);


# Url, Refresh, and bookmarks
my $url = "";
my $ref = "";
my %bms = ();

GetOptions('help|?|h'                    => \$help,
           'full-screen|f|giblets-mom' => \$fullscreen,
           'list|l'                      => \$list,
           'geometry|g=s'                => \$geometry,
           'verbose|v'                   => \$verbose,
           'add|a'                       => \$add,
           'keep-images|k|save-pr0n'     => \$keep,
           'title|t=s'                   => \$title,
           'debug|d'                     => \$DEBUG,
           'bookmarks|b=s'               => \$bp
           );

my $key = shift @ARGV;

&print_usage_and_exit if ($help);

if ($verbose)    {
    $PRE .= " -V ";
}

if ($fullscreen) {
      $PRE =~ s/-w//;
      $PRE .= " --full-screen --auto-zoom ";
}

if ($geometry) {
  $PRE .= " --geometry $geometry ";
}

# if requested, add a key/url pair to bookmarks file
if ($add)    {
    my $mytitle          = '';
    ($url,$ref,$mytitle) = @ARGV;
    die "Bad key syntax\n" unless ($key && $url && $ref);

    $mytitle="" unless ($mytitle);

    open(BMF, ">>$bp") or die "Couldn't open bookmarks file \"$bp\": $!\n";
    print BMF "$key=$ref,$url \"$mytitle\"\n";
    close BMF;
    print "Added URL key \"$key\" = $url, $ref.\n"; # its useful to have this even if you arent debugging --richlowe
    exit 0;
}

if ($keep)    {
    $PRE .= " -k ";
}



# load bookmarks
open(BMF, "$bp") or die "Couldn't open bookmarks file \"$bp\": $!\n";
foreach (<BMF>)    {
    next unless /^(.*?)=(.*)$/;
    $bms{$1} = $2;
    print "key=$1, url=$2\n" if ($DEBUG);
}
close BMF;

# if requested, dump a list of key/url pair values
if ($list) {
    foreach (sort keys %bms)    {
        my $t = $bms{$_}; 

           $t =~ s/^(.+?),(.+?)(^ "(.*)"|)?$/$2/;
        chomp $t;
        print "$_ = $t, $1, $3\n";
    }
    exit 0;
}

# main loop
MAIN: {
    do {
        $title="";
        $url = $bms{$key};
        die "Couldn't find URL key \"$key.\"\n" unless($url);
        
        $url =~ s/^(.+?),(.+?)( "(.*)")?$/$2/;
        $ref = $1;
   
         if ($4) {
            $title = $4;
         } else {
           $title = $deftitle;
         }

        if ($title) {
            $title =~ s/\%c/$key/g;
            $title =~ s/\%u/$url/g;
            $title =~ s/\%r/$ref/g;
            $title =~ s/\%\%/\%/g;
            $title = " --title \"$title\" ";
        }
    
        my $cmd = "$feh $PRE $title -T".$key."cam -R $ref $url $POST";
        print "$cmd\n" if ($DEBUG);
        FORK: {
           my $pid;
            if ($pid = fork) {
                # We're a daddy! :)
            } elsif (defined $pid) {
                # child
                exec "$cmd" or die "Couldnt exec() $feh: $!\n";
            } elsif ( $! =~ /No more process/) {
                sleep 5;
                redo FORK;
            } else {
                # wtf?
                die "Unrecoverable fork() error: $!\n";
            }
        }
    } while ($key = shift @ARGV);
}

   
sub print_usage_and_exit()    {
    print <<END_USAGE;
$0 0.4
by Paul Duncan <pabs\@pablotron.org>, and
   Richard Lowe <richlowe\@btinternet.com>

Description:
  A convenient webcam wrapper for feh.

Usage:
  $0 <keys>
    Load the urls specified by the given keys.
    key : a url key stored in the bookmarks file (\"$bp\").
  $0 <-a|--add> key url refresh
    Add a key to the bookmarks file.
    key : short key (ex \"jenni\"),
    url : url (ex \"http://www.jennicam.org/webcam/cam.jpg\"),
    refresh : refresh, in seconds (ex 120)
  $0 <-l|--list>
    List each url key in the bookmarks file (\"$bp\").
  $0 [-f|--full-screen] keys
    Start feh in full-screen mode (aka --giblets-mom \"viewing\" mode).
  $0 [-k|--keep-images] keys
    Save each image in the current directory (aka --save-pr0n).
  $0 [-g|--geometry] xxx
    Use window geometry xxx (e.g. 640x480).
  $0 [-v|--verbose] options
    Start feh in verbose mode (feh -V).
  $0 <-h|-?|--help>
    Display usage information (this screen).

Notes:
Thanks to giblet for feh, an awesome image and webcam viewing program,
and raster for Imlib2.
END_USAGE
    exit(-1);
}