2011-01-17 14:57:59 +01:00
|
|
|
/* sxiv: main.c
|
|
|
|
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2011-01-17 16:41:50 +01:00
|
|
|
#include <stdlib.h>
|
2011-02-02 10:34:14 +01:00
|
|
|
#include <string.h>
|
2011-03-01 18:49:02 +01:00
|
|
|
#include <unistd.h>
|
2011-07-26 18:01:29 +02:00
|
|
|
#include <sys/stat.h>
|
2011-01-17 16:41:50 +01:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
#include "events.h"
|
2011-01-19 14:07:45 +01:00
|
|
|
#include "image.h"
|
2011-02-03 10:15:01 +01:00
|
|
|
#include "options.h"
|
2011-02-16 21:40:20 +01:00
|
|
|
#include "thumbs.h"
|
2011-02-03 10:15:01 +01:00
|
|
|
#include "util.h"
|
2011-01-19 14:07:45 +01:00
|
|
|
#include "window.h"
|
2011-04-14 12:00:35 +02:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
enum {
|
|
|
|
TITLE_LEN = 256,
|
|
|
|
FNAME_CNT = 1024
|
|
|
|
};
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-02-16 17:09:46 +01:00
|
|
|
appmode_t mode;
|
2011-01-19 14:07:45 +01:00
|
|
|
img_t img;
|
2011-02-16 21:40:20 +01:00
|
|
|
tns_t tns;
|
2011-01-19 14:07:45 +01:00
|
|
|
win_t win;
|
2011-01-20 16:24:48 +01:00
|
|
|
|
2011-05-29 11:45:58 +02:00
|
|
|
char **filenames;
|
2011-01-23 12:36:27 +01:00
|
|
|
int filecnt, fileidx;
|
2011-02-03 14:32:02 +01:00
|
|
|
size_t filesize;
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-01-20 17:00:59 +01:00
|
|
|
char win_title[TITLE_LEN];
|
|
|
|
|
2011-02-03 10:15:01 +01:00
|
|
|
void cleanup() {
|
|
|
|
static int in = 0;
|
|
|
|
|
|
|
|
if (!in++) {
|
2011-03-01 18:49:02 +01:00
|
|
|
img_close(&img, 0);
|
2011-04-11 16:58:38 +02:00
|
|
|
tns_free(&tns);
|
2011-02-03 10:15:01 +01:00
|
|
|
win_close(&win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
int check_add_file(char *filename) {
|
|
|
|
if (!filename)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (access(filename, R_OK)) {
|
|
|
|
warn("could not open file: %s", filename);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (fileidx == filecnt) {
|
|
|
|
filecnt *= 2;
|
|
|
|
filenames = (char**) s_realloc(filenames, filecnt * sizeof(char*));
|
|
|
|
}
|
|
|
|
filenames[fileidx++] = filename;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-11 16:58:38 +02:00
|
|
|
void remove_file(int n, unsigned char silent) {
|
|
|
|
if (n < 0 || n >= filecnt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (filecnt == 1) {
|
|
|
|
if (!silent)
|
2011-04-11 20:42:08 +02:00
|
|
|
fprintf(stderr, "sxiv: no more files to display, aborting\n");
|
2011-04-11 16:58:38 +02:00
|
|
|
cleanup();
|
|
|
|
exit(!silent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n + 1 < filecnt)
|
|
|
|
memmove(filenames + n, filenames + n + 1, (filecnt - n - 1) *
|
2011-05-29 11:45:58 +02:00
|
|
|
sizeof(char*));
|
2011-04-11 16:58:38 +02:00
|
|
|
if (n + 1 < tns.cnt) {
|
|
|
|
memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
|
|
|
|
sizeof(thumb_t));
|
|
|
|
memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
|
|
|
|
}
|
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
filecnt--;
|
2011-04-11 16:58:38 +02:00
|
|
|
if (n < tns.cnt)
|
2011-07-26 18:01:29 +02:00
|
|
|
tns.cnt--;
|
2011-04-11 16:58:38 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
void load_image(int new) {
|
2011-02-03 14:32:02 +01:00
|
|
|
struct stat fstats;
|
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
if (new < 0 || new >= filecnt)
|
|
|
|
return;
|
2011-04-11 16:58:38 +02:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
/* cursor is reset in redraw() */
|
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
|
|
|
img_close(&img, 0);
|
|
|
|
|
|
|
|
while (!img_load(&img, filenames[new])) {
|
|
|
|
remove_file(new, 0);
|
|
|
|
if (new >= filecnt)
|
|
|
|
new = filecnt - 1;
|
2011-02-27 13:29:24 +01:00
|
|
|
}
|
2011-07-26 18:01:29 +02:00
|
|
|
|
|
|
|
fileidx = new;
|
|
|
|
if (!stat(filenames[new], &fstats))
|
|
|
|
filesize = fstats.st_size;
|
|
|
|
else
|
|
|
|
filesize = 0;
|
2011-02-03 14:32:02 +01:00
|
|
|
}
|
|
|
|
|
2011-04-11 11:53:00 +02:00
|
|
|
void update_title() {
|
|
|
|
int n;
|
|
|
|
float size;
|
|
|
|
const char *unit;
|
|
|
|
|
|
|
|
if (mode == MODE_THUMBS) {
|
|
|
|
n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
|
|
|
|
tns.cnt ? tns.sel + 1 : 0, tns.cnt,
|
|
|
|
tns.cnt ? filenames[tns.sel] : "");
|
|
|
|
} else {
|
2011-04-11 16:58:38 +02:00
|
|
|
size = filesize;
|
|
|
|
size_readable(&size, &unit);
|
2011-05-22 19:42:33 +02:00
|
|
|
n = snprintf(win_title, TITLE_LEN,
|
|
|
|
"sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
|
|
|
|
fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w, img.h,
|
|
|
|
size, unit, filenames[fileidx]);
|
2011-04-11 11:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n >= TITLE_LEN) {
|
2011-06-24 13:28:25 +02:00
|
|
|
for (n = 0; n < 3; n++)
|
|
|
|
win_title[TITLE_LEN - n - 2] = '.';
|
2011-04-11 11:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
win_set_title(&win, win_title);
|
|
|
|
}
|
|
|
|
|
2011-04-08 10:23:42 +02:00
|
|
|
int fncmp(const void *a, const void *b) {
|
|
|
|
return strcoll(*((char* const*) a), *((char* const*) b));
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:57:59 +01:00
|
|
|
int main(int argc, char **argv) {
|
2011-05-29 11:45:58 +02:00
|
|
|
int i, len, start;
|
|
|
|
size_t n;
|
|
|
|
char *filename = NULL;
|
2011-02-02 09:01:05 +01:00
|
|
|
struct stat fstats;
|
2011-04-08 10:23:42 +02:00
|
|
|
r_dir_t dir;
|
2011-01-20 16:24:48 +01:00
|
|
|
|
2011-01-19 18:16:44 +01:00
|
|
|
parse_options(argc, argv);
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-04-08 14:44:00 +02:00
|
|
|
if (options->clean_cache) {
|
|
|
|
tns_init(&tns, 0);
|
2011-04-11 16:58:38 +02:00
|
|
|
tns_clean_cache(&tns);
|
2011-04-08 14:44:00 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-01-19 14:07:45 +01:00
|
|
|
if (!options->filecnt) {
|
|
|
|
print_usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:04 +01:00
|
|
|
if (options->recursive || options->from_stdin)
|
2011-02-02 09:01:05 +01:00
|
|
|
filecnt = FNAME_CNT;
|
|
|
|
else
|
|
|
|
filecnt = options->filecnt;
|
|
|
|
|
2011-05-29 11:45:58 +02:00
|
|
|
filenames = (char**) s_malloc(filecnt * sizeof(char*));
|
2011-01-19 14:07:45 +01:00
|
|
|
fileidx = 0;
|
2011-01-20 16:24:48 +01:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
/* build file list: */
|
2011-02-14 17:51:04 +01:00
|
|
|
if (options->from_stdin) {
|
2011-05-29 11:45:58 +02:00
|
|
|
while ((len = getline(&filename, &n, stdin)) > 0) {
|
|
|
|
if (filename[len-1] == '\n')
|
|
|
|
filename[len-1] = '\0';
|
2011-07-26 18:01:29 +02:00
|
|
|
if (!*filename || !check_add_file(filename))
|
2011-05-29 11:45:58 +02:00
|
|
|
free(filename);
|
|
|
|
filename = NULL;
|
2011-02-14 17:51:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
2011-07-26 18:01:29 +02:00
|
|
|
for (i = 0; i < options->filecnt; i++) {
|
2011-02-14 17:51:04 +01:00
|
|
|
filename = options->filenames[i];
|
2011-04-08 10:23:42 +02:00
|
|
|
|
|
|
|
if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
|
2011-07-26 18:01:29 +02:00
|
|
|
check_add_file(filename);
|
2011-04-08 10:23:42 +02:00
|
|
|
} else {
|
|
|
|
if (!options->recursive) {
|
2011-02-14 17:51:04 +01:00
|
|
|
warn("ignoring directory: %s", filename);
|
2011-04-08 10:23:42 +02:00
|
|
|
continue;
|
2011-04-07 19:15:00 +02:00
|
|
|
}
|
2011-04-08 10:23:42 +02:00
|
|
|
if (r_opendir(&dir, filename)) {
|
|
|
|
warn("could not open directory: %s", filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
start = fileidx;
|
|
|
|
while ((filename = r_readdir(&dir))) {
|
2011-07-26 18:01:29 +02:00
|
|
|
if (!check_add_file(filename))
|
2011-04-08 10:23:42 +02:00
|
|
|
free((void*) filename);
|
|
|
|
}
|
|
|
|
r_closedir(&dir);
|
|
|
|
if (fileidx - start > 1)
|
|
|
|
qsort(filenames + start, fileidx - start, sizeof(char*), fncmp);
|
2011-02-14 17:51:04 +01:00
|
|
|
}
|
2011-02-02 09:01:05 +01:00
|
|
|
}
|
2011-01-20 16:24:48 +01:00
|
|
|
}
|
|
|
|
|
2011-05-25 09:23:23 +02:00
|
|
|
if (!fileidx) {
|
2011-04-11 16:58:38 +02:00
|
|
|
fprintf(stderr, "sxiv: no valid image file given, aborting\n");
|
2011-01-20 16:24:48 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-05-25 09:23:23 +02:00
|
|
|
filecnt = fileidx;
|
2011-06-28 13:45:57 +02:00
|
|
|
fileidx = options->startnum < filecnt ? options->startnum : 0;
|
2011-05-25 09:23:23 +02:00
|
|
|
|
2011-04-11 16:58:38 +02:00
|
|
|
win_init(&win);
|
2011-01-21 12:57:35 +01:00
|
|
|
img_init(&img, &win);
|
2011-01-17 16:41:50 +01:00
|
|
|
|
2011-02-21 14:59:29 +01:00
|
|
|
if (options->thumbnails) {
|
2011-02-16 17:09:46 +01:00
|
|
|
mode = MODE_THUMBS;
|
2011-02-21 14:59:29 +01:00
|
|
|
tns_init(&tns, filecnt);
|
2011-04-11 16:58:38 +02:00
|
|
|
while (!tns_load(&tns, 0, filenames[0], 0))
|
|
|
|
remove_file(0, 0);
|
|
|
|
tns.cnt = 1;
|
2011-02-16 17:09:46 +01:00
|
|
|
} else {
|
|
|
|
mode = MODE_NORMAL;
|
2011-02-21 14:59:29 +01:00
|
|
|
tns.thumbs = NULL;
|
2011-02-27 13:29:24 +01:00
|
|
|
load_image(fileidx);
|
2011-02-16 17:09:46 +01:00
|
|
|
}
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-04-11 16:58:38 +02:00
|
|
|
win_open(&win);
|
|
|
|
|
2011-01-19 14:07:45 +01:00
|
|
|
run();
|
|
|
|
cleanup();
|
2011-01-17 16:41:50 +01:00
|
|
|
|
2011-01-17 14:57:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|