ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/bin/cv
(Generate patch)

Comparing CV/bin/cv (file contents):
Revision 1.1 by root, Sun Nov 2 12:55:41 2003 UTC vs.
Revision 1.71 by root, Thu Oct 4 10:30:12 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines