2011-01-17 14:57:59 +01:00
|
|
|
/* sxiv: main.c
|
2012-02-15 19:16:24 +01:00
|
|
|
* Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
|
2011-01-17 14:57:59 +01:00
|
|
|
*
|
2011-09-03 23:11:45 +02: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 01:18:26 +02:00
|
|
|
*
|
2011-09-03 23:11:45 +02: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 01:18:26 +02:00
|
|
|
*
|
2011-09-03 23:11:45 +02: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 14:57:59 +01:00
|
|
|
*/
|
|
|
|
|
2011-09-08 20:54:24 +02:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2011-09-11 21:01:24 +02:00
|
|
|
#define _MAPPINGS_CONFIG
|
2011-09-08 20:54:24 +02:00
|
|
|
|
2011-01-17 16:41:50 +01:00
|
|
|
#include <stdlib.h>
|
2011-08-19 15:02:10 +02:00
|
|
|
#include <stdio.h>
|
2011-02-02 10:34:14 +01:00
|
|
|
#include <string.h>
|
2012-02-12 19:00:41 +01:00
|
|
|
#include <libgen.h>
|
2011-03-01 18:49:02 +01:00
|
|
|
#include <unistd.h>
|
2011-07-26 18:01:29 +02:00
|
|
|
#include <sys/stat.h>
|
2011-08-19 15:02:10 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <X11/keysym.h>
|
2011-01-17 16:41:50 +01:00
|
|
|
|
2011-08-19 15:02:10 +02:00
|
|
|
#include "commands.h"
|
2011-01-19 14:07:45 +01:00
|
|
|
#include "image.h"
|
2011-02-03 10:15:01 +01:00
|
|
|
#include "options.h"
|
2011-02-16 21:40:20 +01:00
|
|
|
#include "thumbs.h"
|
2011-08-18 00:38:55 +02:00
|
|
|
#include "types.h"
|
2011-02-03 10:15:01 +01:00
|
|
|
#include "util.h"
|
2011-01-19 14:07:45 +01:00
|
|
|
#include "window.h"
|
2011-08-19 15:02:10 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
enum {
|
2012-02-12 19:00:41 +01:00
|
|
|
INFO_STR_LEN = 256,
|
|
|
|
FILENAME_CNT = 1024
|
2011-07-26 18:01:29 +02:00
|
|
|
};
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-09-02 04:33:44 +02:00
|
|
|
typedef struct {
|
|
|
|
struct timeval when;
|
2011-09-11 21:01:24 +02:00
|
|
|
bool active;
|
2011-09-02 04:33:44 +02:00
|
|
|
timeout_f handler;
|
|
|
|
} timeout_t;
|
|
|
|
|
|
|
|
/* timeout handler functions: */
|
2011-10-12 18:38:29 +02:00
|
|
|
void redraw(void);
|
|
|
|
void reset_cursor(void);
|
|
|
|
void animate(void);
|
2011-09-02 04:33:44 +02:00
|
|
|
|
2011-02-16 17:09:46 +01:00
|
|
|
appmode_t mode;
|
2011-01-19 14:07:45 +01:00
|
|
|
img_t img;
|
2011-02-16 21:40:20 +01:00
|
|
|
tns_t tns;
|
2011-01-19 14:07:45 +01:00
|
|
|
win_t win;
|
2011-01-20 16:24:48 +01:00
|
|
|
|
2011-08-18 00:38:55 +02:00
|
|
|
fileinfo_t *files;
|
2011-01-23 12:36:27 +01:00
|
|
|
int filecnt, fileidx;
|
2011-02-03 14:32:02 +01:00
|
|
|
size_t filesize;
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-10-16 16:08:55 +02:00
|
|
|
int prefix;
|
|
|
|
|
2012-02-12 19:00:41 +01:00
|
|
|
char win_bar_l[INFO_STR_LEN];
|
|
|
|
char win_bar_r[INFO_STR_LEN];
|
|
|
|
char win_title[INFO_STR_LEN];
|
2011-01-20 17:00:59 +01:00
|
|
|
|
2011-09-02 04:33:44 +02:00
|
|
|
timeout_t timeouts[] = {
|
2011-09-11 21:01:24 +02:00
|
|
|
{ { 0, 0 }, false, redraw },
|
|
|
|
{ { 0, 0 }, false, reset_cursor },
|
|
|
|
{ { 0, 0 }, false, animate },
|
2011-09-02 04:33:44 +02:00
|
|
|
};
|
2011-08-19 15:02:10 +02:00
|
|
|
|
2011-10-12 18:38:29 +02:00
|
|
|
void cleanup(void) {
|
2011-09-29 12:43:36 +02:00
|
|
|
static bool in = false;
|
2011-02-03 10:15:01 +01:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (!in) {
|
|
|
|
in = true;
|
2011-09-11 21:01:24 +02:00
|
|
|
img_close(&img, false);
|
2011-04-11 16:58:38 +02:00
|
|
|
tns_free(&tns);
|
2011-02-03 10:15:01 +01:00
|
|
|
win_close(&win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-18 00:38:55 +02:00
|
|
|
void check_add_file(char *filename) {
|
2011-09-29 12:43:36 +02:00
|
|
|
if (filename == NULL || *filename == '\0')
|
2011-08-18 00:38:55 +02:00
|
|
|
return;
|
2011-07-26 18:01:29 +02:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (access(filename, R_OK) < 0) {
|
2011-07-26 18:01:29 +02:00
|
|
|
warn("could not open file: %s", filename);
|
2011-08-18 00:38:55 +02: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 12:43:36 +02:00
|
|
|
if (files[fileidx].path == NULL) {
|
2011-08-18 00:38:55 +02:00
|
|
|
warn("could not get absolute path of file: %s\n", filename);
|
|
|
|
return;
|
2011-07-26 18:01:29 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-11 21:01:24 +02:00
|
|
|
files[fileidx].loaded = false;
|
2011-08-18 00:38:55 +02:00
|
|
|
files[fileidx].name = s_strdup(filename);
|
2012-02-12 19:00:41 +01:00
|
|
|
files[fileidx].base = s_strdup(basename(filename));
|
2011-08-18 00:38:55 +02:00
|
|
|
if (*filename == '/')
|
|
|
|
files[fileidx].path = files[fileidx].name;
|
|
|
|
fileidx++;
|
2011-07-26 18:01:29 +02:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:28:27 +02:00
|
|
|
void remove_file(int n, bool manual) {
|
2011-04-11 16:58:38 +02:00
|
|
|
if (n < 0 || n >= filecnt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (filecnt == 1) {
|
2011-09-26 21:28:27 +02:00
|
|
|
if (!manual)
|
2011-04-11 20:42:08 +02:00
|
|
|
fprintf(stderr, "sxiv: no more files to display, aborting\n");
|
2011-04-11 16:58:38 +02:00
|
|
|
cleanup();
|
2011-09-26 21:28:27 +02:00
|
|
|
exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
|
2011-04-11 16:58:38 +02:00
|
|
|
}
|
|
|
|
|
2011-09-03 19:08:49 +02:00
|
|
|
if (files[n].path != files[n].name)
|
|
|
|
free((void*) files[n].path);
|
|
|
|
free((void*) files[n].name);
|
|
|
|
|
|
|
|
if (n + 1 < filecnt)
|
2011-08-18 00:38:55 +02:00
|
|
|
memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
|
2011-04-11 16:58:38 +02:00
|
|
|
if (n + 1 < tns.cnt) {
|
|
|
|
memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
|
|
|
|
sizeof(thumb_t));
|
|
|
|
memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
|
|
|
|
}
|
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
filecnt--;
|
2011-04-11 16:58:38 +02:00
|
|
|
if (n < tns.cnt)
|
2011-07-26 18:01:29 +02:00
|
|
|
tns.cnt--;
|
2011-04-11 16:58:38 +02:00
|
|
|
}
|
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
void set_timeout(timeout_f handler, int time, bool overwrite) {
|
2011-09-02 04:33:44 +02:00
|
|
|
int i;
|
|
|
|
|
2011-09-06 09:11:03 +02:00
|
|
|
for (i = 0; i < ARRLEN(timeouts); i++) {
|
2011-09-02 04:33:44 +02:00
|
|
|
if (timeouts[i].handler == handler) {
|
|
|
|
if (!timeouts[i].active || overwrite) {
|
|
|
|
gettimeofday(&timeouts[i].when, 0);
|
2011-10-13 16:50:06 +02:00
|
|
|
TV_ADD_MSEC(&timeouts[i].when, time);
|
2011-09-11 21:01:24 +02:00
|
|
|
timeouts[i].active = true;
|
2011-09-02 04:33:44 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_timeout(timeout_f handler) {
|
|
|
|
int i;
|
|
|
|
|
2011-09-06 09:11:03 +02:00
|
|
|
for (i = 0; i < ARRLEN(timeouts); i++) {
|
2011-09-02 04:33:44 +02:00
|
|
|
if (timeouts[i].handler == handler) {
|
2011-09-11 21:01:24 +02:00
|
|
|
timeouts[i].active = false;
|
2011-09-02 04:33:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
bool check_timeouts(struct timeval *t) {
|
2011-09-02 18:54:41 +02:00
|
|
|
int i = 0, tdiff, tmin = -1;
|
2011-09-02 04:33:44 +02:00
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
gettimeofday(&now, 0);
|
2011-09-06 09:11:03 +02:00
|
|
|
while (i < ARRLEN(timeouts)) {
|
2011-09-02 04:33:44 +02:00
|
|
|
if (timeouts[i].active) {
|
2011-10-13 16:50:06 +02:00
|
|
|
tdiff = TV_DIFF(&timeouts[i].when, &now);
|
2011-09-02 04:33:44 +02:00
|
|
|
if (tdiff <= 0) {
|
2011-09-11 21:01:24 +02:00
|
|
|
timeouts[i].active = false;
|
2011-09-29 12:43:36 +02:00
|
|
|
if (timeouts[i].handler != NULL)
|
2011-09-02 04:33:44 +02:00
|
|
|
timeouts[i].handler();
|
2011-09-02 18:54:41 +02:00
|
|
|
i = tmin = -1;
|
2011-09-02 04:33:44 +02:00
|
|
|
} else if (tmin < 0 || tdiff < tmin) {
|
|
|
|
tmin = tdiff;
|
|
|
|
}
|
|
|
|
}
|
2011-09-02 18:54:41 +02:00
|
|
|
i++;
|
2011-09-02 04:33:44 +02:00
|
|
|
}
|
2011-09-29 12:43:36 +02:00
|
|
|
if (tmin > 0 && t != NULL)
|
2011-10-13 16:50:06 +02:00
|
|
|
TV_SET_MSEC(t, tmin);
|
2011-09-02 04:33:44 +02:00
|
|
|
return tmin > 0;
|
|
|
|
}
|
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
void load_image(int new) {
|
2011-02-03 14:32:02 +01:00
|
|
|
struct stat fstats;
|
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
if (new < 0 || new >= filecnt)
|
|
|
|
return;
|
2011-04-11 16:58:38 +02:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
2011-09-03 17:01:39 +02:00
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
img_close(&img, false);
|
2011-08-18 00:38:55 +02:00
|
|
|
while (!img_load(&img, &files[new])) {
|
2011-09-11 21:01:24 +02:00
|
|
|
remove_file(new, false);
|
2011-07-26 18:01:29 +02:00
|
|
|
if (new >= filecnt)
|
|
|
|
new = filecnt - 1;
|
2011-02-27 13:29:24 +01:00
|
|
|
}
|
2011-07-26 18:01:29 +02:00
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
files[new].loaded = true;
|
2011-07-26 18:01:29 +02:00
|
|
|
fileidx = new;
|
2011-09-29 12:43:36 +02:00
|
|
|
if (stat(files[new].path, &fstats) == 0)
|
2011-07-26 18:01:29 +02:00
|
|
|
filesize = fstats.st_size;
|
|
|
|
else
|
|
|
|
filesize = 0;
|
2011-08-18 16:05:32 +02:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (img.multi.cnt > 0 && img.multi.animate)
|
2011-09-11 21:01:24 +02:00
|
|
|
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
|
2011-09-06 11:55:31 +02:00
|
|
|
else
|
|
|
|
reset_timeout(animate);
|
2011-02-03 14:32:02 +01:00
|
|
|
}
|
|
|
|
|
2012-02-12 19:00:41 +01:00
|
|
|
void update_info(void) {
|
|
|
|
unsigned int i, fw, pw;
|
2011-09-10 18:41:20 +02:00
|
|
|
char frame_info[16];
|
2012-02-11 02:34:18 +01:00
|
|
|
const char *size_unit;
|
2012-02-12 19:00:41 +01:00
|
|
|
float size = filesize;
|
|
|
|
|
|
|
|
pw = 0;
|
|
|
|
for (i = filecnt; i > 0; i /= 10)
|
|
|
|
pw++;
|
2011-04-11 11:53:00 +02:00
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_THUMB) {
|
2012-02-15 19:29:56 +01:00
|
|
|
if (tns.cnt != filecnt) {
|
|
|
|
snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d",
|
|
|
|
pw, tns.cnt, filecnt);
|
|
|
|
} else {
|
|
|
|
snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
|
|
|
|
pw, tns.sel + 1, filecnt, BAR_SEPARATOR, files[tns.sel].base);
|
|
|
|
}
|
|
|
|
win_set_title(&win, "sxiv");
|
|
|
|
win_set_bar_info(&win, win_bar_l, NULL);
|
2011-04-11 11:53:00 +02:00
|
|
|
} else {
|
2011-09-10 18:41:20 +02:00
|
|
|
size_readable(&size, &size_unit);
|
2012-02-12 19:00:41 +01:00
|
|
|
if (img.multi.cnt > 0) {
|
|
|
|
fw = 0;
|
|
|
|
for (i = img.multi.cnt; i > 0; i /= 10)
|
|
|
|
fw++;
|
2012-02-15 18:25:45 +01:00
|
|
|
snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
|
|
|
|
BAR_SEPARATOR, fw, img.multi.sel + 1, img.multi.cnt);
|
2012-02-12 19:00:41 +01:00
|
|
|
} else {
|
2011-09-10 18:41:20 +02:00
|
|
|
frame_info[0] = '\0';
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
2012-02-15 18:25:45 +01:00
|
|
|
snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
|
|
|
|
pw, fileidx + 1, filecnt, BAR_SEPARATOR, files[fileidx].base);
|
|
|
|
snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
|
|
|
|
size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
|
|
|
|
(int) (img.zoom * 100.0), frame_info);
|
2012-02-15 19:29:56 +01:00
|
|
|
snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name);
|
|
|
|
win_set_title(&win, win_title);
|
|
|
|
win_set_bar_info(&win, win_bar_l, win_bar_r);
|
2011-04-11 11:53:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-12 18:38:29 +02:00
|
|
|
void redraw(void) {
|
2012-02-11 02:34:18 +01:00
|
|
|
if (mode == MODE_IMAGE)
|
2011-09-17 17:23:51 +02:00
|
|
|
img_render(&img);
|
2012-02-11 02:34:18 +01:00
|
|
|
else
|
2011-09-17 17:23:51 +02:00
|
|
|
tns_render(&tns);
|
2012-02-12 19:00:41 +01:00
|
|
|
update_info();
|
|
|
|
win_draw(&win);
|
2011-09-02 04:33:44 +02:00
|
|
|
reset_timeout(redraw);
|
2011-09-03 17:01:39 +02:00
|
|
|
reset_cursor();
|
2011-09-02 04:33:44 +02:00
|
|
|
}
|
|
|
|
|
2011-10-12 18:38:29 +02:00
|
|
|
void reset_cursor(void) {
|
2011-09-03 17:01:39 +02:00
|
|
|
int i;
|
|
|
|
cursor_t cursor = CURSOR_NONE;
|
|
|
|
|
|
|
|
if (mode == MODE_IMAGE) {
|
2011-09-06 09:11:03 +02:00
|
|
|
for (i = 0; i < ARRLEN(timeouts); i++) {
|
2011-09-03 17:01:39 +02: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 04:33:44 +02:00
|
|
|
}
|
|
|
|
|
2011-10-12 18:38:29 +02:00
|
|
|
void animate(void) {
|
2011-09-11 21:01:24 +02:00
|
|
|
if (img_frame_animate(&img, false)) {
|
|
|
|
redraw();
|
|
|
|
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
|
|
|
|
}
|
2011-09-10 18:41:20 +02:00
|
|
|
}
|
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
bool keymask(const keymap_t *k, unsigned int state) {
|
2011-08-19 15:02:10 +02:00
|
|
|
return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
|
|
|
|
}
|
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
bool buttonmask(const button_t *b, unsigned int state) {
|
2011-08-19 15:02:10 +02:00
|
|
|
return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
|
|
|
|
(state & (ControlMask | ShiftMask));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_keypress(XKeyEvent *kev) {
|
|
|
|
int i;
|
|
|
|
KeySym ksym;
|
|
|
|
char key;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (kev == NULL)
|
2011-08-19 15:02:10 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
XLookupString(kev, &key, 1, &ksym, NULL);
|
|
|
|
|
2011-10-16 17:39:22 +02:00
|
|
|
if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
|
|
|
|
(kev->state & ControlMask) == 0)
|
|
|
|
{
|
2011-10-16 16:08:55 +02:00
|
|
|
/* number prefix for commands */
|
2011-10-16 17:39:22 +02:00
|
|
|
prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
|
2011-10-16 16:08:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-06 09:11:03 +02:00
|
|
|
for (i = 0; i < ARRLEN(keys); i++) {
|
2011-08-19 15:02:10 +02:00
|
|
|
if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
|
2011-09-29 12:43:36 +02:00
|
|
|
if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
|
2011-08-19 15:02:10 +02:00
|
|
|
redraw();
|
2011-10-16 16:08:55 +02:00
|
|
|
prefix = 0;
|
2011-08-19 15:02:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_buttonpress(XButtonEvent *bev) {
|
|
|
|
int i, sel;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (bev == NULL)
|
2011-08-19 15:02:10 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (mode == MODE_IMAGE) {
|
|
|
|
win_set_cursor(&win, CURSOR_ARROW);
|
2011-09-11 21:01:24 +02:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
|
2011-08-19 15:02:10 +02:00
|
|
|
|
2011-09-06 09:11:03 +02:00
|
|
|
for (i = 0; i < ARRLEN(buttons); i++) {
|
2011-08-19 15:02:10 +02:00
|
|
|
if (buttons[i].button == bev->button &&
|
|
|
|
buttonmask(&buttons[i], bev->state))
|
|
|
|
{
|
2011-09-29 12:43:36 +02:00
|
|
|
if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
|
2011-08-19 15:02:10 +02:00
|
|
|
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-11 21:01:24 +02:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
|
2011-09-03 17:01:39 +02:00
|
|
|
load_image(tns.sel);
|
2011-08-19 15:02:10 +02:00
|
|
|
} else {
|
2011-09-17 17:23:51 +02:00
|
|
|
tns_highlight(&tns, tns.sel, false);
|
|
|
|
tns_highlight(&tns, sel, true);
|
2011-08-19 15:02:10 +02:00
|
|
|
tns.sel = sel;
|
|
|
|
}
|
2011-09-04 20:14:38 +02:00
|
|
|
redraw();
|
2011-08-19 15:02:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Button4:
|
|
|
|
case Button5:
|
2011-10-27 16:21:01 +02:00
|
|
|
if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
|
|
|
|
(bev->state & ControlMask) != 0))
|
2011-08-19 15:02:10 +02:00
|
|
|
redraw();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-12 18:38:29 +02:00
|
|
|
void run(void) {
|
2011-09-02 04:33:44 +02:00
|
|
|
int xfd;
|
2011-08-19 15:02:10 +02:00
|
|
|
fd_set fds;
|
2011-09-02 04:33:44 +02:00
|
|
|
struct timeval timeout;
|
2011-08-19 15:02:10 +02:00
|
|
|
XEvent ev;
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
while (true) {
|
2011-09-02 15:56:03 +02:00
|
|
|
while (mode == MODE_THUMB && tns.cnt < filecnt &&
|
2011-09-29 12:43:36 +02:00
|
|
|
XPending(win.env.dpy) == 0)
|
2011-09-02 15:56:03 +02:00
|
|
|
{
|
2011-08-19 15:02:10 +02:00
|
|
|
/* load thumbnails */
|
2011-09-11 21:01:24 +02:00
|
|
|
set_timeout(redraw, TO_REDRAW_THUMBS, false);
|
2011-11-01 08:36:20 +01:00
|
|
|
if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
|
2011-09-02 15:56:03 +02:00
|
|
|
tns.cnt++;
|
2011-11-01 08:36:20 +01:00
|
|
|
} else {
|
2011-09-11 21:01:24 +02:00
|
|
|
remove_file(tns.cnt, false);
|
2011-11-01 08:36:20 +01:00
|
|
|
if (tns.sel >= tns.cnt)
|
|
|
|
tns.sel--;
|
|
|
|
}
|
2011-09-03 17:01:39 +02:00
|
|
|
if (tns.cnt == filecnt)
|
2011-09-02 15:56:03 +02:00
|
|
|
redraw();
|
2011-09-03 17:01:39 +02:00
|
|
|
else
|
2011-09-02 15:56:03 +02:00
|
|
|
check_timeouts(NULL);
|
|
|
|
}
|
2011-09-02 04:33:44 +02:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
|
2011-09-02 18:54:41 +02:00
|
|
|
/* wait for timeouts */
|
2011-09-02 15:56:03 +02:00
|
|
|
xfd = ConnectionNumber(win.env.dpy);
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(xfd, &fds);
|
2011-09-02 18:54:41 +02:00
|
|
|
select(xfd + 1, &fds, 0, 0, &timeout);
|
2011-08-19 15:02:10 +02:00
|
|
|
}
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
XNextEvent(win.env.dpy, &ev);
|
|
|
|
switch (ev.type) {
|
2011-08-19 15:02:10 +02:00
|
|
|
/* handle events */
|
2011-09-29 12:43:36 +02:00
|
|
|
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)) {
|
|
|
|
set_timeout(redraw, TO_REDRAW_RESIZE, false);
|
|
|
|
if (mode == MODE_IMAGE)
|
|
|
|
img.checkpan = true;
|
|
|
|
else
|
|
|
|
tns.dirty = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KeyPress:
|
|
|
|
on_keypress(&ev.xkey);
|
|
|
|
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 15:02:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-08 10:23:42 +02:00
|
|
|
int fncmp(const void *a, const void *b) {
|
2011-08-18 00:38:55 +02:00
|
|
|
return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
|
2011-04-08 10:23:42 +02:00
|
|
|
}
|
|
|
|
|
2011-01-17 14:57:59 +01:00
|
|
|
int main(int argc, char **argv) {
|
2011-09-08 20:54:24 +02:00
|
|
|
int i, start;
|
2011-05-29 11:45:58 +02:00
|
|
|
size_t n;
|
2011-09-08 20:54:24 +02:00
|
|
|
ssize_t len;
|
2011-08-18 00:38:55 +02:00
|
|
|
char *filename;
|
2011-02-02 09:01:05 +01:00
|
|
|
struct stat fstats;
|
2011-04-08 10:23:42 +02:00
|
|
|
r_dir_t dir;
|
2011-01-20 16:24:48 +01:00
|
|
|
|
2011-01-19 18:16:44 +01:00
|
|
|
parse_options(argc, argv);
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-04-08 14:44:00 +02:00
|
|
|
if (options->clean_cache) {
|
2011-09-17 17:23:51 +02:00
|
|
|
tns_init(&tns, 0, NULL);
|
2011-04-11 16:58:38 +02:00
|
|
|
tns_clean_cache(&tns);
|
2011-09-26 15:40:07 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-04-08 14:44:00 +02:00
|
|
|
}
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (options->filecnt == 0) {
|
2011-01-19 14:07:45 +01:00
|
|
|
print_usage();
|
2011-09-26 15:40:07 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2011-01-19 14:07:45 +01:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:51:04 +01:00
|
|
|
if (options->recursive || options->from_stdin)
|
2012-02-12 19:00:41 +01:00
|
|
|
filecnt = FILENAME_CNT;
|
2011-02-02 09:01:05 +01:00
|
|
|
else
|
|
|
|
filecnt = options->filecnt;
|
|
|
|
|
2011-08-18 00:38:55 +02:00
|
|
|
files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
|
2011-01-19 14:07:45 +01:00
|
|
|
fileidx = 0;
|
2011-01-20 16:24:48 +01:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
/* build file list: */
|
2011-02-14 17:51:04 +01:00
|
|
|
if (options->from_stdin) {
|
2011-08-18 00:38:55 +02:00
|
|
|
filename = NULL;
|
2011-09-08 20:54:24 +02:00
|
|
|
while ((len = get_line(&filename, &n, stdin)) > 0) {
|
2011-05-29 11:45:58 +02:00
|
|
|
if (filename[len-1] == '\n')
|
|
|
|
filename[len-1] = '\0';
|
2011-08-18 00:38:55 +02:00
|
|
|
check_add_file(filename);
|
2011-02-14 17:51:04 +01:00
|
|
|
}
|
2011-09-29 12:43:36 +02:00
|
|
|
if (filename != NULL)
|
2011-09-08 20:54:24 +02:00
|
|
|
free(filename);
|
2011-02-14 17:51:04 +01:00
|
|
|
} else {
|
2011-07-26 18:01:29 +02:00
|
|
|
for (i = 0; i < options->filecnt; i++) {
|
2011-02-14 17:51:04 +01:00
|
|
|
filename = options->filenames[i];
|
2011-04-08 10:23:42 +02:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (stat(filename, &fstats) < 0) {
|
|
|
|
warn("could not stat file: %s", filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(fstats.st_mode)) {
|
2011-07-26 18:01:29 +02:00
|
|
|
check_add_file(filename);
|
2011-04-08 10:23:42 +02:00
|
|
|
} else {
|
|
|
|
if (!options->recursive) {
|
2011-02-14 17:51:04 +01:00
|
|
|
warn("ignoring directory: %s", filename);
|
2011-04-08 10:23:42 +02:00
|
|
|
continue;
|
2011-04-07 19:15:00 +02:00
|
|
|
}
|
2011-09-29 12:43:36 +02:00
|
|
|
if (r_opendir(&dir, filename) < 0) {
|
2011-04-08 10:23:42 +02:00
|
|
|
warn("could not open directory: %s", filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
start = fileidx;
|
2011-09-29 12:43:36 +02:00
|
|
|
while ((filename = r_readdir(&dir)) != NULL) {
|
2011-08-18 00:38:55 +02:00
|
|
|
check_add_file(filename);
|
|
|
|
free((void*) filename);
|
2011-04-08 10:23:42 +02:00
|
|
|
}
|
|
|
|
r_closedir(&dir);
|
|
|
|
if (fileidx - start > 1)
|
2011-08-18 00:38:55 +02:00
|
|
|
qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
|
2011-02-14 17:51:04 +01:00
|
|
|
}
|
2011-02-02 09:01:05 +01:00
|
|
|
}
|
2011-01-20 16:24:48 +01:00
|
|
|
}
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (fileidx == 0) {
|
2011-04-11 16:58:38 +02:00
|
|
|
fprintf(stderr, "sxiv: no valid image file given, aborting\n");
|
2011-09-26 15:40:07 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2011-01-20 16:24:48 +01:00
|
|
|
}
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-05-25 09:23:23 +02:00
|
|
|
filecnt = fileidx;
|
2011-06-28 13:45:57 +02:00
|
|
|
fileidx = options->startnum < filecnt ? options->startnum : 0;
|
2011-05-25 09:23:23 +02:00
|
|
|
|
2011-04-11 16:58:38 +02:00
|
|
|
win_init(&win);
|
2011-01-21 12:57:35 +01:00
|
|
|
img_init(&img, &win);
|
2011-01-17 16:41:50 +01:00
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
if (options->thumb_mode) {
|
2011-08-19 13:09:22 +02:00
|
|
|
mode = MODE_THUMB;
|
2011-09-17 17:23:51 +02:00
|
|
|
tns_init(&tns, filecnt, &win);
|
2011-09-11 21:01:24 +02:00
|
|
|
while (!tns_load(&tns, 0, &files[0], false, false))
|
|
|
|
remove_file(0, false);
|
2011-04-11 16:58:38 +02:00
|
|
|
tns.cnt = 1;
|
2011-02-16 17:09:46 +01:00
|
|
|
} else {
|
2011-08-19 13:09:22 +02:00
|
|
|
mode = MODE_IMAGE;
|
2011-02-21 14:59:29 +01:00
|
|
|
tns.thumbs = NULL;
|
2011-02-27 13:29:24 +01:00
|
|
|
load_image(fileidx);
|
2011-02-16 17:09:46 +01:00
|
|
|
}
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2011-04-11 16:58:38 +02:00
|
|
|
win_open(&win);
|
2011-09-29 12:43:36 +02:00
|
|
|
|
2011-01-19 14:07:45 +01:00
|
|
|
run();
|
|
|
|
cleanup();
|
2011-01-17 16:41:50 +01:00
|
|
|
|
2011-01-17 14:57:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|