2011-01-17 13:57:59 +00:00
|
|
|
/* sxiv: main.c
|
|
|
|
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
|
|
|
|
*
|
2011-09-03 22:11:45 +01:00
|
|
|
* 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.
|
2011-08-18 00:18:26 +01:00
|
|
|
*
|
2011-09-03 22:11:45 +01:00
|
|
|
* 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.
|
2011-08-18 00:18:26 +01:00
|
|
|
*
|
2011-09-03 22:11:45 +01:00
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-01-17 13:57:59 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-17 15:41:50 +00:00
|
|
|
#include <stdlib.h>
|
2011-08-19 14:02:10 +01:00
|
|
|
#include <stdio.h>
|
2011-02-02 09:34:14 +00:00
|
|
|
#include <string.h>
|
2011-03-01 17:49:02 +00:00
|
|
|
#include <unistd.h>
|
2011-07-26 17:01:29 +01:00
|
|
|
#include <sys/stat.h>
|
2011-08-19 14:02:10 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <X11/keysym.h>
|
2011-01-17 15:41:50 +00:00
|
|
|
|
2011-08-19 14:02:10 +01:00
|
|
|
#include "commands.h"
|
2011-01-19 13:07:45 +00:00
|
|
|
#include "image.h"
|
2011-02-03 09:15:01 +00:00
|
|
|
#include "options.h"
|
2011-02-16 20:40:20 +00:00
|
|
|
#include "thumbs.h"
|
2011-08-17 23:38:55 +01:00
|
|
|
#include "types.h"
|
2011-02-03 09:15:01 +00:00
|
|
|
#include "util.h"
|
2011-01-19 13:07:45 +00:00
|
|
|
#include "window.h"
|
2011-04-14 11:00:35 +01:00
|
|
|
|
2011-08-19 14:02:10 +01:00
|
|
|
#define _MAPPINGS_CONFIG
|
|
|
|
#include "config.h"
|
|
|
|
|
2011-07-26 17:01:29 +01:00
|
|
|
enum {
|
|
|
|
TITLE_LEN = 256,
|
|
|
|
FNAME_CNT = 1024
|
|
|
|
};
|
2011-01-19 13:07:45 +00:00
|
|
|
|
2011-09-02 03:33:44 +01:00
|
|
|
typedef struct {
|
|
|
|
struct timeval when;
|
|
|
|
Bool active;
|
|
|
|
timeout_f handler;
|
|
|
|
} timeout_t;
|
|
|
|
|
|
|
|
/* timeout handler functions: */
|
|
|
|
void redraw();
|
2011-09-03 16:01:39 +01:00
|
|
|
void reset_cursor();
|
2011-09-02 03:33:44 +01:00
|
|
|
void animate();
|
|
|
|
|
2011-02-16 16:09:46 +00:00
|
|
|
appmode_t mode;
|
2011-01-19 13:07:45 +00:00
|
|
|
img_t img;
|
2011-02-16 20:40:20 +00:00
|
|
|
tns_t tns;
|
2011-01-19 13:07:45 +00:00
|
|
|
win_t win;
|
2011-01-20 15:24:48 +00:00
|
|
|
|
2011-08-17 23:38:55 +01:00
|
|
|
fileinfo_t *files;
|
2011-01-23 11:36:27 +00:00
|
|
|
int filecnt, fileidx;
|
2011-02-03 13:32:02 +00:00
|
|
|
size_t filesize;
|
2011-01-19 13:07:45 +00:00
|
|
|
|
2011-01-20 16:00:59 +00:00
|
|
|
char win_title[TITLE_LEN];
|
|
|
|
|
2011-09-02 03:33:44 +01:00
|
|
|
timeout_t timeouts[] = {
|
|
|
|
{ { 0, 0 }, False, redraw },
|
2011-09-03 16:01:39 +01:00
|
|
|
{ { 0, 0 }, False, reset_cursor },
|
2011-09-02 03:33:44 +01:00
|
|
|
{ { 0, 0 }, False, animate }
|
|
|
|
};
|
2011-08-19 14:02:10 +01:00
|
|
|
|
2011-02-03 09:15:01 +00:00
|
|
|
void cleanup() {
|
|
|
|
static int in = 0;
|
|
|
|
|
|
|
|
if (!in++) {
|
2011-03-01 17:49:02 +00:00
|
|
|
img_close(&img, 0);
|
2011-04-11 15:58:38 +01:00
|
|
|
tns_free(&tns);
|
2011-02-03 09:15:01 +00:00
|
|
|
win_close(&win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 23:38:55 +01:00
|
|
|
void check_add_file(char *filename) {
|
|
|
|
if (!filename || !*filename)
|
|
|
|
return;
|
2011-07-26 17:01:29 +01:00
|
|
|
|
|
|
|
if (access(filename, R_OK)) {
|
|
|
|
warn("could not open file: %s", filename);
|
2011-08-17 23:38:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileidx == filecnt) {
|
|
|
|
filecnt *= 2;
|
|
|
|
files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
|
|
|
|
}
|
|
|
|
if (*filename != '/') {
|
|
|
|
files[fileidx].path = absolute_path(filename);
|
|
|
|
if (!files[fileidx].path) {
|
|
|
|
warn("could not get absolute path of file: %s\n", filename);
|
|
|
|
return;
|
2011-07-26 17:01:29 +01:00
|
|
|
}
|
|
|
|
}
|
2011-08-17 23:38:55 +01:00
|
|
|
files[fileidx].name = s_strdup(filename);
|
|
|
|
if (*filename == '/')
|
|
|
|
files[fileidx].path = files[fileidx].name;
|
|
|
|
fileidx++;
|
2011-07-26 17:01:29 +01:00
|
|
|
}
|
|
|
|
|
2011-04-11 15:58:38 +01:00
|
|
|
void remove_file(int n, unsigned char silent) {
|
|
|
|
if (n < 0 || n >= filecnt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (filecnt == 1) {
|
|
|
|
if (!silent)
|
2011-04-11 19:42:08 +01:00
|
|
|
fprintf(stderr, "sxiv: no more files to display, aborting\n");
|
2011-04-11 15:58:38 +01:00
|
|
|
cleanup();
|
|
|
|
exit(!silent);
|
|
|
|
}
|
|
|
|
|
2011-09-03 18:08:49 +01:00
|
|
|
if (files[n].path != files[n].name)
|
|
|
|
free((void*) files[n].path);
|
|
|
|
free((void*) files[n].name);
|
|
|
|
|
|
|
|
if (n + 1 < filecnt)
|
2011-08-17 23:38:55 +01:00
|
|
|
memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
|
2011-04-11 15:58:38 +01: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 17:01:29 +01:00
|
|
|
filecnt--;
|
2011-04-11 15:58:38 +01:00
|
|
|
if (n < tns.cnt)
|
2011-07-26 17:01:29 +01:00
|
|
|
tns.cnt--;
|
2011-04-11 15:58:38 +01:00
|
|
|
}
|
|
|
|
|
2011-09-02 03:33:44 +01:00
|
|
|
void set_timeout(timeout_f handler, int time, int overwrite) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(timeouts); i++) {
|
|
|
|
if (timeouts[i].handler == handler) {
|
|
|
|
if (!timeouts[i].active || overwrite) {
|
|
|
|
gettimeofday(&timeouts[i].when, 0);
|
|
|
|
MSEC_ADD_TO_TIMEVAL(time, &timeouts[i].when);
|
|
|
|
timeouts[i].active = True;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_timeout(timeout_f handler) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(timeouts); i++) {
|
|
|
|
if (timeouts[i].handler == handler) {
|
|
|
|
timeouts[i].active = False;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_timeouts(struct timeval *t) {
|
2011-09-02 17:54:41 +01:00
|
|
|
int i = 0, tdiff, tmin = -1;
|
2011-09-02 03:33:44 +01:00
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
gettimeofday(&now, 0);
|
2011-09-02 17:54:41 +01:00
|
|
|
while (i < LEN(timeouts)) {
|
2011-09-02 03:33:44 +01:00
|
|
|
if (timeouts[i].active) {
|
|
|
|
tdiff = TIMEDIFF(&timeouts[i].when, &now);
|
|
|
|
if (tdiff <= 0) {
|
|
|
|
timeouts[i].active = False;
|
|
|
|
if (timeouts[i].handler)
|
|
|
|
timeouts[i].handler();
|
2011-09-02 17:54:41 +01:00
|
|
|
i = tmin = -1;
|
2011-09-02 03:33:44 +01:00
|
|
|
} else if (tmin < 0 || tdiff < tmin) {
|
|
|
|
tmin = tdiff;
|
|
|
|
}
|
|
|
|
}
|
2011-09-02 17:54:41 +01:00
|
|
|
i++;
|
2011-09-02 03:33:44 +01:00
|
|
|
}
|
|
|
|
if (tmin > 0 && t)
|
|
|
|
MSEC_TO_TIMEVAL(tmin, t);
|
|
|
|
return tmin > 0;
|
|
|
|
}
|
|
|
|
|
2011-07-26 17:01:29 +01:00
|
|
|
void load_image(int new) {
|
2011-02-03 13:32:02 +00:00
|
|
|
struct stat fstats;
|
|
|
|
|
2011-07-26 17:01:29 +01:00
|
|
|
if (new < 0 || new >= filecnt)
|
|
|
|
return;
|
2011-04-11 15:58:38 +01:00
|
|
|
|
2011-07-26 17:01:29 +01:00
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
2011-09-03 16:01:39 +01:00
|
|
|
|
2011-07-26 17:01:29 +01:00
|
|
|
img_close(&img, 0);
|
2011-08-17 23:38:55 +01:00
|
|
|
while (!img_load(&img, &files[new])) {
|
2011-07-26 17:01:29 +01:00
|
|
|
remove_file(new, 0);
|
|
|
|
if (new >= filecnt)
|
|
|
|
new = filecnt - 1;
|
2011-02-27 12:29:24 +00:00
|
|
|
}
|
2011-07-26 17:01:29 +01:00
|
|
|
|
|
|
|
fileidx = new;
|
2011-08-17 23:38:55 +01:00
|
|
|
if (!stat(files[new].path, &fstats))
|
2011-07-26 17:01:29 +01:00
|
|
|
filesize = fstats.st_size;
|
|
|
|
else
|
|
|
|
filesize = 0;
|
2011-08-18 15:05:32 +01:00
|
|
|
|
2011-08-19 17:46:17 +01:00
|
|
|
if (img.multi.cnt) {
|
|
|
|
if (img.multi.animate)
|
2011-09-02 03:33:44 +01:00
|
|
|
set_timeout(animate, img.multi.frames[img.multi.sel].delay, 1);
|
2011-08-19 17:46:17 +01:00
|
|
|
else
|
2011-09-02 03:33:44 +01:00
|
|
|
reset_timeout(animate);
|
2011-08-19 17:46:17 +01:00
|
|
|
}
|
2011-02-03 13:32:02 +00:00
|
|
|
}
|
|
|
|
|
2011-04-11 10:53:00 +01:00
|
|
|
void update_title() {
|
|
|
|
int n;
|
|
|
|
float size;
|
|
|
|
const char *unit;
|
|
|
|
|
2011-08-19 12:09:22 +01:00
|
|
|
if (mode == MODE_THUMB) {
|
2011-04-11 10:53:00 +01:00
|
|
|
n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
|
|
|
|
tns.cnt ? tns.sel + 1 : 0, tns.cnt,
|
2011-08-17 23:38:55 +01:00
|
|
|
tns.cnt ? files[tns.sel].name : "");
|
2011-04-11 10:53:00 +01:00
|
|
|
} else {
|
2011-04-11 15:58:38 +01:00
|
|
|
size = filesize;
|
|
|
|
size_readable(&size, &unit);
|
2011-08-16 23:56:18 +01:00
|
|
|
if (img.multi.cnt)
|
|
|
|
n = snprintf(win_title, TITLE_LEN,
|
|
|
|
"sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
|
|
|
|
fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
|
2011-08-17 17:01:21 +01:00
|
|
|
img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
|
2011-08-16 23:56:18 +01:00
|
|
|
files[fileidx].name);
|
|
|
|
else
|
|
|
|
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, files[fileidx].name);
|
2011-04-11 10:53:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n >= TITLE_LEN) {
|
2011-06-24 12:28:25 +01:00
|
|
|
for (n = 0; n < 3; n++)
|
|
|
|
win_title[TITLE_LEN - n - 2] = '.';
|
2011-04-11 10:53:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
win_set_title(&win, win_title);
|
|
|
|
}
|
|
|
|
|
2011-08-19 14:02:10 +01:00
|
|
|
void redraw() {
|
2011-09-03 16:01:39 +01:00
|
|
|
if (mode == MODE_IMAGE)
|
2011-08-19 14:02:10 +01:00
|
|
|
img_render(&img, &win);
|
2011-09-03 16:01:39 +01:00
|
|
|
else
|
2011-08-19 14:02:10 +01:00
|
|
|
tns_render(&tns, &win);
|
|
|
|
update_title();
|
2011-09-02 03:33:44 +01:00
|
|
|
reset_timeout(redraw);
|
2011-09-03 16:01:39 +01:00
|
|
|
reset_cursor();
|
2011-09-02 03:33:44 +01:00
|
|
|
}
|
|
|
|
|
2011-09-03 16:01:39 +01:00
|
|
|
void reset_cursor() {
|
|
|
|
int i;
|
|
|
|
cursor_t cursor = CURSOR_NONE;
|
|
|
|
|
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
for (i = 0; i < LEN(timeouts); i++) {
|
|
|
|
if (timeouts[i].handler == reset_cursor) {
|
|
|
|
if (timeouts[i].active)
|
|
|
|
cursor = CURSOR_ARROW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tns.cnt != filecnt)
|
|
|
|
cursor = CURSOR_WATCH;
|
|
|
|
else
|
|
|
|
cursor = CURSOR_ARROW;
|
|
|
|
}
|
|
|
|
win_set_cursor(&win, cursor);
|
2011-09-02 03:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void animate() {
|
|
|
|
int delay;
|
|
|
|
|
|
|
|
delay = img_frame_animate(&img, 0);
|
|
|
|
if (delay) {
|
|
|
|
set_timeout(animate, delay, 1);
|
|
|
|
redraw();
|
|
|
|
}
|
2011-08-19 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Bool keymask(const keymap_t *k, unsigned int state) {
|
|
|
|
return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bool buttonmask(const button_t *b, unsigned int state) {
|
|
|
|
return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
|
|
|
|
(state & (ControlMask | ShiftMask));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_keypress(XKeyEvent *kev) {
|
|
|
|
int i;
|
|
|
|
KeySym ksym;
|
|
|
|
char key;
|
|
|
|
|
|
|
|
if (!kev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
XLookupString(kev, &key, 1, &ksym, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(keys); i++) {
|
|
|
|
if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
|
|
|
|
if (keys[i].cmd && keys[i].cmd(keys[i].arg))
|
|
|
|
redraw();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_buttonpress(XButtonEvent *bev) {
|
|
|
|
int i, sel;
|
|
|
|
|
|
|
|
if (!bev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
win_set_cursor(&win, CURSOR_ARROW);
|
2011-09-03 16:01:39 +01:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
|
2011-08-19 14:02:10 +01:00
|
|
|
|
|
|
|
for (i = 0; i < LEN(buttons); i++) {
|
|
|
|
if (buttons[i].button == bev->button &&
|
|
|
|
buttonmask(&buttons[i], bev->state))
|
|
|
|
{
|
|
|
|
if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
|
|
|
|
redraw();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* thumbnail mode (hard-coded) */
|
|
|
|
switch (bev->button) {
|
|
|
|
case Button1:
|
|
|
|
if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
|
|
|
|
if (sel == tns.sel) {
|
|
|
|
mode = MODE_IMAGE;
|
2011-09-03 16:01:39 +01:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
|
|
|
|
load_image(tns.sel);
|
|
|
|
redraw();
|
2011-08-19 14:02:10 +01:00
|
|
|
} else {
|
|
|
|
tns_highlight(&tns, &win, tns.sel, False);
|
|
|
|
tns_highlight(&tns, &win, sel, True);
|
|
|
|
tns.sel = sel;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Button4:
|
|
|
|
case Button5:
|
|
|
|
if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN))
|
|
|
|
redraw();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run() {
|
2011-09-02 03:33:44 +01:00
|
|
|
int xfd;
|
2011-08-19 14:02:10 +01:00
|
|
|
fd_set fds;
|
2011-09-02 03:33:44 +01:00
|
|
|
struct timeval timeout;
|
2011-08-19 14:02:10 +01:00
|
|
|
XEvent ev;
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
|
|
|
|
while (1) {
|
2011-09-02 14:56:03 +01:00
|
|
|
while (mode == MODE_THUMB && tns.cnt < filecnt &&
|
|
|
|
!XPending(win.env.dpy))
|
|
|
|
{
|
2011-08-19 14:02:10 +01:00
|
|
|
/* load thumbnails */
|
2011-09-02 14:56:03 +01:00
|
|
|
set_timeout(redraw, TO_REDRAW_THUMBS, 0);
|
|
|
|
if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
|
|
|
|
tns.cnt++;
|
|
|
|
else
|
|
|
|
remove_file(tns.cnt, 0);
|
2011-09-03 16:01:39 +01:00
|
|
|
if (tns.cnt == filecnt)
|
2011-09-02 14:56:03 +01:00
|
|
|
redraw();
|
2011-09-03 16:01:39 +01:00
|
|
|
else
|
2011-09-02 14:56:03 +01:00
|
|
|
check_timeouts(NULL);
|
|
|
|
}
|
2011-09-02 03:33:44 +01:00
|
|
|
|
2011-09-02 17:54:41 +01:00
|
|
|
while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
|
|
|
|
/* wait for timeouts */
|
2011-09-02 14:56:03 +01:00
|
|
|
xfd = ConnectionNumber(win.env.dpy);
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(xfd, &fds);
|
2011-09-02 17:54:41 +01:00
|
|
|
select(xfd + 1, &fds, 0, 0, &timeout);
|
2011-08-19 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!XNextEvent(win.env.dpy, &ev)) {
|
|
|
|
/* handle events */
|
|
|
|
switch (ev.type) {
|
|
|
|
case ButtonPress:
|
|
|
|
on_buttonpress(&ev.xbutton);
|
|
|
|
break;
|
|
|
|
case ClientMessage:
|
|
|
|
if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
case ConfigureNotify:
|
|
|
|
if (win_configure(&win, &ev.xconfigure)) {
|
2011-09-02 03:33:44 +01:00
|
|
|
set_timeout(redraw, TO_REDRAW_RESIZE, 0);
|
2011-08-19 14:02:10 +01:00
|
|
|
if (mode == MODE_IMAGE)
|
|
|
|
img.checkpan = 1;
|
|
|
|
else
|
|
|
|
tns.dirty = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KeyPress:
|
|
|
|
on_keypress(&ev.xkey);
|
|
|
|
break;
|
|
|
|
case MotionNotify:
|
2011-09-02 03:33:44 +01:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-08-19 14:02:10 +01:00
|
|
|
win_set_cursor(&win, CURSOR_ARROW);
|
2011-09-03 16:01:39 +01:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
|
2011-09-02 03:33:44 +01:00
|
|
|
}
|
2011-08-19 14:02:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-08 09:23:42 +01:00
|
|
|
int fncmp(const void *a, const void *b) {
|
2011-08-17 23:38:55 +01:00
|
|
|
return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
|
2011-04-08 09:23:42 +01:00
|
|
|
}
|
|
|
|
|
2011-01-17 13:57:59 +00:00
|
|
|
int main(int argc, char **argv) {
|
2011-05-29 10:45:58 +01:00
|
|
|
int i, len, start;
|
|
|
|
size_t n;
|
2011-08-17 23:38:55 +01:00
|
|
|
char *filename;
|
2011-02-02 08:01:05 +00:00
|
|
|
struct stat fstats;
|
2011-04-08 09:23:42 +01:00
|
|
|
r_dir_t dir;
|
2011-01-20 15:24:48 +00:00
|
|
|
|
2011-01-19 17:16:44 +00:00
|
|
|
parse_options(argc, argv);
|
2011-01-19 13:07:45 +00:00
|
|
|
|
2011-04-08 13:44:00 +01:00
|
|
|
if (options->clean_cache) {
|
|
|
|
tns_init(&tns, 0);
|
2011-04-11 15:58:38 +01:00
|
|
|
tns_clean_cache(&tns);
|
2011-04-08 13:44:00 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-01-19 13:07:45 +00:00
|
|
|
if (!options->filecnt) {
|
|
|
|
print_usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:04 +00:00
|
|
|
if (options->recursive || options->from_stdin)
|
2011-02-02 08:01:05 +00:00
|
|
|
filecnt = FNAME_CNT;
|
|
|
|
else
|
|
|
|
filecnt = options->filecnt;
|
|
|
|
|
2011-08-17 23:38:55 +01:00
|
|
|
files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
|
2011-01-19 13:07:45 +00:00
|
|
|
fileidx = 0;
|
2011-01-20 15:24:48 +00:00
|
|
|
|
2011-07-26 17:01:29 +01:00
|
|
|
/* build file list: */
|
2011-02-14 16:51:04 +00:00
|
|
|
if (options->from_stdin) {
|
2011-08-17 23:38:55 +01:00
|
|
|
filename = NULL;
|
2011-05-29 10:45:58 +01:00
|
|
|
while ((len = getline(&filename, &n, stdin)) > 0) {
|
|
|
|
if (filename[len-1] == '\n')
|
|
|
|
filename[len-1] = '\0';
|
2011-08-17 23:38:55 +01:00
|
|
|
check_add_file(filename);
|
2011-02-14 16:51:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-07-26 17:01:29 +01:00
|
|
|
for (i = 0; i < options->filecnt; i++) {
|
2011-02-14 16:51:04 +00:00
|
|
|
filename = options->filenames[i];
|
2011-04-08 09:23:42 +01:00
|
|
|
|
|
|
|
if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
|
2011-07-26 17:01:29 +01:00
|
|
|
check_add_file(filename);
|
2011-04-08 09:23:42 +01:00
|
|
|
} else {
|
|
|
|
if (!options->recursive) {
|
2011-02-14 16:51:04 +00:00
|
|
|
warn("ignoring directory: %s", filename);
|
2011-04-08 09:23:42 +01:00
|
|
|
continue;
|
2011-04-07 18:15:00 +01:00
|
|
|
}
|
2011-04-08 09:23:42 +01:00
|
|
|
if (r_opendir(&dir, filename)) {
|
|
|
|
warn("could not open directory: %s", filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
start = fileidx;
|
2011-08-17 23:38:55 +01:00
|
|
|
printf("reading dir: %s\n", filename);
|
2011-04-08 09:23:42 +01:00
|
|
|
while ((filename = r_readdir(&dir))) {
|
2011-08-17 23:38:55 +01:00
|
|
|
check_add_file(filename);
|
|
|
|
free((void*) filename);
|
2011-04-08 09:23:42 +01:00
|
|
|
}
|
|
|
|
r_closedir(&dir);
|
|
|
|
if (fileidx - start > 1)
|
2011-08-17 23:38:55 +01:00
|
|
|
qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
|
2011-02-14 16:51:04 +00:00
|
|
|
}
|
2011-02-02 08:01:05 +00:00
|
|
|
}
|
2011-01-20 15:24:48 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 08:23:23 +01:00
|
|
|
if (!fileidx) {
|
2011-04-11 15:58:38 +01:00
|
|
|
fprintf(stderr, "sxiv: no valid image file given, aborting\n");
|
2011-01-20 15:24:48 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-01-19 13:07:45 +00:00
|
|
|
|
2011-05-25 08:23:23 +01:00
|
|
|
filecnt = fileidx;
|
2011-06-28 12:45:57 +01:00
|
|
|
fileidx = options->startnum < filecnt ? options->startnum : 0;
|
2011-05-25 08:23:23 +01:00
|
|
|
|
2011-04-11 15:58:38 +01:00
|
|
|
win_init(&win);
|
2011-01-21 11:57:35 +00:00
|
|
|
img_init(&img, &win);
|
2011-01-17 15:41:50 +00:00
|
|
|
|
2011-02-21 13:59:29 +00:00
|
|
|
if (options->thumbnails) {
|
2011-08-19 12:09:22 +01:00
|
|
|
mode = MODE_THUMB;
|
2011-02-21 13:59:29 +00:00
|
|
|
tns_init(&tns, filecnt);
|
2011-08-19 12:26:58 +01:00
|
|
|
while (!tns_load(&tns, 0, &files[0], False, False))
|
2011-04-11 15:58:38 +01:00
|
|
|
remove_file(0, 0);
|
|
|
|
tns.cnt = 1;
|
2011-02-16 16:09:46 +00:00
|
|
|
} else {
|
2011-08-19 12:09:22 +01:00
|
|
|
mode = MODE_IMAGE;
|
2011-02-21 13:59:29 +00:00
|
|
|
tns.thumbs = NULL;
|
2011-02-27 12:29:24 +00:00
|
|
|
load_image(fileidx);
|
2011-02-16 16:09:46 +00:00
|
|
|
}
|
2011-01-19 13:07:45 +00:00
|
|
|
|
2011-04-11 15:58:38 +01:00
|
|
|
win_open(&win);
|
|
|
|
|
2011-01-19 13:07:45 +00:00
|
|
|
run();
|
|
|
|
cleanup();
|
2011-01-17 15:41:50 +00:00
|
|
|
|
2011-01-17 13:57:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|