2021-10-28 10:41:16 +00:00
|
|
|
/* Copyright 2011-2020 Bert Muennich
|
2023-01-15 09:26:46 +00:00
|
|
|
* Copyright 2021-2023 nsxiv contributors
|
2011-01-17 13:57:59 +00:00
|
|
|
*
|
2021-09-16 09:32:59 +00:00
|
|
|
* This file is a part of nsxiv.
|
2011-08-17 23:18:26 +00:00
|
|
|
*
|
2021-09-16 09:32:59 +00:00
|
|
|
* nsxiv is free software; you can redistribute it and/or modify
|
2013-02-08 20:52:41 +00:00
|
|
|
* 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
|
|
|
*
|
2021-09-16 09:32:59 +00:00
|
|
|
* nsxiv is distributed in the hope that it will be useful,
|
2013-02-08 20:52:41 +00:00
|
|
|
* 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
|
2021-09-16 09:32:59 +00:00
|
|
|
* along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
|
2011-01-17 13:57:59 +00:00
|
|
|
*/
|
|
|
|
|
2021-09-16 09:32:59 +00:00
|
|
|
#include "nsxiv.h"
|
2022-06-15 07:42:34 +00:00
|
|
|
#define INCLUDE_MAPPINGS_CONFIG
|
2022-05-03 15:36:57 +00:00
|
|
|
#include "commands.h"
|
2017-10-16 19:10:35 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2023-08-28 10:28:57 +00:00
|
|
|
#include <assert.h>
|
2013-02-11 22:05:26 +00:00
|
|
|
#include <errno.h>
|
2022-06-16 07:39:03 +00:00
|
|
|
#include <fcntl.h>
|
2017-10-24 19:43:36 +00:00
|
|
|
#include <locale.h>
|
2022-04-28 03:12:15 +00:00
|
|
|
#include <poll.h>
|
2022-06-16 07:39:03 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-07-26 16:01:29 +00:00
|
|
|
#include <sys/stat.h>
|
2022-06-16 07:39:03 +00:00
|
|
|
#include <sys/time.h>
|
2013-02-11 22:05:26 +00:00
|
|
|
#include <sys/wait.h>
|
2017-05-17 18:07:32 +00:00
|
|
|
#include <time.h>
|
2022-06-16 07:39:03 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-01-09 19:38:46 +00:00
|
|
|
#include <X11/XF86keysym.h>
|
2022-06-16 07:39:03 +00:00
|
|
|
#include <X11/keysym.h>
|
2011-01-17 15:41:50 +00:00
|
|
|
|
2022-07-02 15:06:24 +00:00
|
|
|
#define MODMASK(mask) (USED_MODMASK & (mask))
|
2021-10-11 03:07:18 +00:00
|
|
|
#define BAR_SEP " "
|
|
|
|
|
2022-05-31 11:25:14 +00:00
|
|
|
#define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \
|
|
|
|
((t1)->tv_usec - (t2)->tv_usec) / 1000)
|
2023-02-10 05:35:53 +00:00
|
|
|
#define TV_ADD_MSEC(tv, t) \
|
|
|
|
do { \
|
|
|
|
(tv)->tv_sec += (t) / 1000; \
|
|
|
|
(tv)->tv_usec += (t) % 1000 * 1000; \
|
|
|
|
} while (0)
|
2022-05-31 11:25:14 +00:00
|
|
|
|
2022-05-03 15:36:57 +00:00
|
|
|
typedef struct {
|
|
|
|
int err;
|
|
|
|
char *cmd;
|
|
|
|
} extcmd_t;
|
2011-09-02 02:33:44 +00:00
|
|
|
|
2022-05-03 15:36:57 +00:00
|
|
|
/* these are not declared in nsxiv.h, as it causes too many -Wshadow warnings */
|
2017-05-17 18:07:32 +00:00
|
|
|
arl_t arl;
|
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;
|
2022-05-03 15:36:57 +00:00
|
|
|
|
|
|
|
appmode_t mode;
|
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;
|
2014-08-16 17:24:34 +00:00
|
|
|
int markcnt;
|
2018-06-09 09:57:42 +00:00
|
|
|
int markidx;
|
2011-10-16 14:08:55 +00:00
|
|
|
int prefix;
|
2022-10-19 12:46:22 +00:00
|
|
|
bool title_dirty;
|
|
|
|
const XButtonEvent *xbutton_ev;
|
2011-10-16 14:08:55 +00:00
|
|
|
|
2023-08-28 10:28:57 +00:00
|
|
|
static void autoreload(void);
|
|
|
|
|
2022-10-19 12:46:22 +00:00
|
|
|
static bool extprefix;
|
2021-11-20 03:51:49 +00:00
|
|
|
static bool resized = false;
|
2012-03-13 20:58:48 +00:00
|
|
|
|
2022-02-03 09:44:10 +00:00
|
|
|
static struct {
|
2022-05-03 15:34:23 +00:00
|
|
|
extcmd_t f, ft;
|
2020-01-16 09:31:41 +00:00
|
|
|
int fd;
|
|
|
|
pid_t pid;
|
2023-01-09 08:12:30 +00:00
|
|
|
} info, wintitle;
|
2011-01-20 16:00:59 +00:00
|
|
|
|
2022-02-03 09:44:10 +00:00
|
|
|
static struct {
|
2015-10-28 22:03:37 +00:00
|
|
|
extcmd_t f;
|
2014-02-18 20:10:07 +00:00
|
|
|
bool warned;
|
|
|
|
} keyhandler;
|
2014-01-11 21:47:41 +00:00
|
|
|
|
2022-08-16 08:53:05 +00:00
|
|
|
static struct {
|
|
|
|
timeout_f handler;
|
|
|
|
struct timeval when;
|
|
|
|
bool active;
|
|
|
|
} timeouts[] = {
|
2023-08-28 10:28:57 +00:00
|
|
|
{ autoreload },
|
2022-08-16 08:53:05 +00:00
|
|
|
{ redraw },
|
|
|
|
{ reset_cursor },
|
|
|
|
{ slideshow },
|
|
|
|
{ animate },
|
|
|
|
{ clear_resize },
|
2011-09-02 02:33:44 +00:00
|
|
|
};
|
2011-08-19 13:02:10 +00:00
|
|
|
|
2022-10-19 12:46:22 +00:00
|
|
|
/*
|
|
|
|
* function implementations
|
|
|
|
*/
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void cleanup(void)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2015-10-28 22:03:37 +00:00
|
|
|
img_close(&img, false);
|
2017-05-17 18:07:32 +00:00
|
|
|
arl_cleanup(&arl);
|
2015-10-28 22:03:37 +00:00
|
|
|
tns_free(&tns);
|
|
|
|
win_close(&win);
|
2011-02-03 09:15:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 10:08:01 +00:00
|
|
|
static bool xgetline(char **lineptr, size_t *n)
|
|
|
|
{
|
|
|
|
ssize_t len = getdelim(lineptr, n, options->using_null ? '\0' : '\n', stdin);
|
2023-02-10 05:35:53 +00:00
|
|
|
if (!options->using_null && len > 0 && (*lineptr)[len - 1] == '\n')
|
|
|
|
(*lineptr)[len - 1] = '\0';
|
2021-11-19 10:08:01 +00:00
|
|
|
return len > 0;
|
|
|
|
}
|
|
|
|
|
2022-10-30 18:18:44 +00:00
|
|
|
static int fncmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return strcoll(((fileinfo_t *)a)->name, ((fileinfo_t *)b)->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_add_file(const char *filename, bool given)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2015-10-28 22:03:37 +00:00
|
|
|
char *path;
|
2012-02-21 11:49:29 +00:00
|
|
|
|
2015-10-28 19:59:48 +00:00
|
|
|
if (*filename == '\0')
|
2011-08-17 22:38:55 +00:00
|
|
|
return;
|
2011-07-26 16:01:29 +00:00
|
|
|
|
2015-10-28 22:03:37 +00:00
|
|
|
if (access(filename, R_OK) < 0 ||
|
|
|
|
(path = realpath(filename, NULL)) == NULL)
|
|
|
|
{
|
2014-10-24 09:14:01 +00:00
|
|
|
if (given)
|
2015-10-28 22:03:37 +00:00
|
|
|
error(0, errno, "%s", filename);
|
2011-08-17 22:38:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileidx == filecnt) {
|
|
|
|
filecnt *= 2;
|
2015-10-28 21:29:01 +00:00
|
|
|
files = erealloc(files, filecnt * sizeof(*files));
|
2023-02-10 05:35:53 +00:00
|
|
|
memset(&files[filecnt / 2], 0, filecnt / 2 * sizeof(*files));
|
2011-08-17 22:38:55 +00:00
|
|
|
}
|
2014-03-17 19:01:53 +00:00
|
|
|
|
2015-10-28 21:29:01 +00:00
|
|
|
files[fileidx].name = estrdup(filename);
|
2015-10-28 22:03:37 +00:00
|
|
|
files[fileidx].path = path;
|
2015-01-04 14:38:49 +00:00
|
|
|
if (given)
|
|
|
|
files[fileidx].flags |= FF_WARN;
|
2011-08-17 22:38:55 +00:00
|
|
|
fileidx++;
|
2011-07-26 16:01:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 18:18:44 +00:00
|
|
|
static void add_entry(const char *entry_name)
|
|
|
|
{
|
|
|
|
int start;
|
|
|
|
char *filename;
|
|
|
|
struct stat fstats;
|
|
|
|
r_dir_t dir;
|
|
|
|
|
|
|
|
if (stat(entry_name, &fstats) < 0) {
|
|
|
|
error(0, errno, "%s", entry_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(fstats.st_mode)) {
|
|
|
|
check_add_file(entry_name, true);
|
|
|
|
} else {
|
|
|
|
if (r_opendir(&dir, entry_name, options->recursive) < 0) {
|
|
|
|
error(0, errno, "%s", entry_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
start = fileidx;
|
|
|
|
while ((filename = r_readdir(&dir, true)) != NULL) {
|
|
|
|
check_add_file(filename, false);
|
|
|
|
free(filename);
|
|
|
|
}
|
|
|
|
r_closedir(&dir);
|
|
|
|
if (fileidx - start > 1)
|
|
|
|
qsort(files + start, fileidx - start, sizeof(*files), fncmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2022-07-15 05:56:45 +00:00
|
|
|
fprintf(stderr, "%s: no more files to display, aborting\n", progname);
|
2011-09-26 19:28:27 +00:00
|
|
|
exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
|
2011-04-11 14:58:38 +00:00
|
|
|
}
|
2015-01-04 14:38:49 +00:00
|
|
|
if (files[n].flags & FF_MARK)
|
2014-08-16 17:24:34 +00:00
|
|
|
markcnt--;
|
2011-04-11 14:58:38 +00:00
|
|
|
|
2011-09-03 17:08:49 +00:00
|
|
|
if (files[n].path != files[n].name)
|
2023-02-10 05:35:53 +00:00
|
|
|
free((void *)files[n].path);
|
|
|
|
free((void *)files[n].name);
|
2023-03-04 07:01:19 +00:00
|
|
|
if (tns.thumbs != NULL)
|
|
|
|
tns_unload(&tns, n);
|
2011-09-03 17:08:49 +00:00
|
|
|
|
2014-09-30 19:54:17 +00:00
|
|
|
if (n + 1 < filecnt) {
|
2014-10-01 18:25:36 +00:00
|
|
|
if (tns.thumbs != NULL) {
|
2022-07-02 15:06:24 +00:00
|
|
|
memmove(tns.thumbs + n, tns.thumbs + n + 1,
|
|
|
|
(filecnt - n - 1) * sizeof(*tns.thumbs));
|
2014-10-01 18:25:36 +00:00
|
|
|
memset(tns.thumbs + filecnt - 1, 0, sizeof(*tns.thumbs));
|
|
|
|
}
|
|
|
|
memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(*files));
|
2011-04-11 14:58:38 +00:00
|
|
|
}
|
2011-07-26 16:01:29 +00:00
|
|
|
filecnt--;
|
2018-12-29 17:48:23 +00:00
|
|
|
if (fileidx > n || fileidx == filecnt)
|
2017-10-26 20:20:39 +00:00
|
|
|
fileidx--;
|
2018-12-29 17:48:23 +00:00
|
|
|
if (alternate > n || alternate == filecnt)
|
2013-10-21 19:57:21 +00:00
|
|
|
alternate--;
|
2018-12-29 17:48:23 +00:00
|
|
|
if (markidx > n || markidx == filecnt)
|
2018-06-09 09:57:42 +00:00
|
|
|
markidx--;
|
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)
|
|
|
|
{
|
2021-10-28 20:00:53 +00:00
|
|
|
unsigned int i;
|
2011-09-02 02:33:44 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2021-10-28 20:00:53 +00:00
|
|
|
unsigned int i;
|
2011-09-02 02:33:44 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 14:47:46 +00:00
|
|
|
static bool check_timeouts(int *t)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2022-07-15 20:46:23 +00:00
|
|
|
while (i < (int)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)
|
2022-05-12 14:47:46 +00:00
|
|
|
*t = tmin;
|
2011-09-02 02:33:44 +00:00
|
|
|
return tmin > 0;
|
|
|
|
}
|
|
|
|
|
2023-08-28 10:28:57 +00:00
|
|
|
static void autoreload(void)
|
|
|
|
{
|
|
|
|
if (img.autoreload_pending) {
|
|
|
|
img_close(&img, true);
|
|
|
|
/* load_image() sets autoreload_pending to false */
|
|
|
|
load_image(fileidx);
|
|
|
|
redraw();
|
|
|
|
} else {
|
|
|
|
assert(!"unreachable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 08:12:30 +00:00
|
|
|
static void kill_close(pid_t pid, int *fd)
|
|
|
|
{
|
|
|
|
if (fd != NULL && *fd != -1) {
|
|
|
|
kill(pid, SIGTERM);
|
|
|
|
close(*fd);
|
|
|
|
*fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_title(void)
|
|
|
|
{
|
|
|
|
kill_close(wintitle.pid, &wintitle.fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_title(void)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
char buf[512];
|
|
|
|
|
2023-02-10 05:35:53 +00:00
|
|
|
if ((n = read(wintitle.fd, buf, sizeof(buf) - 1)) > 0) {
|
2023-01-09 08:12:30 +00:00
|
|
|
buf[n] = '\0';
|
|
|
|
win_set_title(&win, buf, n);
|
|
|
|
}
|
|
|
|
close_title();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void open_title(void)
|
2022-02-23 09:23:22 +00:00
|
|
|
{
|
|
|
|
char *argv[8];
|
|
|
|
char w[12] = "", h[12] = "", z[12] = "", fidx[12], fcnt[12];
|
|
|
|
|
2023-01-09 08:12:30 +00:00
|
|
|
if (wintitle.f.err || !title_dirty)
|
|
|
|
return;
|
2022-02-23 09:23:22 +00:00
|
|
|
|
2023-01-09 08:12:30 +00:00
|
|
|
close_title();
|
2022-06-01 10:47:17 +00:00
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
snprintf(w, ARRLEN(w), "%d", img.w);
|
|
|
|
snprintf(h, ARRLEN(h), "%d", img.h);
|
|
|
|
snprintf(z, ARRLEN(z), "%d", (int)(img.zoom * 100));
|
|
|
|
}
|
2023-02-10 05:35:53 +00:00
|
|
|
snprintf(fidx, ARRLEN(fidx), "%d", fileidx + 1);
|
2022-06-01 10:47:17 +00:00
|
|
|
snprintf(fcnt, ARRLEN(fcnt), "%d", filecnt);
|
|
|
|
construct_argv(argv, ARRLEN(argv), wintitle.f.cmd, files[fileidx].path,
|
|
|
|
fidx, fcnt, w, h, z, NULL);
|
2023-01-09 08:12:30 +00:00
|
|
|
if ((wintitle.pid = spawn(&wintitle.fd, NULL, argv)) > 0)
|
|
|
|
fcntl(wintitle.fd, F_SETFL, O_NONBLOCK);
|
|
|
|
title_dirty = false;
|
2022-02-23 09:23:22 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 12:16:00 +00:00
|
|
|
void close_info(void)
|
|
|
|
{
|
2023-01-09 08:12:30 +00:00
|
|
|
kill_close(info.pid, &info.fd);
|
2018-02-18 12:16:00 +00:00
|
|
|
}
|
|
|
|
|
2013-02-11 22:05:26 +00:00
|
|
|
void open_info(void)
|
|
|
|
{
|
2022-06-22 03:28:07 +00:00
|
|
|
char *argv[6], w[12] = "", h[12] = "";
|
2022-05-03 15:34:23 +00:00
|
|
|
char *cmd = mode == MODE_IMAGE ? info.f.cmd : info.ft.cmd;
|
|
|
|
bool ferr = mode == MODE_IMAGE ? info.f.err : info.ft.err;
|
2013-02-11 22:05:26 +00:00
|
|
|
|
2022-05-03 15:34:23 +00:00
|
|
|
if (ferr || info.fd >= 0 || win.bar.h == 0)
|
2013-02-12 16:55:47 +00:00
|
|
|
return;
|
2014-10-01 20:35:22 +00:00
|
|
|
win.bar.l.buf[0] = '\0';
|
2022-05-03 15:34:23 +00:00
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
snprintf(w, sizeof(w), "%d", img.w);
|
|
|
|
snprintf(h, sizeof(h), "%d", img.h);
|
|
|
|
}
|
|
|
|
construct_argv(argv, ARRLEN(argv), cmd, files[fileidx].name, w, h,
|
|
|
|
files[fileidx].path, NULL);
|
2022-06-22 03:28:07 +00:00
|
|
|
if ((info.pid = spawn(&info.fd, NULL, argv)) > 0)
|
|
|
|
fcntl(info.fd, F_SETFL, O_NONBLOCK);
|
2013-02-11 22:05:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void read_info(void)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2013-02-11 22:05:26 +00:00
|
|
|
ssize_t i, n;
|
|
|
|
|
2022-05-09 13:06:24 +00:00
|
|
|
if ((n = read(info.fd, win.bar.l.buf, win.bar.l.size - 1)) > 0) {
|
|
|
|
win.bar.l.buf[n] = '\0';
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (win.bar.l.buf[i] == '\n')
|
|
|
|
win.bar.l.buf[i] = ' ';
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2022-05-09 13:06:24 +00:00
|
|
|
win_draw(&win);
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2018-02-18 12:16:00 +00:00
|
|
|
close_info();
|
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)
|
|
|
|
{
|
2017-10-26 20:20:39 +00:00
|
|
|
bool prev = new < fileidx;
|
2014-08-29 18:16:24 +00:00
|
|
|
static int current;
|
|
|
|
|
2011-07-26 16:01:29 +00:00
|
|
|
if (new < 0 || new >= filecnt)
|
|
|
|
return;
|
2011-04-11 14:58:38 +00:00
|
|
|
|
2015-10-28 19:59:48 +00:00
|
|
|
if (win.xwin != None)
|
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
2023-08-28 10:28:57 +00:00
|
|
|
reset_timeout(autoreload);
|
2014-01-04 17:38:40 +00:00
|
|
|
reset_timeout(slideshow);
|
2011-09-03 15:01:39 +00:00
|
|
|
|
2023-08-28 10:28:57 +00:00
|
|
|
if (new != current) {
|
2014-08-29 18:16:24 +00:00
|
|
|
alternate = current;
|
2023-08-28 10:28:57 +00:00
|
|
|
img.autoreload_pending = false;
|
|
|
|
}
|
2013-10-21 19:57:21 +00:00
|
|
|
|
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;
|
2017-10-26 20:20:39 +00:00
|
|
|
else if (new > 0 && prev)
|
2022-07-02 15:06:24 +00:00
|
|
|
new -= 1;
|
2011-02-27 12:29:24 +00:00
|
|
|
}
|
2015-01-04 14:38:49 +00:00
|
|
|
files[new].flags &= ~FF_WARN;
|
2014-08-29 18:16:24 +00:00
|
|
|
fileidx = current = new;
|
2013-01-07 13:30:42 +00:00
|
|
|
|
2018-02-18 12:16:00 +00:00
|
|
|
close_info();
|
2013-02-11 22:05:26 +00:00
|
|
|
open_info();
|
2022-07-30 08:05:10 +00:00
|
|
|
arl_add(&arl, files[fileidx].path);
|
2022-06-01 08:59:16 +00:00
|
|
|
title_dirty = true;
|
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
|
|
|
}
|
|
|
|
|
2018-06-09 11:12:46 +00:00
|
|
|
bool mark_image(int n, bool on)
|
|
|
|
{
|
2018-06-09 12:06:16 +00:00
|
|
|
markidx = n;
|
2018-06-09 11:12:46 +00:00
|
|
|
if (!!(files[n].flags & FF_MARK) != on) {
|
|
|
|
files[n].flags ^= FF_MARK;
|
|
|
|
markcnt += on ? 1 : -1;
|
|
|
|
if (mode == MODE_THUMB)
|
|
|
|
tns_mark(&tns, n, on);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void bar_put(win_bar_t *bar, const char *fmt, ...)
|
2014-10-01 20:35:22 +00:00
|
|
|
{
|
|
|
|
size_t len = bar->size - (bar->p - bar->buf), n;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
n = vsnprintf(bar->p, len, fmt, ap);
|
|
|
|
bar->p += MIN(len, n);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void update_info(void)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2014-10-01 20:35:22 +00:00
|
|
|
unsigned int i, fn, fw;
|
2022-05-03 15:34:23 +00:00
|
|
|
const char *mark;
|
2014-10-01 20:35:22 +00:00
|
|
|
win_bar_t *l = &win.bar.l, *r = &win.bar.r;
|
2011-04-11 09:53:00 +00:00
|
|
|
|
2013-02-12 16:55:47 +00:00
|
|
|
/* update bar contents */
|
2022-05-09 13:23:54 +00:00
|
|
|
if (win.bar.h == 0 || extprefix)
|
2013-02-12 16:55:47 +00:00
|
|
|
return;
|
2023-02-10 05:35:53 +00:00
|
|
|
for (fw = 0, i = filecnt; i > 0; fw++, i /= 10)
|
|
|
|
;
|
2015-01-04 14:38:49 +00:00
|
|
|
mark = files[fileidx].flags & FF_MARK ? "* " : "";
|
2014-10-01 20:35:22 +00:00
|
|
|
l->p = l->buf;
|
|
|
|
r->p = r->buf;
|
2013-02-12 16:55:47 +00:00
|
|
|
if (mode == MODE_THUMB) {
|
2017-12-07 13:08:37 +00:00
|
|
|
if (tns.loadnext < tns.end)
|
2023-05-23 03:01:44 +00:00
|
|
|
bar_put(r, "Loading... %0*d | ", fw, tns.loadnext + 1);
|
2017-12-07 13:08:37 +00:00
|
|
|
else if (tns.initnext < filecnt)
|
2023-05-23 03:01:44 +00:00
|
|
|
bar_put(r, "Caching... %0*d | ", fw, tns.initnext + 1);
|
2014-10-01 20:35:22 +00:00
|
|
|
bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
|
2023-05-23 03:01:44 +00:00
|
|
|
if (info.ft.err)
|
|
|
|
strncpy(l->buf, files[fileidx].name, l->size);
|
2011-04-11 09:53:00 +00:00
|
|
|
} else {
|
2014-10-01 20:35:22 +00:00
|
|
|
bar_put(r, "%s", mark);
|
2016-12-01 19:33:24 +00:00
|
|
|
if (img.ss.on) {
|
|
|
|
if (img.ss.delay % 10 != 0)
|
2017-12-07 20:44:40 +00:00
|
|
|
bar_put(r, "%2.1fs" BAR_SEP, (float)img.ss.delay / 10);
|
2016-12-01 19:33:24 +00:00
|
|
|
else
|
2017-12-07 20:44:40 +00:00
|
|
|
bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10);
|
2016-12-01 19:33:24 +00:00
|
|
|
}
|
2021-10-28 20:00:53 +00:00
|
|
|
if (img.gamma)
|
2017-12-07 20:44:40 +00:00
|
|
|
bar_put(r, "G%+d" BAR_SEP, img.gamma);
|
2022-12-22 11:21:40 +00:00
|
|
|
if (img.brightness)
|
|
|
|
bar_put(r, "B%+d" BAR_SEP, img.brightness);
|
|
|
|
if (img.contrast)
|
|
|
|
bar_put(r, "C%+d" BAR_SEP, img.contrast);
|
2023-02-10 05:35:53 +00:00
|
|
|
bar_put(r, "%3d%%" BAR_SEP, (int)(img.zoom * 100.0));
|
2012-02-12 18:00:41 +00:00
|
|
|
if (img.multi.cnt > 0) {
|
2023-02-10 05:35:53 +00:00
|
|
|
for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10)
|
|
|
|
;
|
2017-12-07 20:44:40 +00:00
|
|
|
bar_put(r, "%0*d/%d" BAR_SEP, fn, img.multi.sel + 1, img.multi.cnt);
|
2012-02-12 18:00:41 +00:00
|
|
|
}
|
2014-10-01 20:35:22 +00:00
|
|
|
bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
|
2017-12-07 13:08:37 +00:00
|
|
|
if (info.f.err)
|
2014-10-01 20:35:22 +00:00
|
|
|
strncpy(l->buf, files[fileidx].name, l->size);
|
2011-04-11 09:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 04:20:28 +00:00
|
|
|
int nav_button(void)
|
2017-10-05 11:53:29 +00:00
|
|
|
{
|
2021-11-04 04:20:28 +00:00
|
|
|
int x, y, nw;
|
|
|
|
|
|
|
|
if (NAV_WIDTH == 0)
|
|
|
|
return 1;
|
2017-10-05 11:53:29 +00:00
|
|
|
|
|
|
|
win_cursor_pos(&win, &x, &y);
|
2021-11-04 04:20:28 +00:00
|
|
|
nw = NAV_IS_REL ? win.w * NAV_WIDTH / 100 : NAV_WIDTH;
|
2022-07-15 20:46:23 +00:00
|
|
|
nw = MIN(nw, ((int)win.w + 1) / 2);
|
2021-11-04 04:20:28 +00:00
|
|
|
|
|
|
|
if (x < nw)
|
|
|
|
return 0;
|
2022-07-15 20:46:23 +00:00
|
|
|
else if (x < (int)win.w - nw)
|
2021-11-04 04:20:28 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 2;
|
2017-10-05 11:53:29 +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) {
|
2016-12-01 19:33:24 +00:00
|
|
|
t = img.ss.delay * 100;
|
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();
|
2023-01-09 08:12:30 +00:00
|
|
|
open_title();
|
2012-02-12 18:00:41 +00:00
|
|
|
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)
|
|
|
|
{
|
2021-10-28 20:00:53 +00:00
|
|
|
int c;
|
|
|
|
unsigned int i;
|
2011-09-03 15:01:39 +00:00
|
|
|
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) {
|
2017-10-05 11:53:29 +00:00
|
|
|
if (timeouts[i].active) {
|
2021-11-04 04:20:28 +00:00
|
|
|
c = nav_button();
|
2017-10-05 11:53:29 +00:00
|
|
|
c = MAX(fileidx > 0 ? 0 : 1, c);
|
|
|
|
c = MIN(fileidx + 1 < filecnt ? 2 : 1, c);
|
|
|
|
cursor = imgcursor[c];
|
|
|
|
}
|
2011-09-03 15:01:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-01-04 20:19:26 +00:00
|
|
|
if (tns.loadnext < tns.end || tns.initnext < filecnt)
|
2011-09-03 15:01:39 +00:00
|
|
|
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)
|
|
|
|
{
|
2014-09-01 18:41:27 +00:00
|
|
|
if (img_frame_animate(&img)) {
|
2011-09-11 19:01:24 +00:00
|
|
|
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
|
2022-01-02 20:07:43 +00:00
|
|
|
redraw();
|
2011-09-11 19:01:24 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:36:57 +00:00
|
|
|
static Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
|
2016-11-29 11:07:08 +00:00
|
|
|
{
|
|
|
|
return ev->type == ButtonPress || ev->type == KeyPress;
|
|
|
|
}
|
|
|
|
|
2021-10-07 00:37:34 +00:00
|
|
|
void handle_key_handler(bool init)
|
|
|
|
{
|
|
|
|
extprefix = init;
|
|
|
|
if (win.bar.h == 0)
|
|
|
|
return;
|
|
|
|
if (init) {
|
|
|
|
close_info();
|
2022-07-02 15:06:24 +00:00
|
|
|
snprintf(win.bar.l.buf, win.bar.l.size,
|
|
|
|
"Getting key handler input (%s to abort)...",
|
|
|
|
XKeysymToString(KEYHANDLER_ABORT));
|
2021-10-07 00:37:34 +00:00
|
|
|
} else { /* abort */
|
|
|
|
open_info();
|
|
|
|
update_info();
|
|
|
|
}
|
|
|
|
win_draw(&win);
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:24:26 +00:00
|
|
|
static bool run_key_handler(const char *key, unsigned int mask)
|
2014-01-02 22:19:31 +00:00
|
|
|
{
|
2014-11-27 21:37:20 +00:00
|
|
|
FILE *pfs;
|
|
|
|
bool marked = mode == MODE_THUMB && markcnt > 0;
|
2014-08-16 19:31:05 +00:00
|
|
|
bool changed = false;
|
2022-06-22 03:28:07 +00:00
|
|
|
pid_t pid;
|
|
|
|
int writefd, f, i;
|
2014-11-27 21:37:20 +00:00
|
|
|
int fcnt = marked ? markcnt : 1;
|
2015-08-19 19:29:39 +00:00
|
|
|
char kstr[32];
|
2014-11-27 21:37:20 +00:00
|
|
|
struct stat *oldst, st;
|
2016-11-29 11:07:08 +00:00
|
|
|
XEvent dump;
|
2022-02-20 15:54:29 +00:00
|
|
|
char *argv[3];
|
2014-01-02 22:19:31 +00:00
|
|
|
|
2021-10-28 20:00:53 +00:00
|
|
|
if (keyhandler.f.err) {
|
2014-02-18 20:10:07 +00:00
|
|
|
if (!keyhandler.warned) {
|
2015-10-28 22:03:37 +00:00
|
|
|
error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
|
2014-02-18 20:10:07 +00:00
|
|
|
keyhandler.warned = true;
|
|
|
|
}
|
2022-01-03 09:24:26 +00:00
|
|
|
return false;
|
2014-02-18 20:10:07 +00:00
|
|
|
}
|
|
|
|
if (key == NULL)
|
2022-01-03 09:24:26 +00:00
|
|
|
return false;
|
2014-01-02 22:19:31 +00:00
|
|
|
|
2018-02-18 12:16:00 +00:00
|
|
|
close_info();
|
2014-10-01 20:35:22 +00:00
|
|
|
strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
|
2014-07-28 18:36:32 +00:00
|
|
|
win_draw(&win);
|
2014-06-15 12:15:48 +00:00
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
2022-02-20 15:54:29 +00:00
|
|
|
setenv("NSXIV_USING_NULL", options->using_null ? "1" : "0", 1);
|
2014-01-08 19:31:50 +00:00
|
|
|
|
2014-11-27 21:37:20 +00:00
|
|
|
snprintf(kstr, sizeof(kstr), "%s%s%s%s",
|
|
|
|
mask & ControlMask ? "C-" : "",
|
|
|
|
mask & Mod1Mask ? "M-" : "",
|
|
|
|
mask & ShiftMask ? "S-" : "", key);
|
2022-02-20 15:54:29 +00:00
|
|
|
construct_argv(argv, ARRLEN(argv), keyhandler.f.cmd, kstr, NULL);
|
2022-06-22 03:28:07 +00:00
|
|
|
if ((pid = spawn(NULL, &writefd, argv)) < 0)
|
2022-02-20 15:54:29 +00:00
|
|
|
return false;
|
2022-06-22 03:28:07 +00:00
|
|
|
if ((pfs = fdopen(writefd, "w")) == NULL) {
|
2022-02-20 15:54:29 +00:00
|
|
|
error(0, errno, "open pipe");
|
2022-06-22 03:28:07 +00:00
|
|
|
close(writefd);
|
2022-02-20 15:54:29 +00:00
|
|
|
return false;
|
2014-01-02 22:19:31 +00:00
|
|
|
}
|
2014-11-27 21:37:20 +00:00
|
|
|
|
2022-02-20 15:54:29 +00:00
|
|
|
oldst = emalloc(fcnt * sizeof(*oldst));
|
2014-11-27 21:37:20 +00:00
|
|
|
for (f = i = 0; f < fcnt; i++) {
|
2015-01-04 14:38:49 +00:00
|
|
|
if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
|
2014-11-27 21:37:20 +00:00
|
|
|
stat(files[i].path, &oldst[f]);
|
2021-10-28 05:43:36 +00:00
|
|
|
fprintf(pfs, "%s%c", files[i].name, options->using_null ? '\0' : '\n');
|
2014-11-27 21:37:20 +00:00
|
|
|
f++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(pfs);
|
2023-02-10 05:35:53 +00:00
|
|
|
while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
|
|
|
|
;
|
2014-01-08 19:31:50 +00:00
|
|
|
|
2014-11-27 21:37:20 +00:00
|
|
|
for (f = i = 0; f < fcnt; i++) {
|
2015-01-04 14:38:49 +00:00
|
|
|
if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
|
2014-11-27 21:37:20 +00:00
|
|
|
if (stat(files[i].path, &st) != 0 ||
|
2021-10-11 03:07:18 +00:00
|
|
|
memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
|
2014-11-27 21:37:20 +00:00
|
|
|
{
|
|
|
|
if (tns.thumbs != NULL) {
|
|
|
|
tns_unload(&tns, i);
|
|
|
|
tns.loadnext = MIN(tns.loadnext, i);
|
|
|
|
}
|
|
|
|
changed = true;
|
2014-08-16 19:31:05 +00:00
|
|
|
}
|
2014-11-27 21:37:20 +00:00
|
|
|
f++;
|
2014-08-16 19:31:05 +00:00
|
|
|
}
|
2014-01-08 19:31:50 +00:00
|
|
|
}
|
2017-02-15 19:20:41 +00:00
|
|
|
/* drop user input events that occurred while running the key handler */
|
2023-02-10 05:35:53 +00:00
|
|
|
while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL))
|
|
|
|
;
|
2016-11-29 11:07:08 +00:00
|
|
|
|
2022-08-16 08:53:05 +00:00
|
|
|
if (mode == MODE_IMAGE && changed) {
|
|
|
|
img_close(&img, true);
|
|
|
|
load_image(fileidx);
|
|
|
|
} else {
|
2022-05-09 11:56:27 +00:00
|
|
|
open_info();
|
2022-08-16 08:53:05 +00:00
|
|
|
}
|
2014-11-27 21:37:20 +00:00
|
|
|
free(oldst);
|
2014-07-28 18:36:32 +00:00
|
|
|
reset_cursor();
|
2022-01-03 09:24:26 +00:00
|
|
|
return true;
|
2014-01-02 22:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 00:14:12 +00:00
|
|
|
static bool process_bindings(const keymap_t *bindings, unsigned int len, KeySym ksym_or_button,
|
2021-11-30 23:41:22 +00:00
|
|
|
unsigned int state, unsigned int implicit_mod)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2021-10-28 20:00:53 +00:00
|
|
|
unsigned int i;
|
2021-11-19 05:49:42 +00:00
|
|
|
bool dirty = false;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2021-12-24 00:14:12 +00:00
|
|
|
if (bindings[i].ksym_or_button == ksym_or_button &&
|
|
|
|
MODMASK(bindings[i].mask | implicit_mod) == MODMASK(state) &&
|
2022-08-16 08:53:05 +00:00
|
|
|
bindings[i].cmd.func != NULL &&
|
2021-12-24 00:14:12 +00:00
|
|
|
(bindings[i].cmd.mode == MODE_ALL || bindings[i].cmd.mode == mode))
|
2021-11-19 05:49:42 +00:00
|
|
|
{
|
2021-12-24 00:14:12 +00:00
|
|
|
if (bindings[i].cmd.func(bindings[i].arg))
|
2021-11-19 05:49:42 +00:00
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dirty;
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void on_keypress(XKeyEvent *kev)
|
2021-11-19 05:49:42 +00:00
|
|
|
{
|
2018-01-09 18:37:56 +00:00
|
|
|
unsigned int sh = 0;
|
2014-01-02 12:36:00 +00:00
|
|
|
KeySym ksym, shksym;
|
2018-01-09 18:37:56 +00:00
|
|
|
char dummy, key;
|
2014-07-23 21:41:23 +00:00
|
|
|
bool dirty = false;
|
2011-08-19 13:02:10 +00:00
|
|
|
|
2018-01-09 18:37:56 +00:00
|
|
|
XLookupString(kev, &key, 1, &ksym, NULL);
|
|
|
|
|
2014-01-02 12:36:00 +00:00
|
|
|
if (kev->state & ShiftMask) {
|
|
|
|
kev->state &= ~ShiftMask;
|
2018-01-09 18:37:56 +00:00
|
|
|
XLookupString(kev, &dummy, 1, &shksym, NULL);
|
2014-01-02 12:36:00 +00:00
|
|
|
kev->state |= ShiftMask;
|
2018-01-09 18:37:56 +00:00
|
|
|
if (ksym != shksym)
|
|
|
|
sh = ShiftMask;
|
2014-01-02 12:36:00 +00:00
|
|
|
}
|
2014-01-02 22:19:31 +00:00
|
|
|
if (IsModifierKey(ksym))
|
|
|
|
return;
|
2021-11-24 11:38:25 +00:00
|
|
|
if (extprefix && ksym == KEYHANDLER_ABORT && MODMASK(kev->state) == 0) {
|
2021-10-07 00:37:34 +00:00
|
|
|
handle_key_handler(false);
|
2014-01-31 12:21:23 +00:00
|
|
|
} else if (extprefix) {
|
2022-01-03 09:24:26 +00:00
|
|
|
if ((dirty = run_key_handler(XKeysymToString(ksym), kev->state & ~sh)))
|
|
|
|
extprefix = false;
|
|
|
|
else
|
|
|
|
handle_key_handler(false);
|
2014-01-31 12:21:23 +00:00
|
|
|
} else if (key >= '0' && key <= '9') {
|
2011-10-16 14:08:55 +00:00
|
|
|
/* number prefix for commands */
|
2023-02-10 05:35:53 +00:00
|
|
|
prefix = prefix * 10 + (int)(key - '0');
|
2011-10-16 14:08:55 +00:00
|
|
|
return;
|
2021-11-19 05:49:42 +00:00
|
|
|
} else {
|
2021-11-30 23:41:22 +00:00
|
|
|
dirty = process_bindings(keys, ARRLEN(keys), ksym, kev->state, sh);
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
2014-07-23 21:41:23 +00:00
|
|
|
if (dirty)
|
|
|
|
redraw();
|
2014-01-08 21:58:27 +00:00
|
|
|
prefix = 0;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:52:06 +00:00
|
|
|
static void on_buttonpress(const XButtonEvent *bev)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2014-07-23 21:41:23 +00:00
|
|
|
bool dirty = false;
|
2011-08-19 13:02:10 +00:00
|
|
|
|
|
|
|
if (mode == MODE_IMAGE) {
|
2011-09-11 19:01:24 +00:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
|
2017-10-05 11:49:11 +00:00
|
|
|
reset_cursor();
|
2022-01-10 16:52:06 +00:00
|
|
|
dirty = process_bindings(buttons_img, ARRLEN(buttons_img), bev->button, bev->state, 0);
|
|
|
|
} else { /* thumbnail mode */
|
|
|
|
dirty = process_bindings(buttons_tns, ARRLEN(buttons_tns), bev->button, bev->state, 0);
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
2022-01-10 16:52:06 +00:00
|
|
|
if (dirty)
|
|
|
|
redraw();
|
2014-01-08 21:58:27 +00:00
|
|
|
prefix = 0;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void run(void)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2023-01-09 08:12:30 +00:00
|
|
|
enum { FD_X, FD_INFO, FD_TITLE, FD_ARL, FD_CNT };
|
2022-04-28 03:12:15 +00:00
|
|
|
struct pollfd pfd[FD_CNT];
|
2022-05-12 14:47:46 +00:00
|
|
|
int timeout = 0;
|
2015-01-04 20:19:26 +00:00
|
|
|
bool discard, init_thumb, load_thumb, to_set;
|
2012-08-17 14:54:29 +00:00
|
|
|
XEvent ev, nextev;
|
2011-08-19 13:02:10 +00:00
|
|
|
|
2022-01-10 16:52:06 +00:00
|
|
|
xbutton_ev = &ev.xbutton;
|
2011-09-29 10:43:36 +00:00
|
|
|
while (true) {
|
2014-09-26 20:45:01 +00:00
|
|
|
to_set = check_timeouts(&timeout);
|
2015-01-04 20:19:26 +00:00
|
|
|
init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
|
2014-09-26 20:45:01 +00:00
|
|
|
load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
|
2011-09-02 02:33:44 +00:00
|
|
|
|
2022-07-02 15:06:24 +00:00
|
|
|
if ((init_thumb || load_thumb || to_set || info.fd != -1 || arl.fd != -1) &&
|
|
|
|
XPending(win.env.dpy) == 0)
|
2013-02-11 22:05:26 +00:00
|
|
|
{
|
2014-09-26 20:45:01 +00:00
|
|
|
if (load_thumb) {
|
|
|
|
set_timeout(redraw, TO_REDRAW_THUMBS, false);
|
2015-01-04 20:19:26 +00:00
|
|
|
if (!tns_load(&tns, tns.loadnext, false, false)) {
|
2014-09-26 20:45:01 +00:00
|
|
|
remove_file(tns.loadnext, false);
|
|
|
|
tns.dirty = true;
|
|
|
|
}
|
2022-07-26 05:20:39 +00:00
|
|
|
if (tns.loadnext >= tns.end) {
|
|
|
|
open_info();
|
2014-09-26 20:45:01 +00:00
|
|
|
redraw();
|
2022-07-26 05:20:39 +00:00
|
|
|
}
|
2015-01-04 20:19:26 +00:00
|
|
|
} else if (init_thumb) {
|
|
|
|
set_timeout(redraw, TO_REDRAW_THUMBS, false);
|
|
|
|
if (!tns_load(&tns, tns.initnext, false, true))
|
|
|
|
remove_file(tns.initnext, false);
|
2014-09-26 20:45:01 +00:00
|
|
|
} else {
|
2022-04-28 03:12:15 +00:00
|
|
|
pfd[FD_X].fd = ConnectionNumber(win.env.dpy);
|
|
|
|
pfd[FD_INFO].fd = info.fd;
|
2023-01-09 08:12:30 +00:00
|
|
|
pfd[FD_TITLE].fd = wintitle.fd;
|
2022-04-28 03:12:15 +00:00
|
|
|
pfd[FD_ARL].fd = arl.fd;
|
2023-01-09 08:12:30 +00:00
|
|
|
pfd[FD_X].events = pfd[FD_INFO].events = pfd[FD_TITLE].events = pfd[FD_ARL].events = POLLIN;
|
2022-04-28 03:12:15 +00:00
|
|
|
|
2022-05-12 14:47:46 +00:00
|
|
|
if (poll(pfd, ARRLEN(pfd), to_set ? timeout : -1) < 0)
|
2022-04-28 03:12:15 +00:00
|
|
|
continue;
|
|
|
|
if (pfd[FD_INFO].revents & POLLIN)
|
2014-09-26 20:45:01 +00:00
|
|
|
read_info();
|
2023-01-09 08:12:30 +00:00
|
|
|
if (pfd[FD_TITLE].revents & POLLIN)
|
|
|
|
read_title();
|
2023-08-28 10:28:57 +00:00
|
|
|
if ((pfd[FD_ARL].revents & POLLIN) && arl_handle(&arl)) {
|
|
|
|
img.autoreload_pending = true;
|
|
|
|
set_timeout(autoreload, TO_AUTORELOAD, true);
|
2017-05-17 18:07:32 +00:00
|
|
|
}
|
2013-02-11 22:05:26 +00:00
|
|
|
}
|
2014-09-26 20:45:01 +00:00
|
|
|
continue;
|
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) {
|
2022-08-16 08:54:31 +00:00
|
|
|
case ConfigureNotify:
|
|
|
|
case MotionNotify:
|
|
|
|
discard = ev.type == nextev.type;
|
|
|
|
break;
|
|
|
|
case KeyPress:
|
2022-07-02 15:06:24 +00:00
|
|
|
discard = (nextev.type == KeyPress || nextev.type == KeyRelease) &&
|
2023-02-10 05:35:53 +00:00
|
|
|
ev.xkey.keycode == nextev.xkey.keycode;
|
2022-08-16 08:54:31 +00:00
|
|
|
break;
|
2012-10-29 17:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (discard);
|
2012-08-17 14:54:29 +00:00
|
|
|
|
2022-08-16 08:54:31 +00:00
|
|
|
switch (ev.type) { /* handle events */
|
|
|
|
case ButtonPress:
|
|
|
|
on_buttonpress(&ev.xbutton);
|
|
|
|
break;
|
|
|
|
case ClientMessage:
|
2023-02-10 05:35:53 +00:00
|
|
|
if ((Atom)ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
|
2022-08-16 08:54:31 +00:00
|
|
|
cg_quit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
case DestroyNotify:
|
|
|
|
cg_quit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
case ConfigureNotify:
|
|
|
|
if (win_configure(&win, &ev.xconfigure)) {
|
2011-09-29 10:43:36 +00:00
|
|
|
if (mode == MODE_IMAGE) {
|
2022-08-16 08:54:31 +00:00
|
|
|
img.dirty = true;
|
|
|
|
img.checkpan = true;
|
|
|
|
} else {
|
|
|
|
tns.dirty = true;
|
2011-09-29 10:43:36 +00:00
|
|
|
}
|
2022-08-16 08:54:31 +00:00
|
|
|
if (!resized) {
|
|
|
|
redraw();
|
|
|
|
set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
|
|
|
|
resized = true;
|
|
|
|
} else {
|
|
|
|
set_timeout(redraw, TO_REDRAW_RESIZE, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KeyPress:
|
|
|
|
on_keypress(&ev.xkey);
|
|
|
|
break;
|
|
|
|
case MotionNotify:
|
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
|
|
|
|
reset_cursor();
|
|
|
|
}
|
|
|
|
break;
|
2011-08-19 13:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 16:26:32 +00:00
|
|
|
static void setup_signal(int sig, void (*handler)(int sig), int flags)
|
2018-02-18 13:32:55 +00:00
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
sa.sa_handler = handler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
2023-01-26 16:26:32 +00:00
|
|
|
sa.sa_flags = flags;
|
|
|
|
if (sigaction(sig, &sa, NULL) < 0)
|
2018-02-18 13:32:55 +00:00
|
|
|
error(EXIT_FAILURE, errno, "signal %d", sig);
|
|
|
|
}
|
|
|
|
|
2021-10-11 03:07:18 +00:00
|
|
|
int main(int argc, char *argv[])
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2022-10-30 18:18:44 +00:00
|
|
|
int i;
|
2011-05-29 09:45:58 +00:00
|
|
|
size_t n;
|
2014-01-11 21:47:41 +00:00
|
|
|
const char *homedir, *dsuffix = "";
|
2011-01-20 15:24:48 +00:00
|
|
|
|
2023-02-10 05:35:53 +00:00
|
|
|
setup_signal(SIGCHLD, SIG_DFL, SA_RESTART | SA_NOCLDSTOP | SA_NOCLDWAIT);
|
2023-01-26 16:26:32 +00:00
|
|
|
setup_signal(SIGPIPE, SIG_IGN, 0);
|
2014-12-01 13:03:49 +00:00
|
|
|
|
2017-10-24 19:43:36 +00:00
|
|
|
setlocale(LC_COLLATE, "");
|
|
|
|
|
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) {
|
2014-09-30 19:54:17 +00:00
|
|
|
tns_init(&tns, NULL, NULL, NULL, NULL);
|
2021-10-28 20:00:53 +00:00
|
|
|
tns_clean_cache();
|
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)
|
2015-10-28 21:23:28 +00:00
|
|
|
filecnt = 1024;
|
2011-02-02 08:01:05 +00:00
|
|
|
else
|
|
|
|
filecnt = options->filecnt;
|
|
|
|
|
2022-02-03 08:22:25 +00:00
|
|
|
files = ecalloc(filecnt, sizeof(*files));
|
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) {
|
2022-10-30 18:18:44 +00:00
|
|
|
char *filename = NULL;
|
2015-10-28 20:50:17 +00:00
|
|
|
n = 0;
|
2021-11-19 10:08:01 +00:00
|
|
|
while (xgetline(&filename, &n))
|
2022-10-30 18:18:44 +00:00
|
|
|
add_entry(filename);
|
2014-08-21 18:51:02 +00:00
|
|
|
free(filename);
|
2013-03-19 20:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 18:18:44 +00:00
|
|
|
for (i = 0; i < options->filecnt; i++)
|
|
|
|
add_entry(options->filenames[i]);
|
2011-01-20 15:24:48 +00:00
|
|
|
|
2015-10-28 22:03:37 +00:00
|
|
|
if (fileidx == 0)
|
|
|
|
error(EXIT_FAILURE, 0, "No valid image file given, aborting");
|
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);
|
2017-05-17 18:07:32 +00:00
|
|
|
arl_init(&arl);
|
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) {
|
2022-05-03 15:34:23 +00:00
|
|
|
extcmd_t *cmd[] = { &info.f, &info.ft, &keyhandler.f, &wintitle.f };
|
|
|
|
const char *name[] = { "image-info", "thumb-info", "key-handler", "win-title" };
|
2021-09-16 09:32:59 +00:00
|
|
|
const char *s = "/nsxiv/exec/";
|
2014-01-11 21:47:41 +00:00
|
|
|
|
2022-07-15 20:46:23 +00:00
|
|
|
for (i = 0; i < (int)ARRLEN(cmd); i++) {
|
2021-10-28 20:00:53 +00:00
|
|
|
n = strlen(homedir) + strlen(dsuffix) + strlen(s) + strlen(name[i]) + 1;
|
2021-10-23 21:57:22 +00:00
|
|
|
cmd[i]->cmd = emalloc(n);
|
2021-09-16 09:32:59 +00:00
|
|
|
snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]);
|
2015-10-28 22:03:37 +00:00
|
|
|
if (access(cmd[i]->cmd, X_OK) != 0)
|
|
|
|
cmd[i]->err = errno;
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2014-01-11 21:47:41 +00:00
|
|
|
} else {
|
2015-10-28 22:03:37 +00:00
|
|
|
error(0, 0, "Exec directory not found");
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2023-01-09 08:12:30 +00:00
|
|
|
wintitle.fd = 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;
|
2014-09-30 19:54:17 +00:00
|
|
|
tns_init(&tns, files, &filecnt, &fileidx, &win);
|
2015-01-04 20:19:26 +00:00
|
|
|
while (!tns_load(&tns, fileidx, false, false))
|
|
|
|
remove_file(fileidx, false);
|
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-04-11 14:58:38 +00:00
|
|
|
win_open(&win);
|
2014-07-28 18:36:32 +00:00
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
2011-09-29 10:43:36 +00:00
|
|
|
|
2015-10-28 22:03:37 +00:00
|
|
|
atexit(cleanup);
|
|
|
|
|
2014-09-26 18:45:15 +00:00
|
|
|
set_timeout(redraw, 25, false);
|
|
|
|
|
2011-01-19 13:07:45 +00:00
|
|
|
run();
|
2011-01-17 15:41:50 +00:00
|
|
|
|
2011-01-17 13:57:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|