ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/CV/bin/cv
Revision: 1.87
Committed: Fri Apr 5 02:30:46 2013 UTC (13 years, 6 months ago) by root
Branch: MAIN
Changes since 1.86: +5 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3 root 1.80 use common::sense;
4    
5 root 1.37 use Cwd ();
6     use Encode ();
7 root 1.72 use File::Glob ();
8 root 1.79 use Scalar::Util ();
9 root 1.37
10 root 1.87 use Gtk2;
11 root 1.12 use Gtk2::Gdk::Keysyms;
12    
13 root 1.48 use Gtk2::CV;
14    
15 root 1.12 use Gtk2::CV::ImageWindow;
16     use Gtk2::CV::Schnauzer;
17    
18 root 1.48 BEGIN {
19     require Gtk2::CV::Plugin;
20     require "$ENV{HOME}/.cvrc" if -r "$ENV{HOME}/.cvrc";
21     }
22    
23 root 1.82 use Carp (); $Carp::MaxArgLen = 256;#d#
24    
25 root 1.48 use Gtk2::CV::Plugin::NameCluster;
26     use Gtk2::CV::Plugin::RCluster;
27 root 1.81 use Gtk2::CV::Plugin::PatRenamer;
28 root 1.16
29 root 1.86 use AnyEvent::Fork::Template;
30    
31 root 1.87 Gtk2::CV::Jobber::set_template $AnyEvent::Fork::Template;
32    
33     # now we can initialize Gtk2 etc.
34     init Gtk2;
35 root 1.77
36 root 1.16 Gtk2::Rc->parse (Gtk2::CV::find_rcfile "gtkrc");
37    
38 root 1.20 use File::Spec;
39    
40     my $mainwin;
41 root 1.12 my $viewer;
42 root 1.62 my $viewer_count;
43 root 1.12 my $schnauzer;
44 root 1.20 my $info;
45 root 1.21 my $help;
46 root 1.12
47 root 1.25 my $schnauzer_idx = 0;
48    
49 root 1.12 sub new_schnauzer {
50 root 1.20 my $s = new Gtk2::CV::Schnauzer;
51 root 1.12
52     $s->signal_connect_after (key_press_event => \&std_keys);
53 root 1.20 $s->signal_connect (activate => sub {
54     my $label = sprintf "%s (%d)",
55 root 1.75 (Glib::filename_display_name +(File::Spec->splitpath ($_[1]))[2]),
56 root 1.20 -s $_[1];
57     $info->set_label ($label);
58 root 1.62 $viewer->load_image ($_[1]) if $viewer; # TODO: error, or chose ANY viewer
59 root 1.20 });
60 root 1.12
61 root 1.40 Gtk2::CV::Plugin->call (new_schnauzer => $s);
62    
63 root 1.53 $s
64 root 1.12 }
65    
66 root 1.79 our %VIEWER; # global viewer container so we can propagate signals
67    
68     $SIG{USR1} = sub {
69     # I assume glib calls us in a safe enough context to create an idle watcher
70     add Glib::Idle sub {
71     $_->reload for values %VIEWER;
72     0
73     };
74     };
75    
76 root 1.62 sub new_viewer {
77     my $self = new Gtk2::CV::ImageWindow;
78    
79 root 1.79 Scalar::Util::weaken ($VIEWER{$self+0} = $self);
80    
81 root 1.62 $viewer_count++;
82    
83     $self->set_title ("CV: Image");
84    
85     $self->signal_connect (key_press_event => sub {
86     $viewer = $_[0];
87    
88     my $key = $_[1]->keyval;
89     my $state = $_[1]->state;
90    
91     if ($state * "control-mask" && $key == $Gtk2::Gdk::Keysyms{c}) {
92     my $viewer = new_viewer ();
93     $viewer->set_image ($_[0]->{image});
94     $viewer->show_all;
95     1
96     } else {
97     &std_keys
98     or $schnauzer->signal_emit (key_press_event => $_[1])
99     }
100     });
101 root 1.63 $self->signal_connect (delete_event => sub { $_[0]->destroy; 0 });
102 root 1.62 $self->signal_connect (destroy => sub {
103 root 1.79 delete $VIEWER{$_[0]+0};
104 root 1.62 $viewer = undef if $viewer == $_[0];
105    
106     main_quit Gtk2 unless --$viewer_count;
107 root 1.63
108     0
109 root 1.62 });
110    
111     $self->signal_connect (button3_press_event => sub {
112     $mainwin->visible
113     ? $mainwin->hide
114     : $mainwin->show_all;
115 root 1.63
116 root 1.62 1
117     });
118    
119     Gtk2::CV::Plugin->call (new_imagewindow => $self);
120    
121     $self
122     }
123    
124 root 1.12 sub std_keys {
125     my $key = $_[1]->keyval;
126     my $state = $_[1]->state;
127    
128 root 1.21 my $ctrl = $state * "control-mask";
129 root 1.12
130     if ($key == $Gtk2::Gdk::Keysyms{q}) {
131 root 1.62 $viewer->destroy;
132 root 1.12 } elsif ($ctrl && $key == $Gtk2::Gdk::Keysyms{v}) {
133 root 1.20 my $w = new Gtk2::Window;
134 root 1.26
135 root 1.66 $w->set_role ("schnauzer");
136 root 1.26 $w->set_title ("CV: Schnauzer");
137 root 1.20 $w->add (my $s = new_schnauzer);
138     $s->set_dir (File::Spec->curdir);
139 root 1.25 $s->set_geometry_hints;
140 root 1.21 $w->show_all;
141 root 1.25
142 root 1.21 } elsif ($ctrl && $key == $Gtk2::Gdk::Keysyms{h}) {
143     unless ($help) {
144 root 1.73 require Gtk2::Ex::PodViewer;
145 root 1.21
146     $help = new Gtk2::Window;
147 root 1.67 $help->set_role ("help");
148 root 1.26 $help->set_title ("CV: Help");
149 root 1.21 $help->set_default_size (500, 300);
150     $help->signal_connect (delete_event => sub { $help->hide; 1 });
151    
152     $help->add (my $sw = new Gtk2::ScrolledWindow);
153 root 1.73 $sw->add (my $h = new Gtk2::Ex::PodViewer);
154 root 1.21
155     #binmode DATA, ":utf8";
156     $h->load_string (do { local $/; <DATA> });
157     }
158    
159     $help->show_all;
160 root 1.83 } elsif (!$state && $Gtk2::Gdk::Keysyms{a} <= $key && $key <= $Gtk2::Gdk::Keysyms{z}) {
161 root 1.82 #
162 root 1.12 } else {
163 root 1.43 return 0;
164 root 1.12 }
165    
166 root 1.43 1
167 root 1.12 }
168    
169 root 1.20 {
170 root 1.62 $viewer = new_viewer;
171     $::cur_viewer = $viewer;
172 root 1.40
173 root 1.20 $schnauzer = new_schnauzer;
174    
175     $mainwin = new Gtk2::Window;
176 root 1.66 $mainwin->set_role ("main");
177 root 1.20 $mainwin->set_title ("CV");
178     $mainwin->add (my $vbox = new Gtk2::VBox);
179 root 1.53 $mainwin->signal_connect (delete_event => sub { $mainwin->hide; 1 });
180 root 1.20
181     $vbox->add ($schnauzer);
182 root 1.22 $vbox->pack_end (my $frame = new Gtk2::Frame, 0, 0, 0);
183 root 1.20 $frame->add (my $hbox = new Gtk2::HBox 0, 0);
184 root 1.45 $hbox->pack_start ((new Gtk2::Label "Info: "), 0, 0, 0);
185     $hbox->pack_end (my $labelwindow = new Gtk2::EventBox, 1, 1, 0);
186     $labelwindow->add ($info = new Gtk2::Label);
187     $labelwindow->signal_connect_after (size_request => sub { $_[1]->width (0); 0 });
188     $info->set (selectable => 1, xalign => 0, justify => "left");
189 root 1.25
190     $schnauzer->set_geometry_hints;
191 root 1.20 }
192 root 1.12
193     if (@ARGV) {
194 root 1.68 my $show_first = sub {
195     $schnauzer->show_all;
196 root 1.85
197     # activate first file, but avoid dirs
198     my $entry = $schnauzer->{entry}[0];
199     my $path = "$entry->[0]/$entry->[1]";
200    
201     $schnauzer->handle_key ($Gtk2::Gdk::Keysyms{space}, [])
202     unless -d $path;
203    
204 root 1.68 $viewer->show_all;
205     };
206    
207 root 1.71 if (@ARGV == 1 && $ARGV[0] eq "-0r") {
208     local $/;
209 root 1.74 $schnauzer->set_paths ([split /\x00/, <STDIN>], 1, $show_first);
210 root 1.71 } elsif (@ARGV == 1 && -d $ARGV[0]) {
211 root 1.74 $schnauzer->set_dir (shift, $show_first);
212 root 1.71 } else {
213 root 1.72 if ($ARGV[0] eq "-g") {
214     shift @ARGV;
215     @ARGV = map +(File::Glob::bsd_glob $_, File::Glob::GLOB_BRACE | File::Glob::GLOB_QUOTE), @ARGV;
216     }
217 root 1.74 $schnauzer->set_paths ([@ARGV], 1, $show_first);
218 root 1.71 }
219 root 1.17 } else {
220 root 1.68 $schnauzer->set_dir (File::Spec->curdir, sub {
221     $mainwin->show_all;
222     $viewer->show_all;
223     });
224 root 1.12 }
225    
226     main Gtk2;
227 root 1.21
228 root 1.69 Gtk2::CV::flush_aio;
229    
230 root 1.21 __DATA__
231 root 1.12
232 root 1.58 =encoding utf-8
233    
234 root 1.11 =head1 NAME
235    
236 root 1.47 cv - a fast gtk+ image viewer loosely modeled after XV
237 root 1.11
238     =head1 SYNOPSIS
239    
240 root 1.84 cv
241    
242     cv directory
243    
244     cv path...
245    
246     cv -g <glob expression...>
247    
248     find .. -print0 | cv -0r
249 root 1.11
250 root 1.47 =head1 FEATURES
251    
252     CV is supposed to work similar to the venerable XV image viewer, just
253     faster. Why faster?
254    
255     =over 4
256    
257     =item * optimized directory scanning algorithm
258    
259 root 1.55 The directory scanning in CV uses some tricks that - on most modern
260 root 1.47 filesystems - makes it possible to detect filetypes faster than stat()'ing
261     every file. This makes CV suitable for directories with lots of files
262     (10000+).
263    
264     This algorithm is quite unprecise - it doesn't make a difference between
265     files, device nodes, symlinks and the like, and filetype detection is done
266     using the file extension only.
267    
268 root 1.55 On the positive side, it is usually many orders of magnitude faster than
269     traditional scanning techniques (good for directories with 10000 or
270     100000+ files).
271    
272 root 1.53 =item * queuing for all time-consuming background tasks
273    
274     All tasks, such as unlinking files or generating thumbnails, that can be
275     done in the background will be done so - no waiting required, even when
276     changing directories.
277    
278 root 1.47 =item * use of asynchronous I/O
279    
280     CV tries to use asynchronous I/O whereever it makes sense, for example
281 root 1.53 while scanning directories, waiting for stat data, unlinking files or
282     generating thumbnails. This usually decreases scanning times for large
283     directories a bit (especially on RAID devices and over NFS) and makes CV
284     much more interactive.
285 root 1.47
286     =item * fast image loading
287    
288     The time span between the user issuing a command and displaying the new
289     image should be as small as possible. CV uses optimized (especially
290     for JPEG) loading functions and sacrifices some quality (e.g no gamma
291 root 1.55 correction, although this might change) to achieve this speed.
292 root 1.47
293     =item * fast thumbnail creation
294    
295 root 1.55 Thumbnail creation uses both CPU and Disk-I/O. CV interleaves both, so
296     on modern CPUs, thumbnailing is usually limited by I/O speed. Thumbnail
297     creation for JPEGs has been specially optimized and can even take
298     advantage of multiple CPUs.
299 root 1.47
300     =item * minimum optical clutter
301    
302     CV has no menus or other user interface elements that take up a lot of
303 root 1.55 screen space (or are useful for beginning users). The schnauzer windows
304     can also be somewhat crowded.
305 root 1.47
306     The point of an image viewer is viewing images, not a nice GUI. This is
307     similar to XV's behaviour.
308    
309     =item * efficient (and hard to learn) user interface
310    
311     CV uses key combinations. A lot. If you are an experienced XV user, you
312     will find most of these keys familiar. If not, CV might be hard to use at
313     first, but will be an efficient tool later.
314    
315 root 1.55 =item * multi-window GUI
316    
317     CV doesn't force you to use a specific layout, instead it relies on your
318     window manager, thus enabling you to chose whatever layout that suits you
319     most.
320    
321 root 1.47 =item * i18n'ed filename handling throughout
322    
323     As long as glib can recognize your filename encoding (either UTF-8 or
324 root 1.55 locale-specific, depending on the setting of G_BROKEN_FILENAMES) and you
325     have the relevant fonts, CV will display your filenames correctly.
326 root 1.47
327     =item * extensible through plug-ins
328    
329     I have weird plug-ins that access remote databases to find a
330     directory. This is not likely to be of any use to other people. Likewise,
331     others might have weird requirements I cannot dream of.
332    
333     =item * filename clustering
334    
335     Among the standard plug-ins is a filename clustering plug-in, that (in
336     case of tens of thousands images in one directory) might be able to
337     cluster similar names together.
338    
339     =back
340    
341 root 1.11 =head1 DESCRIPTION
342    
343     =head2 THE IMAGE WINDOW
344    
345     You can use the following keys in the image window:
346    
347 root 1.50 q quit the program
348     < half the image size
349     > double the image size
350     , shrink the image by 10%
351     . enlarge the image by 10%
352     n reset to normal size
353     m maximize to screensize
354     M maximize to screensize, respecting image aspect
355     ctrl-m toggle maxpect-always mode
356 root 1.59 ctrl-sift-m toggle using current image size as max image size
357 root 1.50 u uncrop
358     r set scaling mode to 'nearest' (fastest)
359     s set scaling mode to 'bilinear' (default)
360     shift-s set scaling mode to 'hyper' (slowest)
361     t rotate clockwise 90°
362     T rotate counterclockwise°
363 root 1.64 a apply all rotations loslessly to a jpeg file (using exiftran)
364 root 1.50 ctrl-v open a new visual schnauzer window for the current dir
365 root 1.63 ctrl-c clone the current image window
366 root 1.50 ctrl-e run an editor ($CV_EDITOR or "gimp") on the current image
367 root 1.57 ctrl-p fire up the print dialog
368 root 1.50 escape cancel a crop action
369 root 1.11
370 root 1.33 And when playing movies, these additional keys are active:
371    
372 root 1.50 left rewind by 10 seconds
373     right forward by 10 seconds
374     down rewind by 60 seconds
375     up forward by 60 seconds
376     pg_up rewind by 600 seconds
377     pg_down forward by 600 seconds
378     o toggle on-screen display
379     p pause/unpause
380     escape stop playing
381     9 turn volume down
382     0 turn volume up
383 root 1.33
384 root 1.47 Any other keys will be sent to the default schnauzer window, which can be
385     toggled on and off by right-clicking into the image window.
386 root 1.11
387 root 1.47 Left-clicking into the image window will let you crop the image (usually
388     to zoom into large images that CV scales down).
389 root 1.11
390     =head2 THE VISUAL SCHNAUZER
391    
392 root 1.63 Any image-loading action in a schnauzer window acts on the
393     "last-recently-activated" imagewindow, which currently is simply the last
394     image window that received a keypress.
395    
396 root 1.11 You can use the following keys in the schnauzer window:
397    
398 root 1.47 ctrl-space,
399 root 1.50 space move to and display next image
400 root 1.47 ctrl-backspace,
401 root 1.50 backspace move to and display previous image
402 root 1.47 ctrl-return,
403 root 1.50 return display selected picture, or enter directory
404 root 1.11
405 root 1.50 cursor keys move selection
406     page-up move one page up
407     page-down move one page down
408     home move to first file
409     end move to last file
410    
411     ctrl-a select all files
412     ctrl-shift-a select all files currently displayed in the schnauzer window
413     ctrl-d delete selected files WITHOUT ASKING AGAIN
414     ctrl-g force generation of thumbnais for the selected files
415     ctrl-s rescan current direcory or files updates/deletes etc.
416     ctrl-u update selected (or all) icons if neccessary
417 root 1.76 ctrl-- unselected thumbnailed images
418     ctrl-+ keep only thumbnailed images, deselect others
419 root 1.47
420 root 1.56 ^ go to parent directory (caret).
421    
422 root 1.47 0-9,
423 root 1.50 a-z find the first filename beginning with this letter
424 root 1.47
425     Right-clicking into the schnauzer window displays a pop-up menu with
426     additional actions.
427    
428 root 1.60 =head3 SELECTION
429    
430     You can select entries in the Schnauzer in a variety of ways:
431    
432     =over 4
433    
434     =item Keyboard
435    
436     Moving the cursor with the keyboard will first deselect all files and then
437     select the file you moved to.
438    
439 root 1.61 =item Clicking
440 root 1.60
441 root 1.61 Clicking on an entry will select the one you clicked and deselect all
442     others.
443 root 1.60
444 root 1.61 =item Shift-Clicking
445    
446     Shift-clicking will toggle the selection on the entry under the mouse.
447    
448     =item Dragging
449 root 1.60
450     Dragging will select all entries between the one selected when pushing the
451     button and the one selected when releasing the button. If you move above
452     or below the schnauzer area while drag-selecting, the schnauzer will move
453     up/down one row twice per second. In addition, horizontal mouse movement
454     acts as a kind of invisible horizontal scrollbar.
455    
456     =item Hint: double-click works while click-selecting
457    
458     You can double-click any image while click-selecting to display it
459     without stopping the selection process. This will act as if you normally
460     double-clicked the image to display it, and will toggle the selection
461     twice, resulting in no change.
462    
463     =back
464    
465 root 1.47 =head1 FILES
466    
467     When starting, CV runs the F<.cvrc> file in your F<$HOME> directory as if
468     it were a perl script. in that, you will mostly load plug-ins.
469    
470     Example:
471    
472     system "fping -q -t 10 ether"
473     or require "/fs/cv/cvplugin.pl";
474    
475     This will load a plug-in, but only if the machine I<ether> is reachable
476     (supposedly the plug-in is networked in some way :).
477 root 1.11
478 root 1.79 =head1 ENVIRONMENT VARIABLES
479 root 1.27
480     =over 4
481    
482 root 1.38 =item CV_EDITOR
483    
484     The program that gets executed when the user presses C<CTRL-e> in the
485     Schnauzer or image window. The default is C<gimp>.
486    
487 root 1.70 =item CV_AUDIO_PLAYER
488    
489     Program used to play all sorts of audio (wav, aif, mp3, ogg...), default "play".
490     Will be called like C<< $CV_AUDIO_PLAYER -- <path> >>.
491    
492 root 1.27 =item CV_PRINT_DESTINATION
493    
494     The default (perl-style) destination to use in the print dialog.
495    
496 root 1.38 =item CV_TRASHCAN
497    
498 root 1.78 When set, must point to a directory where all files that are deleted by
499     the "Delete Physically" (ctrl-d) action are moved to (other deletion
500     actions still delete!). If unset, files that are deleted are really being
501     deleted.
502 root 1.38
503 root 1.27 =back
504    
505 root 1.79 =head1 SIGNALS
506    
507     Sending CV a SIGUSR1 signal will cause all image viewers to reload the
508     currently loaded image. This is useful if you use CV as a viewer for
509     changing data - just run it in the background with some path and each time
510     the image changes, send it a SIGUSR1.
511    
512 root 1.23 =head1 SECURITY CONSIDERATIONS
513    
514 root 1.54 CV uses Pixbuf to load non-JPEG images. Pixbuf is not considered safe for
515     this purpose, though (from the gtk-2.2 release notes):
516 root 1.23
517     "While efforts have been made to make gdk-pixbuf robust against invalid
518     images, using gdk-pixbuf to load untrusted data is not recommended, due to
519     the likelyhood that there are additional problems where an invalid image
520     could cause gdk-pixbuf to crash or worse."
521    
522 root 1.11 =head1 BUGS/TODO
523 root 1.23
524 root 1.47 Lots of functionality is missing.
525    
526     Pixbuf doesn't always honor G_BROKEN_FILENAMES, so accessing files with
527 root 1.54 names incompatible with utf-8 might fail.
528 root 1.11
529     rotate on disk
530     lots of ui issues
531     save(?)
532     preferences
533    
534     =head1 AUTHOR
535    
536     Marc Lehmann <cv@plan9.de>.
537    
538     =cut
539 root 1.1