2013-02-08 20:52:41 +00:00
|
|
|
/* Copyright 2011-2013 Bert Muennich
|
2011-01-17 13:57:59 +00:00
|
|
|
*
|
2013-02-08 20:52:41 +00:00
|
|
|
* This file is part of sxiv.
|
2011-08-17 23:18:26 +00:00
|
|
|
*
|
2013-02-08 20:52:41 +00:00
|
|
|
* sxiv 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-17 23:18:26 +00:00
|
|
|
*
|
2013-02-08 20:52:41 +00:00
|
|
|
* sxiv 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 sxiv. If not, see <http://www.gnu.org/licenses/>.
|
2011-01-17 13:57:59 +00:00
|
|
|
*/
|
|
|
|
|
2011-09-08 18:54:24 +00:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2011-09-11 19:01:24 +00:00
|
|
|
#define _MAPPINGS_CONFIG
|
2011-09-08 18:54:24 +00:00
|
|
|
|
2011-01-17 15:41:50 +00:00
|
|
|
#include <stdlib.h>
|
2011-08-19 13:02:10 +00:00
|
|
|
#include <stdio.h>
|
2011-02-02 09:34:14 +00:00
|
|
|
#include <string.h>
|
2013-02-11 22:05:26 +00:00
|
|
|
#include <fcntl.h>
|
2011-03-01 17:49:02 +00:00
|
|
|
#include <unistd.h>
|
2013-02-11 22:05:26 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
2012-02-27 18:22:05 +00:00
|
|
|
#include <sys/select.h>
|
2011-07-26 16:01:29 +00:00
|
|
|
#include <sys/stat.h>
|
2011-08-19 13:02:10 +00:00
|
|
|
#include <sys/time.h>
|
2013-02-11 22:05:26 +00:00
|
|
|
#include <sys/wait.h>
|
2011-08-19 13:02:10 +00:00
|
|
|
#include <X11/keysym.h>
|
2014-01-09 19:38:46 +00:00
|
|
|
#include <X11/XF86keysym.h>
|
2011-01-17 15:41:50 +00:00
|
|
|
|
2012-12-27 15:34:51 +00:00
|
|
|
#include "types.h"
|
2011-08-19 13:02:10 +00: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-02-03 09:15:01 +00:00
|
|
|
#include "util.h"
|
2011-01-19 13:07:45 +00:00
|
|
|
#include "window.h"
|
2011-08-19 13:02:10 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-07-26 16:01:29 +00:00
|
|
|
enum {
|
2013-01-07 13:30:42 +00:00
|
|
|
FILENAME_CNT = 1024,
|
|
|
|
TITLE_LEN = 256
|
2011-07-26 16:01:29 +00:00
|
|
|
};
|
2011-01-19 13:07:45 +00:00
|
|
|
|
2014-01-02 22:19:31 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
char *cmd;
|
|
|
|
} exec_t;
|
|
|
|
|
2011-09-02 02:33:44 +00:00
|
|
|
typedef struct {
|
|
|
|
struct timeval when;
|
2011-09-11 19:01:24 +00:00
|
|
|
bool active;
|
2011-09-02 02:33:44 +00:00
|
|
|
timeout_f handler;
|
|
|
|
} timeout_t;
|
|
|
|
|
|
|
|
/* timeout handler functions: */
|
2011-10-12 16:38:29 +00:00
|
|
|
void redraw(void);
|
|
|
|
void reset_cursor(void);
|
|
|
|
void animate(void);
|
2014-01-04 17:38:40 +00:00
|
|
|
void slideshow(void);
|
2012-03-13 20:58:48 +00:00
|
|
|
void clear_resize(void);
|
2011-09-02 02:33:44 +00:00
|
|
|
|
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 22:38:55 +00:00
|
|
|
fileinfo_t *files;
|
2011-01-23 11:36:27 +00:00
|
|
|
int filecnt, fileidx;
|
2012-08-16 11:40:04 +00:00
|
|
|
int alternate;
|
2011-01-19 13:07:45 +00:00
|
|
|
|
2011-10-16 14:08:55 +00:00
|
|
|
int prefix;
|
2014-01-31 12:21:23 +00:00
|
|
|
bool extprefix;
|
2011-10-16 14:08:55 +00:00
|
|
|
|
2012-03-13 20:58:48 +00:00
|
|
|
bool resized = false;
|
|
|
|
|
2013-01-07 13:30:42 +00:00
|
|
|
struct {
|
2014-01-02 22:19:31 +00:00
|
|
|
char *cmd;
|
2013-02-11 22:05:26 +00:00
|
|
|
int fd;
|
|
|
|
unsigned int i, lastsep;
|
2013-02-12 16:55:47 +00:00
|
|
|
bool open;
|
2013-02-11 22:05:26 +00:00
|
|
|
} info;
|
2011-01-20 16:00:59 +00:00
|
|
|
|
2014-01-11 21:47:41 +00:00
|
|
|
char * keyhandler;
|
|
|
|
|
2011-09-02 02:33:44 +00:00
|
|
|
timeout_t timeouts[] = {
|
2014-01-04 17:38:40 +00:00
|
|
|
{ { 0, 0 }, false, redraw },
|
2011-09-11 19:01:24 +00:00
|
|
|
{ { 0, 0 }, false, reset_cursor },
|
2014-01-04 17:38:40 +00:00
|
|
|
{ { 0, 0 }, false, animate },
|
|
|
|
{ { 0, 0 }, false, slideshow },
|
2012-03-13 20:58:48 +00:00
|
|
|
{ { 0, 0 }, false, clear_resize },
|
2011-09-02 02:33:44 +00:00
|
|
|
};
|
2011-08-19 13:02:10 +00:00
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void cleanup(void)
|
|
|
|
{
|
2011-09-29 10:43:36 +00:00
|
|
|
static bool in = false;
|
2011-02-03 09:15:01 +00:00
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
if (!in) {
|
|
|
|
in = true;
|
2011-09-11 19:01:24 +00:00
|
|
|
img_close(&img, false);
|
2011-04-11 14:58:38 +00:00
|
|
|
tns_free(&tns);
|
2011-02-03 09:15:01 +00:00
|
|
|
win_close(&win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void check_add_file(char *filename)
|
|
|
|
{
|
2012-02-21 11:49:29 +00:00
|
|
|
const char *bn;
|
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
if (filename == NULL || *filename == '\0')
|
2011-08-17 22:38:55 +00:00
|
|
|
return;
|
2011-07-26 16:01:29 +00:00
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
if (access(filename, R_OK) < 0) {
|
2011-07-26 16:01:29 +00:00
|
|
|
warn("could not open file: %s", filename);
|
2011-08-17 22:38:55 +00: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);
|
2011-09-29 10:43:36 +00:00
|
|
|
if (files[fileidx].path == NULL) {
|
2011-08-17 22:38:55 +00:00
|
|
|
warn("could not get absolute path of file: %s\n", filename);
|
|
|
|
return;
|
2011-07-26 16:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-11 19:01:24 +00:00
|
|
|
files[fileidx].loaded = false;
|
2011-08-17 22:38:55 +00:00
|
|
|
files[fileidx].name = s_strdup(filename);
|
|
|
|
if (*filename == '/')
|
|
|
|
files[fileidx].path = files[fileidx].name;
|
2012-02-21 11:49:29 +00:00
|
|
|
if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
|
|
|
|
files[fileidx].base = ++bn;
|
|
|
|
else
|
|
|
|
files[fileidx].base = files[fileidx].name;
|
2011-08-17 22:38:55 +00:00
|
|
|
fileidx++;
|
2011-07-26 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void remove_file(int n, bool manual)
|
|
|
|
{
|
2011-04-11 14:58:38 +00:00
|
|
|
if (n < 0 || n >= filecnt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (filecnt == 1) {
|
2011-09-26 19:28:27 +00:00
|
|
|
if (!manual)
|
2011-04-11 18:42:08 +00:00
|
|
|
fprintf(stderr, "sxiv: no more files to display, aborting\n");
|
2011-04-11 14:58:38 +00:00
|
|
|
cleanup();
|
2011-09-26 19:28:27 +00:00
|
|
|
exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
|
2011-04-11 14:58:38 +00:00
|
|
|
}
|
|
|
|
|
2011-09-03 17:08:49 +00: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 22:38:55 +00:00
|
|
|
memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
|
2011-04-11 14:58:38 +00: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 16:01:29 +00:00
|
|
|
filecnt--;
|
2011-04-11 14:58:38 +00:00
|
|
|
if (n < tns.cnt)
|
2011-07-26 16:01:29 +00:00
|
|
|
tns.cnt--;
|
2013-10-21 19:57:21 +00:00
|
|
|
if (n < alternate)
|
|
|
|
alternate--;
|
2011-04-11 14:58:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void set_timeout(timeout_f handler, int time, bool overwrite)
|
|
|
|
{
|
2011-09-02 02:33:44 +00:00
|
|
|
int i;
|
|
|
|
|
2011-09-06 07:11:03 +00:00
|
|
|
for (i = 0; i < ARRLEN(timeouts); i++) {
|
2011-09-02 02:33:44 +00:00
|
|
|
if (timeouts[i].handler == handler) {
|
|
|
|
if (!timeouts[i].active || overwrite) {
|
|
|
|
gettimeofday(&timeouts[i].when, 0);
|
2011-10-13 14:50:06 +00:00
|
|
|
TV_ADD_MSEC(&timeouts[i].when, time);
|
2011-09-11 19:01:24 +00:00
|
|
|
timeouts[i].active = true;
|
2011-09-02 02:33:44 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void reset_timeout(timeout_f handler)
|
|
|
|
{
|
2011-09-02 02:33:44 +00:00
|
|
|
int i;
|
|
|
|
|
2011-09-06 07:11:03 +00:00
|
|
|
for (i = 0; i < ARRLEN(timeouts); i++) {
|
2011-09-02 02:33:44 +00:00
|
|
|
if (timeouts[i].handler == handler) {
|
2011-09-11 19:01:24 +00:00
|
|
|
timeouts[i].active = false;
|
2011-09-02 02:33:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
bool check_timeouts(struct timeval *t)
|
|
|
|
{
|
2011-09-02 16:54:41 +00:00
|
|
|
int i = 0, tdiff, tmin = -1;
|
2011-09-02 02:33:44 +00:00
|
|
|
struct timeval now;
|
|
|
|
|
2011-09-06 07:11:03 +00:00
|
|
|
while (i < ARRLEN(timeouts)) {
|
2011-09-02 02:33:44 +00:00
|
|
|
if (timeouts[i].active) {
|
2012-10-31 22:24:21 +00:00
|
|
|
gettimeofday(&now, 0);
|
2011-10-13 14:50:06 +00:00
|
|
|
tdiff = TV_DIFF(&timeouts[i].when, &now);
|
2011-09-02 02:33:44 +00:00
|
|
|
if (tdiff <= 0) {
|
2011-09-11 19:01:24 +00:00
|
|
|
timeouts[i].active = false;
|
2011-09-29 10:43:36 +00:00
|
|
|
if (timeouts[i].handler != NULL)
|
2011-09-02 02:33:44 +00:00
|
|
|
timeouts[i].handler();
|
2011-09-02 16:54:41 +00:00
|
|
|
i = tmin = -1;
|
2011-09-02 02:33:44 +00:00
|
|
|
} else if (tmin < 0 || tdiff < tmin) {
|
|
|
|
tmin = tdiff;
|
|
|
|
}
|
|
|
|
}
|
2011-09-02 16:54:41 +00:00
|
|
|
i++;
|
2011-09-02 02:33:44 +00:00
|
|
|
}
|
2011-09-29 10:43:36 +00:00
|
|
|
if (tmin > 0 && t != NULL)
|
2011-10-13 14:50:06 +00:00
|
|
|
TV_SET_MSEC(t, tmin);
|
2011-09-02 02:33:44 +00:00
|
|
|
return tmin > 0;
|
|
|
|
}
|
|
|
|
|
2013-02-11 22:05:26 +00:00
|
|
|
void open_info(void)
|
|
|
|
{
|
|
|
|
static pid_t pid;
|
|
|
|
int pfd[2];
|
|
|
|
|
2014-01-02 22:19:31 +00:00
|
|
|
if (info.cmd == NULL || info.open || win.bar.h == 0)
|
2013-02-12 16:55:47 +00:00
|
|
|
return;
|
2013-02-11 22:05:26 +00:00
|
|
|
if (info.fd != -1) {
|
|
|
|
close(info.fd);
|
|
|
|
kill(pid, SIGTERM);
|
|
|
|
info.fd = -1;
|
|
|
|
}
|
2013-02-12 16:55:47 +00:00
|
|
|
win.bar.l[0] = '\0';
|
2013-02-11 22:05:26 +00:00
|
|
|
|
2013-02-12 16:55:47 +00:00
|
|
|
if (pipe(pfd) < 0)
|
|
|
|
return;
|
2013-02-11 22:05:26 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid > 0) {
|
|
|
|
close(pfd[1]);
|
|
|
|
fcntl(pfd[0], F_SETFL, O_NONBLOCK);
|
|
|
|
info.fd = pfd[0];
|
|
|
|
info.i = info.lastsep = 0;
|
2013-02-12 16:55:47 +00:00
|
|
|
info.open = true;
|
2013-02-11 22:05:26 +00:00
|
|
|
} else if (pid == 0) {
|
|
|
|
close(pfd[0]);
|
|
|
|
dup2(pfd[1], 1);
|
2014-01-02 22:19:31 +00:00
|
|
|
execl(info.cmd, info.cmd, files[fileidx].name, NULL);
|
|
|
|
warn("could not exec: %s", info.cmd);
|
2013-02-24 13:55:49 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2013-02-11 22:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void read_info(void)
|
|
|
|
{
|
2013-02-11 22:05:26 +00:00
|
|
|
ssize_t i, n;
|
|
|
|
char buf[BAR_L_LEN];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
n = read(info.fd, buf, sizeof(buf));
|
|
|
|
if (n < 0 && errno == EAGAIN)
|
|
|
|
return;
|
|
|
|
else if (n == 0)
|
2013-01-07 13:30:42 +00:00
|
|
|
goto end;
|
2013-02-11 22:05:26 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (buf[i] == '\n') {
|
|
|
|
if (info.lastsep == 0) {
|
|
|
|
win.bar.l[info.i++] = ' ';
|
|
|
|
info.lastsep = 1;
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-02-11 22:05:26 +00:00
|
|
|
win.bar.l[info.i++] = buf[i];
|
|
|
|
info.lastsep = 0;
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2013-02-11 22:05:26 +00:00
|
|
|
if (info.i + 1 == sizeof(win.bar.l))
|
|
|
|
goto end;
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
2013-02-11 22:05:26 +00:00
|
|
|
info.i -= info.lastsep;
|
|
|
|
win.bar.l[info.i] = '\0';
|
|
|
|
win_update_bar(&win);
|
2013-11-17 11:54:55 +00:00
|
|
|
close(info.fd);
|
2013-02-11 22:05:26 +00:00
|
|
|
info.fd = -1;
|
|
|
|
while (waitpid(-1, NULL, WNOHANG) > 0);
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2011-02-03 13:32:02 +00:00
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void load_image(int new)
|
|
|
|
{
|
2011-07-26 16:01:29 +00:00
|
|
|
if (new < 0 || new >= filecnt)
|
|
|
|
return;
|
2011-04-11 14:58:38 +00:00
|
|
|
|
2011-07-26 16:01:29 +00:00
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
2014-01-04 17:38:40 +00:00
|
|
|
reset_timeout(slideshow);
|
2011-09-03 15:01:39 +00:00
|
|
|
|
2013-10-21 19:57:21 +00:00
|
|
|
if (new != fileidx)
|
|
|
|
alternate = fileidx;
|
|
|
|
|
2011-09-11 19:01:24 +00:00
|
|
|
img_close(&img, false);
|
2011-08-17 22:38:55 +00:00
|
|
|
while (!img_load(&img, &files[new])) {
|
2011-09-11 19:01:24 +00:00
|
|
|
remove_file(new, false);
|
2011-07-26 16:01:29 +00:00
|
|
|
if (new >= filecnt)
|
|
|
|
new = filecnt - 1;
|
2013-11-16 19:19:55 +00:00
|
|
|
else if (new > 0 && new < fileidx)
|
2013-10-21 19:57:21 +00:00
|
|
|
new--;
|
2011-02-27 12:29:24 +00:00
|
|
|
}
|
2011-09-11 19:01:24 +00:00
|
|
|
files[new].loaded = true;
|
2011-07-26 16:01:29 +00:00
|
|
|
fileidx = new;
|
2013-01-07 13:30:42 +00:00
|
|
|
|
2013-02-12 16:55:47 +00:00
|
|
|
info.open = false;
|
2013-02-11 22:05:26 +00:00
|
|
|
open_info();
|
2011-08-18 14:05:32 +00:00
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
if (img.multi.cnt > 0 && img.multi.animate)
|
2011-09-11 19:01:24 +00:00
|
|
|
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
|
2011-09-06 09:55:31 +00:00
|
|
|
else
|
|
|
|
reset_timeout(animate);
|
2011-02-03 13:32:02 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void update_info(void)
|
|
|
|
{
|
2013-01-07 13:30:42 +00:00
|
|
|
int sel;
|
2013-02-11 22:05:26 +00:00
|
|
|
unsigned int i, fn, fw, n;
|
|
|
|
unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
|
|
|
|
char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
|
2013-08-10 19:18:53 +00:00
|
|
|
const char * mark;
|
2013-01-07 13:30:42 +00:00
|
|
|
bool ow_info;
|
2012-02-12 18:00:41 +00:00
|
|
|
|
2013-01-07 13:30:42 +00:00
|
|
|
for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
|
|
|
|
sel = mode == MODE_IMAGE ? fileidx : tns.sel;
|
2011-04-11 09:53:00 +00:00
|
|
|
|
2013-02-12 16:55:47 +00:00
|
|
|
/* update window title */
|
2011-08-19 11:09:22 +00:00
|
|
|
if (mode == MODE_THUMB) {
|
2013-01-07 13:30:42 +00:00
|
|
|
win_set_title(&win, "sxiv");
|
2013-02-12 16:55:47 +00:00
|
|
|
} else {
|
|
|
|
snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
|
|
|
|
win_set_title(&win, title);
|
|
|
|
}
|
2013-01-07 13:30:42 +00:00
|
|
|
|
2013-02-12 16:55:47 +00:00
|
|
|
/* update bar contents */
|
|
|
|
if (win.bar.h == 0)
|
|
|
|
return;
|
2013-08-10 19:18:53 +00:00
|
|
|
mark = files[sel].marked ? "* " : "";
|
2013-02-12 16:55:47 +00:00
|
|
|
if (mode == MODE_THUMB) {
|
2013-01-07 13:30:42 +00:00
|
|
|
if (tns.cnt == filecnt) {
|
2013-08-10 19:18:53 +00:00
|
|
|
n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, sel + 1, filecnt);
|
2013-01-07 13:30:42 +00:00
|
|
|
ow_info = true;
|
2012-02-15 18:29:56 +00:00
|
|
|
} else {
|
2013-02-11 22:05:26 +00:00
|
|
|
snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.cnt, filecnt);
|
|
|
|
rt[0] = '\0';
|
2013-01-07 13:30:42 +00:00
|
|
|
ow_info = false;
|
2012-02-15 18:29:56 +00:00
|
|
|
}
|
2011-04-11 09:53:00 +00:00
|
|
|
} else {
|
2013-11-14 16:06:20 +00:00
|
|
|
n = snprintf(rt, rlen, "%s", mark);
|
2014-01-04 17:38:40 +00:00
|
|
|
if (img.ss.on)
|
|
|
|
n += snprintf(rt + n, rlen - n, "%ds | ", img.ss.delay);
|
2013-11-14 16:06:20 +00:00
|
|
|
if (img.gamma != 0)
|
|
|
|
n += snprintf(rt + n, rlen - n, "G%+d | ", img.gamma);
|
|
|
|
n += snprintf(rt + n, rlen - n, "%3d%% | ", (int) (img.zoom * 100.0));
|
2012-02-12 18:00:41 +00:00
|
|
|
if (img.multi.cnt > 0) {
|
2013-01-07 13:30:42 +00:00
|
|
|
for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
|
2013-02-24 19:04:55 +00:00
|
|
|
n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
|
2013-01-07 13:30:42 +00:00
|
|
|
fn, img.multi.sel + 1, img.multi.cnt);
|
2012-02-12 18:00:41 +00:00
|
|
|
}
|
2013-02-11 22:05:26 +00:00
|
|
|
n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
|
2014-01-02 22:19:31 +00:00
|
|
|
ow_info = info.cmd == NULL;
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
|
|
|
if (ow_info) {
|
|
|
|
fn = strlen(files[sel].name);
|
2013-02-11 22:05:26 +00:00
|
|
|
if (fn < llen &&
|
2013-01-07 13:30:42 +00:00
|
|
|
win_textwidth(files[sel].name, fn, true) +
|
2013-02-11 22:05:26 +00:00
|
|
|
win_textwidth(rt, n, true) < win.w)
|
2012-02-21 11:49:29 +00:00
|
|
|
{
|
2013-02-11 22:05:26 +00:00
|
|
|
strncpy(lt, files[sel].name, llen);
|
2013-01-07 13:30:42 +00:00
|
|
|
} else {
|
2013-02-11 22:05:26 +00:00
|
|
|
strncpy(lt, files[sel].base, llen);
|
2012-02-21 11:49:29 +00:00
|
|
|
}
|
2011-04-11 09:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void redraw(void)
|
|
|
|
{
|
2014-01-04 17:38:40 +00:00
|
|
|
int t;
|
|
|
|
|
|
|
|
if (mode == MODE_IMAGE) {
|
2011-09-17 15:23:51 +00:00
|
|
|
img_render(&img);
|
2014-01-04 17:38:40 +00:00
|
|
|
if (img.ss.on) {
|
|
|
|
t = img.ss.delay * 1000;
|
2014-01-08 19:31:07 +00:00
|
|
|
if (img.multi.cnt > 0 && img.multi.animate)
|
|
|
|
t = MAX(t, img.multi.length);
|
2014-01-04 17:38:40 +00:00
|
|
|
set_timeout(slideshow, t, false);
|
|
|
|
}
|
|
|
|
} else {
|
2011-09-17 15:23:51 +00:00
|
|
|
tns_render(&tns);
|
2014-01-04 17:38:40 +00:00
|
|
|
}
|
2012-02-12 18:00:41 +00:00
|
|
|
update_info();
|
|
|
|
win_draw(&win);
|
2011-09-02 02:33:44 +00:00
|
|
|
reset_timeout(redraw);
|
2011-09-03 15:01:39 +00:00
|
|
|
reset_cursor();
|
2011-09-02 02:33:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void reset_cursor(void)
|
|
|
|
{
|
2011-09-03 15:01:39 +00:00
|
|
|
int i;
|
|
|
|
cursor_t cursor = CURSOR_NONE;
|
|
|
|
|
|
|
|
if (mode == MODE_IMAGE) {
|
2011-09-06 07:11:03 +00:00
|
|
|
for (i = 0; i < ARRLEN(timeouts); i++) {
|
2011-09-03 15:01:39 +00:00
|
|
|
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 02:33:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void animate(void)
|
|
|
|
{
|
2011-09-11 19:01:24 +00:00
|
|
|
if (img_frame_animate(&img, false)) {
|
|
|
|
redraw();
|
|
|
|
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
|
|
|
|
}
|
2011-09-10 16:41:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 17:38:40 +00:00
|
|
|
void slideshow(void)
|
|
|
|
{
|
|
|
|
load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void clear_resize(void)
|
|
|
|
{
|
2012-03-13 20:58:48 +00:00
|
|
|
resized = false;
|
|
|
|
}
|
|
|
|
|
2014-01-11 21:47:41 +00:00
|
|
|
void run_key_handler(const char *key, unsigned int mask)
|
2014-01-02 22:19:31 +00:00
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int retval, status, n = mode == MODE_IMAGE ? fileidx : tns.sel;
|
2014-01-11 21:47:41 +00:00
|
|
|
char kstr[32];
|
2014-01-08 20:14:01 +00:00
|
|
|
struct stat oldst, newst;
|
2014-01-02 22:19:31 +00:00
|
|
|
|
2014-01-11 21:47:41 +00:00
|
|
|
if (keyhandler == NULL || key == NULL)
|
2014-01-02 22:19:31 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf(kstr, sizeof(kstr), "%s%s%s%s",
|
|
|
|
mask & ControlMask ? "C-" : "",
|
|
|
|
mask & Mod1Mask ? "M-" : "",
|
|
|
|
mask & ShiftMask ? "S-" : "", key);
|
|
|
|
|
2014-01-08 20:14:01 +00:00
|
|
|
stat(files[n].path, &oldst);
|
2014-01-08 19:31:50 +00:00
|
|
|
|
2014-01-02 22:19:31 +00:00
|
|
|
if ((pid = fork()) == 0) {
|
2014-01-11 21:47:41 +00:00
|
|
|
execl(keyhandler, keyhandler, kstr, files[n].path, NULL);
|
2014-01-02 22:19:31 +00:00
|
|
|
warn("could not exec key handler");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else if (pid < 0) {
|
|
|
|
warn("could not fork key handler");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
|
|
|
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
retval = WEXITSTATUS(status);
|
|
|
|
if (WIFEXITED(status) == 0 || retval != 0)
|
|
|
|
warn("key handler exited with non-zero return value: %d", retval);
|
2014-01-08 19:31:50 +00:00
|
|
|
|
2014-01-08 20:14:01 +00:00
|
|
|
if (stat(files[n].path, &newst) == 0 &&
|
|
|
|
memcmp(&oldst.st_mtime, &newst.st_mtime, sizeof(oldst.st_mtime)) == 0)
|
2014-01-08 19:31:50 +00:00
|
|
|
{
|
|
|
|
/* file has not changed */
|
2014-01-08 20:14:01 +00:00
|
|
|
win_set_cursor(&win, CURSOR_ARROW);
|
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
|
2014-01-08 19:31:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-02 22:19:31 +00:00
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
img_close(&img, true);
|
|
|
|
load_image(fileidx);
|
|
|
|
}
|
|
|
|
if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
|
|
|
|
mode == MODE_THUMB)
|
|
|
|
{
|
|
|
|
remove_file(tns.sel, false);
|
|
|
|
tns.dirty = true;
|
|
|
|
if (tns.sel >= tns.cnt)
|
|
|
|
tns.sel = tns.cnt - 1;
|
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2014-01-02 12:36:00 +00:00
|
|
|
#define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
|
2011-08-19 13:02:10 +00:00
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void on_keypress(XKeyEvent *kev)
|
|
|
|
{
|
2011-08-19 13:02:10 +00:00
|
|
|
int i;
|
2014-01-02 12:36:00 +00:00
|
|
|
unsigned int sh;
|
|
|
|
KeySym ksym, shksym;
|
2011-08-19 13:02:10 +00:00
|
|
|
char key;
|
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
if (kev == NULL)
|
2011-08-19 13:02:10 +00:00
|
|
|
return;
|
|
|
|
|
2014-01-02 12:36:00 +00:00
|
|
|
if (kev->state & ShiftMask) {
|
|
|
|
kev->state &= ~ShiftMask;
|
|
|
|
XLookupString(kev, &key, 1, &shksym, NULL);
|
|
|
|
kev->state |= ShiftMask;
|
|
|
|
}
|
2011-08-19 13:02:10 +00:00
|
|
|
XLookupString(kev, &key, 1, &ksym, NULL);
|
2014-01-02 12:36:00 +00:00
|
|
|
sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
|
2011-08-19 13:02:10 +00:00
|
|
|
|
2014-01-02 22:19:31 +00:00
|
|
|
if (IsModifierKey(ksym))
|
|
|
|
return;
|
2014-01-31 12:21:23 +00:00
|
|
|
if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
|
|
|
|
extprefix = False;
|
|
|
|
} else if (extprefix) {
|
|
|
|
run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
|
|
|
|
extprefix = False;
|
|
|
|
} else if (key >= '0' && key <= '9') {
|
2011-10-16 14:08:55 +00:00
|
|
|
/* number prefix for commands */
|
2014-01-31 12:21:23 +00:00
|
|
|
prefix = prefix * 10 + (int) (key - '0');
|
2011-10-16 14:08:55 +00:00
|
|
|
return;
|
2014-01-31 12:21:23 +00:00
|
|
|
} else for (i = 0; i < ARRLEN(keys); i++) {
|
2014-01-02 12:36:00 +00:00
|
|
|
if (keys[i].ksym == ksym &&
|
2014-01-08 21:58:27 +00:00
|
|
|
MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
|
|
|
|
keys[i].cmd != NULL)
|
2014-01-02 12:36:00 +00:00
|
|
|
{
|
2014-01-08 21:58:27 +00:00
|
|
|
cmdreturn_t ret = keys[i].cmd(keys[i].arg);
|
|
|
|
|
|
|
|
if (ret == CMD_INVALID)
|
|
|
|
continue;
|
|
|
|
if (ret == CMD_DIRTY)
|
2011-08-19 13:02:10 +00:00
|
|
|
redraw();
|
2014-01-08 21:58:27 +00:00
|
|
|
break;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-08 21:58:27 +00:00
|
|
|
prefix = 0;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void on_buttonpress(XButtonEvent *bev)
|
|
|
|
{
|
2011-08-19 13:02:10 +00:00
|
|
|
int i, sel;
|
2014-01-11 21:52:37 +00:00
|
|
|
static Time firstclick;
|
2011-08-19 13:02:10 +00:00
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
if (bev == NULL)
|
2011-08-19 13:02:10 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
win_set_cursor(&win, CURSOR_ARROW);
|
2011-09-11 19:01:24 +00:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
|
2011-08-19 13:02:10 +00:00
|
|
|
|
2011-09-06 07:11:03 +00:00
|
|
|
for (i = 0; i < ARRLEN(buttons); i++) {
|
2011-08-19 13:02:10 +00:00
|
|
|
if (buttons[i].button == bev->button &&
|
2014-01-08 21:58:27 +00:00
|
|
|
MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
|
|
|
|
buttons[i].cmd != NULL)
|
2011-08-19 13:02:10 +00:00
|
|
|
{
|
2014-01-08 21:58:27 +00:00
|
|
|
cmdreturn_t ret = buttons[i].cmd(buttons[i].arg);
|
|
|
|
|
|
|
|
if (ret == CMD_INVALID)
|
|
|
|
continue;
|
|
|
|
if (ret == CMD_DIRTY)
|
2011-08-19 13:02:10 +00:00
|
|
|
redraw();
|
2014-01-08 21:58:27 +00:00
|
|
|
break;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* thumbnail mode (hard-coded) */
|
|
|
|
switch (bev->button) {
|
|
|
|
case Button1:
|
|
|
|
if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
|
2014-01-11 21:52:37 +00:00
|
|
|
if (sel != tns.sel) {
|
|
|
|
tns_highlight(&tns, tns.sel, false);
|
|
|
|
tns_highlight(&tns, sel, true);
|
|
|
|
tns.sel = sel;
|
|
|
|
firstclick = bev->time;
|
|
|
|
redraw();
|
|
|
|
} else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
|
2011-08-19 13:02:10 +00:00
|
|
|
mode = MODE_IMAGE;
|
2011-09-11 19:01:24 +00:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
|
2011-09-03 15:01:39 +00:00
|
|
|
load_image(tns.sel);
|
2014-01-11 21:52:37 +00:00
|
|
|
redraw();
|
2011-08-19 13:02:10 +00:00
|
|
|
} else {
|
2014-01-11 21:52:37 +00:00
|
|
|
firstclick = bev->time;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-11-14 13:37:08 +00:00
|
|
|
case Button3:
|
|
|
|
if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
|
|
|
|
files[sel].marked = !files[sel].marked;
|
|
|
|
tns_mark(&tns, sel, files[sel].marked);
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
break;
|
2011-08-19 13:02:10 +00:00
|
|
|
case Button4:
|
|
|
|
case Button5:
|
2011-10-27 14:21:01 +00:00
|
|
|
if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
|
|
|
|
(bev->state & ControlMask) != 0))
|
2011-08-19 13:02:10 +00:00
|
|
|
redraw();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-08 21:58:27 +00:00
|
|
|
prefix = 0;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void run(void)
|
|
|
|
{
|
2011-09-02 02:33:44 +00:00
|
|
|
int xfd;
|
2011-08-19 13:02:10 +00:00
|
|
|
fd_set fds;
|
2011-09-02 02:33:44 +00:00
|
|
|
struct timeval timeout;
|
2013-02-11 22:05:26 +00:00
|
|
|
bool discard, to_set;
|
2012-08-17 14:54:29 +00:00
|
|
|
XEvent ev, nextev;
|
2011-08-19 13:02:10 +00:00
|
|
|
|
|
|
|
redraw();
|
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
while (true) {
|
2011-09-02 13:56:03 +00:00
|
|
|
while (mode == MODE_THUMB && tns.cnt < filecnt &&
|
2011-09-29 10:43:36 +00:00
|
|
|
XPending(win.env.dpy) == 0)
|
2011-09-02 13:56:03 +00:00
|
|
|
{
|
2011-08-19 13:02:10 +00:00
|
|
|
/* load thumbnails */
|
2011-09-11 19:01:24 +00:00
|
|
|
set_timeout(redraw, TO_REDRAW_THUMBS, false);
|
2011-11-01 07:36:20 +00:00
|
|
|
if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
|
2011-09-02 13:56:03 +00:00
|
|
|
tns.cnt++;
|
2011-11-01 07:36:20 +00:00
|
|
|
} else {
|
2011-09-11 19:01:24 +00:00
|
|
|
remove_file(tns.cnt, false);
|
2013-11-16 19:19:55 +00:00
|
|
|
if (tns.sel > 0 && tns.sel >= tns.cnt)
|
2011-11-01 07:36:20 +00:00
|
|
|
tns.sel--;
|
|
|
|
}
|
2011-09-03 15:01:39 +00:00
|
|
|
if (tns.cnt == filecnt)
|
2011-09-02 13:56:03 +00:00
|
|
|
redraw();
|
2011-09-03 15:01:39 +00:00
|
|
|
else
|
2011-09-02 13:56:03 +00:00
|
|
|
check_timeouts(NULL);
|
|
|
|
}
|
2011-09-02 02:33:44 +00:00
|
|
|
|
2013-02-11 22:05:26 +00:00
|
|
|
while (XPending(win.env.dpy) == 0
|
|
|
|
&& ((to_set = check_timeouts(&timeout)) || info.fd != -1))
|
|
|
|
{
|
|
|
|
/* check for timeouts & input */
|
2011-09-02 13:56:03 +00:00
|
|
|
xfd = ConnectionNumber(win.env.dpy);
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(xfd, &fds);
|
2013-02-11 22:05:26 +00:00
|
|
|
if (info.fd != -1) {
|
|
|
|
FD_SET(info.fd, &fds);
|
|
|
|
xfd = MAX(xfd, info.fd);
|
|
|
|
}
|
|
|
|
select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
|
2013-04-02 17:32:59 +00:00
|
|
|
if (info.fd != -1 && FD_ISSET(info.fd, &fds))
|
2013-02-11 22:05:26 +00:00
|
|
|
read_info();
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 14:54:29 +00:00
|
|
|
do {
|
|
|
|
XNextEvent(win.env.dpy, &ev);
|
2012-10-29 17:25:17 +00:00
|
|
|
discard = false;
|
|
|
|
if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
|
2012-08-17 14:54:29 +00:00
|
|
|
XPeekEvent(win.env.dpy, &nextev);
|
2012-10-29 17:25:17 +00:00
|
|
|
switch (ev.type) {
|
|
|
|
case ConfigureNotify:
|
|
|
|
discard = ev.type == nextev.type;
|
|
|
|
break;
|
|
|
|
case KeyPress:
|
|
|
|
discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
|
|
|
|
&& ev.xkey.keycode == nextev.xkey.keycode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (discard);
|
2012-08-17 14:54:29 +00:00
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
switch (ev.type) {
|
2011-08-19 13:02:10 +00:00
|
|
|
/* handle events */
|
2011-09-29 10:43:36 +00:00
|
|
|
case ButtonPress:
|
|
|
|
on_buttonpress(&ev.xbutton);
|
|
|
|
break;
|
|
|
|
case ClientMessage:
|
2014-01-27 22:16:08 +00:00
|
|
|
if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
|
2011-09-29 10:43:36 +00:00
|
|
|
return;
|
|
|
|
break;
|
|
|
|
case ConfigureNotify:
|
|
|
|
if (win_configure(&win, &ev.xconfigure)) {
|
2012-12-20 08:57:36 +00:00
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
img.dirty = true;
|
2011-09-29 10:43:36 +00:00
|
|
|
img.checkpan = true;
|
2012-12-20 08:57:36 +00:00
|
|
|
} else {
|
2011-09-29 10:43:36 +00:00
|
|
|
tns.dirty = true;
|
2012-12-20 08:57:36 +00:00
|
|
|
}
|
2012-03-13 20:58:48 +00:00
|
|
|
if (!resized || win.fullscreen) {
|
|
|
|
redraw();
|
|
|
|
set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
|
|
|
|
resized = true;
|
|
|
|
} else {
|
|
|
|
set_timeout(redraw, TO_REDRAW_RESIZE, false);
|
|
|
|
}
|
2011-09-29 10:43:36 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-10-29 17:25:17 +00:00
|
|
|
case Expose:
|
|
|
|
win_expose(&win, &ev.xexpose);
|
|
|
|
break;
|
2011-09-29 10:43:36 +00:00
|
|
|
case KeyPress:
|
2012-10-29 17:25:17 +00:00
|
|
|
on_keypress(&ev.xkey);
|
2011-09-29 10:43:36 +00:00
|
|
|
break;
|
|
|
|
case MotionNotify:
|
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
win_set_cursor(&win, CURSOR_ARROW);
|
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
|
|
|
|
}
|
|
|
|
break;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
int fncmp(const void *a, const void *b)
|
|
|
|
{
|
2011-08-17 22:38:55 +00:00
|
|
|
return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
|
2011-04-08 08:23:42 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2011-09-08 18:54:24 +00:00
|
|
|
int i, start;
|
2011-05-29 09:45:58 +00:00
|
|
|
size_t n;
|
2011-09-08 18:54:24 +00:00
|
|
|
ssize_t len;
|
2011-08-17 22:38:55 +00:00
|
|
|
char *filename;
|
2014-01-11 21:47:41 +00:00
|
|
|
const char *homedir, *dsuffix = "";
|
2011-02-02 08:01:05 +00:00
|
|
|
struct stat fstats;
|
2011-04-08 08:23:42 +00: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 12:44:00 +00:00
|
|
|
if (options->clean_cache) {
|
2011-09-17 15:23:51 +00:00
|
|
|
tns_init(&tns, 0, NULL);
|
2011-04-11 14:58:38 +00:00
|
|
|
tns_clean_cache(&tns);
|
2011-09-26 13:40:07 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-04-08 12:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-03-19 20:11:29 +00:00
|
|
|
if (options->filecnt == 0 && !options->from_stdin) {
|
2011-01-19 13:07:45 +00:00
|
|
|
print_usage();
|
2011-09-26 13:40:07 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2011-01-19 13:07:45 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 16:51:04 +00:00
|
|
|
if (options->recursive || options->from_stdin)
|
2012-02-12 18:00:41 +00:00
|
|
|
filecnt = FILENAME_CNT;
|
2011-02-02 08:01:05 +00:00
|
|
|
else
|
|
|
|
filecnt = options->filecnt;
|
|
|
|
|
2011-08-17 22:38:55 +00: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-02-14 16:51:04 +00:00
|
|
|
if (options->from_stdin) {
|
2011-08-17 22:38:55 +00:00
|
|
|
filename = NULL;
|
2011-09-08 18:54:24 +00:00
|
|
|
while ((len = get_line(&filename, &n, stdin)) > 0) {
|
2011-05-29 09:45:58 +00:00
|
|
|
if (filename[len-1] == '\n')
|
|
|
|
filename[len-1] = '\0';
|
2011-08-17 22:38:55 +00:00
|
|
|
check_add_file(filename);
|
2011-02-14 16:51:04 +00:00
|
|
|
}
|
2011-09-29 10:43:36 +00:00
|
|
|
if (filename != NULL)
|
2011-09-08 18:54:24 +00:00
|
|
|
free(filename);
|
2013-03-19 20:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < options->filecnt; i++) {
|
|
|
|
filename = options->filenames[i];
|
2011-04-08 08:23:42 +00:00
|
|
|
|
2013-03-19 20:11:29 +00:00
|
|
|
if (stat(filename, &fstats) < 0) {
|
|
|
|
warn("could not stat file: %s", filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(fstats.st_mode)) {
|
|
|
|
check_add_file(filename);
|
|
|
|
} else {
|
|
|
|
if (!options->recursive) {
|
|
|
|
warn("ignoring directory: %s", filename);
|
2011-09-29 10:43:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-03-19 20:11:29 +00:00
|
|
|
if (r_opendir(&dir, filename) < 0) {
|
|
|
|
warn("could not open directory: %s", filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
start = fileidx;
|
|
|
|
while ((filename = r_readdir(&dir)) != NULL) {
|
2011-07-26 16:01:29 +00:00
|
|
|
check_add_file(filename);
|
2013-03-19 20:11:29 +00:00
|
|
|
free((void*) filename);
|
2011-02-14 16:51:04 +00:00
|
|
|
}
|
2013-03-19 20:11:29 +00:00
|
|
|
r_closedir(&dir);
|
|
|
|
if (fileidx - start > 1)
|
|
|
|
qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
|
2011-02-02 08:01:05 +00:00
|
|
|
}
|
2011-01-20 15:24:48 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:43:36 +00:00
|
|
|
if (fileidx == 0) {
|
2011-04-11 14:58:38 +00:00
|
|
|
fprintf(stderr, "sxiv: no valid image file given, aborting\n");
|
2011-09-26 13:40:07 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2011-01-20 15:24:48 +00:00
|
|
|
}
|
2011-01-19 13:07:45 +00:00
|
|
|
|
2011-05-25 07:23:23 +00:00
|
|
|
filecnt = fileidx;
|
2011-06-28 11:45:57 +00:00
|
|
|
fileidx = options->startnum < filecnt ? options->startnum : 0;
|
2011-05-25 07:23:23 +00:00
|
|
|
|
2011-04-11 14:58:38 +00:00
|
|
|
win_init(&win);
|
2011-01-21 11:57:35 +00:00
|
|
|
img_init(&img, &win);
|
2011-01-17 15:41:50 +00:00
|
|
|
|
2014-01-11 21:47:41 +00:00
|
|
|
if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
|
|
|
|
homedir = getenv("HOME");
|
|
|
|
dsuffix = "/.config";
|
|
|
|
}
|
|
|
|
if (homedir != NULL) {
|
|
|
|
char **cmd[] = { &info.cmd, &keyhandler };
|
|
|
|
const char *name[] = { "image-info", "key-handler" };
|
|
|
|
|
|
|
|
for (i = 0; i < ARRLEN(cmd); i++) {
|
|
|
|
len = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
|
|
|
|
*cmd[i] = (char*) s_malloc(len);
|
|
|
|
snprintf(*cmd[i], len, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
|
|
|
|
if (access(*cmd[i], X_OK) != 0) {
|
|
|
|
free(*cmd[i]);
|
|
|
|
*cmd[i] = NULL;
|
|
|
|
}
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2014-01-11 21:47:41 +00:00
|
|
|
} else {
|
|
|
|
warn("could not locate exec directory");
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2013-02-11 22:05:26 +00:00
|
|
|
info.fd = -1;
|
2013-01-07 13:30:42 +00:00
|
|
|
|
2011-09-11 19:01:24 +00:00
|
|
|
if (options->thumb_mode) {
|
2011-08-19 11:09:22 +00:00
|
|
|
mode = MODE_THUMB;
|
2011-09-17 15:23:51 +00:00
|
|
|
tns_init(&tns, filecnt, &win);
|
2011-09-11 19:01:24 +00:00
|
|
|
while (!tns_load(&tns, 0, &files[0], false, false))
|
|
|
|
remove_file(0, false);
|
2011-04-11 14:58:38 +00:00
|
|
|
tns.cnt = 1;
|
2011-02-16 16:09:46 +00:00
|
|
|
} else {
|
2011-08-19 11:09:22 +00: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 14:58:38 +00:00
|
|
|
win_open(&win);
|
2011-09-29 10:43:36 +00:00
|
|
|
|
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;
|
|
|
|
}
|