ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/CV/bin/cv
Revision: 1.88
Committed: Sat Jul 4 22:49:21 2015 UTC (11 years, 3 months ago) by root
Branch: MAIN
Changes since 1.87: +1 -0 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.88 ctrl-shift-p same as ctrl-p, but automatically selects "ok"
369 root 1.50 escape cancel a crop action
370 root 1.11
371 root 1.33 And when playing movies, these additional keys are active:
372    
373 root 1.50 left rewind by 10 seconds
374     right forward by 10 seconds
375     down rewind by 60 seconds
376     up forward by 60 seconds
377     pg_up rewind by 600 seconds
378     pg_down forward by 600 seconds
379     o toggle on-screen display
380     p pause/unpause
381     escape stop playing
382     9 turn volume down
383     0 turn volume up
384 root 1.33
385 root 1.47 Any other keys will be sent to the default schnauzer window, which can be
386     toggled on and off by right-clicking into the image window.
387 root 1.11
388 root 1.47 Left-clicking into the image window will let you crop the image (usually
389     to zoom into large images that CV scales down).
390 root 1.11
391     =head2 THE VISUAL SCHNAUZER
392    
393 root 1.63 Any image-loading action in a schnauzer window acts on the
394     "last-recently-activated" imagewindow, which currently is simply the last
395     image window that received a keypress.
396    
397 root 1.11 You can use the following keys in the schnauzer window:
398    
399 root 1.47 ctrl-space,
400 root 1.50 space move to and display next image
401 root 1.47 ctrl-backspace,
402 root 1.50 backspace move to and display previous image
403 root 1.47 ctrl-return,
404 root 1.50 return display selected picture, or enter directory
405 root 1.11
406 root 1.50 cursor keys move selection
407     page-up move one page up
408     page-down move one page down
409     home move to first file
410     end move to last file
411    
412     ctrl-a select all files
413     ctrl-shift-a select all files currently displayed in the schnauzer window
414     ctrl-d delete selected files WITHOUT ASKING AGAIN
415     ctrl-g force generation of thumbnais for the selected files
416     ctrl-s rescan current direcory or files updates/deletes etc.
417     ctrl-u update selected (or all) icons if neccessary
418 root 1.76 ctrl-- unselected thumbnailed images
419     ctrl-+ keep only thumbnailed images, deselect others
420 root 1.47
421 root 1.56 ^ go to parent directory (caret).
422    
423 root 1.47 0-9,
424 root 1.50 a-z find the first filename beginning with this letter
425 root 1.47
426     Right-clicking into the schnauzer window displays a pop-up menu with
427     additional actions.
428    
429 root 1.60 =head3 SELECTION
430    
431     You can select entries in the Schnauzer in a variety of ways:
432    
433     =over 4
434    
435     =item Keyboard
436    
437     Moving the cursor with the keyboard will first deselect all files and then
438     select the file you moved to.
439    
440 root 1.61 =item Clicking
441 root 1.60
442 root 1.61 Clicking on an entry will select the one you clicked and deselect all
443     others.
444 root 1.60
445 root 1.61 =item Shift-Clicking
446    
447     Shift-clicking will toggle the selection on the entry under the mouse.
448    
449     =item Dragging
450 root 1.60
451     Dragging will select all entries between the one selected when pushing the
452     button and the one selected when releasing the button. If you move above
453     or below the schnauzer area while drag-selecting, the schnauzer will move
454     up/down one row twice per second. In addition, horizontal mouse movement
455     acts as a kind of invisible horizontal scrollbar.
456    
457     =item Hint: double-click works while click-selecting
458    
459     You can double-click any image while click-selecting to display it
460     without stopping the selection process. This will act as if you normally
461     double-clicked the image to display it, and will toggle the selection
462     twice, resulting in no change.
463    
464     =back
465    
466 root 1.47 =head1 FILES
467    
468     When starting, CV runs the F<.cvrc> file in your F<$HOME> directory as if
469     it were a perl script. in that, you will mostly load plug-ins.
470    
471     Example:
472    
473     system "fping -q -t 10 ether"
474     or require "/fs/cv/cvplugin.pl";
475    
476     This will load a plug-in, but only if the machine I<ether> is reachable
477     (supposedly the plug-in is networked in some way :).
478 root 1.11
479 root 1.79 =head1 ENVIRONMENT VARIABLES
480 root 1.27
481     =over 4
482    
483 root 1.38 =item CV_EDITOR
484    
485     The program that gets executed when the user presses C<CTRL-e> in the
486     Schnauzer or image window. The default is C<gimp>.
487    
488 root 1.70 =item CV_AUDIO_PLAYER
489    
490     Program used to play all sorts of audio (wav, aif, mp3, ogg...), default "play".
491     Will be called like C<< $CV_AUDIO_PLAYER -- <path> >>.
492    
493 root 1.27 =item CV_PRINT_DESTINATION
494    
495     The default (perl-style) destination to use in the print dialog.
496    
497 root 1.38 =item CV_TRASHCAN
498    
499 root 1.78 When set, must point to a directory where all files that are deleted by
500     the "Delete Physically" (ctrl-d) action are moved to (other deletion
501     actions still delete!). If unset, files that are deleted are really being
502     deleted.
503 root 1.38
504 root 1.27 =back
505    
506 root 1.79 =head1 SIGNALS
507    
508     Sending CV a SIGUSR1 signal will cause all image viewers to reload the
509     currently loaded image. This is useful if you use CV as a viewer for
510     changing data - just run it in the background with some path and each time
511     the image changes, send it a SIGUSR1.
512    
513 root 1.23 =head1 SECURITY CONSIDERATIONS
514    
515 root 1.54 CV uses Pixbuf to load non-JPEG images. Pixbuf is not considered safe for
516     this purpose, though (from the gtk-2.2 release notes):
517 root 1.23
518     "While efforts have been made to make gdk-pixbuf robust against invalid
519     images, using gdk-pixbuf to load untrusted data is not recommended, due to
520     the likelyhood that there are additional problems where an invalid image
521     could cause gdk-pixbuf to crash or worse."
522    
523 root 1.11 =head1 BUGS/TODO
524 root 1.23
525 root 1.47 Lots of functionality is missing.
526    
527     Pixbuf doesn't always honor G_BROKEN_FILENAMES, so accessing files with
528 root 1.54 names incompatible with utf-8 might fail.
529 root 1.11
530     rotate on disk
531     lots of ui issues
532     save(?)
533     preferences
534    
535     =head1 AUTHOR
536    
537     Marc Lehmann <cv@plan9.de>.
538    
539     =cut
540 root 1.1